1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: zipstorage.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/helper/zipstorage.hxx"
32 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/embed/XStorage.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/io/XInputStream.hpp>
37 #include <com/sun/star/io/XOutputStream.hpp>
38 #include <comphelper/storagehelper.hxx>
39 #include "oox/helper/helper.hxx"
41 using ::rtl::OUString
;
42 using ::com::sun::star::uno::Any
;
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::Sequence
;
45 using ::com::sun::star::uno::Exception
;
46 using ::com::sun::star::uno::UNO_QUERY
;
47 using ::com::sun::star::lang::XMultiServiceFactory
;
48 using ::com::sun::star::embed::XStorage
;
49 using ::com::sun::star::io::XInputStream
;
50 using ::com::sun::star::io::XOutputStream
;
51 using ::com::sun::star::io::XStream
;
55 // ============================================================================
57 ZipStorage::ZipStorage(
58 const Reference
< XMultiServiceFactory
>& rxFactory
,
59 const Reference
< XInputStream
>& rxInStream
) :
60 StorageBase( rxInStream
, false )
62 OSL_ENSURE( rxFactory
.is(), "ZipStorage::ZipStorage - missing service factory" );
63 // create base storage object
66 mxStorage
= ::comphelper::OStorageHelper::GetStorageFromInputStream( rxInStream
, rxFactory
);
73 ZipStorage::ZipStorage(
74 const Reference
< XMultiServiceFactory
>& rxFactory
,
75 const Reference
< XStream
>& rxStream
) :
76 StorageBase( rxStream
, false )
78 OSL_ENSURE( rxFactory
.is(), "ZipStorage::ZipStorage - missing service factory" );
79 // create base storage object
82 mxStorage
= ::comphelper::OStorageHelper::GetStorageOfFormatFromStream( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OFOPXMLFormat" ) ),
84 com::sun::star::embed::ElementModes::READWRITE
85 | com::sun::star::embed::ElementModes::TRUNCATE
,
90 OSL_ENSURE( false, "ZipStorage::ZipStorage - cannot open output storage" );
94 ZipStorage::ZipStorage( const ZipStorage
& rParentStorage
, const Reference
< XStorage
>& rxStorage
, const OUString
& rElementName
) :
95 StorageBase( rParentStorage
, rElementName
),
96 mxStorage( rxStorage
)
98 OSL_ENSURE( mxStorage
.is(), "ZipStorage::ZipStorage - missing storage" );
101 ZipStorage::~ZipStorage()
105 bool ZipStorage::implIsStorage() const
107 return mxStorage
.is();
110 Reference
< XStorage
> ZipStorage::implGetXStorage() const
115 void ZipStorage::implGetElementNames( ::std::vector
< OUString
>& orElementNames
) const
117 Sequence
< OUString
> aNames
;
118 if( mxStorage
.is() ) try
120 aNames
= mxStorage
->getElementNames();
121 if( aNames
.getLength() > 0 )
122 orElementNames
.insert( orElementNames
.end(), aNames
.getConstArray(), aNames
.getConstArray() + aNames
.getLength() );
129 StorageRef
ZipStorage::implOpenSubStorage( const OUString
& rElementName
, bool bCreate
)
131 Reference
< XStorage
> xSubXStorage
;
132 bool bMissing
= false;
133 if( mxStorage
.is() ) try
135 // XStorage::isStorageElement may throw various exceptions...
136 if( mxStorage
->isStorageElement( rElementName
) )
137 xSubXStorage
= mxStorage
->openStorageElement(
138 rElementName
, ::com::sun::star::embed::ElementModes::READ
);
140 catch( ::com::sun::star::container::NoSuchElementException
& )
148 if( bMissing
&& bCreate
)
151 xSubXStorage
= mxStorage
->openStorageElement(
152 rElementName
, ::com::sun::star::embed::ElementModes::READWRITE
);
158 StorageRef xSubStorage
;
159 if( xSubXStorage
.is() )
160 xSubStorage
.reset( new ZipStorage( *this, xSubXStorage
, rElementName
) );
164 Reference
< XInputStream
> ZipStorage::implOpenInputStream( const OUString
& rElementName
)
166 Reference
< XInputStream
> xInStream
;
167 if( mxStorage
.is() ) try
169 xInStream
.set( mxStorage
->openStreamElement( rElementName
, ::com::sun::star::embed::ElementModes::READ
), UNO_QUERY
);
177 Reference
< XOutputStream
> ZipStorage::implOpenOutputStream( const OUString
& rElementName
)
179 Reference
< XOutputStream
> xOutStream
;
180 if( mxStorage
.is() ) try
182 xOutStream
.set( mxStorage
->openStreamElement( rElementName
, ::com::sun::star::embed::ElementModes::READWRITE
), UNO_QUERY
);
190 // ============================================================================