Line data Source code
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 :
20 : #include "oox/helper/zipstorage.hxx"
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/lang/XMultiServiceFactory.hpp>
28 : #include <com/sun/star/uno/XComponentContext.hpp>
29 : #include <comphelper/storagehelper.hxx>
30 : #include "oox/helper/helper.hxx"
31 :
32 : namespace oox {
33 :
34 : // ============================================================================
35 :
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;
41 :
42 : using ::rtl::OUString;
43 :
44 : // ============================================================================
45 :
46 796 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
47 796 : StorageBase( rxInStream, false )
48 : {
49 : OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
50 : // create base storage object
51 796 : if( rxContext.is() ) try
52 : {
53 : /* #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
54 : cannot be used here as it will open a storage with format type
55 : 'PackageFormat' that will not work with OOXML packages.
56 :
57 : #161971# The MS-document storages should always be opened in repair
58 : mode to ignore the format errors and get so much info as possible.
59 : I hate this solution, but it seems to be the only consistent way to
60 : handle the MS documents.
61 :
62 : TODO: #i105410# switch to 'OFOPXMLFormat' and use its
63 : implementation of relations handling.
64 : */
65 : mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
66 : ZIP_STORAGE_FORMAT_STRING, rxInStream, rxContext,
67 796 : sal_False ); // DEV300_m80: Was sal_True, but DOCX and others did not load
68 : }
69 0 : catch (Exception const& e)
70 : {
71 : SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
72 : "exception opening input storage: " << e.Message);
73 : }
74 796 : }
75 :
76 52 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
77 52 : StorageBase( rxStream, false )
78 : {
79 : OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
80 : // create base storage object
81 52 : if( rxContext.is() ) try
82 : {
83 52 : const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
84 : mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
85 52 : OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, rxContext, sal_True );
86 : }
87 0 : catch (Exception const& e)
88 : {
89 : SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
90 : "exception opening output storage: " << e.Message);
91 : }
92 52 : }
93 :
94 598 : ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
95 598 : StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
96 598 : mxStorage( rxStorage )
97 : {
98 : SAL_WARN_IF(!mxStorage.is(), "oox.storage", "ZipStorage::ZipStorage "
99 : " - missing storage" );
100 598 : }
101 :
102 2198 : ZipStorage::~ZipStorage()
103 : {
104 2198 : }
105 :
106 694 : bool ZipStorage::implIsStorage() const
107 : {
108 694 : return mxStorage.is();
109 : }
110 :
111 150 : Reference< XStorage > ZipStorage::implGetXStorage() const
112 : {
113 150 : return mxStorage;
114 : }
115 :
116 0 : void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
117 : {
118 0 : Sequence< OUString > aNames;
119 0 : if( mxStorage.is() ) try
120 : {
121 0 : aNames = mxStorage->getElementNames();
122 0 : if( aNames.getLength() > 0 )
123 0 : orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
124 : }
125 0 : catch (Exception const& e)
126 : {
127 : SAL_INFO("oox.storage", "getElementNames: exception: " << e.Message);
128 0 : }
129 0 : }
130 :
131 664 : StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
132 : {
133 664 : Reference< XStorage > xSubXStorage;
134 664 : bool bMissing = false;
135 664 : if( mxStorage.is() ) try
136 : {
137 : // XStorage::isStorageElement may throw various exceptions...
138 664 : if( mxStorage->isStorageElement( rElementName ) )
139 494 : xSubXStorage = mxStorage->openStorageElement(
140 494 : rElementName, ::com::sun::star::embed::ElementModes::READ );
141 : }
142 340 : catch( NoSuchElementException& )
143 : {
144 170 : bMissing = true;
145 : }
146 0 : catch (Exception const& e)
147 : {
148 : SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
149 : }
150 :
151 664 : if( bMissing && bCreateMissing ) try
152 : {
153 104 : xSubXStorage = mxStorage->openStorageElement(
154 104 : rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
155 : }
156 0 : catch (Exception const& e)
157 : {
158 : SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
159 : }
160 :
161 664 : StorageRef xSubStorage;
162 664 : if( xSubXStorage.is() )
163 598 : xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
164 664 : return xSubStorage;
165 : }
166 :
167 770 : Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
168 : {
169 770 : Reference< XInputStream > xInStream;
170 770 : if( mxStorage.is() ) try
171 : {
172 770 : xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
173 : }
174 56 : catch (Exception const& e)
175 : {
176 : SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
177 : }
178 770 : return xInStream;
179 : }
180 :
181 304 : Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
182 : {
183 304 : Reference< XOutputStream > xOutStream;
184 304 : if( mxStorage.is() ) try
185 : {
186 304 : xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
187 : }
188 0 : catch (Exception const& e)
189 : {
190 : SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
191 : }
192 304 : return xOutStream;
193 : }
194 :
195 154 : void ZipStorage::implCommit() const
196 : {
197 : try
198 : {
199 154 : Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
200 : }
201 0 : catch (Exception const& e)
202 : {
203 : SAL_WARN("oox.storage", "commit: exception: " << e.Message);
204 : }
205 154 : }
206 :
207 : // ============================================================================
208 :
209 174 : } // namespace oox
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|