1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: storagebase.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/helper/storagebase.hxx"
32 #include <com/sun/star/io/XStream.hpp>
33 #include <com/sun/star/embed/XTransactedObject.hpp>
34 #include <rtl/ustrbuf.hxx>
36 using ::rtl::OUString
;
37 using ::rtl::OUStringBuffer
;
38 using ::com::sun::star::uno::Reference
;
39 using ::com::sun::star::uno::UNO_QUERY
;
40 using ::com::sun::star::embed::XStorage
;
41 using ::com::sun::star::embed::XTransactedObject
;
42 using ::com::sun::star::io::XInputStream
;
43 using ::com::sun::star::io::XOutputStream
;
44 using ::com::sun::star::io::XStream
;
48 // ============================================================================
52 void lclSplitFirstElement( OUString
& orElement
, OUString
& orRemainder
, const OUString
& rFullName
)
54 sal_Int32 nSlashPos
= rFullName
.indexOf( '/' );
55 if( (0 <= nSlashPos
) && (nSlashPos
< rFullName
.getLength()) )
57 orElement
= rFullName
.copy( 0, nSlashPos
);
58 orRemainder
= rFullName
.copy( nSlashPos
+ 1 );
62 orElement
= rFullName
;
68 // ----------------------------------------------------------------------------
70 StorageBase::StorageBase( const Reference
< XInputStream
>& rxInStream
, bool bBaseStreamAccess
) :
71 mxInStream( rxInStream
),
73 mbBaseStreamAccess( bBaseStreamAccess
)
75 OSL_ENSURE( mxInStream
.is(), "StorageBase::StorageBase - missing base input stream" );
78 StorageBase::StorageBase( const Reference
< XStream
>& rxOutStream
, bool bBaseStreamAccess
) :
79 mxOutStream( rxOutStream
),
81 mbBaseStreamAccess( bBaseStreamAccess
)
83 OSL_ENSURE( mxOutStream
.is(), "StorageBase::StorageBase - missing base output stream" );
86 StorageBase::StorageBase( const StorageBase
& rParentStorage
, const OUString
& rStorageName
) :
87 maStorageName( rStorageName
),
88 mpParentStorage( &rParentStorage
),
89 mbBaseStreamAccess( false )
93 StorageBase::~StorageBase()
97 bool StorageBase::isStorage() const
99 return implIsStorage();
102 Reference
< XStorage
> StorageBase::getXStorage() const
104 return implGetXStorage();
107 const OUString
& StorageBase::getName() const
109 return maStorageName
;
112 OUString
StorageBase::getPath() const
114 OUStringBuffer aBuffer
;
115 if( mpParentStorage
)
116 aBuffer
.append( mpParentStorage
->getPath() );
117 if( aBuffer
.getLength() > 0 )
118 aBuffer
.append( sal_Unicode( '/' ) );
119 aBuffer
.append( maStorageName
);
120 return aBuffer
.makeStringAndClear();
123 void StorageBase::getElementNames( ::std::vector
< OUString
>& orElementNames
) const
125 orElementNames
.clear();
126 implGetElementNames( orElementNames
);
129 StorageRef
StorageBase::openSubStorage( const OUString
& rStorageName
, bool bCreate
)
131 StorageRef xSubStorage
;
132 OUString aElement
, aRemainder
;
133 lclSplitFirstElement( aElement
, aRemainder
, rStorageName
);
134 if( aElement
.getLength() > 0 )
135 xSubStorage
= getSubStorage( aElement
, bCreate
);
136 if( xSubStorage
.get() && (aRemainder
.getLength() > 0) )
137 xSubStorage
= xSubStorage
->openSubStorage( aRemainder
, bCreate
);
141 Reference
< XInputStream
> StorageBase::openInputStream( const OUString
& rStreamName
)
143 Reference
< XInputStream
> xInStream
;
144 OUString aElement
, aRemainder
;
145 lclSplitFirstElement( aElement
, aRemainder
, rStreamName
);
146 if( aElement
.getLength() > 0 )
148 if( aRemainder
.getLength() > 0 )
150 StorageRef xSubStorage
= getSubStorage( aElement
, false );
151 if( xSubStorage
.get() )
152 xInStream
= xSubStorage
->openInputStream( aRemainder
);
156 xInStream
= implOpenInputStream( aElement
);
159 else if( mbBaseStreamAccess
)
161 xInStream
= mxInStream
;
166 Reference
< XOutputStream
> StorageBase::openOutputStream( const OUString
& rStreamName
)
168 Reference
< XOutputStream
> xOutStream
;
169 OUString aElement
, aRemainder
;
170 lclSplitFirstElement( aElement
, aRemainder
, rStreamName
);
171 if( aElement
.getLength() > 0 )
173 if( aRemainder
.getLength() > 0 )
175 StorageRef xSubStorage
= getSubStorage( aElement
, true );
176 if( xSubStorage
.get() )
177 xOutStream
= xSubStorage
->openOutputStream( aRemainder
);
181 xOutStream
= implOpenOutputStream( aElement
);
184 else if( mbBaseStreamAccess
)
186 xOutStream
= mxOutStream
->getOutputStream();
191 StorageRef
StorageBase::getSubStorage( const OUString
& rElementName
, bool bCreate
)
193 SubStorageMap::iterator aIt
= maSubStorages
.find( rElementName
);
194 return (aIt
== maSubStorages
.end()) ?
195 (maSubStorages
[ rElementName
] = implOpenSubStorage( rElementName
, bCreate
)) : aIt
->second
;
198 void StorageBase::commit()
200 for( SubStorageMap::iterator aIt
= maSubStorages
.begin(); aIt
!= maSubStorages
.end(); aIt
++ )
201 aIt
->second
->commit();
203 Reference
< XTransactedObject
> xTransactedObj( getXStorage(), UNO_QUERY
);
205 if( xTransactedObj
.is() )
206 xTransactedObj
->commit();
209 // ============================================================================