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