merged tag ooo/OOO330_m14
[LibreOffice.git] / comphelper / source / xml / ofopxmlhelper.cxx
blob9ad617b0e6d15da7916139f57d1449003167c3a5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
32 #include <com/sun/star/beans/StringPair.hpp>
33 #endif
34 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #endif
37 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP
38 #include <com/sun/star/io/XActiveDataSource.hpp>
39 #endif
40 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP
41 #include <com/sun/star/xml/sax/XParser.hpp>
42 #endif
43 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP
44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
45 #endif
46 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP
47 #include <com/sun/star/lang/IllegalArgumentException.hpp>
48 #endif
50 #include <comphelper/ofopxmlhelper.hxx>
51 #include <comphelper/attributelist.hxx>
53 #define RELATIONINFO_FORMAT 0
54 #define CONTENTTYPE_FORMAT 1
55 #define FORMAT_MAX_ID CONTENTTYPE_FORMAT
57 using namespace ::com::sun::star;
59 namespace comphelper {
61 // -----------------------------------
62 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 )
63 throw( uno::Exception )
65 ::rtl::OUString aStringID = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_rels/" ) );
66 aStringID += aStreamName;
67 return ReadSequence_Impl( xInStream, aStringID, RELATIONINFO_FORMAT, xFactory );
70 // -----------------------------------
71 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OFOPXMLHelper::ReadContentTypeSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< lang::XMultiServiceFactory > xFactory )
72 throw( uno::Exception )
74 ::rtl::OUString aStringID = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "[Content_Types].xml" ) );
75 return ReadSequence_Impl( xInStream, aStringID, CONTENTTYPE_FORMAT, xFactory );
78 // -----------------------------------
79 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 )
80 throw( uno::Exception )
82 if ( !xOutStream.is() )
83 throw uno::RuntimeException();
85 uno::Reference< io::XActiveDataSource > xWriterSource(
86 xFactory->createInstance(
87 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
88 uno::UNO_QUERY_THROW );
89 uno::Reference< xml::sax::XDocumentHandler > xWriterHandler( xWriterSource, uno::UNO_QUERY_THROW );
91 xWriterSource->setOutputStream( xOutStream );
93 ::rtl::OUString aRelListElement( RTL_CONSTASCII_USTRINGPARAM( "Relationships" ) );
94 ::rtl::OUString aRelElement( RTL_CONSTASCII_USTRINGPARAM( "Relationship" ) );
95 ::rtl::OUString aIDAttr( RTL_CONSTASCII_USTRINGPARAM( "Id" ) );
96 ::rtl::OUString aTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "Type" ) );
97 ::rtl::OUString aTargetModeAttr( RTL_CONSTASCII_USTRINGPARAM( "TargetMode" ) );
98 ::rtl::OUString aTargetAttr( RTL_CONSTASCII_USTRINGPARAM( "Target" ) );
99 ::rtl::OUString aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
100 ::rtl::OUString aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
102 // write the namespace
103 AttributeList* pRootAttrList = new AttributeList;
104 uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
105 pRootAttrList->AddAttribute(
106 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
107 aCDATAString,
108 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://schemas.openxmlformats.org/package/2006/relationships" ) ) );
110 xWriterHandler->startDocument();
111 xWriterHandler->startElement( aRelListElement, xRootAttrList );
113 for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ )
115 AttributeList *pAttrList = new AttributeList;
116 uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
117 for( sal_Int32 nSecInd = 0; nSecInd < aSequence[nInd].getLength(); nSecInd++ )
119 if ( aSequence[nInd][nSecInd].First.equals( aIDAttr )
120 || aSequence[nInd][nSecInd].First.equals( aTypeAttr )
121 || aSequence[nInd][nSecInd].First.equals( aTargetModeAttr )
122 || aSequence[nInd][nSecInd].First.equals( aTargetAttr ) )
124 pAttrList->AddAttribute( aSequence[nInd][nSecInd].First, aCDATAString, aSequence[nInd][nSecInd].Second );
126 else
128 // TODO/LATER: should the extensions be allowed?
129 throw lang::IllegalArgumentException();
133 xWriterHandler->startElement( aRelElement, xAttrList );
134 xWriterHandler->ignorableWhitespace( aWhiteSpace );
135 xWriterHandler->endElement( aRelElement );
138 xWriterHandler->ignorableWhitespace( aWhiteSpace );
139 xWriterHandler->endElement( aRelListElement );
140 xWriterHandler->endDocument();
143 // -----------------------------------
144 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 )
145 throw( uno::Exception )
147 if ( !xOutStream.is() )
148 throw uno::RuntimeException();
150 uno::Reference< io::XActiveDataSource > xWriterSource(
151 xFactory->createInstance(
152 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
153 uno::UNO_QUERY_THROW );
154 uno::Reference< xml::sax::XDocumentHandler > xWriterHandler( xWriterSource, uno::UNO_QUERY_THROW );
156 xWriterSource->setOutputStream( xOutStream );
158 ::rtl::OUString aTypesElement( RTL_CONSTASCII_USTRINGPARAM( "Types" ) );
159 ::rtl::OUString aDefaultElement( RTL_CONSTASCII_USTRINGPARAM( "Default" ) );
160 ::rtl::OUString aOverrideElement( RTL_CONSTASCII_USTRINGPARAM( "Override" ) );
161 ::rtl::OUString aExtensionAttr( RTL_CONSTASCII_USTRINGPARAM( "Extension" ) );
162 ::rtl::OUString aPartNameAttr( RTL_CONSTASCII_USTRINGPARAM( "PartName" ) );
163 ::rtl::OUString aContentTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "ContentType" ) );
164 ::rtl::OUString aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
165 ::rtl::OUString aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
167 // write the namespace
168 AttributeList* pRootAttrList = new AttributeList;
169 uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
170 pRootAttrList->AddAttribute(
171 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
172 aCDATAString,
173 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://schemas.openxmlformats.org/package/2006/content-types" ) ) );
175 xWriterHandler->startDocument();
176 xWriterHandler->startElement( aTypesElement, xRootAttrList );
178 for ( sal_Int32 nInd = 0; nInd < aDefaultsSequence.getLength(); nInd++ )
180 AttributeList *pAttrList = new AttributeList;
181 uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
182 pAttrList->AddAttribute( aExtensionAttr, aCDATAString, aDefaultsSequence[nInd].First );
183 pAttrList->AddAttribute( aContentTypeAttr, aCDATAString, aDefaultsSequence[nInd].Second );
185 xWriterHandler->startElement( aDefaultElement, xAttrList );
186 xWriterHandler->ignorableWhitespace( aWhiteSpace );
187 xWriterHandler->endElement( aDefaultElement );
190 for ( sal_Int32 nInd = 0; nInd < aOverridesSequence.getLength(); nInd++ )
192 AttributeList *pAttrList = new AttributeList;
193 uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
194 pAttrList->AddAttribute( aPartNameAttr, aCDATAString, aOverridesSequence[nInd].First );
195 pAttrList->AddAttribute( aContentTypeAttr, aCDATAString, aOverridesSequence[nInd].Second );
197 xWriterHandler->startElement( aOverrideElement, xAttrList );
198 xWriterHandler->ignorableWhitespace( aWhiteSpace );
199 xWriterHandler->endElement( aOverrideElement );
202 xWriterHandler->ignorableWhitespace( aWhiteSpace );
203 xWriterHandler->endElement( aTypesElement );
204 xWriterHandler->endDocument();
208 // ==================================================================================
210 // -----------------------------------
211 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 )
212 throw( uno::Exception )
214 if ( !xFactory.is() || !xInStream.is() || nFormat > FORMAT_MAX_ID )
215 throw uno::RuntimeException();
217 uno::Sequence< uno::Sequence< beans::StringPair > > aResult;
219 uno::Reference< xml::sax::XParser > xParser( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW );
221 OFOPXMLHelper* pHelper = new OFOPXMLHelper( nFormat );
222 uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
223 xml::sax::InputSource aParserInput;
224 aParserInput.aInputStream = xInStream;
225 aParserInput.sSystemId = aStringID;
226 xParser->setDocumentHandler( xHelper );
227 xParser->parseStream( aParserInput );
228 xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
230 return pHelper->GetParsingResult();
233 // -----------------------------------
234 OFOPXMLHelper::OFOPXMLHelper( sal_uInt16 nFormat )
235 : m_nFormat( nFormat )
236 , m_aRelListElement( RTL_CONSTASCII_USTRINGPARAM( "Relationships" ) )
237 , m_aRelElement( RTL_CONSTASCII_USTRINGPARAM( "Relationship" ) )
238 , m_aIDAttr( RTL_CONSTASCII_USTRINGPARAM( "Id" ) )
239 , m_aTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "Type" ) )
240 , m_aTargetModeAttr( RTL_CONSTASCII_USTRINGPARAM( "TargetMode" ) )
241 , m_aTargetAttr( RTL_CONSTASCII_USTRINGPARAM( "Target" ) )
242 , m_aTypesElement( RTL_CONSTASCII_USTRINGPARAM( "Types" ) )
243 , m_aDefaultElement( RTL_CONSTASCII_USTRINGPARAM( "Default" ) )
244 , m_aOverrideElement( RTL_CONSTASCII_USTRINGPARAM( "Override" ) )
245 , m_aExtensionAttr( RTL_CONSTASCII_USTRINGPARAM( "Extension" ) )
246 , m_aPartNameAttr( RTL_CONSTASCII_USTRINGPARAM( "PartName" ) )
247 , m_aContentTypeAttr( RTL_CONSTASCII_USTRINGPARAM( "ContentType" ) )
251 // -----------------------------------
252 OFOPXMLHelper::~OFOPXMLHelper()
256 // -----------------------------------
257 uno::Sequence< uno::Sequence< beans::StringPair > > OFOPXMLHelper::GetParsingResult()
259 if ( m_aElementsSeq.getLength() )
260 throw uno::RuntimeException(); // the parsing has still not finished!
262 return m_aResultSeq;
265 // -----------------------------------
266 void SAL_CALL OFOPXMLHelper::startDocument()
267 throw(xml::sax::SAXException, uno::RuntimeException)
271 // -----------------------------------
272 void SAL_CALL OFOPXMLHelper::endDocument()
273 throw(xml::sax::SAXException, uno::RuntimeException)
277 // -----------------------------------
278 void SAL_CALL OFOPXMLHelper::startElement( const ::rtl::OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
279 throw( xml::sax::SAXException, uno::RuntimeException )
281 if ( m_nFormat == RELATIONINFO_FORMAT )
283 if ( aName == m_aRelListElement )
285 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
287 if ( nNewLength != 1 )
288 throw xml::sax::SAXException(); // TODO: this element must be the first level element
290 m_aElementsSeq.realloc( nNewLength );
291 m_aElementsSeq[nNewLength-1] = aName;
293 return; // nothing to do
295 else if ( aName == m_aRelElement )
297 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
298 if ( nNewLength != 2 )
299 throw xml::sax::SAXException(); // TODO: this element must be the second level element
301 m_aElementsSeq.realloc( nNewLength );
302 m_aElementsSeq[nNewLength-1] = aName;
304 sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
305 m_aResultSeq.realloc( nNewEntryNum );
306 sal_Int32 nAttrNum = 0;
307 m_aResultSeq[nNewEntryNum-1].realloc( 4 ); // the maximal expected number of arguments is 4
309 ::rtl::OUString aIDValue = xAttribs->getValueByName( m_aIDAttr );
310 if ( !aIDValue.getLength() )
311 throw xml::sax::SAXException(); // TODO: the ID value must present
313 ::rtl::OUString aTypeValue = xAttribs->getValueByName( m_aTypeAttr );
314 ::rtl::OUString aTargetValue = xAttribs->getValueByName( m_aTargetAttr );
315 ::rtl::OUString aTargetModeValue = xAttribs->getValueByName( m_aTargetModeAttr );
317 m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aIDAttr;
318 m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aIDValue;
320 if ( aTypeValue.getLength() )
322 m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTypeAttr;
323 m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTypeValue;
326 if ( aTargetValue.getLength() )
328 m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetAttr;
329 m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetValue;
332 if ( aTargetModeValue.getLength() )
334 m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetModeAttr;
335 m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetModeValue;
338 m_aResultSeq[nNewEntryNum-1].realloc( nAttrNum );
340 else
341 throw xml::sax::SAXException(); // TODO: no other elements expected!
343 else if ( m_nFormat == CONTENTTYPE_FORMAT )
345 if ( aName == m_aTypesElement )
347 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
349 if ( nNewLength != 1 )
350 throw xml::sax::SAXException(); // TODO: this element must be the first level element
352 m_aElementsSeq.realloc( nNewLength );
353 m_aElementsSeq[nNewLength-1] = aName;
355 if ( !m_aResultSeq.getLength() )
356 m_aResultSeq.realloc( 2 );
358 return; // nothing to do
360 else if ( aName == m_aDefaultElement )
362 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
363 if ( nNewLength != 2 )
364 throw xml::sax::SAXException(); // TODO: this element must be the second level element
366 m_aElementsSeq.realloc( nNewLength );
367 m_aElementsSeq[nNewLength-1] = aName;
369 if ( !m_aResultSeq.getLength() )
370 m_aResultSeq.realloc( 2 );
372 if ( m_aResultSeq.getLength() != 2 )
373 throw uno::RuntimeException();
375 ::rtl::OUString aExtensionValue = xAttribs->getValueByName( m_aExtensionAttr );
376 if ( !aExtensionValue.getLength() )
377 throw xml::sax::SAXException(); // TODO: the Extension value must present
379 ::rtl::OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
380 if ( !aContentTypeValue.getLength() )
381 throw xml::sax::SAXException(); // TODO: the ContentType value must present
383 sal_Int32 nNewResultLen = m_aResultSeq[0].getLength() + 1;
384 m_aResultSeq[0].realloc( nNewResultLen );
386 m_aResultSeq[0][nNewResultLen-1].First = aExtensionValue;
387 m_aResultSeq[0][nNewResultLen-1].Second = aContentTypeValue;
389 else if ( aName == m_aOverrideElement )
391 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
392 if ( nNewLength != 2 )
393 throw xml::sax::SAXException(); // TODO: this element must be the second level element
395 m_aElementsSeq.realloc( nNewLength );
396 m_aElementsSeq[nNewLength-1] = aName;
398 if ( !m_aResultSeq.getLength() )
399 m_aResultSeq.realloc( 2 );
401 if ( m_aResultSeq.getLength() != 2 )
402 throw uno::RuntimeException();
404 ::rtl::OUString aPartNameValue = xAttribs->getValueByName( m_aPartNameAttr );
405 if ( !aPartNameValue.getLength() )
406 throw xml::sax::SAXException(); // TODO: the PartName value must present
408 ::rtl::OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
409 if ( !aContentTypeValue.getLength() )
410 throw xml::sax::SAXException(); // TODO: the ContentType value must present
412 sal_Int32 nNewResultLen = m_aResultSeq[1].getLength() + 1;
413 m_aResultSeq[1].realloc( nNewResultLen );
415 m_aResultSeq[1][nNewResultLen-1].First = aPartNameValue;
416 m_aResultSeq[1][nNewResultLen-1].Second = aContentTypeValue;
418 else
419 throw xml::sax::SAXException(); // TODO: no other elements expected!
421 else
422 throw xml::sax::SAXException(); // TODO: no other elements expected!
425 // -----------------------------------
426 void SAL_CALL OFOPXMLHelper::endElement( const ::rtl::OUString& aName )
427 throw( xml::sax::SAXException, uno::RuntimeException )
429 if ( m_nFormat == RELATIONINFO_FORMAT || m_nFormat == CONTENTTYPE_FORMAT )
431 sal_Int32 nLength = m_aElementsSeq.getLength();
432 if ( nLength <= 0 )
433 throw xml::sax::SAXException(); // TODO: no other end elements expected!
435 if ( !m_aElementsSeq[nLength-1].equals( aName ) )
436 throw xml::sax::SAXException(); // TODO: unexpected element ended
438 m_aElementsSeq.realloc( nLength - 1 );
442 // -----------------------------------
443 void SAL_CALL OFOPXMLHelper::characters( const ::rtl::OUString& /*aChars*/ )
444 throw(xml::sax::SAXException, uno::RuntimeException)
448 // -----------------------------------
449 void SAL_CALL OFOPXMLHelper::ignorableWhitespace( const ::rtl::OUString& /*aWhitespaces*/ )
450 throw(xml::sax::SAXException, uno::RuntimeException)
454 // -----------------------------------
455 void SAL_CALL OFOPXMLHelper::processingInstruction( const ::rtl::OUString& /*aTarget*/, const ::rtl::OUString& /*aData*/ )
456 throw(xml::sax::SAXException, uno::RuntimeException)
460 // -----------------------------------
461 void SAL_CALL OFOPXMLHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
462 throw(xml::sax::SAXException, uno::RuntimeException)
466 } // namespace comphelper