LibreOffice Module oox (master) 1
relationshandler.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 <string_view>
23
25
26#include <sal/log.hxx>
28#include <oox/token/namespaces.hxx>
29#include <oox/token/tokens.hxx>
30
31namespace oox::core {
32
33using namespace ::com::sun::star::uno;
34using namespace ::com::sun::star::xml::sax;
35
36namespace {
37
38/* Build path to relations file from passed fragment path, e.g.:
39 'path/path/file.xml' -> 'path/path/_rels/file.xml.rels'
40 'file.xml' -> '_rels/file.xml.rels'
41 '' -> '_rels/.rels'
42 */
43OUString lclGetRelationsPath( const OUString& rFragmentPath )
44{
45 sal_Int32 nPathLen = ::std::max< sal_Int32 >( rFragmentPath.lastIndexOf( '/' ) + 1, 0 );
46 return OUString::Concat(std::u16string_view(rFragmentPath.getStr(), nPathLen )) + // file path including slash
47 "_rels/" + // additional '_rels/' path
48 std::u16string_view(rFragmentPath.getStr() + nPathLen) + // file name after path
49 ".rels"; // '.rels' suffix
50}
51
52} // namespace
53
55 FragmentHandler( rFilter, lclGetRelationsPath( xRelations->getFragmentPath() ), xRelations ),
56 mxRelations( xRelations )
57{
58}
59
60Reference< XFastContextHandler > RelationsFragment::createFastChildContext(
61 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
62{
63 Reference< XFastContextHandler > xRet;
64 AttributeList aAttribs( rxAttribs );
65 switch( nElement )
66 {
67 case PR_TOKEN( Relationship ):
68 {
69 Relation aRelation;
70 aRelation.maId = aAttribs.getStringDefaulted( XML_Id);
71 aRelation.maType = aAttribs.getStringDefaulted( XML_Type);
72 aRelation.maTarget = aAttribs.getStringDefaulted( XML_Target);
73 if( !aRelation.maId.isEmpty() && !aRelation.maType.isEmpty() && !aRelation.maTarget.isEmpty() )
74 {
75 sal_Int32 nTargetMode = aAttribs.getToken( XML_TargetMode, XML_Internal );
76 SAL_WARN_IF( (nTargetMode != XML_Internal) && (nTargetMode != XML_External), "oox",
77 "RelationsFragment::createFastChildContext - unexpected target mode, assuming external" );
78 aRelation.mbExternal = nTargetMode != XML_Internal;
79
80 SAL_WARN_IF( mxRelations->count( aRelation.maId ) != 0, "oox",
81 "RelationsFragment::createFastChildContext - relation identifier exists already" );
82 mxRelations->emplace( aRelation.maId, aRelation );
83 }
84 }
85 break;
86 case PR_TOKEN( Relationships ):
87 xRet = getFastContextHandler();
88 break;
89 }
90 return xRet;
91}
92
93} // namespace oox::core
94
95/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Provides access to attribute values of an element.
OUString getStringDefaulted(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute, returns an empty string if attribute not present...
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
Returns the token identifier of the value of the specified attribute.
css::uno::Reference< css::xml::sax::XFastContextHandler > getFastContextHandler()
Returns the com.sun.star.xml.sax.XFastContextHandler interface of this context.
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &rxAttribs) override
RelationsFragment(XmlFilterBase &rFilter, const RelationsRef &xRelations)
#define SAL_WARN_IF(condition, area, stream)
std::shared_ptr< Relations > RelationsRef
Definition: relations.hxx:63
Relationship