LCOV - code coverage report
Current view: top level - oox/source/helper - zipstorage.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 55 69 79.7 %
Date: 2015-06-13 12:38:46 Functions: 13 14 92.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 "oox/helper/zipstorage.hxx"
      21             : 
      22             : #include <com/sun/star/embed/ElementModes.hpp>
      23             : #include <com/sun/star/embed/XStorage.hpp>
      24             : #include <com/sun/star/embed/XTransactedObject.hpp>
      25             : #include <com/sun/star/io/XInputStream.hpp>
      26             : #include <com/sun/star/io/XOutputStream.hpp>
      27             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      28             : #include <com/sun/star/uno/XComponentContext.hpp>
      29             : #include <osl/diagnose.h>
      30             : #include <comphelper/storagehelper.hxx>
      31             : #include "oox/helper/helper.hxx"
      32             : 
      33             : namespace oox {
      34             : 
      35             : using namespace ::com::sun::star::container;
      36             : using namespace ::com::sun::star::embed;
      37             : using namespace ::com::sun::star::io;
      38             : using namespace ::com::sun::star::lang;
      39             : using namespace ::com::sun::star::uno;
      40             : 
      41       10883 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
      42       10883 :     StorageBase( rxInStream, false )
      43             : {
      44             :     OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
      45             :     // create base storage object
      46       10883 :     if( rxContext.is() ) try
      47             :     {
      48             :         /*  #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
      49             :             cannot be used here as it will open a storage with format type
      50             :             'PackageFormat' that will not work with OOXML packages.
      51             : 
      52             :             #161971# The MS-document storages should always be opened in repair
      53             :             mode to ignore the format errors and get so much info as possible.
      54             :             I hate this solution, but it seems to be the only consistent way to
      55             :             handle the MS documents.
      56             : 
      57             :             TODO: #i105410# switch to 'OFOPXMLFormat' and use its
      58             :             implementation of relations handling.
      59             :          */
      60       20077 :         mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
      61             :             ZIP_STORAGE_FORMAT_STRING, rxInStream, rxContext,
      62        9194 :             false );    // DEV300_m80: Was sal_True, but DOCX and others did not load
      63             :     }
      64        1689 :     catch (Exception const& e)
      65             :     {
      66             :         SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
      67             :                 "exception opening input storage: " << e.Message);
      68             :     }
      69       10883 : }
      70             : 
      71         644 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
      72         644 :     StorageBase( rxStream, false )
      73             : {
      74             :     OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
      75             :     // create base storage object
      76         644 :     if( rxContext.is() ) try
      77             :     {
      78         644 :         const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
      79        1288 :         mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
      80         644 :             OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, rxContext, true );
      81             :     }
      82           0 :     catch (Exception const& e)
      83             :     {
      84             :         SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
      85             :                 "exception opening output storage: " << e.Message);
      86             :     }
      87         644 : }
      88             : 
      89       10323 : ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
      90       10323 :     StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
      91       10323 :     mxStorage( rxStorage )
      92             : {
      93             :     SAL_WARN_IF(!mxStorage.is(), "oox.storage", "ZipStorage::ZipStorage "
      94             :             " - missing storage" );
      95       10323 : }
      96             : 
      97       34747 : ZipStorage::~ZipStorage()
      98             : {
      99       34747 : }
     100             : 
     101        8953 : bool ZipStorage::implIsStorage() const
     102             : {
     103        8953 :     return mxStorage.is();
     104             : }
     105             : 
     106        2162 : Reference< XStorage > ZipStorage::implGetXStorage() const
     107             : {
     108        2162 :     return mxStorage;
     109             : }
     110             : 
     111           0 : void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
     112             : {
     113           0 :     Sequence< OUString > aNames;
     114           0 :     if( mxStorage.is() ) try
     115             :     {
     116           0 :         aNames = mxStorage->getElementNames();
     117           0 :         if( aNames.getLength() > 0 )
     118           0 :             orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
     119             :     }
     120           0 :     catch (Exception const& e)
     121             :     {
     122             :         SAL_INFO("oox.storage", "getElementNames: exception: " << e.Message);
     123           0 :     }
     124           0 : }
     125             : 
     126       11531 : StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
     127             : {
     128       11531 :     Reference< XStorage > xSubXStorage;
     129       11531 :     bool bMissing = false;
     130       11531 :     if( mxStorage.is() ) try
     131             :     {
     132             :         // XStorage::isStorageElement may throw various exceptions...
     133       11531 :         if( mxStorage->isStorageElement( rElementName ) )
     134       24693 :             xSubXStorage = mxStorage->openStorageElement(
     135       16462 :                 rElementName, ::com::sun::star::embed::ElementModes::READ );
     136             :     }
     137        6600 :     catch( NoSuchElementException& )
     138             :     {
     139        3300 :         bMissing = true;
     140             :     }
     141           0 :     catch (Exception const& e)
     142             :     {
     143             :         SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
     144             :     }
     145             : 
     146       11531 :     if( bMissing && bCreateMissing ) try
     147             :     {
     148        6276 :         xSubXStorage = mxStorage->openStorageElement(
     149        4184 :             rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
     150             :     }
     151           0 :     catch (Exception const& e)
     152             :     {
     153             :         SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
     154             :     }
     155             : 
     156       11531 :     StorageRef xSubStorage;
     157       11531 :     if( xSubXStorage.is() )
     158       10323 :         xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
     159       11531 :     return xSubStorage;
     160             : }
     161             : 
     162        9761 : Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
     163             : {
     164        9761 :     Reference< XInputStream > xInStream;
     165        9761 :     if( mxStorage.is() ) try
     166             :     {
     167        9761 :         xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
     168             :     }
     169         434 :     catch (Exception const& e)
     170             :     {
     171             :         SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
     172             :     }
     173        9761 :     return xInStream;
     174             : }
     175             : 
     176        7393 : Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
     177             : {
     178        7393 :     Reference< XOutputStream > xOutStream;
     179        7393 :     if( mxStorage.is() ) try
     180             :     {
     181        7393 :         xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
     182             :     }
     183           0 :     catch (Exception const& e)
     184             :     {
     185             :         SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
     186             :     }
     187        7393 :     return xOutStream;
     188             : }
     189             : 
     190        2655 : void ZipStorage::implCommit() const
     191             : {
     192             :     try
     193             :     {
     194        2655 :         Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
     195             :     }
     196           0 :     catch (Exception const& e)
     197             :     {
     198             :         SAL_WARN("oox.storage", "commit: exception: " << e.Message);
     199             :     }
     200        2655 : }
     201             : 
     202         246 : } // namespace oox
     203             : 
     204             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11