Branch data 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 : 815 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
47 : 815 : StorageBase( rxInStream, false )
48 : : {
49 : : OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
50 : : // create base storage object
51 [ + - ]: 815 : 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 [ + - ][ + - ]: 815 : Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
[ + - ]
66 : : mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
67 : : ZIP_STORAGE_FORMAT_STRING, rxInStream, xFactory,
68 [ + - ][ + - ]: 815 : sal_False ); // DEV300_m80: Was sal_True, but DOCX and others did not load
[ # # ][ + - ]
69 : : }
70 [ # # ]: 0 : catch( Exception& )
71 : : {
72 : : }
73 : 815 : }
74 : :
75 : 57 : ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
76 : 57 : StorageBase( rxStream, false )
77 : : {
78 : : OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
79 : : // create base storage object
80 [ + - ]: 57 : if( rxContext.is() ) try
81 : : {
82 [ + - ][ + - ]: 57 : Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
[ + - ]
83 : 57 : const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
84 : : mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
85 [ + - ][ + - ]: 57 : OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, xFactory, sal_True );
[ # # ][ + - ]
86 : : }
87 [ # # ]: 0 : catch( Exception& )
88 : : {
89 : : OSL_FAIL( "ZipStorage::ZipStorage - cannot open output storage" );
90 : : }
91 : 57 : }
92 : :
93 : 623 : ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
94 : 623 : StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
95 : 623 : mxStorage( rxStorage )
96 : : {
97 : : OSL_ENSURE( mxStorage.is(), "ZipStorage::ZipStorage - missing storage" );
98 : 623 : }
99 : :
100 : 1495 : ZipStorage::~ZipStorage()
101 : : {
102 [ - + ]: 2265 : }
103 : :
104 : 725 : bool ZipStorage::implIsStorage() const
105 : : {
106 : 725 : return mxStorage.is();
107 : : }
108 : :
109 : 171 : Reference< XStorage > ZipStorage::implGetXStorage() const
110 : : {
111 : 171 : 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& )
124 : : {
125 [ # # ]: 0 : }
126 : 0 : }
127 : :
128 : 714 : StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
129 : : {
130 : 714 : Reference< XStorage > xSubXStorage;
131 : 714 : bool bMissing = false;
132 [ + - ]: 714 : if( mxStorage.is() ) try
133 : : {
134 : : // XStorage::isStorageElement may throw various exceptions...
135 [ + - ][ + + ]: 714 : if( mxStorage->isStorageElement( rElementName ) )
[ + - ]
136 [ + - ]: 509 : xSubXStorage = mxStorage->openStorageElement(
137 [ + - ][ + - ]: 714 : rElementName, ::com::sun::star::embed::ElementModes::READ );
[ - + - ]
138 : : }
139 [ + - ]: 205 : catch( NoSuchElementException& )
140 : : {
141 : 205 : bMissing = true;
142 : : }
143 [ # # ]: 0 : catch( Exception& )
144 : : {
145 : : }
146 : :
147 [ + + ][ + + ]: 714 : if( bMissing && bCreateMissing ) try
148 : : {
149 [ + - ]: 114 : xSubXStorage = mxStorage->openStorageElement(
150 [ + - ][ + - ]: 114 : rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
[ # # ]
151 : : }
152 [ # # ]: 0 : catch( Exception& )
153 : : {
154 : : }
155 : :
156 [ + - ]: 714 : StorageRef xSubStorage;
157 [ + + ]: 714 : if( xSubXStorage.is() )
158 [ + - ][ + - ]: 623 : xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
[ + - ]
159 : 714 : return xSubStorage;
160 : : }
161 : :
162 : 817 : Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
163 : : {
164 : 817 : Reference< XInputStream > xInStream;
165 [ + - ]: 817 : if( mxStorage.is() ) try
166 : : {
167 [ + - ][ + + ]: 817 : xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
[ + - ][ - + ]
168 : : }
169 [ + - ]: 60 : catch( Exception& )
170 : : {
171 : : }
172 : 817 : return xInStream;
173 : : }
174 : :
175 : 345 : Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
176 : : {
177 : 345 : Reference< XOutputStream > xOutStream;
178 [ + - ]: 345 : if( mxStorage.is() ) try
179 : : {
180 [ + - ][ + - ]: 345 : xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
[ + - ][ # # ]
181 : : }
182 [ # # ]: 0 : catch( Exception& )
183 : : {
184 : : }
185 : 345 : return xOutStream;
186 : : }
187 : :
188 : 171 : void ZipStorage::implCommit() const
189 : : {
190 : : try
191 : : {
192 [ + - ][ + - ]: 171 : Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
[ + - ][ # # ]
193 : : }
194 : 0 : catch( Exception& )
195 : : {
196 : : }
197 : 171 : }
198 : :
199 : : // ============================================================================
200 : :
201 [ + - ][ + - ]: 285 : } // namespace oox
202 : :
203 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|