Debian Codegraph @

comphelper/source/xml/ofopxmlhelper.cxx

raw
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 <comphelper/ofopxmlhelper.hxx>
22 #include <comphelper/attributelist.hxx>
23
24 #include <cppuhelper/implbase.hxx>
25 #include <rtl/ref.hxx>
26
27 #include <com/sun/star/beans/StringPair.hpp>
28 #include <com/sun/star/xml/sax/Parser.hpp>
29 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
30 #include <com/sun/star/xml/sax/SAXException.hpp>
31 #include <com/sun/star/xml/sax/Writer.hpp>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 #include <vector>
34
35 #define RELATIONINFO_FORMAT 0
36 #define CONTENTTYPE_FORMAT 1
37 #define FORMAT_MAX_ID CONTENTTYPE_FORMAT
38
39 using namespace ::com::sun::star;
40
41 namespace comphelper {
42
43 namespace {
44
45 // this helper class is designed to allow to parse ContentType- and Relationship-related information from OfficeOpenXML format
46 class OFOPXMLHelper_Impl
47 : public cppu::WeakImplHelper< css::xml::sax::XDocumentHandler >
48 {
49 sal_uInt16 const m_nFormat; // which format to parse
50
51 css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > m_aResultSeq;
52 std::vector< OUString > m_aElementsSeq; // stack of elements being parsed
53
54
55 public:
56 css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > const & GetParsingResult() const;
57
58 explicit OFOPXMLHelper_Impl( sal_uInt16 nFormat ); // must not be created directly
59
60 // XDocumentHandler
61 virtual void SAL_CALL startDocument() override;
62 virtual void SAL_CALL endDocument() override;
63 virtual void SAL_CALL startElement( const OUString& aName, const css::uno::Reference< css::xml::sax::XAttributeList >& xAttribs ) override;
64 virtual void SAL_CALL endElement( const OUString& aName ) override;
65 virtual void SAL_CALL characters( const OUString& aChars ) override;
66 virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) override;
67 virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) override;
68 virtual void SAL_CALL setDocumentLocator( const css::uno::Reference< css::xml::sax::XLocator >& xLocator ) override;
69 };
70
71 }
72
73 namespace OFOPXMLHelper {
74
75 /// @throws css::uno::Exception
76 static uno::Sequence<uno::Sequence< beans::StringPair>> ReadSequence_Impl(
77 const uno::Reference<io::XInputStream>& xInStream,
78 const OUString& aStringID, sal_uInt16 nFormat,
79 const uno::Reference<uno::XComponentContext>& xContext);
80
81 uno::Sequence< uno::Sequence< beans::StringPair > > ReadRelationsInfoSequence(
82 const uno::Reference< io::XInputStream >& xInStream,
83 std::u16string_view aStreamName,
84 const uno::Reference< uno::XComponentContext >& rContext )
85 {
86 OUString aStringID = OUString::Concat("_rels/") + aStreamName;
87 return ReadSequence_Impl( xInStream, aStringID, RELATIONINFO_FORMAT, rContext );
88 }
89
90
91 uno::Sequence< uno::Sequence< beans::StringPair > > ReadContentTypeSequence(
92 const uno::Reference< io::XInputStream >& xInStream,
93 const uno::Reference< uno::XComponentContext >& rContext )
94 {
95 return ReadSequence_Impl( xInStream, u"[Content_Types].xml"_ustr, CONTENTTYPE_FORMAT, rContext );
96 }
97
98 OUString GetContentTypeByName(
99 const css::uno::Sequence<css::uno::Sequence<css::beans::StringPair>>& rContentTypes,
100 const OUString& rFilename)
101 {
102 if (rContentTypes.getLength() < 2)
103 {
104 return OUString();
105 }
106
107 const uno::Sequence<beans::StringPair>& rDefaults = rContentTypes[0];
108 const uno::Sequence<beans::StringPair>& rOverrides = rContentTypes[1];
109
110 // Find the extension and use it to get the type.
111 const sal_Int32 nDotOffset = rFilename.lastIndexOf('.');
112 const OUString aExt = (nDotOffset >= 0 ? rFilename.copy(nDotOffset + 1) : rFilename); // Skip the dot.
113
114 const std::vector<OUString> aNames = { aExt, "/" + rFilename };
115 for (const OUString& aName : aNames)
116 {
117 const auto it1 = std::find_if(rOverrides.begin(), rOverrides.end(), [&aName](const beans::StringPair& rPair)
118 { return rPair.First == aName; });
119 if (it1 != rOverrides.end())
120 return it1->Second;
121
122 const auto it2 = std::find_if(rDefaults.begin(), rDefaults.end(), [&aName](const beans::StringPair& rPair)
123 { return rPair.First == aName; });
124 if (it2 != rDefaults.end())
125 return it2->Second;
126 }
127
128 return OUString();
129 }
130
131 void WriteRelationsInfoSequence(
132 const uno::Reference< io::XOutputStream >& xOutStream,
133 const uno::Sequence< uno::Sequence< beans::StringPair > >& aSequence,
134 const uno::Reference< uno::XComponentContext >& rContext )
135 {
136 if ( !xOutStream.is() )
137 throw uno::RuntimeException("Invalid output stream");
138
139 uno::Reference< css::xml::sax::XWriter > xWriter = css::xml::sax::Writer::create(rContext);
140
141 xWriter->setOutputStream( xOutStream );
142
143 OUString aRelListElement( u"Relationships"_ustr );
144 OUString aRelElement( u"Relationship"_ustr );
145 OUString aWhiteSpace( u" "_ustr );
146
147 // write the namespace
148 rtl::Reference<AttributeList> pRootAttrList = new AttributeList;
149 pRootAttrList->AddAttribute(
150 u"xmlns"_ustr,
151 u"http://schemas.openxmlformats.org/package/2006/relationships"_ustr );
152
153 xWriter->startDocument();
154 xWriter->startElement( aRelListElement, pRootAttrList );
155
156 for ( const auto & i : aSequence )
157 {
158 rtl::Reference<AttributeList> pAttrList = new AttributeList;
159 for( const beans::StringPair & pair : i )
160 {
161 if ( !(pair.First == "Id"
162 || pair.First == "Type"
163 || pair.First == "TargetMode"
164 || pair.First == "Target") )
165 {
166 // TODO/LATER: should the extensions be allowed?
167 throw lang::IllegalArgumentException();
168 }
169 pAttrList->AddAttribute( pair.First, pair.Second );
170 }
171
172 xWriter->startElement( aRelElement, pAttrList );
173 xWriter->ignorableWhitespace( aWhiteSpace );
174 xWriter->endElement( aRelElement );
175 }
176
177 xWriter->ignorableWhitespace( aWhiteSpace );
178 xWriter->endElement( aRelListElement );
179 xWriter->endDocument();
180 }
181
182
183 void WriteContentSequence(
184 const uno::Reference< io::XOutputStream >& xOutStream,
185 const uno::Sequence< beans::StringPair >& aDefaultsSequence,
186 const uno::Sequence< beans::StringPair >& aOverridesSequence,
187 const uno::Reference< uno::XComponentContext >& rContext )
188 {
189 if ( !xOutStream.is() )
190 throw uno::RuntimeException("Invalid output stream");
191
192 uno::Reference< css::xml::sax::XWriter > xWriter = css::xml::sax::Writer::create(rContext);
193
194 xWriter->setOutputStream( xOutStream );
195
196 static constexpr OUString aTypesElement(u"Types"_ustr);
197 static constexpr OUString aDefaultElement(u"Default"_ustr);
198 static constexpr OUString aOverrideElement(u"Override"_ustr);
199 static constexpr OUString aContentTypeAttr(u"ContentType"_ustr);
200 static constexpr OUString aWhiteSpace(u" "_ustr);
201
202 // write the namespace
203 rtl::Reference<AttributeList> pRootAttrList = new AttributeList;
204 pRootAttrList->AddAttribute(
205 u"xmlns"_ustr,
206 u"http://schemas.openxmlformats.org/package/2006/content-types"_ustr );
207
208 xWriter->startDocument();
209 xWriter->startElement( aTypesElement, pRootAttrList );
210
211 for ( const beans::StringPair & pair : aDefaultsSequence )
212 {
213 rtl::Reference<AttributeList> pAttrList = new AttributeList;
214 pAttrList->AddAttribute( u"Extension"_ustr, pair.First );
215 pAttrList->AddAttribute( aContentTypeAttr, pair.Second );
216
217 xWriter->startElement( aDefaultElement, pAttrList );
218 xWriter->ignorableWhitespace( aWhiteSpace );
219 xWriter->endElement( aDefaultElement );
220 }
221
222 for ( const beans::StringPair & pair : aOverridesSequence )
223 {
224 rtl::Reference<AttributeList> pAttrList = new AttributeList;
225 pAttrList->AddAttribute( u"PartName"_ustr, pair.First );
226 pAttrList->AddAttribute( aContentTypeAttr, pair.Second );
227
228 xWriter->startElement( aOverrideElement, pAttrList );
229 xWriter->ignorableWhitespace( aWhiteSpace );
230 xWriter->endElement( aOverrideElement );
231 }
232
233 xWriter->ignorableWhitespace( aWhiteSpace );
234 xWriter->endElement( aTypesElement );
235 xWriter->endDocument();
236
237 }
238
239 uno::Sequence< uno::Sequence< beans::StringPair > > ReadSequence_Impl(
240 const uno::Reference< io::XInputStream >& xInStream,
241 const OUString& aStringID, sal_uInt16 nFormat,
242 const uno::Reference< uno::XComponentContext >& rContext )
243 {
244 if ( !rContext.is() || !xInStream.is() || nFormat > FORMAT_MAX_ID )
245 throw uno::RuntimeException("Invalid input stream or context");
246
247
248 uno::Reference< css::xml::sax::XParser > xParser = css::xml::sax::Parser::create( rContext );
249
250 rtl::Reference<OFOPXMLHelper_Impl> pHelper = new OFOPXMLHelper_Impl( nFormat );
251 css::xml::sax::InputSource aParserInput;
252 aParserInput.aInputStream = xInStream;
253 aParserInput.sSystemId = aStringID;
254 xParser->setDocumentHandler( pHelper );
255 xParser->parseStream( aParserInput );
256 xParser->setDocumentHandler( uno::Reference < css::xml::sax::XDocumentHandler > () );
257
258 return pHelper->GetParsingResult();
259 }
260
261 } // namespace OFOPXMLHelper
262
263 // Relations info related strings
264 constexpr OUStringLiteral g_aRelListElement(u"Relationships");
265 constexpr OUStringLiteral g_aRelElement( u"Relationship" );
266 constexpr OUString g_aIDAttr( u"Id"_ustr );
267 constexpr OUString g_aTypeAttr( u"Type"_ustr );
268 constexpr OUString g_aTargetModeAttr( u"TargetMode"_ustr );
269 constexpr OUString g_aTargetAttr( u"Target"_ustr );
270
271 // ContentType related strings
272 constexpr OUStringLiteral g_aTypesElement( u"Types" );
273 constexpr OUStringLiteral g_aDefaultElement( u"Default" );
274 constexpr OUStringLiteral g_aOverrideElement( u"Override" );
275 constexpr OUStringLiteral g_aExtensionAttr( u"Extension" );
276 constexpr OUStringLiteral g_aPartNameAttr( u"PartName" );
277 constexpr OUString g_aContentTypeAttr( u"ContentType"_ustr );
278
279 OFOPXMLHelper_Impl::OFOPXMLHelper_Impl( sal_uInt16 nFormat )
280 : m_nFormat( nFormat )
281 {
282 }
283
284 uno::Sequence< uno::Sequence< beans::StringPair > > const & OFOPXMLHelper_Impl::GetParsingResult() const
285 {
286 if ( !m_aElementsSeq.empty() )
287 throw uno::RuntimeException(); // the parsing has still not finished!
288
289 return m_aResultSeq;
290 }
291
292
293 void SAL_CALL OFOPXMLHelper_Impl::startDocument()
294 {
295 }
296
297
298 void SAL_CALL OFOPXMLHelper_Impl::endDocument()
299 {
300 }
301
302
303 void SAL_CALL OFOPXMLHelper_Impl::startElement( const OUString& aName, const uno::Reference< css::xml::sax::XAttributeList >& xAttribs )
304 {
305 if ( m_nFormat == RELATIONINFO_FORMAT )
306 {
307 if ( aName == g_aRelListElement )
308 {
309 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
310
311 if ( nNewLength != 1 )
312 throw css::xml::sax::SAXException(); // TODO: this element must be the first level element
313
314 m_aElementsSeq.push_back( aName );
315
316 return; // nothing to do
317 }
318 else if ( aName == g_aRelElement )
319 {
320 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
321 if ( nNewLength != 2 )
322 throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
323
324 m_aElementsSeq.push_back( aName );
325
326 sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
327 m_aResultSeq.realloc( nNewEntryNum );
328 auto pResultSeq = m_aResultSeq.getArray();
329 sal_Int32 nAttrNum = 0;
330 pResultSeq[nNewEntryNum-1].realloc( 4 ); // the maximal expected number of arguments is 4
331 auto pAttrs = pResultSeq[nNewEntryNum-1].getArray();
332
333 OUString aIDValue = xAttribs->getValueByName( g_aIDAttr );
334 if ( aIDValue.isEmpty() )
335 throw css::xml::sax::SAXException(); // TODO: the ID value must present
336
337 OUString aTypeValue = xAttribs->getValueByName( g_aTypeAttr );
338 OUString aTargetValue = xAttribs->getValueByName( g_aTargetAttr );
339 OUString aTargetModeValue = xAttribs->getValueByName( g_aTargetModeAttr );
340
341 pAttrs[++nAttrNum - 1].First = g_aIDAttr;
342 pAttrs[nAttrNum - 1].Second = aIDValue;
343
344 if ( !aTypeValue.isEmpty() )
345 {
346 pAttrs[++nAttrNum - 1].First = g_aTypeAttr;
347 pAttrs[nAttrNum - 1].Second = aTypeValue;
348 }
349
350 if ( !aTargetValue.isEmpty() )
351 {
352 pAttrs[++nAttrNum - 1].First = g_aTargetAttr;
353 pAttrs[nAttrNum - 1].Second = aTargetValue;
354 }
355
356 if ( !aTargetModeValue.isEmpty() )
357 {
358 pAttrs[++nAttrNum - 1].First = g_aTargetModeAttr;
359 pAttrs[nAttrNum - 1].Second = aTargetModeValue;
360 }
361
362 pResultSeq[nNewEntryNum-1].realloc( nAttrNum );
363 }
364 else
365 throw css::xml::sax::SAXException(); // TODO: no other elements expected!
366 }
367 else if ( m_nFormat == CONTENTTYPE_FORMAT )
368 {
369 if ( aName == g_aTypesElement )
370 {
371 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
372
373 if ( nNewLength != 1 )
374 throw css::xml::sax::SAXException(); // TODO: this element must be the first level element
375
376 m_aElementsSeq.push_back( aName );
377
378 if ( !m_aResultSeq.hasElements() )
379 m_aResultSeq.realloc( 2 );
380
381 return; // nothing to do
382 }
383 else if ( aName == g_aDefaultElement )
384 {
385 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
386 if ( nNewLength != 2 )
387 throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
388
389 m_aElementsSeq.push_back( aName );
390
391 if ( !m_aResultSeq.hasElements() )
392 m_aResultSeq.realloc( 2 );
393
394 if ( m_aResultSeq.getLength() != 2 )
395 throw uno::RuntimeException("m_aResultSeq already has elements and is not reallocated to 2.");
396
397 auto pResultSeq = m_aResultSeq.getArray();
398
399 const OUString aExtensionValue = xAttribs->getValueByName( g_aExtensionAttr );
400 if ( aExtensionValue.isEmpty() )
401 throw css::xml::sax::SAXException(); // TODO: the Extension value must present
402
403 const OUString aContentTypeValue = xAttribs->getValueByName( g_aContentTypeAttr );
404 if ( aContentTypeValue.isEmpty() )
405 throw css::xml::sax::SAXException(); // TODO: the ContentType value must present
406
407 const sal_Int32 nNewResultLen = m_aResultSeq[0].getLength() + 1;
408 pResultSeq[0].realloc( nNewResultLen );
409 auto pSeq = pResultSeq[0].getArray();
410
411 pSeq[nNewResultLen-1].First = aExtensionValue;
412 pSeq[nNewResultLen-1].Second = aContentTypeValue;
413 }
414 else if ( aName == g_aOverrideElement )
415 {
416 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
417 if ( nNewLength != 2 )
418 throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
419
420 m_aElementsSeq.push_back( aName );
421
422 if ( !m_aResultSeq.hasElements() )
423 m_aResultSeq.realloc( 2 );
424
425 if ( m_aResultSeq.getLength() != 2 )
426 throw uno::RuntimeException("m_aResultSeq already has elements and is not reallocated to 2.");
427
428 auto pResultSeq = m_aResultSeq.getArray();
429
430 OUString aPartNameValue = xAttribs->getValueByName( g_aPartNameAttr );
431 if ( aPartNameValue.isEmpty() )
432 throw css::xml::sax::SAXException(); // TODO: the PartName value must present
433
434 OUString aContentTypeValue = xAttribs->getValueByName( g_aContentTypeAttr );
435 if ( aContentTypeValue.isEmpty() )
436 throw css::xml::sax::SAXException(); // TODO: the ContentType value must present
437
438 sal_Int32 nNewResultLen = m_aResultSeq[1].getLength() + 1;
439 pResultSeq[1].realloc( nNewResultLen );
440 auto pSeq = pResultSeq[1].getArray();
441
442 pSeq[nNewResultLen-1].First = aPartNameValue;
443 pSeq[nNewResultLen-1].Second = aContentTypeValue;
444 }
445 else
446 throw css::xml::sax::SAXException(); // TODO: no other elements expected!
447 }
448 else
449 throw css::xml::sax::SAXException(); // TODO: no other elements expected!
450 }
451
452
453 void SAL_CALL OFOPXMLHelper_Impl::endElement( const OUString& aName )
454 {
455 if ( m_nFormat == RELATIONINFO_FORMAT || m_nFormat == CONTENTTYPE_FORMAT )
456 {
457 sal_Int32 nLength = m_aElementsSeq.size();
458 if ( nLength <= 0 )
459 throw css::xml::sax::SAXException(); // TODO: no other end elements expected!
460
461 if ( m_aElementsSeq[nLength-1] != aName )
462 throw css::xml::sax::SAXException(); // TODO: unexpected element ended
463
464 m_aElementsSeq.resize( nLength - 1 );
465 }
466 }
467
468
469 void SAL_CALL OFOPXMLHelper_Impl::characters( const OUString& /*aChars*/ )
470 {
471 }
472
473
474 void SAL_CALL OFOPXMLHelper_Impl::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
475 {
476 }
477
478
479 void SAL_CALL OFOPXMLHelper_Impl::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
480 {
481 }
482
483
484 void SAL_CALL OFOPXMLHelper_Impl::setDocumentLocator( const uno::Reference< css::xml::sax::XLocator >& /*xLocator*/ )
485 {
486 }
487
488 } // namespace comphelper
489
490 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */