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 : namespace {
29 :
30 9318 : OUString lclRemoveFileName( const OUString& rPath )
31 : {
32 9318 : return rPath.copy( 0, ::std::max< sal_Int32 >( rPath.lastIndexOf( '/' ), 0 ) );
33 : }
34 :
35 16024 : OUString lclAppendFileName( const OUString& rPath, const OUString& rFileName )
36 : {
37 16024 : return rPath.isEmpty() ? rFileName :
38 16024 : OUStringBuffer( rPath ).append( '/' ).append( rFileName ).makeStringAndClear();
39 : }
40 :
41 7442 : OUString createOfficeDocRelationTypeTransitional(const OUString& rType)
42 : {
43 7442 : static const OUString aTransitionalBase("http://schemas.openxmlformats.org/officeDocument/2006/relationships/");
44 7442 : return aTransitionalBase + rType;
45 : }
46 :
47 1674 : OUString createOfficeDocRelationTypeStrict(const OUString& rType)
48 : {
49 1674 : static const OUString aStrictBase("http://purl.oclc.org/ooxml/officeDocument/relationships/");
50 1674 : return aStrictBase + rType;
51 : }
52 :
53 : }
54 :
55 6828 : Relations::Relations( const OUString& rFragmentPath )
56 : : maMap()
57 6828 : , maFragmentPath( rFragmentPath )
58 : {
59 6828 : }
60 :
61 2706 : const Relation* Relations::getRelationFromRelId( const OUString& rId ) const
62 : {
63 2706 : ::std::map< OUString, Relation >::const_iterator aIt = maMap.find( rId );
64 2706 : return (aIt == maMap.end()) ? 0 : &aIt->second;
65 : }
66 :
67 8902 : const Relation* Relations::getRelationFromFirstType( const OUString& rType ) const
68 : {
69 47868 : for( ::std::map< OUString, Relation >::const_iterator aIt = maMap.begin(), aEnd = maMap.end(); aIt != aEnd; ++aIt )
70 44838 : if( aIt->second.maType.equalsIgnoreAsciiCase( rType ) )
71 5872 : return &aIt->second;
72 3030 : return 0;
73 : }
74 :
75 920 : RelationsRef Relations::getRelationsFromTypeFromOfficeDoc( const OUString& rType ) const
76 : {
77 920 : RelationsRef xRelations( new Relations( maFragmentPath ) );
78 1352 : for( ::std::map< OUString, Relation >::const_iterator aIt = maMap.begin(), aEnd = maMap.end(); aIt != aEnd; ++aIt )
79 1400 : if( aIt->second.maType.equalsIgnoreAsciiCase( createOfficeDocRelationTypeTransitional(rType) ) ||
80 968 : aIt->second.maType.equalsIgnoreAsciiCase( createOfficeDocRelationTypeStrict(rType) ))
81 164 : xRelations->maMap[ aIt->first ] = aIt->second;
82 920 : return xRelations;
83 : }
84 :
85 18 : OUString Relations::getExternalTargetFromRelId( const OUString& rRelId ) const
86 : {
87 18 : const Relation* pRelation = getRelationFromRelId( rRelId );
88 18 : return (pRelation && pRelation->mbExternal) ? pRelation->maTarget : OUString();
89 : }
90 :
91 0 : OUString Relations::getInternalTargetFromRelId( const OUString& rRelId ) const
92 : {
93 0 : const Relation* pRelation = getRelationFromRelId( rRelId );
94 0 : return (pRelation && !pRelation->mbExternal) ? pRelation->maTarget : OUString();
95 : }
96 :
97 8412 : OUString Relations::getFragmentPathFromRelation( const Relation& rRelation ) const
98 : {
99 : // no target, no fragment path
100 8412 : if( rRelation.mbExternal || rRelation.maTarget.isEmpty() )
101 18 : return OUString();
102 :
103 : // absolute target: return it without leading slash (#i100978)
104 8394 : if( rRelation.maTarget[ 0 ] == '/' )
105 0 : return rRelation.maTarget.copy( 1 );
106 :
107 : // empty fragment path: return target
108 8394 : if( maFragmentPath.isEmpty() )
109 226 : return rRelation.maTarget;
110 :
111 : // resolve relative target path according to base path
112 8168 : OUString aPath = lclRemoveFileName( maFragmentPath );
113 8168 : sal_Int32 nStartPos = 0;
114 33510 : while( nStartPos < rRelation.maTarget.getLength() )
115 : {
116 17174 : sal_Int32 nSepPos = rRelation.maTarget.indexOf( '/', nStartPos );
117 17174 : if( nSepPos < 0 ) nSepPos = rRelation.maTarget.getLength();
118 : // append next directory name from aTarget to aPath, or remove last directory on '../'
119 17174 : if( (nStartPos + 2 == nSepPos) && (rRelation.maTarget[ nStartPos ] == '.') && (rRelation.maTarget[ nStartPos + 1 ] == '.') )
120 1150 : aPath = lclRemoveFileName( aPath );
121 : else
122 16024 : aPath = lclAppendFileName( aPath, rRelation.maTarget.copy( nStartPos, nSepPos - nStartPos ) );
123 : // move nStartPos to next directory name
124 17174 : nStartPos = nSepPos + 1;
125 : }
126 :
127 8168 : return aPath;
128 : }
129 :
130 2422 : OUString Relations::getFragmentPathFromRelId( const OUString& rRelId ) const
131 : {
132 2422 : const Relation* pRelation = getRelationFromRelId( rRelId );
133 2422 : return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
134 : }
135 :
136 486 : OUString Relations::getFragmentPathFromFirstType( const OUString& rType ) const
137 : {
138 486 : const Relation* pRelation = getRelationFromFirstType( rType );
139 486 : return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
140 : }
141 :
142 7010 : OUString Relations::getFragmentPathFromFirstTypeFromOfficeDoc( const OUString& rType ) const
143 : {
144 7010 : OUString aTransitionalType(createOfficeDocRelationTypeTransitional(rType));
145 7010 : const Relation* pRelation = getRelationFromFirstType( aTransitionalType );
146 7010 : if(!pRelation)
147 : {
148 1406 : OUString aStrictType = createOfficeDocRelationTypeStrict(rType);
149 1406 : pRelation = getRelationFromFirstType( aStrictType );
150 : }
151 7010 : return pRelation ? getFragmentPathFromRelation( *pRelation ) : OUString();
152 : }
153 :
154 : } // namespace core
155 : } // namespace oox
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|