merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / model / filter / XMLFilter.cxx
blobb66c4bc46102dbd57b36f75922861e7952d8f6ed
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
31 #include "XMLFilter.hxx"
32 #include "macros.hxx"
33 #include "MediaDescriptorHelper.hxx"
34 #include "ContainerHelper.hxx"
35 #include <comphelper/mediadescriptor.hxx>
37 // for ERRCODE_SFX_GENERAL etc.
38 // header contains only macros
39 #include <svtools/sfxecode.hxx>
40 // header for class SvtSaveOptions
41 #include <unotools/saveopt.hxx>
42 #include <comphelper/genericpropertyset.hxx>
43 // header for struct PropertyMapEntry
44 #include <comphelper/propertysetinfo.hxx>
45 #include <comphelper/documentconstants.hxx>
47 // header for class SotStorage
48 #include <sot/storage.hxx>
49 #include <com/sun/star/beans/PropertyAttribute.hpp>
50 #include <com/sun/star/xml/sax/InputSource.hpp>
51 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
52 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
53 #include <com/sun/star/embed/ElementModes.hpp>
54 #include <com/sun/star/embed/XStorage.hpp>
55 #include <com/sun/star/embed/XTransactedObject.hpp>
56 #include <com/sun/star/frame/XModel.hpp>
57 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
58 #include <com/sun/star/xml/sax/XParser.hpp>
59 #include <com/sun/star/xml/sax/SAXParseException.hpp>
60 #include <com/sun/star/packages/zip/ZipIOException.hpp>
61 #include <com/sun/star/document/XGraphicObjectResolver.hpp>
62 #include <com/sun/star/container/XNameAccess.hpp>
64 using namespace ::com::sun::star;
66 using ::com::sun::star::uno::Reference;
67 using ::com::sun::star::uno::Sequence;
68 using ::rtl::OUString;
69 using ::osl::MutexGuard;
71 // ----------------------------------------
72 namespace
74 #define LOCAL_CONST_STR(i, x) sal_Char __READONLY_DATA i[sizeof(x)] = x
75 #define MAP_LEN(x) x, sizeof(x) - 1
77 LOCAL_CONST_STR( sXML_metaStreamName, "meta.xml");
78 LOCAL_CONST_STR( sXML_styleStreamName, "styles.xml" );
79 LOCAL_CONST_STR( sXML_contentStreamName, "content.xml" );
80 LOCAL_CONST_STR( sXML_oldContentStreamName, "Content.xml" );
82 // soffice 6/7
83 // LOCAL_CONST_STR( sXML_export_chart_meta_service, "com.sun.star.comp.Chart.XMLMetaExporter" );
84 LOCAL_CONST_STR( sXML_export_chart_styles_service, "com.sun.star.comp.Chart.XMLStylesExporter" );
85 LOCAL_CONST_STR( sXML_export_chart_content_service, "com.sun.star.comp.Chart.XMLContentExporter" );
87 // LOCAL_CONST_STR( sXML_import_chart_meta_service, "com.sun.star.comp.Chart.XMLMetaImporter" );
88 LOCAL_CONST_STR( sXML_import_chart_styles_service, "com.sun.star.comp.Chart.XMLStylesImporter" );
89 LOCAL_CONST_STR( sXML_import_chart_content_service, "com.sun.star.comp.Chart.XMLContentImporter" );
90 LOCAL_CONST_STR( sXML_import_chart_old_content_service, "com.sun.star.office.sax.importer.Chart" );
92 // Oasis
93 LOCAL_CONST_STR( sXML_export_chart_oasis_styles_service, "com.sun.star.comp.Chart.XMLOasisStylesExporter" );
94 LOCAL_CONST_STR( sXML_export_chart_oasis_content_service, "com.sun.star.comp.Chart.XMLOasisContentExporter" );
95 LOCAL_CONST_STR( sXML_export_chart_oasis_meta_service, "com.sun.star.comp.Chart.XMLOasisMetaExporter" );
97 LOCAL_CONST_STR( sXML_import_chart_oasis_styles_service, "com.sun.star.comp.Chart.XMLOasisStylesImporter" );
98 LOCAL_CONST_STR( sXML_import_chart_oasis_content_service, "com.sun.star.comp.Chart.XMLOasisContentImporter" );
99 LOCAL_CONST_STR( sXML_import_chart_oasis_meta_service, "com.sun.star.comp.Chart.XMLOasisMetaImporter" );
101 uno::Reference< embed::XStorage > lcl_getWriteStorage(
102 const Sequence< beans::PropertyValue >& rMediaDescriptor,
103 const uno::Reference< uno::XComponentContext >& xContext,const ::rtl::OUString& _sMediaType)
105 uno::Reference< embed::XStorage > xStorage;
108 apphelper::MediaDescriptorHelper aMDHelper( rMediaDescriptor );
109 if( aMDHelper.ISSET_Storage )
111 xStorage = aMDHelper.Storage;
113 else
115 Reference< lang::XSingleServiceFactory > xStorageFact(
116 xContext->getServiceManager()->createInstanceWithContext(
117 C2U("com.sun.star.embed.StorageFactory"),
118 xContext ), uno::UNO_QUERY_THROW );
120 ::std::vector< beans::PropertyValue > aPropertiesForStorage;
122 for( sal_Int32 i=rMediaDescriptor.getLength(); i--; )
124 // properties understood by storage factory
125 // (see package/source/xstor/xfactory.cxx for details)
126 if ( rMediaDescriptor[i].Name.equalsAsciiL(
127 RTL_CONSTASCII_STRINGPARAM( "InteractionHandler" ))
128 || rMediaDescriptor[i].Name.equalsAsciiL(
129 RTL_CONSTASCII_STRINGPARAM( "Password" ))
130 || rMediaDescriptor[i].Name.equalsAsciiL(
131 RTL_CONSTASCII_STRINGPARAM( "RepairPackage" ))
132 // || rMediaDescriptor[i].Name.equalsAsciiL(
133 // RTL_CONSTASCII_STRINGPARAM( "StatusIndicator" ))
134 // || rMediaDescriptor[i].Name.equalsAsciiL(
135 // RTL_CONSTASCII_STRINGPARAM( "Unpacked" ))
138 aPropertiesForStorage.push_back( rMediaDescriptor[i] );
142 if( aMDHelper.ISSET_Storage )
143 xStorage.set( aMDHelper.Storage );
144 else
146 Sequence< uno::Any > aStorageArgs( 3 );
147 if( aMDHelper.ISSET_OutputStream )
148 aStorageArgs[0] <<= aMDHelper.OutputStream;
149 else
150 aStorageArgs[0] <<= aMDHelper.URL;
151 aStorageArgs[1] <<= (embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE);
152 aStorageArgs[2] <<= ::chart::ContainerHelper::ContainerToSequence( aPropertiesForStorage );
154 xStorage.set(
155 xStorageFact->createInstanceWithArguments( aStorageArgs ),
156 uno::UNO_QUERY_THROW );
160 // set correct media type at storage
161 uno::Reference<beans::XPropertySet> xProp(xStorage,uno::UNO_QUERY);
162 OUString aMediaType;
163 if ( ! xProp.is() ||
164 ! ( xProp->getPropertyValue( C2U("MediaType")) >>= aMediaType ) ||
165 ( aMediaType.getLength() == 0 ))
167 xProp->setPropertyValue( C2U("MediaType"), uno::makeAny( _sMediaType ));
170 catch( uno::Exception & ex )
172 ASSERT_EXCEPTION( ex );
174 return xStorage;
177 uno::Reference< embed::XStorage > lcl_getReadStorage(
178 const Sequence< beans::PropertyValue >& rMediaDescriptor,
179 const uno::Reference< uno::XComponentContext >& xContext)
181 uno::Reference< embed::XStorage > xStorage;
185 apphelper::MediaDescriptorHelper aMDHelper( rMediaDescriptor );
186 if( aMDHelper.ISSET_Storage )
188 xStorage = aMDHelper.Storage;
190 else
192 // get XStream from MediaDescriptor
193 uno::Reference< io::XInputStream > xStream;
194 ::std::vector< beans::PropertyValue > aPropertiesForStorage;
195 for( sal_Int32 i=rMediaDescriptor.getLength(); i--; )
197 if( rMediaDescriptor[i].Name.equalsAsciiL(
198 RTL_CONSTASCII_STRINGPARAM( "InputStream" )))
199 xStream.set( rMediaDescriptor[i].Value, uno::UNO_QUERY );
201 // properties understood by storage factory
202 // (see package/source/xstor/xfactory.cxx for details)
203 if ( rMediaDescriptor[i].Name.equalsAsciiL(
204 RTL_CONSTASCII_STRINGPARAM( "InteractionHandler" ))
205 || rMediaDescriptor[i].Name.equalsAsciiL(
206 RTL_CONSTASCII_STRINGPARAM( "Password" ))
207 || rMediaDescriptor[i].Name.equalsAsciiL(
208 RTL_CONSTASCII_STRINGPARAM( "RepairPackage" ))
209 // || rMediaDescriptor[i].Name.equalsAsciiL(
210 // RTL_CONSTASCII_STRINGPARAM( "StatusIndicator" ))
211 // || rMediaDescriptor[i].Name.equalsAsciiL(
212 // RTL_CONSTASCII_STRINGPARAM( "Unpacked" ))
215 aPropertiesForStorage.push_back( rMediaDescriptor[i] );
218 OSL_ENSURE( xStream.is(), "No Stream" );
219 if( ! xStream.is())
220 return xStorage;
222 // convert XInputStream to XStorage via the storage factory
223 Reference< lang::XSingleServiceFactory > xStorageFact(
224 xContext->getServiceManager()->createInstanceWithContext(
225 C2U("com.sun.star.embed.StorageFactory"),
226 xContext ),
227 uno::UNO_QUERY_THROW );
228 Sequence< uno::Any > aStorageArgs( 3 );
229 aStorageArgs[0] <<= xStream;
230 aStorageArgs[1] <<= (embed::ElementModes::READ | embed::ElementModes::NOCREATE);
231 aStorageArgs[2] <<= ::chart::ContainerHelper::ContainerToSequence( aPropertiesForStorage );
232 xStorage.set(
233 xStorageFact->createInstanceWithArguments( aStorageArgs ), uno::UNO_QUERY_THROW );
236 OSL_ENSURE( xStorage.is(), "No Storage" );
238 catch( uno::Exception & ex )
240 ASSERT_EXCEPTION( ex );
243 return xStorage;
247 } // anonymous namespace
249 // ----------------------------------------
251 namespace chart
254 XMLFilter::XMLFilter( Reference< uno::XComponentContext > const & xContext ) :
255 m_xContext( xContext ),
256 m_bCancelOperation( false )
259 XMLFilter::~XMLFilter()
262 // ____ XFilter ____
263 sal_Bool SAL_CALL XMLFilter::filter(
264 const Sequence< beans::PropertyValue >& aDescriptor )
265 throw (uno::RuntimeException)
267 bool bResult = false;
269 MutexGuard aGuard( m_aMutex );
271 // ignore cancel flag at start of function
272 // note: is currently ignored during import/export
273 if( m_bCancelOperation )
274 m_bCancelOperation = false;
276 if( m_xSourceDoc.is())
278 OSL_ENSURE( ! m_xTargetDoc.is(), "source doc is set -> target document should not be set" );
279 if( impl_Export( m_xSourceDoc,
280 aDescriptor ) == 0 )
282 m_xSourceDoc = NULL;
283 bResult = true;
286 else if( m_xTargetDoc.is())
288 if( impl_Import( m_xTargetDoc,
289 aDescriptor ) == 0 )
291 m_xTargetDoc = NULL;
292 bResult = true;
295 else
297 OSL_ENSURE( false, "filter() called with no document set" );
300 return bResult;
303 void SAL_CALL XMLFilter::cancel()
304 throw (uno::RuntimeException)
306 // if mutex is locked set "cancel state"
307 // note: is currently ignored in filter-method
308 if( ! m_aMutex.tryToAcquire())
310 m_bCancelOperation = true;
314 // ____ XImporter ____
315 void SAL_CALL XMLFilter::setTargetDocument(
316 const Reference< lang::XComponent >& Document )
317 throw (lang::IllegalArgumentException,
318 uno::RuntimeException)
320 MutexGuard aGuard( m_aMutex );
321 OSL_ENSURE( ! m_xSourceDoc.is(), "Setting target doc while source doc is set" );
323 m_xTargetDoc = Document;
327 // ____ XExporter ____
328 void SAL_CALL XMLFilter::setSourceDocument(
329 const Reference< lang::XComponent >& Document )
330 throw (lang::IllegalArgumentException,
331 uno::RuntimeException)
333 MutexGuard aGuard( m_aMutex );
334 OSL_ENSURE( ! m_xTargetDoc.is(), "Setting source doc while target doc is set" );
336 m_xSourceDoc = Document;
340 sal_Int32 XMLFilter::impl_Import(
341 const Reference< lang::XComponent > & xDocumentComp,
342 const Sequence< beans::PropertyValue > & rMediaDescriptor )
344 sal_Int32 nWarning = 0;
346 OSL_ENSURE( xDocumentComp.is(), "Import: No Model" );
347 OSL_ENSURE( m_xContext.is(), "Import: No ComponentContext" );
349 if( ! (xDocumentComp.is() &&
350 m_xContext.is()))
351 return nWarning;
355 Reference< lang::XServiceInfo > xServInfo( xDocumentComp, uno::UNO_QUERY_THROW );
356 if( ! xServInfo->supportsService( C2U( "com.sun.star.chart2.ChartDocument" )))
358 OSL_ENSURE( false, "Import: No ChartDocument" );
359 return ERRCODE_SFX_GENERAL;
362 Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager());
363 OSL_ENSURE( xFactory.is(), "Import: No Factory" );
364 if( ! xFactory.is())
365 return ERRCODE_SFX_GENERAL;
367 // create a sax parser
368 Reference< xml::sax::XParser > xSaxParser(
369 xFactory->createInstanceWithContext( C2U( "com.sun.star.xml.sax.Parser" ), m_xContext ),
370 uno::UNO_QUERY_THROW );
372 bool bOasis = true;
373 isOasisFormat( rMediaDescriptor, bOasis );
374 Reference< embed::XStorage > xStorage( lcl_getReadStorage( rMediaDescriptor, m_xContext));
375 if( ! xStorage.is())
376 return ERRCODE_SFX_GENERAL;
378 // bool bOasis = (SotStorage::GetVersion( xStorage ) > SOFFICE_FILEFORMAT_60);
380 Reference< document::XGraphicObjectResolver > xGraphicObjectResolver;
381 uno::Reference< lang::XMultiServiceFactory > xServiceFactory( xFactory, uno::UNO_QUERY);
382 if( xServiceFactory.is())
384 uno::Sequence< uno::Any > aArgs(1);
385 aArgs[0] <<= xStorage;
386 xGraphicObjectResolver.set(
387 xServiceFactory->createInstanceWithArguments(
388 C2U("com.sun.star.comp.Svx.GraphicImportHelper"), aArgs ), uno::UNO_QUERY );
391 // create XPropertySet with extra informatio for the filter
392 /** property map for import info set */
393 comphelper::PropertyMapEntry aImportInfoMap[] =
395 // #80365# necessary properties for XML progress bar at load time
396 { MAP_LEN( "ProgressRange" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
397 { MAP_LEN( "ProgressMax" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
398 { MAP_LEN( "ProgressCurrent" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
399 { MAP_LEN( "PrivateData" ), 0,
400 &::getCppuType( (Reference<XInterface> *)0 ),
401 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
402 { MAP_LEN( "BaseURI" ), 0,
403 &::getCppuType( (OUString *)0 ),
404 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
405 { MAP_LEN( "StreamRelPath" ), 0,
406 &::getCppuType( (OUString *)0 ),
407 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
408 { MAP_LEN( "StreamName" ), 0,
409 &::getCppuType( (OUString *)0 ),
410 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
411 { MAP_LEN( "BuildId" ), 0,
412 &::getCppuType( (OUString *)0 ),
413 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
414 { NULL, 0, 0, NULL, 0, 0 }
416 uno::Reference< beans::XPropertySet > xImportInfo(
417 comphelper::GenericPropertySet_CreateInstance(
418 new comphelper::PropertySetInfo( aImportInfoMap ) ) );
420 // Set base URI and Hierarchical Name
421 OUString aHierarchName, aBaseUri;
422 uno::Reference< frame::XModel > xModel( m_xSourceDoc, uno::UNO_QUERY );
423 if( xModel.is() )
425 uno::Sequence< beans::PropertyValue > aModProps = xModel->getArgs();
426 for( sal_Int32 nInd = 0; nInd < aModProps.getLength(); nInd++ )
428 if( aModProps[nInd].Name.equals( C2U( "HierarchicalDocumentName" ) ) )
430 // Actually this argument only has meaning for embedded documents
431 aModProps[nInd].Value >>= aHierarchName;
433 else if( aModProps[nInd].Name.equals( C2U( "DocumentBaseURL" ) ) )
435 aModProps[nInd].Value >>= aBaseUri;
440 if( aBaseUri.getLength() )
441 xImportInfo->setPropertyValue( C2U("BaseURI"), uno::makeAny( aBaseUri ) );
443 if( aHierarchName.getLength() )
444 xImportInfo->setPropertyValue( C2U("StreamRelPath"), uno::makeAny( aHierarchName ) );
446 // import meta information
447 if( bOasis )
448 nWarning |= impl_ImportStream(
449 C2U( sXML_metaStreamName ),
450 C2U( sXML_import_chart_oasis_meta_service ),
451 xStorage, xSaxParser, xFactory, xGraphicObjectResolver, xImportInfo );
453 // import styles
454 nWarning |= impl_ImportStream(
455 C2U( sXML_styleStreamName ),
456 bOasis
457 ? C2U( sXML_import_chart_oasis_styles_service )
458 : C2U( sXML_import_chart_styles_service ),
459 xStorage, xSaxParser, xFactory, xGraphicObjectResolver, xImportInfo );
461 // import content
462 sal_Int32 nContentWarning = impl_ImportStream(
463 C2U( sXML_contentStreamName ),
464 bOasis
465 ? C2U( sXML_import_chart_oasis_content_service )
466 : C2U( sXML_import_chart_content_service ),
467 xStorage, xSaxParser, xFactory, xGraphicObjectResolver, xImportInfo );
468 nWarning |= nContentWarning;
470 // import of "content.xml" didn't work - try old "Content.xml" stream
471 if( nContentWarning != 0 )
473 nWarning = impl_ImportStream(
474 C2U( sXML_oldContentStreamName ),
475 C2U( sXML_import_chart_old_content_service ),
476 xStorage, xSaxParser, xFactory, xGraphicObjectResolver, xImportInfo );
479 catch( uno::Exception & ex )
481 ASSERT_EXCEPTION( ex );
483 // something went awry
484 nWarning = ERRCODE_SFX_GENERAL;
487 return nWarning;
490 sal_Int32 XMLFilter::impl_ImportStream(
491 const OUString & rStreamName,
492 const OUString & rServiceName,
493 const Reference< embed::XStorage > & xStorage,
494 const Reference< xml::sax::XParser > & xParser,
495 const Reference< lang::XMultiComponentFactory > & xFactory,
496 const Reference< document::XGraphicObjectResolver > & xGraphicObjectResolver,
497 uno::Reference< beans::XPropertySet >& xImportInfo )
499 sal_Int32 nWarning = ERRCODE_SFX_GENERAL;
501 Reference< container::XNameAccess > xNameAcc( xStorage, uno::UNO_QUERY );
502 if( ! (xNameAcc.is() &&
503 xNameAcc->hasByName( rStreamName )))
504 return 0;
506 if( xImportInfo.is() )
507 xImportInfo->setPropertyValue( C2U("StreamName"), uno::makeAny( rStreamName ) );
509 if( xStorage.is() &&
510 xStorage->isStreamElement( rStreamName ) )
514 xml::sax::InputSource aParserInput;
515 aParserInput.aInputStream.set(
516 xStorage->openStreamElement(
517 rStreamName,
518 embed::ElementModes::READ | embed::ElementModes::NOCREATE ),
519 uno::UNO_QUERY );
521 // todo: encryption
523 if( aParserInput.aInputStream.is())
525 sal_Int32 nArgs = 0;
526 //if( m_xStatusIndicator.is())
527 // nArgs++;
528 if( xGraphicObjectResolver.is())
529 nArgs++;
530 if( xImportInfo.is())
531 nArgs++;
533 uno::Sequence< uno::Any > aFilterCompArgs( nArgs );
535 nArgs = 0;
536 //if( m_xStatusIndicator.is())
537 // aFilterCompArgs[ nArgs++ ] <<= m_xStatusIndicator;
538 if( xGraphicObjectResolver.is())
539 aFilterCompArgs[nArgs++] <<= xGraphicObjectResolver;
540 if( xImportInfo.is())
541 aFilterCompArgs[ nArgs++ ] <<= xImportInfo;
543 Reference< xml::sax::XDocumentHandler > xDocHandler(
544 xFactory->createInstanceWithArgumentsAndContext( rServiceName, aFilterCompArgs, m_xContext ),
545 uno::UNO_QUERY_THROW );
548 Reference< document::XImporter > xImporter( xDocHandler, uno::UNO_QUERY_THROW );
549 xImporter->setTargetDocument( Reference< lang::XComponent >( m_xTargetDoc, uno::UNO_QUERY_THROW ));
551 if ( m_sDocumentHandler.getLength() )
555 uno::Sequence< uno::Any > aArgs(2);
556 beans::NamedValue aValue;
557 aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentHandler"));
558 aValue.Value <<= xDocHandler;
559 aArgs[0] <<= aValue;
560 aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Model"));
561 aValue.Value <<= m_xTargetDoc;
562 aArgs[1] <<= aValue;
564 xDocHandler.set(xFactory->createInstanceWithArgumentsAndContext(m_sDocumentHandler,aArgs,m_xContext), uno::UNO_QUERY );
565 xImporter.set(xDocHandler,uno::UNO_QUERY);
567 catch(uno::Exception&)
569 OSL_ENSURE(0,"Exception caught!");
572 xParser->setDocumentHandler( xDocHandler );
573 xParser->parseStream( aParserInput );
576 // load was successful
577 nWarning = 0;
579 catch( xml::sax::SAXParseException )
581 // todo: if encrypted: ERRCODE_SFX_WRONGPASSWORD
583 catch( xml::sax::SAXException )
585 // todo: if encrypted: ERRCODE_SFX_WRONGPASSWORD
587 catch( packages::zip::ZipIOException )
589 nWarning = ERRCODE_IO_BROKENPACKAGE;
591 catch( io::IOException )
594 catch( uno::Exception& aEx )
596 ASSERT_EXCEPTION( aEx );
600 return nWarning;
603 sal_Int32 XMLFilter::impl_Export(
604 const Reference< lang::XComponent > & xDocumentComp,
605 const Sequence< beans::PropertyValue > & rMediaDescriptor )
607 //save
609 sal_Int32 nWarning = 0;
611 OSL_ENSURE( xDocumentComp.is(), "Export: No Model" );
612 OSL_ENSURE( m_xContext.is(), "Export: No ComponentContext" );
614 if( !xDocumentComp.is() || !m_xContext.is() )
615 return nWarning;
619 Reference< lang::XServiceInfo > xServInfo( xDocumentComp, uno::UNO_QUERY_THROW );
620 if( ! xServInfo->supportsService( C2U( "com.sun.star.chart2.ChartDocument" )))
622 OSL_ENSURE( false, "Export: No ChartDocument" );
623 return ERRCODE_SFX_GENERAL;
626 Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager());
627 OSL_ENSURE( xFactory.is(), "Export: No Factory" );
628 if( ! xFactory.is())
629 return ERRCODE_SFX_GENERAL;
630 uno::Reference< lang::XMultiServiceFactory > xServiceFactory( m_xContext->getServiceManager(), uno::UNO_QUERY);
631 if( ! xServiceFactory.is())
632 return ERRCODE_SFX_GENERAL;
634 uno::Reference< io::XActiveDataSource > xSaxWriter( xServiceFactory->createInstance(
635 C2U("com.sun.star.xml.sax.Writer")), uno::UNO_QUERY );
636 if ( !xSaxWriter.is() )
637 return ERRCODE_SFX_GENERAL;
639 bool bOasis = true;
640 isOasisFormat( rMediaDescriptor, bOasis );
642 uno::Reference< embed::XStorage > xStorage( lcl_getWriteStorage( rMediaDescriptor, m_xContext, getMediaType(bOasis) ) );
643 OSL_ENSURE( xStorage.is(), "No Storage" );
644 if( ! xStorage.is())
645 return ERRCODE_SFX_GENERAL;
647 uno::Reference< xml::sax::XDocumentHandler> xDocHandler( xSaxWriter, uno::UNO_QUERY );
649 if ( m_sDocumentHandler.getLength() )
653 uno::Sequence< uno::Any > aArgs(2);
654 beans::NamedValue aValue;
655 aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentHandler"));
656 aValue.Value <<= xDocHandler;
657 aArgs[0] <<= aValue;
658 aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Model"));
659 aValue.Value <<= xDocumentComp;
660 aArgs[1] <<= aValue;
662 xDocHandler.set(xServiceFactory->createInstanceWithArguments(m_sDocumentHandler,aArgs), uno::UNO_QUERY );
663 xSaxWriter.set(xDocHandler,uno::UNO_QUERY);
665 catch(uno::Exception&)
667 OSL_ENSURE(0,"Exception caught!");
671 uno::Sequence< uno::Any > aGraphicResolverArgs(1);
672 aGraphicResolverArgs[0] <<= xStorage;
673 Reference< document::XGraphicObjectResolver > xGraphicObjectResolver(
674 xServiceFactory->createInstanceWithArguments(
675 C2U("com.sun.star.comp.Svx.GraphicExportHelper"), aGraphicResolverArgs ), uno::UNO_QUERY );
677 uno::Reference< beans::XPropertySet > xInfoSet;
679 // property map for export info set
680 comphelper::PropertyMapEntry aExportInfoMap[] =
682 { MAP_LEN("UsePrettyPrinting"), 0, &::getBooleanCppuType(), beans::PropertyAttribute::MAYBEVOID, 0},
683 { MAP_LEN("BaseURI"), 0, &::getCppuType( (OUString *)0 ), beans::PropertyAttribute::MAYBEVOID, 0 },
684 { MAP_LEN("StreamRelPath"), 0, &::getCppuType( (OUString *)0 ), beans::PropertyAttribute::MAYBEVOID, 0 },
685 { MAP_LEN("StreamName"), 0, &::getCppuType( (OUString *)0 ), beans::PropertyAttribute::MAYBEVOID, 0 },
686 { MAP_LEN("ExportTableNumberList"), 0, &::getBooleanCppuType(), beans::PropertyAttribute::MAYBEVOID, 0 },
687 { NULL, 0, 0, NULL, 0, 0 }
690 xInfoSet = comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) );
692 SvtSaveOptions aSaveOpt;
693 OUString sUsePrettyPrinting(RTL_CONSTASCII_USTRINGPARAM("UsePrettyPrinting"));
694 sal_Bool bUsePrettyPrinting( aSaveOpt.IsPrettyPrinting() );
695 xInfoSet->setPropertyValue( sUsePrettyPrinting, uno::makeAny( bUsePrettyPrinting ) );
696 if( ! bOasis )
697 xInfoSet->setPropertyValue( C2U("ExportTableNumberList"), uno::makeAny( true ));
700 sal_Int32 nArgs = 2;
701 if( xGraphicObjectResolver.is())
702 nArgs++;
704 uno::Sequence< uno::Any > aFilterProperties( nArgs );
706 nArgs = 0;
707 aFilterProperties[ nArgs++ ] <<= xInfoSet;
708 aFilterProperties[ nArgs++ ] <<= xDocHandler;
709 if( xGraphicObjectResolver.is())
710 aFilterProperties[ nArgs++ ] <<= xGraphicObjectResolver;
713 // bool bOasis = (SotStorage::GetVersion( xStorage ) > SOFFICE_FILEFORMAT_60);
715 // export meta information
716 if( bOasis )
717 nWarning |= impl_ExportStream(
718 C2U( sXML_metaStreamName ),
719 C2U( sXML_export_chart_oasis_meta_service ),
720 xStorage, xSaxWriter, xServiceFactory, aFilterProperties );
722 // export styles
723 nWarning |= impl_ExportStream(
724 C2U( sXML_styleStreamName ),
725 bOasis
726 ? C2U( sXML_export_chart_oasis_styles_service )
727 : C2U( sXML_export_chart_styles_service ),
728 xStorage, xSaxWriter, xServiceFactory, aFilterProperties );
730 // export content
731 sal_Int32 nContentWarning = impl_ExportStream(
732 C2U( sXML_contentStreamName ),
733 bOasis
734 ? C2U( sXML_export_chart_oasis_content_service )
735 : C2U( sXML_export_chart_content_service ),
736 xStorage, xSaxWriter, xServiceFactory, aFilterProperties );
737 nWarning |= nContentWarning;
739 Reference< lang::XComponent > xComp( xGraphicObjectResolver, uno::UNO_QUERY );
740 if( xComp.is())
741 xComp->dispose();
743 uno::Reference<embed::XTransactedObject> xTransact( xStorage ,uno::UNO_QUERY);
744 if ( xTransact.is() )
745 xTransact->commit();
747 catch( uno::Exception & ex )
749 ASSERT_EXCEPTION( ex );
751 // something went awry
752 nWarning = ERRCODE_SFX_GENERAL;
755 return nWarning;
758 sal_Int32 XMLFilter::impl_ExportStream(
759 const OUString & rStreamName,
760 const OUString & rServiceName,
761 const Reference< embed::XStorage > & xStorage,
762 const uno::Reference< io::XActiveDataSource >& xActiveDataSource,
763 const Reference< lang::XMultiServiceFactory >& xServiceFactory,
764 const Sequence< uno::Any > & rFilterProperties )
766 sal_Int32 nWarning = 0;
770 if( !xServiceFactory.is() )
771 return ERRCODE_SFX_GENERAL;
772 if( !xStorage.is() )
773 return ERRCODE_SFX_GENERAL;
774 if ( !xActiveDataSource.is() )
775 return ERRCODE_SFX_GENERAL;
777 uno::Reference< io::XStream > xStream( xStorage->openStreamElement(
778 rStreamName, embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE ) );
779 if ( !xStream.is() )
780 return ERRCODE_SFX_GENERAL;
781 uno::Reference< io::XOutputStream > xOutputStream( xStream->getOutputStream() );
782 if ( !xOutputStream.is() )
783 return ERRCODE_SFX_GENERAL;
785 uno::Reference< beans::XPropertySet > xStreamProp( xOutputStream, uno::UNO_QUERY );
786 if(xStreamProp.is()) try
788 xStreamProp->setPropertyValue( C2U("MediaType"), uno::makeAny( C2U("text/xml") ) );
789 xStreamProp->setPropertyValue( C2U("Compressed"), uno::makeAny( sal_True ) );//@todo?
790 xStreamProp->setPropertyValue( C2U("UseCommonStoragePasswordEncryption"), uno::makeAny( sal_True ) );
792 catch( uno::Exception& rEx )
794 ASSERT_EXCEPTION( rEx );
797 xActiveDataSource->setOutputStream(xOutputStream);
799 // set Base URL
801 uno::Reference< beans::XPropertySet > xInfoSet;
802 if( rFilterProperties.getLength() > 0 )
803 rFilterProperties.getConstArray()[0] >>= xInfoSet;
804 OSL_ENSURE( xInfoSet.is(), "missing infoset for export" );
805 if( xInfoSet.is() )
806 xInfoSet->setPropertyValue( C2U("StreamName"), uno::makeAny( rStreamName ) );
809 Reference< XExporter > xExporter( xServiceFactory->createInstanceWithArguments(
810 rServiceName, rFilterProperties ), uno::UNO_QUERY);
811 if ( !xExporter.is() )
812 return ERRCODE_SFX_GENERAL;
814 xExporter->setSourceDocument( m_xSourceDoc );
816 uno::Reference< document::XFilter > xFilter( xExporter, uno::UNO_QUERY );
817 if ( !xFilter.is() )
818 return ERRCODE_SFX_GENERAL;
820 uno::Sequence < beans::PropertyValue > aMediaDesc(0);
821 //@todo? filter properties? ... url? ...
822 xFilter->filter( aMediaDesc );
824 catch( uno::Exception& rEx )
826 ASSERT_EXCEPTION( rEx );
828 return nWarning;
831 // --------------------------------------------------------------------------------
833 Sequence< OUString > XMLFilter::getSupportedServiceNames_Static()
835 Sequence< OUString > aServices( 2 );
836 aServices[ 0 ] = C2U( "com.sun.star.document.ImportFilter" );
837 aServices[ 1 ] = C2U( "com.sun.star.document.ExportFilter" );
839 // todo: services are incomplete. Missing:
840 // XInitialization, XNamed
841 return aServices;
843 // -----------------------------------------------------------------------------
845 void XMLFilter::isOasisFormat(const Sequence< beans::PropertyValue >& _rMediaDescriptor, bool & rOutOASIS )
847 apphelper::MediaDescriptorHelper aMDHelper( _rMediaDescriptor );
848 if( aMDHelper.ISSET_FilterName )
849 rOutOASIS = aMDHelper.FilterName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("chart8"));
851 // -----------------------------------------------------------------------------
852 ::rtl::OUString XMLFilter::getMediaType(bool _bOasis)
854 return _bOasis ? MIMETYPE_OASIS_OPENDOCUMENT_CHART : MIMETYPE_VND_SUN_XML_CHART;
856 // -----------------------------------------------------------------------------
858 APPHELPER_XSERVICEINFO_IMPL( XMLFilter, C2U( "com.sun.star.comp.chart2.XMLFilter" ) );
859 // -----------------------------------------------------------------------------
861 void XMLReportFilterHelper::isOasisFormat(const Sequence< beans::PropertyValue >& _rMediaDescriptor, bool & rOutOASIS )
863 apphelper::MediaDescriptorHelper aMDHelper( _rMediaDescriptor );
864 if( aMDHelper.ISSET_FilterName )
865 rOutOASIS = aMDHelper.FilterName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("StarOffice XML (Base) Report Chart"));
867 // -----------------------------------------------------------------------------
868 ::rtl::OUString XMLReportFilterHelper::getMediaType(bool )
870 return MIMETYPE_OASIS_OPENDOCUMENT_REPORT_CHART;
873 } // namespace chart