xref: /trunk/main/oox/source/core/relations.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/core/relations.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
27cdf0e10cSrcweir #include "oox/helper/helper.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace oox {
30cdf0e10cSrcweir namespace core {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ============================================================================
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using ::rtl::OUString;
35cdf0e10cSrcweir using ::rtl::OUStringBuffer;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // ============================================================================
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace {
40cdf0e10cSrcweir 
lclRemoveFileName(const OUString & rPath)41cdf0e10cSrcweir OUString lclRemoveFileName( const OUString& rPath )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     return rPath.copy( 0, ::std::max< sal_Int32 >( rPath.lastIndexOf( '/' ), 0 ) );
44cdf0e10cSrcweir }
45cdf0e10cSrcweir 
lclAppendFileName(const OUString & rPath,const OUString & rFileName)46cdf0e10cSrcweir OUString lclAppendFileName( const OUString& rPath, const OUString& rFileName )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     return (rPath.getLength() == 0) ? rFileName :
49cdf0e10cSrcweir         OUStringBuffer( rPath ).append( sal_Unicode( '/' ) ).append( rFileName ).makeStringAndClear();
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir } // namespace
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // ============================================================================
55cdf0e10cSrcweir 
Relations(const OUString & rFragmentPath)56cdf0e10cSrcweir Relations::Relations( const OUString& rFragmentPath ) :
57cdf0e10cSrcweir     maFragmentPath( rFragmentPath )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
getRelationFromRelId(const OUString & rId) const61cdf0e10cSrcweir const Relation* Relations::getRelationFromRelId( const OUString& rId ) const
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     const_iterator aIt = find( rId );
64cdf0e10cSrcweir     return (aIt == end()) ? 0 : &aIt->second;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
getRelationFromFirstType(const OUString & rType) const67cdf0e10cSrcweir const Relation* Relations::getRelationFromFirstType( const OUString& rType ) const
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
70cdf0e10cSrcweir         if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
71cdf0e10cSrcweir             return &aIt->second;
72cdf0e10cSrcweir     return 0;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
getRelationsFromType(const OUString & rType) const75cdf0e10cSrcweir RelationsRef Relations::getRelationsFromType( const OUString& rType ) const
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     RelationsRef xRelations( new Relations( maFragmentPath ) );
78cdf0e10cSrcweir     for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
79cdf0e10cSrcweir         if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
80cdf0e10cSrcweir             (*xRelations)[ aIt->first ] = aIt->second;
81cdf0e10cSrcweir     return xRelations;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
getExternalTargetFromRelId(const OUString & rRelId) const84cdf0e10cSrcweir OUString Relations::getExternalTargetFromRelId( const OUString& rRelId ) const
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     const Relation* pRelation = getRelationFromRelId( rRelId );
87cdf0e10cSrcweir     return (pRelation && pRelation->mbExternal) ? pRelation->maTarget : OUString();
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
getExternalTargetFromFirstType(const OUString & rType) const90cdf0e10cSrcweir OUString Relations::getExternalTargetFromFirstType( const OUString& rType ) const
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     const Relation* pRelation = getRelationFromFirstType( rType );
93cdf0e10cSrcweir     return (pRelation && pRelation->mbExternal) ? pRelation->maTarget : OUString();
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
getFragmentPathFromRelation(const Relation & rRelation) const96cdf0e10cSrcweir OUString Relations::getFragmentPathFromRelation( const Relation& rRelation ) const
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     // no target, no fragment path
99cdf0e10cSrcweir     if( rRelation.mbExternal || (rRelation.maTarget.getLength() == 0) )
100cdf0e10cSrcweir         return OUString();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     // absolute target: return it without leading slash (#i100978)
103cdf0e10cSrcweir     if( rRelation.maTarget[ 0 ] == '/' )
104cdf0e10cSrcweir         return rRelation.maTarget.copy( 1 );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     // empty fragment path: return target
107cdf0e10cSrcweir     if( maFragmentPath.getLength() == 0 )
108cdf0e10cSrcweir         return rRelation.maTarget;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     // resolve relative target path according to base path
111cdf0e10cSrcweir     OUString aPath = lclRemoveFileName( maFragmentPath );
112cdf0e10cSrcweir     sal_Int32 nStartPos = 0;
113cdf0e10cSrcweir     while( nStartPos < rRelation.maTarget.getLength() )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         sal_Int32 nSepPos = rRelation.maTarget.indexOf( '/', nStartPos );
116cdf0e10cSrcweir         if( nSepPos < 0 ) nSepPos = rRelation.maTarget.getLength();
117cdf0e10cSrcweir         // append next directory name from aTarget to aPath, or remove last directory on '../'
118cdf0e10cSrcweir         if( (nStartPos + 2 == nSepPos) && (rRelation.maTarget[ nStartPos ] == '.') && (rRelation.maTarget[ nStartPos + 1 ] == '.') )
119cdf0e10cSrcweir             aPath = lclRemoveFileName( aPath );
120cdf0e10cSrcweir         else
121cdf0e10cSrcweir             aPath = lclAppendFileName( aPath, rRelation.maTarget.copy( nStartPos, nSepPos - nStartPos ) );
122cdf0e10cSrcweir         // move nStartPos to next directory name
123cdf0e10cSrcweir         nStartPos = nSepPos + 1;
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     return aPath;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
getFragmentPathFromRelId(const OUString & rRelId) const129cdf0e10cSrcweir OUString Relations::getFragmentPathFromRelId( const OUString& rRelId ) const
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     const Relation* pRelation = getRelationFromRelId( rRelId );
132cdf0e10cSrcweir     return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
getFragmentPathFromFirstType(const OUString & rType) const135cdf0e10cSrcweir OUString Relations::getFragmentPathFromFirstType( const OUString& rType ) const
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     const Relation* pRelation = getRelationFromFirstType( rType );
138cdf0e10cSrcweir     return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir // ============================================================================
142cdf0e10cSrcweir 
143cdf0e10cSrcweir } // namespace core
144cdf0e10cSrcweir } // namespace oox
145