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