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