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>
32 #include <oox/helper/containerhelper.hxx>
33 #include "docprophandler.hxx"
34 
35 #include <cppuhelper/supportsservice.hxx>
36 
37 using namespace ::com::sun::star;
38 
39 namespace oox::docprop {
40 
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::document;
43 using namespace ::com::sun::star::embed;
44 using namespace ::com::sun::star::io;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::xml::sax;
48 
49 namespace {
50 
51 /// @throws RuntimeException
52 /// @throws css::io::IOException
lclGetRelatedStreams(const Reference<XStorage> & rxStorage,const OUString & rStreamType)53 Sequence< 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 ContainerHelper::vectorToSequence( aResult );
86 }
87 
88 } // namespace
89 
DocumentPropertiesImport(const Reference<XComponentContext> & rxContext)90 DocumentPropertiesImport::DocumentPropertiesImport( const Reference< XComponentContext >& rxContext ) :
91     mxContext( rxContext )
92 {
93 }
94 
95 // XServiceInfo
getImplementationName()96 OUString SAL_CALL DocumentPropertiesImport::getImplementationName()
97 {
98     return "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter";
99 }
100 
supportsService(const OUString & rServiceName)101 sal_Bool SAL_CALL DocumentPropertiesImport::supportsService( const OUString& rServiceName )
102 {
103     return cppu::supportsService(this, rServiceName);
104 }
105 
getSupportedServiceNames()106 Sequence< OUString > SAL_CALL DocumentPropertiesImport::getSupportedServiceNames()
107 {
108     Sequence<OUString> aServices { "com.sun.star.document.OOXMLDocumentPropertiesImporter" };
109     return aServices;
110 }
111 
112 // XOOXMLDocumentPropertiesImporter
importProperties(const Reference<XStorage> & rxSource,const Reference<XDocumentProperties> & rxDocumentProperties)113 void SAL_CALL DocumentPropertiesImport::importProperties(
114         const Reference< XStorage >& rxSource, const Reference< XDocumentProperties >& rxDocumentProperties )
115 {
116     if( !mxContext.is() )
117         throw RuntimeException();
118 
119     if( !rxSource.is() || !rxDocumentProperties.is() )
120         throw IllegalArgumentException();
121 
122     Sequence< InputSource > aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "metadata/core-properties" ) );
123     // OOXML strict
124     if( !aCoreStreams.hasElements() )
125         aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "metadata/core-properties" ) );
126     // MS Office seems to have a bug, so we have to do similar handling
127     if( !aCoreStreams.hasElements() )
128         aCoreStreams = lclGetRelatedStreams( rxSource, "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" );
129 
130     Sequence< InputSource > aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "extended-properties" ) );
131     // OOXML strict
132     if( !aExtStreams.hasElements() )
133         aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "extended-properties" ) );
134     Sequence< InputSource > aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "custom-properties" ) );
135     // OOXML strict
136     if( !aCustomStreams.hasElements() )
137         aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "custom-properties" ) );
138 
139     if( !(aCoreStreams.hasElements() || aExtStreams.hasElements() || aCustomStreams.hasElements()) )
140         return;
141 
142     if( aCoreStreams.getLength() > 1 )
143         throw IOException( "Unexpected core properties stream!" );
144 
145     ::oox::core::FastParser aParser;
146     aParser.registerNamespace( NMSP_packageMetaCorePr );
147     aParser.registerNamespace( NMSP_dc );
148     aParser.registerNamespace( NMSP_dcTerms );
149     aParser.registerNamespace( NMSP_officeExtPr );
150     aParser.registerNamespace( NMSP_officeCustomPr );
151     aParser.registerNamespace( NMSP_officeDocPropsVT );
152     aParser.setDocumentHandler( new OOXMLDocPropHandler( mxContext, rxDocumentProperties ) );
153 
154     if( aCoreStreams.hasElements() )
155         aParser.parseStream( aCoreStreams[ 0 ], true );
156     for( const auto& rExtStream : std::as_const(aExtStreams) )
157         aParser.parseStream( rExtStream, true );
158     for( const auto& rCustomStream : std::as_const(aCustomStreams) )
159         aParser.parseStream( rCustomStream, true );
160 }
161 
162 } // namespace oox::docprop
163 
164 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
com_sun_star_comp_oox_docprop_DocumentPropertiesImporter_get_implementation(uno::XComponentContext * pCtx,uno::Sequence<uno::Any> const &)165 com_sun_star_comp_oox_docprop_DocumentPropertiesImporter_get_implementation(
166     uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
167 {
168     return cppu::acquire(new oox::docprop::DocumentPropertiesImport(pCtx));
169 }
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
172