xref: /aoo42x/main/oox/source/core/relations.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/core/relations.hxx"
29 
30 #include <rtl/ustrbuf.hxx>
31 #include "oox/helper/helper.hxx"
32 
33 namespace oox {
34 namespace core {
35 
36 // ============================================================================
37 
38 using ::rtl::OUString;
39 using ::rtl::OUStringBuffer;
40 
41 // ============================================================================
42 
43 namespace {
44 
45 OUString lclRemoveFileName( const OUString& rPath )
46 {
47     return rPath.copy( 0, ::std::max< sal_Int32 >( rPath.lastIndexOf( '/' ), 0 ) );
48 }
49 
50 OUString lclAppendFileName( const OUString& rPath, const OUString& rFileName )
51 {
52     return (rPath.getLength() == 0) ? rFileName :
53         OUStringBuffer( rPath ).append( sal_Unicode( '/' ) ).append( rFileName ).makeStringAndClear();
54 }
55 
56 } // namespace
57 
58 // ============================================================================
59 
60 Relations::Relations( const OUString& rFragmentPath ) :
61     maFragmentPath( rFragmentPath )
62 {
63 }
64 
65 const Relation* Relations::getRelationFromRelId( const OUString& rId ) const
66 {
67     const_iterator aIt = find( rId );
68     return (aIt == end()) ? 0 : &aIt->second;
69 }
70 
71 const Relation* Relations::getRelationFromFirstType( const OUString& rType ) const
72 {
73     for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
74         if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
75             return &aIt->second;
76     return 0;
77 }
78 
79 RelationsRef Relations::getRelationsFromType( const OUString& rType ) const
80 {
81     RelationsRef xRelations( new Relations( maFragmentPath ) );
82     for( const_iterator aIt = begin(), aEnd = end(); aIt != aEnd; ++aIt )
83         if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
84             (*xRelations)[ aIt->first ] = aIt->second;
85     return xRelations;
86 }
87 
88 OUString Relations::getExternalTargetFromRelId( const OUString& rRelId ) const
89 {
90     const Relation* pRelation = getRelationFromRelId( rRelId );
91     return (pRelation && pRelation->mbExternal) ? pRelation->maTarget : OUString();
92 }
93 
94 OUString Relations::getExternalTargetFromFirstType( const OUString& rType ) const
95 {
96     const Relation* pRelation = getRelationFromFirstType( rType );
97     return (pRelation && pRelation->mbExternal) ? pRelation->maTarget : OUString();
98 }
99 
100 OUString Relations::getFragmentPathFromRelation( const Relation& rRelation ) const
101 {
102     // no target, no fragment path
103     if( rRelation.mbExternal || (rRelation.maTarget.getLength() == 0) )
104         return OUString();
105 
106     // absolute target: return it without leading slash (#i100978)
107     if( rRelation.maTarget[ 0 ] == '/' )
108         return rRelation.maTarget.copy( 1 );
109 
110     // empty fragment path: return target
111     if( maFragmentPath.getLength() == 0 )
112         return rRelation.maTarget;
113 
114     // resolve relative target path according to base path
115     OUString aPath = lclRemoveFileName( maFragmentPath );
116     sal_Int32 nStartPos = 0;
117     while( nStartPos < rRelation.maTarget.getLength() )
118     {
119         sal_Int32 nSepPos = rRelation.maTarget.indexOf( '/', nStartPos );
120         if( nSepPos < 0 ) nSepPos = rRelation.maTarget.getLength();
121         // append next directory name from aTarget to aPath, or remove last directory on '../'
122         if( (nStartPos + 2 == nSepPos) && (rRelation.maTarget[ nStartPos ] == '.') && (rRelation.maTarget[ nStartPos + 1 ] == '.') )
123             aPath = lclRemoveFileName( aPath );
124         else
125             aPath = lclAppendFileName( aPath, rRelation.maTarget.copy( nStartPos, nSepPos - nStartPos ) );
126         // move nStartPos to next directory name
127         nStartPos = nSepPos + 1;
128     }
129 
130     return aPath;
131 }
132 
133 OUString Relations::getFragmentPathFromRelId( const OUString& rRelId ) const
134 {
135     const Relation* pRelation = getRelationFromRelId( rRelId );
136     return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
137 }
138 
139 OUString Relations::getFragmentPathFromFirstType( const OUString& rType ) const
140 {
141     const Relation* pRelation = getRelationFromFirstType( rType );
142     return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
143 }
144 
145 // ============================================================================
146 
147 } // namespace core
148 } // namespace oox
149