bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / docprop / ooxmldocpropimport.cxx
blob3b87c04c652cf7e99ca7ee0b7959b426d5e56c0d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ooxmldocpropimport.hxx"
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"
33 #include <cppuhelper/supportsservice.hxx>
35 using namespace ::com::sun::star;
37 namespace oox {
38 namespace docprop {
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::document;
42 using namespace ::com::sun::star::embed;
43 using namespace ::com::sun::star::io;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::xml::sax;
48 namespace {
50 /// @throws RuntimeException
51 /// @throws css::io::IOException
52 Sequence< InputSource > lclGetRelatedStreams( const Reference< XStorage >& rxStorage, const OUString& rStreamType )
54 Reference< XRelationshipAccess > xRelation( rxStorage, UNO_QUERY_THROW );
55 Reference< XHierarchicalStorageAccess > xHierarchy( rxStorage, UNO_QUERY_THROW );
57 Sequence< Sequence< StringPair > > aPropsInfo = xRelation->getRelationshipsByType( rStreamType );
59 ::std::vector< InputSource > aResult;
61 for( sal_Int32 nIndex = 0, nLength = aPropsInfo.getLength(); nIndex < nLength; ++nIndex )
63 const Sequence< StringPair >& rEntries = aPropsInfo[ nIndex ];
64 for( sal_Int32 nEntryIndex = 0, nEntryLength = rEntries.getLength(); nEntryIndex < nEntryLength; ++nEntryIndex )
66 const StringPair& rEntry = rEntries[ nEntryIndex ];
67 if ( rEntry.First == "Target" )
69 // The stream path is always a relative one, ignore the leading "/" if it's there.
70 OUString aStreamPath = rEntry.Second;
71 if (aStreamPath.startsWith("/"))
72 aStreamPath = aStreamPath.copy(1);
74 Reference< XExtendedStorageStream > xExtStream(
75 xHierarchy->openStreamElementByHierarchicalName( aStreamPath, ElementModes::READ ), UNO_SET_THROW );
76 Reference< XInputStream > xInStream = xExtStream->getInputStream();
77 if( xInStream.is() )
79 aResult.emplace_back();
80 aResult.back().sSystemId = rEntry.Second;
81 aResult.back().aInputStream = xExtStream->getInputStream();
83 break;
88 return ContainerHelper::vectorToSequence( aResult );
91 } // namespace
93 DocumentPropertiesImport::DocumentPropertiesImport( const Reference< XComponentContext >& rxContext ) :
94 mxContext( rxContext )
98 // XServiceInfo
99 OUString SAL_CALL DocumentPropertiesImport::getImplementationName()
101 return OUString( "com.sun.star.comp.oox.docprop.DocumentPropertiesImporter" );
104 sal_Bool SAL_CALL DocumentPropertiesImport::supportsService( const OUString& rServiceName )
106 return cppu::supportsService(this, rServiceName);
109 Sequence< OUString > SAL_CALL DocumentPropertiesImport::getSupportedServiceNames()
111 Sequence<OUString> aServices { "com.sun.star.document.OOXMLDocumentPropertiesImporter" };
112 return aServices;
115 // XOOXMLDocumentPropertiesImporter
116 void SAL_CALL DocumentPropertiesImport::importProperties(
117 const Reference< XStorage >& rxSource, const Reference< XDocumentProperties >& rxDocumentProperties )
119 if( !mxContext.is() )
120 throw RuntimeException();
122 if( !rxSource.is() || !rxDocumentProperties.is() )
123 throw IllegalArgumentException();
125 Sequence< InputSource > aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "metadata/core-properties" ) );
126 // OOXML strict
127 if( !aCoreStreams.hasElements() )
128 aCoreStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "metadata/core-properties" ) );
129 // MS Office seems to have a bug, so we have to do similar handling
130 if( !aCoreStreams.hasElements() )
131 aCoreStreams = lclGetRelatedStreams( rxSource, "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" );
133 Sequence< InputSource > aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "extended-properties" ) );
134 // OOXML strict
135 if( !aExtStreams.hasElements() )
136 aExtStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "extended-properties" ) );
137 Sequence< InputSource > aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE( "custom-properties" ) );
138 // OOXML strict
139 if( !aCustomStreams.hasElements() )
140 aCustomStreams = lclGetRelatedStreams( rxSource, CREATE_OFFICEDOC_RELATION_TYPE_STRICT( "custom-properties" ) );
142 if( aCoreStreams.hasElements() || aExtStreams.hasElements() || aCustomStreams.hasElements() )
144 if( aCoreStreams.getLength() > 1 )
145 throw IOException( "Unexpected core properties stream!" );
147 ::oox::core::FastParser aParser;
148 aParser.registerNamespace( NMSP_packageMetaCorePr );
149 aParser.registerNamespace( NMSP_dc );
150 aParser.registerNamespace( NMSP_dcTerms );
151 aParser.registerNamespace( NMSP_officeExtPr );
152 aParser.registerNamespace( NMSP_officeCustomPr );
153 aParser.registerNamespace( NMSP_officeDocPropsVT );
154 aParser.setDocumentHandler( new OOXMLDocPropHandler( mxContext, rxDocumentProperties ) );
156 if( aCoreStreams.hasElements() )
157 aParser.parseStream( aCoreStreams[ 0 ], true );
158 for( sal_Int32 nIndex = 0; nIndex < aExtStreams.getLength(); ++nIndex )
159 aParser.parseStream( aExtStreams[ nIndex ], true );
160 for( sal_Int32 nIndex = 0; nIndex < aCustomStreams.getLength(); ++nIndex )
161 aParser.parseStream( aCustomStreams[ nIndex ], true );
165 } // namespace docprop
166 } // namespace oox
168 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
169 com_sun_star_comp_oox_docprop_DocumentPropertiesImporter_get_implementation(
170 uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
172 return cppu::acquire(new oox::docprop::DocumentPropertiesImport(pCtx));
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */