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