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