1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <tools/diagnose_ex.h>
32 #include <comphelper/storagehelper.hxx>
33 #include <oox/helper/helper.hxx>
37 using namespace ::com::sun::star::container
;
38 using namespace ::com::sun::star::embed
;
39 using namespace ::com::sun::star::io
;
40 using namespace ::com::sun::star::lang
;
41 using namespace ::com::sun::star::uno
;
43 ZipStorage::ZipStorage( const Reference
< XComponentContext
>& rxContext
, const Reference
< XInputStream
>& rxInStream
) :
44 StorageBase( rxInStream
, false )
46 OSL_ENSURE( rxContext
.is(), "ZipStorage::ZipStorage - missing component context" );
47 // create base storage object
48 if( rxContext
.is() ) try
50 /* #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
51 cannot be used here as it will open a storage with format type
52 'PackageFormat' that will not work with OOXML packages.
54 #161971# The MS-document storages should always be opened in repair
55 mode to ignore the format errors and get so much info as possible.
56 I hate this solution, but it seems to be the only consistent way to
57 handle the MS documents.
59 TODO: #i105410# switch to 'OFOPXMLFormat' and use its
60 implementation of relations handling.
62 mxStorage
= ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
63 ZIP_STORAGE_FORMAT_STRING
, rxInStream
, rxContext
, false);
65 catch (Exception
const&)
67 TOOLS_WARN_EXCEPTION("oox.storage", "ZipStorage::ZipStorage exception opening input storage");
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&)
84 TOOLS_WARN_EXCEPTION("oox.storage", "ZipStorage::ZipStorage exception opening output storage");
88 ZipStorage::ZipStorage( const ZipStorage
& rParentStorage
, const Reference
< XStorage
>& rxStorage
, const OUString
& rElementName
) :
89 StorageBase( rParentStorage
, rElementName
, rParentStorage
.isReadOnly() ),
90 mxStorage( rxStorage
)
92 SAL_WARN_IF(!mxStorage
.is(), "oox.storage", "ZipStorage::ZipStorage "
93 " - missing storage" );
96 ZipStorage::~ZipStorage()
100 bool ZipStorage::implIsStorage() const
102 return mxStorage
.is();
105 Reference
< XStorage
> ZipStorage::implGetXStorage() const
110 void ZipStorage::implGetElementNames( ::std::vector
< OUString
>& orElementNames
) const
112 Sequence
< OUString
> aNames
;
113 if( mxStorage
.is() ) try
115 aNames
= mxStorage
->getElementNames();
116 if( aNames
.hasElements() )
117 orElementNames
.insert( orElementNames
.end(), aNames
.begin(), aNames
.end() );
119 catch (Exception
const&)
121 TOOLS_INFO_EXCEPTION("oox.storage", "getElementNames");
125 StorageRef
ZipStorage::implOpenSubStorage( const OUString
& rElementName
, bool bCreateMissing
)
127 Reference
< XStorage
> xSubXStorage
;
128 bool bMissing
= false;
129 if( mxStorage
.is() ) try
131 // XStorage::isStorageElement may throw various exceptions...
132 if( mxStorage
->isStorageElement( rElementName
) )
133 xSubXStorage
= mxStorage
->openStorageElement(
134 rElementName
, css::embed::ElementModes::READ
);
136 catch( NoSuchElementException
& )
140 catch (Exception
const&)
142 TOOLS_INFO_EXCEPTION("oox.storage", "openStorageElement");
145 if( bMissing
&& bCreateMissing
) try
147 xSubXStorage
= mxStorage
->openStorageElement(
148 rElementName
, css::embed::ElementModes::READWRITE
);
150 catch (Exception
const&)
152 TOOLS_INFO_EXCEPTION("oox.storage", "openStorageElement");
155 StorageRef xSubStorage
;
156 if( xSubXStorage
.is() )
157 xSubStorage
.reset( new ZipStorage( *this, xSubXStorage
, rElementName
) );
161 Reference
< XInputStream
> ZipStorage::implOpenInputStream( const OUString
& rElementName
)
163 Reference
< XInputStream
> xInStream
;
164 if( mxStorage
.is() ) try
166 xInStream
.set( mxStorage
->openStreamElement( rElementName
, css::embed::ElementModes::READ
), UNO_QUERY
);
168 catch (Exception
const&)
170 TOOLS_INFO_EXCEPTION("oox.storage", "openStreamElement");
175 Reference
< XOutputStream
> ZipStorage::implOpenOutputStream( const OUString
& rElementName
)
177 Reference
< XOutputStream
> xOutStream
;
178 if( mxStorage
.is() ) try
180 xOutStream
.set( mxStorage
->openStreamElement( rElementName
, css::embed::ElementModes::READWRITE
), UNO_QUERY
);
182 catch (Exception
const&)
184 TOOLS_INFO_EXCEPTION("oox.storage", "openStreamElement");
189 void ZipStorage::implCommit() const
193 Reference
< XTransactedObject
>( mxStorage
, UNO_QUERY_THROW
)->commit();
195 catch (Exception
const&)
197 TOOLS_WARN_EXCEPTION("oox.storage", "commit");
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */