LibreOffice Module oox (master) 1
ooxmldocpropimport.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
21
22#include <vector>
23#include <com/sun/star/embed/ElementModes.hpp>
24#include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
25#include <com/sun/star/embed/XRelationshipAccess.hpp>
26#include <com/sun/star/embed/XStorage.hpp>
27#include <com/sun/star/io/IOException.hpp>
28#include <com/sun/star/lang/IllegalArgumentException.hpp>
29#include <com/sun/star/xml/sax/InputSource.hpp>
32#include "docprophandler.hxx"
33
36
37using namespace ::com::sun::star;
38
39namespace oox::docprop {
40
41using namespace ::com::sun::star::beans;
42using namespace ::com::sun::star::document;
43using namespace ::com::sun::star::embed;
44using namespace ::com::sun::star::io;
45using namespace ::com::sun::star::lang;
46using namespace ::com::sun::star::uno;
47using namespace ::com::sun::star::xml::sax;
48
49namespace {
50
53Sequence< InputSource > lclGetRelatedStreams( const Reference< XStorage >& rxStorage, const OUString& rStreamType )
54{
55 Reference< XRelationshipAccess > xRelation( rxStorage, UNO_QUERY_THROW );
56 Reference< XHierarchicalStorageAccess > xHierarchy( rxStorage, UNO_QUERY_THROW );
57
58 const Sequence< Sequence< StringPair > > aPropsInfo = xRelation->getRelationshipsByType( rStreamType );
59
60 ::std::vector< InputSource > aResult;
61
62 for( const Sequence< StringPair >& rEntries : aPropsInfo )
63 {
64 auto pEntry = std::find_if(rEntries.begin(), rEntries.end(),
65 [](const StringPair& rEntry) { return rEntry.First == "Target"; });
66 if (pEntry != rEntries.end())
67 {
68 // The stream path is always a relative one, ignore the leading "/" if it's there.
69 OUString aStreamPath = pEntry->Second;
70 if (aStreamPath.startsWith("/"))
71 aStreamPath = aStreamPath.copy(1);
72
73 Reference< XExtendedStorageStream > xExtStream(
74 xHierarchy->openStreamElementByHierarchicalName( aStreamPath, ElementModes::READ ), UNO_SET_THROW );
75 Reference< XInputStream > xInStream = xExtStream->getInputStream();
76 if( xInStream.is() )
77 {
78 aResult.emplace_back();
79 aResult.back().sSystemId = pEntry->Second;
80 aResult.back().aInputStream = xExtStream->getInputStream();
81 }
82 }
83 }
84
85 return comphelper::containerToSequence( aResult );
86}
87
88Sequence< InputSource > lclGetCoreStreams(const Reference< XStorage >& rxSource)
89{
90 Sequence< InputSource > aCoreStreams = lclGetRelatedStreams(rxSource, CREATE_OFFICEDOC_RELATION_TYPE("metadata/core-properties"));
91 // OOXML strict
92 if (!aCoreStreams.hasElements())
93 aCoreStreams = lclGetRelatedStreams(rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT("metadata/core-properties"));
94 // MS Office seems to have a bug, so we have to do similar handling
95 if (!aCoreStreams.hasElements())
96 aCoreStreams = lclGetRelatedStreams(rxSource, "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties");
97
98 return aCoreStreams;
99}
100
101Sequence< InputSource > lclGetExtStreams(const Reference< XStorage >& rxSource)
102{
103 Sequence< InputSource > aExtStreams = lclGetRelatedStreams(rxSource, CREATE_OFFICEDOC_RELATION_TYPE("extended-properties"));
104 // OOXML strict
105 if (!aExtStreams.hasElements())
106 aExtStreams = lclGetRelatedStreams(rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT("extended-properties"));
107
108 return aExtStreams;
109}
110
111Sequence< InputSource > lclGetCustomStreams(const Reference< XStorage >& rxSource)
112{
113 Sequence< InputSource > aCustomStreams = lclGetRelatedStreams(rxSource, CREATE_OFFICEDOC_RELATION_TYPE("custom-properties"));
114 // OOXML strict
115 if (!aCustomStreams.hasElements())
116 aCustomStreams = lclGetRelatedStreams(rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT("custom-properties"));
117
118 return aCustomStreams;
119}
120
121
122} // namespace
123
124DocumentPropertiesImport::DocumentPropertiesImport( const Reference< XComponentContext >& rxContext ) :
125 mxContext( rxContext )
126{
127}
128
129// XServiceInfo
131{
132 return "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter";
133}
134
135sal_Bool SAL_CALL DocumentPropertiesImport::supportsService( const OUString& rServiceName )
136{
137 return cppu::supportsService(this, rServiceName);
138}
139
141{
142 Sequence<OUString> aServices { "com.sun.star.document.OOXMLDocumentPropertiesImporter" };
143 return aServices;
144}
145
146// XOOXMLDocumentPropertiesImporter
148 const Reference< XStorage >& rxSource, const Reference< XDocumentProperties >& rxDocumentProperties )
149{
150 if( !mxContext.is() )
151 throw RuntimeException();
152
153 if( !rxSource.is() || !rxDocumentProperties.is() )
154 throw IllegalArgumentException();
155
156 Sequence< InputSource > aCoreStreams = lclGetCoreStreams(rxSource);
157
158 Sequence< InputSource > aExtStreams = lclGetExtStreams(rxSource);
159
160 Sequence< InputSource > aCustomStreams = lclGetCustomStreams(rxSource);
161
162 if( !(aCoreStreams.hasElements() || aExtStreams.hasElements() || aCustomStreams.hasElements()) )
163 return;
164
165 if( aCoreStreams.getLength() > 1 )
166 throw IOException( "Unexpected core properties stream!" );
167
169 aParser.registerNamespace( NMSP_packageMetaCorePr );
170 aParser.registerNamespace( NMSP_dc );
171 aParser.registerNamespace( NMSP_dcTerms );
172 aParser.registerNamespace( NMSP_officeExtPr );
173 aParser.registerNamespace( NMSP_officeCustomPr );
174 aParser.registerNamespace( NMSP_officeDocPropsVT );
175 aParser.setDocumentHandler( new OOXMLDocPropHandler( mxContext, rxDocumentProperties ) );
176
177 if( aCoreStreams.hasElements() )
178 aParser.parseStream( aCoreStreams[ 0 ], true );
179 for( const auto& rExtStream : std::as_const(aExtStreams) )
180 aParser.parseStream( rExtStream, true );
181 for( const auto& rCustomStream : std::as_const(aCustomStreams) )
182 aParser.parseStream( rCustomStream, true );
183}
184
185Reference < com::sun::star::io::XInputStream > SAL_CALL DocumentPropertiesImport::getCorePropertiesStream(
186 const Reference< XStorage >& rxSource)
187{
188 Sequence< InputSource > aCoreStreams = lclGetCoreStreams(rxSource);
189 if (!aCoreStreams.hasElements())
190 return nullptr;
191
192 return aCoreStreams[0].aInputStream;
193}
194
195Reference < com::sun::star::io::XInputStream > SAL_CALL DocumentPropertiesImport::getExtendedPropertiesStream(
196 const Reference< XStorage >& rxSource)
197{
198 Sequence< InputSource > aExtStreams = lclGetExtStreams(rxSource);
199 if (!aExtStreams.hasElements())
200 return nullptr;
201
202 return aExtStreams[0].aInputStream;
203}
204
205css::uno::Sequence< css::uno::Reference< com::sun::star::io::XInputStream > > SAL_CALL DocumentPropertiesImport::getCustomPropertiesStreams(
206 const Reference< XStorage >& rxSource)
207{
208 Sequence <InputSource> aExtStreams = lclGetCustomStreams(rxSource);
209
210 // Repack the sequence
211 std::vector<Reference<XInputStream>> aResult(aExtStreams.getLength());
212 for (const auto& aInputSource : aExtStreams)
213 {
214 aResult.push_back(aInputSource.aInputStream);
215 }
216
217 return comphelper::containerToSequence(aResult);
218}
219
220} // namespace oox::docprop
221
222extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
224 uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
225{
226 return cppu::acquire(new oox::docprop::DocumentPropertiesImport(pCtx));
227}
228
229/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Wrapper for a fast SAX parser that works on automatically generated OOXML token and namespace identif...
Definition: fastparser.hxx:54
void parseStream(const css::xml::sax::InputSource &rInputSource, bool bCloseStream=false)
Parses the passed SAX input source.
void registerNamespace(sal_Int32 nNamespaceId)
Registers an OOXML namespace at the parser.
Definition: fastparser.cxx:81
void setDocumentHandler(const css::uno::Reference< css::xml::sax::XFastDocumentHandler > &rxDocHandler)
Sets the passed document handler that will receive the SAX parser events.
Definition: fastparser.cxx:101
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL getCorePropertiesStream(const css::uno::Reference< css::embed::XStorage > &rxSource) override
virtual sal_Bool SAL_CALL supportsService(const OUString &rServiceName) override
DocumentPropertiesImport(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual css::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL getExtendedPropertiesStream(const css::uno::Reference< css::embed::XStorage > &rxSource) override
virtual void SAL_CALL importProperties(const css::uno::Reference< css::embed::XStorage > &rxSource, const css::uno::Reference< css::document::XDocumentProperties > &rxDocumentProperties) override
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< css::uno::Reference< com::sun::star::io::XInputStream > > SAL_CALL getCustomPropertiesStreams(const css::uno::Reference< css::embed::XStorage > &rxSource) override
uno::Reference< uno::XComponentContext > mxContext
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_oox_docprop_DocumentPropertiesImporter_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)
#define CREATE_OFFICEDOC_RELATION_TYPE(ascii)
Expands to an OUString containing an 'officeDocument' transitional relation type created from the pas...
Definition: relations.hxx:36
#define CREATE_OFFICEDOC_RELATION_TYPE_STRICT(ascii)
Expands to an OUString containing an 'officeDocument' strict relation type created from the passed li...
Definition: relations.hxx:41
unsigned char sal_Bool