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