update dev300-m58
[ooovba.git] / oox / source / core / relationshandler.cxx
blob9ca0378cd95fa037e98f4eab2ab8c147a05ff3fc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: relationshandler.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/core/relationshandler.hxx"
32 #include <rtl/ustrbuf.hxx>
33 #include "tokens.hxx"
34 #include "oox/helper/attributelist.hxx"
35 #include "oox/core/namespaces.hxx"
37 using ::rtl::OUString;
38 using ::rtl::OUStringBuffer;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::xml::sax;
42 namespace oox {
43 namespace core {
45 // ============================================================================
47 namespace {
49 /* Build path to relations file from passed fragment path, e.g.:
50 'path/path/file.xml' -> 'path/path/_rels/file.xml.rels'
51 'file.xml' -> '_rels/file.xml.rels'
52 '' -> '_rels/.rels'
54 OUString lclGetRelationsPath( const OUString& rFragmentPath )
56 sal_Int32 nPathLen = ::std::max< sal_Int32 >( rFragmentPath.lastIndexOf( '/' ) + 1, 0 );
57 return
58 OUStringBuffer( rFragmentPath.copy( 0, nPathLen ) ). // file path including slash
59 appendAscii( "_rels/" ). // additional '_rels/' path
60 append( rFragmentPath.copy( nPathLen ) ). // file name after path
61 appendAscii( ".rels" ). // '.rels' suffix
62 makeStringAndClear();
65 } // namespace
67 // ============================================================================
69 RelationsFragment::RelationsFragment( XmlFilterBase& rFilter, RelationsRef xRelations ) :
70 FragmentHandler( rFilter, lclGetRelationsPath( xRelations->getFragmentPath() ), xRelations ),
71 mxRelations( xRelations )
75 Reference< XFastContextHandler > RelationsFragment::createFastChildContext(
76 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw (SAXException, RuntimeException)
78 Reference< XFastContextHandler > xRet;
79 AttributeList aAttribs( rxAttribs );
80 switch( nElement )
82 case NMSP_PACKAGE_RELATIONSHIPS|XML_Relationship:
84 Relation aRelation;
85 aRelation.maId = aAttribs.getString( XML_Id, OUString() );
86 aRelation.maType = aAttribs.getString( XML_Type, OUString() );
87 aRelation.maTarget = aAttribs.getString( XML_Target, OUString() );
88 if( (aRelation.maId.getLength() > 0) && (aRelation.maType.getLength() > 0) && (aRelation.maTarget.getLength() > 0) )
90 sal_Int32 nTargetMode = aAttribs.getToken( XML_TargetMode, XML_Internal );
91 OSL_ENSURE( (nTargetMode == XML_Internal) || (nTargetMode == XML_External),
92 "RelationsFragment::createFastChildContext - unexpected target mode, assuming external" );
93 aRelation.mbExternal = nTargetMode != XML_Internal;
95 OSL_ENSURE( mxRelations->count( aRelation.maId ) == 0,
96 "RelationsFragment::createFastChildContext - relation identifier exists already" );
97 mxRelations->insert( Relations::value_type( aRelation.maId, aRelation ) );
100 break;
101 case NMSP_PACKAGE_RELATIONSHIPS|XML_Relationships:
102 xRet = getFastContextHandler();
103 break;
105 return xRet;
108 // ============================================================================
110 } // namespace core
111 } // namespace oox