Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <com/sun/star/beans/StringPair.hpp>
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/io/XActiveDataSource.hpp>
24 : #include <com/sun/star/xml/sax/Parser.hpp>
25 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
26 : #include <com/sun/star/xml/sax/Writer.hpp>
27 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 :
29 : #include <comphelper/ofopxmlhelper.hxx>
30 : #include <comphelper/attributelist.hxx>
31 :
32 : #define RELATIONINFO_FORMAT 0
33 : #define CONTENTTYPE_FORMAT 1
34 : #define FORMAT_MAX_ID CONTENTTYPE_FORMAT
35 :
36 : using namespace ::com::sun::star;
37 :
38 : namespace comphelper {
39 :
40 :
41 15782 : uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OFOPXMLHelper::ReadRelationsInfoSequence( const uno::Reference< io::XInputStream >& xInStream, const OUString & aStreamName, const uno::Reference< uno::XComponentContext > xContext )
42 : throw( uno::Exception )
43 : {
44 15782 : OUString aStringID = "_rels/";
45 15782 : aStringID += aStreamName;
46 15782 : return ReadSequence_Impl( xInStream, aStringID, RELATIONINFO_FORMAT, xContext );
47 : }
48 :
49 :
50 8620 : uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OFOPXMLHelper::ReadContentTypeSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< uno::XComponentContext > xContext )
51 : throw( uno::Exception )
52 : {
53 8620 : OUString aStringID = "[Content_Types].xml";
54 8620 : return ReadSequence_Impl( xInStream, aStringID, CONTENTTYPE_FORMAT, xContext );
55 : }
56 :
57 :
58 3582 : void SAL_CALL OFOPXMLHelper::WriteRelationsInfoSequence( const uno::Reference< io::XOutputStream >& xOutStream, const uno::Sequence< uno::Sequence< beans::StringPair > >& aSequence, const uno::Reference< uno::XComponentContext > xContext )
59 : throw( uno::Exception )
60 : {
61 3582 : if ( !xOutStream.is() )
62 0 : throw uno::RuntimeException();
63 :
64 3582 : uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
65 :
66 3582 : xWriter->setOutputStream( xOutStream );
67 :
68 7164 : OUString aRelListElement( "Relationships" );
69 7164 : OUString aRelElement( "Relationship" );
70 7164 : OUString aIDAttr( "Id" );
71 7164 : OUString aTypeAttr( "Type" );
72 7164 : OUString aTargetModeAttr( "TargetMode" );
73 7164 : OUString aTargetAttr( "Target" );
74 7164 : OUString aCDATAString( "CDATA" );
75 7164 : OUString aWhiteSpace( " " );
76 :
77 : // write the namespace
78 3582 : AttributeList* pRootAttrList = new AttributeList;
79 7164 : uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
80 : pRootAttrList->AddAttribute(
81 : OUString( "xmlns" ),
82 : aCDATAString,
83 3582 : OUString( "http://schemas.openxmlformats.org/package/2006/relationships" ) );
84 :
85 3582 : xWriter->startDocument();
86 3582 : xWriter->startElement( aRelListElement, xRootAttrList );
87 :
88 15518 : for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ )
89 : {
90 11936 : AttributeList *pAttrList = new AttributeList;
91 11936 : uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
92 47946 : for( sal_Int32 nSecInd = 0; nSecInd < aSequence[nInd].getLength(); nSecInd++ )
93 : {
94 72020 : if ( aSequence[nInd][nSecInd].First.equals( aIDAttr )
95 24074 : || aSequence[nInd][nSecInd].First.equals( aTypeAttr )
96 12138 : || aSequence[nInd][nSecInd].First.equals( aTargetModeAttr )
97 47946 : || aSequence[nInd][nSecInd].First.equals( aTargetAttr ) )
98 : {
99 36010 : pAttrList->AddAttribute( aSequence[nInd][nSecInd].First, aCDATAString, aSequence[nInd][nSecInd].Second );
100 : }
101 : else
102 : {
103 : // TODO/LATER: should the extensions be allowed?
104 0 : throw lang::IllegalArgumentException();
105 : }
106 : }
107 :
108 11936 : xWriter->startElement( aRelElement, xAttrList );
109 11936 : xWriter->ignorableWhitespace( aWhiteSpace );
110 11936 : xWriter->endElement( aRelElement );
111 11936 : }
112 :
113 3582 : xWriter->ignorableWhitespace( aWhiteSpace );
114 3582 : xWriter->endElement( aRelListElement );
115 7164 : xWriter->endDocument();
116 3582 : }
117 :
118 :
119 918 : 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< uno::XComponentContext > xContext )
120 : throw( uno::Exception )
121 : {
122 918 : if ( !xOutStream.is() )
123 0 : throw uno::RuntimeException();
124 :
125 918 : uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
126 :
127 918 : xWriter->setOutputStream( xOutStream );
128 :
129 1836 : OUString aTypesElement( "Types" );
130 1836 : OUString aDefaultElement( "Default" );
131 1836 : OUString aOverrideElement( "Override" );
132 1836 : OUString aExtensionAttr( "Extension" );
133 1836 : OUString aPartNameAttr( "PartName" );
134 1836 : OUString aContentTypeAttr( "ContentType" );
135 1836 : OUString aCDATAString( "CDATA" );
136 1836 : OUString aWhiteSpace( " " );
137 :
138 : // write the namespace
139 918 : AttributeList* pRootAttrList = new AttributeList;
140 1836 : uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
141 : pRootAttrList->AddAttribute(
142 : OUString( "xmlns" ),
143 : aCDATAString,
144 918 : OUString( "http://schemas.openxmlformats.org/package/2006/content-types" ) );
145 :
146 918 : xWriter->startDocument();
147 918 : xWriter->startElement( aTypesElement, xRootAttrList );
148 :
149 918 : for ( sal_Int32 nInd = 0; nInd < aDefaultsSequence.getLength(); nInd++ )
150 : {
151 0 : AttributeList *pAttrList = new AttributeList;
152 0 : uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
153 0 : pAttrList->AddAttribute( aExtensionAttr, aCDATAString, aDefaultsSequence[nInd].First );
154 0 : pAttrList->AddAttribute( aContentTypeAttr, aCDATAString, aDefaultsSequence[nInd].Second );
155 :
156 0 : xWriter->startElement( aDefaultElement, xAttrList );
157 0 : xWriter->ignorableWhitespace( aWhiteSpace );
158 0 : xWriter->endElement( aDefaultElement );
159 0 : }
160 :
161 15944 : for ( sal_Int32 nInd = 0; nInd < aOverridesSequence.getLength(); nInd++ )
162 : {
163 15026 : AttributeList *pAttrList = new AttributeList;
164 15026 : uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
165 15026 : pAttrList->AddAttribute( aPartNameAttr, aCDATAString, aOverridesSequence[nInd].First );
166 15026 : pAttrList->AddAttribute( aContentTypeAttr, aCDATAString, aOverridesSequence[nInd].Second );
167 :
168 15026 : xWriter->startElement( aOverrideElement, xAttrList );
169 15026 : xWriter->ignorableWhitespace( aWhiteSpace );
170 15026 : xWriter->endElement( aOverrideElement );
171 15026 : }
172 :
173 918 : xWriter->ignorableWhitespace( aWhiteSpace );
174 918 : xWriter->endElement( aTypesElement );
175 1836 : xWriter->endDocument();
176 :
177 918 : }
178 :
179 :
180 :
181 :
182 24402 : uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OFOPXMLHelper::ReadSequence_Impl( const uno::Reference< io::XInputStream >& xInStream, const OUString& aStringID, sal_uInt16 nFormat, const uno::Reference< uno::XComponentContext > xContext )
183 : throw( uno::Exception )
184 : {
185 24402 : if ( !xContext.is() || !xInStream.is() || nFormat > FORMAT_MAX_ID )
186 0 : throw uno::RuntimeException();
187 :
188 24402 : uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
189 :
190 24402 : OFOPXMLHelper* pHelper = new OFOPXMLHelper( nFormat );
191 48804 : uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
192 48804 : xml::sax::InputSource aParserInput;
193 24402 : aParserInput.aInputStream = xInStream;
194 24402 : aParserInput.sSystemId = aStringID;
195 24402 : xParser->setDocumentHandler( xHelper );
196 24402 : xParser->parseStream( aParserInput );
197 24402 : xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
198 :
199 48804 : return pHelper->GetParsingResult();
200 : }
201 :
202 :
203 24402 : OFOPXMLHelper::OFOPXMLHelper( sal_uInt16 nFormat )
204 : : m_nFormat( nFormat )
205 : , m_aRelListElement( "Relationships" )
206 : , m_aRelElement( "Relationship" )
207 : , m_aIDAttr( "Id" )
208 : , m_aTypeAttr( "Type" )
209 : , m_aTargetModeAttr( "TargetMode" )
210 : , m_aTargetAttr( "Target" )
211 : , m_aTypesElement( "Types" )
212 : , m_aDefaultElement( "Default" )
213 : , m_aOverrideElement( "Override" )
214 : , m_aExtensionAttr( "Extension" )
215 : , m_aPartNameAttr( "PartName" )
216 24402 : , m_aContentTypeAttr( "ContentType" )
217 : {
218 24402 : }
219 :
220 :
221 48804 : OFOPXMLHelper::~OFOPXMLHelper()
222 : {
223 48804 : }
224 :
225 :
226 24402 : uno::Sequence< uno::Sequence< beans::StringPair > > OFOPXMLHelper::GetParsingResult()
227 : {
228 24402 : if ( m_aElementsSeq.getLength() )
229 0 : throw uno::RuntimeException(); // the parsing has still not finished!
230 :
231 24402 : return m_aResultSeq;
232 : }
233 :
234 :
235 24402 : void SAL_CALL OFOPXMLHelper::startDocument()
236 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
237 : {
238 24402 : }
239 :
240 :
241 24402 : void SAL_CALL OFOPXMLHelper::endDocument()
242 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
243 : {
244 24402 : }
245 :
246 :
247 215944 : void SAL_CALL OFOPXMLHelper::startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
248 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
249 : {
250 215944 : if ( m_nFormat == RELATIONINFO_FORMAT )
251 : {
252 76410 : if ( aName == m_aRelListElement )
253 : {
254 15782 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
255 :
256 15782 : if ( nNewLength != 1 )
257 0 : throw xml::sax::SAXException(); // TODO: this element must be the first level element
258 :
259 15782 : m_aElementsSeq.realloc( nNewLength );
260 15782 : m_aElementsSeq[nNewLength-1] = aName;
261 :
262 15782 : return; // nothing to do
263 : }
264 60628 : else if ( aName == m_aRelElement )
265 : {
266 60628 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
267 60628 : if ( nNewLength != 2 )
268 0 : throw xml::sax::SAXException(); // TODO: this element must be the second level element
269 :
270 60628 : m_aElementsSeq.realloc( nNewLength );
271 60628 : m_aElementsSeq[nNewLength-1] = aName;
272 :
273 60628 : sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
274 60628 : m_aResultSeq.realloc( nNewEntryNum );
275 60628 : sal_Int32 nAttrNum = 0;
276 60628 : m_aResultSeq[nNewEntryNum-1].realloc( 4 ); // the maximal expected number of arguments is 4
277 :
278 60628 : OUString aIDValue = xAttribs->getValueByName( m_aIDAttr );
279 60628 : if ( aIDValue.isEmpty() )
280 0 : throw xml::sax::SAXException(); // TODO: the ID value must present
281 :
282 121256 : OUString aTypeValue = xAttribs->getValueByName( m_aTypeAttr );
283 121256 : OUString aTargetValue = xAttribs->getValueByName( m_aTargetAttr );
284 121256 : OUString aTargetModeValue = xAttribs->getValueByName( m_aTargetModeAttr );
285 :
286 60628 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aIDAttr;
287 60628 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aIDValue;
288 :
289 60628 : if ( !aTypeValue.isEmpty() )
290 : {
291 60628 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTypeAttr;
292 60628 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTypeValue;
293 : }
294 :
295 60628 : if ( !aTargetValue.isEmpty() )
296 : {
297 60626 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetAttr;
298 60626 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetValue;
299 : }
300 :
301 60628 : if ( !aTargetModeValue.isEmpty() )
302 : {
303 1688 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetModeAttr;
304 1688 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetModeValue;
305 : }
306 :
307 121256 : m_aResultSeq[nNewEntryNum-1].realloc( nAttrNum );
308 : }
309 : else
310 0 : throw xml::sax::SAXException(); // TODO: no other elements expected!
311 : }
312 139534 : else if ( m_nFormat == CONTENTTYPE_FORMAT )
313 : {
314 139534 : if ( aName == m_aTypesElement )
315 : {
316 8620 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
317 :
318 8620 : if ( nNewLength != 1 )
319 0 : throw xml::sax::SAXException(); // TODO: this element must be the first level element
320 :
321 8620 : m_aElementsSeq.realloc( nNewLength );
322 8620 : m_aElementsSeq[nNewLength-1] = aName;
323 :
324 8620 : if ( !m_aResultSeq.getLength() )
325 8620 : m_aResultSeq.realloc( 2 );
326 :
327 8620 : return; // nothing to do
328 : }
329 130914 : else if ( aName == m_aDefaultElement )
330 : {
331 13060 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
332 13060 : if ( nNewLength != 2 )
333 0 : throw xml::sax::SAXException(); // TODO: this element must be the second level element
334 :
335 13060 : m_aElementsSeq.realloc( nNewLength );
336 13060 : m_aElementsSeq[nNewLength-1] = aName;
337 :
338 13060 : if ( !m_aResultSeq.getLength() )
339 0 : m_aResultSeq.realloc( 2 );
340 :
341 13060 : if ( m_aResultSeq.getLength() != 2 )
342 0 : throw uno::RuntimeException();
343 :
344 13060 : OUString aExtensionValue = xAttribs->getValueByName( m_aExtensionAttr );
345 13060 : if ( aExtensionValue.isEmpty() )
346 0 : throw xml::sax::SAXException(); // TODO: the Extension value must present
347 :
348 26120 : OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
349 13060 : if ( aContentTypeValue.isEmpty() )
350 0 : throw xml::sax::SAXException(); // TODO: the ContentType value must present
351 :
352 13060 : sal_Int32 nNewResultLen = m_aResultSeq[0].getLength() + 1;
353 13060 : m_aResultSeq[0].realloc( nNewResultLen );
354 :
355 13060 : m_aResultSeq[0][nNewResultLen-1].First = aExtensionValue;
356 26120 : m_aResultSeq[0][nNewResultLen-1].Second = aContentTypeValue;
357 : }
358 117854 : else if ( aName == m_aOverrideElement )
359 : {
360 117854 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
361 117854 : if ( nNewLength != 2 )
362 0 : throw xml::sax::SAXException(); // TODO: this element must be the second level element
363 :
364 117854 : m_aElementsSeq.realloc( nNewLength );
365 117854 : m_aElementsSeq[nNewLength-1] = aName;
366 :
367 117854 : if ( !m_aResultSeq.getLength() )
368 0 : m_aResultSeq.realloc( 2 );
369 :
370 117854 : if ( m_aResultSeq.getLength() != 2 )
371 0 : throw uno::RuntimeException();
372 :
373 117854 : OUString aPartNameValue = xAttribs->getValueByName( m_aPartNameAttr );
374 117854 : if ( aPartNameValue.isEmpty() )
375 0 : throw xml::sax::SAXException(); // TODO: the PartName value must present
376 :
377 235708 : OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
378 117854 : if ( aContentTypeValue.isEmpty() )
379 0 : throw xml::sax::SAXException(); // TODO: the ContentType value must present
380 :
381 117854 : sal_Int32 nNewResultLen = m_aResultSeq[1].getLength() + 1;
382 117854 : m_aResultSeq[1].realloc( nNewResultLen );
383 :
384 117854 : m_aResultSeq[1][nNewResultLen-1].First = aPartNameValue;
385 235708 : m_aResultSeq[1][nNewResultLen-1].Second = aContentTypeValue;
386 : }
387 : else
388 0 : throw xml::sax::SAXException(); // TODO: no other elements expected!
389 : }
390 : else
391 0 : throw xml::sax::SAXException(); // TODO: no other elements expected!
392 : }
393 :
394 :
395 215944 : void SAL_CALL OFOPXMLHelper::endElement( const OUString& aName )
396 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
397 : {
398 215944 : if ( m_nFormat == RELATIONINFO_FORMAT || m_nFormat == CONTENTTYPE_FORMAT )
399 : {
400 215944 : sal_Int32 nLength = m_aElementsSeq.getLength();
401 215944 : if ( nLength <= 0 )
402 0 : throw xml::sax::SAXException(); // TODO: no other end elements expected!
403 :
404 215944 : if ( !m_aElementsSeq[nLength-1].equals( aName ) )
405 0 : throw xml::sax::SAXException(); // TODO: unexpected element ended
406 :
407 215944 : m_aElementsSeq.realloc( nLength - 1 );
408 : }
409 215944 : }
410 :
411 :
412 10260 : void SAL_CALL OFOPXMLHelper::characters( const OUString& /*aChars*/ )
413 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
414 : {
415 10260 : }
416 :
417 :
418 0 : void SAL_CALL OFOPXMLHelper::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
419 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
420 : {
421 0 : }
422 :
423 :
424 0 : void SAL_CALL OFOPXMLHelper::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
425 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
426 : {
427 0 : }
428 :
429 :
430 24402 : void SAL_CALL OFOPXMLHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
431 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
432 : {
433 24402 : }
434 :
435 : } // namespace comphelper
436 :
437 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|