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