Line data Source code
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 "oox/core/fastparser.hxx"
28 : #include "oox/core/relations.hxx"
29 : #include "oox/helper/containerhelper.hxx"
30 : #include "oox/helper/helper.hxx"
31 : #include "docprophandler.hxx"
32 :
33 : #include <cppuhelper/supportsservice.hxx>
34 :
35 : namespace oox {
36 : namespace docprop {
37 :
38 : using namespace ::com::sun::star::beans;
39 : using namespace ::com::sun::star::document;
40 : using namespace ::com::sun::star::embed;
41 : using namespace ::com::sun::star::io;
42 : using namespace ::com::sun::star::lang;
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::com::sun::star::xml::sax;
45 :
46 0 : OUString SAL_CALL DocumentPropertiesImport_getImplementationName()
47 : {
48 0 : return OUString( "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter" );
49 : }
50 :
51 0 : Sequence< OUString > SAL_CALL DocumentPropertiesImport_getSupportedServiceNames()
52 : {
53 0 : Sequence< OUString > aServices( 1 );
54 0 : aServices[ 0 ] = "com.sun.star.document.OOXMLDocumentPropertiesImporter";
55 0 : return aServices;
56 : }
57 :
58 0 : Reference< XInterface > SAL_CALL DocumentPropertiesImport_createInstance( const Reference< XComponentContext >& rxContext ) SAL_THROW((Exception))
59 : {
60 0 : return static_cast< ::cppu::OWeakObject* >( new DocumentPropertiesImport( rxContext ) );
61 : }
62 :
63 : namespace {
64 :
65 0 : Sequence< InputSource > lclGetRelatedStreams( const Reference< XStorage >& rxStorage, const OUString& rStreamType ) throw (RuntimeException)
66 : {
67 0 : Reference< XRelationshipAccess > xRelation( rxStorage, UNO_QUERY_THROW );
68 0 : Reference< XHierarchicalStorageAccess > xHierarchy( rxStorage, UNO_QUERY_THROW );
69 :
70 0 : Sequence< Sequence< StringPair > > aPropsInfo = xRelation->getRelationshipsByType( rStreamType );
71 :
72 0 : ::std::vector< InputSource > aResult;
73 :
74 0 : for( sal_Int32 nIndex = 0, nLength = aPropsInfo.getLength(); nIndex < nLength; ++nIndex )
75 : {
76 0 : const Sequence< StringPair >& rEntries = aPropsInfo[ nIndex ];
77 0 : for( sal_Int32 nEntryIndex = 0, nEntryLength = rEntries.getLength(); nEntryIndex < nEntryLength; ++nEntryIndex )
78 : {
79 0 : const StringPair& rEntry = rEntries[ nEntryIndex ];
80 0 : if ( rEntry.First == "Target" )
81 : {
82 : Reference< XExtendedStorageStream > xExtStream(
83 0 : xHierarchy->openStreamElementByHierarchicalName( rEntry.Second, ElementModes::READ ), UNO_QUERY_THROW );
84 0 : Reference< XInputStream > xInStream = xExtStream->getInputStream();
85 0 : if( xInStream.is() )
86 : {
87 0 : aResult.resize( aResult.size() + 1 );
88 0 : aResult.back().sSystemId = rEntry.Second;
89 0 : aResult.back().aInputStream = xExtStream->getInputStream();
90 : }
91 0 : break;
92 : }
93 : }
94 : }
95 :
96 0 : return ContainerHelper::vectorToSequence( aResult );
97 : }
98 :
99 : } // namespace
100 :
101 0 : DocumentPropertiesImport::DocumentPropertiesImport( const Reference< XComponentContext >& rxContext ) :
102 0 : mxContext( rxContext )
103 : {
104 0 : }
105 :
106 : // XServiceInfo
107 0 : OUString SAL_CALL DocumentPropertiesImport::getImplementationName() throw (RuntimeException, std::exception)
108 : {
109 0 : return DocumentPropertiesImport_getImplementationName();
110 : }
111 :
112 0 : sal_Bool SAL_CALL DocumentPropertiesImport::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
113 : {
114 0 : return cppu::supportsService(this, rServiceName);
115 : }
116 :
117 0 : Sequence< OUString > SAL_CALL DocumentPropertiesImport::getSupportedServiceNames() throw (RuntimeException, std::exception)
118 : {
119 0 : return DocumentPropertiesImport_getSupportedServiceNames();
120 : }
121 :
122 : // XOOXMLDocumentPropertiesImporter
123 0 : void SAL_CALL DocumentPropertiesImport::importProperties(
124 : const Reference< XStorage >& rxSource, const Reference< XDocumentProperties >& rxDocumentProperties )
125 : throw (RuntimeException, IllegalArgumentException, SAXException, Exception, std::exception)
126 : {
127 0 : if( !mxContext.is() )
128 0 : throw RuntimeException();
129 :
130 0 : if( !rxSource.is() || !rxDocumentProperties.is() )
131 0 : throw IllegalArgumentException();
132 :
133 0 : Sequence< InputSource > aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "metadata/core-properties" ) );
134 : // OOXML strict
135 0 : if( !aCoreStreams.hasElements() )
136 0 : aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "metadata/core-properties" ) );
137 : // MS Office seems to have a bug, so we have to do similar handling
138 0 : if( !aCoreStreams.hasElements() )
139 0 : aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_PACKAGE_RELATION_TYPE( "metadata/core-properties" ) );
140 :
141 0 : Sequence< InputSource > aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "extended-properties" ) );
142 : // OOXML strict
143 0 : if( !aExtStreams.hasElements() )
144 0 : aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "extended-properties" ) );
145 0 : Sequence< InputSource > aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "custom-properties" ) );
146 : // OOXML strict
147 0 : if( !aCustomStreams.hasElements() )
148 0 : aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "custom-properties" ) );
149 :
150 0 : if( aCoreStreams.hasElements() || aExtStreams.hasElements() || aCustomStreams.hasElements() )
151 : {
152 0 : if( aCoreStreams.getLength() > 1 )
153 0 : throw IOException( "Unexpected core properties stream!", Reference< XInterface >() );
154 :
155 0 : ::oox::core::FastParser aParser( mxContext );
156 0 : aParser.registerNamespace( NMSP_packageMetaCorePr );
157 0 : aParser.registerNamespace( NMSP_dc );
158 0 : aParser.registerNamespace( NMSP_dcTerms );
159 0 : aParser.registerNamespace( NMSP_officeExtPr );
160 0 : aParser.registerNamespace( NMSP_officeCustomPr );
161 0 : aParser.registerNamespace( NMSP_officeDocPropsVT );
162 0 : aParser.setDocumentHandler( new OOXMLDocPropHandler( mxContext, rxDocumentProperties ) );
163 :
164 0 : if( aCoreStreams.hasElements() )
165 0 : aParser.parseStream( aCoreStreams[ 0 ], true );
166 0 : for( sal_Int32 nIndex = 0; nIndex < aExtStreams.getLength(); ++nIndex )
167 0 : aParser.parseStream( aExtStreams[ nIndex ], true );
168 0 : for( sal_Int32 nIndex = 0; nIndex < aCustomStreams.getLength(); ++nIndex )
169 0 : aParser.parseStream( aCustomStreams[ nIndex ], true );
170 0 : }
171 0 : }
172 :
173 :
174 :
175 : } // namespace docprop
176 : } // namespace oox
177 :
178 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|