update credits
[LibreOffice.git] / oox / source / helper / zipstorage.cxx
bloba93045b10f207a3d29f8d4fcc8f2c07723e23d23
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "oox/helper/zipstorage.hxx"
22 #include <com/sun/star/embed/ElementModes.hpp>
23 #include <com/sun/star/embed/XStorage.hpp>
24 #include <com/sun/star/embed/XTransactedObject.hpp>
25 #include <com/sun/star/io/XInputStream.hpp>
26 #include <com/sun/star/io/XOutputStream.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <comphelper/storagehelper.hxx>
30 #include "oox/helper/helper.hxx"
32 namespace oox {
34 // ============================================================================
36 using namespace ::com::sun::star::container;
37 using namespace ::com::sun::star::embed;
38 using namespace ::com::sun::star::io;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::uno;
42 // ============================================================================
44 ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
45 StorageBase( rxInStream, false )
47 OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
48 // create base storage object
49 if( rxContext.is() ) try
51 /* #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
52 cannot be used here as it will open a storage with format type
53 'PackageFormat' that will not work with OOXML packages.
55 #161971# The MS-document storages should always be opened in repair
56 mode to ignore the format errors and get so much info as possible.
57 I hate this solution, but it seems to be the only consistent way to
58 handle the MS documents.
60 TODO: #i105410# switch to 'OFOPXMLFormat' and use its
61 implementation of relations handling.
63 mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
64 ZIP_STORAGE_FORMAT_STRING, rxInStream, rxContext,
65 sal_False ); // DEV300_m80: Was sal_True, but DOCX and others did not load
67 catch (Exception const& e)
69 SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
70 "exception opening input storage: " << e.Message);
74 ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
75 StorageBase( rxStream, false )
77 OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
78 // create base storage object
79 if( rxContext.is() ) try
81 const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
82 mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
83 OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, rxContext, sal_True );
85 catch (Exception const& e)
87 SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
88 "exception opening output storage: " << e.Message);
92 ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
93 StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
94 mxStorage( rxStorage )
96 SAL_WARN_IF(!mxStorage.is(), "oox.storage", "ZipStorage::ZipStorage "
97 " - missing storage" );
100 ZipStorage::~ZipStorage()
104 bool ZipStorage::implIsStorage() const
106 return mxStorage.is();
109 Reference< XStorage > ZipStorage::implGetXStorage() const
111 return mxStorage;
114 void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
116 Sequence< OUString > aNames;
117 if( mxStorage.is() ) try
119 aNames = mxStorage->getElementNames();
120 if( aNames.getLength() > 0 )
121 orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
123 catch (Exception const& e)
125 SAL_INFO("oox.storage", "getElementNames: exception: " << e.Message);
129 StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
131 Reference< XStorage > xSubXStorage;
132 bool bMissing = false;
133 if( mxStorage.is() ) try
135 // XStorage::isStorageElement may throw various exceptions...
136 if( mxStorage->isStorageElement( rElementName ) )
137 xSubXStorage = mxStorage->openStorageElement(
138 rElementName, ::com::sun::star::embed::ElementModes::READ );
140 catch( NoSuchElementException& )
142 bMissing = true;
144 catch (Exception const& e)
146 SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
149 if( bMissing && bCreateMissing ) try
151 xSubXStorage = mxStorage->openStorageElement(
152 rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
154 catch (Exception const& e)
156 SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
159 StorageRef xSubStorage;
160 if( xSubXStorage.is() )
161 xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
162 return xSubStorage;
165 Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
167 Reference< XInputStream > xInStream;
168 if( mxStorage.is() ) try
170 xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
172 catch (Exception const& e)
174 SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
176 return xInStream;
179 Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
181 Reference< XOutputStream > xOutStream;
182 if( mxStorage.is() ) try
184 xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
186 catch (Exception const& e)
188 SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
190 return xOutStream;
193 void ZipStorage::implCommit() const
197 Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
199 catch (Exception const& e)
201 SAL_WARN("oox.storage", "commit: exception: " << e.Message);
205 // ============================================================================
207 } // namespace oox
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */