update dev300-m58
[ooovba.git] / filter / source / xsltdialog / xmlfilterjar.cxx
blobdd52522c6c15db757280d437cc3940d287d30806
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlfilterjar.cxx,v $
10 * $Revision: 1.9 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_filter.hxx"
33 #include <com/sun/star/io/XActiveDataControl.hpp>
34 #include <com/sun/star/io/XActiveDataSource.hpp>
35 #include <com/sun/star/frame/XConfigManager.hpp>
36 #include <com/sun/star/io/XInputStream.hpp>
37 #include <com/sun/star/io/XActiveDataSink.hpp>
38 #ifndef _COM_SUN_STAR_BEANS_NAMEDVALUE_HPP_
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 #endif
41 #include <com/sun/star/container/XNamed.hpp>
42 #include <com/sun/star/container/XChild.hpp>
43 #include <com/sun/star/util/XChangesBatch.hpp>
46 #include <comphelper/oslfile2streamwrap.hxx>
47 #include <unotools/streamwrap.hxx>
48 #include <tools/stream.hxx>
49 #include <tools/urlobj.hxx>
50 #include <unotools/tempfile.hxx>
51 #include <svtools/urihelper.hxx>
52 #include <osl/file.hxx>
54 #include <rtl/uri.hxx>
56 #include "xmlfilterjar.hxx"
57 #include "xmlfilterdialogstrings.hrc"
58 #include "xmlfiltersettingsdialog.hxx"
59 #include "typedetectionexport.hxx"
60 #include "typedetectionimport.hxx"
62 using namespace rtl;
63 using namespace osl;
64 using namespace comphelper;
65 using namespace com::sun::star::lang;
66 using namespace com::sun::star::frame;
67 using namespace com::sun::star::uno;
68 using namespace com::sun::star::util;
69 using namespace com::sun::star::container;
70 using namespace com::sun::star::beans;
71 using namespace com::sun::star::io;
73 XMLFilterJarHelper::XMLFilterJarHelper( Reference< XMultiServiceFactory >& xMSF )
74 : mxMSF( xMSF ),
75 sVndSunStarPackage( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.Package:" ) ),
76 sXSLTPath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/xslt/" ) ),
77 sDTDPath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/dtd/" ) ),
78 sTemplatePath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/template/") ),
79 sSpecialConfigManager( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.config.SpecialConfigManager" ) ),
80 sPump( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.Pump" ) ),
81 sProgPath( RTL_CONSTASCII_USTRINGPARAM( "$(prog)/" ) )
83 try
85 Reference< XConfigManager > xCfgMgr( xMSF->createInstance(OUString::createFromAscii("com.sun.star.config.SpecialConfigManager")), UNO_QUERY );
86 if( xCfgMgr.is() )
88 sProgPath = xCfgMgr->substituteVariables( sProgPath );
89 sXSLTPath = xCfgMgr->substituteVariables( sXSLTPath );
90 sDTDPath = xCfgMgr->substituteVariables( sDTDPath );
91 sTemplatePath = xCfgMgr->substituteVariables( sTemplatePath );
94 catch(Exception&)
99 static OUString encodeZipUri( const OUString& rURI )
101 return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 );
104 static Reference< XInterface > addFolder( Reference< XInterface >& xRootFolder, Reference< XSingleServiceFactory >& xFactory, const OUString& rName ) throw( Exception )
106 Sequence< Any > aArgs(1);
107 aArgs[0] <<= (sal_Bool)sal_True;
109 Reference< XInterface > xFolder( xFactory->createInstanceWithArguments(aArgs) );
110 Reference< XNamed > xNamed( xFolder, UNO_QUERY );
111 Reference< XChild > xChild( xFolder, UNO_QUERY );
113 if( xNamed.is() && xChild.is() )
115 OUString aName( encodeZipUri( rName ) );
116 xNamed->setName( aName );
117 xChild->setParent( xRootFolder );
120 return xFolder;
123 static void _addFile( Reference< XInterface >& xRootFolder, Reference< XSingleServiceFactory >& xFactory, Reference< XInputStream >& xInput, OUString aName ) throw( Exception )
126 Reference< XActiveDataSink > xSink( xFactory->createInstance(), UNO_QUERY );
127 Reference< XUnoTunnel > xTunnel( xSink, UNO_QUERY );
128 if( xSink.is() && xTunnel.is())
130 Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY );
131 xNameContainer->insertByName(aName = encodeZipUri( aName ), makeAny(xTunnel));
132 xSink->setInputStream( xInput );
137 static void addFile( Reference< XInterface > xRootFolder, Reference< XSingleServiceFactory > xFactory, const OUString& rSourceFile, const OUString& rName ) throw( Exception )
139 Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( new SvFileStream(rSourceFile, STREAM_READ ), true ) );
140 _addFile( xRootFolder, xFactory, xInput, rName );
144 void XMLFilterJarHelper::addFile( Reference< XInterface > xRootFolder, Reference< XSingleServiceFactory > xFactory, const OUString& rSourceFile ) throw( Exception )
146 if( rSourceFile.getLength() &&
147 (rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("http:") ) != 0) &&
148 (rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("shttp:") ) != 0) &&
149 (rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("jar:") ) != 0) &&
150 (rSourceFile.compareToAscii( RTL_CONSTASCII_STRINGPARAM("ftp:") ) != 0))
152 OUString aFileURL( rSourceFile );
154 if( !aFileURL.matchIgnoreAsciiCase( OUString( RTL_CONSTASCII_USTRINGPARAM("file://") ) ) )
156 aFileURL = URIHelper::SmartRel2Abs( sProgPath, aFileURL, Link(), false );
159 INetURLObject aURL( aFileURL );
160 OUString aName( aURL.getName() );
162 SvFileStream* pStream = new SvFileStream(aFileURL, STREAM_READ );
163 Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( pStream, true ) );
164 _addFile( xRootFolder, xFactory, xInput, aName );
168 bool XMLFilterJarHelper::savePackage( const OUString& rPackageURL, const XMLFilterVector& rFilters )
172 osl::File::remove( rPackageURL );
174 // create the package jar file
176 Sequence< Any > aArguments( 1 );
177 aArguments[ 0 ] <<= rPackageURL;
179 Reference< XHierarchicalNameAccess > xIfc(
180 mxMSF->createInstanceWithArguments(
181 rtl::OUString::createFromAscii(
182 "com.sun.star.packages.comp.ZipPackage" ),
183 aArguments ), UNO_QUERY );
185 if( xIfc.is() )
187 Reference< XSingleServiceFactory > xFactory( xIfc, UNO_QUERY );
189 // get root zip folder
190 Reference< XInterface > xRootFolder;
191 OUString szRootFolder( RTL_CONSTASCII_USTRINGPARAM("/") );
192 xIfc->getByHierarchicalName( szRootFolder ) >>= xRootFolder;
194 // export filters files
195 XMLFilterVector::const_iterator aIter( rFilters.begin() );
196 while( aIter != rFilters.end() )
198 const filter_info_impl* pFilter = (*aIter);
200 Reference< XInterface > xFilterRoot( addFolder( xRootFolder, xFactory, pFilter->maFilterName ) );
202 if( xFilterRoot.is() )
204 if( pFilter->maDTD.getLength() )
205 addFile( xFilterRoot, xFactory, pFilter->maDTD );
207 if( pFilter->maExportXSLT.getLength() )
208 addFile( xFilterRoot, xFactory, pFilter->maExportXSLT );
211 if( pFilter->maImportXSLT.getLength() )
212 addFile( xFilterRoot, xFactory, pFilter->maImportXSLT );
214 catch( com::sun::star::container::ElementExistException&)
216 // in case of same named import / export XSLT the latter
217 // is ignored
218 DBG_ERROR( "XMLFilterJarHelper::same named xslt filter exception!" );
221 if( pFilter->maImportTemplate.getLength() )
222 addFile( xFilterRoot, xFactory, pFilter->maImportTemplate );
225 aIter++;
228 // create TypeDetection.xcu
229 utl::TempFile aTempFile;
230 aTempFile.EnableKillingFile();
231 OUString aTempFileURL( aTempFile.GetURL() );
234 osl::File aOutputFile( aTempFileURL );
235 /* osl::File::RC rc = */ aOutputFile.open( OpenFlag_Write );
236 Reference< XOutputStream > xOS( new OSLOutputStreamWrapper( aOutputFile ) );
238 TypeDetectionExporter aExporter( mxMSF );
239 aExporter.doExport(xOS,rFilters);
242 Reference< XInputStream > XIS( new utl::OSeekableInputStreamWrapper( new SvFileStream(aTempFileURL, STREAM_READ ), true ) );
243 OUString szTypeDetection( RTL_CONSTASCII_USTRINGPARAM( "TypeDetection.xcu" ) );
244 _addFile( xRootFolder, xFactory, XIS, szTypeDetection );
246 Reference< XChangesBatch > xBatch( xIfc, UNO_QUERY );
247 if( xBatch.is() )
248 xBatch->commitChanges();
250 return true;
253 catch( Exception& )
255 DBG_ERROR( "XMLFilterJarHelper::savePackage exception catched!" );
258 osl::File::remove( rPackageURL );
260 return false;
267 void XMLFilterJarHelper::openPackage( const OUString& rPackageURL, XMLFilterVector& rFilters )
271 // create the package jar file
273 Sequence< Any > aArguments( 1 );
274 aArguments[ 0 ] <<= rPackageURL;
276 Reference< XHierarchicalNameAccess > xIfc(
277 mxMSF->createInstanceWithArguments(
278 rtl::OUString::createFromAscii(
279 "com.sun.star.packages.comp.ZipPackage" ),
280 aArguments ), UNO_QUERY );
282 if( xIfc.is() )
284 Reference< XSingleServiceFactory > xFactory( xIfc, UNO_QUERY );
286 // get root zip folder
287 Reference< XInterface > xRootFolder;
288 OUString szRootFolder( RTL_CONSTASCII_USTRINGPARAM("/") );
289 xIfc->getByHierarchicalName( szRootFolder ) >>= xRootFolder;
291 OUString szTypeDetection( RTL_CONSTASCII_USTRINGPARAM("TypeDetection.xcu") );
292 if( xIfc->hasByHierarchicalName( szTypeDetection ) )
294 Reference< XActiveDataSink > xTypeDetection;
295 xIfc->getByHierarchicalName( szTypeDetection ) >>= xTypeDetection;
297 if( xTypeDetection.is() )
299 Reference< XInputStream > xIS( xTypeDetection->getInputStream() );
301 XMLFilterVector aFilters;
302 TypeDetectionImporter::doImport( mxMSF, xIS, aFilters );
304 // copy all files used by the filters imported from the
305 // typedetection to office/user/xslt
306 XMLFilterVector::iterator aIter( aFilters.begin() );
307 while( aIter != aFilters.end() )
309 if( copyFiles( xIfc, (*aIter) ) )
311 rFilters.push_back( (*aIter) );
313 else
315 // failed to copy all files
316 delete (*aIter);
318 aIter++;
324 catch( Exception& )
326 DBG_ERROR( "XMLFilterJarHelper::savePackage exception catched!" );
330 bool XMLFilterJarHelper::copyFiles( Reference< XHierarchicalNameAccess > xIfc, filter_info_impl* pFilter )
332 bool bOk = copyFile( xIfc, pFilter->maDTD, sDTDPath );
334 if( bOk )
335 bOk = copyFile( xIfc, pFilter->maExportXSLT, sXSLTPath );
337 if( bOk )
338 bOk = copyFile( xIfc, pFilter->maImportXSLT, sXSLTPath );
340 if( bOk )
341 bOk = copyFile( xIfc, pFilter->maImportTemplate, sTemplatePath );
343 return bOk;
346 bool XMLFilterJarHelper::copyFile( Reference< XHierarchicalNameAccess > xIfc, OUString& rURL, const OUString& rTargetURL )
348 if( !rURL.matchIgnoreAsciiCase( sVndSunStarPackage ) )
349 return true;
353 OUString szPackagePath( encodeZipUri( rURL.copy( sVndSunStarPackage.getLength() ) ) );
355 if( xIfc->hasByHierarchicalName( szPackagePath ) )
357 Reference< XActiveDataSink > xFileEntry;
358 xIfc->getByHierarchicalName( szPackagePath ) >>= xFileEntry;
360 if( xFileEntry.is() )
362 Reference< XInputStream > xIS( xFileEntry->getInputStream() );
364 INetURLObject aBaseURL( rTargetURL );
366 rURL = URIHelper::SmartRel2Abs( aBaseURL, szPackagePath, Link(), false );
368 if( rURL.getLength() )
370 // create output directory if needed
371 if( !createDirectory( rURL ) )
372 return false;
374 SvFileStream aOutputStream(rURL, STREAM_WRITE );
375 Reference< XOutputStream > xOS( new utl::OOutputStreamWrapper( aOutputStream ) );
377 return copyStreams( xIS, xOS );
382 catch( Exception& )
384 DBG_ERROR( "XMLFilterJarHelper::copyFile exception catched" );
386 return false;