2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-29 14:02:24 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2006-11-07 13:50:10 +00:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
#include <comphelper/ofopxmlhelper.hxx>
|
|
|
|
#include <comphelper/attributelist.hxx>
|
|
|
|
|
|
|
|
#include <cppuhelper/implbase.hxx>
|
2021-02-22 19:45:09 +02:00
|
|
|
#include <rtl/ref.hxx>
|
2015-09-01 17:53:14 +02:00
|
|
|
|
2006-10-13 10:41:33 +00:00
|
|
|
#include <com/sun/star/beans/StringPair.hpp>
|
2012-10-03 13:30:43 +02:00
|
|
|
#include <com/sun/star/xml/sax/Parser.hpp>
|
2006-10-13 10:41:33 +00:00
|
|
|
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
|
2017-02-06 17:08:38 +01:00
|
|
|
#include <com/sun/star/xml/sax/SAXException.hpp>
|
2012-10-10 10:13:18 +02:00
|
|
|
#include <com/sun/star/xml/sax/Writer.hpp>
|
2006-10-13 10:41:33 +00:00
|
|
|
#include <com/sun/star/lang/IllegalArgumentException.hpp>
|
2016-02-06 19:15:11 +02:00
|
|
|
#include <vector>
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
#define RELATIONINFO_FORMAT 0
|
|
|
|
#define CONTENTTYPE_FORMAT 1
|
|
|
|
#define FORMAT_MAX_ID CONTENTTYPE_FORMAT
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
|
|
|
namespace comphelper {
|
|
|
|
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
// this helper class is designed to allow to parse ContentType- and Relationship-related information from OfficeOpenXML format
|
|
|
|
class OFOPXMLHelper_Impl
|
2015-11-10 19:20:12 -05:00
|
|
|
: public cppu::WeakImplHelper< css::xml::sax::XDocumentHandler >
|
2015-09-01 17:53:14 +02:00
|
|
|
{
|
2018-07-19 16:28:37 +02:00
|
|
|
sal_uInt16 const m_nFormat; // which format to parse
|
2015-09-01 17:53:14 +02:00
|
|
|
|
2015-11-10 19:20:12 -05:00
|
|
|
css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > m_aResultSeq;
|
2016-02-06 19:15:11 +02:00
|
|
|
std::vector< OUString > m_aElementsSeq; // stack of elements being parsed
|
2015-09-01 17:53:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
public:
|
2017-10-24 13:02:58 +02:00
|
|
|
css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > const & GetParsingResult() const;
|
2015-09-01 17:53:14 +02:00
|
|
|
|
2015-10-18 07:53:51 +02:00
|
|
|
explicit OFOPXMLHelper_Impl( sal_uInt16 nFormat ); // must not be created directly
|
2015-09-01 17:53:14 +02:00
|
|
|
|
|
|
|
// XDocumentHandler
|
2017-01-26 12:28:58 +01:00
|
|
|
virtual void SAL_CALL startDocument() override;
|
|
|
|
virtual void SAL_CALL endDocument() override;
|
|
|
|
virtual void SAL_CALL startElement( const OUString& aName, const css::uno::Reference< css::xml::sax::XAttributeList >& xAttribs ) override;
|
|
|
|
virtual void SAL_CALL endElement( const OUString& aName ) override;
|
|
|
|
virtual void SAL_CALL characters( const OUString& aChars ) override;
|
|
|
|
virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) override;
|
|
|
|
virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) override;
|
|
|
|
virtual void SAL_CALL setDocumentLocator( const css::uno::Reference< css::xml::sax::XLocator >& xLocator ) override;
|
2015-09-01 17:53:14 +02:00
|
|
|
};
|
|
|
|
|
Extend loplugin:external to warn about classes
...following up on 314f15bff08b76bf96acf99141776ef64d2f1355 "Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2019-11-19 16:32:49 +01:00
|
|
|
}
|
2015-09-01 17:53:14 +02:00
|
|
|
|
|
|
|
namespace OFOPXMLHelper {
|
|
|
|
|
2017-01-19 18:00:00 +01:00
|
|
|
/// @throws css::uno::Exception
|
2015-09-01 17:53:14 +02:00
|
|
|
static uno::Sequence<uno::Sequence< beans::StringPair>> ReadSequence_Impl(
|
|
|
|
const uno::Reference<io::XInputStream>& xInStream,
|
|
|
|
const OUString& aStringID, sal_uInt16 nFormat,
|
2017-01-26 12:28:58 +01:00
|
|
|
const uno::Reference<uno::XComponentContext>& xContext);
|
2015-09-01 17:53:14 +02:00
|
|
|
|
|
|
|
uno::Sequence< uno::Sequence< beans::StringPair > > ReadRelationsInfoSequence(
|
|
|
|
const uno::Reference< io::XInputStream >& xInStream,
|
2020-12-28 17:56:40 +01:00
|
|
|
std::u16string_view aStreamName,
|
2015-09-01 17:53:14 +02:00
|
|
|
const uno::Reference< uno::XComponentContext >& rContext )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2020-12-28 17:56:40 +01:00
|
|
|
OUString aStringID = OUString::Concat("_rels/") + aStreamName;
|
2015-03-09 21:10:04 +00:00
|
|
|
return ReadSequence_Impl( xInStream, aStringID, RELATIONINFO_FORMAT, rContext );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
uno::Sequence< uno::Sequence< beans::StringPair > > ReadContentTypeSequence(
|
|
|
|
const uno::Reference< io::XInputStream >& xInStream,
|
|
|
|
const uno::Reference< uno::XComponentContext >& rContext )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2020-06-02 18:47:06 +02:00
|
|
|
return ReadSequence_Impl( xInStream, "[Content_Types].xml", CONTENTTYPE_FORMAT, rContext );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 22:43:34 -05:00
|
|
|
OUString GetContentTypeByName(
|
|
|
|
const css::uno::Sequence<css::uno::Sequence<css::beans::StringPair>>& rContentTypes,
|
|
|
|
const OUString& rFilename)
|
|
|
|
{
|
|
|
|
if (rContentTypes.getLength() < 2)
|
|
|
|
{
|
|
|
|
return OUString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const uno::Sequence<beans::StringPair>& rDefaults = rContentTypes[0];
|
|
|
|
const uno::Sequence<beans::StringPair>& rOverrides = rContentTypes[1];
|
|
|
|
|
|
|
|
// Find the extension and use it to get the type.
|
|
|
|
const sal_Int32 nDotOffset = rFilename.lastIndexOf('.');
|
|
|
|
const OUString aExt = (nDotOffset >= 0 ? rFilename.copy(nDotOffset + 1) : rFilename); // Skip the dot.
|
|
|
|
|
|
|
|
const std::vector<OUString> aNames = { aExt, "/" + rFilename };
|
|
|
|
for (const OUString& aName : aNames)
|
|
|
|
{
|
|
|
|
const auto it1 = std::find_if(rOverrides.begin(), rOverrides.end(), [&aName](const beans::StringPair& rPair)
|
|
|
|
{ return rPair.First == aName; });
|
|
|
|
if (it1 != rOverrides.end())
|
|
|
|
return it1->Second;
|
|
|
|
|
|
|
|
const auto it2 = std::find_if(rDefaults.begin(), rDefaults.end(), [&aName](const beans::StringPair& rPair)
|
|
|
|
{ return rPair.First == aName; });
|
|
|
|
if (it2 != rDefaults.end())
|
|
|
|
return it2->Second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OUString();
|
|
|
|
}
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
void WriteRelationsInfoSequence(
|
|
|
|
const uno::Reference< io::XOutputStream >& xOutStream,
|
|
|
|
const uno::Sequence< uno::Sequence< beans::StringPair > >& aSequence,
|
|
|
|
const uno::Reference< uno::XComponentContext >& rContext )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
if ( !xOutStream.is() )
|
|
|
|
throw uno::RuntimeException();
|
|
|
|
|
2015-11-10 19:20:12 -05:00
|
|
|
uno::Reference< css::xml::sax::XWriter > xWriter = css::xml::sax::Writer::create(rContext);
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->setOutputStream( xOutStream );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2013-01-29 21:41:06 +01:00
|
|
|
OUString aRelListElement( "Relationships" );
|
|
|
|
OUString aRelElement( "Relationship" );
|
|
|
|
OUString aWhiteSpace( " " );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
// write the namespace
|
2021-02-22 19:45:09 +02:00
|
|
|
rtl::Reference<AttributeList> pRootAttrList = new AttributeList;
|
2006-10-13 10:41:33 +00:00
|
|
|
pRootAttrList->AddAttribute(
|
2015-11-06 09:34:55 +01:00
|
|
|
"xmlns",
|
|
|
|
"http://schemas.openxmlformats.org/package/2006/relationships" );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->startDocument();
|
2021-02-22 19:45:09 +02:00
|
|
|
xWriter->startElement( aRelListElement, pRootAttrList );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2020-05-29 14:04:44 +02:00
|
|
|
for ( const auto & i : aSequence )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2021-02-22 19:45:09 +02:00
|
|
|
rtl::Reference<AttributeList> pAttrList = new AttributeList;
|
2020-05-29 14:04:44 +02:00
|
|
|
for( const beans::StringPair & pair : i )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2020-05-29 14:04:44 +02:00
|
|
|
if ( !(pair.First == "Id"
|
|
|
|
|| pair.First == "Type"
|
|
|
|
|| pair.First == "TargetMode"
|
|
|
|
|| pair.First == "Target") )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
// TODO/LATER: should the extensions be allowed?
|
|
|
|
throw lang::IllegalArgumentException();
|
|
|
|
}
|
2023-01-15 13:43:31 +03:00
|
|
|
pAttrList->AddAttribute( pair.First, pair.Second );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 19:45:09 +02:00
|
|
|
xWriter->startElement( aRelElement, pAttrList );
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->ignorableWhitespace( aWhiteSpace );
|
|
|
|
xWriter->endElement( aRelElement );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->ignorableWhitespace( aWhiteSpace );
|
|
|
|
xWriter->endElement( aRelListElement );
|
|
|
|
xWriter->endDocument();
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
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 )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
if ( !xOutStream.is() )
|
|
|
|
throw uno::RuntimeException();
|
|
|
|
|
2015-11-10 19:20:12 -05:00
|
|
|
uno::Reference< css::xml::sax::XWriter > xWriter = css::xml::sax::Writer::create(rContext);
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->setOutputStream( xOutStream );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2020-09-20 12:53:27 +01:00
|
|
|
static constexpr OUStringLiteral aTypesElement(u"Types");
|
|
|
|
static constexpr OUStringLiteral aDefaultElement(u"Default");
|
|
|
|
static constexpr OUStringLiteral aOverrideElement(u"Override");
|
|
|
|
static constexpr OUStringLiteral aContentTypeAttr(u"ContentType");
|
|
|
|
static constexpr OUStringLiteral aWhiteSpace(u" ");
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
// write the namespace
|
2021-02-22 19:45:09 +02:00
|
|
|
rtl::Reference<AttributeList> pRootAttrList = new AttributeList;
|
2006-10-13 10:41:33 +00:00
|
|
|
pRootAttrList->AddAttribute(
|
2015-11-06 09:34:55 +01:00
|
|
|
"xmlns",
|
|
|
|
"http://schemas.openxmlformats.org/package/2006/content-types" );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->startDocument();
|
2021-02-22 19:45:09 +02:00
|
|
|
xWriter->startElement( aTypesElement, pRootAttrList );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2020-05-29 14:04:44 +02:00
|
|
|
for ( const beans::StringPair & pair : aDefaultsSequence )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2021-02-22 19:45:09 +02:00
|
|
|
rtl::Reference<AttributeList> pAttrList = new AttributeList;
|
2023-01-15 13:43:31 +03:00
|
|
|
pAttrList->AddAttribute( "Extension", pair.First );
|
|
|
|
pAttrList->AddAttribute( aContentTypeAttr, pair.Second );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2021-02-22 19:45:09 +02:00
|
|
|
xWriter->startElement( aDefaultElement, pAttrList );
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->ignorableWhitespace( aWhiteSpace );
|
|
|
|
xWriter->endElement( aDefaultElement );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 14:04:44 +02:00
|
|
|
for ( const beans::StringPair & pair : aOverridesSequence )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2021-02-22 19:45:09 +02:00
|
|
|
rtl::Reference<AttributeList> pAttrList = new AttributeList;
|
2023-01-15 13:43:31 +03:00
|
|
|
pAttrList->AddAttribute( "PartName", pair.First );
|
|
|
|
pAttrList->AddAttribute( aContentTypeAttr, pair.Second );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2021-02-22 19:45:09 +02:00
|
|
|
xWriter->startElement( aOverrideElement, pAttrList );
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->ignorableWhitespace( aWhiteSpace );
|
|
|
|
xWriter->endElement( aOverrideElement );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 10:13:18 +02:00
|
|
|
xWriter->ignorableWhitespace( aWhiteSpace );
|
|
|
|
xWriter->endElement( aTypesElement );
|
|
|
|
xWriter->endDocument();
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
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 >& rContext )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2015-03-09 21:10:04 +00:00
|
|
|
if ( !rContext.is() || !xInStream.is() || nFormat > FORMAT_MAX_ID )
|
2006-10-13 10:41:33 +00:00
|
|
|
throw uno::RuntimeException();
|
|
|
|
|
2015-11-10 19:20:12 -05:00
|
|
|
uno::Reference< css::xml::sax::XParser > xParser = css::xml::sax::Parser::create( rContext );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2021-02-22 19:45:09 +02:00
|
|
|
rtl::Reference<OFOPXMLHelper_Impl> pHelper = new OFOPXMLHelper_Impl( nFormat );
|
2015-11-10 19:20:12 -05:00
|
|
|
css::xml::sax::InputSource aParserInput;
|
2006-10-13 10:41:33 +00:00
|
|
|
aParserInput.aInputStream = xInStream;
|
|
|
|
aParserInput.sSystemId = aStringID;
|
2021-02-22 19:45:09 +02:00
|
|
|
xParser->setDocumentHandler( pHelper );
|
2006-10-13 10:41:33 +00:00
|
|
|
xParser->parseStream( aParserInput );
|
2015-11-10 19:20:12 -05:00
|
|
|
xParser->setDocumentHandler( uno::Reference < css::xml::sax::XDocumentHandler > () );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
return pHelper->GetParsingResult();
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
} // namespace OFOPXMLHelper
|
|
|
|
|
2018-08-16 16:57:15 +02:00
|
|
|
// Relations info related strings
|
2020-09-20 12:53:27 +01:00
|
|
|
constexpr OUStringLiteral g_aRelListElement(u"Relationships");
|
|
|
|
constexpr OUStringLiteral g_aRelElement( u"Relationship" );
|
|
|
|
constexpr OUStringLiteral g_aIDAttr( u"Id" );
|
|
|
|
constexpr OUStringLiteral g_aTypeAttr( u"Type" );
|
|
|
|
constexpr OUStringLiteral g_aTargetModeAttr( u"TargetMode" );
|
|
|
|
constexpr OUStringLiteral g_aTargetAttr( u"Target" );
|
2018-08-16 16:57:15 +02:00
|
|
|
|
|
|
|
// ContentType related strings
|
2020-09-20 12:53:27 +01:00
|
|
|
constexpr OUStringLiteral g_aTypesElement( u"Types" );
|
|
|
|
constexpr OUStringLiteral g_aDefaultElement( u"Default" );
|
|
|
|
constexpr OUStringLiteral g_aOverrideElement( u"Override" );
|
|
|
|
constexpr OUStringLiteral g_aExtensionAttr( u"Extension" );
|
|
|
|
constexpr OUStringLiteral g_aPartNameAttr( u"PartName" );
|
|
|
|
constexpr OUStringLiteral g_aContentTypeAttr( u"ContentType" );
|
2018-08-16 16:57:15 +02:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
OFOPXMLHelper_Impl::OFOPXMLHelper_Impl( sal_uInt16 nFormat )
|
2006-10-13 10:41:33 +00:00
|
|
|
: m_nFormat( nFormat )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:02:58 +02:00
|
|
|
uno::Sequence< uno::Sequence< beans::StringPair > > const & OFOPXMLHelper_Impl::GetParsingResult() const
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2018-10-19 08:12:17 +02:00
|
|
|
if ( !m_aElementsSeq.empty() )
|
2006-10-13 10:41:33 +00:00
|
|
|
throw uno::RuntimeException(); // the parsing has still not finished!
|
|
|
|
|
|
|
|
return m_aResultSeq;
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::startDocument()
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::endDocument()
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-11-10 19:20:12 -05:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::startElement( const OUString& aName, const uno::Reference< css::xml::sax::XAttributeList >& xAttribs )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
if ( m_nFormat == RELATIONINFO_FORMAT )
|
|
|
|
{
|
2018-08-16 16:57:15 +02:00
|
|
|
if ( aName == g_aRelListElement )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2016-02-06 19:15:11 +02:00
|
|
|
sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
if ( nNewLength != 1 )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: this element must be the first level element
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2016-02-06 19:15:11 +02:00
|
|
|
m_aElementsSeq.push_back( aName );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
return; // nothing to do
|
|
|
|
}
|
2018-08-16 16:57:15 +02:00
|
|
|
else if ( aName == g_aRelElement )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2016-02-06 19:15:11 +02:00
|
|
|
sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
|
2006-10-13 10:41:33 +00:00
|
|
|
if ( nNewLength != 2 )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2016-02-06 19:15:11 +02:00
|
|
|
m_aElementsSeq.push_back( aName );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
|
|
|
|
m_aResultSeq.realloc( nNewEntryNum );
|
2021-10-28 21:14:28 +03:00
|
|
|
auto pResultSeq = m_aResultSeq.getArray();
|
2006-10-13 10:41:33 +00:00
|
|
|
sal_Int32 nAttrNum = 0;
|
2021-10-28 21:14:28 +03:00
|
|
|
pResultSeq[nNewEntryNum-1].realloc( 4 ); // the maximal expected number of arguments is 4
|
|
|
|
auto pAttrs = pResultSeq[nNewEntryNum-1].getArray();
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2018-08-16 16:57:15 +02:00
|
|
|
OUString aIDValue = xAttribs->getValueByName( g_aIDAttr );
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( aIDValue.isEmpty() )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: the ID value must present
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2018-08-16 16:57:15 +02:00
|
|
|
OUString aTypeValue = xAttribs->getValueByName( g_aTypeAttr );
|
|
|
|
OUString aTargetValue = xAttribs->getValueByName( g_aTargetAttr );
|
|
|
|
OUString aTargetModeValue = xAttribs->getValueByName( g_aTargetModeAttr );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2021-10-28 21:14:28 +03:00
|
|
|
pAttrs[++nAttrNum - 1].First = g_aIDAttr;
|
|
|
|
pAttrs[nAttrNum - 1].Second = aIDValue;
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( !aTypeValue.isEmpty() )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2021-10-28 21:14:28 +03:00
|
|
|
pAttrs[++nAttrNum - 1].First = g_aTypeAttr;
|
|
|
|
pAttrs[nAttrNum - 1].Second = aTypeValue;
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( !aTargetValue.isEmpty() )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2021-10-28 21:14:28 +03:00
|
|
|
pAttrs[++nAttrNum - 1].First = g_aTargetAttr;
|
|
|
|
pAttrs[nAttrNum - 1].Second = aTargetValue;
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( !aTargetModeValue.isEmpty() )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2021-10-28 21:14:28 +03:00
|
|
|
pAttrs[++nAttrNum - 1].First = g_aTargetModeAttr;
|
|
|
|
pAttrs[nAttrNum - 1].Second = aTargetModeValue;
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:14:28 +03:00
|
|
|
pResultSeq[nNewEntryNum-1].realloc( nAttrNum );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
else
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: no other elements expected!
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
else if ( m_nFormat == CONTENTTYPE_FORMAT )
|
|
|
|
{
|
2018-08-16 16:57:15 +02:00
|
|
|
if ( aName == g_aTypesElement )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2016-02-06 19:15:11 +02:00
|
|
|
sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
if ( nNewLength != 1 )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: this element must be the first level element
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2016-02-06 19:15:11 +02:00
|
|
|
m_aElementsSeq.push_back( aName );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2019-05-04 23:16:48 +03:00
|
|
|
if ( !m_aResultSeq.hasElements() )
|
2006-10-13 10:41:33 +00:00
|
|
|
m_aResultSeq.realloc( 2 );
|
|
|
|
|
|
|
|
return; // nothing to do
|
|
|
|
}
|
2018-08-16 16:57:15 +02:00
|
|
|
else if ( aName == g_aDefaultElement )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2016-02-06 19:15:11 +02:00
|
|
|
sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
|
2006-10-13 10:41:33 +00:00
|
|
|
if ( nNewLength != 2 )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2016-02-06 19:15:11 +02:00
|
|
|
m_aElementsSeq.push_back( aName );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2019-05-04 23:16:48 +03:00
|
|
|
if ( !m_aResultSeq.hasElements() )
|
2006-10-13 10:41:33 +00:00
|
|
|
m_aResultSeq.realloc( 2 );
|
|
|
|
|
|
|
|
if ( m_aResultSeq.getLength() != 2 )
|
|
|
|
throw uno::RuntimeException();
|
|
|
|
|
2021-10-28 21:14:28 +03:00
|
|
|
auto pResultSeq = m_aResultSeq.getArray();
|
|
|
|
|
2018-08-16 16:57:15 +02:00
|
|
|
const OUString aExtensionValue = xAttribs->getValueByName( g_aExtensionAttr );
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( aExtensionValue.isEmpty() )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: the Extension value must present
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2018-08-16 16:57:15 +02:00
|
|
|
const OUString aContentTypeValue = xAttribs->getValueByName( g_aContentTypeAttr );
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( aContentTypeValue.isEmpty() )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: the ContentType value must present
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2018-02-06 17:23:58 -05:00
|
|
|
const sal_Int32 nNewResultLen = m_aResultSeq[0].getLength() + 1;
|
2021-10-28 21:14:28 +03:00
|
|
|
pResultSeq[0].realloc( nNewResultLen );
|
|
|
|
auto pSeq = pResultSeq[0].getArray();
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2021-10-28 21:14:28 +03:00
|
|
|
pSeq[nNewResultLen-1].First = aExtensionValue;
|
|
|
|
pSeq[nNewResultLen-1].Second = aContentTypeValue;
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
2018-08-16 16:57:15 +02:00
|
|
|
else if ( aName == g_aOverrideElement )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
2016-02-06 19:15:11 +02:00
|
|
|
sal_Int32 nNewLength = m_aElementsSeq.size() + 1;
|
2006-10-13 10:41:33 +00:00
|
|
|
if ( nNewLength != 2 )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: this element must be the second level element
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2016-02-06 19:15:11 +02:00
|
|
|
m_aElementsSeq.push_back( aName );
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2019-05-04 23:16:48 +03:00
|
|
|
if ( !m_aResultSeq.hasElements() )
|
2006-10-13 10:41:33 +00:00
|
|
|
m_aResultSeq.realloc( 2 );
|
|
|
|
|
|
|
|
if ( m_aResultSeq.getLength() != 2 )
|
|
|
|
throw uno::RuntimeException();
|
|
|
|
|
2021-10-28 21:14:28 +03:00
|
|
|
auto pResultSeq = m_aResultSeq.getArray();
|
|
|
|
|
2018-08-16 16:57:15 +02:00
|
|
|
OUString aPartNameValue = xAttribs->getValueByName( g_aPartNameAttr );
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( aPartNameValue.isEmpty() )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: the PartName value must present
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2018-08-16 16:57:15 +02:00
|
|
|
OUString aContentTypeValue = xAttribs->getValueByName( g_aContentTypeAttr );
|
2011-12-12 23:01:55 -02:00
|
|
|
if ( aContentTypeValue.isEmpty() )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: the ContentType value must present
|
2006-10-13 10:41:33 +00:00
|
|
|
|
|
|
|
sal_Int32 nNewResultLen = m_aResultSeq[1].getLength() + 1;
|
2021-10-28 21:14:28 +03:00
|
|
|
pResultSeq[1].realloc( nNewResultLen );
|
|
|
|
auto pSeq = pResultSeq[1].getArray();
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2021-10-28 21:14:28 +03:00
|
|
|
pSeq[nNewResultLen-1].First = aPartNameValue;
|
|
|
|
pSeq[nNewResultLen-1].Second = aContentTypeValue;
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
else
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: no other elements expected!
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
else
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: no other elements expected!
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::endElement( const OUString& aName )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
if ( m_nFormat == RELATIONINFO_FORMAT || m_nFormat == CONTENTTYPE_FORMAT )
|
|
|
|
{
|
2016-02-06 19:15:11 +02:00
|
|
|
sal_Int32 nLength = m_aElementsSeq.size();
|
2006-10-13 10:41:33 +00:00
|
|
|
if ( nLength <= 0 )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: no other end elements expected!
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2017-07-13 09:55:21 +02:00
|
|
|
if ( m_aElementsSeq[nLength-1] != aName )
|
2015-11-10 19:20:12 -05:00
|
|
|
throw css::xml::sax::SAXException(); // TODO: unexpected element ended
|
2006-10-13 10:41:33 +00:00
|
|
|
|
2016-02-06 19:15:11 +02:00
|
|
|
m_aElementsSeq.resize( nLength - 1 );
|
2006-10-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::characters( const OUString& /*aChars*/ )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-09-01 17:53:14 +02:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2015-11-10 19:20:12 -05:00
|
|
|
void SAL_CALL OFOPXMLHelper_Impl::setDocumentLocator( const uno::Reference< css::xml::sax::XLocator >& /*xLocator*/ )
|
2006-10-13 10:41:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace comphelper
|
|
|
|
|
2010-10-14 08:27:31 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|