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 :
43 :
44 0 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
45 0 : StorageBase( rxInStream, false )
46 : {
47 : OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
48 : // create base storage object
49 0 : if( rxContext.is() ) 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 : */
63 0 : mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
64 : ZIP_STORAGE_FORMAT_STRING, rxInStream, rxContext,
65 0 : false ); // DEV300_m80: Was sal_True, but DOCX and others did not load
66 : }
67 0 : catch (Exception const& e)
68 : {
69 : SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
70 : "exception opening input storage: " << e.Message);
71 : }
72 0 : }
73 :
74 0 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
75 0 : StorageBase( rxStream, false )
76 : {
77 : OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
78 : // create base storage object
79 0 : if( rxContext.is() ) try
80 : {
81 0 : const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
82 0 : mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
83 0 : OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, rxContext, true );
84 : }
85 0 : catch (Exception const& e)
86 : {
87 : SAL_WARN("oox.storage", "ZipStorage::ZipStorage "
88 : "exception opening output storage: " << e.Message);
89 : }
90 0 : }
91 :
92 0 : ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
93 0 : StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
94 0 : mxStorage( rxStorage )
95 : {
96 : SAL_WARN_IF(!mxStorage.is(), "oox.storage", "ZipStorage::ZipStorage "
97 : " - missing storage" );
98 0 : }
99 :
100 0 : ZipStorage::~ZipStorage()
101 : {
102 0 : }
103 :
104 0 : bool ZipStorage::implIsStorage() const
105 : {
106 0 : return mxStorage.is();
107 : }
108 :
109 0 : Reference< XStorage > ZipStorage::implGetXStorage() const
110 : {
111 0 : return mxStorage;
112 : }
113 :
114 0 : void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
115 : {
116 0 : Sequence< OUString > aNames;
117 0 : if( mxStorage.is() ) try
118 : {
119 0 : aNames = mxStorage->getElementNames();
120 0 : if( aNames.getLength() > 0 )
121 0 : orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
122 : }
123 0 : catch (Exception const& e)
124 : {
125 : SAL_INFO("oox.storage", "getElementNames: exception: " << e.Message);
126 0 : }
127 0 : }
128 :
129 0 : StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
130 : {
131 0 : Reference< XStorage > xSubXStorage;
132 0 : bool bMissing = false;
133 0 : if( mxStorage.is() ) try
134 : {
135 : // XStorage::isStorageElement may throw various exceptions...
136 0 : if( mxStorage->isStorageElement( rElementName ) )
137 0 : xSubXStorage = mxStorage->openStorageElement(
138 0 : rElementName, ::com::sun::star::embed::ElementModes::READ );
139 : }
140 0 : catch( NoSuchElementException& )
141 : {
142 0 : bMissing = true;
143 : }
144 0 : catch (Exception const& e)
145 : {
146 : SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
147 : }
148 :
149 0 : if( bMissing && bCreateMissing ) try
150 : {
151 0 : xSubXStorage = mxStorage->openStorageElement(
152 0 : rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
153 : }
154 0 : catch (Exception const& e)
155 : {
156 : SAL_INFO("oox.storage", "openStorageElement: exception: " << e.Message);
157 : }
158 :
159 0 : StorageRef xSubStorage;
160 0 : if( xSubXStorage.is() )
161 0 : xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
162 0 : return xSubStorage;
163 : }
164 :
165 0 : Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
166 : {
167 0 : Reference< XInputStream > xInStream;
168 0 : if( mxStorage.is() ) try
169 : {
170 0 : xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
171 : }
172 0 : catch (Exception const& e)
173 : {
174 : SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
175 : }
176 0 : return xInStream;
177 : }
178 :
179 0 : Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
180 : {
181 0 : Reference< XOutputStream > xOutStream;
182 0 : if( mxStorage.is() ) try
183 : {
184 0 : xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
185 : }
186 0 : catch (Exception const& e)
187 : {
188 : SAL_INFO("oox.storage", "openStreamElement: exception: " << e.Message);
189 : }
190 0 : return xOutStream;
191 : }
192 :
193 0 : void ZipStorage::implCommit() const
194 : {
195 : try
196 : {
197 0 : Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
198 : }
199 0 : catch (Exception const& e)
200 : {
201 : SAL_WARN("oox.storage", "commit: exception: " << e.Message);
202 : }
203 0 : }
204 :
205 :
206 :
207 0 : } // namespace oox
208 :
209 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|