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