LibreOffice Module comphelper (master) 1
ofopxmlhelper.cxx
Go to the documentation of this file.
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
23
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
39using namespace ::com::sun::star;
40
41namespace comphelper {
42
43namespace {
44
45// this helper class is designed to allow to parse ContentType- and Relationship-related information from OfficeOpenXML format
46class 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
55public:
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
73namespace OFOPXMLHelper {
74
76static 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
81uno::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
91uno::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, "[Content_Types].xml", CONTENTTYPE_FORMAT, rContext );
96}
97
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
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();
138
139 uno::Reference< css::xml::sax::XWriter > xWriter = css::xml::sax::Writer::create(rContext);
140
141 xWriter->setOutputStream( xOutStream );
142
143 OUString aRelListElement( "Relationships" );
144 OUString aRelElement( "Relationship" );
145 OUString aWhiteSpace( " " );
146
147 // write the namespace
149 pRootAttrList->AddAttribute(
150 "xmlns",
151 "http://schemas.openxmlformats.org/package/2006/relationships" );
152
153 xWriter->startDocument();
154 xWriter->startElement( aRelListElement, pRootAttrList );
155
156 for ( const auto & i : aSequence )
157 {
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
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();
191
192 uno::Reference< css::xml::sax::XWriter > xWriter = css::xml::sax::Writer::create(rContext);
193
194 xWriter->setOutputStream( xOutStream );
195
196 static constexpr OUStringLiteral aTypesElement(u"Types");
197 static constexpr OUStringLiteral aDefaultElement(u"Default");
198 static constexpr OUStringLiteral aOverrideElement(u"Override");
199 static constexpr OUStringLiteral aContentTypeAttr(u"ContentType");
200 static constexpr OUStringLiteral aWhiteSpace(u" ");
201
202 // write the namespace
204 pRootAttrList->AddAttribute(
205 "xmlns",
206 "http://schemas.openxmlformats.org/package/2006/content-types" );
207
208 xWriter->startDocument();
209 xWriter->startElement( aTypesElement, pRootAttrList );
210
211 for ( const beans::StringPair & pair : aDefaultsSequence )
212 {
214 pAttrList->AddAttribute( "Extension", 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 {
225 pAttrList->AddAttribute( "PartName", 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
239uno::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();
246
247 uno::Reference< css::xml::sax::XParser > xParser = css::xml::sax::Parser::create( rContext );
248
249 rtl::Reference<OFOPXMLHelper_Impl> pHelper = new OFOPXMLHelper_Impl( nFormat );
250 css::xml::sax::InputSource aParserInput;
251 aParserInput.aInputStream = xInStream;
252 aParserInput.sSystemId = aStringID;
253 xParser->setDocumentHandler( pHelper );
254 xParser->parseStream( aParserInput );
255 xParser->setDocumentHandler( uno::Reference < css::xml::sax::XDocumentHandler > () );
256
257 return pHelper->GetParsingResult();
258}
259
260} // namespace OFOPXMLHelper
261
262// Relations info related strings
263constexpr OUStringLiteral g_aRelListElement(u"Relationships");
264constexpr OUStringLiteral g_aRelElement( u"Relationship" );
265constexpr OUStringLiteral g_aIDAttr( u"Id" );
266constexpr OUStringLiteral g_aTypeAttr( u"Type" );
267constexpr OUStringLiteral g_aTargetModeAttr( u"TargetMode" );
268constexpr OUStringLiteral g_aTargetAttr( u"Target" );
269
270// ContentType related strings
271constexpr OUStringLiteral g_aTypesElement( u"Types" );
272constexpr OUStringLiteral g_aDefaultElement( u"Default" );
273constexpr OUStringLiteral g_aOverrideElement( u"Override" );
274constexpr OUStringLiteral g_aExtensionAttr( u"Extension" );
275constexpr OUStringLiteral g_aPartNameAttr( u"PartName" );
276constexpr OUStringLiteral g_aContentTypeAttr( u"ContentType" );
277
278OFOPXMLHelper_Impl::OFOPXMLHelper_Impl( sal_uInt16 nFormat )
279: m_nFormat( nFormat )
280{
281}
282
283uno::Sequence< uno::Sequence< beans::StringPair > > const & OFOPXMLHelper_Impl::GetParsingResult() const
284{
285 if ( !m_aElementsSeq.empty() )
286 throw uno::RuntimeException(); // the parsing has still not finished!
287
288 return m_aResultSeq;
289}
290
291
292void SAL_CALL OFOPXMLHelper_Impl::startDocument()
293{
294}
295
296
297void SAL_CALL OFOPXMLHelper_Impl::endDocument()
298{
299}
300
301
302void SAL_CALL OFOPXMLHelper_Impl::startElement( const OUString& aName, const uno::Reference< css::xml::sax::XAttributeList >& xAttribs )
303{
305 {
306 if ( aName == g_aRelListElement )
307 {
308 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
309
310 if ( nNewLength != 1 )
311 throw css::xml::sax::SAXException(); // TODO: this element must be the first level element
312
313 m_aElementsSeq.push_back( aName );
314
315 return; // nothing to do
316 }
317 else if ( aName == g_aRelElement )
318 {
319 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
320 if ( nNewLength != 2 )
321 throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
322
323 m_aElementsSeq.push_back( aName );
324
325 sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
326 m_aResultSeq.realloc( nNewEntryNum );
327 auto pResultSeq = m_aResultSeq.getArray();
328 sal_Int32 nAttrNum = 0;
329 pResultSeq[nNewEntryNum-1].realloc( 4 ); // the maximal expected number of arguments is 4
330 auto pAttrs = pResultSeq[nNewEntryNum-1].getArray();
331
332 OUString aIDValue = xAttribs->getValueByName( g_aIDAttr );
333 if ( aIDValue.isEmpty() )
334 throw css::xml::sax::SAXException(); // TODO: the ID value must present
335
336 OUString aTypeValue = xAttribs->getValueByName( g_aTypeAttr );
337 OUString aTargetValue = xAttribs->getValueByName( g_aTargetAttr );
338 OUString aTargetModeValue = xAttribs->getValueByName( g_aTargetModeAttr );
339
340 pAttrs[++nAttrNum - 1].First = g_aIDAttr;
341 pAttrs[nAttrNum - 1].Second = aIDValue;
342
343 if ( !aTypeValue.isEmpty() )
344 {
345 pAttrs[++nAttrNum - 1].First = g_aTypeAttr;
346 pAttrs[nAttrNum - 1].Second = aTypeValue;
347 }
348
349 if ( !aTargetValue.isEmpty() )
350 {
351 pAttrs[++nAttrNum - 1].First = g_aTargetAttr;
352 pAttrs[nAttrNum - 1].Second = aTargetValue;
353 }
354
355 if ( !aTargetModeValue.isEmpty() )
356 {
357 pAttrs[++nAttrNum - 1].First = g_aTargetModeAttr;
358 pAttrs[nAttrNum - 1].Second = aTargetModeValue;
359 }
360
361 pResultSeq[nNewEntryNum-1].realloc( nAttrNum );
362 }
363 else
364 throw css::xml::sax::SAXException(); // TODO: no other elements expected!
365 }
366 else if ( m_nFormat == CONTENTTYPE_FORMAT )
367 {
368 if ( aName == g_aTypesElement )
369 {
370 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
371
372 if ( nNewLength != 1 )
373 throw css::xml::sax::SAXException(); // TODO: this element must be the first level element
374
375 m_aElementsSeq.push_back( aName );
376
377 if ( !m_aResultSeq.hasElements() )
378 m_aResultSeq.realloc( 2 );
379
380 return; // nothing to do
381 }
382 else if ( aName == g_aDefaultElement )
383 {
384 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
385 if ( nNewLength != 2 )
386 throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
387
388 m_aElementsSeq.push_back( aName );
389
390 if ( !m_aResultSeq.hasElements() )
391 m_aResultSeq.realloc( 2 );
392
393 if ( m_aResultSeq.getLength() != 2 )
394 throw uno::RuntimeException();
395
396 auto pResultSeq = m_aResultSeq.getArray();
397
398 const OUString aExtensionValue = xAttribs->getValueByName( g_aExtensionAttr );
399 if ( aExtensionValue.isEmpty() )
400 throw css::xml::sax::SAXException(); // TODO: the Extension value must present
401
402 const OUString aContentTypeValue = xAttribs->getValueByName( g_aContentTypeAttr );
403 if ( aContentTypeValue.isEmpty() )
404 throw css::xml::sax::SAXException(); // TODO: the ContentType value must present
405
406 const sal_Int32 nNewResultLen = m_aResultSeq[0].getLength() + 1;
407 pResultSeq[0].realloc( nNewResultLen );
408 auto pSeq = pResultSeq[0].getArray();
409
410 pSeq[nNewResultLen-1].First = aExtensionValue;
411 pSeq[nNewResultLen-1].Second = aContentTypeValue;
412 }
413 else if ( aName == g_aOverrideElement )
414 {
415 sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
416 if ( nNewLength != 2 )
417 throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
418
419 m_aElementsSeq.push_back( aName );
420
421 if ( !m_aResultSeq.hasElements() )
422 m_aResultSeq.realloc( 2 );
423
424 if ( m_aResultSeq.getLength() != 2 )
425 throw uno::RuntimeException();
426
427 auto pResultSeq = m_aResultSeq.getArray();
428
429 OUString aPartNameValue = xAttribs->getValueByName( g_aPartNameAttr );
430 if ( aPartNameValue.isEmpty() )
431 throw css::xml::sax::SAXException(); // TODO: the PartName value must present
432
433 OUString aContentTypeValue = xAttribs->getValueByName( g_aContentTypeAttr );
434 if ( aContentTypeValue.isEmpty() )
435 throw css::xml::sax::SAXException(); // TODO: the ContentType value must present
436
437 sal_Int32 nNewResultLen = m_aResultSeq[1].getLength() + 1;
438 pResultSeq[1].realloc( nNewResultLen );
439 auto pSeq = pResultSeq[1].getArray();
440
441 pSeq[nNewResultLen-1].First = aPartNameValue;
442 pSeq[nNewResultLen-1].Second = aContentTypeValue;
443 }
444 else
445 throw css::xml::sax::SAXException(); // TODO: no other elements expected!
446 }
447 else
448 throw css::xml::sax::SAXException(); // TODO: no other elements expected!
449}
450
451
452void SAL_CALL OFOPXMLHelper_Impl::endElement( const OUString& aName )
453{
455 {
456 sal_Int32 nLength = m_aElementsSeq.size();
457 if ( nLength <= 0 )
458 throw css::xml::sax::SAXException(); // TODO: no other end elements expected!
459
460 if ( m_aElementsSeq[nLength-1] != aName )
461 throw css::xml::sax::SAXException(); // TODO: unexpected element ended
462
463 m_aElementsSeq.resize( nLength - 1 );
464 }
465}
466
467
468void SAL_CALL OFOPXMLHelper_Impl::characters( const OUString& /*aChars*/ )
469{
470}
471
472
473void SAL_CALL OFOPXMLHelper_Impl::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
474{
475}
476
477
478void SAL_CALL OFOPXMLHelper_Impl::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
479{
480}
481
482
483void SAL_CALL OFOPXMLHelper_Impl::setDocumentLocator( const uno::Reference< css::xml::sax::XLocator >& /*xLocator*/ )
484{
485}
486
487} // namespace comphelper
488
489/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
float u
OUString aName
uno::Sequence< uno::Sequence< beans::StringPair > > ReadContentTypeSequence(const uno::Reference< io::XInputStream > &xInStream, const uno::Reference< uno::XComponentContext > &rContext)
void WriteContentSequence(const uno::Reference< io::XOutputStream > &xOutStream, const uno::Sequence< beans::StringPair > &aDefaultsSequence, const uno::Sequence< beans::StringPair > &aOverridesSequence, const uno::Reference< uno::XComponentContext > &rContext)
OUString GetContentTypeByName(const css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > &rContentTypes, const OUString &rFilename)
static uno::Sequence< uno::Sequence< beans::StringPair > > ReadSequence_Impl(const uno::Reference< io::XInputStream > &xInStream, const OUString &aStringID, sal_uInt16 nFormat, const uno::Reference< uno::XComponentContext > &xContext)
uno::Sequence< uno::Sequence< beans::StringPair > > ReadRelationsInfoSequence(const uno::Reference< io::XInputStream > &xInStream, std::u16string_view aStreamName, const uno::Reference< uno::XComponentContext > &rContext)
void WriteRelationsInfoSequence(const uno::Reference< io::XOutputStream > &xOutStream, const uno::Sequence< uno::Sequence< beans::StringPair > > &aSequence, const uno::Reference< uno::XComponentContext > &rContext)
constexpr OUStringLiteral g_aRelListElement(u"Relationships")
constexpr OUStringLiteral g_aIDAttr(u"Id")
constexpr OUStringLiteral g_aTargetModeAttr(u"TargetMode")
constexpr OUStringLiteral g_aTypeAttr(u"Type")
constexpr OUStringLiteral g_aPartNameAttr(u"PartName")
constexpr OUStringLiteral g_aOverrideElement(u"Override")
constexpr OUStringLiteral g_aExtensionAttr(u"Extension")
constexpr OUStringLiteral g_aTargetAttr(u"Target")
constexpr OUStringLiteral g_aContentTypeAttr(u"ContentType")
constexpr OUStringLiteral g_aTypesElement(u"Types")
constexpr OUStringLiteral g_aDefaultElement(u"Default")
constexpr OUStringLiteral g_aRelElement(u"Relationship")
int i
css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > m_aResultSeq
#define CONTENTTYPE_FORMAT
#define RELATIONINFO_FORMAT
sal_uInt16 const m_nFormat
std::vector< OUString > m_aElementsSeq
#define FORMAT_MAX_ID
sal_Int32 nLength