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