Branch libreoffice-5-0-4
[LibreOffice.git] / oox / source / core / relationshandler.cxx
blob9bcd1e39e5b7fa9bd103c992d05cf16857abf713
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "oox/core/relationshandler.hxx"
22 #include <rtl/ustrbuf.hxx>
23 #include <osl/diagnose.h>
24 #include "oox/helper/attributelist.hxx"
26 namespace oox {
27 namespace core {
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::xml::sax;
32 namespace {
34 /* Build path to relations file from passed fragment path, e.g.:
35 'path/path/file.xml' -> 'path/path/_rels/file.xml.rels'
36 'file.xml' -> '_rels/file.xml.rels'
37 '' -> '_rels/.rels'
39 OUString lclGetRelationsPath( const OUString& rFragmentPath )
41 sal_Int32 nPathLen = ::std::max< sal_Int32 >( rFragmentPath.lastIndexOf( '/' ) + 1, 0 );
42 return
43 OUStringBuffer( rFragmentPath.copy( 0, nPathLen ) ). // file path including slash
44 appendAscii( "_rels/" ). // additional '_rels/' path
45 append( rFragmentPath.copy( nPathLen ) ). // file name after path
46 appendAscii( ".rels" ). // '.rels' suffix
47 makeStringAndClear();
50 } // namespace
52 RelationsFragment::RelationsFragment( XmlFilterBase& rFilter, RelationsRef xRelations ) :
53 FragmentHandler( rFilter, lclGetRelationsPath( xRelations->getFragmentPath() ), xRelations ),
54 mxRelations( xRelations )
58 Reference< XFastContextHandler > RelationsFragment::createFastChildContext(
59 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw (SAXException, RuntimeException, std::exception)
61 Reference< XFastContextHandler > xRet;
62 AttributeList aAttribs( rxAttribs );
63 switch( nElement )
65 case PR_TOKEN( Relationship ):
67 Relation aRelation;
68 aRelation.maId = aAttribs.getString( XML_Id, OUString() );
69 aRelation.maType = aAttribs.getString( XML_Type, OUString() );
70 aRelation.maTarget = aAttribs.getString( XML_Target, OUString() );
71 if( !aRelation.maId.isEmpty() && !aRelation.maType.isEmpty() && !aRelation.maTarget.isEmpty() )
73 sal_Int32 nTargetMode = aAttribs.getToken( XML_TargetMode, XML_Internal );
74 OSL_ENSURE( (nTargetMode == XML_Internal) || (nTargetMode == XML_External),
75 "RelationsFragment::createFastChildContext - unexpected target mode, assuming external" );
76 aRelation.mbExternal = nTargetMode != XML_Internal;
78 OSL_ENSURE( mxRelations->count( aRelation.maId ) == 0,
79 "RelationsFragment::createFastChildContext - relation identifier exists already" );
80 mxRelations->insert( ::std::map< OUString, Relation >::value_type( aRelation.maId, aRelation ) );
83 break;
84 case PR_TOKEN( Relationships ):
85 xRet = getFastContextHandler();
86 break;
88 return xRet;
91 } // namespace core
92 } // namespace oox
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */