Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "oox/core/relations.hxx"
21 :
22 : #include <rtl/ustrbuf.hxx>
23 : #include "oox/helper/helper.hxx"
24 :
25 : namespace oox {
26 : namespace core {
27 :
28 : // ============================================================================
29 :
30 : using ::rtl::OUString;
31 : using ::rtl::OUStringBuffer;
32 :
33 : // ============================================================================
34 :
35 : namespace {
36 :
37 129 : OUString lclRemoveFileName( const OUString& rPath )
38 : {
39 129 : return rPath.copy( 0, ::std::max< sal_Int32 >( rPath.lastIndexOf( '/' ), 0 ) );
40 : }
41 :
42 188 : OUString lclAppendFileName( const OUString& rPath, const OUString& rFileName )
43 : {
44 188 : return rPath.isEmpty() ? rFileName :
45 188 : OUStringBuffer( rPath ).append( sal_Unicode( '/' ) ).append( rFileName ).makeStringAndClear();
46 : }
47 :
48 : } // namespace
49 :
50 : // ============================================================================
51 :
52 201 : Relations::Relations( const OUString& rFragmentPath ) :
53 201 : maFragmentPath( rFragmentPath )
54 : {
55 201 : }
56 :
57 80 : const Relation* Relations::getRelationFromRelId( const OUString& rId ) const
58 : {
59 80 : const_iterator aIt = find( rId );
60 80 : return (aIt == end()) ? 0 : &aIt->second;
61 : }
62 :
63 131 : const Relation* Relations::getRelationFromFirstType( const OUString& rType ) const
64 : {
65 420 : for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
66 347 : if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
67 58 : return &aIt->second;
68 73 : return 0;
69 : }
70 :
71 75 : RelationsRef Relations::getRelationsFromType( const OUString& rType ) const
72 : {
73 75 : RelationsRef xRelations( new Relations( maFragmentPath ) );
74 102 : for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
75 27 : if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
76 0 : (*xRelations)[ aIt->first ] = aIt->second;
77 75 : return xRelations;
78 : }
79 :
80 0 : OUString Relations::getExternalTargetFromRelId( const OUString& rRelId ) const
81 : {
82 0 : const Relation* pRelation = getRelationFromRelId( rRelId );
83 0 : return (pRelation && pRelation->mbExternal) ? pRelation->maTarget : OUString();
84 : }
85 :
86 0 : OUString Relations::getInternalTargetFromRelId( const OUString& rRelId ) const
87 : {
88 0 : const Relation* pRelation = getRelationFromRelId( rRelId );
89 0 : return (pRelation && !pRelation->mbExternal) ? pRelation->maTarget : OUString();
90 : }
91 :
92 121 : OUString Relations::getFragmentPathFromRelation( const Relation& rRelation ) const
93 : {
94 : // no target, no fragment path
95 121 : if( rRelation.mbExternal || rRelation.maTarget.isEmpty() )
96 0 : return OUString();
97 :
98 : // absolute target: return it without leading slash (#i100978)
99 121 : if( rRelation.maTarget[ 0 ] == '/' )
100 0 : return rRelation.maTarget.copy( 1 );
101 :
102 : // empty fragment path: return target
103 121 : if( maFragmentPath.isEmpty() )
104 15 : return rRelation.maTarget;
105 :
106 : // resolve relative target path according to base path
107 106 : OUString aPath = lclRemoveFileName( maFragmentPath );
108 106 : sal_Int32 nStartPos = 0;
109 423 : while( nStartPos < rRelation.maTarget.getLength() )
110 : {
111 211 : sal_Int32 nSepPos = rRelation.maTarget.indexOf( '/', nStartPos );
112 211 : if( nSepPos < 0 ) nSepPos = rRelation.maTarget.getLength();
113 : // append next directory name from aTarget to aPath, or remove last directory on '../'
114 211 : if( (nStartPos + 2 == nSepPos) && (rRelation.maTarget[ nStartPos ] == '.') && (rRelation.maTarget[ nStartPos + 1 ] == '.') )
115 23 : aPath = lclRemoveFileName( aPath );
116 : else
117 188 : aPath = lclAppendFileName( aPath, rRelation.maTarget.copy( nStartPos, nSepPos - nStartPos ) );
118 : // move nStartPos to next directory name
119 211 : nStartPos = nSepPos + 1;
120 : }
121 :
122 106 : return aPath;
123 : }
124 :
125 55 : OUString Relations::getFragmentPathFromRelId( const OUString& rRelId ) const
126 : {
127 55 : const Relation* pRelation = getRelationFromRelId( rRelId );
128 55 : return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
129 : }
130 :
131 131 : OUString Relations::getFragmentPathFromFirstType( const OUString& rType ) const
132 : {
133 131 : const Relation* pRelation = getRelationFromFirstType( rType );
134 131 : return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
135 : }
136 :
137 : // ============================================================================
138 :
139 : } // namespace core
140 : } // namespace oox
141 :
142 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|