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