1ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ca5ec200SAndrew Rist  * distributed with this work for additional information
6ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10ca5ec200SAndrew Rist  *
11ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ca5ec200SAndrew Rist  *
13ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18ca5ec200SAndrew Rist  * under the License.
19ca5ec200SAndrew Rist  *
20ca5ec200SAndrew Rist  *************************************************************/
21ca5ec200SAndrew Rist 
22ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/core/relationshandler.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
27cdf0e10cSrcweir #include "oox/helper/attributelist.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace oox {
30cdf0e10cSrcweir namespace core {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ============================================================================
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::com::sun::star::uno;
35cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using ::rtl::OUString;
38cdf0e10cSrcweir using ::rtl::OUStringBuffer;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir // ============================================================================
41cdf0e10cSrcweir 
42cdf0e10cSrcweir namespace {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir /*  Build path to relations file from passed fragment path, e.g.:
45cdf0e10cSrcweir         'path/path/file.xml'    -> 'path/path/_rels/file.xml.rels'
46cdf0e10cSrcweir         'file.xml'              -> '_rels/file.xml.rels'
47cdf0e10cSrcweir         ''                      -> '_rels/.rels'
48cdf0e10cSrcweir  */
lclGetRelationsPath(const OUString & rFragmentPath)49cdf0e10cSrcweir OUString lclGetRelationsPath( const OUString& rFragmentPath )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     sal_Int32 nPathLen = ::std::max< sal_Int32 >( rFragmentPath.lastIndexOf( '/' ) + 1, 0 );
52cdf0e10cSrcweir     return
53cdf0e10cSrcweir         OUStringBuffer( rFragmentPath.copy( 0, nPathLen ) ).    // file path including slash
54cdf0e10cSrcweir         appendAscii( "_rels/" ).                                // additional '_rels/' path
55cdf0e10cSrcweir         append( rFragmentPath.copy( nPathLen ) ).               // file name after path
56cdf0e10cSrcweir         appendAscii( ".rels" ).                                 // '.rels' suffix
57cdf0e10cSrcweir         makeStringAndClear();
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir } // namespace
61cdf0e10cSrcweir 
62cdf0e10cSrcweir // ============================================================================
63cdf0e10cSrcweir 
RelationsFragment(XmlFilterBase & rFilter,RelationsRef xRelations)64cdf0e10cSrcweir RelationsFragment::RelationsFragment( XmlFilterBase& rFilter, RelationsRef xRelations ) :
65cdf0e10cSrcweir     FragmentHandler( rFilter, lclGetRelationsPath( xRelations->getFragmentPath() ), xRelations ),
66cdf0e10cSrcweir     mxRelations( xRelations )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
createFastChildContext(sal_Int32 nElement,const Reference<XFastAttributeList> & rxAttribs)70cdf0e10cSrcweir Reference< XFastContextHandler > RelationsFragment::createFastChildContext(
71cdf0e10cSrcweir         sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs ) throw (SAXException, RuntimeException)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     Reference< XFastContextHandler > xRet;
74cdf0e10cSrcweir     AttributeList aAttribs( rxAttribs );
75cdf0e10cSrcweir     switch( nElement )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         case PR_TOKEN( Relationship ):
78cdf0e10cSrcweir         {
79cdf0e10cSrcweir             Relation aRelation;
80cdf0e10cSrcweir             aRelation.maId     = aAttribs.getString( XML_Id, OUString() );
81cdf0e10cSrcweir             aRelation.maType   = aAttribs.getString( XML_Type, OUString() );
82*3ff2b12aSDamjan Jovanovic             aRelation.maTarget = removeDuplicateSlashes( aAttribs.getString( XML_Target, OUString() ) );
83cdf0e10cSrcweir             if( (aRelation.maId.getLength() > 0) && (aRelation.maType.getLength() > 0) && (aRelation.maTarget.getLength() > 0) )
84cdf0e10cSrcweir             {
85cdf0e10cSrcweir                 sal_Int32 nTargetMode = aAttribs.getToken( XML_TargetMode, XML_Internal );
86cdf0e10cSrcweir                 OSL_ENSURE( (nTargetMode == XML_Internal) || (nTargetMode == XML_External),
87cdf0e10cSrcweir                     "RelationsFragment::createFastChildContext - unexpected target mode, assuming external" );
88cdf0e10cSrcweir                 aRelation.mbExternal = nTargetMode != XML_Internal;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir                 OSL_ENSURE( mxRelations->count( aRelation.maId ) == 0,
91cdf0e10cSrcweir                     "RelationsFragment::createFastChildContext - relation identifier exists already" );
92cdf0e10cSrcweir                 mxRelations->insert( Relations::value_type( aRelation.maId, aRelation ) );
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir         break;
96cdf0e10cSrcweir         case PR_TOKEN( Relationships ):
97cdf0e10cSrcweir             xRet = getFastContextHandler();
98cdf0e10cSrcweir         break;
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir     return xRet;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
removeDuplicateSlashes(const OUString & path)103*3ff2b12aSDamjan Jovanovic OUString RelationsFragment::removeDuplicateSlashes( const OUString &path )
104*3ff2b12aSDamjan Jovanovic {
105*3ff2b12aSDamjan Jovanovic     rtl::OUStringBuffer buffer;
106*3ff2b12aSDamjan Jovanovic     bool hadSlash = false;
107*3ff2b12aSDamjan Jovanovic     for ( sal_Int32 i = 0; i < path.getLength(); i++ )
108*3ff2b12aSDamjan Jovanovic     {
109*3ff2b12aSDamjan Jovanovic         sal_Unicode ch = path[i];
110*3ff2b12aSDamjan Jovanovic         if ( ch == '/' )
111*3ff2b12aSDamjan Jovanovic         {
112*3ff2b12aSDamjan Jovanovic             if ( !hadSlash )
113*3ff2b12aSDamjan Jovanovic             {
114*3ff2b12aSDamjan Jovanovic                 hadSlash = true;
115*3ff2b12aSDamjan Jovanovic                 buffer.append( sal_Unicode( '/' ) );
116*3ff2b12aSDamjan Jovanovic             }
117*3ff2b12aSDamjan Jovanovic         }
118*3ff2b12aSDamjan Jovanovic         else
119*3ff2b12aSDamjan Jovanovic         {
120*3ff2b12aSDamjan Jovanovic             hadSlash = false;
121*3ff2b12aSDamjan Jovanovic             buffer.append( ch );
122*3ff2b12aSDamjan Jovanovic         }
123*3ff2b12aSDamjan Jovanovic     }
124*3ff2b12aSDamjan Jovanovic     return buffer.makeStringAndClear();
125*3ff2b12aSDamjan Jovanovic }
126*3ff2b12aSDamjan Jovanovic 
127cdf0e10cSrcweir // ============================================================================
128cdf0e10cSrcweir 
129cdf0e10cSrcweir } // namespace core
130cdf0e10cSrcweir } // namespace oox
131