1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/document/XFilter.hpp>
23 #include <com/sun/star/document/XExporter.hpp>
24 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
25 #include <com/sun/star/document/XGraphicStorageHandler.hpp>
26 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
27 #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
28 #include <com/sun/star/frame/Desktop.hpp>
29 #include <com/sun/star/frame/XStorable.hpp>
30 #include <com/sun/star/io/XActiveDataSource.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/system/SystemShellExecute.hpp>
33 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
34 #include <com/sun/star/task/InteractionHandler.hpp>
35 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
36 #include <com/sun/star/xml/XImportFilter.hpp>
37 #include <com/sun/star/xml/XExportFilter.hpp>
38 #include <com/sun/star/xml/sax/Writer.hpp>
39 
40 #include <comphelper/oslfile2streamwrap.hxx>
41 #include <vcl/svapp.hxx>
42 #include <sfx2/filedlghelper.hxx>
43 #include <osl/file.hxx>
44 #include <unotools/tempfile.hxx>
45 #include <tools/debug.hxx>
46 #include <tools/urlobj.hxx>
47 #include <comphelper/processfactory.hxx>
48 
49 #include <strings.hrc>
50 #include "xmlfiltersettingsdialog.hxx"
51 #include "xmlfiltertestdialog.hxx"
52 
53 
54 using namespace utl;
55 using namespace osl;
56 using namespace comphelper;
57 using namespace com::sun::star::lang;
58 using namespace com::sun::star::beans;
59 using namespace com::sun::star::container;
60 using namespace com::sun::star::document;
61 using namespace com::sun::star::frame;
62 using namespace com::sun::star::task;
63 using namespace com::sun::star::uno;
64 using namespace com::sun::star::io;
65 using namespace com::sun::star::system;
66 using namespace com::sun::star::xml;
67 using namespace com::sun::star::xml::sax;
68 
69 
70 class GlobalEventListenerImpl : public ::cppu::WeakImplHelper< css::document::XDocumentEventListener >
71 {
72 public:
73     explicit GlobalEventListenerImpl( XMLFilterTestDialog* pDialog );
74 
75     // XDocumentEventListener
76     virtual void SAL_CALL documentEventOccured( const css::document::DocumentEvent& Event ) override;
77 
78     // lang::XEventListener
79     virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
80 private:
81     XMLFilterTestDialog* mpDialog;
82 };
83 
GlobalEventListenerImpl(XMLFilterTestDialog * pDialog)84 GlobalEventListenerImpl::GlobalEventListenerImpl( XMLFilterTestDialog* pDialog )
85 : mpDialog( pDialog )
86 {
87 }
88 
documentEventOccured(const css::document::DocumentEvent & Event)89 void SAL_CALL GlobalEventListenerImpl::documentEventOccured( const css::document::DocumentEvent& Event )
90 {
91     ::SolarMutexGuard aGuard;
92     if( Event.EventName == "OnFocus" || Event.EventName == "OnUnload" )
93     {
94         Reference< XComponent > xComp( Event.Source, UNO_QUERY );
95         mpDialog->updateCurrentDocumentButtonState( &xComp );
96     }
97 }
98 
disposing(const css::lang::EventObject &)99 void SAL_CALL GlobalEventListenerImpl::disposing( const css::lang::EventObject& /* Source */ )
100 {
101 }
102 
103 /** returns true if the given component supports the given service */
checkComponent(Reference<XComponent> const & rxComponent,const OUString & rServiceName)104 static bool checkComponent( Reference< XComponent > const & rxComponent, const OUString& rServiceName )
105 {
106     try
107     {
108         Reference< XServiceInfo > xInfo( rxComponent, UNO_QUERY );
109         if( xInfo.is() )
110         {
111             if( xInfo->supportsService( rServiceName ) )
112             {
113                 // special case for impress documents which supports same service as draw documents
114                 if ( rServiceName == "com.sun.star.drawing.DrawingDocument" )
115                 {
116                     // so if we want a draw we need to check if it's not an impress
117                     if( !xInfo->supportsService("com.sun.star.presentation.PresentationDocument") )
118                         return true;
119                 }
120                 else
121                 {
122                     return true;
123                 }
124             }
125         }
126     }
127     catch( const Exception& )
128     {
129         OSL_FAIL( "checkComponent exception caught!" );
130     }
131 
132     return false;
133 }
134 
XMLFilterTestDialog(weld::Window * pParent,const Reference<XComponentContext> & rxContext)135 XMLFilterTestDialog::XMLFilterTestDialog(weld::Window* pParent,
136     const Reference<XComponentContext>& rxContext)
137     : GenericDialogController(pParent, "filter/ui/testxmlfilter.ui", "TestXMLFilterDialog")
138     , mxContext(rxContext)
139     , m_xExport(m_xBuilder->weld_widget("export"))
140     , m_xFTExportXSLTFile(m_xBuilder->weld_label("exportxsltfile"))
141     , m_xPBExportBrowse(m_xBuilder->weld_button("exportbrowse"))
142     , m_xPBCurrentDocument(m_xBuilder->weld_button("currentdocument"))
143     , m_xFTNameOfCurrentFile(m_xBuilder->weld_label("currentfilename"))
144     , m_xImport(m_xBuilder->weld_widget("import"))
145     , m_xFTImportXSLTFile(m_xBuilder->weld_label("importxsltfile"))
146     , m_xFTImportTemplate(m_xBuilder->weld_label("templateimport"))
147     , m_xFTImportTemplateFile(m_xBuilder->weld_label("importxslttemplate"))
148     , m_xCBXDisplaySource(m_xBuilder->weld_check_button("displaysource"))
149     , m_xPBImportBrowse(m_xBuilder->weld_button("importbrowse"))
150     , m_xPBRecentFile(m_xBuilder->weld_button("recentfile"))
151     , m_xFTNameOfRecentFile(m_xBuilder->weld_label("recentfilename"))
152     , m_xPBClose(m_xBuilder->weld_button("close"))
153 {
154     m_xPBExportBrowse->connect_clicked(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
155     m_xPBCurrentDocument->connect_clicked(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
156     m_xPBImportBrowse->connect_clicked(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
157     m_xPBRecentFile->connect_clicked(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
158     m_xPBClose->connect_clicked(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
159 
160     m_sDialogTitle = m_xDialog->get_title();
161 
162     try
163     {
164         mxGlobalBroadcaster = theGlobalEventBroadcaster::get(mxContext);
165         mxGlobalEventListener = new GlobalEventListenerImpl( this );
166         mxGlobalBroadcaster->addDocumentEventListener( mxGlobalEventListener );
167     }
168     catch( const Exception& )
169     {
170         OSL_FAIL( "XMLFilterTestDialog::XMLFilterTestDialog exception caught!" );
171     }
172 }
173 
~XMLFilterTestDialog()174 XMLFilterTestDialog::~XMLFilterTestDialog()
175 {
176     try
177     {
178         if( mxGlobalBroadcaster.is() )
179             mxGlobalBroadcaster->removeDocumentEventListener( mxGlobalEventListener );
180     }
181     catch( const Exception& )
182     {
183         OSL_FAIL( "XMLFilterTestDialog::~XMLFilterTestDialog exception caught!" );
184     }
185 }
186 
test(const filter_info_impl & rFilterInfo)187 void XMLFilterTestDialog::test( const filter_info_impl& rFilterInfo )
188 {
189     m_xFilterInfo.reset(new filter_info_impl( rFilterInfo ));
190 
191     m_sImportRecentFile.clear();
192 
193     initDialog();
194 
195     m_xDialog->run();
196 }
197 
getFileNameFromURL(OUString const & rURL)198 static OUString getFileNameFromURL( OUString const & rURL )
199 {
200     INetURLObject aURL( rURL );
201     OUString aName( aURL.getName(INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset) );
202     return aName;
203 }
204 
updateCurrentDocumentButtonState(Reference<XComponent> const * pRef)205 void XMLFilterTestDialog::updateCurrentDocumentButtonState( Reference< XComponent > const * pRef /* = NULL */ )
206 {
207     if( pRef && pRef->is() )
208     {
209         if( checkComponent( *pRef, m_xFilterInfo->maDocumentService ) )
210             mxLastFocusModel = *pRef;
211     }
212 
213     bool bExport = (m_xFilterInfo->maFlags & 2) == 2;
214     Reference< XComponent > xCurrentDocument;
215     if( bExport )
216         xCurrentDocument = getFrontMostDocument( m_xFilterInfo->maDocumentService );
217     m_xPBCurrentDocument->set_sensitive( bExport && xCurrentDocument.is() );
218     m_xFTNameOfCurrentFile->set_sensitive( bExport && xCurrentDocument.is() );
219 
220     if( xCurrentDocument.is() )
221     {
222         OUString aTitle;
223         Reference< XDocumentPropertiesSupplier > xDPS( xCurrentDocument, UNO_QUERY );
224         if( xDPS.is() )
225         {
226             Reference< XDocumentProperties > xProps( xDPS->getDocumentProperties() );
227             if( xProps.is() )
228             {
229                 aTitle = xProps->getTitle();
230             }
231         }
232 
233         if( aTitle.isEmpty() )
234         {
235             Reference< XStorable > xStorable( xCurrentDocument, UNO_QUERY );
236             if( xStorable.is() )
237             {
238                 if( xStorable->hasLocation() )
239                 {
240                     OUString aURL( xStorable->getLocation() );
241                     aTitle = getFileNameFromURL( aURL );
242                 }
243             }
244         }
245 
246         m_xFTNameOfCurrentFile->set_label( aTitle );
247     }
248 }
249 
initDialog()250 void XMLFilterTestDialog::initDialog()
251 {
252     DBG_ASSERT( m_xFilterInfo, "i need a filter I can test!" );
253     if( nullptr == m_xFilterInfo )
254         return;
255 
256     OUString aTitle( m_sDialogTitle );
257     aTitle = aTitle.replaceFirst( "%s", m_xFilterInfo->maFilterName );
258     m_xDialog->set_title( aTitle );
259 
260     bool bImport = (m_xFilterInfo->maFlags & 1) == 1;
261     bool bExport = (m_xFilterInfo->maFlags & 2) == 2;
262 
263     updateCurrentDocumentButtonState();
264 
265     m_xExport->set_sensitive(bExport);
266     m_xFTExportXSLTFile->set_label( getFileNameFromURL( m_xFilterInfo->maExportXSLT ) );
267 
268 
269     m_xImport->set_sensitive(bImport);
270     m_xFTImportTemplate->set_sensitive(bImport && !m_xFilterInfo->maImportTemplate.isEmpty());
271     m_xFTImportTemplateFile->set_sensitive(bImport && !m_xFilterInfo->maImportTemplate.isEmpty());
272     m_xPBRecentFile->set_sensitive(bImport && !m_sImportRecentFile.isEmpty());
273     m_xFTNameOfRecentFile->set_sensitive(bImport && !m_sImportRecentFile.isEmpty());
274 
275     m_xFTImportXSLTFile->set_label( getFileNameFromURL( m_xFilterInfo->maImportXSLT ) );
276     m_xFTImportTemplateFile->set_label( getFileNameFromURL( m_xFilterInfo->maImportTemplate ) );
277     m_xFTNameOfRecentFile->set_label( getFileNameFromURL( m_sImportRecentFile ) );
278 }
279 
onExportBrowse()280 void XMLFilterTestDialog::onExportBrowse()
281 {
282     try
283     {
284         // Open Fileopen-Dialog
285         ::sfx2::FileDialogHelper aDlg(
286             css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
287             FileDialogFlags::NONE, m_xDialog.get());
288 
289         Reference< XNameAccess > xFilterContainer( mxContext->getServiceManager()->createInstanceWithContext( "com.sun.star.document.FilterFactory", mxContext ), UNO_QUERY );
290         Reference< XNameAccess > xTypeDetection( mxContext->getServiceManager()->createInstanceWithContext( "com.sun.star.document.TypeDetection", mxContext ), UNO_QUERY );
291         if( xFilterContainer.is() && xTypeDetection.is() )
292         {
293             const Sequence< OUString > aFilterNames( xFilterContainer->getElementNames() );
294 
295             for( OUString const & filterName : aFilterNames )
296             {
297                 Sequence< PropertyValue > aValues;
298 
299                 Any aAny( xFilterContainer->getByName( filterName ) );
300                 if( !(aAny >>= aValues) )
301                     continue;
302 
303                 OUString aInterfaceName;
304                 OUString aType, aService;
305                 sal_Int32 nFlags( 0 );
306 
307                 int nFound = 0;
308 
309                 for( const PropertyValue& rValue : std::as_const(aValues) )
310                 {
311                     if ( rValue.Name == "Type" )
312                     {
313                         rValue.Value >>= aType;
314                         nFound |= 1;
315                     }
316                     else if ( rValue.Name == "DocumentService" )
317                     {
318                         rValue.Value >>= aService;
319                         nFound |= 2;
320                     }
321                     else if ( rValue.Name == "Flags" )
322                     {
323                         rValue.Value >>= nFlags;
324                         nFound |= 4;
325                     }
326                     else if ( rValue.Name == "UIName" )
327                     {
328                         rValue.Value >>= aInterfaceName;
329                         nFound |= 8;
330                     }
331 
332                     if (nFound == 15)
333                         break;
334                 }
335 
336                 if( (nFound == 15) && (!aType.isEmpty() && aService == m_xFilterInfo->maDocumentService) )
337                 {
338                     // see if this filter is not suppressed in dialog
339                     if( (nFlags & 0x1000) == 0 )
340                     {
341                         aAny = xTypeDetection->getByName( aType );
342                         Sequence< PropertyValue > aValues2;
343 
344                         if( aAny >>= aValues2 )
345                         {
346                             PropertyValue* pValues2 = aValues2.getArray();
347                             sal_Int32 nValue;
348 
349                             OUString aExtension;
350                             for( nValue = 0; nValue < aValues2.getLength(); nValue++, pValues2++ )
351                             {
352                                 if ( pValues2->Name == "Extensions" )
353                                 {
354                                     Sequence< OUString > aExtensions;
355                                     if( pValues2->Value >>= aExtensions )
356                                     {
357                                         const sal_Int32 nCount( aExtensions.getLength() );
358                                         OUString* pExtensions = aExtensions.getArray();
359                                         sal_Int32 n;
360                                         for( n = 0; n < nCount; n++ )
361                                         {
362                                             if( n > 0 )
363                                                 aExtension += ";";
364                                             aExtension += "*." + *pExtensions++;
365                                         }
366                                     }
367                                 }
368                             }
369 
370                             OUString aFilterName( aInterfaceName + " (" + aExtension + ")" );
371 
372                             aDlg.AddFilter( aFilterName, aExtension );
373 
374                             if( (nFlags & 0x100) == 0x100 )
375                                 aDlg.SetCurrentFilter( aFilterName );
376                         }
377                     }
378                 }
379 
380             }
381         }
382 
383         aDlg.SetDisplayDirectory( m_sExportRecentFile );
384 
385         if ( aDlg.Execute() == ERRCODE_NONE )
386         {
387             m_sExportRecentFile = aDlg.GetPath();
388 
389             Reference< XDesktop2 > xLoader = Desktop::create( mxContext );
390             Reference< XInteractionHandler2 > xInter = InteractionHandler::createWithParent(mxContext, nullptr);
391             Sequence< PropertyValue > aArguments(1);
392             aArguments[0].Name = "InteractionHandler";
393             aArguments[0].Value <<= xInter;
394             Reference< XComponent > xComp( xLoader->loadComponentFromURL( m_sExportRecentFile, "_default", 0, aArguments ) );
395             if( xComp.is() )
396             {
397                 doExport( xComp );
398             }
399         }
400     }
401     catch(const Exception&)
402     {
403         OSL_FAIL("XMLFilterTestDialog::onExportBrowse exception caught!");
404     }
405 
406     initDialog();
407 }
408 
onExportCurrentDocument()409 void XMLFilterTestDialog::onExportCurrentDocument()
410 {
411     doExport( getFrontMostDocument( m_xFilterInfo->maDocumentService ) );
412 }
413 
doExport(const Reference<XComponent> & xComp)414 void XMLFilterTestDialog::doExport( const Reference< XComponent >& xComp )
415 {
416     try
417     {
418         Reference< XStorable > xStorable( xComp, UNO_QUERY );
419         if( xStorable.is() )
420         {
421             OUString const ext(".xml");
422             utl::TempFile aTempFile(OUString(), true, &ext);
423             OUString aTempFileURL( aTempFile.GetURL() );
424 
425             const application_info_impl* pAppInfo = getApplicationInfo( m_xFilterInfo->maExportService );
426             if( pAppInfo )
427             {
428                 File aOutputFile( aTempFileURL );
429                 /* File::RC rc = */ aOutputFile.open( osl_File_OpenFlag_Write );
430 
431                 // create xslt exporter
432                 Reference< XOutputStream > xIS( new comphelper::OSLOutputStreamWrapper( aOutputFile ) );
433                 int bUseDocType = m_xFilterInfo->maDocType.isEmpty()  ? 0 : 1;
434                 Sequence< PropertyValue > aSourceData( 2 + bUseDocType );
435                 int i = 0;
436 
437 
438                 aSourceData[i  ].Name = "OutputStream";
439                 aSourceData[i++].Value <<= xIS;
440 
441                 aSourceData[i].Name = "Indent";
442                 aSourceData[i++].Value <<= true;
443 
444                 if( bUseDocType )
445                     {
446                         aSourceData[i  ].Name = "DocType_Public";
447                         aSourceData[i++].Value <<= m_xFilterInfo->maDocType;
448                     }
449 
450                 Reference< XExportFilter > xExporter( mxContext->getServiceManager()->createInstanceWithContext( "com.sun.star.documentconversion.XSLTFilter", mxContext ), UNO_QUERY );
451                 Reference< XDocumentHandler > xHandler( xExporter, UNO_QUERY );
452                 if( xHandler.is() )
453                 {
454                     Sequence< OUString > aFilterUserData( m_xFilterInfo->getFilterUserData() );
455                     xExporter->exporter( aSourceData, aFilterUserData );
456 
457                     Reference< XMultiServiceFactory > xDocFac( xComp, UNO_QUERY );
458 
459                     Reference< XEmbeddedObjectResolver > xObjectResolver;
460                     Reference<XGraphicStorageHandler> xGraphicStorageHandler;
461 
462                     if( xDocFac.is() )
463                     {
464                         try
465                         {
466                             xGraphicStorageHandler.set(xDocFac->createInstance("com.sun.star.document.ExportGraphicStorageHandler"), UNO_QUERY);
467                             xObjectResolver.set( xDocFac->createInstance("com.sun.star.document.ExportEmbeddedObjectResolver"), UNO_QUERY );
468                         }
469                         catch( const Exception& )
470                         {
471                         }
472                     }
473 
474                     Sequence< Any > aArgs( 1 + ( xGraphicStorageHandler.is() ? 1 : 0 ) + ( xObjectResolver.is() ? 1 : 0 ) );
475                     Any* pArgs = aArgs.getArray();
476                     if (xGraphicStorageHandler.is())
477                         *pArgs++ <<= xGraphicStorageHandler;
478 
479                     if  (xObjectResolver.is())
480                         *pArgs++ <<= xObjectResolver;
481 
482     //              *pArgs++ <<= xInfoSet;
483                     *pArgs   <<= xHandler;
484 
485                     Reference< XFilter > xFilter( mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( pAppInfo->maXMLExporter, aArgs, mxContext ), UNO_QUERY );
486                     if( xFilter.is() )
487                     {
488                         Reference< XExporter > xExporter2( xFilter, UNO_QUERY );
489                         if( xExporter2.is() )
490                         {
491                             xExporter2->setSourceDocument( xComp );
492 
493                             Sequence< PropertyValue > aDescriptor( 1 );
494                             aDescriptor[0].Name = "FileName";
495                             aDescriptor[0].Value <<= aTempFileURL;
496 
497                             if( xFilter->filter( aDescriptor ) )
498                                 displayXMLFile( aTempFileURL );
499                         }
500                     }
501                 }
502             }
503         }
504     }
505     catch( const Exception& )
506     {
507         OSL_FAIL( "XMLFilterTestDialog::doExport exception caught!" );
508     }
509 }
510 
displayXMLFile(const OUString & rURL)511 void XMLFilterTestDialog::displayXMLFile( const OUString& rURL )
512 {
513     Reference< XSystemShellExecute > xSystemShellExecute(
514           SystemShellExecute::create(comphelper::getProcessComponentContext()) );
515     xSystemShellExecute->execute( rURL, OUString(), SystemShellExecuteFlags::URIS_ONLY );
516 }
517 
onImportBrowse()518 void XMLFilterTestDialog::onImportBrowse()
519 {
520     // Open Fileopen-Dialog
521     ::sfx2::FileDialogHelper aDlg(
522         css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
523         FileDialogFlags::NONE, m_xDialog.get());
524     OUString aFilterName( m_xFilterInfo->maInterfaceName );
525     OUString aExtensions;
526 
527     int nLastIndex = 0;
528     int nCurrentIndex = 0;
529     for( int i = 0; nLastIndex != -1; i++ )
530     {
531         nLastIndex = m_xFilterInfo->maExtension.indexOf( ';', nLastIndex );
532 
533         if( i > 0 )
534             aExtensions += ";";
535 
536         aExtensions += "*.";
537 
538         if( nLastIndex == -1 )
539         {
540 
541             aExtensions += m_xFilterInfo->maExtension.copy( nCurrentIndex );
542         }
543         else
544         {
545             aExtensions += m_xFilterInfo->maExtension.copy( nCurrentIndex, nLastIndex - nCurrentIndex );
546             nCurrentIndex = nLastIndex + 1;
547             nLastIndex = nCurrentIndex;
548         }
549     }
550 
551     aFilterName += " (" + aExtensions + ")";
552 
553     aDlg.AddFilter( aFilterName, aExtensions );
554     aDlg.SetDisplayDirectory( m_sImportRecentFile );
555 
556     if ( aDlg.Execute() == ERRCODE_NONE )
557     {
558         m_sImportRecentFile = aDlg.GetPath();
559         import( m_sImportRecentFile );
560     }
561 
562     initDialog();
563 }
564 
import(const OUString & rURL)565 void XMLFilterTestDialog::import( const OUString& rURL )
566 {
567     try
568     {
569         Reference< XDesktop2 > xLoader = Desktop::create( mxContext );
570         Reference< XInteractionHandler2 > xInter = InteractionHandler::createWithParent(mxContext, nullptr);
571 
572         Sequence< PropertyValue > aArguments(2);
573         aArguments[0].Name = "FilterName";
574         aArguments[0].Value <<= m_xFilterInfo->maFilterName;
575         aArguments[1].Name = "InteractionHandler";
576         aArguments[1].Value <<= xInter;
577 
578         xLoader->loadComponentFromURL( rURL, "_default", 0, aArguments );
579 
580         if( m_xCBXDisplaySource->get_active() )
581         {
582             OUString const ext(".xml");
583             TempFile aTempFile(OUString(), true, &ext);
584             OUString aTempFileURL( aTempFile.GetURL() );
585 
586             Reference< XImportFilter > xImporter( mxContext->getServiceManager()->createInstanceWithContext( "com.sun.star.documentconversion.XSLTFilter", mxContext ), UNO_QUERY );
587             if( xImporter.is() )
588             {
589                 osl::File aInputFile( rURL );
590                 aInputFile.open( osl_File_OpenFlag_Read );
591 
592                 Reference< XInputStream > xIS( new comphelper::OSLInputStreamWrapper( aInputFile ) );
593 
594                 Sequence< PropertyValue > aSourceData( 3 );
595                 int i = 0;
596 
597                 aSourceData[i  ].Name = "InputStream";
598                 aSourceData[i++].Value <<= xIS;
599 
600                 aSourceData[i  ].Name = "FileName";
601                 aSourceData[i++].Value <<= rURL;
602 
603                 aSourceData[i  ].Name = "Indent";
604                 aSourceData[i++].Value <<= true;
605 
606                 Reference< XWriter > xWriter = Writer::create( mxContext );
607 
608                 File aOutputFile( aTempFileURL );
609                 aOutputFile.open( osl_File_OpenFlag_Write );
610 
611                 Reference< XOutputStream > xOS( new OSLOutputStreamWrapper( aOutputFile ) );
612                 xWriter->setOutputStream( xOS );
613 
614                 Sequence< OUString > aFilterUserData( m_xFilterInfo->getFilterUserData() );
615                 xImporter->importer( aSourceData, xWriter, aFilterUserData );
616             }
617 
618             displayXMLFile( aTempFileURL );
619         }
620     }
621     catch(const Exception&)
622     {
623         OSL_FAIL("XMLFilterTestDialog::import caught an exception" );
624     }
625 }
626 
IMPL_LINK(XMLFilterTestDialog,ClickHdl_Impl,weld::Button &,rButton,void)627 IMPL_LINK(XMLFilterTestDialog, ClickHdl_Impl, weld::Button&, rButton, void )
628 {
629     if (m_xPBExportBrowse.get() == &rButton)
630     {
631         onExportBrowse();
632     }
633     else if (m_xPBCurrentDocument.get() == &rButton)
634     {
635         onExportCurrentDocument();
636     }
637     else if (m_xPBImportBrowse.get() == &rButton)
638     {
639         onImportBrowse();
640     }
641     else if (m_xPBRecentFile.get() == &rButton)
642     {
643         import( m_sImportRecentFile );
644     }
645     else if (m_xPBClose.get() == &rButton)
646     {
647         m_xDialog->response(RET_CLOSE);
648     }
649 }
650 
651 /** returns the front most open component that supports the given service */
getFrontMostDocument(const OUString & rServiceName)652 Reference< XComponent > XMLFilterTestDialog::getFrontMostDocument( const OUString& rServiceName )
653 {
654     Reference< XComponent > xRet;
655 
656     try
657     {
658         Reference< XDesktop2 > xDesktop = Desktop::create( mxContext );
659         Reference< XComponent > xTest( mxLastFocusModel );
660         if( checkComponent( xTest, rServiceName ) )
661         {
662             xRet = xTest;
663         }
664         else
665         {
666             xTest = xDesktop->getCurrentComponent();
667 
668             if( checkComponent( xTest, rServiceName ) )
669             {
670                 xRet = xTest;
671             }
672             else
673             {
674                 Reference< XEnumerationAccess > xAccess( xDesktop->getComponents() );
675                 if( xAccess.is() )
676                 {
677                     Reference< XEnumeration > xEnum( xAccess->createEnumeration() );
678                     if( xEnum.is() )
679                     {
680                         while( xEnum->hasMoreElements() )
681                         {
682                             if( (xEnum->nextElement() >>= xTest) && xTest.is() )
683                             {
684                                 if( checkComponent( xTest, rServiceName ) )
685                                 {
686                                     xRet = xTest;
687                                     break;
688                                 }
689                             }
690                         }
691                     }
692                 }
693             }
694         }
695     }
696     catch( const Exception& )
697     {
698         OSL_FAIL( "XMLFilterTestDialog::getFrontMostDocument exception caught!" );
699     }
700 
701     return xRet;
702 }
703 
704 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
705