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

Generated by: LCOV version 1.11