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 
20 #include "ooxmldocpropimport.hxx"
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>
30 #include <oox/core/fastparser.hxx>
31 #include <oox/core/relations.hxx>
33 #include "docprophandler.hxx"
34 
35 #include <comphelper/sequence.hxx>
37 
38 using namespace ::com::sun::star;
39 
40 namespace oox::docprop {
41 
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::document;
44 using namespace ::com::sun::star::embed;
45 using namespace ::com::sun::star::io;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::xml::sax;
49 
50 namespace {
51 
54 Sequence< InputSource > lclGetRelatedStreams( const Reference< XStorage >& rxStorage, const OUString& rStreamType )
55 {
56  Reference< XRelationshipAccess > xRelation( rxStorage, UNO_QUERY_THROW );
57  Reference< XHierarchicalStorageAccess > xHierarchy( rxStorage, UNO_QUERY_THROW );
58 
59  const Sequence< Sequence< StringPair > > aPropsInfo = xRelation->getRelationshipsByType( rStreamType );
60 
61  ::std::vector< InputSource > aResult;
62 
63  for( const Sequence< StringPair >& rEntries : aPropsInfo )
64  {
65  auto pEntry = std::find_if(rEntries.begin(), rEntries.end(),
66  [](const StringPair& rEntry) { return rEntry.First == "Target"; });
67  if (pEntry != rEntries.end())
68  {
69  // The stream path is always a relative one, ignore the leading "/" if it's there.
70  OUString aStreamPath = pEntry->Second;
71  if (aStreamPath.startsWith("/"))
72  aStreamPath = aStreamPath.copy(1);
73 
74  Reference< XExtendedStorageStream > xExtStream(
75  xHierarchy->openStreamElementByHierarchicalName( aStreamPath, ElementModes::READ ), UNO_SET_THROW );
76  Reference< XInputStream > xInStream = xExtStream->getInputStream();
77  if( xInStream.is() )
78  {
79  aResult.emplace_back();
80  aResult.back().sSystemId = pEntry->Second;
81  aResult.back().aInputStream = xExtStream->getInputStream();
82  }
83  }
84  }
85 
86  return comphelper::containerToSequence( aResult );
87 }
88 
89 } // namespace
90 
92  mxContext( rxContext )
93 {
94 }
95 
96 // XServiceInfo
98 {
99  return "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter";
100 }
101 
102 sal_Bool SAL_CALL DocumentPropertiesImport::supportsService( const OUString& rServiceName )
103 {
104  return cppu::supportsService(this, rServiceName);
105 }
106 
108 {
109  Sequence<OUString> aServices { "com.sun.star.document.OOXMLDocumentPropertiesImporter" };
110  return aServices;
111 }
112 
113 // XOOXMLDocumentPropertiesImporter
115  const Reference< XStorage >& rxSource, const Reference< XDocumentProperties >& rxDocumentProperties )
116 {
117  if( !mxContext.is() )
118  throw RuntimeException();
119 
120  if( !rxSource.is() || !rxDocumentProperties.is() )
121  throw IllegalArgumentException();
122 
123  Sequence< InputSource > aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "metadata/core-properties" ) );
124  // OOXML strict
125  if( !aCoreStreams.hasElements() )
126  aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "metadata/core-properties" ) );
127  // MS Office seems to have a bug, so we have to do similar handling
128  if( !aCoreStreams.hasElements() )
129  aCoreStreams = lclGetRelatedStreams( rxSource, "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" );
130 
131  Sequence< InputSource > aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "extended-properties" ) );
132  // OOXML strict
133  if( !aExtStreams.hasElements() )
134  aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "extended-properties" ) );
135  Sequence< InputSource > aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "custom-properties" ) );
136  // OOXML strict
137  if( !aCustomStreams.hasElements() )
138  aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "custom-properties" ) );
139 
140  if( !(aCoreStreams.hasElements() || aExtStreams.hasElements() || aCustomStreams.hasElements()) )
141  return;
142 
143  if( aCoreStreams.getLength() > 1 )
144  throw IOException( "Unexpected core properties stream!" );
145 
146  ::oox::core::FastParser aParser;
147  aParser.registerNamespace( NMSP_packageMetaCorePr );
148  aParser.registerNamespace( NMSP_dc );
149  aParser.registerNamespace( NMSP_dcTerms );
150  aParser.registerNamespace( NMSP_officeExtPr );
151  aParser.registerNamespace( NMSP_officeCustomPr );
152  aParser.registerNamespace( NMSP_officeDocPropsVT );
153  aParser.setDocumentHandler( new OOXMLDocPropHandler( mxContext, rxDocumentProperties ) );
154 
155  if( aCoreStreams.hasElements() )
156  aParser.parseStream( aCoreStreams[ 0 ], true );
157  for( const auto& rExtStream : std::as_const(aExtStreams) )
158  aParser.parseStream( rExtStream, true );
159  for( const auto& rCustomStream : std::as_const(aCustomStreams) )
160  aParser.parseStream( rCustomStream, true );
161 }
162 
163 } // namespace oox::docprop
164 
165 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
167  uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
168 {
169  return cppu::acquire(new oox::docprop::DocumentPropertiesImport(pCtx));
170 }
171 
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void registerNamespace(sal_Int32 nNamespaceId)
Registers an OOXML namespace at the parser.
Definition: fastparser.cxx:81
virtual OUString SAL_CALL getImplementationName() override
uno::Reference< uno::XComponentContext > mxContext
#define CREATE_OFFICEDOC_RELATION_TYPE(ascii)
Expands to an OUString containing an 'officeDocument' transitional relation type created from the pas...
Definition: relations.hxx:36
css::uno::Reference< css::uno::XComponentContext > mxContext
void parseStream(const css::xml::sax::InputSource &rInputSource, bool bCloseStream=false)
Parses the passed SAX input source.
virtual sal_Bool SAL_CALL supportsService(const OUString &rServiceName) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
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
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_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
DocumentPropertiesImport(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Wrapper for a fast SAX parser that works on automatically generated OOXML token and namespace identif...
Definition: fastparser.hxx:53
virtual void SAL_CALL importProperties(const css::uno::Reference< css::embed::XStorage > &rxSource, const css::uno::Reference< css::document::XDocumentProperties > &rxDocumentProperties) override