LCOV - code coverage report
Current view: top level - libreoffice/filter/source/xsltdialog - xmlfiltertestdialog.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 372 0.0 %
Date: 2012-12-17 Functions: 0 25 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/XGraphicObjectResolver.hpp>
      26             : #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
      27             : #include <com/sun/star/frame/GlobalEventBroadcaster.hpp>
      28             : #include <com/sun/star/frame/XConfigManager.hpp>
      29             : #include <com/sun/star/frame/XDesktop.hpp>
      30             : #include <com/sun/star/frame/XComponentLoader.hpp>
      31             : #include <com/sun/star/frame/XStorable.hpp>
      32             : #include <com/sun/star/io/XActiveDataSource.hpp>
      33             : #include <com/sun/star/system/SystemShellExecute.hpp>
      34             : #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
      35             : #include <com/sun/star/task/InteractionHandler.hpp>
      36             : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
      37             : #include <com/sun/star/xml/XImportFilter.hpp>
      38             : #include <com/sun/star/xml/XExportFilter.hpp>
      39             : #include <com/sun/star/xml/sax/Writer.hpp>
      40             : 
      41             : #include <comphelper/oslfile2streamwrap.hxx>
      42             : #include <vcl/svapp.hxx>
      43             : #include <osl/mutex.hxx>
      44             : #include <sfx2/filedlghelper.hxx>
      45             : #include <osl/file.hxx>
      46             : #include <unotools/tempfile.hxx>
      47             : #include <tools/urlobj.hxx>
      48             : #include <comphelper/processfactory.hxx>
      49             : 
      50             : #include "xmlfilterdialogstrings.hrc"
      51             : #include "xmlfiltersettingsdialog.hxx"
      52             : #include "xmlfiltertestdialog.hxx"
      53             : 
      54             : 
      55             : using namespace utl;
      56             : using namespace osl;
      57             : using namespace comphelper;
      58             : using namespace com::sun::star::lang;
      59             : using namespace com::sun::star::beans;
      60             : using namespace com::sun::star::container;
      61             : using namespace com::sun::star::document;
      62             : using namespace com::sun::star::frame;
      63             : using namespace com::sun::star::task;
      64             : using namespace com::sun::star::uno;
      65             : using namespace com::sun::star::io;
      66             : using namespace com::sun::star::system;
      67             : using namespace com::sun::star::xml;
      68             : using namespace com::sun::star::xml::sax;
      69             : 
      70             : using ::rtl::OUString;
      71             : 
      72           0 : class GlobalEventListenerImpl : public ::cppu::WeakImplHelper1< com::sun::star::document::XEventListener >
      73             : {
      74             : public:
      75             :     GlobalEventListenerImpl( XMLFilterTestDialog* pDialog );
      76             : 
      77             :     // XEventListener
      78             :     virtual void SAL_CALL notifyEvent( const com::sun::star::document::EventObject& Event ) throw (RuntimeException);
      79             : 
      80             :     // lang::XEventListener
      81             :     virtual void SAL_CALL disposing( const com::sun::star::lang::EventObject& Source ) throw (RuntimeException);
      82             : private:
      83             :     XMLFilterTestDialog* mpDialog;
      84             : };
      85             : 
      86           0 : GlobalEventListenerImpl::GlobalEventListenerImpl( XMLFilterTestDialog* pDialog )
      87           0 : : mpDialog( pDialog )
      88             : {
      89           0 : }
      90             : 
      91           0 : void SAL_CALL GlobalEventListenerImpl::notifyEvent( const com::sun::star::document::EventObject& Event ) throw (RuntimeException)
      92             : {
      93           0 :     ::SolarMutexGuard aGuard;
      94           0 :     if( (Event.EventName.compareToAscii( RTL_CONSTASCII_STRINGPARAM("OnFocus") ) == 0) ||
      95           0 :         (Event.EventName.compareToAscii( RTL_CONSTASCII_STRINGPARAM("OnUnload") ) == 0) )
      96             :     {
      97           0 :         Reference< XComponent > xComp( Event.Source, UNO_QUERY );
      98           0 :         mpDialog->updateCurrentDocumentButtonState( &xComp );
      99           0 :     }
     100           0 : }
     101             : 
     102           0 : void SAL_CALL GlobalEventListenerImpl::disposing( const com::sun::star::lang::EventObject& /* Source */ ) throw (RuntimeException)
     103             : {
     104           0 : }
     105             : 
     106             : /** returns true if the given component supports the given service */
     107           0 : static bool checkComponent( Reference< XComponent >& rxComponent, const OUString& rServiceName )
     108             : {
     109             :     try
     110             :     {
     111           0 :         Reference< XServiceInfo > xInfo( rxComponent, UNO_QUERY );
     112           0 :         if( xInfo.is() )
     113             :         {
     114           0 :             if( xInfo->supportsService( rServiceName ) )
     115             :             {
     116             :                 // special case for impress documents which supports same service as draw documents
     117           0 :                 if ( rServiceName == "com.sun.star.drawing.DrawingDocument" )
     118             :                 {
     119             :                     // so if we want a draw we need to check if its not an impress
     120           0 :                     if( !xInfo->supportsService( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.PresentationDocument") ) ) )
     121           0 :                         return true;
     122             :                 }
     123             :                 else
     124             :                 {
     125           0 :                     return true;
     126             :                 }
     127             :             }
     128           0 :         }
     129             :     }
     130           0 :     catch( const Exception& )
     131             :     {
     132             :         OSL_FAIL( "checkComponent exception catched!" );
     133             :     }
     134             : 
     135           0 :     return false;
     136             : }
     137             : 
     138           0 : XMLFilterTestDialog::XMLFilterTestDialog(Window* pParent,
     139             :     const Reference<XMultiServiceFactory>& rxMSF)
     140             :     : ModalDialog(pParent, "TestXMLFilterDialog", "filter/ui/testxmlfilter.ui")
     141             :     , mxMSF(rxMSF)
     142           0 :     , m_pFilterInfo(NULL)
     143             : {
     144           0 :     get(m_pExport, "export");
     145           0 :     get(m_pFTExportXSLTFile, "exportxsltfile");
     146           0 :     get(m_pPBExportBrowse, "exportbrowse");
     147           0 :     get(m_pPBCurrentDocument, "currentdocument");
     148           0 :     get(m_pFTNameOfCurrentFile, "currentfilename");
     149             : 
     150           0 :     get(m_pImport, "import");
     151           0 :     get(m_pFTImportXSLTFile, "importxsltfile");
     152           0 :     get(m_pFTImportTemplate, "templateimport");
     153           0 :     get(m_pFTImportTemplateFile, "importxslttemplate");
     154           0 :     get(m_pCBXDisplaySource, "displaysource");
     155           0 :     get(m_pPBImportBrowse, "importbrowse");
     156           0 :     get(m_pPBRecentFile, "recentfile");
     157           0 :     get(m_pFTNameOfRecentFile, "recentfilename");
     158             : 
     159             :     long nWidth =
     160           0 :         std::max(m_pPBExportBrowse->get_preferred_size().Width(),
     161           0 :         std::max(m_pPBCurrentDocument->get_preferred_size().Width(),
     162           0 :         std::max(m_pPBImportBrowse->get_preferred_size().Width(),
     163           0 :                  m_pPBRecentFile->get_preferred_size().Width())));
     164             : 
     165             :     //make sure that the second columns of the two disconnected
     166             :     //grids are of equal width by ensuring that the widest
     167             :     //entry in each equals
     168           0 :     m_pPBExportBrowse->set_width_request(nWidth);
     169           0 :     m_pPBImportBrowse->set_width_request(nWidth);
     170             : 
     171           0 :     get(m_pPBClose, "close");
     172             : 
     173           0 :     m_pPBExportBrowse->SetClickHdl(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
     174           0 :     m_pPBCurrentDocument->SetClickHdl(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
     175           0 :     m_pPBImportBrowse->SetClickHdl(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
     176           0 :     m_pPBRecentFile->SetClickHdl(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
     177           0 :     m_pPBClose->SetClickHdl(LINK( this, XMLFilterTestDialog, ClickHdl_Impl ) );
     178             : 
     179           0 :     m_sDialogTitle = GetText();
     180             : 
     181             :     try
     182             :     {
     183           0 :         mxGlobalBroadcaster = Reference < XEventBroadcaster >( GlobalEventBroadcaster::create(comphelper::getComponentContext(mxMSF)), UNO_QUERY_THROW );
     184           0 :         mxGlobalEventListener = new GlobalEventListenerImpl( this );
     185           0 :         mxGlobalBroadcaster->addEventListener( mxGlobalEventListener );
     186             :     }
     187           0 :     catch( const Exception& )
     188             :     {
     189             :         OSL_FAIL( "XMLFilterTestDialog::XMLFilterTestDialog exception catched!" );
     190             :     }
     191           0 : }
     192             : 
     193           0 : XMLFilterTestDialog::~XMLFilterTestDialog()
     194             : {
     195             :     try
     196             :     {
     197           0 :         if( mxGlobalBroadcaster.is() )
     198           0 :             mxGlobalBroadcaster->removeEventListener( mxGlobalEventListener );
     199             :     }
     200           0 :     catch( const Exception& )
     201             :     {
     202             :         OSL_FAIL( "XMLFilterTestDialog::~XMLFilterTestDialog exception catched!" );
     203             :     }
     204             : 
     205           0 :     delete m_pFilterInfo;
     206           0 : }
     207             : 
     208           0 : void XMLFilterTestDialog::test( const filter_info_impl& rFilterInfo )
     209             : {
     210           0 :     delete m_pFilterInfo;
     211           0 :     m_pFilterInfo = new filter_info_impl( rFilterInfo );
     212             : 
     213           0 :     m_sImportRecentFile = OUString();
     214             : 
     215           0 :     initDialog();
     216             : 
     217           0 :     Execute();
     218           0 : }
     219             : 
     220           0 : static OUString getFileNameFromURL( OUString& rURL )
     221             : {
     222           0 :     INetURLObject aURL( rURL );
     223           0 :     OUString aName( aURL.getName(INetURLObject::LAST_SEGMENT, sal_True, INetURLObject::DECODE_WITH_CHARSET) );
     224           0 :     return aName;
     225             : }
     226             : 
     227           0 : void XMLFilterTestDialog::updateCurrentDocumentButtonState( Reference< XComponent > * pRef /* = NULL */ )
     228             : {
     229           0 :     if( pRef && pRef->is() )
     230             :     {
     231           0 :         if( checkComponent( *pRef, m_pFilterInfo->maDocumentService ) )
     232           0 :             mxLastFocusModel = *pRef;
     233             :     }
     234             : 
     235           0 :     bool bExport = (m_pFilterInfo->maFlags & 2) == 2;
     236           0 :     Reference< XComponent > xCurrentDocument;
     237           0 :     if( bExport )
     238           0 :         xCurrentDocument = getFrontMostDocument( m_pFilterInfo->maDocumentService );
     239           0 :     m_pPBCurrentDocument->Enable( bExport && xCurrentDocument.is() );
     240           0 :     m_pFTNameOfCurrentFile->Enable( bExport && xCurrentDocument.is() );
     241             : 
     242           0 :     if( xCurrentDocument.is() )
     243             :     {
     244           0 :         OUString aTitle;
     245           0 :         Reference< XDocumentPropertiesSupplier > xDPS( xCurrentDocument, UNO_QUERY );
     246           0 :         if( xDPS.is() )
     247             :         {
     248           0 :             Reference< XDocumentProperties > xProps( xDPS->getDocumentProperties() );
     249           0 :             if( xProps.is() )
     250             :             {
     251           0 :                 aTitle = xProps->getTitle();
     252           0 :             }
     253             :         }
     254             : 
     255           0 :         if( aTitle.isEmpty() )
     256             :         {
     257           0 :             Reference< XStorable > xStorable( xCurrentDocument, UNO_QUERY );
     258           0 :             if( xStorable.is() )
     259             :             {
     260           0 :                 if( xStorable->hasLocation() )
     261             :                 {
     262           0 :                     OUString aURL( xStorable->getLocation() );
     263           0 :                     aTitle = getFileNameFromURL( aURL );
     264             :                 }
     265           0 :             }
     266             :         }
     267             : 
     268           0 :         m_pFTNameOfCurrentFile->SetText( aTitle );
     269           0 :     }
     270           0 : }
     271             : 
     272           0 : void XMLFilterTestDialog::initDialog()
     273             : {
     274             :     DBG_ASSERT( m_pFilterInfo, "i need a filter I can test!" );
     275           0 :     if( NULL == m_pFilterInfo )
     276           0 :         return;
     277             : 
     278           0 :     String aTitle( m_sDialogTitle );
     279           0 :     aTitle.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM("%s") ), m_pFilterInfo->maFilterName );
     280           0 :     SetText( aTitle );
     281             : 
     282           0 :     String aEmpty;
     283           0 :     bool bImport = (m_pFilterInfo->maFlags & 1) == 1;
     284           0 :     bool bExport = (m_pFilterInfo->maFlags & 2) == 2;
     285             : 
     286           0 :     updateCurrentDocumentButtonState();
     287             : 
     288           0 :     m_pExport->Enable(bExport);
     289           0 :     m_pFTExportXSLTFile->SetText( getFileNameFromURL( m_pFilterInfo->maExportXSLT ) );
     290             : 
     291             :     // ---
     292             : 
     293           0 :     m_pImport->Enable(bImport);
     294           0 :     m_pFTImportTemplate->Enable(bImport && !m_pFilterInfo->maImportTemplate.isEmpty());
     295           0 :     m_pFTImportTemplateFile->Enable(bImport && !m_pFilterInfo->maImportTemplate.isEmpty());
     296           0 :     m_pPBRecentFile->Enable(bImport && !m_sImportRecentFile.isEmpty());
     297           0 :     m_pFTNameOfRecentFile->Enable(bImport && !m_sImportRecentFile.isEmpty());
     298             : 
     299           0 :     m_pFTImportXSLTFile->SetText( getFileNameFromURL( m_pFilterInfo->maImportXSLT ) );
     300           0 :     m_pFTImportTemplateFile->SetText( getFileNameFromURL( m_pFilterInfo->maImportTemplate ) );
     301           0 :     m_pFTNameOfRecentFile->SetText( getFileNameFromURL( m_sImportRecentFile ) );
     302             : }
     303             : 
     304           0 : void XMLFilterTestDialog::onExportBrowse()
     305             : {
     306             :     try
     307             :     {
     308             :         // Open Fileopen-Dialog
     309             :            ::sfx2::FileDialogHelper aDlg(
     310             :             com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
     311           0 :             0 );
     312             : 
     313           0 :         Reference< XNameAccess > xFilterContainer( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" )) ), UNO_QUERY );
     314           0 :         Reference< XNameAccess > xTypeDetection( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.TypeDetection" )) ), UNO_QUERY );
     315           0 :         if( xFilterContainer.is() && xTypeDetection.is() )
     316             :         {
     317           0 :             Sequence< OUString > aFilterNames( xFilterContainer->getElementNames() );
     318           0 :             OUString* pFilterName = aFilterNames.getArray();
     319             : 
     320           0 :             for( sal_Int32 nFilter = 0; nFilter < aFilterNames.getLength(); nFilter++, pFilterName++ )
     321             :             {
     322           0 :                 Sequence< PropertyValue > aValues;
     323             : 
     324           0 :                 Any aAny( xFilterContainer->getByName( *pFilterName ) );
     325           0 :                 if( !(aAny >>= aValues) )
     326           0 :                     continue;
     327             : 
     328           0 :                 OUString aInterfaceName;
     329           0 :                 PropertyValue* pValues = aValues.getArray();
     330           0 :                 OUString aType, aService;
     331           0 :                 sal_Int32 nFlags( 0 );
     332             : 
     333           0 :                 int nFound = 0;
     334             : 
     335           0 :                 for( sal_Int32  nValue = 0; (nValue < aValues.getLength()) && (nFound != 15); nValue++, pValues++ )
     336             :                 {
     337           0 :                     if ( pValues->Name == "Type" )
     338             :                     {
     339           0 :                         pValues->Value >>= aType;
     340           0 :                         nFound |= 1;
     341             :                     }
     342           0 :                     else if ( pValues->Name == "DocumentService" )
     343             :                     {
     344           0 :                         pValues->Value >>= aService;
     345           0 :                         nFound |= 2;
     346             :                     }
     347           0 :                     else if ( pValues->Name == "Flags" )
     348             :                     {
     349           0 :                         pValues->Value >>= nFlags;
     350           0 :                         nFound |= 4;
     351             :                     }
     352           0 :                     if ( pValues->Name == "UIName" )
     353             :                     {
     354           0 :                         pValues->Value >>= aInterfaceName;
     355           0 :                         nFound |= 8;
     356             :                     }
     357             : 
     358             :                 }
     359             : 
     360           0 :                 if( (nFound == 15) && (!aType.isEmpty() && aService == m_pFilterInfo->maDocumentService) )
     361             :                 {
     362             :                     // see if this filter is not supressed in dialog
     363           0 :                     if( (nFlags & 0x1000) == 0 )
     364             :                     {
     365           0 :                         aAny = xTypeDetection->getByName( aType );
     366           0 :                         Sequence< PropertyValue > aValues2;
     367             : 
     368           0 :                         if( aAny >>= aValues2 )
     369             :                         {
     370           0 :                             PropertyValue* pValues2 = aValues2.getArray();
     371             :                             sal_Int32 nValue;
     372             : 
     373           0 :                             OUString aExtension;
     374           0 :                             for( nValue = 0; nValue < aValues2.getLength(); nValue++, pValues2++ )
     375             :                             {
     376           0 :                                 if ( pValues2->Name == "Extensions" )
     377             :                                 {
     378           0 :                                     Sequence< OUString > aExtensions;
     379           0 :                                     if( pValues2->Value >>= aExtensions )
     380             :                                     {
     381           0 :                                         const sal_Int32 nCount( aExtensions.getLength() );
     382           0 :                                         OUString* pExtensions = aExtensions.getArray();
     383             :                                         sal_Int32 n;
     384           0 :                                         for( n = 0; n < nCount; n++ )
     385             :                                         {
     386           0 :                                             if( n > 0 )
     387           0 :                                                 aExtension += OUString( sal_Unicode(';') );
     388           0 :                                             aExtension += OUString( RTL_CONSTASCII_USTRINGPARAM( "*." ));
     389           0 :                                             aExtension += (*pExtensions++);
     390             :                                         }
     391           0 :                                     }
     392             :                                 }
     393             :                             }
     394             : 
     395           0 :                             String aExtensions( aExtension );
     396           0 :                             String aFilterName( aInterfaceName );
     397           0 :                             aFilterName += String( RTL_CONSTASCII_USTRINGPARAM(" (") );
     398           0 :                             aFilterName += aExtensions;
     399           0 :                             aFilterName += sal_Unicode(')');
     400             : 
     401           0 :                             aDlg.AddFilter( aFilterName, aExtensions );
     402             : 
     403           0 :                             if( (nFlags & 0x100) == 0x100 )
     404           0 :                                 aDlg.SetCurrentFilter( aFilterName );
     405           0 :                         }
     406             :                     }
     407             :                 }
     408             : 
     409           0 :             }
     410             :         }
     411             : 
     412           0 :         aDlg.SetDisplayDirectory( m_sExportRecentFile );
     413             : 
     414           0 :         if ( aDlg.Execute() == ERRCODE_NONE )
     415             :         {
     416           0 :             m_sExportRecentFile = aDlg.GetPath();
     417             : 
     418           0 :             Reference< XComponentLoader > xLoader( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" )) ), UNO_QUERY );
     419           0 :             Reference< XInteractionHandler2 > xInter( InteractionHandler::createWithParent(comphelper::getComponentContext(mxMSF), 0) );
     420           0 :             if( xLoader.is() )
     421             :             {
     422           0 :                 OUString aFrame( RTL_CONSTASCII_USTRINGPARAM( "_default" ) );
     423           0 :                 Sequence< PropertyValue > aArguments(1);
     424           0 :                 aArguments[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ));
     425           0 :                 aArguments[0].Value <<= xInter;
     426           0 :                 Reference< XComponent > xComp( xLoader->loadComponentFromURL( m_sExportRecentFile, aFrame, 0, aArguments ) );
     427           0 :                 if( xComp.is() )
     428             :                 {
     429           0 :                     doExport( xComp );
     430           0 :                 }
     431           0 :             }
     432           0 :         }
     433             :     }
     434           0 :     catch(const Exception&)
     435             :     {
     436             :         OSL_FAIL("XMLFilterTestDialog::onExportBrowse exception caught!");
     437             :     }
     438             : 
     439           0 :     initDialog();
     440           0 : }
     441             : 
     442           0 : void XMLFilterTestDialog::onExportCurrentDocument()
     443             : {
     444           0 :     doExport( getFrontMostDocument( m_pFilterInfo->maDocumentService ) );
     445           0 : }
     446             : 
     447           0 : void XMLFilterTestDialog::doExport( Reference< XComponent > xComp )
     448             : {
     449             :     try
     450             :     {
     451           0 :         Reference< XStorable > xStorable( xComp, UNO_QUERY );
     452           0 :         if( xStorable.is() )
     453             :         {
     454           0 :             String leadingChars;
     455           0 :             String ext(RTL_CONSTASCII_USTRINGPARAM(".xml"));
     456           0 :             utl::TempFile aTempFile(leadingChars, &ext);
     457           0 :             OUString aTempFileURL( aTempFile.GetURL() );
     458             : 
     459           0 :             const application_info_impl* pAppInfo = getApplicationInfo( m_pFilterInfo->maExportService );
     460           0 :             if( pAppInfo )
     461             :             {
     462           0 :                 File aOutputFile( aTempFileURL );
     463           0 :                 /* File::RC rc = */ aOutputFile.open( osl_File_OpenFlag_Write );
     464             : 
     465             :                 // create xslt exporter
     466           0 :                 Reference< XOutputStream > xIS( new comphelper::OSLOutputStreamWrapper( aOutputFile ) );
     467           0 :                 int bUseDocType = m_pFilterInfo->maDocType.isEmpty()  ? 0 : 1;
     468           0 :                 Sequence< PropertyValue > aSourceData( 2 + bUseDocType );
     469           0 :                 int i = 0;
     470             :                 
     471             :                 
     472           0 :                 aSourceData[i  ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "OutputStream" ) );
     473           0 :                 aSourceData[i++].Value <<= xIS;
     474             :                 
     475           0 :                 aSourceData[i].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Indent" ) );
     476           0 :                 aSourceData[i++].Value <<= (sal_Bool)sal_True;
     477             :                 
     478           0 :                 if( bUseDocType )
     479             :                     {
     480           0 :                         aSourceData[i  ].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("DocType_Public"));
     481           0 :                         aSourceData[i++].Value <<= m_pFilterInfo->maDocType;
     482             :                     }
     483             : 
     484           0 :                 Reference< XExportFilter > xExporter( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.documentconversion.XSLTFilter" )) ), UNO_QUERY );
     485           0 :                 Reference< XDocumentHandler > xHandler( xExporter, UNO_QUERY );
     486           0 :                 if( xHandler.is() )
     487             :                 {
     488           0 :                     Sequence< OUString > aFilterUserData( m_pFilterInfo->getFilterUserData() );
     489           0 :                     xExporter->exporter( aSourceData, aFilterUserData );
     490             : 
     491           0 :                     Reference< XMultiServiceFactory > xDocFac( xComp, UNO_QUERY );
     492             : 
     493           0 :                     Reference< XEmbeddedObjectResolver > xObjectResolver;
     494           0 :                     Reference< XGraphicObjectResolver > xGrfResolver;
     495             : 
     496           0 :                     if( xDocFac.is() )
     497             :                     {
     498             :                         try
     499             :                         {
     500           0 :                             xGrfResolver = Reference< XGraphicObjectResolver >::query( xDocFac->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportGraphicObjectResolver" )) ) );
     501           0 :                             xObjectResolver = Reference< XEmbeddedObjectResolver >::query( xDocFac->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportEmbeddedObjectResolver" )) ) );
     502             :                         }
     503           0 :                         catch( const Exception& )
     504             :                         {
     505             :                         }
     506             :                     }
     507             : 
     508           0 :                     Sequence< Any > aArgs( 1 + ( xGrfResolver.is() ? 1 : 0 ) + ( xObjectResolver.is() ? 1 : 0 ) );
     509           0 :                     Any* pArgs = aArgs.getArray();
     510           0 :                     if( xGrfResolver.is() )         *pArgs++ <<= xGrfResolver;
     511           0 :                     if( xObjectResolver.is() )      *pArgs++ <<= xObjectResolver;
     512             : 
     513             :     //              *pArgs++ <<= xInfoSet;
     514           0 :                     *pArgs   <<= xHandler;
     515             : 
     516           0 :                     Reference< XFilter > xFilter( mxMSF->createInstanceWithArguments( pAppInfo->maXMLExporter, aArgs ), UNO_QUERY );
     517           0 :                     if( xFilter.is() )
     518             :                     {
     519           0 :                         Reference< XExporter > xExporter2( xFilter, UNO_QUERY );
     520           0 :                         if( xExporter2.is() )
     521             :                         {
     522           0 :                             xExporter2->setSourceDocument( xComp );
     523             : 
     524           0 :                             Sequence< PropertyValue > aDescriptor( 1 );
     525           0 :                             aDescriptor[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "FileName" ) );
     526           0 :                             aDescriptor[0].Value <<= aTempFileURL;
     527             : 
     528           0 :                             if( xFilter->filter( aDescriptor ) )
     529           0 :                                 displayXMLFile( aTempFileURL );
     530           0 :                         }
     531           0 :                     }
     532           0 :                 }
     533           0 :             }
     534           0 :         }
     535             :     }
     536           0 :     catch( const Exception& )
     537             :     {
     538             :         OSL_FAIL( "XMLFilterTestDialog::doExport exception catched!" );
     539             :     }
     540           0 : }
     541             : 
     542           0 : void XMLFilterTestDialog::displayXMLFile( const OUString& rURL )
     543             : {
     544             :     Reference< XSystemShellExecute > xSystemShellExecute(
     545           0 :           SystemShellExecute::create(comphelper::getProcessComponentContext()) );
     546           0 :     xSystemShellExecute->execute( rURL, rtl::OUString(), SystemShellExecuteFlags::URIS_ONLY );
     547           0 : }
     548             : 
     549           0 : void XMLFilterTestDialog::onImportBrowse()
     550             : {
     551             :     // Open Fileopen-Dialog
     552             :        ::sfx2::FileDialogHelper aDlg(
     553           0 :         com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
     554           0 :     String aFilterName( m_pFilterInfo->maInterfaceName );
     555           0 :     String aExtensions;
     556             : 
     557           0 :     int nLastIndex = 0;
     558           0 :     int nCurrentIndex = 0;
     559           0 :     for( int i = 0; nLastIndex != -1; i++ )
     560             :     {
     561           0 :         nLastIndex = m_pFilterInfo->maExtension.indexOf( sal_Unicode( ';' ), nLastIndex );
     562             : 
     563           0 :         if( i > 0 )
     564           0 :             aExtensions += ';';
     565             : 
     566           0 :         aExtensions += String( RTL_CONSTASCII_USTRINGPARAM("*.") );
     567             : 
     568           0 :         if( nLastIndex == -1 )
     569             :         {
     570             : 
     571           0 :             aExtensions += String( m_pFilterInfo->maExtension.copy( nCurrentIndex ) );
     572             :         }
     573             :         else
     574             :         {
     575           0 :             aExtensions += String( m_pFilterInfo->maExtension.copy( nCurrentIndex, nLastIndex - nCurrentIndex ) );
     576           0 :             nCurrentIndex = nLastIndex + 1;
     577           0 :             nLastIndex = nCurrentIndex;
     578             :         }
     579             :     }
     580             : 
     581           0 :     aFilterName += String( RTL_CONSTASCII_USTRINGPARAM( " (" ) );
     582           0 :     aFilterName += aExtensions;
     583           0 :     aFilterName += sal_Unicode(')');
     584             : 
     585           0 :     aDlg.AddFilter( aFilterName, aExtensions );
     586           0 :     aDlg.SetDisplayDirectory( m_sImportRecentFile );
     587             : 
     588           0 :     if ( aDlg.Execute() == ERRCODE_NONE )
     589             :     {
     590           0 :         m_sImportRecentFile = aDlg.GetPath();
     591           0 :         import( m_sImportRecentFile );
     592             :     }
     593             : 
     594           0 :     initDialog();
     595           0 : }
     596             : 
     597           0 : void XMLFilterTestDialog::onImportRecentDocument()
     598             : {
     599           0 :     import( m_sImportRecentFile );
     600           0 : }
     601             : 
     602           0 : void XMLFilterTestDialog::import( const OUString& rURL )
     603             : {
     604             :     try
     605             :     {
     606           0 :         Reference< XComponentLoader > xLoader( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" )) ), UNO_QUERY );
     607           0 :         Reference< XInteractionHandler2 > xInter( InteractionHandler::createWithParent(comphelper::getComponentContext(mxMSF), 0) );
     608           0 :         if( xLoader.is() )
     609             :         {
     610             : 
     611           0 :             OUString aFrame( RTL_CONSTASCII_USTRINGPARAM( "_default" ) );
     612           0 :             Sequence< PropertyValue > aArguments(2);
     613           0 :             aArguments[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ));
     614           0 :             aArguments[0].Value <<= m_pFilterInfo->maFilterName;
     615           0 :             aArguments[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ));
     616           0 :             aArguments[1].Value <<= xInter;
     617             : 
     618           0 :             xLoader->loadComponentFromURL( rURL, aFrame, 0, aArguments );
     619             :         }
     620             : 
     621           0 :         if( m_pCBXDisplaySource->IsChecked() )
     622             :         {
     623           0 :             String lead;
     624           0 :             String ext(RTL_CONSTASCII_USTRINGPARAM(".xml"));
     625           0 :             TempFile aTempFile(lead, &ext);
     626           0 :             OUString aTempFileURL( aTempFile.GetURL() );
     627             : 
     628           0 :             Reference< XImportFilter > xImporter( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.documentconversion.XSLTFilter" )) ), UNO_QUERY );
     629           0 :             if( xImporter.is() )
     630             :             {
     631           0 :                 osl::File aInputFile( rURL );
     632           0 :                 aInputFile.open( osl_File_OpenFlag_Read );
     633             : 
     634           0 :                 Reference< XInputStream > xIS( new comphelper::OSLInputStreamWrapper( aInputFile ) );
     635             : 
     636           0 :                 Sequence< PropertyValue > aSourceData( 3 );
     637           0 :                 int i = 0;
     638             : 
     639           0 :                 aSourceData[i  ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "InputStream" ));
     640           0 :                 aSourceData[i++].Value <<= xIS;
     641             : 
     642           0 :                 aSourceData[i  ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "FileName" ));
     643           0 :                 aSourceData[i++].Value <<= rURL;
     644             : 
     645           0 :                 aSourceData[i  ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Indent" ));
     646           0 :                 aSourceData[i++].Value <<= (sal_Bool)sal_True;
     647             : 
     648           0 :                 Reference< XWriter > xWriter = Writer::create( comphelper::getComponentContext(mxMSF) );
     649             : 
     650           0 :                 File aOutputFile( aTempFileURL );
     651           0 :                 aOutputFile.open( osl_File_OpenFlag_Write );
     652             : 
     653           0 :                 Reference< XOutputStream > xOS( new OSLOutputStreamWrapper( aOutputFile ) );
     654           0 :                 Reference< XActiveDataSource > xDocSrc( xWriter, UNO_QUERY );
     655           0 :                 xDocSrc->setOutputStream( xOS );
     656             : 
     657           0 :                 Sequence< OUString > aFilterUserData( m_pFilterInfo->getFilterUserData() );
     658           0 :                 xImporter->importer( aSourceData, Reference<XDocumentHandler>(xWriter, UNO_QUERY_THROW), aFilterUserData );
     659             :             }
     660             : 
     661           0 :             displayXMLFile( aTempFileURL );
     662           0 :         }
     663             :     }
     664           0 :     catch(const Exception&)
     665             :     {
     666             :         OSL_FAIL("XMLFilterTestDialog::import catched an exception" );
     667             :     }
     668           0 : }
     669             : 
     670           0 : IMPL_LINK(XMLFilterTestDialog, ClickHdl_Impl, PushButton *, pButton )
     671             : {
     672           0 :     if (m_pPBExportBrowse == pButton)
     673             :     {
     674           0 :         onExportBrowse();
     675             :     }
     676           0 :     else if (m_pPBCurrentDocument == pButton)
     677             :     {
     678           0 :         onExportCurrentDocument();
     679             :     }
     680           0 :     else if (m_pPBImportBrowse == pButton)
     681             :     {
     682           0 :         onImportBrowse();
     683             :     }
     684           0 :     else if (m_pPBRecentFile == pButton)
     685             :     {
     686           0 :         onImportRecentDocument();
     687             :     }
     688           0 :     else if (m_pPBClose == pButton)
     689             :     {
     690           0 :         Close();
     691             :     }
     692             : 
     693           0 :     return 0;
     694             : }
     695             : 
     696             : /** returns the front most open component that supports the given service */
     697           0 : Reference< XComponent > XMLFilterTestDialog::getFrontMostDocument( const OUString& rServiceName )
     698             : {
     699           0 :     Reference< XComponent > xRet;
     700             : 
     701             :     try
     702             :     {
     703           0 :         Reference< XDesktop > xDesktop( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" )) ), UNO_QUERY );
     704           0 :         if( xDesktop.is() )
     705             :         {
     706           0 :             Reference< XComponent > xTest( mxLastFocusModel );
     707           0 :             if( checkComponent( xTest, rServiceName ) )
     708             :             {
     709           0 :                 xRet = xTest;
     710             :             }
     711             :             else
     712             :             {
     713           0 :                 xTest = (Reference< XComponent >)xDesktop->getCurrentComponent();
     714             : 
     715           0 :                 if( checkComponent( xTest, rServiceName ) )
     716             :                 {
     717           0 :                     xRet = xTest;
     718             :                 }
     719             :                 else
     720             :                 {
     721           0 :                     Reference< XEnumerationAccess > xAccess( xDesktop->getComponents() );
     722           0 :                     if( xAccess.is() )
     723             :                     {
     724           0 :                         Reference< XEnumeration > xEnum( xAccess->createEnumeration() );
     725           0 :                         if( xEnum.is() )
     726             :                         {
     727           0 :                             while( xEnum->hasMoreElements() )
     728             :                             {
     729           0 :                                 if( (xEnum->nextElement() >>= xTest) && xTest.is() )
     730             :                                 {
     731           0 :                                     if( checkComponent( xTest, rServiceName ) )
     732             :                                     {
     733           0 :                                         xRet = xTest;
     734           0 :                                         break;
     735             :                                     }
     736             :                                 }
     737             :                             }
     738           0 :                         }
     739           0 :                     }
     740             :                 }
     741           0 :             }
     742           0 :         }
     743             :     }
     744           0 :     catch( const Exception& )
     745             :     {
     746             :         OSL_FAIL( "XMLFilterTestDialog::getFrontMostDocument exception catched!" );
     747             :     }
     748             : 
     749           0 :     return xRet;
     750           0 : }
     751             : 
     752             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10