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: ofopxmlhelper.cxx,v $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
35 #include <com/sun/star/beans/StringPair.hpp>
37 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP
41 #include <com/sun/star/io/XActiveDataSource.hpp>
43 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP
44 #include <com/sun/star/xml/sax/XParser.hpp>
46 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP
47 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
49 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP
50 #include <com/sun/star/lang/IllegalArgumentException.hpp>
53 #include <comphelper/ofopxmlhelper.hxx>
54 #include <comphelper/attributelist.hxx>
56 #define RELATIONINFO_FORMAT 0
57 #define CONTENTTYPE_FORMAT 1
58 #define FORMAT_MAX_ID CONTENTTYPE_FORMAT
60 using namespace ::com::sun::star
;
62 namespace comphelper
{
64 // -----------------------------------
65 uno::Sequence
< uno::Sequence
< beans::StringPair
> > SAL_CALL
OFOPXMLHelper::ReadRelationsInfoSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const ::rtl::OUString aStreamName
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
66 throw( uno::Exception
)
68 ::rtl::OUString aStringID
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_rels/" ) );
69 aStringID
+= aStreamName
;
70 return ReadSequence_Impl( xInStream
, aStringID
, RELATIONINFO_FORMAT
, xFactory
);
73 // -----------------------------------
74 uno::Sequence
< uno::Sequence
< beans::StringPair
> > SAL_CALL
OFOPXMLHelper::ReadContentTypeSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
75 throw( uno::Exception
)
77 ::rtl::OUString aStringID
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "[Content_Types].xml" ) );
78 return ReadSequence_Impl( xInStream
, aStringID
, CONTENTTYPE_FORMAT
, xFactory
);
81 // -----------------------------------
82 void SAL_CALL
OFOPXMLHelper::WriteRelationsInfoSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const uno::Sequence
< uno::Sequence
< beans::StringPair
> >& aSequence
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
83 throw( uno::Exception
)
85 if ( !xOutStream
.is() )
86 throw uno::RuntimeException();
88 uno::Reference
< io::XActiveDataSource
> xWriterSource(
89 xFactory
->createInstance(
90 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
91 uno::UNO_QUERY_THROW
);
92 uno::Reference
< xml::sax::XDocumentHandler
> xWriterHandler( xWriterSource
, uno::UNO_QUERY_THROW
);
94 xWriterSource
->setOutputStream( xOutStream
);
96 ::rtl::OUString
aRelListElement( RTL_CONSTASCII_USTRINGPARAM( "Relationships" ) );
97 ::rtl::OUString
aRelElement( RTL_CONSTASCII_USTRINGPARAM( "Relationship" ) );
98 ::rtl::OUString
aIDAttr( RTL_CONSTASCII_USTRINGPARAM( "Id" ) );
99 ::rtl::OUString
aTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "Type" ) );
100 ::rtl::OUString
aTargetModeAttr( RTL_CONSTASCII_USTRINGPARAM( "TargetMode" ) );
101 ::rtl::OUString
aTargetAttr( RTL_CONSTASCII_USTRINGPARAM( "Target" ) );
102 ::rtl::OUString
aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
103 ::rtl::OUString
aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
105 // write the namespace
106 AttributeList
* pRootAttrList
= new AttributeList
;
107 uno::Reference
< xml::sax::XAttributeList
> xRootAttrList( pRootAttrList
);
108 pRootAttrList
->AddAttribute(
109 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
111 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://schemas.openxmlformats.org/package/2006/relationships" ) ) );
113 xWriterHandler
->startDocument();
114 xWriterHandler
->startElement( aRelListElement
, xRootAttrList
);
116 for ( sal_Int32 nInd
= 0; nInd
< aSequence
.getLength(); nInd
++ )
118 AttributeList
*pAttrList
= new AttributeList
;
119 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
120 for( sal_Int32 nSecInd
= 0; nSecInd
< aSequence
[nInd
].getLength(); nSecInd
++ )
122 if ( aSequence
[nInd
][nSecInd
].First
.equals( aIDAttr
)
123 || aSequence
[nInd
][nSecInd
].First
.equals( aTypeAttr
)
124 || aSequence
[nInd
][nSecInd
].First
.equals( aTargetModeAttr
)
125 || aSequence
[nInd
][nSecInd
].First
.equals( aTargetAttr
) )
127 pAttrList
->AddAttribute( aSequence
[nInd
][nSecInd
].First
, aCDATAString
, aSequence
[nInd
][nSecInd
].Second
);
131 // TODO/LATER: should the extensions be allowed?
132 throw lang::IllegalArgumentException();
136 xWriterHandler
->startElement( aRelElement
, xAttrList
);
137 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
138 xWriterHandler
->endElement( aRelElement
);
141 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
142 xWriterHandler
->endElement( aRelListElement
);
143 xWriterHandler
->endDocument();
146 // -----------------------------------
147 void SAL_CALL
OFOPXMLHelper::WriteContentSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const uno::Sequence
< beans::StringPair
>& aDefaultsSequence
, const uno::Sequence
< beans::StringPair
>& aOverridesSequence
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
148 throw( uno::Exception
)
150 if ( !xOutStream
.is() )
151 throw uno::RuntimeException();
153 uno::Reference
< io::XActiveDataSource
> xWriterSource(
154 xFactory
->createInstance(
155 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
156 uno::UNO_QUERY_THROW
);
157 uno::Reference
< xml::sax::XDocumentHandler
> xWriterHandler( xWriterSource
, uno::UNO_QUERY_THROW
);
159 xWriterSource
->setOutputStream( xOutStream
);
161 ::rtl::OUString
aTypesElement( RTL_CONSTASCII_USTRINGPARAM( "Types" ) );
162 ::rtl::OUString
aDefaultElement( RTL_CONSTASCII_USTRINGPARAM( "Default" ) );
163 ::rtl::OUString
aOverrideElement( RTL_CONSTASCII_USTRINGPARAM( "Override" ) );
164 ::rtl::OUString
aExtensionAttr( RTL_CONSTASCII_USTRINGPARAM( "Extension" ) );
165 ::rtl::OUString
aPartNameAttr( RTL_CONSTASCII_USTRINGPARAM( "PartName" ) );
166 ::rtl::OUString
aContentTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "ContentType" ) );
167 ::rtl::OUString
aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
168 ::rtl::OUString
aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
170 // write the namespace
171 AttributeList
* pRootAttrList
= new AttributeList
;
172 uno::Reference
< xml::sax::XAttributeList
> xRootAttrList( pRootAttrList
);
173 pRootAttrList
->AddAttribute(
174 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
176 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://schemas.openxmlformats.org/package/2006/content-types" ) ) );
178 xWriterHandler
->startDocument();
179 xWriterHandler
->startElement( aTypesElement
, xRootAttrList
);
181 for ( sal_Int32 nInd
= 0; nInd
< aDefaultsSequence
.getLength(); nInd
++ )
183 AttributeList
*pAttrList
= new AttributeList
;
184 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
185 pAttrList
->AddAttribute( aExtensionAttr
, aCDATAString
, aDefaultsSequence
[nInd
].First
);
186 pAttrList
->AddAttribute( aContentTypeAttr
, aCDATAString
, aDefaultsSequence
[nInd
].Second
);
188 xWriterHandler
->startElement( aDefaultElement
, xAttrList
);
189 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
190 xWriterHandler
->endElement( aDefaultElement
);
193 for ( sal_Int32 nInd
= 0; nInd
< aOverridesSequence
.getLength(); nInd
++ )
195 AttributeList
*pAttrList
= new AttributeList
;
196 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
197 pAttrList
->AddAttribute( aPartNameAttr
, aCDATAString
, aOverridesSequence
[nInd
].First
);
198 pAttrList
->AddAttribute( aContentTypeAttr
, aCDATAString
, aOverridesSequence
[nInd
].Second
);
200 xWriterHandler
->startElement( aOverrideElement
, xAttrList
);
201 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
202 xWriterHandler
->endElement( aOverrideElement
);
205 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
206 xWriterHandler
->endElement( aTypesElement
);
207 xWriterHandler
->endDocument();
211 // ==================================================================================
213 // -----------------------------------
214 uno::Sequence
< uno::Sequence
< beans::StringPair
> > SAL_CALL
OFOPXMLHelper::ReadSequence_Impl( const uno::Reference
< io::XInputStream
>& xInStream
, const ::rtl::OUString
& aStringID
, sal_uInt16 nFormat
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
215 throw( uno::Exception
)
217 if ( !xFactory
.is() || !xInStream
.is() || nFormat
> FORMAT_MAX_ID
)
218 throw uno::RuntimeException();
220 uno::Sequence
< uno::Sequence
< beans::StringPair
> > aResult
;
222 uno::Reference
< xml::sax::XParser
> xParser( xFactory
->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW
);
224 OFOPXMLHelper
* pHelper
= new OFOPXMLHelper( nFormat
);
225 uno::Reference
< xml::sax::XDocumentHandler
> xHelper( static_cast< xml::sax::XDocumentHandler
* >( pHelper
) );
226 xml::sax::InputSource aParserInput
;
227 aParserInput
.aInputStream
= xInStream
;
228 aParserInput
.sSystemId
= aStringID
;
229 xParser
->setDocumentHandler( xHelper
);
230 xParser
->parseStream( aParserInput
);
231 xParser
->setDocumentHandler( uno::Reference
< xml::sax::XDocumentHandler
> () );
233 return pHelper
->GetParsingResult();
236 // -----------------------------------
237 OFOPXMLHelper::OFOPXMLHelper( sal_uInt16 nFormat
)
238 : m_nFormat( nFormat
)
239 , m_aRelListElement( RTL_CONSTASCII_USTRINGPARAM( "Relationships" ) )
240 , m_aRelElement( RTL_CONSTASCII_USTRINGPARAM( "Relationship" ) )
241 , m_aIDAttr( RTL_CONSTASCII_USTRINGPARAM( "Id" ) )
242 , m_aTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "Type" ) )
243 , m_aTargetModeAttr( RTL_CONSTASCII_USTRINGPARAM( "TargetMode" ) )
244 , m_aTargetAttr( RTL_CONSTASCII_USTRINGPARAM( "Target" ) )
245 , m_aTypesElement( RTL_CONSTASCII_USTRINGPARAM( "Types" ) )
246 , m_aDefaultElement( RTL_CONSTASCII_USTRINGPARAM( "Default" ) )
247 , m_aOverrideElement( RTL_CONSTASCII_USTRINGPARAM( "Override" ) )
248 , m_aExtensionAttr( RTL_CONSTASCII_USTRINGPARAM( "Extension" ) )
249 , m_aPartNameAttr( RTL_CONSTASCII_USTRINGPARAM( "PartName" ) )
250 , m_aContentTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "ContentType" ) )
254 // -----------------------------------
255 OFOPXMLHelper::~OFOPXMLHelper()
259 // -----------------------------------
260 uno::Sequence
< uno::Sequence
< beans::StringPair
> > OFOPXMLHelper::GetParsingResult()
262 if ( m_aElementsSeq
.getLength() )
263 throw uno::RuntimeException(); // the parsing has still not finished!
268 // -----------------------------------
269 void SAL_CALL
OFOPXMLHelper::startDocument()
270 throw(xml::sax::SAXException
, uno::RuntimeException
)
274 // -----------------------------------
275 void SAL_CALL
OFOPXMLHelper::endDocument()
276 throw(xml::sax::SAXException
, uno::RuntimeException
)
280 // -----------------------------------
281 void SAL_CALL
OFOPXMLHelper::startElement( const ::rtl::OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
282 throw( xml::sax::SAXException
, uno::RuntimeException
)
284 if ( m_nFormat
== RELATIONINFO_FORMAT
)
286 if ( aName
== m_aRelListElement
)
288 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
290 if ( nNewLength
!= 1 )
291 throw xml::sax::SAXException(); // TODO: this element must be the first level element
293 m_aElementsSeq
.realloc( nNewLength
);
294 m_aElementsSeq
[nNewLength
-1] = aName
;
296 return; // nothing to do
298 else if ( aName
== m_aRelElement
)
300 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
301 if ( nNewLength
!= 2 )
302 throw xml::sax::SAXException(); // TODO: this element must be the second level element
304 m_aElementsSeq
.realloc( nNewLength
);
305 m_aElementsSeq
[nNewLength
-1] = aName
;
307 sal_Int32 nNewEntryNum
= m_aResultSeq
.getLength() + 1;
308 m_aResultSeq
.realloc( nNewEntryNum
);
309 sal_Int32 nAttrNum
= 0;
310 m_aResultSeq
[nNewEntryNum
-1].realloc( 4 ); // the maximal expected number of arguments is 4
312 ::rtl::OUString aIDValue
= xAttribs
->getValueByName( m_aIDAttr
);
313 if ( !aIDValue
.getLength() )
314 throw xml::sax::SAXException(); // TODO: the ID value must present
316 ::rtl::OUString aTypeValue
= xAttribs
->getValueByName( m_aTypeAttr
);
317 ::rtl::OUString aTargetValue
= xAttribs
->getValueByName( m_aTargetAttr
);
318 ::rtl::OUString aTargetModeValue
= xAttribs
->getValueByName( m_aTargetModeAttr
);
320 m_aResultSeq
[nNewEntryNum
-1][++nAttrNum
- 1].First
= m_aIDAttr
;
321 m_aResultSeq
[nNewEntryNum
-1][nAttrNum
- 1].Second
= aIDValue
;
323 if ( aTypeValue
.getLength() )
325 m_aResultSeq
[nNewEntryNum
-1][++nAttrNum
- 1].First
= m_aTypeAttr
;
326 m_aResultSeq
[nNewEntryNum
-1][nAttrNum
- 1].Second
= aTypeValue
;
329 if ( aTargetValue
.getLength() )
331 m_aResultSeq
[nNewEntryNum
-1][++nAttrNum
- 1].First
= m_aTargetAttr
;
332 m_aResultSeq
[nNewEntryNum
-1][nAttrNum
- 1].Second
= aTargetValue
;
335 if ( aTargetModeValue
.getLength() )
337 m_aResultSeq
[nNewEntryNum
-1][++nAttrNum
- 1].First
= m_aTargetModeAttr
;
338 m_aResultSeq
[nNewEntryNum
-1][nAttrNum
- 1].Second
= aTargetModeValue
;
341 m_aResultSeq
[nNewEntryNum
-1].realloc( nAttrNum
);
344 throw xml::sax::SAXException(); // TODO: no other elements expected!
346 else if ( m_nFormat
== CONTENTTYPE_FORMAT
)
348 if ( aName
== m_aTypesElement
)
350 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
352 if ( nNewLength
!= 1 )
353 throw xml::sax::SAXException(); // TODO: this element must be the first level element
355 m_aElementsSeq
.realloc( nNewLength
);
356 m_aElementsSeq
[nNewLength
-1] = aName
;
358 if ( !m_aResultSeq
.getLength() )
359 m_aResultSeq
.realloc( 2 );
361 return; // nothing to do
363 else if ( aName
== m_aDefaultElement
)
365 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
366 if ( nNewLength
!= 2 )
367 throw xml::sax::SAXException(); // TODO: this element must be the second level element
369 m_aElementsSeq
.realloc( nNewLength
);
370 m_aElementsSeq
[nNewLength
-1] = aName
;
372 if ( !m_aResultSeq
.getLength() )
373 m_aResultSeq
.realloc( 2 );
375 if ( m_aResultSeq
.getLength() != 2 )
376 throw uno::RuntimeException();
378 ::rtl::OUString aExtensionValue
= xAttribs
->getValueByName( m_aExtensionAttr
);
379 if ( !aExtensionValue
.getLength() )
380 throw xml::sax::SAXException(); // TODO: the Extension value must present
382 ::rtl::OUString aContentTypeValue
= xAttribs
->getValueByName( m_aContentTypeAttr
);
383 if ( !aContentTypeValue
.getLength() )
384 throw xml::sax::SAXException(); // TODO: the ContentType value must present
386 sal_Int32 nNewResultLen
= m_aResultSeq
[0].getLength() + 1;
387 m_aResultSeq
[0].realloc( nNewResultLen
);
389 m_aResultSeq
[0][nNewResultLen
-1].First
= aExtensionValue
;
390 m_aResultSeq
[0][nNewResultLen
-1].Second
= aContentTypeValue
;
392 else if ( aName
== m_aOverrideElement
)
394 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
395 if ( nNewLength
!= 2 )
396 throw xml::sax::SAXException(); // TODO: this element must be the second level element
398 m_aElementsSeq
.realloc( nNewLength
);
399 m_aElementsSeq
[nNewLength
-1] = aName
;
401 if ( !m_aResultSeq
.getLength() )
402 m_aResultSeq
.realloc( 2 );
404 if ( m_aResultSeq
.getLength() != 2 )
405 throw uno::RuntimeException();
407 ::rtl::OUString aPartNameValue
= xAttribs
->getValueByName( m_aPartNameAttr
);
408 if ( !aPartNameValue
.getLength() )
409 throw xml::sax::SAXException(); // TODO: the PartName value must present
411 ::rtl::OUString aContentTypeValue
= xAttribs
->getValueByName( m_aContentTypeAttr
);
412 if ( !aContentTypeValue
.getLength() )
413 throw xml::sax::SAXException(); // TODO: the ContentType value must present
415 sal_Int32 nNewResultLen
= m_aResultSeq
[1].getLength() + 1;
416 m_aResultSeq
[1].realloc( nNewResultLen
);
418 m_aResultSeq
[1][nNewResultLen
-1].First
= aPartNameValue
;
419 m_aResultSeq
[1][nNewResultLen
-1].Second
= aContentTypeValue
;
422 throw xml::sax::SAXException(); // TODO: no other elements expected!
425 throw xml::sax::SAXException(); // TODO: no other elements expected!
428 // -----------------------------------
429 void SAL_CALL
OFOPXMLHelper::endElement( const ::rtl::OUString
& aName
)
430 throw( xml::sax::SAXException
, uno::RuntimeException
)
432 if ( m_nFormat
== RELATIONINFO_FORMAT
|| m_nFormat
== CONTENTTYPE_FORMAT
)
434 sal_Int32 nLength
= m_aElementsSeq
.getLength();
436 throw xml::sax::SAXException(); // TODO: no other end elements expected!
438 if ( !m_aElementsSeq
[nLength
-1].equals( aName
) )
439 throw xml::sax::SAXException(); // TODO: unexpected element ended
441 m_aElementsSeq
.realloc( nLength
- 1 );
445 // -----------------------------------
446 void SAL_CALL
OFOPXMLHelper::characters( const ::rtl::OUString
& /*aChars*/ )
447 throw(xml::sax::SAXException
, uno::RuntimeException
)
451 // -----------------------------------
452 void SAL_CALL
OFOPXMLHelper::ignorableWhitespace( const ::rtl::OUString
& /*aWhitespaces*/ )
453 throw(xml::sax::SAXException
, uno::RuntimeException
)
457 // -----------------------------------
458 void SAL_CALL
OFOPXMLHelper::processingInstruction( const ::rtl::OUString
& /*aTarget*/, const ::rtl::OUString
& /*aData*/ )
459 throw(xml::sax::SAXException
, uno::RuntimeException
)
463 // -----------------------------------
464 void SAL_CALL
OFOPXMLHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
465 throw(xml::sax::SAXException
, uno::RuntimeException
)
469 } // namespace comphelper