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