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