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"
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 // ----------------------------------------
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" );
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" );
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
;
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
);
146 Sequence
< uno::Any
> aStorageArgs( 3 );
147 if( aMDHelper
.ISSET_OutputStream
)
148 aStorageArgs
[0] <<= aMDHelper
.OutputStream
;
150 aStorageArgs
[0] <<= aMDHelper
.URL
;
151 aStorageArgs
[1] <<= (embed::ElementModes::READWRITE
| embed::ElementModes::TRUNCATE
);
152 aStorageArgs
[2] <<= ::chart::ContainerHelper::ContainerToSequence( aPropertiesForStorage
);
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
);
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
);
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
;
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" );
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"),
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
);
233 xStorageFact
->createInstanceWithArguments( aStorageArgs
), uno::UNO_QUERY_THROW
);
236 OSL_ENSURE( xStorage
.is(), "No Storage" );
238 catch( uno::Exception
& ex
)
240 ASSERT_EXCEPTION( ex
);
247 } // anonymous namespace
249 // ----------------------------------------
254 XMLFilter::XMLFilter( Reference
< uno::XComponentContext
> const & xContext
) :
255 m_xContext( xContext
),
256 m_bCancelOperation( false )
259 XMLFilter::~XMLFilter()
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
,
286 else if( m_xTargetDoc
.is())
288 if( impl_Import( m_xTargetDoc
,
297 OSL_ENSURE( false, "filter() called with no document set" );
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() &&
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" );
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
);
373 isOasisFormat( rMediaDescriptor
, bOasis
);
374 Reference
< embed::XStorage
> xStorage( lcl_getReadStorage( rMediaDescriptor
, m_xContext
));
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
);
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
448 nWarning
|= impl_ImportStream(
449 C2U( sXML_metaStreamName
),
450 C2U( sXML_import_chart_oasis_meta_service
),
451 xStorage
, xSaxParser
, xFactory
, xGraphicObjectResolver
, xImportInfo
);
454 nWarning
|= impl_ImportStream(
455 C2U( sXML_styleStreamName
),
457 ? C2U( sXML_import_chart_oasis_styles_service
)
458 : C2U( sXML_import_chart_styles_service
),
459 xStorage
, xSaxParser
, xFactory
, xGraphicObjectResolver
, xImportInfo
);
462 sal_Int32 nContentWarning
= impl_ImportStream(
463 C2U( sXML_contentStreamName
),
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
;
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
)))
506 if( xImportInfo
.is() )
507 xImportInfo
->setPropertyValue( C2U("StreamName"), uno::makeAny( rStreamName
) );
510 xStorage
->isStreamElement( rStreamName
) )
514 xml::sax::InputSource aParserInput
;
515 aParserInput
.aInputStream
.set(
516 xStorage
->openStreamElement(
518 embed::ElementModes::READ
| embed::ElementModes::NOCREATE
),
523 if( aParserInput
.aInputStream
.is())
526 //if( m_xStatusIndicator.is())
528 if( xGraphicObjectResolver
.is())
530 if( xImportInfo
.is())
533 uno::Sequence
< uno::Any
> aFilterCompArgs( nArgs
);
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
;
560 aValue
.Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Model"));
561 aValue
.Value
<<= m_xTargetDoc
;
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
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
);
603 sal_Int32
XMLFilter::impl_Export(
604 const Reference
< lang::XComponent
> & xDocumentComp
,
605 const Sequence
< beans::PropertyValue
> & rMediaDescriptor
)
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() )
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" );
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
;
640 isOasisFormat( rMediaDescriptor
, bOasis
);
642 uno::Reference
< embed::XStorage
> xStorage( lcl_getWriteStorage( rMediaDescriptor
, m_xContext
, getMediaType(bOasis
) ) );
643 OSL_ENSURE( xStorage
.is(), "No Storage" );
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
;
658 aValue
.Name
= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Model"));
659 aValue
.Value
<<= xDocumentComp
;
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
) );
697 xInfoSet
->setPropertyValue( C2U("ExportTableNumberList"), uno::makeAny( true ));
701 if( xGraphicObjectResolver
.is())
704 uno::Sequence
< uno::Any
> aFilterProperties( nArgs
);
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
717 nWarning
|= impl_ExportStream(
718 C2U( sXML_metaStreamName
),
719 C2U( sXML_export_chart_oasis_meta_service
),
720 xStorage
, xSaxWriter
, xServiceFactory
, aFilterProperties
);
723 nWarning
|= impl_ExportStream(
724 C2U( sXML_styleStreamName
),
726 ? C2U( sXML_export_chart_oasis_styles_service
)
727 : C2U( sXML_export_chart_styles_service
),
728 xStorage
, xSaxWriter
, xServiceFactory
, aFilterProperties
);
731 sal_Int32 nContentWarning
= impl_ExportStream(
732 C2U( sXML_contentStreamName
),
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
);
743 uno::Reference
<embed::XTransactedObject
> xTransact( xStorage
,uno::UNO_QUERY
);
744 if ( xTransact
.is() )
747 catch( uno::Exception
& ex
)
749 ASSERT_EXCEPTION( ex
);
751 // something went awry
752 nWarning
= ERRCODE_SFX_GENERAL
;
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
;
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
) );
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
);
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" );
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
);
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
);
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
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
;