Branch data 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 : : using ::rtl::OUString;
37 : : using ::rtl::OUStringBuffer;
38 : :
39 : : // ============================================================================
40 : :
41 : : namespace {
42 : :
43 : 3647 : void lclSplitFirstElement( OUString& orElement, OUString& orRemainder, OUString aFullName )
44 : : {
45 : 3647 : sal_Int32 nSlashPos = aFullName.indexOf( '/' );
46 : :
47 : : // strip leading slashes
48 [ - + ]: 3647 : while( nSlashPos == 0 )
49 : : {
50 : 0 : aFullName = aFullName.copy(1);
51 : 0 : nSlashPos = aFullName.indexOf( '/' );
52 : : }
53 : :
54 [ + + ][ + - ]: 3647 : if( (0 <= nSlashPos) && (nSlashPos < aFullName.getLength()) )
[ + + ]
55 : : {
56 : 1587 : orElement = aFullName.copy( 0, nSlashPos );
57 : 1587 : orRemainder = aFullName.copy( nSlashPos + 1 );
58 : : }
59 : : else
60 : : {
61 : 2060 : orElement = aFullName;
62 : : }
63 : 3647 : }
64 : :
65 : : } // namespace
66 : :
67 : : // ----------------------------------------------------------------------------
68 : :
69 : 1121 : StorageBase::StorageBase( const Reference< XInputStream >& rxInStream, bool bBaseStreamAccess ) :
70 : : mxInStream( rxInStream ),
71 : : mbBaseStreamAccess( bBaseStreamAccess ),
72 : 1121 : mbReadOnly( true )
73 : : {
74 : : OSL_ENSURE( mxInStream.is(), "StorageBase::StorageBase - missing base input stream" );
75 : 1121 : }
76 : :
77 : 70 : StorageBase::StorageBase( const Reference< XStream >& rxOutStream, bool bBaseStreamAccess ) :
78 : : mxOutStream( rxOutStream ),
79 : : mbBaseStreamAccess( bBaseStreamAccess ),
80 : 70 : mbReadOnly( false )
81 : : {
82 : : OSL_ENSURE( mxOutStream.is(), "StorageBase::StorageBase - missing base output stream" );
83 : 70 : }
84 : :
85 : 665 : StorageBase::StorageBase( const StorageBase& rParentStorage, const OUString& rStorageName, bool bReadOnly ) :
86 : : maParentPath( rParentStorage.getPath() ),
87 : : maStorageName( rStorageName ),
88 : : mbBaseStreamAccess( false ),
89 [ + - ]: 665 : mbReadOnly( bReadOnly )
90 : : {
91 : 665 : }
92 : :
93 : 1856 : StorageBase::~StorageBase()
94 : : {
95 [ - + ]: 1856 : }
96 : :
97 : 1177 : bool StorageBase::isStorage() const
98 : : {
99 : 1177 : return implIsStorage();
100 : : }
101 : :
102 : 0 : bool StorageBase::isRootStorage() const
103 : : {
104 [ # # ][ # # ]: 0 : return implIsStorage() && maStorageName.isEmpty();
105 : : }
106 : :
107 : 1148 : bool StorageBase::isReadOnly() const
108 : : {
109 : 1148 : return mbReadOnly;
110 : : }
111 : :
112 : 171 : Reference< XStorage > StorageBase::getXStorage() const
113 : : {
114 : 171 : return implGetXStorage();
115 : : }
116 : :
117 : 26 : const OUString& StorageBase::getName() const
118 : : {
119 : 26 : return maStorageName;
120 : : }
121 : :
122 : 665 : OUString StorageBase::getPath() const
123 : : {
124 [ + - ]: 665 : OUStringBuffer aBuffer( maParentPath );
125 [ + + ]: 665 : if( aBuffer.getLength() > 0 )
126 [ + - ]: 42 : aBuffer.append( sal_Unicode( '/' ) );
127 [ + - ]: 665 : aBuffer.append( maStorageName );
128 [ + - ]: 665 : return aBuffer.makeStringAndClear();
129 : : }
130 : :
131 : 39 : void StorageBase::getElementNames( ::std::vector< OUString >& orElementNames ) const
132 : : {
133 : 39 : orElementNames.clear();
134 : 39 : implGetElementNames( orElementNames );
135 : 39 : }
136 : :
137 : 389 : StorageRef StorageBase::openSubStorage( const OUString& rStorageName, bool bCreateMissing )
138 : : {
139 : 389 : StorageRef xSubStorage;
140 : : OSL_ENSURE( !bCreateMissing || !mbReadOnly, "StorageBase::openSubStorage - cannot create substorage in read-only mode" );
141 [ + - ][ + + ]: 389 : if( !bCreateMissing || !mbReadOnly )
142 : : {
143 : 389 : OUString aElement, aRemainder;
144 : 389 : lclSplitFirstElement( aElement, aRemainder, rStorageName );
145 [ + - ]: 389 : if( !aElement.isEmpty() )
146 [ + - ][ + - ]: 389 : xSubStorage = getSubStorage( aElement, bCreateMissing );
[ + - ]
147 [ + + ][ - + ]: 389 : if( xSubStorage.get() && !aRemainder.isEmpty() )
[ - + ]
148 [ # # ][ # # ]: 389 : xSubStorage = xSubStorage->openSubStorage( aRemainder, bCreateMissing );
[ # # ]
149 : : }
150 : 389 : return xSubStorage;
151 : : }
152 : :
153 : 2359 : Reference< XInputStream > StorageBase::openInputStream( const OUString& rStreamName )
154 : : {
155 : 2359 : Reference< XInputStream > xInStream;
156 : 2359 : OUString aElement, aRemainder;
157 : 2359 : lclSplitFirstElement( aElement, aRemainder, rStreamName );
158 [ + - ]: 2359 : if( !aElement.isEmpty() )
159 : : {
160 [ + + ]: 2359 : if( !aRemainder.isEmpty() )
161 : : {
162 [ + - ]: 1242 : StorageRef xSubStorage = getSubStorage( aElement, false );
163 [ + + ]: 1242 : if( xSubStorage.get() )
164 [ + - ][ + - ]: 1242 : xInStream = xSubStorage->openInputStream( aRemainder );
[ + - ]
165 : : }
166 : : else
167 : : {
168 [ + - ][ + - ]: 1117 : xInStream = implOpenInputStream( aElement );
169 : : }
170 : : }
171 [ # # ]: 0 : else if( mbBaseStreamAccess )
172 : : {
173 [ # # ]: 0 : xInStream = mxInStream;
174 : : }
175 : 2359 : return xInStream;
176 : : }
177 : :
178 : 899 : Reference< XOutputStream > StorageBase::openOutputStream( const OUString& rStreamName )
179 : : {
180 : 899 : Reference< XOutputStream > xOutStream;
181 : : OSL_ENSURE( !mbReadOnly, "StorageBase::openOutputStream - cannot create output stream in read-only mode" );
182 [ + - ]: 899 : if( !mbReadOnly )
183 : : {
184 : 899 : OUString aElement, aRemainder;
185 : 899 : lclSplitFirstElement( aElement, aRemainder, rStreamName );
186 [ + - ]: 899 : if( !aElement.isEmpty() )
187 : : {
188 [ + + ]: 899 : if( !aRemainder.isEmpty() )
189 : : {
190 [ + - ]: 345 : StorageRef xSubStorage = getSubStorage( aElement, true );
191 [ + - ]: 345 : if( xSubStorage.get() )
192 [ + - ][ + - ]: 345 : xOutStream = xSubStorage->openOutputStream( aRemainder );
[ + - ]
193 : : }
194 : : else
195 : : {
196 [ + - ][ + - ]: 554 : xOutStream = implOpenOutputStream( aElement );
197 : : }
198 : : }
199 [ # # ]: 0 : else if( mbBaseStreamAccess )
200 : : {
201 [ # # ][ # # ]: 0 : xOutStream = mxOutStream->getOutputStream();
[ # # ]
202 : 899 : }
203 : : }
204 : 899 : return xOutStream;
205 : : }
206 : :
207 : 222 : void StorageBase::copyToStorage( StorageBase& rDestStrg, const OUString& rElementName )
208 : : {
209 : : OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" );
210 : : OSL_ENSURE( !rElementName.isEmpty(), "StorageBase::copyToStorage - invalid element name" );
211 [ + - ][ + - ]: 222 : if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() && !rElementName.isEmpty() )
[ + - ][ + - ]
212 : : {
213 [ + - ]: 222 : StorageRef xSubStrg = openSubStorage( rElementName, false );
214 [ + + ]: 222 : if( xSubStrg.get() )
215 : : {
216 [ + - ]: 13 : StorageRef xDestSubStrg = rDestStrg.openSubStorage( rElementName, true );
217 [ + - ]: 13 : if( xDestSubStrg.get() )
218 [ + - ][ + - ]: 13 : xSubStrg->copyStorageToStorage( *xDestSubStrg );
219 : : }
220 : : else
221 : : {
222 [ + - ]: 209 : Reference< XInputStream > xInStrm = openInputStream( rElementName );
223 [ + - ]: 209 : if( xInStrm.is() )
224 : : {
225 [ + - ]: 209 : Reference< XOutputStream > xOutStrm = rDestStrg.openOutputStream( rElementName );
226 [ + - ]: 209 : if( xOutStrm.is() )
227 : : {
228 [ + - ]: 209 : BinaryXInputStream aInStrm( xInStrm, true );
229 [ + - ]: 209 : BinaryXOutputStream aOutStrm( xOutStrm, true );
230 [ + - ][ + - ]: 209 : aInStrm.copyToStream( aOutStrm );
[ + - ]
231 : 209 : }
232 : 209 : }
233 [ + - ]: 222 : }
234 : : }
235 : 222 : }
236 : :
237 : 26 : void StorageBase::copyStorageToStorage( StorageBase& rDestStrg )
238 : : {
239 : : OSL_ENSURE( rDestStrg.isStorage() && !rDestStrg.isReadOnly(), "StorageBase::copyToStorage - invalid destination" );
240 [ + - ][ + - ]: 26 : if( rDestStrg.isStorage() && !rDestStrg.isReadOnly() )
[ + - ]
241 : : {
242 [ + - ]: 26 : ::std::vector< OUString > aElements;
243 [ + - ]: 26 : getElementNames( aElements );
244 [ + - ][ + + ]: 248 : for( ::std::vector< OUString >::iterator aIt = aElements.begin(), aEnd = aElements.end(); aIt != aEnd; ++aIt )
245 [ + - ]: 248 : copyToStorage( rDestStrg, *aIt );
246 : : }
247 : 26 : }
248 : :
249 : 197 : void StorageBase::commit()
250 : : {
251 : : OSL_ENSURE( !mbReadOnly, "StorageBase::commit - cannot commit in read-only mode" );
252 [ + - ]: 197 : if( !mbReadOnly )
253 : : {
254 : : // commit all open substorages
255 : 197 : maSubStorages.forEachMem( &StorageBase::commit );
256 : : // commit this storage
257 : 197 : implCommit();
258 : : }
259 : 197 : }
260 : :
261 : : // private --------------------------------------------------------------------
262 : :
263 : 1976 : StorageRef StorageBase::getSubStorage( const OUString& rElementName, bool bCreateMissing )
264 : : {
265 : 1976 : StorageRef& rxSubStrg = maSubStorages[ rElementName ];
266 [ + + ]: 1976 : if( !rxSubStrg )
267 [ + - ]: 1090 : rxSubStrg = implOpenSubStorage( rElementName, bCreateMissing );
268 : 1976 : return rxSubStrg;
269 : : }
270 : :
271 : : // ============================================================================
272 : :
273 [ + - ][ + - ]: 285 : } // namespace oox
274 : :
275 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|