bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / helper / zipstorage.cxx
blob2dbf3cdf27003558040137f0c6319e329f3da395
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 <osl/diagnose.h>
30 #include <sal/log.hxx>
31 #include <comphelper/storagehelper.hxx>
32 #include <oox/helper/helper.hxx>
34 namespace oox {
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 ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
43 StorageBase( rxInStream, false )
45 OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
46 // create base storage object
47 if( rxContext.is() ) try
49 /* #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
50 cannot be used here as it will open a storage with format type
51 'PackageFormat' that will not work with OOXML packages.
53 #161971# The MS-document storages should always be opened in repair
54 mode to ignore the format errors and get so much info as possible.
55 I hate this solution, but it seems to be the only consistent way to
56 handle the MS documents.
58 TODO: #i105410# switch to 'OFOPXMLFormat' and use its
59 implementation of relations handling.
61 mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
62 ZIP_STORAGE_FORMAT_STRING, rxInStream, rxContext, false);
64 catch (Exception const& e)
66 SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
67 "exception opening input storage: " << e);
71 ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
72 StorageBase( rxStream, false )
74 OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
75 // create base storage object
76 if( rxContext.is() ) try
78 const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
79 mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
80 OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, rxContext, true);
82 catch (Exception const& e)
84 SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
85 "exception opening output storage: " << e);
89 ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
90 StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
91 mxStorage( rxStorage )
93 SAL_WARN_IF(!mxStorage.is(), "oox.storage", "ZipStorage::ZipStorage "
94 " - missing storage" );
97 ZipStorage::~ZipStorage()
101 bool ZipStorage::implIsStorage() const
103 return mxStorage.is();
106 Reference< XStorage > ZipStorage::implGetXStorage() const
108 return mxStorage;
111 void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
113 Sequence< OUString > aNames;
114 if( mxStorage.is() ) try
116 aNames = mxStorage->getElementNames();
117 if( aNames.hasElements() )
118 orElementNames.insert( orElementNames.end(), aNames.begin(), aNames.end() );
120 catch (Exception const& e)
122 SAL_INFO("oox.storage", "getElementNames: " << e);
126 StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
128 Reference< XStorage > xSubXStorage;
129 bool bMissing = false;
130 if( mxStorage.is() ) try
132 // XStorage::isStorageElement may throw various exceptions...
133 if( mxStorage->isStorageElement( rElementName ) )
134 xSubXStorage = mxStorage->openStorageElement(
135 rElementName, css::embed::ElementModes::READ );
137 catch( NoSuchElementException& )
139 bMissing = true;
141 catch (Exception const& e)
143 SAL_INFO("oox.storage", "openStorageElement: " << e);
146 if( bMissing && bCreateMissing ) try
148 xSubXStorage = mxStorage->openStorageElement(
149 rElementName, css::embed::ElementModes::READWRITE );
151 catch (Exception const& e)
153 SAL_INFO("oox.storage", "openStorageElement: " << e);
156 StorageRef xSubStorage;
157 if( xSubXStorage.is() )
158 xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
159 return xSubStorage;
162 Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
164 Reference< XInputStream > xInStream;
165 if( mxStorage.is() ) try
167 xInStream.set( mxStorage->openStreamElement( rElementName, css::embed::ElementModes::READ ), UNO_QUERY );
169 catch (Exception const& e)
171 SAL_INFO("oox.storage", "openStreamElement: " << e);
173 return xInStream;
176 Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
178 Reference< XOutputStream > xOutStream;
179 if( mxStorage.is() ) try
181 xOutStream.set( mxStorage->openStreamElement( rElementName, css::embed::ElementModes::READWRITE ), UNO_QUERY );
183 catch (Exception const& e)
185 SAL_INFO("oox.storage", "openStreamElement: " << e);
187 return xOutStream;
190 void ZipStorage::implCommit() const
194 Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
196 catch (Exception const& e)
198 SAL_WARN("oox.storage", "commit: " << e);
202 } // namespace oox
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */