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: xmlfilterjar.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 // 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 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/beans/NamedValue.hpp>
40 #include <com/sun/star/container/XNamed.hpp>
41 #include <com/sun/star/container/XChild.hpp>
42 #include <com/sun/star/util/XChangesBatch.hpp>
45 #include <comphelper/oslfile2streamwrap.hxx>
46 #include <comphelper/storagehelper.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"
64 using namespace comphelper
;
65 using namespace com::sun::star
;
66 using namespace com::sun::star::lang
;
67 using namespace com::sun::star::frame
;
68 using namespace com::sun::star::uno
;
69 using namespace com::sun::star::util
;
70 using namespace com::sun::star::container
;
71 using namespace com::sun::star::beans
;
72 using namespace com::sun::star::io
;
74 XMLFilterJarHelper::XMLFilterJarHelper( Reference
< XMultiServiceFactory
>& xMSF
)
76 sVndSunStarPackage( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.Package:" ) ),
77 sXSLTPath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/xslt/" ) ),
78 sDTDPath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/dtd/" ) ),
79 sTemplatePath( RTL_CONSTASCII_USTRINGPARAM( "$(user)/template/") ),
80 sSpecialConfigManager( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.config.SpecialConfigManager" ) ),
81 sPump( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.Pump" ) ),
82 sProgPath( RTL_CONSTASCII_USTRINGPARAM( "$(prog)/" ) )
86 Reference
< XConfigManager
> xCfgMgr( xMSF
->createInstance(OUString::createFromAscii("com.sun.star.config.SpecialConfigManager")), UNO_QUERY
);
89 sProgPath
= xCfgMgr
->substituteVariables( sProgPath
);
90 sXSLTPath
= xCfgMgr
->substituteVariables( sXSLTPath
);
91 sDTDPath
= xCfgMgr
->substituteVariables( sDTDPath
);
92 sTemplatePath
= xCfgMgr
->substituteVariables( sTemplatePath
);
100 static OUString
encodeZipUri( const OUString
& rURI
)
102 return Uri::encode( rURI
, rtl_UriCharClassUric
, rtl_UriEncodeCheckEscapes
, RTL_TEXTENCODING_UTF8
);
105 static Reference
< XInterface
> addFolder( Reference
< XInterface
>& xRootFolder
, Reference
< XSingleServiceFactory
>& xFactory
, const OUString
& rName
) throw( Exception
)
107 Sequence
< Any
> aArgs(1);
108 aArgs
[0] <<= (sal_Bool
)sal_True
;
110 Reference
< XInterface
> xFolder( xFactory
->createInstanceWithArguments(aArgs
) );
111 Reference
< XNamed
> xNamed( xFolder
, UNO_QUERY
);
112 Reference
< XChild
> xChild( xFolder
, UNO_QUERY
);
114 if( xNamed
.is() && xChild
.is() )
116 OUString
aName( encodeZipUri( rName
) );
117 xNamed
->setName( aName
);
118 xChild
->setParent( xRootFolder
);
124 static void _addFile( Reference
< XInterface
>& xRootFolder
, Reference
< XSingleServiceFactory
>& xFactory
, Reference
< XInputStream
>& xInput
, OUString aName
) throw( Exception
)
127 Reference
< XActiveDataSink
> xSink( xFactory
->createInstance(), UNO_QUERY
);
128 Reference
< XUnoTunnel
> xTunnel( xSink
, UNO_QUERY
);
129 if( xSink
.is() && xTunnel
.is())
131 Reference
< XNameContainer
> xNameContainer(xRootFolder
, UNO_QUERY
);
132 xNameContainer
->insertByName(aName
= encodeZipUri( aName
), makeAny(xTunnel
));
133 xSink
->setInputStream( xInput
);
138 static void addFile( Reference< XInterface > xRootFolder, Reference< XSingleServiceFactory > xFactory, const OUString& rSourceFile, const OUString& rName ) throw( Exception )
140 Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( new SvFileStream(rSourceFile, STREAM_READ ), true ) );
141 _addFile( xRootFolder, xFactory, xInput, rName );
145 void XMLFilterJarHelper::addFile( Reference
< XInterface
> xRootFolder
, Reference
< XSingleServiceFactory
> xFactory
, const OUString
& rSourceFile
) throw( Exception
)
147 if( rSourceFile
.getLength() &&
148 (rSourceFile
.compareToAscii( RTL_CONSTASCII_STRINGPARAM("http:") ) != 0) &&
149 (rSourceFile
.compareToAscii( RTL_CONSTASCII_STRINGPARAM("shttp:") ) != 0) &&
150 (rSourceFile
.compareToAscii( RTL_CONSTASCII_STRINGPARAM("jar:") ) != 0) &&
151 (rSourceFile
.compareToAscii( RTL_CONSTASCII_STRINGPARAM("ftp:") ) != 0))
153 OUString
aFileURL( rSourceFile
);
155 if( !aFileURL
.matchIgnoreAsciiCase( OUString( RTL_CONSTASCII_USTRINGPARAM("file://") ) ) )
157 aFileURL
= URIHelper::SmartRel2Abs( sProgPath
, aFileURL
, Link(), false );
160 INetURLObject
aURL( aFileURL
);
161 OUString
aName( aURL
.getName() );
163 SvFileStream
* pStream
= new SvFileStream(aFileURL
, STREAM_READ
);
164 Reference
< XInputStream
> xInput( new utl::OSeekableInputStreamWrapper( pStream
, true ) );
165 _addFile( xRootFolder
, xFactory
, xInput
, aName
);
169 bool XMLFilterJarHelper::savePackage( const OUString
& rPackageURL
, const XMLFilterVector
& rFilters
)
173 osl::File::remove( rPackageURL
);
175 // create the package jar file
177 Sequence
< Any
> aArguments( 2 );
178 aArguments
[ 0 ] <<= rPackageURL
;
180 // let ZipPackage be used ( no manifest.xml is required )
181 beans::NamedValue aArg
;
182 aArg
.Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
183 aArg
.Value
<<= ZIP_STORAGE_FORMAT_STRING
;
184 aArguments
[ 1 ] <<= aArg
;
186 Reference
< XHierarchicalNameAccess
> xIfc(
187 mxMSF
->createInstanceWithArguments(
188 rtl::OUString::createFromAscii(
189 "com.sun.star.packages.comp.ZipPackage" ),
190 aArguments
), UNO_QUERY
);
194 Reference
< XSingleServiceFactory
> xFactory( xIfc
, UNO_QUERY
);
196 // get root zip folder
197 Reference
< XInterface
> xRootFolder
;
198 OUString
szRootFolder( RTL_CONSTASCII_USTRINGPARAM("/") );
199 xIfc
->getByHierarchicalName( szRootFolder
) >>= xRootFolder
;
201 // export filters files
202 XMLFilterVector::const_iterator
aIter( rFilters
.begin() );
203 while( aIter
!= rFilters
.end() )
205 const filter_info_impl
* pFilter
= (*aIter
);
207 Reference
< XInterface
> xFilterRoot( addFolder( xRootFolder
, xFactory
, pFilter
->maFilterName
) );
209 if( xFilterRoot
.is() )
211 if( pFilter
->maDTD
.getLength() )
212 addFile( xFilterRoot
, xFactory
, pFilter
->maDTD
);
214 if( pFilter
->maExportXSLT
.getLength() )
215 addFile( xFilterRoot
, xFactory
, pFilter
->maExportXSLT
);
218 if( pFilter
->maImportXSLT
.getLength() )
219 addFile( xFilterRoot
, xFactory
, pFilter
->maImportXSLT
);
221 catch( com::sun::star::container::ElementExistException
&)
223 // in case of same named import / export XSLT the latter
225 DBG_ERROR( "XMLFilterJarHelper::same named xslt filter exception!" );
228 if( pFilter
->maImportTemplate
.getLength() )
229 addFile( xFilterRoot
, xFactory
, pFilter
->maImportTemplate
);
235 // create TypeDetection.xcu
236 utl::TempFile aTempFile
;
237 aTempFile
.EnableKillingFile();
238 OUString
aTempFileURL( aTempFile
.GetURL() );
241 osl::File
aOutputFile( aTempFileURL
);
242 /* osl::File::RC rc = */ aOutputFile
.open( OpenFlag_Write
);
243 Reference
< XOutputStream
> xOS( new OSLOutputStreamWrapper( aOutputFile
) );
245 TypeDetectionExporter
aExporter( mxMSF
);
246 aExporter
.doExport(xOS
,rFilters
);
249 Reference
< XInputStream
> XIS( new utl::OSeekableInputStreamWrapper( new SvFileStream(aTempFileURL
, STREAM_READ
), true ) );
250 OUString
szTypeDetection( RTL_CONSTASCII_USTRINGPARAM( "TypeDetection.xcu" ) );
251 _addFile( xRootFolder
, xFactory
, XIS
, szTypeDetection
);
253 Reference
< XChangesBatch
> xBatch( xIfc
, UNO_QUERY
);
255 xBatch
->commitChanges();
262 DBG_ERROR( "XMLFilterJarHelper::savePackage exception catched!" );
265 osl::File::remove( rPackageURL
);
274 void XMLFilterJarHelper::openPackage( const OUString
& rPackageURL
, XMLFilterVector
& rFilters
)
278 // create the package jar file
280 Sequence
< Any
> aArguments( 2 );
281 aArguments
[ 0 ] <<= rPackageURL
;
283 // let ZipPackage be used ( no manifest.xml is required )
284 beans::NamedValue aArg
;
285 aArg
.Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
286 aArg
.Value
<<= ZIP_STORAGE_FORMAT_STRING
;
287 aArguments
[ 1 ] <<= aArg
;
289 Reference
< XHierarchicalNameAccess
> xIfc(
290 mxMSF
->createInstanceWithArguments(
291 rtl::OUString::createFromAscii(
292 "com.sun.star.packages.comp.ZipPackage" ),
293 aArguments
), UNO_QUERY
);
297 Reference
< XSingleServiceFactory
> xFactory( xIfc
, UNO_QUERY
);
299 // get root zip folder
300 Reference
< XInterface
> xRootFolder
;
301 OUString
szRootFolder( RTL_CONSTASCII_USTRINGPARAM("/") );
302 xIfc
->getByHierarchicalName( szRootFolder
) >>= xRootFolder
;
304 OUString
szTypeDetection( RTL_CONSTASCII_USTRINGPARAM("TypeDetection.xcu") );
305 if( xIfc
->hasByHierarchicalName( szTypeDetection
) )
307 Reference
< XActiveDataSink
> xTypeDetection
;
308 xIfc
->getByHierarchicalName( szTypeDetection
) >>= xTypeDetection
;
310 if( xTypeDetection
.is() )
312 Reference
< XInputStream
> xIS( xTypeDetection
->getInputStream() );
314 XMLFilterVector aFilters
;
315 TypeDetectionImporter::doImport( mxMSF
, xIS
, aFilters
);
317 // copy all files used by the filters imported from the
318 // typedetection to office/user/xslt
319 XMLFilterVector::iterator
aIter( aFilters
.begin() );
320 while( aIter
!= aFilters
.end() )
322 if( copyFiles( xIfc
, (*aIter
) ) )
324 rFilters
.push_back( (*aIter
) );
328 // failed to copy all files
339 DBG_ERROR( "XMLFilterJarHelper::savePackage exception catched!" );
343 bool XMLFilterJarHelper::copyFiles( Reference
< XHierarchicalNameAccess
> xIfc
, filter_info_impl
* pFilter
)
345 bool bOk
= copyFile( xIfc
, pFilter
->maDTD
, sDTDPath
);
348 bOk
= copyFile( xIfc
, pFilter
->maExportXSLT
, sXSLTPath
);
351 bOk
= copyFile( xIfc
, pFilter
->maImportXSLT
, sXSLTPath
);
354 bOk
= copyFile( xIfc
, pFilter
->maImportTemplate
, sTemplatePath
);
359 bool XMLFilterJarHelper::copyFile( Reference
< XHierarchicalNameAccess
> xIfc
, OUString
& rURL
, const OUString
& rTargetURL
)
361 if( !rURL
.matchIgnoreAsciiCase( sVndSunStarPackage
) )
366 OUString
szPackagePath( encodeZipUri( rURL
.copy( sVndSunStarPackage
.getLength() ) ) );
368 if( xIfc
->hasByHierarchicalName( szPackagePath
) )
370 Reference
< XActiveDataSink
> xFileEntry
;
371 xIfc
->getByHierarchicalName( szPackagePath
) >>= xFileEntry
;
373 if( xFileEntry
.is() )
375 Reference
< XInputStream
> xIS( xFileEntry
->getInputStream() );
377 INetURLObject
aBaseURL( rTargetURL
);
379 rURL
= URIHelper::SmartRel2Abs( aBaseURL
, szPackagePath
, Link(), false );
381 if( rURL
.getLength() )
383 // create output directory if needed
384 if( !createDirectory( rURL
) )
387 SvFileStream
aOutputStream(rURL
, STREAM_WRITE
);
388 Reference
< XOutputStream
> xOS( new utl::OOutputStreamWrapper( aOutputStream
) );
390 return copyStreams( xIS
, xOS
);
397 DBG_ERROR( "XMLFilterJarHelper::copyFile exception catched" );