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