19e0fc027SAndrew Rist /**************************************************************
2*2eda3cbfSmseidel  *
39e0fc027SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49e0fc027SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59e0fc027SAndrew Rist  * distributed with this work for additional information
69e0fc027SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79e0fc027SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89e0fc027SAndrew Rist  * "License"); you may not use this file except in compliance
99e0fc027SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2eda3cbfSmseidel  *
119e0fc027SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2eda3cbfSmseidel  *
139e0fc027SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149e0fc027SAndrew Rist  * software distributed under the License is distributed on an
159e0fc027SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169e0fc027SAndrew Rist  * KIND, either express or implied.  See the License for the
179e0fc027SAndrew Rist  * specific language governing permissions and limitations
189e0fc027SAndrew Rist  * under the License.
19*2eda3cbfSmseidel  *
209e0fc027SAndrew Rist  *************************************************************/
219e0fc027SAndrew Rist 
229e0fc027SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_filter.hxx"
26cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataControl.hpp>
27cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
28cdf0e10cSrcweir #include <com/sun/star/frame/XConfigManager.hpp>
29cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
30cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
33cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
34cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
35cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <comphelper/oslfile2streamwrap.hxx>
39cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
40cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
41cdf0e10cSrcweir #include <tools/stream.hxx>
42cdf0e10cSrcweir #include <tools/urlobj.hxx>
43cdf0e10cSrcweir #include <unotools/tempfile.hxx>
44cdf0e10cSrcweir #include <svl/urihelper.hxx>
45cdf0e10cSrcweir #include <osl/file.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <rtl/uri.hxx>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include "xmlfilterjar.hxx"
50cdf0e10cSrcweir #include "xmlfilterdialogstrings.hrc"
51cdf0e10cSrcweir #include "xmlfiltersettingsdialog.hxx"
52cdf0e10cSrcweir #include "typedetectionexport.hxx"
53cdf0e10cSrcweir #include "typedetectionimport.hxx"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir using namespace rtl;
56cdf0e10cSrcweir using namespace osl;
57cdf0e10cSrcweir using namespace comphelper;
58cdf0e10cSrcweir using namespace com::sun::star;
59cdf0e10cSrcweir using namespace com::sun::star::lang;
60cdf0e10cSrcweir using namespace com::sun::star::frame;
61cdf0e10cSrcweir using namespace com::sun::star::uno;
62cdf0e10cSrcweir using namespace com::sun::star::util;
63cdf0e10cSrcweir using namespace com::sun::star::container;
64cdf0e10cSrcweir using namespace com::sun::star::beans;
65cdf0e10cSrcweir using namespace com::sun::star::io;
66cdf0e10cSrcweir 
XMLFilterJarHelper(Reference<XMultiServiceFactory> & xMSF)67cdf0e10cSrcweir XMLFilterJarHelper::XMLFilterJarHelper( Reference< XMultiServiceFactory >& xMSF )
68cdf0e10cSrcweir : mxMSF( xMSF ),
69cdf0e10cSrcweir 	sVndSunStarPackage( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.Package:" ) ),
70cdf0e10cSrcweir 	sXSLTPath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/xslt/" ) ),
71cdf0e10cSrcweir 	sDTDPath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/dtd/" ) ),
72cdf0e10cSrcweir 	sTemplatePath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/template/") ),
73cdf0e10cSrcweir 	sSpecialConfigManager( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.config.SpecialConfigManager" ) ),
74cdf0e10cSrcweir 	sPump( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.Pump" ) ),
75cdf0e10cSrcweir 	sProgPath( RTL_CONSTASCII_USTRINGPARAM( "$(prog)/" ) )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	try
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		Reference< XConfigManager > xCfgMgr( xMSF->createInstance(OUString::createFromAscii("com.sun.star.config.SpecialConfigManager")), UNO_QUERY );
80cdf0e10cSrcweir 		if( xCfgMgr.is() )
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			sProgPath = xCfgMgr->substituteVariables( sProgPath );
83cdf0e10cSrcweir 			sXSLTPath = xCfgMgr->substituteVariables( sXSLTPath );
84cdf0e10cSrcweir 			sDTDPath = xCfgMgr->substituteVariables( sDTDPath );
85cdf0e10cSrcweir 			sTemplatePath = xCfgMgr->substituteVariables( sTemplatePath );
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir 	catch(Exception&)
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
encodeZipUri(const OUString & rURI)93cdf0e10cSrcweir static OUString encodeZipUri( const OUString& rURI )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 );
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
addFolder(Reference<XInterface> & xRootFolder,Reference<XSingleServiceFactory> & xFactory,const OUString & rName)98cdf0e10cSrcweir static Reference< XInterface > addFolder( Reference< XInterface >& xRootFolder, Reference< XSingleServiceFactory >& xFactory, const OUString& rName ) throw( Exception )
99cdf0e10cSrcweir {
100*2eda3cbfSmseidel 	if ( rName.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( ".." ) ) )
101*2eda3cbfSmseidel 	  || rName.equals( OUString( RTL_CONSTASCII_USTRINGPARAM( "." ) ) ) )
102*2eda3cbfSmseidel 		throw lang::IllegalArgumentException();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	Sequence< Any > aArgs(1);
105cdf0e10cSrcweir 	aArgs[0] <<= (sal_Bool)sal_True;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	Reference< XInterface > xFolder( xFactory->createInstanceWithArguments(aArgs) );
108cdf0e10cSrcweir 	Reference< XNamed > xNamed( xFolder, UNO_QUERY );
109cdf0e10cSrcweir 	Reference< XChild > xChild( xFolder, UNO_QUERY );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	if( xNamed.is() && xChild.is() )
112cdf0e10cSrcweir 	{
113cdf0e10cSrcweir 		OUString aName( encodeZipUri( rName ) );
114cdf0e10cSrcweir 		xNamed->setName( aName );
115cdf0e10cSrcweir 		xChild->setParent( xRootFolder );
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	return xFolder;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
_addFile(Reference<XInterface> & xRootFolder,Reference<XSingleServiceFactory> & xFactory,Reference<XInputStream> & xInput,OUString aName)121cdf0e10cSrcweir static void _addFile( Reference< XInterface >& xRootFolder, Reference< XSingleServiceFactory >& xFactory, Reference< XInputStream >& xInput, OUString aName ) throw( Exception )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 
124*2eda3cbfSmseidel 	Reference< XActiveDataSink > xSink( xFactory->createInstance(), UNO_QUERY );
125*2eda3cbfSmseidel 	Reference< XUnoTunnel > xTunnel( xSink, UNO_QUERY );
126cdf0e10cSrcweir 	if( xSink.is() && xTunnel.is())
127*2eda3cbfSmseidel 	{
128*2eda3cbfSmseidel 		Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY );
129*2eda3cbfSmseidel 		xNameContainer->insertByName(aName = encodeZipUri( aName ), makeAny(xTunnel));
130cdf0e10cSrcweir 		xSink->setInputStream( xInput );
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir /*
135cdf0e10cSrcweir static void addFile( Reference< XInterface > xRootFolder, Reference< XSingleServiceFactory > xFactory, const OUString& rSourceFile, const OUString& rName ) throw( Exception )
136cdf0e10cSrcweir {
137*2eda3cbfSmseidel 	Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( new SvFileStream(rSourceFile, STREAM_READ ), true ) );
138cdf0e10cSrcweir 	_addFile( xRootFolder, xFactory, xInput, rName );
139cdf0e10cSrcweir }
140cdf0e10cSrcweir */
141cdf0e10cSrcweir 
addFile(Reference<XInterface> xRootFolder,Reference<XSingleServiceFactory> xFactory,const OUString & rSourceFile)142cdf0e10cSrcweir void XMLFilterJarHelper::addFile( Reference< XInterface > xRootFolder, Reference< XSingleServiceFactory > xFactory, const OUString& rSourceFile ) throw( Exception )
143cdf0e10cSrcweir {
144*2eda3cbfSmseidel 	if( rSourceFile.getLength() &&
145cdf0e10cSrcweir 		(rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("http:") ) != 0) &&
146cdf0e10cSrcweir 		(rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("shttp:") ) != 0) &&
147cdf0e10cSrcweir 		(rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("jar:") ) != 0) &&
148cdf0e10cSrcweir 		(rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("ftp:") ) != 0))
149cdf0e10cSrcweir 	{
150cdf0e10cSrcweir 		OUString aFileURL( rSourceFile );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 		if( !aFileURL.matchIgnoreAsciiCase( OUString( RTL_CONSTASCII_USTRINGPARAM("file://") ) ) )
153cdf0e10cSrcweir 		{
154cdf0e10cSrcweir 			aFileURL = URIHelper::SmartRel2Abs( sProgPath, aFileURL, Link(), false );
155cdf0e10cSrcweir 		}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 		INetURLObject aURL( aFileURL );
158cdf0e10cSrcweir 		OUString aName( aURL.getName() );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		SvFileStream* pStream = new SvFileStream(aFileURL, STREAM_READ );
161*2eda3cbfSmseidel 		Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( pStream, true ) );
162cdf0e10cSrcweir 		_addFile( xRootFolder, xFactory, xInput, aName );
163cdf0e10cSrcweir 	}
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
savePackage(const OUString & rPackageURL,const XMLFilterVector & rFilters)166cdf0e10cSrcweir bool XMLFilterJarHelper::savePackage( const OUString& rPackageURL, const XMLFilterVector& rFilters )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	try
169cdf0e10cSrcweir 	{
170cdf0e10cSrcweir 		osl::File::remove( rPackageURL );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 		// create the package jar file
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		Sequence< Any > aArguments( 2 );
175cdf0e10cSrcweir 		aArguments[ 0 ] <<= rPackageURL;
176cdf0e10cSrcweir 
177*2eda3cbfSmseidel 		// let ZipPackage be used ( no manifest.xml is required )
178*2eda3cbfSmseidel 		beans::NamedValue aArg;
179*2eda3cbfSmseidel 		aArg.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
180*2eda3cbfSmseidel 		aArg.Value <<= ZIP_STORAGE_FORMAT_STRING;
181*2eda3cbfSmseidel 		aArguments[ 1 ] <<= aArg;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		Reference< XHierarchicalNameAccess > xIfc(
184cdf0e10cSrcweir 			mxMSF->createInstanceWithArguments(
185cdf0e10cSrcweir 				rtl::OUString::createFromAscii(
186cdf0e10cSrcweir 								"com.sun.star.packages.comp.ZipPackage" ),
187cdf0e10cSrcweir 				aArguments ), UNO_QUERY );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 		if( xIfc.is() )
190cdf0e10cSrcweir 		{
191cdf0e10cSrcweir 			Reference< XSingleServiceFactory > xFactory( xIfc, UNO_QUERY );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 			// get root zip folder
194cdf0e10cSrcweir 			Reference< XInterface > xRootFolder;
195cdf0e10cSrcweir 			OUString szRootFolder( RTL_CONSTASCII_USTRINGPARAM("/") );
196cdf0e10cSrcweir 			xIfc->getByHierarchicalName( szRootFolder ) >>= xRootFolder;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 			// export filters files
199cdf0e10cSrcweir 			XMLFilterVector::const_iterator aIter( rFilters.begin() );
200cdf0e10cSrcweir 			while( aIter != rFilters.end() )
201cdf0e10cSrcweir 			{
202cdf0e10cSrcweir 				const filter_info_impl* pFilter = (*aIter);
203cdf0e10cSrcweir 
204*2eda3cbfSmseidel 				Reference< XInterface > xFilterRoot( addFolder( xRootFolder, xFactory, pFilter->maFilterName ) );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 				if( xFilterRoot.is() )
207cdf0e10cSrcweir 				{
208cdf0e10cSrcweir 					if( pFilter->maDTD.getLength() )
209cdf0e10cSrcweir 						addFile( xFilterRoot, xFactory, pFilter->maDTD );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 					if( pFilter->maExportXSLT.getLength() )
212cdf0e10cSrcweir 						addFile( xFilterRoot, xFactory, pFilter->maExportXSLT );
213cdf0e10cSrcweir 					try
214cdf0e10cSrcweir 					{
215cdf0e10cSrcweir 						if( pFilter->maImportXSLT.getLength() )
216cdf0e10cSrcweir 							addFile( xFilterRoot, xFactory, pFilter->maImportXSLT );
217cdf0e10cSrcweir 					}
218cdf0e10cSrcweir 					catch( com::sun::star::container::ElementExistException&)
219cdf0e10cSrcweir 					{
220*2eda3cbfSmseidel 					// in case of same named import / export XSLT the latter
221cdf0e10cSrcweir 					// is ignored
222cdf0e10cSrcweir 						DBG_ERROR( "XMLFilterJarHelper::same named xslt filter exception!" );
223cdf0e10cSrcweir 					}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 					if( pFilter->maImportTemplate.getLength() )
226cdf0e10cSrcweir 						addFile( xFilterRoot, xFactory, pFilter->maImportTemplate );
227cdf0e10cSrcweir 				}
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 				aIter++;
230cdf0e10cSrcweir 			}
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 			// create TypeDetection.xcu
233cdf0e10cSrcweir 			utl::TempFile aTempFile;
234cdf0e10cSrcweir 			aTempFile.EnableKillingFile();
235cdf0e10cSrcweir 			OUString aTempFileURL( aTempFile.GetURL() );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 			{
238cdf0e10cSrcweir 				osl::File aOutputFile( aTempFileURL );
239cdf0e10cSrcweir 				/* osl::File::RC rc = */ aOutputFile.open( OpenFlag_Write );
240cdf0e10cSrcweir 				Reference< XOutputStream > xOS( new OSLOutputStreamWrapper( aOutputFile ) );
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 				TypeDetectionExporter aExporter( mxMSF );
243cdf0e10cSrcweir 				aExporter.doExport(xOS,rFilters);
244cdf0e10cSrcweir 			}
245cdf0e10cSrcweir 
246*2eda3cbfSmseidel 			Reference< XInputStream > XIS( new utl::OSeekableInputStreamWrapper( new SvFileStream(aTempFileURL, STREAM_READ ), true ) );
247cdf0e10cSrcweir 			OUString szTypeDetection( RTL_CONSTASCII_USTRINGPARAM( "TypeDetection.xcu" ) );
248*2eda3cbfSmseidel 			_addFile( xRootFolder, xFactory, XIS, szTypeDetection );
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 			Reference< XChangesBatch > xBatch( xIfc, UNO_QUERY );
251cdf0e10cSrcweir 			if( xBatch.is() )
252cdf0e10cSrcweir 				xBatch->commitChanges();
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 			return true;
255cdf0e10cSrcweir 		}
256cdf0e10cSrcweir 	}
257cdf0e10cSrcweir 	catch( Exception& )
258cdf0e10cSrcweir 	{
259*2eda3cbfSmseidel 		DBG_ERROR( "XMLFilterJarHelper::savePackage exception caught!" );
260cdf0e10cSrcweir 	}
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	osl::File::remove( rPackageURL );
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 	return false;
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 
269cdf0e10cSrcweir //
270cdf0e10cSrcweir 
openPackage(const OUString & rPackageURL,XMLFilterVector & rFilters)271cdf0e10cSrcweir void XMLFilterJarHelper::openPackage( const OUString& rPackageURL, XMLFilterVector& rFilters )
272cdf0e10cSrcweir {
273cdf0e10cSrcweir 	try
274cdf0e10cSrcweir 	{
275cdf0e10cSrcweir 		// create the package jar file
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 		Sequence< Any > aArguments( 2 );
278cdf0e10cSrcweir 		aArguments[ 0 ] <<= rPackageURL;
279cdf0e10cSrcweir 
280*2eda3cbfSmseidel 		// let ZipPackage be used ( no manifest.xml is required )
281*2eda3cbfSmseidel 		beans::NamedValue aArg;
282*2eda3cbfSmseidel 		aArg.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
283*2eda3cbfSmseidel 		aArg.Value <<= ZIP_STORAGE_FORMAT_STRING;
284*2eda3cbfSmseidel 		aArguments[ 1 ] <<= aArg;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 		Reference< XHierarchicalNameAccess > xIfc(
287cdf0e10cSrcweir 			mxMSF->createInstanceWithArguments(
288cdf0e10cSrcweir 				rtl::OUString::createFromAscii(
289cdf0e10cSrcweir 								"com.sun.star.packages.comp.ZipPackage" ),
290cdf0e10cSrcweir 				aArguments ), UNO_QUERY );
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 		if( xIfc.is() )
293cdf0e10cSrcweir 		{
294cdf0e10cSrcweir 			Reference< XSingleServiceFactory > xFactory( xIfc, UNO_QUERY );
295cdf0e10cSrcweir 
296cdf0e10cSrcweir 			// get root zip folder
297cdf0e10cSrcweir 			Reference< XInterface > xRootFolder;
298cdf0e10cSrcweir 			OUString szRootFolder( RTL_CONSTASCII_USTRINGPARAM("/") );
299cdf0e10cSrcweir 			xIfc->getByHierarchicalName( szRootFolder ) >>= xRootFolder;
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 			OUString szTypeDetection( RTL_CONSTASCII_USTRINGPARAM("TypeDetection.xcu") );
302cdf0e10cSrcweir 			if( xIfc->hasByHierarchicalName( szTypeDetection ) )
303cdf0e10cSrcweir 			{
304cdf0e10cSrcweir 				Reference< XActiveDataSink > xTypeDetection;
305cdf0e10cSrcweir 				xIfc->getByHierarchicalName( szTypeDetection ) >>= xTypeDetection;
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 				if( xTypeDetection.is() )
308cdf0e10cSrcweir 				{
309cdf0e10cSrcweir 					Reference< XInputStream > xIS( xTypeDetection->getInputStream() );
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 					XMLFilterVector aFilters;
312cdf0e10cSrcweir 					TypeDetectionImporter::doImport( mxMSF, xIS, aFilters );
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 					// copy all files used by the filters imported from the
315cdf0e10cSrcweir 					// typedetection to office/user/xslt
316cdf0e10cSrcweir 					XMLFilterVector::iterator aIter( aFilters.begin() );
317cdf0e10cSrcweir 					while( aIter != aFilters.end() )
318cdf0e10cSrcweir 					{
319cdf0e10cSrcweir 						if( copyFiles( xIfc, (*aIter) ) )
320cdf0e10cSrcweir 						{
321cdf0e10cSrcweir 							rFilters.push_back( (*aIter) );
322cdf0e10cSrcweir 						}
323cdf0e10cSrcweir 						else
324cdf0e10cSrcweir 						{
325cdf0e10cSrcweir 							// failed to copy all files
326cdf0e10cSrcweir 							delete (*aIter);
327cdf0e10cSrcweir 						}
328cdf0e10cSrcweir 						aIter++;
329cdf0e10cSrcweir 					}
330cdf0e10cSrcweir 				}
331cdf0e10cSrcweir 			}
332cdf0e10cSrcweir 		}
333cdf0e10cSrcweir 	}
334cdf0e10cSrcweir 	catch( Exception& )
335cdf0e10cSrcweir 	{
336*2eda3cbfSmseidel 		DBG_ERROR( "XMLFilterJarHelper::savePackage exception caught!" );
337cdf0e10cSrcweir 	}
338cdf0e10cSrcweir }
339cdf0e10cSrcweir 
copyFiles(Reference<XHierarchicalNameAccess> xIfc,filter_info_impl * pFilter)340cdf0e10cSrcweir bool XMLFilterJarHelper::copyFiles( Reference< XHierarchicalNameAccess > xIfc, filter_info_impl* pFilter )
341cdf0e10cSrcweir {
342cdf0e10cSrcweir 	bool bOk = copyFile( xIfc, pFilter->maDTD, sDTDPath );
343cdf0e10cSrcweir 
344cdf0e10cSrcweir 	if( bOk )
345cdf0e10cSrcweir 		bOk = copyFile( xIfc, pFilter->maExportXSLT, sXSLTPath );
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	if( bOk )
348cdf0e10cSrcweir 		bOk = copyFile( xIfc, pFilter->maImportXSLT, sXSLTPath );
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 	if( bOk )
351cdf0e10cSrcweir 		bOk = copyFile( xIfc, pFilter->maImportTemplate, sTemplatePath );
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 	return bOk;
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
copyFile(Reference<XHierarchicalNameAccess> xIfc,OUString & rURL,const OUString & rTargetURL)356cdf0e10cSrcweir bool XMLFilterJarHelper::copyFile( Reference< XHierarchicalNameAccess > xIfc, OUString& rURL, const OUString& rTargetURL )
357cdf0e10cSrcweir {
358cdf0e10cSrcweir 	if( !rURL.matchIgnoreAsciiCase( sVndSunStarPackage ) )
359cdf0e10cSrcweir 		return true;
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 	try
362cdf0e10cSrcweir 	{
363cdf0e10cSrcweir 		OUString szPackagePath( encodeZipUri( rURL.copy( sVndSunStarPackage.getLength() ) ) );
364cdf0e10cSrcweir 
365*2eda3cbfSmseidel 		if ( ::comphelper::OStorageHelper::PathHasSegment( szPackagePath, OUString( RTL_CONSTASCII_USTRINGPARAM( ".." ) ) )
366*2eda3cbfSmseidel 		  || ::comphelper::OStorageHelper::PathHasSegment( szPackagePath, OUString( RTL_CONSTASCII_USTRINGPARAM( "." ) ) ) )
367*2eda3cbfSmseidel 			throw lang::IllegalArgumentException();
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 		if( xIfc->hasByHierarchicalName( szPackagePath ) )
370cdf0e10cSrcweir 		{
371cdf0e10cSrcweir 			Reference< XActiveDataSink > xFileEntry;
372cdf0e10cSrcweir 			xIfc->getByHierarchicalName( szPackagePath ) >>= xFileEntry;
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 			if( xFileEntry.is() )
375cdf0e10cSrcweir 			{
376cdf0e10cSrcweir 				Reference< XInputStream > xIS( xFileEntry->getInputStream() );
377*2eda3cbfSmseidel 
378cdf0e10cSrcweir 				INetURLObject aBaseURL( rTargetURL );
379cdf0e10cSrcweir 
380cdf0e10cSrcweir 				rURL = URIHelper::SmartRel2Abs( aBaseURL, szPackagePath, Link(), false );
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 				if( rURL.getLength() )
383cdf0e10cSrcweir 				{
384cdf0e10cSrcweir 					// create output directory if needed
385cdf0e10cSrcweir 					if( !createDirectory( rURL ) )
386cdf0e10cSrcweir 						return false;
387cdf0e10cSrcweir 
388*2eda3cbfSmseidel 					::osl::File file(rURL);
389*2eda3cbfSmseidel 					::osl::FileBase::RC rc =
390*2eda3cbfSmseidel 						file.open(OpenFlag_Write|OpenFlag_Create);
391*2eda3cbfSmseidel 					if (::osl::FileBase::E_EXIST == rc) {
392*2eda3cbfSmseidel 						rc = file.open(OpenFlag_Write);
393*2eda3cbfSmseidel 						if (::osl::FileBase::E_None == rc) {
394*2eda3cbfSmseidel 							file.setSize(0); // #i97170# truncate
395*2eda3cbfSmseidel 						}
396*2eda3cbfSmseidel 					}
397*2eda3cbfSmseidel 					if (::osl::FileBase::E_None != rc) {
398*2eda3cbfSmseidel 						throw RuntimeException();
399*2eda3cbfSmseidel 					}
400*2eda3cbfSmseidel 					Reference< XOutputStream > const xOS(
401*2eda3cbfSmseidel 							new comphelper::OSLOutputStreamWrapper(file));
402cdf0e10cSrcweir 
403cdf0e10cSrcweir 					return copyStreams( xIS, xOS );
404cdf0e10cSrcweir 				}
405cdf0e10cSrcweir 			}
406cdf0e10cSrcweir 		}
407cdf0e10cSrcweir 	}
408cdf0e10cSrcweir 	catch( Exception& )
409cdf0e10cSrcweir 	{
410*2eda3cbfSmseidel 		DBG_ERROR( "XMLFilterJarHelper::copyFile exception caught" );
411cdf0e10cSrcweir 	}
412cdf0e10cSrcweir 	return false;
413cdf0e10cSrcweir }
414