Update ooo320-m1
[ooovba.git] / oox / source / helper / olestorage.cxx
blob73ce24b0430b98e816d7c13791dd646555dce336
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: olestorage.cxx,v $
10 * $Revision: 1.4 $
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/olestorage.hxx"
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <com/sun/star/container/XNameContainer.hpp>
35 #include <com/sun/star/io/XInputStream.hpp>
36 #include <com/sun/star/io/XOutputStream.hpp>
37 #include <com/sun/star/io/XStream.hpp>
38 #include "oox/helper/helper.hxx"
40 using ::rtl::OUString;
41 using ::com::sun::star::uno::Any;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::uno::Exception;
45 using ::com::sun::star::uno::UNO_QUERY;
46 using ::com::sun::star::uno::UNO_QUERY_THROW;
47 using ::com::sun::star::container::XNameAccess;
48 using ::com::sun::star::lang::XMultiServiceFactory;
49 using ::com::sun::star::beans::PropertyValue;
50 using ::com::sun::star::embed::XStorage;
51 using ::com::sun::star::io::XInputStream;
52 using ::com::sun::star::io::XOutputStream;
53 using ::com::sun::star::io::XStream;
55 namespace oox {
57 // ============================================================================
59 OleStorage::OleStorage(
60 const Reference< XMultiServiceFactory >& rxFactory,
61 const Reference< XInputStream >& rxInStream,
62 bool bBaseStreamAccess ) :
63 StorageBase( rxInStream, bBaseStreamAccess )
65 OSL_ENSURE( rxFactory.is(), "OleStorage::OleStorage - missing service factory" );
66 // create base storage object
67 Sequence< Any > aArgs( 2 );
68 aArgs[ 0 ] <<= rxInStream;
69 aArgs[ 1 ] <<= true; // true = do not create a copy of the input stream
70 mxStorage.set( rxFactory->createInstanceWithArguments(
71 CREATE_OUSTRING( "com.sun.star.embed.OLESimpleStorage" ), aArgs ), UNO_QUERY );
72 mxElements.set( mxStorage, UNO_QUERY );
75 OleStorage::OleStorage(
76 const Reference< XMultiServiceFactory >& rxFactory,
77 const Reference< XStream >& rxStream,
78 bool bBaseStreamAccess ) :
79 StorageBase( rxStream, bBaseStreamAccess )
81 OSL_ENSURE( rxFactory.is(), "OleStorage::OleStorage - missing service factory" );
82 (void)rxFactory; // prevent compiler warning
83 OSL_ENSURE( false, "OleStorage::OleStorage - not implemented" );
84 mxElements.set( mxStorage, UNO_QUERY );
87 OleStorage::OleStorage( const OleStorage& rParentStorage, const Reference< XNameAccess >& rxElementsAccess, const OUString& rElementName ) :
88 StorageBase( rParentStorage, rElementName ),
89 mxStorage( rParentStorage.mxStorage ),
90 mxElements( rxElementsAccess )
92 OSL_ENSURE( mxElements.is(), "OleStorage::OleStorage - missing elements access" );
95 OleStorage::~OleStorage()
99 // StorageBase interface ------------------------------------------------------
101 bool OleStorage::implIsStorage() const
103 if( mxStorage.is() && mxElements.is() ) try
105 /* If this is not a storage, hasElements() throws an exception. But we
106 do not return the result of hasElements(), because an empty storage
107 is a valid storage too. */
108 mxElements->hasElements();
109 return true;
111 catch( Exception& )
114 return false;
117 Reference< XStorage > OleStorage::implGetXStorage() const
119 OSL_ENSURE( false, "OleStorage::getXStorage - not implemented" );
120 return Reference< XStorage >();
123 void OleStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
125 Sequence< OUString > aNames;
126 if( mxElements.is() ) try
128 aNames = mxElements->getElementNames();
129 if( aNames.getLength() > 0 )
130 orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
132 catch( Exception& )
137 StorageRef OleStorage::implOpenSubStorage( const OUString& rElementName, bool bCreate )
139 OSL_ENSURE( !bCreate, "OleStorage::implOpenSubStorage - creating substorages not implemented" );
140 (void)bCreate; // prevent compiler warning
141 StorageRef xSubStorage;
142 if( mxElements.is() ) try
144 Reference< XNameAccess > xSubElements( mxElements->getByName( rElementName ), UNO_QUERY_THROW );
145 xSubStorage.reset( new OleStorage( *this, xSubElements, rElementName ) );
147 catch( Exception& )
150 return xSubStorage;
153 Reference< XInputStream > OleStorage::implOpenInputStream( const OUString& rElementName )
155 Reference< XInputStream > xInStream;
156 if( mxElements.is() ) try
158 xInStream.set( mxElements->getByName( rElementName ), UNO_QUERY );
160 catch( Exception& )
163 return xInStream;
166 Reference< XOutputStream > OleStorage::implOpenOutputStream( const OUString& rElementName )
168 Reference< XOutputStream > xOutStream;
169 if( mxElements.is() && (rElementName.getLength() > 0) ) try
171 (void)rElementName; // prevent compiler warning
172 OSL_ENSURE( false, "OleStorage::implOpenOutputStream - not implemented" );
174 catch( Exception& )
177 return xOutStream;
180 // ============================================================================
182 } // namespace oox