xref: /aoo41x/main/oox/source/helper/zipstorage.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "oox/helper/zipstorage.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
37*cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
38*cdf0e10cSrcweir #include "oox/helper/helper.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace oox {
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // ============================================================================
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using namespace ::com::sun::star::container;
45*cdf0e10cSrcweir using namespace ::com::sun::star::embed;
46*cdf0e10cSrcweir using namespace ::com::sun::star::io;
47*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
48*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using ::rtl::OUString;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir // ============================================================================
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStream ) :
55*cdf0e10cSrcweir     StorageBase( rxInStream, false )
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
58*cdf0e10cSrcweir     // create base storage object
59*cdf0e10cSrcweir     if( rxContext.is() ) try
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir         /*  #i105325# ::comphelper::OStorageHelper::GetStorageFromInputStream()
62*cdf0e10cSrcweir             cannot be used here as it will open a storage with format type
63*cdf0e10cSrcweir             'PackageFormat' that will not work with OOXML packages.
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir             #161971# The MS-document storages should always be opened in repair
66*cdf0e10cSrcweir             mode to ignore the format errors and get so much info as possible.
67*cdf0e10cSrcweir             I hate this solution, but it seems to be the only consistent way to
68*cdf0e10cSrcweir             handle the MS documents.
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir             TODO: #i105410# switch to 'OFOPXMLFormat' and use its
71*cdf0e10cSrcweir             implementation of relations handling.
72*cdf0e10cSrcweir          */
73*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
74*cdf0e10cSrcweir         mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream(
75*cdf0e10cSrcweir             ZIP_STORAGE_FORMAT_STRING, rxInStream, xFactory,
76*cdf0e10cSrcweir             sal_False );    // DEV300_m80: Was sal_True, but DOCX and others did not load
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir     catch( Exception& )
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir     }
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir ZipStorage::ZipStorage( const Reference< XComponentContext >& rxContext, const Reference< XStream >& rxStream ) :
84*cdf0e10cSrcweir     StorageBase( rxStream, false )
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir     OSL_ENSURE( rxContext.is(), "ZipStorage::ZipStorage - missing component context" );
87*cdf0e10cSrcweir     // create base storage object
88*cdf0e10cSrcweir     if( rxContext.is() ) try
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
91*cdf0e10cSrcweir         const sal_Int32 nOpenMode = ElementModes::READWRITE | ElementModes::TRUNCATE;
92*cdf0e10cSrcweir         mxStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
93*cdf0e10cSrcweir             OFOPXML_STORAGE_FORMAT_STRING, rxStream, nOpenMode, xFactory, sal_True );
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir     catch( Exception& )
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         OSL_ENSURE( false, "ZipStorage::ZipStorage - cannot open output storage" );
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir ZipStorage::ZipStorage( const ZipStorage& rParentStorage, const Reference< XStorage >& rxStorage, const OUString& rElementName ) :
102*cdf0e10cSrcweir     StorageBase( rParentStorage, rElementName, rParentStorage.isReadOnly() ),
103*cdf0e10cSrcweir     mxStorage( rxStorage )
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir     OSL_ENSURE( mxStorage.is(), "ZipStorage::ZipStorage - missing storage" );
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir ZipStorage::~ZipStorage()
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir bool ZipStorage::implIsStorage() const
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     return mxStorage.is();
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir Reference< XStorage > ZipStorage::implGetXStorage() const
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir     return mxStorage;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir void ZipStorage::implGetElementNames( ::std::vector< OUString >& orElementNames ) const
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     Sequence< OUString > aNames;
125*cdf0e10cSrcweir     if( mxStorage.is() ) try
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         aNames = mxStorage->getElementNames();
128*cdf0e10cSrcweir         if( aNames.getLength() > 0 )
129*cdf0e10cSrcweir             orElementNames.insert( orElementNames.end(), aNames.getConstArray(), aNames.getConstArray() + aNames.getLength() );
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir     catch( Exception& )
132*cdf0e10cSrcweir     {
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir StorageRef ZipStorage::implOpenSubStorage( const OUString& rElementName, bool bCreateMissing )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir     Reference< XStorage > xSubXStorage;
139*cdf0e10cSrcweir     bool bMissing = false;
140*cdf0e10cSrcweir     if( mxStorage.is() ) try
141*cdf0e10cSrcweir     {
142*cdf0e10cSrcweir         // XStorage::isStorageElement may throw various exceptions...
143*cdf0e10cSrcweir         if( mxStorage->isStorageElement( rElementName ) )
144*cdf0e10cSrcweir             xSubXStorage = mxStorage->openStorageElement(
145*cdf0e10cSrcweir                 rElementName, ::com::sun::star::embed::ElementModes::READ );
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir     catch( NoSuchElementException& )
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         bMissing = true;
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir     catch( Exception& )
152*cdf0e10cSrcweir     {
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     if( bMissing && bCreateMissing ) try
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         xSubXStorage = mxStorage->openStorageElement(
158*cdf0e10cSrcweir             rElementName, ::com::sun::star::embed::ElementModes::READWRITE );
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir     catch( Exception& )
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     StorageRef xSubStorage;
165*cdf0e10cSrcweir     if( xSubXStorage.is() )
166*cdf0e10cSrcweir         xSubStorage.reset( new ZipStorage( *this, xSubXStorage, rElementName ) );
167*cdf0e10cSrcweir     return xSubStorage;
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir Reference< XInputStream > ZipStorage::implOpenInputStream( const OUString& rElementName )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir     Reference< XInputStream > xInStream;
173*cdf0e10cSrcweir     if( mxStorage.is() ) try
174*cdf0e10cSrcweir     {
175*cdf0e10cSrcweir         xInStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READ ), UNO_QUERY );
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir     catch( Exception& )
178*cdf0e10cSrcweir     {
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir     return xInStream;
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir Reference< XOutputStream > ZipStorage::implOpenOutputStream( const OUString& rElementName )
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     Reference< XOutputStream > xOutStream;
186*cdf0e10cSrcweir     if( mxStorage.is() ) try
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         xOutStream.set( mxStorage->openStreamElement( rElementName, ::com::sun::star::embed::ElementModes::READWRITE ), UNO_QUERY );
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir     catch( Exception& )
191*cdf0e10cSrcweir     {
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir     return xOutStream;
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir void ZipStorage::implCommit() const
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir     try
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir         Reference< XTransactedObject >( mxStorage, UNO_QUERY_THROW )->commit();
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir     catch( Exception& )
203*cdf0e10cSrcweir     {
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir // ============================================================================
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir } // namespace oox
210