LibreOffice Module oox (master)  1
zipstorage.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
21 
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/uno/XComponentContext.hpp>
28 #include <osl/diagnose.h>
29 #include <sal/log.hxx>
30 #include <tools/diagnose_ex.h>
32 
33 namespace oox {
34 
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::embed;
37 using namespace ::com::sun::star::io;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::uno;
40 
42  StorageBase( rxInStream, false )
43 {
44  OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
45  // create base storage object
46  if( !rxContext.is() )
47  return;
48 
49  try
50  {
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.
54 
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.
59 
60  TODO: #i105410# switch to 'OFOPXMLFormat' and use its
61  implementation of relations handling.
62  */
64  ZIP_STORAGE_FORMAT_STRING, rxInStream, rxContext, false);
65  }
66  catch (Exception const&)
67  {
68  TOOLS_WARN_EXCEPTION("oox.storage", "ZipStorage::ZipStorage exception opening input storage");
69  }
70 }
71 
72 ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
73  StorageBase( rxStream, false )
74 {
75  OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
76  // create base storage object
77  if( rxContext.is() ) try
78  {
79  const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
81  OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, rxContext, true);
82  }
83  catch (Exception const&)
84  {
85  TOOLS_WARN_EXCEPTION("oox.storage", "ZipStorage::ZipStorage exception opening output storage");
86  }
87 }
88 
89 ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
90  StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
91  mxStorage( rxStorage )
92 {
93  SAL_WARN_IF(!mxStorage.is(), "oox.storage", "ZipStorage::ZipStorage "
94  " - missing storage" );
95 }
96 
98 {
99 }
100 
102 {
103  return mxStorage.is();
104 }
105 
106 Reference< XStorage > ZipStorage::implGetXStorage() const
107 {
108  return mxStorage;
109 }
110 
111 void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
112 {
113  Sequence< OUString > aNames;
114  if( mxStorage.is() ) try
115  {
116  aNames = mxStorage->getElementNames();
117  if( aNames.hasElements() )
118  orElementNames.insert( orElementNames.end(), aNames.begin(), aNames.end() );
119  }
120  catch (Exception const&)
121  {
122  TOOLS_INFO_EXCEPTION("oox.storage", "getElementNames");
123  }
124 }
125 
126 StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
127 {
128  Reference< XStorage > xSubXStorage;
129  bool bMissing = false;
130  if( mxStorage.is() ) try
131  {
132  // XStorage::isStorageElement may throw various exceptions...
133  if( mxStorage->isStorageElement( rElementName ) )
134  xSubXStorage = mxStorage->openStorageElement(
135  rElementName, css::embed::ElementModes::READ );
136  }
137  catch( NoSuchElementException& )
138  {
139  bMissing = true;
140  }
141  catch (Exception const&)
142  {
143  TOOLS_INFO_EXCEPTION("oox.storage", "openStorageElement");
144  }
145 
146  if( bMissing && bCreateMissing ) try
147  {
148  xSubXStorage = mxStorage->openStorageElement(
149  rElementName, css::embed::ElementModes::READWRITE );
150  }
151  catch (Exception const&)
152  {
153  TOOLS_INFO_EXCEPTION("oox.storage", "openStorageElement");
154  }
155 
156  StorageRef xSubStorage;
157  if( xSubXStorage.is() )
158  xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
159  return xSubStorage;
160 }
161 
163 {
164  Reference< XInputStream > xInStream;
165  if( mxStorage.is() ) try
166  {
167  xInStream.set( mxStorage->openStreamElement( rElementName, css::embed::ElementModes::READ ), UNO_QUERY );
168  }
169  catch (Exception const&)
170  {
171  TOOLS_INFO_EXCEPTION("oox.storage", "openStreamElement");
172  }
173  return xInStream;
174 }
175 
176 Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
177 {
178  Reference< XOutputStream > xOutStream;
179  if( mxStorage.is() ) try
180  {
181  xOutStream.set( mxStorage->openStreamElement( rElementName, css::embed::ElementModes::READWRITE ), UNO_QUERY );
182  }
183  catch (Exception const&)
184  {
185  TOOLS_INFO_EXCEPTION("oox.storage", "openStreamElement");
186  }
187  return xOutStream;
188 }
189 
191 {
192  try
193  {
194  Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
195  }
196  catch (Exception const&)
197  {
198  TOOLS_WARN_EXCEPTION("oox.storage", "commit");
199  }
200 }
201 
202 } // namespace oox
203 
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::uno::Reference< css::embed::XStorage > GetStorageOfFormatFromInputStream(const OUString &aFormat, const css::uno::Reference< css::io::XInputStream > &xStream, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >(), bool bRepairStorage=false)
virtual css::uno::Reference< css::io::XInputStream > implOpenInputStream(const OUString &rElementName) override
Opens and returns the specified input stream from the storage.
Definition: zipstorage.cxx:162
ZipStorage(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::io::XInputStream > &rxInStream)
std::shared_ptr< StorageBase > StorageRef
Definition: storagebase.hxx:42
virtual ~ZipStorage() override
Definition: zipstorage.cxx:97
Reference< XNameContainer > mxStorage
Definition: olestorage.cxx:75
static css::uno::Reference< css::embed::XStorage > GetStorageOfFormatFromStream(const OUString &aFormat, const css::uno::Reference< css::io::XStream > &xStream, sal_Int32 nStorageMode=css::embed::ElementModes::READWRITE, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >(), bool bRepairStorage=false)
virtual css::uno::Reference< css::embed::XStorage > implGetXStorage() const override
Returns the com.sun.star.embed.XStorage interface of the current storage.
Definition: zipstorage.cxx:106
#define TOOLS_WARN_EXCEPTION(area, stream)
virtual StorageRef implOpenSubStorage(const OUString &rElementName, bool bCreateMissing) override
Opens and returns the specified sub storage from the storage.
Definition: zipstorage.cxx:126
virtual bool implIsStorage() const override
Returns true, if the object represents a valid storage.
Definition: zipstorage.cxx:101
virtual void implGetElementNames(::std::vector< OUString > &orElementNames) const override
Returns the names of all elements of this storage.
Definition: zipstorage.cxx:111
#define TOOLS_INFO_EXCEPTION(area, stream)
#define SAL_WARN_IF(condition, area, stream)
virtual css::uno::Reference< css::io::XOutputStream > implOpenOutputStream(const OUString &rElementName) override
Opens and returns the specified output stream from the storage.
Definition: zipstorage.cxx:176
virtual void implCommit() const override
Commits the current storage.
Definition: zipstorage.cxx:190
Implements stream access for ZIP storages containing XML streams.
Definition: zipstorage.hxx:41