1ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file
5ca5ec200SAndrew Rist * distributed with this work for additional information
6ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the
8ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance
9ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at
10ca5ec200SAndrew Rist *
11ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12ca5ec200SAndrew Rist *
13ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing,
14ca5ec200SAndrew Rist * software distributed under the License is distributed on an
15ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the
17ca5ec200SAndrew Rist * specific language governing permissions and limitations
18ca5ec200SAndrew Rist * under the License.
19ca5ec200SAndrew Rist *
20ca5ec200SAndrew Rist *************************************************************/
21ca5ec200SAndrew Rist
22ca5ec200SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "oox/helper/zipstorage.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
27cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
28cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
29cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
30cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
31cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
33cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
34cdf0e10cSrcweir #include "oox/helper/helper.hxx"
35cdf0e10cSrcweir
36cdf0e10cSrcweir namespace oox {
37cdf0e10cSrcweir
38cdf0e10cSrcweir // ============================================================================
39cdf0e10cSrcweir
40cdf0e10cSrcweir using namespace ::com::sun::star::container;
41cdf0e10cSrcweir using namespace ::com::sun::star::embed;
42cdf0e10cSrcweir using namespace ::com::sun::star::io;
43cdf0e10cSrcweir using namespace ::com::sun::star::lang;
44cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45cdf0e10cSrcweir
46cdf0e10cSrcweir using ::rtl::OUString;
47cdf0e10cSrcweir
48cdf0e10cSrcweir // ============================================================================
49cdf0e10cSrcweir
ZipStorage(const Reference<XComponentContext> & rxContext,const Reference<XInputStream> & rxInStream)50cdf0e10cSrcweir ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
51cdf0e10cSrcweir StorageBase( rxInStream, false )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
54cdf0e10cSrcweir // create base storage object
55cdf0e10cSrcweir if( rxContext.is() ) try
56cdf0e10cSrcweir {
57cdf0e10cSrcweir /* #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
58cdf0e10cSrcweir cannot be used here as it will open a storage with format type
59cdf0e10cSrcweir 'PackageFormat' that will not work with OOXML packages.
60cdf0e10cSrcweir
61cdf0e10cSrcweir #161971# The MS-document storages should always be opened in repair
62cdf0e10cSrcweir mode to ignore the format errors and get so much info as possible.
63cdf0e10cSrcweir I hate this solution, but it seems to be the only consistent way to
64cdf0e10cSrcweir handle the MS documents.
65cdf0e10cSrcweir
66cdf0e10cSrcweir TODO: #i105410# switch to 'OFOPXMLFormat' and use its
67cdf0e10cSrcweir implementation of relations handling.
68cdf0e10cSrcweir */
69cdf0e10cSrcweir Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
70cdf0e10cSrcweir mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
71cdf0e10cSrcweir ZIP_STORAGE_FORMAT_STRING, rxInStream, xFactory,
72cdf0e10cSrcweir sal_False ); // DEV300_m80: Was sal_True, but DOCX and others did not load
73cdf0e10cSrcweir }
74cdf0e10cSrcweir catch( Exception& )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir }
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
ZipStorage(const Reference<XComponentContext> & rxContext,const Reference<XStream> & rxStream)79cdf0e10cSrcweir ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
80cdf0e10cSrcweir StorageBase( rxStream, false )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
83cdf0e10cSrcweir // create base storage object
84cdf0e10cSrcweir if( rxContext.is() ) try
85cdf0e10cSrcweir {
86cdf0e10cSrcweir Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
87cdf0e10cSrcweir const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
88cdf0e10cSrcweir mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
89cdf0e10cSrcweir OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, xFactory, sal_True );
90cdf0e10cSrcweir }
91cdf0e10cSrcweir catch( Exception& )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir OSL_ENSURE( false, "ZipStorage::ZipStorage - cannot open output storage" );
94cdf0e10cSrcweir }
95cdf0e10cSrcweir }
96cdf0e10cSrcweir
ZipStorage(const ZipStorage & rParentStorage,const Reference<XStorage> & rxStorage,const OUString & rElementName)97cdf0e10cSrcweir ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
98cdf0e10cSrcweir StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
99cdf0e10cSrcweir mxStorage( rxStorage )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir OSL_ENSURE( mxStorage.is(), "ZipStorage::ZipStorage - missing storage" );
102cdf0e10cSrcweir }
103cdf0e10cSrcweir
~ZipStorage()104cdf0e10cSrcweir ZipStorage::~ZipStorage()
105cdf0e10cSrcweir {
106cdf0e10cSrcweir }
107cdf0e10cSrcweir
implIsStorage() const108cdf0e10cSrcweir bool ZipStorage::implIsStorage() const
109cdf0e10cSrcweir {
110cdf0e10cSrcweir return mxStorage.is();
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
implGetXStorage() const113cdf0e10cSrcweir Reference< XStorage > ZipStorage::implGetXStorage() const
114cdf0e10cSrcweir {
115cdf0e10cSrcweir return mxStorage;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
implGetElementNames(::std::vector<OUString> & orElementNames) const118cdf0e10cSrcweir void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
119cdf0e10cSrcweir {
120cdf0e10cSrcweir Sequence< OUString > aNames;
121cdf0e10cSrcweir if( mxStorage.is() ) try
122cdf0e10cSrcweir {
123cdf0e10cSrcweir aNames = mxStorage->getElementNames();
124cdf0e10cSrcweir if( aNames.getLength() > 0 )
125cdf0e10cSrcweir orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
126cdf0e10cSrcweir }
127cdf0e10cSrcweir catch( Exception& )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir }
130cdf0e10cSrcweir }
131cdf0e10cSrcweir
implOpenSubStorage(const OUString & rElementName,bool bCreateMissing)132cdf0e10cSrcweir StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir Reference< XStorage > xSubXStorage;
135cdf0e10cSrcweir bool bMissing = false;
136cdf0e10cSrcweir if( mxStorage.is() ) try
137cdf0e10cSrcweir {
138cdf0e10cSrcweir // XStorage::isStorageElement may throw various exceptions...
139cdf0e10cSrcweir if( mxStorage->isStorageElement( rElementName ) )
140cdf0e10cSrcweir xSubXStorage = mxStorage->openStorageElement(
141cdf0e10cSrcweir rElementName, ::com::sun::star::embed::ElementModes::READ );
142cdf0e10cSrcweir }
143cdf0e10cSrcweir catch( NoSuchElementException& )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir bMissing = true;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir catch( Exception& )
148cdf0e10cSrcweir {
149cdf0e10cSrcweir }
150cdf0e10cSrcweir
151cdf0e10cSrcweir if( bMissing && bCreateMissing ) try
152cdf0e10cSrcweir {
153cdf0e10cSrcweir xSubXStorage = mxStorage->openStorageElement(
154cdf0e10cSrcweir rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
155cdf0e10cSrcweir }
156cdf0e10cSrcweir catch( Exception& )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir }
159cdf0e10cSrcweir
160cdf0e10cSrcweir StorageRef xSubStorage;
161cdf0e10cSrcweir if( xSubXStorage.is() )
162cdf0e10cSrcweir xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
163cdf0e10cSrcweir return xSubStorage;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
implOpenInputStream(const OUString & rElementName)166cdf0e10cSrcweir Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir Reference< XInputStream > xInStream;
169cdf0e10cSrcweir if( mxStorage.is() ) try
170cdf0e10cSrcweir {
171cdf0e10cSrcweir xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
172cdf0e10cSrcweir }
173cdf0e10cSrcweir catch( Exception& )
174cdf0e10cSrcweir {
175*25c6f4b7SDamjan Jovanovic // Bug 126720 - sometimes the relationship says the file is "sharedStrings.xml" but the file is actually "SharedStrings.xml".
176*25c6f4b7SDamjan Jovanovic // Do a case-insensitive search:
177*25c6f4b7SDamjan Jovanovic ::com::sun::star::uno::Sequence< ::rtl::OUString > aNames = mxStorage->getElementNames( );
178*25c6f4b7SDamjan Jovanovic for ( sal_Int32 i = 0; i < aNames.getLength(); i++ )
179*25c6f4b7SDamjan Jovanovic {
180*25c6f4b7SDamjan Jovanovic if ( aNames[i].equalsIgnoreAsciiCase( rElementName ) )
181*25c6f4b7SDamjan Jovanovic {
182*25c6f4b7SDamjan Jovanovic try
183*25c6f4b7SDamjan Jovanovic {
184*25c6f4b7SDamjan Jovanovic xInStream.set( mxStorage->openStreamElement( aNames[i], ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
185*25c6f4b7SDamjan Jovanovic }
186*25c6f4b7SDamjan Jovanovic catch( Exception& )
187*25c6f4b7SDamjan Jovanovic {
188*25c6f4b7SDamjan Jovanovic }
189*25c6f4b7SDamjan Jovanovic break;
190*25c6f4b7SDamjan Jovanovic }
191*25c6f4b7SDamjan Jovanovic }
192cdf0e10cSrcweir }
193cdf0e10cSrcweir return xInStream;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir
implOpenOutputStream(const OUString & rElementName)196cdf0e10cSrcweir Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir Reference< XOutputStream > xOutStream;
199cdf0e10cSrcweir if( mxStorage.is() ) try
200cdf0e10cSrcweir {
201cdf0e10cSrcweir xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
202cdf0e10cSrcweir }
203cdf0e10cSrcweir catch( Exception& )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir }
206cdf0e10cSrcweir return xOutStream;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir
implCommit() const209cdf0e10cSrcweir void ZipStorage::implCommit() const
210cdf0e10cSrcweir {
211cdf0e10cSrcweir try
212cdf0e10cSrcweir {
213cdf0e10cSrcweir Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
214cdf0e10cSrcweir }
215cdf0e10cSrcweir catch( Exception& )
216cdf0e10cSrcweir {
217cdf0e10cSrcweir }
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir // ============================================================================
221cdf0e10cSrcweir
222cdf0e10cSrcweir } // namespace oox
223