1*ca5ec200SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ca5ec200SAndrew Rist * distributed with this work for additional information 6*ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance 9*ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ca5ec200SAndrew Rist * software distributed under the License is distributed on an 15*ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the 17*ca5ec200SAndrew Rist * specific language governing permissions and limitations 18*ca5ec200SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ca5ec200SAndrew Rist *************************************************************/ 21*ca5ec200SAndrew Rist 22*ca5ec200SAndrew 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 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 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 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 104cdf0e10cSrcweir ZipStorage::~ZipStorage() 105cdf0e10cSrcweir { 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir bool ZipStorage::implIsStorage() const 109cdf0e10cSrcweir { 110cdf0e10cSrcweir return mxStorage.is(); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir Reference< XStorage > ZipStorage::implGetXStorage() const 114cdf0e10cSrcweir { 115cdf0e10cSrcweir return mxStorage; 116cdf0e10cSrcweir } 117cdf0e10cSrcweir 118cdf0e10cSrcweir 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 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 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 { 175cdf0e10cSrcweir } 176cdf0e10cSrcweir return xInStream; 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName ) 180cdf0e10cSrcweir { 181cdf0e10cSrcweir Reference< XOutputStream > xOutStream; 182cdf0e10cSrcweir if( mxStorage.is() ) try 183cdf0e10cSrcweir { 184cdf0e10cSrcweir xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY ); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir catch( Exception& ) 187cdf0e10cSrcweir { 188cdf0e10cSrcweir } 189cdf0e10cSrcweir return xOutStream; 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir void ZipStorage::implCommit() const 193cdf0e10cSrcweir { 194cdf0e10cSrcweir try 195cdf0e10cSrcweir { 196cdf0e10cSrcweir Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit(); 197cdf0e10cSrcweir } 198cdf0e10cSrcweir catch( Exception& ) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir } 201cdf0e10cSrcweir } 202cdf0e10cSrcweir 203cdf0e10cSrcweir // ============================================================================ 204cdf0e10cSrcweir 205cdf0e10cSrcweir } // namespace oox 206