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