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

Generated by: LCOV version 1.10