LCOV - code coverage report
Current view: top level - oox/source/helper - storagebase.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 111 117 94.9 %
Date: 2014-04-11 Functions: 20 22 90.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/storagebase.hxx"
      21             : 
      22             : #include <com/sun/star/embed/XTransactedObject.hpp>
      23             : #include <com/sun/star/io/XStream.hpp>
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include "oox/helper/binaryinputstream.hxx"
      26             : #include "oox/helper/binaryoutputstream.hxx"
      27             : 
      28             : namespace oox {
      29             : 
      30             : 
      31             : 
      32             : using namespace ::com::sun::star::embed;
      33             : using namespace ::com::sun::star::io;
      34             : using namespace ::com::sun::star::uno;
      35             : 
      36             : 
      37             : 
      38             : namespace {
      39             : 
      40       23510 : void lclSplitFirstElement( OUString& orElement, OUString& orRemainder, const OUString& _aFullName )
      41             : {
      42       23510 :     OUString aFullName = _aFullName;
      43       23510 :     sal_Int32  nSlashPos = aFullName.indexOf( '/' );
      44             : 
      45             :     // strip leading slashes
      46       47483 :     while( nSlashPos == 0 )
      47             :     {
      48         463 :         aFullName = aFullName.copy(1);
      49         463 :         nSlashPos = aFullName.indexOf( '/' );
      50             :     }
      51             : 
      52       23510 :     if( (0 <= nSlashPos) && (nSlashPos < aFullName.getLength()) )
      53             :     {
      54       13274 :         orElement = aFullName.copy( 0, nSlashPos );
      55       13274 :         orRemainder = aFullName.copy( nSlashPos + 1 );
      56             :     }
      57             :     else
      58             :     {
      59       10236 :         orElement = aFullName;
      60       23510 :     }
      61       23510 : }
      62             : 
      63             : } // namespace
      64             : 
      65             : 
      66             : 
      67        6414 : StorageBase::StorageBase( const Reference< XInputStream >& rxInStream, bool bBaseStreamAccess ) :
      68             :     mxInStream( rxInStream ),
      69             :     mbBaseStreamAccess( bBaseStreamAccess ),
      70        6414 :     mbReadOnly( true )
      71             : {
      72             :     OSL_ENSURE( mxInStream.is(), "StorageBase::StorageBase - missing base input stream" );
      73        6414 : }
      74             : 
      75         337 : StorageBase::StorageBase( const Reference< XStream >& rxOutStream, bool bBaseStreamAccess ) :
      76             :     mxOutStream( rxOutStream ),
      77             :     mbBaseStreamAccess( bBaseStreamAccess ),
      78         337 :     mbReadOnly( false )
      79             : {
      80             :     OSL_ENSURE( mxOutStream.is(), "StorageBase::StorageBase - missing base output stream" );
      81         337 : }
      82             : 
      83        4975 : StorageBase::StorageBase( const StorageBase& rParentStorage, const OUString& rStorageName, bool bReadOnly ) :
      84             :     maParentPath( rParentStorage.getPath() ),
      85             :     maStorageName( rStorageName ),
      86             :     mbBaseStreamAccess( false ),
      87        4975 :     mbReadOnly( bReadOnly )
      88             : {
      89        4975 : }
      90             : 
      91       11726 : StorageBase::~StorageBase()
      92             : {
      93       11726 : }
      94             : 
      95        5733 : bool StorageBase::isStorage() const
      96             : {
      97        5733 :     return implIsStorage();
      98             : }
      99             : 
     100           0 : bool StorageBase::isRootStorage() const
     101             : {
     102           0 :     return implIsStorage() && maStorageName.isEmpty();
     103             : }
     104             : 
     105        6007 : bool StorageBase::isReadOnly() const
     106             : {
     107        6007 :     return mbReadOnly;
     108             : }
     109             : 
     110         862 : Reference< XStorage > StorageBase::getXStorage() const
     111             : {
     112         862 :     return implGetXStorage();
     113             : }
     114             : 
     115          62 : const OUString& StorageBase::getName() const
     116             : {
     117          62 :     return maStorageName;
     118             : }
     119             : 
     120        4975 : OUString StorageBase::getPath() const
     121             : {
     122        4975 :     OUStringBuffer aBuffer( maParentPath );
     123        4975 :     if( !aBuffer.isEmpty() )
     124         176 :         aBuffer.append( '/' );
     125        4975 :     aBuffer.append( maStorageName );
     126        4975 :     return aBuffer.makeStringAndClear();
     127             : }
     128             : 
     129          89 : void StorageBase::getElementNames( ::std::vector< OUString >& orElementNames ) const
     130             : {
     131          89 :     orElementNames.clear();
     132          89 :     implGetElementNames( orElementNames );
     133          89 : }
     134             : 
     135         721 : StorageRef StorageBase::openSubStorage( const OUString& rStorageName, bool bCreateMissing )
     136             : {
     137         721 :     StorageRef xSubStorage;
     138             :     OSL_ENSURE( !bCreateMissing || !mbReadOnly, "StorageBase::openSubStorage - cannot create substorage in read-only mode" );
     139         721 :     if( !bCreateMissing || !mbReadOnly )
     140             :     {
     141        1442 :         OUString aElement, aRemainder;
     142         721 :         lclSplitFirstElement( aElement, aRemainder, rStorageName );
     143         721 :         if( !aElement.isEmpty() )
     144         721 :             xSubStorage = getSubStorage( aElement, bCreateMissing );
     145         721 :         if( xSubStorage.get() && !aRemainder.isEmpty() )
     146         721 :             xSubStorage = xSubStorage->openSubStorage( aRemainder, bCreateMissing );
     147             :     }
     148         721 :     return xSubStorage;
     149             : }
     150             : 
     151       12255 : Reference< XInputStream > StorageBase::openInputStream( const OUString& rStreamName )
     152             : {
     153       12255 :     Reference< XInputStream > xInStream;
     154       24510 :     OUString aElement, aRemainder;
     155       12255 :     lclSplitFirstElement( aElement, aRemainder, rStreamName );
     156       12255 :     if( !aElement.isEmpty() )
     157             :     {
     158       12255 :         if( !aRemainder.isEmpty() )
     159             :         {
     160        7254 :             StorageRef xSubStorage = getSubStorage( aElement, false );
     161        7254 :             if( xSubStorage.get() )
     162        6742 :                 xInStream = xSubStorage->openInputStream( aRemainder );
     163             :         }
     164             :         else
     165             :         {
     166        5001 :             xInStream = implOpenInputStream( aElement );
     167             :         }
     168             :     }
     169           0 :     else if( mbBaseStreamAccess )
     170             :     {
     171           0 :         xInStream = mxInStream;
     172             :     }
     173       24510 :     return xInStream;
     174             : }
     175             : 
     176       10534 : Reference< XOutputStream > StorageBase::openOutputStream( const OUString& rStreamName )
     177             : {
     178       10534 :     Reference< XOutputStream > xOutStream;
     179             :     OSL_ENSURE( !mbReadOnly, "StorageBase::openOutputStream - cannot create output stream in read-only mode" );
     180       10534 :     if( !mbReadOnly )
     181             :     {
     182       21068 :         OUString aElement, aRemainder;
     183       10534 :         lclSplitFirstElement( aElement, aRemainder, rStreamName );
     184       10534 :         if( !aElement.isEmpty() )
     185             :         {
     186       10534 :             if( !aRemainder.isEmpty() )
     187             :             {
     188        6020 :                 StorageRef xSubStorage = getSubStorage( aElement, true );
     189        6020 :                 if( xSubStorage.get() )
     190        6020 :                     xOutStream = xSubStorage->openOutputStream( aRemainder );
     191             :             }
     192             :             else
     193             :             {
     194        4514 :                 xOutStream = implOpenOutputStream( aElement );
     195             :             }
     196             :         }
     197           0 :         else if( mbBaseStreamAccess )
     198             :         {
     199           0 :             xOutStream = mxOutStream->getOutputStream();
     200       10534 :         }
     201             :     }
     202       10534 :     return xOutStream;
     203             : }
     204             : 
     205         472 : void StorageBase::copyToStorage( StorageBase& rDestStrg, const OUString& rElementName )
     206             : {
     207             :     OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" );
     208             :     OSL_ENSURE( !rElementName.isEmpty(), "StorageBase::copyToStorage - invalid element name" );
     209         472 :     if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() && !rElementName.isEmpty() )
     210             :     {
     211         472 :         StorageRef xSubStrg = openSubStorage( rElementName, false );
     212         472 :         if( xSubStrg.get() )
     213             :         {
     214          31 :             StorageRef xDestSubStrg = rDestStrg.openSubStorage( rElementName, true );
     215          31 :             if( xDestSubStrg.get() )
     216          31 :                 xSubStrg->copyStorageToStorage( *xDestSubStrg );
     217             :         }
     218             :         else
     219             :         {
     220         441 :             Reference< XInputStream > xInStrm = openInputStream( rElementName );
     221         441 :             if( xInStrm.is() )
     222             :             {
     223         441 :                 Reference< XOutputStream > xOutStrm = rDestStrg.openOutputStream( rElementName );
     224         441 :                 if( xOutStrm.is() )
     225             :                 {
     226         441 :                     BinaryXInputStream aInStrm( xInStrm, true );
     227         882 :                     BinaryXOutputStream aOutStrm( xOutStrm, true );
     228         882 :                     aInStrm.copyToStream( aOutStrm );
     229         441 :                 }
     230         441 :             }
     231         472 :         }
     232             :     }
     233         472 : }
     234             : 
     235          60 : void StorageBase::copyStorageToStorage( StorageBase& rDestStrg )
     236             : {
     237             :     OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" );
     238          60 :     if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() )
     239             :     {
     240          60 :         ::std::vector< OUString > aElements;
     241          60 :         getElementNames( aElements );
     242         532 :         for( ::std::vector< OUString >::iterator aIt = aElements.begin(), aEnd = aElements.end(); aIt != aEnd; ++aIt )
     243         532 :             copyToStorage( rDestStrg, *aIt );
     244             :     }
     245          60 : }
     246             : 
     247        1360 : void StorageBase::commit()
     248             : {
     249             :     OSL_ENSURE( !mbReadOnly, "StorageBase::commit - cannot commit in read-only mode" );
     250        1360 :     if( !mbReadOnly )
     251             :     {
     252             :         // commit all open substorages
     253        1360 :         maSubStorages.forEachMem( &StorageBase::commit );
     254             :         // commit this storage
     255        1360 :         implCommit();
     256             :     }
     257        1360 : }
     258             : 
     259             : // private --------------------------------------------------------------------
     260             : 
     261       13995 : StorageRef StorageBase::getSubStorage( const OUString& rElementName, bool bCreateMissing )
     262             : {
     263       13995 :     StorageRef& rxSubStrg = maSubStorages[ rElementName ];
     264       13995 :     if( !rxSubStrg )
     265        6057 :         rxSubStrg = implOpenSubStorage( rElementName, bCreateMissing );
     266       13995 :     return rxSubStrg;
     267             : }
     268             : 
     269             : 
     270             : 
     271         177 : } // namespace oox
     272             : 
     273             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10