xref: /trunk/main/oox/source/helper/storagebase.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/helper/storagebase.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
27cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
28cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
29cdf0e10cSrcweir #include "oox/helper/binaryinputstream.hxx"
30cdf0e10cSrcweir #include "oox/helper/binaryoutputstream.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace oox {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir // ============================================================================
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::com::sun::star::embed;
37cdf0e10cSrcweir using namespace ::com::sun::star::io;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using ::rtl::OUString;
41cdf0e10cSrcweir using ::rtl::OUStringBuffer;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // ============================================================================
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace {
46cdf0e10cSrcweir 
lclSplitFirstElement(OUString & orElement,OUString & orRemainder,const OUString & rFullName)47cdf0e10cSrcweir void lclSplitFirstElement( OUString& orElement, OUString& orRemainder, const OUString& rFullName )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     sal_Int32 nSlashPos = rFullName.indexOf( '/' );
50cdf0e10cSrcweir     if( (0 <= nSlashPos) && (nSlashPos < rFullName.getLength()) )
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         orElement = rFullName.copy( 0, nSlashPos );
53cdf0e10cSrcweir         orRemainder = rFullName.copy( nSlashPos + 1 );
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir     else
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         orElement = rFullName;
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir } // namespace
62cdf0e10cSrcweir 
63cdf0e10cSrcweir // ----------------------------------------------------------------------------
64cdf0e10cSrcweir 
StorageBase(const Reference<XInputStream> & rxInStream,bool bBaseStreamAccess)65cdf0e10cSrcweir StorageBase::StorageBase( const Reference< XInputStream >& rxInStream, bool bBaseStreamAccess ) :
66cdf0e10cSrcweir     mxInStream( rxInStream ),
67cdf0e10cSrcweir     mbBaseStreamAccess( bBaseStreamAccess ),
68cdf0e10cSrcweir     mbReadOnly( true )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     OSL_ENSURE( mxInStream.is(), "StorageBase::StorageBase - missing base input stream" );
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
StorageBase(const Reference<XStream> & rxOutStream,bool bBaseStreamAccess)73cdf0e10cSrcweir StorageBase::StorageBase( const Reference< XStream >& rxOutStream, bool bBaseStreamAccess ) :
74cdf0e10cSrcweir     mxOutStream( rxOutStream ),
75cdf0e10cSrcweir     mbBaseStreamAccess( bBaseStreamAccess ),
76cdf0e10cSrcweir     mbReadOnly( false )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     OSL_ENSURE( mxOutStream.is(), "StorageBase::StorageBase - missing base output stream" );
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
StorageBase(const StorageBase & rParentStorage,const OUString & rStorageName,bool bReadOnly)81cdf0e10cSrcweir StorageBase::StorageBase( const StorageBase& rParentStorage, const OUString& rStorageName, bool bReadOnly ) :
82cdf0e10cSrcweir     maParentPath( rParentStorage.getPath() ),
83cdf0e10cSrcweir     maStorageName( rStorageName ),
84cdf0e10cSrcweir     mbBaseStreamAccess( false ),
85cdf0e10cSrcweir     mbReadOnly( bReadOnly )
86cdf0e10cSrcweir {
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
~StorageBase()89cdf0e10cSrcweir StorageBase::~StorageBase()
90cdf0e10cSrcweir {
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
isStorage() const93cdf0e10cSrcweir bool StorageBase::isStorage() const
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     return implIsStorage();
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
isRootStorage() const98cdf0e10cSrcweir bool StorageBase::isRootStorage() const
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     return implIsStorage() && (maStorageName.getLength() == 0);
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
isReadOnly() const103cdf0e10cSrcweir bool StorageBase::isReadOnly() const
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     return mbReadOnly;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
getXStorage() const108cdf0e10cSrcweir Reference< XStorage > StorageBase::getXStorage() const
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     return implGetXStorage();
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
getName() const113cdf0e10cSrcweir const OUString& StorageBase::getName() const
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     return maStorageName;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
getPath() const118cdf0e10cSrcweir OUString StorageBase::getPath() const
119cdf0e10cSrcweir {
120cdf0e10cSrcweir     OUStringBuffer aBuffer( maParentPath );
121cdf0e10cSrcweir     if( aBuffer.getLength() > 0 )
122cdf0e10cSrcweir         aBuffer.append( sal_Unicode( '/' ) );
123cdf0e10cSrcweir     aBuffer.append( maStorageName );
124cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
getElementNames(::std::vector<OUString> & orElementNames) const127cdf0e10cSrcweir void StorageBase::getElementNames( ::std::vector< OUString >& orElementNames ) const
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     orElementNames.clear();
130cdf0e10cSrcweir     implGetElementNames( orElementNames );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
openSubStorage(const OUString & rStorageName,bool bCreateMissing)133cdf0e10cSrcweir StorageRef StorageBase::openSubStorage( const OUString& rStorageName, bool bCreateMissing )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     StorageRef xSubStorage;
136cdf0e10cSrcweir     OSL_ENSURE( !bCreateMissing || !mbReadOnly, "StorageBase::openSubStorage - cannot create substorage in read-only mode" );
137cdf0e10cSrcweir     if( !bCreateMissing || !mbReadOnly )
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         OUString aElement, aRemainder;
140cdf0e10cSrcweir         lclSplitFirstElement( aElement, aRemainder, rStorageName );
141cdf0e10cSrcweir         if( aElement.getLength() > 0 )
142cdf0e10cSrcweir             xSubStorage = getSubStorage( aElement, bCreateMissing );
143cdf0e10cSrcweir         if( xSubStorage.get() && (aRemainder.getLength() > 0) )
144cdf0e10cSrcweir             xSubStorage = xSubStorage->openSubStorage( aRemainder, bCreateMissing );
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir     return xSubStorage;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
openInputStream(const OUString & rStreamName)149cdf0e10cSrcweir Reference< XInputStream > StorageBase::openInputStream( const OUString& rStreamName )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     Reference< XInputStream > xInStream;
152cdf0e10cSrcweir     OUString aElement, aRemainder;
153cdf0e10cSrcweir     lclSplitFirstElement( aElement, aRemainder, rStreamName );
154cdf0e10cSrcweir     if( aElement.getLength() > 0 )
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         if( aRemainder.getLength() > 0 )
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             StorageRef xSubStorage = getSubStorage( aElement, false );
159cdf0e10cSrcweir             if( xSubStorage.get() )
160cdf0e10cSrcweir                 xInStream = xSubStorage->openInputStream( aRemainder );
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         else
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             xInStream = implOpenInputStream( aElement );
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     else if( mbBaseStreamAccess )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         xInStream = mxInStream;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir     return xInStream;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
openOutputStream(const OUString & rStreamName)174cdf0e10cSrcweir Reference< XOutputStream > StorageBase::openOutputStream( const OUString& rStreamName )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     Reference< XOutputStream > xOutStream;
177cdf0e10cSrcweir     OSL_ENSURE( !mbReadOnly, "StorageBase::openOutputStream - cannot create output stream in read-only mode" );
178cdf0e10cSrcweir     if( !mbReadOnly )
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         OUString aElement, aRemainder;
181cdf0e10cSrcweir         lclSplitFirstElement( aElement, aRemainder, rStreamName );
182cdf0e10cSrcweir         if( aElement.getLength() > 0 )
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             if( aRemainder.getLength() > 0 )
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 StorageRef xSubStorage = getSubStorage( aElement, true );
187cdf0e10cSrcweir                 if( xSubStorage.get() )
188cdf0e10cSrcweir                     xOutStream = xSubStorage->openOutputStream( aRemainder );
189cdf0e10cSrcweir             }
190cdf0e10cSrcweir             else
191cdf0e10cSrcweir             {
192cdf0e10cSrcweir                 xOutStream = implOpenOutputStream( aElement );
193cdf0e10cSrcweir             }
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir         else if( mbBaseStreamAccess )
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             xOutStream = mxOutStream->getOutputStream();
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir     return xOutStream;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
copyToStorage(StorageBase & rDestStrg,const OUString & rElementName)203cdf0e10cSrcweir void StorageBase::copyToStorage( StorageBase& rDestStrg, const OUString& rElementName )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" );
206cdf0e10cSrcweir     OSL_ENSURE( rElementName.getLength() > 0, "StorageBase::copyToStorage - invalid element name" );
207cdf0e10cSrcweir     if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() && (rElementName.getLength() > 0) )
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         StorageRef xSubStrg = openSubStorage( rElementName, false );
210cdf0e10cSrcweir         if( xSubStrg.get() )
211cdf0e10cSrcweir         {
212cdf0e10cSrcweir             StorageRef xDestSubStrg = rDestStrg.openSubStorage( rElementName, true );
213cdf0e10cSrcweir             if( xDestSubStrg.get() )
214cdf0e10cSrcweir                 xSubStrg->copyStorageToStorage( *xDestSubStrg );
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir         else
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             Reference< XInputStream > xInStrm = openInputStream( rElementName );
219cdf0e10cSrcweir             if( xInStrm.is() )
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir                 Reference< XOutputStream > xOutStrm = rDestStrg.openOutputStream( rElementName );
222cdf0e10cSrcweir                 if( xOutStrm.is() )
223cdf0e10cSrcweir                 {
224cdf0e10cSrcweir                     BinaryXInputStream aInStrm( xInStrm, true );
225cdf0e10cSrcweir                     BinaryXOutputStream aOutStrm( xOutStrm, true );
226cdf0e10cSrcweir                     aInStrm.copyToStream( aOutStrm );
227cdf0e10cSrcweir                 }
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
copyStorageToStorage(StorageBase & rDestStrg)233cdf0e10cSrcweir void StorageBase::copyStorageToStorage( StorageBase& rDestStrg )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" );
236cdf0e10cSrcweir     if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() )
237cdf0e10cSrcweir     {
238cdf0e10cSrcweir         ::std::vector< OUString > aElements;
239cdf0e10cSrcweir         getElementNames( aElements );
240cdf0e10cSrcweir         for( ::std::vector< OUString >::iterator aIt = aElements.begin(), aEnd = aElements.end(); aIt != aEnd; ++aIt )
241cdf0e10cSrcweir             copyToStorage( rDestStrg, *aIt );
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir }
244cdf0e10cSrcweir 
commit()245cdf0e10cSrcweir void StorageBase::commit()
246cdf0e10cSrcweir {
247cdf0e10cSrcweir     OSL_ENSURE( !mbReadOnly, "StorageBase::commit - cannot commit in read-only mode" );
248cdf0e10cSrcweir     if( !mbReadOnly )
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir         // commit all open substorages
251cdf0e10cSrcweir         maSubStorages.forEachMem( &StorageBase::commit );
252cdf0e10cSrcweir         // commit this storage
253cdf0e10cSrcweir         implCommit();
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir // private --------------------------------------------------------------------
258cdf0e10cSrcweir 
getSubStorage(const OUString & rElementName,bool bCreateMissing)259cdf0e10cSrcweir StorageRef StorageBase::getSubStorage( const OUString& rElementName, bool bCreateMissing )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir     StorageRef& rxSubStrg = maSubStorages[ rElementName ];
262cdf0e10cSrcweir     if( !rxSubStrg )
263cdf0e10cSrcweir         rxSubStrg = implOpenSubStorage( rElementName, bCreateMissing );
264cdf0e10cSrcweir     return rxSubStrg;
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir // ============================================================================
268cdf0e10cSrcweir 
269cdf0e10cSrcweir } // namespace oox
270