1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "oox/helper/storagebase.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp> 31*cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp> 32*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 33*cdf0e10cSrcweir #include "oox/helper/binaryinputstream.hxx" 34*cdf0e10cSrcweir #include "oox/helper/binaryoutputstream.hxx" 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir namespace oox { 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir // ============================================================================ 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir using namespace ::com::sun::star::embed; 41*cdf0e10cSrcweir using namespace ::com::sun::star::io; 42*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir using ::rtl::OUString; 45*cdf0e10cSrcweir using ::rtl::OUStringBuffer; 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir // ============================================================================ 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir namespace { 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir void lclSplitFirstElement( OUString& orElement, OUString& orRemainder, const OUString& rFullName ) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir sal_Int32 nSlashPos = rFullName.indexOf( '/' ); 54*cdf0e10cSrcweir if( (0 <= nSlashPos) && (nSlashPos < rFullName.getLength()) ) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir orElement = rFullName.copy( 0, nSlashPos ); 57*cdf0e10cSrcweir orRemainder = rFullName.copy( nSlashPos + 1 ); 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir else 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir orElement = rFullName; 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir } // namespace 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir StorageBase::StorageBase( const Reference< XInputStream >& rxInStream, bool bBaseStreamAccess ) : 70*cdf0e10cSrcweir mxInStream( rxInStream ), 71*cdf0e10cSrcweir mbBaseStreamAccess( bBaseStreamAccess ), 72*cdf0e10cSrcweir mbReadOnly( true ) 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir OSL_ENSURE( mxInStream.is(), "StorageBase::StorageBase - missing base input stream" ); 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir StorageBase::StorageBase( const Reference< XStream >& rxOutStream, bool bBaseStreamAccess ) : 78*cdf0e10cSrcweir mxOutStream( rxOutStream ), 79*cdf0e10cSrcweir mbBaseStreamAccess( bBaseStreamAccess ), 80*cdf0e10cSrcweir mbReadOnly( false ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir OSL_ENSURE( mxOutStream.is(), "StorageBase::StorageBase - missing base output stream" ); 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir StorageBase::StorageBase( const StorageBase& rParentStorage, const OUString& rStorageName, bool bReadOnly ) : 86*cdf0e10cSrcweir maParentPath( rParentStorage.getPath() ), 87*cdf0e10cSrcweir maStorageName( rStorageName ), 88*cdf0e10cSrcweir mbBaseStreamAccess( false ), 89*cdf0e10cSrcweir mbReadOnly( bReadOnly ) 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir StorageBase::~StorageBase() 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir bool StorageBase::isStorage() const 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir return implIsStorage(); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir bool StorageBase::isRootStorage() const 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir return implIsStorage() && (maStorageName.getLength() == 0); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir bool StorageBase::isReadOnly() const 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir return mbReadOnly; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir Reference< XStorage > StorageBase::getXStorage() const 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir return implGetXStorage(); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir const OUString& StorageBase::getName() const 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir return maStorageName; 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir OUString StorageBase::getPath() const 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir OUStringBuffer aBuffer( maParentPath ); 125*cdf0e10cSrcweir if( aBuffer.getLength() > 0 ) 126*cdf0e10cSrcweir aBuffer.append( sal_Unicode( '/' ) ); 127*cdf0e10cSrcweir aBuffer.append( maStorageName ); 128*cdf0e10cSrcweir return aBuffer.makeStringAndClear(); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir void StorageBase::getElementNames( ::std::vector< OUString >& orElementNames ) const 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir orElementNames.clear(); 134*cdf0e10cSrcweir implGetElementNames( orElementNames ); 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir StorageRef StorageBase::openSubStorage( const OUString& rStorageName, bool bCreateMissing ) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir StorageRef xSubStorage; 140*cdf0e10cSrcweir OSL_ENSURE( !bCreateMissing || !mbReadOnly, "StorageBase::openSubStorage - cannot create substorage in read-only mode" ); 141*cdf0e10cSrcweir if( !bCreateMissing || !mbReadOnly ) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir OUString aElement, aRemainder; 144*cdf0e10cSrcweir lclSplitFirstElement( aElement, aRemainder, rStorageName ); 145*cdf0e10cSrcweir if( aElement.getLength() > 0 ) 146*cdf0e10cSrcweir xSubStorage = getSubStorage( aElement, bCreateMissing ); 147*cdf0e10cSrcweir if( xSubStorage.get() && (aRemainder.getLength() > 0) ) 148*cdf0e10cSrcweir xSubStorage = xSubStorage->openSubStorage( aRemainder, bCreateMissing ); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir return xSubStorage; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir Reference< XInputStream > StorageBase::openInputStream( const OUString& rStreamName ) 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir Reference< XInputStream > xInStream; 156*cdf0e10cSrcweir OUString aElement, aRemainder; 157*cdf0e10cSrcweir lclSplitFirstElement( aElement, aRemainder, rStreamName ); 158*cdf0e10cSrcweir if( aElement.getLength() > 0 ) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir if( aRemainder.getLength() > 0 ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir StorageRef xSubStorage = getSubStorage( aElement, false ); 163*cdf0e10cSrcweir if( xSubStorage.get() ) 164*cdf0e10cSrcweir xInStream = xSubStorage->openInputStream( aRemainder ); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir else 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir xInStream = implOpenInputStream( aElement ); 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir else if( mbBaseStreamAccess ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir xInStream = mxInStream; 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir return xInStream; 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir Reference< XOutputStream > StorageBase::openOutputStream( const OUString& rStreamName ) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir Reference< XOutputStream > xOutStream; 181*cdf0e10cSrcweir OSL_ENSURE( !mbReadOnly, "StorageBase::openOutputStream - cannot create output stream in read-only mode" ); 182*cdf0e10cSrcweir if( !mbReadOnly ) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir OUString aElement, aRemainder; 185*cdf0e10cSrcweir lclSplitFirstElement( aElement, aRemainder, rStreamName ); 186*cdf0e10cSrcweir if( aElement.getLength() > 0 ) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir if( aRemainder.getLength() > 0 ) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir StorageRef xSubStorage = getSubStorage( aElement, true ); 191*cdf0e10cSrcweir if( xSubStorage.get() ) 192*cdf0e10cSrcweir xOutStream = xSubStorage->openOutputStream( aRemainder ); 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir else 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir xOutStream = implOpenOutputStream( aElement ); 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir else if( mbBaseStreamAccess ) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir xOutStream = mxOutStream->getOutputStream(); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir return xOutStream; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir void StorageBase::copyToStorage( StorageBase& rDestStrg, const OUString& rElementName ) 208*cdf0e10cSrcweir { 209*cdf0e10cSrcweir OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" ); 210*cdf0e10cSrcweir OSL_ENSURE( rElementName.getLength() > 0, "StorageBase::copyToStorage - invalid element name" ); 211*cdf0e10cSrcweir if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() && (rElementName.getLength() > 0) ) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir StorageRef xSubStrg = openSubStorage( rElementName, false ); 214*cdf0e10cSrcweir if( xSubStrg.get() ) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir StorageRef xDestSubStrg = rDestStrg.openSubStorage( rElementName, true ); 217*cdf0e10cSrcweir if( xDestSubStrg.get() ) 218*cdf0e10cSrcweir xSubStrg->copyStorageToStorage( *xDestSubStrg ); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir else 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir Reference< XInputStream > xInStrm = openInputStream( rElementName ); 223*cdf0e10cSrcweir if( xInStrm.is() ) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir Reference< XOutputStream > xOutStrm = rDestStrg.openOutputStream( rElementName ); 226*cdf0e10cSrcweir if( xOutStrm.is() ) 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir BinaryXInputStream aInStrm( xInStrm, true ); 229*cdf0e10cSrcweir BinaryXOutputStream aOutStrm( xOutStrm, true ); 230*cdf0e10cSrcweir aInStrm.copyToStream( aOutStrm ); 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir void StorageBase::copyStorageToStorage( StorageBase& rDestStrg ) 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" ); 240*cdf0e10cSrcweir if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() ) 241*cdf0e10cSrcweir { 242*cdf0e10cSrcweir ::std::vector< OUString > aElements; 243*cdf0e10cSrcweir getElementNames( aElements ); 244*cdf0e10cSrcweir for( ::std::vector< OUString >::iterator aIt = aElements.begin(), aEnd = aElements.end(); aIt != aEnd; ++aIt ) 245*cdf0e10cSrcweir copyToStorage( rDestStrg, *aIt ); 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir void StorageBase::commit() 250*cdf0e10cSrcweir { 251*cdf0e10cSrcweir OSL_ENSURE( !mbReadOnly, "StorageBase::commit - cannot commit in read-only mode" ); 252*cdf0e10cSrcweir if( !mbReadOnly ) 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir // commit all open substorages 255*cdf0e10cSrcweir maSubStorages.forEachMem( &StorageBase::commit ); 256*cdf0e10cSrcweir // commit this storage 257*cdf0e10cSrcweir implCommit(); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir // private -------------------------------------------------------------------- 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir StorageRef StorageBase::getSubStorage( const OUString& rElementName, bool bCreateMissing ) 264*cdf0e10cSrcweir { 265*cdf0e10cSrcweir StorageRef& rxSubStrg = maSubStorages[ rElementName ]; 266*cdf0e10cSrcweir if( !rxSubStrg ) 267*cdf0e10cSrcweir rxSubStrg = implOpenSubStorage( rElementName, bCreateMissing ); 268*cdf0e10cSrcweir return rxSubStrg; 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir // ============================================================================ 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir } // namespace oox 274