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