merge the formfield patch from ooo-build
[ooovba.git] / oox / source / helper / zipstorage.cxx
blob5dd3aa67109bf861717a3d3def2ba672f874fbab
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: zipstorage.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/zipstorage.hxx"
32 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/embed/XStorage.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/io/XInputStream.hpp>
37 #include <com/sun/star/io/XOutputStream.hpp>
38 #include <comphelper/storagehelper.hxx>
39 #include "oox/helper/helper.hxx"
41 using ::rtl::OUString;
42 using ::com::sun::star::uno::Any;
43 using ::com::sun::star::uno::Reference;
44 using ::com::sun::star::uno::Sequence;
45 using ::com::sun::star::uno::Exception;
46 using ::com::sun::star::uno::UNO_QUERY;
47 using ::com::sun::star::lang::XMultiServiceFactory;
48 using ::com::sun::star::embed::XStorage;
49 using ::com::sun::star::io::XInputStream;
50 using ::com::sun::star::io::XOutputStream;
51 using ::com::sun::star::io::XStream;
53 namespace oox {
55 // ============================================================================
57 ZipStorage::ZipStorage(
58 const Reference< XMultiServiceFactory >& rxFactory,
59 const Reference< XInputStream >& rxInStream ) :
60 StorageBase( rxInStream, false )
62 OSL_ENSURE( rxFactory.is(), "ZipStorage::ZipStorage - missing service factory" );
63 // create base storage object
64 try
66 /* #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
67 cannot be used here as it will open a storage with format type
68 'PackageFormat' that will not work with OOXML packages.
69 TODO: #i105410# switch to 'OFOPXMLFormat' and use its
70 implementation of relations handling. */
71 mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
72 ZIP_STORAGE_FORMAT_STRING, rxInStream, rxFactory );
74 catch( Exception& )
79 ZipStorage::ZipStorage(
80 const Reference< XMultiServiceFactory >& rxFactory,
81 const Reference< XStream >& rxStream ) :
82 StorageBase( rxStream, false )
84 OSL_ENSURE( rxFactory.is(), "ZipStorage::ZipStorage - missing service factory" );
85 // create base storage object
86 try
88 using namespace ::com::sun::star::embed::ElementModes;
89 mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
90 OFOPXML_STORAGE_FORMAT_STRING, rxStream, READWRITE | TRUNCATE, rxFactory );
92 catch( Exception& )
94 OSL_ENSURE( false, "ZipStorage::ZipStorage - cannot open output storage" );
98 ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
99 StorageBase( rParentStorage, rElementName ),
100 mxStorage( rxStorage )
102 OSL_ENSURE( mxStorage.is(), "ZipStorage::ZipStorage - missing storage" );
105 ZipStorage::~ZipStorage()
109 bool ZipStorage::implIsStorage() const
111 return mxStorage.is();
114 Reference< XStorage > ZipStorage::implGetXStorage() const
116 return mxStorage;
119 void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
121 Sequence< OUString > aNames;
122 if( mxStorage.is() ) try
124 aNames = mxStorage->getElementNames();
125 if( aNames.getLength() > 0 )
126 orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
128 catch( Exception& )
133 StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreate )
135 Reference< XStorage > xSubXStorage;
136 bool bMissing = false;
137 if( mxStorage.is() ) try
139 // XStorage::isStorageElement may throw various exceptions...
140 if( mxStorage->isStorageElement( rElementName ) )
141 xSubXStorage = mxStorage->openStorageElement(
142 rElementName, ::com::sun::star::embed::ElementModes::READ );
144 catch( ::com::sun::star::container::NoSuchElementException& )
146 bMissing = true;
148 catch( Exception& )
152 if( bMissing && bCreate )
155 xSubXStorage = mxStorage->openStorageElement(
156 rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
158 catch( Exception& )
162 StorageRef xSubStorage;
163 if( xSubXStorage.is() )
164 xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
165 return xSubStorage;
168 Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
170 Reference< XInputStream > xInStream;
171 if( mxStorage.is() ) try
173 xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
175 catch( Exception& )
178 return xInStream;
181 Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
183 Reference< XOutputStream > xOutStream;
184 if( mxStorage.is() ) try
186 xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
188 catch( Exception& )
191 return xOutStream;
194 // ============================================================================
196 } // namespace oox