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 4580 : 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 4580 : OUString aStringID = "_rels/";
45 4580 : aStringID += aStreamName;
46 4580 : return ReadSequence_Impl( xInStream, aStringID, RELATIONINFO_FORMAT, xContext );
47 : }
48 :
49 :
50 1852 : 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 1852 : OUString aStringID = "[Content_Types].xml";
54 1852 : return ReadSequence_Impl( xInStream, aStringID, CONTENTTYPE_FORMAT, xContext );
55 : }
56 :
57 :
58 1241 : 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 1241 : if ( !xOutStream.is() )
62 0 : throw uno::RuntimeException();
63 :
64 1241 : uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
65 :
66 1241 : xWriter->setOutputStream( xOutStream );
67 :
68 2482 : OUString aRelListElement( "Relationships" );
69 2482 : OUString aRelElement( "Relationship" );
70 2482 : OUString aIDAttr( "Id" );
71 2482 : OUString aTypeAttr( "Type" );
72 2482 : OUString aTargetModeAttr( "TargetMode" );
73 2482 : OUString aTargetAttr( "Target" );
74 2482 : OUString aCDATAString( "CDATA" );
75 2482 : OUString aWhiteSpace( " " );
76 :
77 : // write the namespace
78 1241 : AttributeList* pRootAttrList = new AttributeList;
79 2482 : uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
80 : pRootAttrList->AddAttribute(
81 : OUString( "xmlns" ),
82 : aCDATAString,
83 1241 : OUString( "http://schemas.openxmlformats.org/package/2006/relationships" ) );
84 :
85 1241 : xWriter->startDocument();
86 1241 : xWriter->startElement( aRelListElement, xRootAttrList );
87 :
88 5029 : for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ )
89 : {
90 3788 : AttributeList *pAttrList = new AttributeList;
91 3788 : uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
92 15351 : for( sal_Int32 nSecInd = 0; nSecInd < aSequence[nInd].getLength(); nSecInd++ )
93 : {
94 23126 : if ( aSequence[nInd][nSecInd].First.equals( aIDAttr )
95 7775 : || aSequence[nInd][nSecInd].First.equals( aTypeAttr )
96 3987 : || aSequence[nInd][nSecInd].First.equals( aTargetModeAttr )
97 15351 : || aSequence[nInd][nSecInd].First.equals( aTargetAttr ) )
98 : {
99 11563 : 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 3788 : xWriter->startElement( aRelElement, xAttrList );
109 3788 : xWriter->ignorableWhitespace( aWhiteSpace );
110 3788 : xWriter->endElement( aRelElement );
111 3788 : }
112 :
113 1241 : xWriter->ignorableWhitespace( aWhiteSpace );
114 1241 : xWriter->endElement( aRelListElement );
115 2482 : xWriter->endDocument();
116 1241 : }
117 :
118 :
119 290 : 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 290 : if ( !xOutStream.is() )
123 0 : throw uno::RuntimeException();
124 :
125 290 : uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
126 :
127 290 : xWriter->setOutputStream( xOutStream );
128 :
129 580 : OUString aTypesElement( "Types" );
130 580 : OUString aDefaultElement( "Default" );
131 580 : OUString aOverrideElement( "Override" );
132 580 : OUString aExtensionAttr( "Extension" );
133 580 : OUString aPartNameAttr( "PartName" );
134 580 : OUString aContentTypeAttr( "ContentType" );
135 580 : OUString aCDATAString( "CDATA" );
136 580 : OUString aWhiteSpace( " " );
137 :
138 : // write the namespace
139 290 : AttributeList* pRootAttrList = new AttributeList;
140 580 : uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
141 : pRootAttrList->AddAttribute(
142 : OUString( "xmlns" ),
143 : aCDATAString,
144 290 : OUString( "http://schemas.openxmlformats.org/package/2006/content-types" ) );
145 :
146 290 : xWriter->startDocument();
147 290 : xWriter->startElement( aTypesElement, xRootAttrList );
148 :
149 290 : 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 5063 : for ( sal_Int32 nInd = 0; nInd < aOverridesSequence.getLength(); nInd++ )
162 : {
163 4773 : AttributeList *pAttrList = new AttributeList;
164 4773 : uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
165 4773 : pAttrList->AddAttribute( aPartNameAttr, aCDATAString, aOverridesSequence[nInd].First );
166 4773 : pAttrList->AddAttribute( aContentTypeAttr, aCDATAString, aOverridesSequence[nInd].Second );
167 :
168 4773 : xWriter->startElement( aOverrideElement, xAttrList );
169 4773 : xWriter->ignorableWhitespace( aWhiteSpace );
170 4773 : xWriter->endElement( aOverrideElement );
171 4773 : }
172 :
173 290 : xWriter->ignorableWhitespace( aWhiteSpace );
174 290 : xWriter->endElement( aTypesElement );
175 580 : xWriter->endDocument();
176 :
177 290 : }
178 :
179 :
180 :
181 :
182 6432 : 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 6432 : if ( !xContext.is() || !xInStream.is() || nFormat > FORMAT_MAX_ID )
186 0 : throw uno::RuntimeException();
187 :
188 6432 : uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
189 :
190 6432 : OFOPXMLHelper* pHelper = new OFOPXMLHelper( nFormat );
191 12864 : uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
192 12864 : xml::sax::InputSource aParserInput;
193 6432 : aParserInput.aInputStream = xInStream;
194 6432 : aParserInput.sSystemId = aStringID;
195 6432 : xParser->setDocumentHandler( xHelper );
196 6432 : xParser->parseStream( aParserInput );
197 6432 : xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
198 :
199 12864 : return pHelper->GetParsingResult();
200 : }
201 :
202 :
203 6432 : 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 6432 : , m_aContentTypeAttr( "ContentType" )
217 : {
218 6432 : }
219 :
220 :
221 12864 : OFOPXMLHelper::~OFOPXMLHelper()
222 : {
223 12864 : }
224 :
225 :
226 6432 : uno::Sequence< uno::Sequence< beans::StringPair > > OFOPXMLHelper::GetParsingResult()
227 : {
228 6432 : if ( m_aElementsSeq.getLength() )
229 0 : throw uno::RuntimeException(); // the parsing has still not finished!
230 :
231 6432 : return m_aResultSeq;
232 : }
233 :
234 :
235 6432 : void SAL_CALL OFOPXMLHelper::startDocument()
236 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
237 : {
238 6432 : }
239 :
240 :
241 6432 : void SAL_CALL OFOPXMLHelper::endDocument()
242 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
243 : {
244 6432 : }
245 :
246 :
247 51489 : 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 51489 : if ( m_nFormat == RELATIONINFO_FORMAT )
251 : {
252 21736 : if ( aName == m_aRelListElement )
253 : {
254 4580 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
255 :
256 4580 : if ( nNewLength != 1 )
257 0 : throw xml::sax::SAXException(); // TODO: this element must be the first level element
258 :
259 4580 : m_aElementsSeq.realloc( nNewLength );
260 4580 : m_aElementsSeq[nNewLength-1] = aName;
261 :
262 4580 : return; // nothing to do
263 : }
264 17156 : else if ( aName == m_aRelElement )
265 : {
266 17156 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
267 17156 : if ( nNewLength != 2 )
268 0 : throw xml::sax::SAXException(); // TODO: this element must be the second level element
269 :
270 17156 : m_aElementsSeq.realloc( nNewLength );
271 17156 : m_aElementsSeq[nNewLength-1] = aName;
272 :
273 17156 : sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
274 17156 : m_aResultSeq.realloc( nNewEntryNum );
275 17156 : sal_Int32 nAttrNum = 0;
276 17156 : m_aResultSeq[nNewEntryNum-1].realloc( 4 ); // the maximal expected number of arguments is 4
277 :
278 17156 : OUString aIDValue = xAttribs->getValueByName( m_aIDAttr );
279 17156 : if ( aIDValue.isEmpty() )
280 0 : throw xml::sax::SAXException(); // TODO: the ID value must present
281 :
282 34312 : OUString aTypeValue = xAttribs->getValueByName( m_aTypeAttr );
283 34312 : OUString aTargetValue = xAttribs->getValueByName( m_aTargetAttr );
284 34312 : OUString aTargetModeValue = xAttribs->getValueByName( m_aTargetModeAttr );
285 :
286 17156 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aIDAttr;
287 17156 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aIDValue;
288 :
289 17156 : if ( !aTypeValue.isEmpty() )
290 : {
291 17156 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTypeAttr;
292 17156 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTypeValue;
293 : }
294 :
295 17156 : if ( !aTargetValue.isEmpty() )
296 : {
297 17155 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetAttr;
298 17155 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetValue;
299 : }
300 :
301 17156 : if ( !aTargetModeValue.isEmpty() )
302 : {
303 1084 : m_aResultSeq[nNewEntryNum-1][++nAttrNum - 1].First = m_aTargetModeAttr;
304 1084 : m_aResultSeq[nNewEntryNum-1][nAttrNum - 1].Second = aTargetModeValue;
305 : }
306 :
307 34312 : m_aResultSeq[nNewEntryNum-1].realloc( nAttrNum );
308 : }
309 : else
310 0 : throw xml::sax::SAXException(); // TODO: no other elements expected!
311 : }
312 29753 : else if ( m_nFormat == CONTENTTYPE_FORMAT )
313 : {
314 29753 : if ( aName == m_aTypesElement )
315 : {
316 1852 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
317 :
318 1852 : if ( nNewLength != 1 )
319 0 : throw xml::sax::SAXException(); // TODO: this element must be the first level element
320 :
321 1852 : m_aElementsSeq.realloc( nNewLength );
322 1852 : m_aElementsSeq[nNewLength-1] = aName;
323 :
324 1852 : if ( !m_aResultSeq.getLength() )
325 1852 : m_aResultSeq.realloc( 2 );
326 :
327 1852 : return; // nothing to do
328 : }
329 27901 : else if ( aName == m_aDefaultElement )
330 : {
331 2677 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
332 2677 : if ( nNewLength != 2 )
333 0 : throw xml::sax::SAXException(); // TODO: this element must be the second level element
334 :
335 2677 : m_aElementsSeq.realloc( nNewLength );
336 2677 : m_aElementsSeq[nNewLength-1] = aName;
337 :
338 2677 : if ( !m_aResultSeq.getLength() )
339 0 : m_aResultSeq.realloc( 2 );
340 :
341 2677 : if ( m_aResultSeq.getLength() != 2 )
342 0 : throw uno::RuntimeException();
343 :
344 2677 : OUString aExtensionValue = xAttribs->getValueByName( m_aExtensionAttr );
345 2677 : if ( aExtensionValue.isEmpty() )
346 0 : throw xml::sax::SAXException(); // TODO: the Extension value must present
347 :
348 5354 : OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
349 2677 : if ( aContentTypeValue.isEmpty() )
350 0 : throw xml::sax::SAXException(); // TODO: the ContentType value must present
351 :
352 2677 : sal_Int32 nNewResultLen = m_aResultSeq[0].getLength() + 1;
353 2677 : m_aResultSeq[0].realloc( nNewResultLen );
354 :
355 2677 : m_aResultSeq[0][nNewResultLen-1].First = aExtensionValue;
356 5354 : m_aResultSeq[0][nNewResultLen-1].Second = aContentTypeValue;
357 : }
358 25224 : else if ( aName == m_aOverrideElement )
359 : {
360 25224 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
361 25224 : if ( nNewLength != 2 )
362 0 : throw xml::sax::SAXException(); // TODO: this element must be the second level element
363 :
364 25224 : m_aElementsSeq.realloc( nNewLength );
365 25224 : m_aElementsSeq[nNewLength-1] = aName;
366 :
367 25224 : if ( !m_aResultSeq.getLength() )
368 0 : m_aResultSeq.realloc( 2 );
369 :
370 25224 : if ( m_aResultSeq.getLength() != 2 )
371 0 : throw uno::RuntimeException();
372 :
373 25224 : OUString aPartNameValue = xAttribs->getValueByName( m_aPartNameAttr );
374 25224 : if ( aPartNameValue.isEmpty() )
375 0 : throw xml::sax::SAXException(); // TODO: the PartName value must present
376 :
377 50448 : OUString aContentTypeValue = xAttribs->getValueByName( m_aContentTypeAttr );
378 25224 : if ( aContentTypeValue.isEmpty() )
379 0 : throw xml::sax::SAXException(); // TODO: the ContentType value must present
380 :
381 25224 : sal_Int32 nNewResultLen = m_aResultSeq[1].getLength() + 1;
382 25224 : m_aResultSeq[1].realloc( nNewResultLen );
383 :
384 25224 : m_aResultSeq[1][nNewResultLen-1].First = aPartNameValue;
385 50448 : 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 51489 : void SAL_CALL OFOPXMLHelper::endElement( const OUString& aName )
396 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
397 : {
398 51489 : if ( m_nFormat == RELATIONINFO_FORMAT || m_nFormat == CONTENTTYPE_FORMAT )
399 : {
400 51489 : sal_Int32 nLength = m_aElementsSeq.getLength();
401 51489 : if ( nLength <= 0 )
402 0 : throw xml::sax::SAXException(); // TODO: no other end elements expected!
403 :
404 51489 : if ( !m_aElementsSeq[nLength-1].equals( aName ) )
405 0 : throw xml::sax::SAXException(); // TODO: unexpected element ended
406 :
407 51489 : m_aElementsSeq.realloc( nLength - 1 );
408 : }
409 51489 : }
410 :
411 :
412 2624 : void SAL_CALL OFOPXMLHelper::characters( const OUString& /*aChars*/ )
413 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
414 : {
415 2624 : }
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 6432 : void SAL_CALL OFOPXMLHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
431 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
432 : {
433 6432 : }
434 :
435 : } // namespace comphelper
436 :
437 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|