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 : 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< XDesktop2 > xLoader = Desktop::create( comphelper::getComponentContext(mxMSF) );
419 0 : Reference< XInteractionHandler2 > xInter( InteractionHandler::createWithParent(comphelper::getComponentContext(mxMSF), 0) );
420 0 : OUString aFrame( RTL_CONSTASCII_USTRINGPARAM( "_default" ) );
421 0 : Sequence< PropertyValue > aArguments(1);
422 0 : aArguments[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ));
423 0 : aArguments[0].Value <<= xInter;
424 0 : Reference< XComponent > xComp( xLoader->loadComponentFromURL( m_sExportRecentFile, aFrame, 0, aArguments ) );
425 0 : if( xComp.is() )
426 : {
427 0 : doExport( xComp );
428 0 : }
429 0 : }
430 : }
431 0 : catch(const Exception&)
432 : {
433 : OSL_FAIL("XMLFilterTestDialog::onExportBrowse exception caught!");
434 : }
435 :
436 0 : initDialog();
437 0 : }
438 :
439 0 : void XMLFilterTestDialog::onExportCurrentDocument()
440 : {
441 0 : doExport( getFrontMostDocument( m_pFilterInfo->maDocumentService ) );
442 0 : }
443 :
444 0 : void XMLFilterTestDialog::doExport( Reference< XComponent > xComp )
445 : {
446 : try
447 : {
448 0 : Reference< XStorable > xStorable( xComp, UNO_QUERY );
449 0 : if( xStorable.is() )
450 : {
451 0 : String leadingChars;
452 0 : String ext(RTL_CONSTASCII_USTRINGPARAM(".xml"));
453 0 : utl::TempFile aTempFile(leadingChars, &ext);
454 0 : OUString aTempFileURL( aTempFile.GetURL() );
455 :
456 0 : const application_info_impl* pAppInfo = getApplicationInfo( m_pFilterInfo->maExportService );
457 0 : if( pAppInfo )
458 : {
459 0 : File aOutputFile( aTempFileURL );
460 0 : /* File::RC rc = */ aOutputFile.open( osl_File_OpenFlag_Write );
461 :
462 : // create xslt exporter
463 0 : Reference< XOutputStream > xIS( new comphelper::OSLOutputStreamWrapper( aOutputFile ) );
464 0 : int bUseDocType = m_pFilterInfo->maDocType.isEmpty() ? 0 : 1;
465 0 : Sequence< PropertyValue > aSourceData( 2 + bUseDocType );
466 0 : int i = 0;
467 :
468 :
469 0 : aSourceData[i ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "OutputStream" ) );
470 0 : aSourceData[i++].Value <<= xIS;
471 :
472 0 : aSourceData[i].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Indent" ) );
473 0 : aSourceData[i++].Value <<= (sal_Bool)sal_True;
474 :
475 0 : if( bUseDocType )
476 : {
477 0 : aSourceData[i ].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("DocType_Public"));
478 0 : aSourceData[i++].Value <<= m_pFilterInfo->maDocType;
479 : }
480 :
481 0 : Reference< XExportFilter > xExporter( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.documentconversion.XSLTFilter" )) ), UNO_QUERY );
482 0 : Reference< XDocumentHandler > xHandler( xExporter, UNO_QUERY );
483 0 : if( xHandler.is() )
484 : {
485 0 : Sequence< OUString > aFilterUserData( m_pFilterInfo->getFilterUserData() );
486 0 : xExporter->exporter( aSourceData, aFilterUserData );
487 :
488 0 : Reference< XMultiServiceFactory > xDocFac( xComp, UNO_QUERY );
489 :
490 0 : Reference< XEmbeddedObjectResolver > xObjectResolver;
491 0 : Reference< XGraphicObjectResolver > xGrfResolver;
492 :
493 0 : if( xDocFac.is() )
494 : {
495 : try
496 : {
497 0 : xGrfResolver = Reference< XGraphicObjectResolver >::query( xDocFac->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportGraphicObjectResolver" )) ) );
498 0 : xObjectResolver = Reference< XEmbeddedObjectResolver >::query( xDocFac->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportEmbeddedObjectResolver" )) ) );
499 : }
500 0 : catch( const Exception& )
501 : {
502 : }
503 : }
504 :
505 0 : Sequence< Any > aArgs( 1 + ( xGrfResolver.is() ? 1 : 0 ) + ( xObjectResolver.is() ? 1 : 0 ) );
506 0 : Any* pArgs = aArgs.getArray();
507 0 : if( xGrfResolver.is() ) *pArgs++ <<= xGrfResolver;
508 0 : if( xObjectResolver.is() ) *pArgs++ <<= xObjectResolver;
509 :
510 : // *pArgs++ <<= xInfoSet;
511 0 : *pArgs <<= xHandler;
512 :
513 0 : Reference< XFilter > xFilter( mxMSF->createInstanceWithArguments( pAppInfo->maXMLExporter, aArgs ), UNO_QUERY );
514 0 : if( xFilter.is() )
515 : {
516 0 : Reference< XExporter > xExporter2( xFilter, UNO_QUERY );
517 0 : if( xExporter2.is() )
518 : {
519 0 : xExporter2->setSourceDocument( xComp );
520 :
521 0 : Sequence< PropertyValue > aDescriptor( 1 );
522 0 : aDescriptor[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "FileName" ) );
523 0 : aDescriptor[0].Value <<= aTempFileURL;
524 :
525 0 : if( xFilter->filter( aDescriptor ) )
526 0 : displayXMLFile( aTempFileURL );
527 0 : }
528 0 : }
529 0 : }
530 0 : }
531 0 : }
532 : }
533 0 : catch( const Exception& )
534 : {
535 : OSL_FAIL( "XMLFilterTestDialog::doExport exception catched!" );
536 : }
537 0 : }
538 :
539 0 : void XMLFilterTestDialog::displayXMLFile( const OUString& rURL )
540 : {
541 : Reference< XSystemShellExecute > xSystemShellExecute(
542 0 : SystemShellExecute::create(comphelper::getProcessComponentContext()) );
543 0 : xSystemShellExecute->execute( rURL, rtl::OUString(), SystemShellExecuteFlags::URIS_ONLY );
544 0 : }
545 :
546 0 : void XMLFilterTestDialog::onImportBrowse()
547 : {
548 : // Open Fileopen-Dialog
549 : ::sfx2::FileDialogHelper aDlg(
550 0 : com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
551 0 : String aFilterName( m_pFilterInfo->maInterfaceName );
552 0 : String aExtensions;
553 :
554 0 : int nLastIndex = 0;
555 0 : int nCurrentIndex = 0;
556 0 : for( int i = 0; nLastIndex != -1; i++ )
557 : {
558 0 : nLastIndex = m_pFilterInfo->maExtension.indexOf( sal_Unicode( ';' ), nLastIndex );
559 :
560 0 : if( i > 0 )
561 0 : aExtensions += ';';
562 :
563 0 : aExtensions += String( RTL_CONSTASCII_USTRINGPARAM("*.") );
564 :
565 0 : if( nLastIndex == -1 )
566 : {
567 :
568 0 : aExtensions += String( m_pFilterInfo->maExtension.copy( nCurrentIndex ) );
569 : }
570 : else
571 : {
572 0 : aExtensions += String( m_pFilterInfo->maExtension.copy( nCurrentIndex, nLastIndex - nCurrentIndex ) );
573 0 : nCurrentIndex = nLastIndex + 1;
574 0 : nLastIndex = nCurrentIndex;
575 : }
576 : }
577 :
578 0 : aFilterName += String( RTL_CONSTASCII_USTRINGPARAM( " (" ) );
579 0 : aFilterName += aExtensions;
580 0 : aFilterName += sal_Unicode(')');
581 :
582 0 : aDlg.AddFilter( aFilterName, aExtensions );
583 0 : aDlg.SetDisplayDirectory( m_sImportRecentFile );
584 :
585 0 : if ( aDlg.Execute() == ERRCODE_NONE )
586 : {
587 0 : m_sImportRecentFile = aDlg.GetPath();
588 0 : import( m_sImportRecentFile );
589 : }
590 :
591 0 : initDialog();
592 0 : }
593 :
594 0 : void XMLFilterTestDialog::onImportRecentDocument()
595 : {
596 0 : import( m_sImportRecentFile );
597 0 : }
598 :
599 0 : void XMLFilterTestDialog::import( const OUString& rURL )
600 : {
601 : try
602 : {
603 0 : Reference< XDesktop2 > xLoader = Desktop::create( comphelper::getComponentContext(mxMSF) );
604 0 : Reference< XInteractionHandler2 > xInter( InteractionHandler::createWithParent(comphelper::getComponentContext(mxMSF), 0) );
605 :
606 0 : OUString aFrame( RTL_CONSTASCII_USTRINGPARAM( "_default" ) );
607 0 : Sequence< PropertyValue > aArguments(2);
608 0 : aArguments[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ));
609 0 : aArguments[0].Value <<= m_pFilterInfo->maFilterName;
610 0 : aArguments[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ));
611 0 : aArguments[1].Value <<= xInter;
612 :
613 0 : xLoader->loadComponentFromURL( rURL, aFrame, 0, aArguments );
614 :
615 0 : if( m_pCBXDisplaySource->IsChecked() )
616 : {
617 0 : String lead;
618 0 : String ext(RTL_CONSTASCII_USTRINGPARAM(".xml"));
619 0 : TempFile aTempFile(lead, &ext);
620 0 : OUString aTempFileURL( aTempFile.GetURL() );
621 :
622 0 : Reference< XImportFilter > xImporter( mxMSF->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.documentconversion.XSLTFilter" )) ), UNO_QUERY );
623 0 : if( xImporter.is() )
624 : {
625 0 : osl::File aInputFile( rURL );
626 0 : aInputFile.open( osl_File_OpenFlag_Read );
627 :
628 0 : Reference< XInputStream > xIS( new comphelper::OSLInputStreamWrapper( aInputFile ) );
629 :
630 0 : Sequence< PropertyValue > aSourceData( 3 );
631 0 : int i = 0;
632 :
633 0 : aSourceData[i ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "InputStream" ));
634 0 : aSourceData[i++].Value <<= xIS;
635 :
636 0 : aSourceData[i ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "FileName" ));
637 0 : aSourceData[i++].Value <<= rURL;
638 :
639 0 : aSourceData[i ].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Indent" ));
640 0 : aSourceData[i++].Value <<= (sal_Bool)sal_True;
641 :
642 0 : Reference< XWriter > xWriter = Writer::create( comphelper::getComponentContext(mxMSF) );
643 :
644 0 : File aOutputFile( aTempFileURL );
645 0 : aOutputFile.open( osl_File_OpenFlag_Write );
646 :
647 0 : Reference< XOutputStream > xOS( new OSLOutputStreamWrapper( aOutputFile ) );
648 0 : Reference< XActiveDataSource > xDocSrc( xWriter, UNO_QUERY );
649 0 : xDocSrc->setOutputStream( xOS );
650 :
651 0 : Sequence< OUString > aFilterUserData( m_pFilterInfo->getFilterUserData() );
652 0 : xImporter->importer( aSourceData, Reference<XDocumentHandler>(xWriter, UNO_QUERY_THROW), aFilterUserData );
653 : }
654 :
655 0 : displayXMLFile( aTempFileURL );
656 0 : }
657 : }
658 0 : catch(const Exception&)
659 : {
660 : OSL_FAIL("XMLFilterTestDialog::import catched an exception" );
661 : }
662 0 : }
663 :
664 0 : IMPL_LINK(XMLFilterTestDialog, ClickHdl_Impl, PushButton *, pButton )
665 : {
666 0 : if (m_pPBExportBrowse == pButton)
667 : {
668 0 : onExportBrowse();
669 : }
670 0 : else if (m_pPBCurrentDocument == pButton)
671 : {
672 0 : onExportCurrentDocument();
673 : }
674 0 : else if (m_pPBImportBrowse == pButton)
675 : {
676 0 : onImportBrowse();
677 : }
678 0 : else if (m_pPBRecentFile == pButton)
679 : {
680 0 : onImportRecentDocument();
681 : }
682 0 : else if (m_pPBClose == pButton)
683 : {
684 0 : Close();
685 : }
686 :
687 0 : return 0;
688 : }
689 :
690 : /** returns the front most open component that supports the given service */
691 0 : Reference< XComponent > XMLFilterTestDialog::getFrontMostDocument( const OUString& rServiceName )
692 : {
693 0 : Reference< XComponent > xRet;
694 :
695 : try
696 : {
697 0 : Reference< XDesktop2 > xDesktop = Desktop::create( comphelper::getComponentContext(mxMSF) );
698 0 : Reference< XComponent > xTest( mxLastFocusModel );
699 0 : if( checkComponent( xTest, rServiceName ) )
700 : {
701 0 : xRet = xTest;
702 : }
703 : else
704 : {
705 0 : xTest = (Reference< XComponent >)xDesktop->getCurrentComponent();
706 :
707 0 : if( checkComponent( xTest, rServiceName ) )
708 : {
709 0 : xRet = xTest;
710 : }
711 : else
712 : {
713 0 : Reference< XEnumerationAccess > xAccess( xDesktop->getComponents() );
714 0 : if( xAccess.is() )
715 : {
716 0 : Reference< XEnumeration > xEnum( xAccess->createEnumeration() );
717 0 : if( xEnum.is() )
718 : {
719 0 : while( xEnum->hasMoreElements() )
720 : {
721 0 : if( (xEnum->nextElement() >>= xTest) && xTest.is() )
722 : {
723 0 : if( checkComponent( xTest, rServiceName ) )
724 : {
725 0 : xRet = xTest;
726 0 : break;
727 : }
728 : }
729 : }
730 0 : }
731 0 : }
732 : }
733 0 : }
734 : }
735 0 : catch( const Exception& )
736 : {
737 : OSL_FAIL( "XMLFilterTestDialog::getFrontMostDocument exception catched!" );
738 : }
739 :
740 0 : return xRet;
741 0 : }
742 :
743 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|