LCOV - code coverage report
Current view: top level - oox/source/docprop - ooxmldocpropimport.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 61 70 87.1 %
Date: 2014-04-11 Functions: 6 9 66.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10