1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #include "ooxmldocpropimport.hxx"
25*b1cdbd2cSJim Jagielski
26*b1cdbd2cSJim Jagielski #include <vector>
27*b1cdbd2cSJim Jagielski #include <com/sun/star/embed/ElementModes.hpp>
28*b1cdbd2cSJim Jagielski #include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
29*b1cdbd2cSJim Jagielski #include <com/sun/star/embed/XRelationshipAccess.hpp>
30*b1cdbd2cSJim Jagielski #include <com/sun/star/embed/XStorage.hpp>
31*b1cdbd2cSJim Jagielski #include "oox/core/fastparser.hxx"
32*b1cdbd2cSJim Jagielski #include "oox/core/relations.hxx"
33*b1cdbd2cSJim Jagielski #include "oox/helper/containerhelper.hxx"
34*b1cdbd2cSJim Jagielski #include "oox/helper/helper.hxx"
35*b1cdbd2cSJim Jagielski #include "docprophandler.hxx"
36*b1cdbd2cSJim Jagielski
37*b1cdbd2cSJim Jagielski namespace oox {
38*b1cdbd2cSJim Jagielski namespace docprop {
39*b1cdbd2cSJim Jagielski
40*b1cdbd2cSJim Jagielski // ============================================================================
41*b1cdbd2cSJim Jagielski
42*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::beans;
43*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::document;
44*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::embed;
45*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::io;
46*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::lang;
47*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::uno;
48*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::xml::sax;
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski using ::rtl::OUString;
51*b1cdbd2cSJim Jagielski
52*b1cdbd2cSJim Jagielski // ============================================================================
53*b1cdbd2cSJim Jagielski
DocumentPropertiesImport_getImplementationName()54*b1cdbd2cSJim Jagielski OUString SAL_CALL DocumentPropertiesImport_getImplementationName()
55*b1cdbd2cSJim Jagielski {
56*b1cdbd2cSJim Jagielski return CREATE_OUSTRING( "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter" );
57*b1cdbd2cSJim Jagielski }
58*b1cdbd2cSJim Jagielski
DocumentPropertiesImport_getSupportedServiceNames()59*b1cdbd2cSJim Jagielski Sequence< OUString > SAL_CALL DocumentPropertiesImport_getSupportedServiceNames()
60*b1cdbd2cSJim Jagielski {
61*b1cdbd2cSJim Jagielski Sequence< OUString > aServices( 1 );
62*b1cdbd2cSJim Jagielski aServices[ 0 ] = CREATE_OUSTRING( "com.sun.star.document.OOXMLDocumentPropertiesImporter" );
63*b1cdbd2cSJim Jagielski return aServices;
64*b1cdbd2cSJim Jagielski }
65*b1cdbd2cSJim Jagielski
DocumentPropertiesImport_createInstance(const Reference<XComponentContext> & rxContext)66*b1cdbd2cSJim Jagielski Reference< XInterface > SAL_CALL DocumentPropertiesImport_createInstance( const Reference< XComponentContext >& rxContext ) SAL_THROW((Exception))
67*b1cdbd2cSJim Jagielski {
68*b1cdbd2cSJim Jagielski return static_cast< ::cppu::OWeakObject* >( new DocumentPropertiesImport( rxContext ) );
69*b1cdbd2cSJim Jagielski }
70*b1cdbd2cSJim Jagielski
71*b1cdbd2cSJim Jagielski // ============================================================================
72*b1cdbd2cSJim Jagielski
73*b1cdbd2cSJim Jagielski namespace {
74*b1cdbd2cSJim Jagielski
lclGetRelatedStreams(const Reference<XStorage> & rxStorage,const OUString & rStreamType)75*b1cdbd2cSJim Jagielski Sequence< InputSource > lclGetRelatedStreams( const Reference< XStorage >& rxStorage, const OUString& rStreamType ) throw (RuntimeException)
76*b1cdbd2cSJim Jagielski {
77*b1cdbd2cSJim Jagielski Reference< XRelationshipAccess > xRelation( rxStorage, UNO_QUERY_THROW );
78*b1cdbd2cSJim Jagielski Reference< XHierarchicalStorageAccess > xHierarchy( rxStorage, UNO_QUERY_THROW );
79*b1cdbd2cSJim Jagielski
80*b1cdbd2cSJim Jagielski Sequence< Sequence< StringPair > > aPropsInfo = xRelation->getRelationshipsByType( rStreamType );
81*b1cdbd2cSJim Jagielski
82*b1cdbd2cSJim Jagielski ::std::vector< InputSource > aResult;
83*b1cdbd2cSJim Jagielski
84*b1cdbd2cSJim Jagielski for( sal_Int32 nIndex = 0, nLength = aPropsInfo.getLength(); nIndex < nLength; ++nIndex )
85*b1cdbd2cSJim Jagielski {
86*b1cdbd2cSJim Jagielski const Sequence< StringPair >& rEntries = aPropsInfo[ nIndex ];
87*b1cdbd2cSJim Jagielski for( sal_Int32 nEntryIndex = 0, nEntryLength = rEntries.getLength(); nEntryIndex < nEntryLength; ++nEntryIndex )
88*b1cdbd2cSJim Jagielski {
89*b1cdbd2cSJim Jagielski const StringPair& rEntry = rEntries[ nEntryIndex ];
90*b1cdbd2cSJim Jagielski if( rEntry.First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Target" ) ) )
91*b1cdbd2cSJim Jagielski {
92*b1cdbd2cSJim Jagielski Reference< XExtendedStorageStream > xExtStream(
93*b1cdbd2cSJim Jagielski xHierarchy->openStreamElementByHierarchicalName( rEntry.Second, ElementModes::READ ), UNO_QUERY_THROW );
94*b1cdbd2cSJim Jagielski Reference< XInputStream > xInStream = xExtStream->getInputStream();
95*b1cdbd2cSJim Jagielski if( xInStream.is() )
96*b1cdbd2cSJim Jagielski {
97*b1cdbd2cSJim Jagielski aResult.resize( aResult.size() + 1 );
98*b1cdbd2cSJim Jagielski aResult.back().sSystemId = rEntry.Second;
99*b1cdbd2cSJim Jagielski aResult.back().aInputStream = xExtStream->getInputStream();
100*b1cdbd2cSJim Jagielski }
101*b1cdbd2cSJim Jagielski break;
102*b1cdbd2cSJim Jagielski }
103*b1cdbd2cSJim Jagielski }
104*b1cdbd2cSJim Jagielski }
105*b1cdbd2cSJim Jagielski
106*b1cdbd2cSJim Jagielski return ContainerHelper::vectorToSequence( aResult );
107*b1cdbd2cSJim Jagielski }
108*b1cdbd2cSJim Jagielski
109*b1cdbd2cSJim Jagielski } // namespace
110*b1cdbd2cSJim Jagielski
111*b1cdbd2cSJim Jagielski // ============================================================================
112*b1cdbd2cSJim Jagielski
DocumentPropertiesImport(const Reference<XComponentContext> & rxContext)113*b1cdbd2cSJim Jagielski DocumentPropertiesImport::DocumentPropertiesImport( const Reference< XComponentContext >& rxContext ) :
114*b1cdbd2cSJim Jagielski mxContext( rxContext )
115*b1cdbd2cSJim Jagielski {
116*b1cdbd2cSJim Jagielski }
117*b1cdbd2cSJim Jagielski
118*b1cdbd2cSJim Jagielski // XServiceInfo
119*b1cdbd2cSJim Jagielski
getImplementationName()120*b1cdbd2cSJim Jagielski OUString SAL_CALL DocumentPropertiesImport::getImplementationName() throw (RuntimeException)
121*b1cdbd2cSJim Jagielski {
122*b1cdbd2cSJim Jagielski return DocumentPropertiesImport_getImplementationName();
123*b1cdbd2cSJim Jagielski }
124*b1cdbd2cSJim Jagielski
supportsService(const OUString & rServiceName)125*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL DocumentPropertiesImport::supportsService( const OUString& rServiceName ) throw (RuntimeException)
126*b1cdbd2cSJim Jagielski {
127*b1cdbd2cSJim Jagielski Sequence< OUString > aServiceNames = DocumentPropertiesImport_getSupportedServiceNames();
128*b1cdbd2cSJim Jagielski for( sal_Int32 nIndex = 0, nLength = aServiceNames.getLength(); nIndex < nLength; ++nIndex )
129*b1cdbd2cSJim Jagielski if( aServiceNames[ nIndex ] == rServiceName )
130*b1cdbd2cSJim Jagielski return sal_True;
131*b1cdbd2cSJim Jagielski return sal_False;
132*b1cdbd2cSJim Jagielski }
133*b1cdbd2cSJim Jagielski
getSupportedServiceNames()134*b1cdbd2cSJim Jagielski Sequence< OUString > SAL_CALL DocumentPropertiesImport::getSupportedServiceNames() throw (RuntimeException)
135*b1cdbd2cSJim Jagielski {
136*b1cdbd2cSJim Jagielski return DocumentPropertiesImport_getSupportedServiceNames();
137*b1cdbd2cSJim Jagielski }
138*b1cdbd2cSJim Jagielski
139*b1cdbd2cSJim Jagielski // XOOXMLDocumentPropertiesImporter
140*b1cdbd2cSJim Jagielski
importProperties(const Reference<XStorage> & rxSource,const Reference<XDocumentProperties> & rxDocumentProperties)141*b1cdbd2cSJim Jagielski void SAL_CALL DocumentPropertiesImport::importProperties(
142*b1cdbd2cSJim Jagielski const Reference< XStorage >& rxSource, const Reference< XDocumentProperties >& rxDocumentProperties )
143*b1cdbd2cSJim Jagielski throw (RuntimeException, IllegalArgumentException, SAXException, Exception)
144*b1cdbd2cSJim Jagielski {
145*b1cdbd2cSJim Jagielski if( !mxContext.is() )
146*b1cdbd2cSJim Jagielski throw RuntimeException();
147*b1cdbd2cSJim Jagielski
148*b1cdbd2cSJim Jagielski if( !rxSource.is() || !rxDocumentProperties.is() )
149*b1cdbd2cSJim Jagielski throw IllegalArgumentException();
150*b1cdbd2cSJim Jagielski
151*b1cdbd2cSJim Jagielski Sequence< InputSource > aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "metadata/core-properties" ) );
152*b1cdbd2cSJim Jagielski // MS Office seems to have a bug, so we have to do similar handling
153*b1cdbd2cSJim Jagielski if( !aCoreStreams.hasElements() )
154*b1cdbd2cSJim Jagielski aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_PACKAGE_RELATION_TYPE( "metadata/core-properties" ) );
155*b1cdbd2cSJim Jagielski
156*b1cdbd2cSJim Jagielski Sequence< InputSource > aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "extended-properties" ) );
157*b1cdbd2cSJim Jagielski Sequence< InputSource > aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "custom-properties" ) );
158*b1cdbd2cSJim Jagielski
159*b1cdbd2cSJim Jagielski if( aCoreStreams.hasElements() || aExtStreams.hasElements() || aCustomStreams.hasElements() )
160*b1cdbd2cSJim Jagielski {
161*b1cdbd2cSJim Jagielski if( aCoreStreams.getLength() > 1 )
162*b1cdbd2cSJim Jagielski throw IOException( CREATE_OUSTRING( "Unexpected core properties stream!" ), Reference< XInterface >() );
163*b1cdbd2cSJim Jagielski
164*b1cdbd2cSJim Jagielski ::oox::core::FastParser aParser( mxContext );
165*b1cdbd2cSJim Jagielski aParser.registerNamespace( NMSP_packageMetaCorePr );
166*b1cdbd2cSJim Jagielski aParser.registerNamespace( NMSP_dc );
167*b1cdbd2cSJim Jagielski aParser.registerNamespace( NMSP_dcTerms );
168*b1cdbd2cSJim Jagielski aParser.registerNamespace( NMSP_officeExtPr );
169*b1cdbd2cSJim Jagielski aParser.registerNamespace( NMSP_officeCustomPr );
170*b1cdbd2cSJim Jagielski aParser.registerNamespace( NMSP_officeDocPropsVT );
171*b1cdbd2cSJim Jagielski aParser.setDocumentHandler( new OOXMLDocPropHandler( mxContext, rxDocumentProperties ) );
172*b1cdbd2cSJim Jagielski
173*b1cdbd2cSJim Jagielski if( aCoreStreams.hasElements() )
174*b1cdbd2cSJim Jagielski aParser.parseStream( aCoreStreams[ 0 ], true );
175*b1cdbd2cSJim Jagielski for( sal_Int32 nIndex = 0; nIndex < aExtStreams.getLength(); ++nIndex )
176*b1cdbd2cSJim Jagielski aParser.parseStream( aExtStreams[ nIndex ], true );
177*b1cdbd2cSJim Jagielski for( sal_Int32 nIndex = 0; nIndex < aCustomStreams.getLength(); ++nIndex )
178*b1cdbd2cSJim Jagielski aParser.parseStream( aCustomStreams[ nIndex ], true );
179*b1cdbd2cSJim Jagielski }
180*b1cdbd2cSJim Jagielski }
181*b1cdbd2cSJim Jagielski
182*b1cdbd2cSJim Jagielski // ============================================================================
183*b1cdbd2cSJim Jagielski
184*b1cdbd2cSJim Jagielski } // namespace docprop
185*b1cdbd2cSJim Jagielski } // namespace oox
186