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