xref: /AOO41X/main/oox/source/docprop/ooxmldocpropimport.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "ooxmldocpropimport.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <vector>
31*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/embed/XRelationshipAccess.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
35*cdf0e10cSrcweir #include "oox/core/fastparser.hxx"
36*cdf0e10cSrcweir #include "oox/core/relations.hxx"
37*cdf0e10cSrcweir #include "oox/helper/containerhelper.hxx"
38*cdf0e10cSrcweir #include "oox/helper/helper.hxx"
39*cdf0e10cSrcweir #include "docprophandler.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir namespace oox {
42*cdf0e10cSrcweir namespace docprop {
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir // ============================================================================
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
47*cdf0e10cSrcweir using namespace ::com::sun::star::document;
48*cdf0e10cSrcweir using namespace ::com::sun::star::embed;
49*cdf0e10cSrcweir using namespace ::com::sun::star::io;
50*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
51*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
52*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir using ::rtl::OUString;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir // ============================================================================
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir OUString SAL_CALL DocumentPropertiesImport_getImplementationName()
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     return CREATE_OUSTRING( "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter" );
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir Sequence< OUString > SAL_CALL DocumentPropertiesImport_getSupportedServiceNames()
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir     Sequence< OUString > aServices( 1 );
66*cdf0e10cSrcweir     aServices[ 0 ] = CREATE_OUSTRING( "com.sun.star.document.OOXMLDocumentPropertiesImporter" );
67*cdf0e10cSrcweir     return aServices;
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir Reference< XInterface > SAL_CALL DocumentPropertiesImport_createInstance( const Reference< XComponentContext >& rxContext ) SAL_THROW((Exception))
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject* >( new DocumentPropertiesImport( rxContext ) );
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir // ============================================================================
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir namespace {
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir Sequence< InputSource > lclGetRelatedStreams( const Reference< XStorage >& rxStorage, const OUString& rStreamType ) throw (RuntimeException)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     Reference< XRelationshipAccess > xRelation( rxStorage, UNO_QUERY_THROW );
82*cdf0e10cSrcweir     Reference< XHierarchicalStorageAccess > xHierarchy( rxStorage, UNO_QUERY_THROW );
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     Sequence< Sequence< StringPair > > aPropsInfo = xRelation->getRelationshipsByType( rStreamType );
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     ::std::vector< InputSource > aResult;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     for( sal_Int32 nIndex = 0, nLength = aPropsInfo.getLength(); nIndex < nLength; ++nIndex )
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         const Sequence< StringPair >& rEntries = aPropsInfo[ nIndex ];
91*cdf0e10cSrcweir         for( sal_Int32 nEntryIndex = 0, nEntryLength = rEntries.getLength(); nEntryIndex < nEntryLength; ++nEntryIndex )
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             const StringPair& rEntry = rEntries[ nEntryIndex ];
94*cdf0e10cSrcweir             if( rEntry.First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Target" ) ) )
95*cdf0e10cSrcweir             {
96*cdf0e10cSrcweir                 Reference< XExtendedStorageStream > xExtStream(
97*cdf0e10cSrcweir                     xHierarchy->openStreamElementByHierarchicalName( rEntry.Second, ElementModes::READ ), UNO_QUERY_THROW );
98*cdf0e10cSrcweir                 Reference< XInputStream > xInStream = xExtStream->getInputStream();
99*cdf0e10cSrcweir                 if( xInStream.is() )
100*cdf0e10cSrcweir                 {
101*cdf0e10cSrcweir                     aResult.resize( aResult.size() + 1 );
102*cdf0e10cSrcweir                     aResult.back().sSystemId = rEntry.Second;
103*cdf0e10cSrcweir                     aResult.back().aInputStream = xExtStream->getInputStream();
104*cdf0e10cSrcweir                 }
105*cdf0e10cSrcweir                 break;
106*cdf0e10cSrcweir             }
107*cdf0e10cSrcweir         }
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     return ContainerHelper::vectorToSequence( aResult );
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir } // namespace
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir // ============================================================================
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir DocumentPropertiesImport::DocumentPropertiesImport( const Reference< XComponentContext >& rxContext ) :
118*cdf0e10cSrcweir     mxContext( rxContext )
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir // XServiceInfo
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir OUString SAL_CALL DocumentPropertiesImport::getImplementationName() throw (RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     return DocumentPropertiesImport_getImplementationName();
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir sal_Bool SAL_CALL DocumentPropertiesImport::supportsService( const OUString& rServiceName ) throw (RuntimeException)
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir     Sequence< OUString > aServiceNames = DocumentPropertiesImport_getSupportedServiceNames();
132*cdf0e10cSrcweir     for( sal_Int32 nIndex = 0, nLength = aServiceNames.getLength(); nIndex < nLength; ++nIndex )
133*cdf0e10cSrcweir         if( aServiceNames[ nIndex ] == rServiceName )
134*cdf0e10cSrcweir             return sal_True;
135*cdf0e10cSrcweir     return sal_False;
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir Sequence< OUString > SAL_CALL DocumentPropertiesImport::getSupportedServiceNames() throw (RuntimeException)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     return DocumentPropertiesImport_getSupportedServiceNames();
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir // XOOXMLDocumentPropertiesImporter
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir void SAL_CALL DocumentPropertiesImport::importProperties(
146*cdf0e10cSrcweir         const Reference< XStorage >& rxSource, const Reference< XDocumentProperties >& rxDocumentProperties )
147*cdf0e10cSrcweir         throw (RuntimeException, IllegalArgumentException, SAXException, Exception)
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir     if( !mxContext.is() )
150*cdf0e10cSrcweir         throw RuntimeException();
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     if( !rxSource.is() || !rxDocumentProperties.is() )
153*cdf0e10cSrcweir         throw IllegalArgumentException();
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     Sequence< InputSource > aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "metadata/core-properties" ) );
156*cdf0e10cSrcweir     // MS Office seems to have a bug, so we have to do similar handling
157*cdf0e10cSrcweir     if( !aCoreStreams.hasElements() )
158*cdf0e10cSrcweir         aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_PACKAGE_RELATION_TYPE( "metadata/core-properties" ) );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     Sequence< InputSource > aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "extended-properties" ) );
161*cdf0e10cSrcweir     Sequence< InputSource > aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "custom-properties" ) );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     if( aCoreStreams.hasElements() || aExtStreams.hasElements() || aCustomStreams.hasElements() )
164*cdf0e10cSrcweir     {
165*cdf0e10cSrcweir         if( aCoreStreams.getLength() > 1 )
166*cdf0e10cSrcweir             throw IOException( CREATE_OUSTRING( "Unexpected core properties stream!" ), Reference< XInterface >() );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         ::oox::core::FastParser aParser( mxContext );
169*cdf0e10cSrcweir         aParser.registerNamespace( NMSP_packageMetaCorePr );
170*cdf0e10cSrcweir         aParser.registerNamespace( NMSP_dc );
171*cdf0e10cSrcweir         aParser.registerNamespace( NMSP_dcTerms );
172*cdf0e10cSrcweir         aParser.registerNamespace( NMSP_officeExtPr );
173*cdf0e10cSrcweir         aParser.registerNamespace( NMSP_officeCustomPr );
174*cdf0e10cSrcweir         aParser.registerNamespace( NMSP_officeDocPropsVT );
175*cdf0e10cSrcweir         aParser.setDocumentHandler( new OOXMLDocPropHandler( mxContext, rxDocumentProperties ) );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir         if( aCoreStreams.hasElements() )
178*cdf0e10cSrcweir             aParser.parseStream( aCoreStreams[ 0 ], true );
179*cdf0e10cSrcweir         for( sal_Int32 nIndex = 0; nIndex < aExtStreams.getLength(); ++nIndex )
180*cdf0e10cSrcweir             aParser.parseStream( aExtStreams[ nIndex ], true );
181*cdf0e10cSrcweir         for( sal_Int32 nIndex = 0; nIndex < aCustomStreams.getLength(); ++nIndex )
182*cdf0e10cSrcweir             aParser.parseStream( aCustomStreams[ nIndex ], true );
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir // ============================================================================
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir } // namespace docprop
189*cdf0e10cSrcweir } // namespace oox
190