xref: /trunk/main/xmloff/source/core/xmlexp.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 
31 #ifdef PRECOMPILED
32 #include "filt_pch.hxx"
33 #endif
34 #include "unointerfacetouniqueidentifiermapper.hxx"
35 #include <osl/mutex.hxx>
36 #include <rtl/uuid.h>
37 #include <tools/debug.hxx>
38 #include <tools/urlobj.hxx>
39 #include <comphelper/genericpropertyset.hxx>
40 #include <com/sun/star/container/XNameAccess.hpp>
41 #include <com/sun/star/io/XInputStream.hpp>
42 #include <com/sun/star/document/XBinaryStreamResolver.hpp>
43 #include <com/sun/star/xml/sax/SAXInvalidCharacterException.hpp>
44 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
45 #include <com/sun/star/uri/UriReferenceFactory.hpp>
46 #include <comphelper/processfactory.hxx>
47 #include <comphelper/configurationhelper.hxx>
48 #include <xmloff/attrlist.hxx>
49 #include <xmloff/nmspmap.hxx>
50 #include <xmloff/xmluconv.hxx>
51 #include "xmloff/xmlnmspe.hxx"
52 #include <xmloff/xmltoken.hxx>
53 #include <xmloff/xmlexp.hxx>
54 #include <xmloff/xmlnumfe.hxx>
55 #include <xmloff/xmlmetae.hxx>
56 #include <xmloff/XMLSettingsExportContext.hxx>
57 #include <xmloff/families.hxx>
58 #include <xmloff/XMLEventExport.hxx>
59 #include "XMLStarBasicExportHandler.hxx"
60 #include "XMLScriptExportHandler.hxx"
61 #include <xmloff/SettingsExportHelper.hxx>
62 #include <com/sun/star/container/XNameAccess.hpp>
63 #include <com/sun/star/container/XIndexContainer.hpp>
64 #include <com/sun/star/document/XEventsSupplier.hpp>
65 #include <com/sun/star/document/XViewDataSupplier.hpp>
66 #include <xmloff/GradientStyle.hxx>
67 #include <xmloff/HatchStyle.hxx>
68 #include <xmloff/ImageStyle.hxx>
69 #include <TransGradientStyle.hxx>
70 #include <xmloff/MarkerStyle.hxx>
71 #include <xmloff/DashStyle.hxx>
72 #include <xmloff/XMLFontAutoStylePool.hxx>
73 #include "XMLImageMapExport.hxx"
74 #include "XMLBase64Export.hxx"
75 #include "xmloff/xmlerror.hxx"
76 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
77 #include <com/sun/star/beans/PropertyAttribute.hpp>
78 #include "xmloff/XMLFilterServiceNames.h"
79 #include "xmloff/XMLEmbeddedObjectExportFilter.hxx"
80 #include "XMLBasicExportFilter.hxx"
81 #include <vos/mutex.hxx>
82 #include <rtl/logfile.hxx>
83 #include <cppuhelper/implbase1.hxx>
84 #include <comphelper/extract.hxx>
85 #include "PropertySetMerger.hxx"
86 
87 #include "svl/urihelper.hxx"
88 #include "xmloff/xformsexport.hxx"
89 
90 #include <unotools/docinfohelper.hxx>
91 #include <unotools/bootstrap.hxx>
92 #include <unotools/configmgr.hxx>
93 #include <tools/inetdef.hxx>
94 #include <com/sun/star/document/XDocumentProperties.hpp>
95 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
96 
97 #include <com/sun/star/rdf/XMetadatable.hpp>
98 #include "RDFaExportHelper.hxx"
99 
100 
101 using ::rtl::OUString;
102 
103 using namespace ::osl;
104 using namespace ::com::sun::star;
105 using namespace ::com::sun::star::uno;
106 using namespace ::com::sun::star::frame;
107 using namespace ::com::sun::star::container;
108 using namespace ::com::sun::star::lang;
109 using namespace ::com::sun::star::document;
110 using namespace ::com::sun::star::beans;
111 using namespace ::com::sun::star::xml::sax;
112 using namespace ::com::sun::star::io;
113 using namespace ::xmloff::token;
114 
115 sal_Char __READONLY_DATA sXML_1_1[] = "1.1";
116 sal_Char __READONLY_DATA sXML_1_2[] = "1.2";
117 
118 const sal_Char *sOpenOfficeOrgProject ="OpenOffice.org_project";
119 
120 // #i115030#: the XSLT is not finished, and not available via HTTP
121 const sal_Char s_grddl_xsl[] = "http://FIXME";
122 
123 #define LOGFILE_AUTHOR "mb93740"
124 
125 #define XML_MODEL_SERVICE_WRITER    "com.sun.star.text.TextDocument"
126 #define XML_MODEL_SERVICE_CALC      "com.sun.star.sheet.SpreadsheetDocument"
127 #define XML_MODEL_SERVICE_DRAW      "com.sun.star.drawing.DrawingDocument"
128 #define XML_MODEL_SERVICE_IMPRESS   "com.sun.star.presentation.PresentationDocument"
129 #define XML_MODEL_SERVICE_MATH      "com.sun.star.formula.FormulaProperties"
130 #define XML_MODEL_SERVICE_CHART     "com.sun.star.chart.ChartDocument"
131 
132 #define XML_USEPRETTYPRINTING       "UsePrettyPrinting"
133 
134 #define C2U(cChar) OUString( RTL_CONSTASCII_USTRINGPARAM(cChar) )
135 
136 struct XMLServiceMapEntry_Impl
137 {
138     const sal_Char *sModelService;
139     sal_Int32      nModelServiceLen;
140     const sal_Char *sFilterService;
141     sal_Int32      nFilterServiceLen;
142 };
143 
144 #define SERVICE_MAP_ENTRY( app ) \
145     { XML_MODEL_SERVICE_##app, sizeof(XML_MODEL_SERVICE_##app)-1, \
146       XML_EXPORT_FILTER_##app, sizeof(XML_EXPORT_FILTER_##app)-1 }
147 
148 const XMLServiceMapEntry_Impl aServiceMap[] =
149 {
150     SERVICE_MAP_ENTRY( WRITER ),
151     SERVICE_MAP_ENTRY( CALC ),
152     SERVICE_MAP_ENTRY( IMPRESS ),// Impress supports DrawingDocument, too, so
153     SERVICE_MAP_ENTRY( DRAW ),   // it must appear before Draw
154     SERVICE_MAP_ENTRY( MATH ),
155     SERVICE_MAP_ENTRY( CHART ),
156     { 0, 0, 0, 0 }
157 };
158 
159 //==============================================================================
160 
161 class SAL_DLLPRIVATE SettingsExportFacade : public ::xmloff::XMLSettingsExportContext
162 {
163 public:
164     SettingsExportFacade( SvXMLExport& i_rExport )
165         :m_rExport( i_rExport )
166     {
167     }
168 
169     virtual ~SettingsExportFacade()
170     {
171     }
172 
173     virtual void    AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName,
174                                   const ::rtl::OUString& i_rValue );
175     virtual void    AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName,
176                                   enum ::xmloff::token::XMLTokenEnum i_eValue );
177 
178     virtual void    StartElement( enum ::xmloff::token::XMLTokenEnum i_eName,
179                                   const sal_Bool i_bIgnoreWhitespace );
180     virtual void    EndElement(   const sal_Bool i_bIgnoreWhitespace );
181 
182     virtual void    Characters( const ::rtl::OUString& i_rCharacters );
183 
184     virtual ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
185                     GetServiceFactory() const;
186 private:
187     SvXMLExport&                    m_rExport;
188     ::std::stack< ::rtl::OUString > m_aElements;
189 };
190 
191 void SettingsExportFacade::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, const ::rtl::OUString& i_rValue )
192 {
193     m_rExport.AddAttribute( XML_NAMESPACE_CONFIG, i_eName, i_rValue );
194 }
195 
196 void SettingsExportFacade::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, enum ::xmloff::token::XMLTokenEnum i_eValue )
197 {
198     m_rExport.AddAttribute( XML_NAMESPACE_CONFIG, i_eName, i_eValue );
199 }
200 
201 void SettingsExportFacade::StartElement( enum ::xmloff::token::XMLTokenEnum i_eName, const sal_Bool i_bIgnoreWhitespace )
202 {
203     const ::rtl::OUString sElementName( m_rExport.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_CONFIG, GetXMLToken( i_eName ) ) );
204     m_rExport.StartElement( sElementName, i_bIgnoreWhitespace );
205     m_aElements.push( sElementName );
206 }
207 
208 void SettingsExportFacade::EndElement( const sal_Bool i_bIgnoreWhitespace )
209 {
210     const ::rtl::OUString sElementName( m_aElements.top() );
211     m_rExport.EndElement( sElementName, i_bIgnoreWhitespace );
212     m_aElements.pop();
213 }
214 
215 void SettingsExportFacade::Characters( const ::rtl::OUString& i_rCharacters )
216 {
217     m_rExport.GetDocHandler()->characters( i_rCharacters );
218 }
219 
220 Reference< XMultiServiceFactory > SettingsExportFacade::GetServiceFactory() const
221 {
222     return m_rExport.getServiceFactory();
223 }
224 
225 //==============================================================================
226 
227 class SvXMLExportEventListener : public cppu::WeakImplHelper1<
228                             com::sun::star::lang::XEventListener >
229 {
230 private:
231     SvXMLExport*    pExport;
232 
233 public:
234                             SvXMLExportEventListener(SvXMLExport* pExport);
235     virtual                 ~SvXMLExportEventListener();
236 
237                             // XEventListener
238     virtual void SAL_CALL disposing(const lang::EventObject& rEventObject) throw(::com::sun::star::uno::RuntimeException);
239 };
240 
241 SvXMLExportEventListener::SvXMLExportEventListener(SvXMLExport* pTempExport)
242     : pExport(pTempExport)
243 {
244 }
245 
246 SvXMLExportEventListener::~SvXMLExportEventListener()
247 {
248 }
249 
250 // XEventListener
251 void SAL_CALL SvXMLExportEventListener::disposing( const lang::EventObject& )
252     throw(uno::RuntimeException)
253 {
254     if (pExport)
255     {
256         pExport->DisposingModel();
257         pExport = NULL;
258     }
259 }
260 
261 //==============================================================================
262 
263 class SvXMLExport_Impl
264 {
265 public:
266     SvXMLExport_Impl();
267 
268     ::comphelper::UnoInterfaceToUniqueIdentifierMapper  maInterfaceToIdentifierMapper;
269     uno::Reference< uri::XUriReferenceFactory >         mxUriReferenceFactory;
270     rtl::OUString                                       msPackageURI;
271     rtl::OUString                                       msPackageURIScheme;
272     // --> OD 2006-09-27 #i69627#
273     sal_Bool                                            mbOutlineStyleAsNormalListStyle;
274     // <--
275     // --> PB 2007-07-06 #i146851#
276     sal_Bool                                            mbSaveBackwardCompatibleODF;
277     // <--
278 
279     uno::Reference< embed::XStorage >                   mxTargetStorage;
280 
281     SvtSaveOptions                                      maSaveOptions;
282 
283     /// relative path of stream in package, e.g. "someobject/content.xml"
284     ::rtl::OUString mStreamPath;
285 
286     const uno::Reference< uno::XComponentContext > mxComponentContext;
287 
288     /// name of stream in package, e.g., "content.xml"
289     ::rtl::OUString mStreamName;
290 
291     /// stack of backed up namespace maps
292     /// long: depth at which namespace map has been backed up into the stack
293     ::std::stack< ::std::pair< SvXMLNamespaceMap *, long > > mNamespaceMaps;
294     /// counts depth (number of open elements/start tags)
295     long mDepth;
296 
297     ::std::auto_ptr< ::xmloff::RDFaExportHelper> mpRDFaHelper;
298 
299     // --> OD 2008-11-26 #158694#
300     sal_Bool                                            mbExportTextNumberElement;
301     // <--
302     sal_Bool                                            mbNullDateInitialized;
303 
304     void SetSchemeOf( const ::rtl::OUString& rOrigFileName )
305     {
306         sal_Int32 nSep = rOrigFileName.indexOf(':');
307         if( nSep != -1 )
308             msPackageURIScheme = rOrigFileName.copy( 0, nSep );
309     }
310 };
311 
312 SvXMLExport_Impl::SvXMLExport_Impl()
313     // --> OD 2006-09-27 #i69627#
314     : mbOutlineStyleAsNormalListStyle( false )
315     // <--
316     // --> PB 2007-07-06 #i146851#
317         ,mbSaveBackwardCompatibleODF( sal_True )
318     // <--
319         ,mxComponentContext( ::comphelper::getProcessComponentContext() )
320         ,mStreamName()
321         ,mNamespaceMaps()
322         ,mDepth(0)
323         ,mpRDFaHelper() // lazy
324     // --> OD 2008-11-26 #158694#
325         ,mbExportTextNumberElement( sal_False )
326     // <--
327         ,mbNullDateInitialized( sal_False )
328 {
329     OSL_ENSURE(mxComponentContext.is(), "SvXMLExport: no ComponentContext");
330     if (!mxComponentContext.is()) throw uno::RuntimeException();
331     mxUriReferenceFactory = uri::UriReferenceFactory::create(
332         mxComponentContext );
333 }
334 
335 //==============================================================================
336 
337 void SvXMLExport::SetDocHandler( const uno::Reference< xml::sax::XDocumentHandler > &rHandler )
338 {
339     mxHandler = rHandler;
340     mxExtHandler = uno::Reference<xml::sax::XExtendedDocumentHandler>( mxHandler, UNO_QUERY );
341 }
342 
343 void SvXMLExport::_InitCtor()
344 {
345     // note: it is not necessary to add XML_NP_XML (it is declared implicitly)
346     if( (getExportFlags() & ~EXPORT_OASIS) != 0 )
347     {
348         mpNamespaceMap->Add( GetXMLToken(XML_NP_OFFICE), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
349         mpNamespaceMap->Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
350     }
351     if( (getExportFlags() & (EXPORT_STYLES|EXPORT_MASTERSTYLES|EXPORT_AUTOSTYLES|EXPORT_FONTDECLS) ) != 0 )
352     {
353         mpNamespaceMap->Add( GetXMLToken(XML_NP_FO), GetXMLToken(XML_N_FO_COMPAT), XML_NAMESPACE_FO );
354     }
355     if( (getExportFlags() & (EXPORT_META|EXPORT_STYLES|EXPORT_MASTERSTYLES|EXPORT_AUTOSTYLES|EXPORT_CONTENT|EXPORT_SCRIPTS|EXPORT_SETTINGS) ) != 0 )
356     {
357         mpNamespaceMap->Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
358     }
359     if( (getExportFlags() & EXPORT_SETTINGS) != 0 )
360     {
361         mpNamespaceMap->Add( GetXMLToken(XML_NP_CONFIG), GetXMLToken(XML_N_CONFIG), XML_NAMESPACE_CONFIG );
362     }
363 
364     if( (getExportFlags() & (EXPORT_META|EXPORT_MASTERSTYLES|EXPORT_CONTENT) ) != 0 )
365     {
366         mpNamespaceMap->Add( GetXMLToken(XML_NP_DC), GetXMLToken(XML_N_DC), XML_NAMESPACE_DC );
367         mpNamespaceMap->Add( GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META), XML_NAMESPACE_META );
368     }
369     if( (getExportFlags() & (EXPORT_STYLES|EXPORT_MASTERSTYLES|EXPORT_AUTOSTYLES|EXPORT_CONTENT|EXPORT_FONTDECLS) ) != 0 )
370     {
371         mpNamespaceMap->Add( GetXMLToken(XML_NP_STYLE), GetXMLToken(XML_N_STYLE), XML_NAMESPACE_STYLE );
372     }
373 
374     // namespaces for documents
375     if( (getExportFlags() & (EXPORT_STYLES|EXPORT_AUTOSTYLES|EXPORT_MASTERSTYLES|EXPORT_CONTENT) ) != 0 )
376     {
377         mpNamespaceMap->Add( GetXMLToken(XML_NP_DC),    GetXMLToken(XML_N_DC),      XML_NAMESPACE_DC );
378         mpNamespaceMap->Add( GetXMLToken(XML_NP_TEXT),  GetXMLToken(XML_N_TEXT),    XML_NAMESPACE_TEXT );
379         mpNamespaceMap->Add( GetXMLToken(XML_NP_DRAW),  GetXMLToken(XML_N_DRAW),    XML_NAMESPACE_DRAW );
380         mpNamespaceMap->Add( GetXMLToken(XML_NP_DR3D),  GetXMLToken(XML_N_DR3D),    XML_NAMESPACE_DR3D );
381         mpNamespaceMap->Add( GetXMLToken(XML_NP_SVG),   GetXMLToken(XML_N_SVG_COMPAT),  XML_NAMESPACE_SVG );
382         mpNamespaceMap->Add( GetXMLToken(XML_NP_CHART), GetXMLToken(XML_N_CHART),   XML_NAMESPACE_CHART );
383         mpNamespaceMap->Add( GetXMLToken(XML_NP_RPT),   GetXMLToken(XML_N_RPT),     XML_NAMESPACE_REPORT );
384         mpNamespaceMap->Add( GetXMLToken(XML_NP_TABLE), GetXMLToken(XML_N_TABLE),   XML_NAMESPACE_TABLE );
385         mpNamespaceMap->Add( GetXMLToken(XML_NP_NUMBER),GetXMLToken(XML_N_NUMBER),  XML_NAMESPACE_NUMBER );
386         mpNamespaceMap->Add( GetXMLToken(XML_NP_OOOW),  GetXMLToken(XML_N_OOOW),    XML_NAMESPACE_OOOW );
387         mpNamespaceMap->Add( GetXMLToken(XML_NP_OOOC),  GetXMLToken(XML_N_OOOC),    XML_NAMESPACE_OOOC );
388         mpNamespaceMap->Add( GetXMLToken(XML_NP_OF),    GetXMLToken(XML_N_OF),      XML_NAMESPACE_OF );
389 
390         if (getDefaultVersion() > SvtSaveOptions::ODFVER_012)
391         {
392             mpNamespaceMap->Add(
393                 GetXMLToken(XML_NP_TABLE_EXT), GetXMLToken(XML_N_TABLE_EXT), XML_NAMESPACE_TABLE_EXT);
394         }
395     }
396     if( (getExportFlags() & (EXPORT_MASTERSTYLES|EXPORT_CONTENT) ) != 0 )
397     {
398         mpNamespaceMap->Add( GetXMLToken(XML_NP_MATH), GetXMLToken(XML_N_MATH), XML_NAMESPACE_MATH );
399         mpNamespaceMap->Add( GetXMLToken(XML_NP_FORM), GetXMLToken(XML_N_FORM), XML_NAMESPACE_FORM );
400     }
401     if( (getExportFlags() & (EXPORT_STYLES|EXPORT_AUTOSTYLES|EXPORT_MASTERSTYLES|EXPORT_CONTENT|EXPORT_SCRIPTS) ) != 0 )
402     {
403         mpNamespaceMap->Add( GetXMLToken(XML_NP_SCRIPT), GetXMLToken(XML_N_SCRIPT), XML_NAMESPACE_SCRIPT );
404         mpNamespaceMap->Add( GetXMLToken(XML_NP_DOM), GetXMLToken(XML_N_DOM), XML_NAMESPACE_DOM );
405     }
406     if( (getExportFlags() & EXPORT_CONTENT ) != 0 )
407     {
408         mpNamespaceMap->Add( GetXMLToken(XML_NP_XFORMS_1_0), GetXMLToken(XML_N_XFORMS_1_0), XML_NAMESPACE_XFORMS );
409         mpNamespaceMap->Add( GetXMLToken(XML_NP_XSD), GetXMLToken(XML_N_XSD), XML_NAMESPACE_XSD );
410         mpNamespaceMap->Add( GetXMLToken(XML_NP_XSI), GetXMLToken(XML_N_XSI), XML_NAMESPACE_XSI );
411         mpNamespaceMap->Add( GetXMLToken(XML_NP_FIELD), GetXMLToken(XML_N_FIELD), XML_NAMESPACE_FIELD );
412     }
413     // RDFa: needed for content and header/footer styles
414     if( (getExportFlags() & (EXPORT_STYLES|EXPORT_AUTOSTYLES|EXPORT_MASTERSTYLES|EXPORT_CONTENT) ) != 0 )
415     {
416         mpNamespaceMap->Add( GetXMLToken(XML_NP_XHTML),
417             GetXMLToken(XML_N_XHTML), XML_NAMESPACE_XHTML );
418     }
419     // GRDDL: to convert RDFa and meta.xml to RDF
420     if( (getExportFlags() & (EXPORT_META|EXPORT_STYLES|EXPORT_AUTOSTYLES|EXPORT_MASTERSTYLES|EXPORT_CONTENT) ) != 0 )
421     {
422         mpNamespaceMap->Add( GetXMLToken(XML_NP_GRDDL),
423             GetXMLToken(XML_N_GRDDL), XML_NAMESPACE_GRDDL );
424     }
425 
426     mxAttrList = (xml::sax::XAttributeList*)mpAttrList;
427 
428     msPicturesPath = OUString( RTL_CONSTASCII_USTRINGPARAM( "#Pictures/" ) );
429     msObjectsPath = OUString( RTL_CONSTASCII_USTRINGPARAM( "#./" ) );
430     msGraphicObjectProtocol = OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.GraphicObject:" ) );
431     msEmbeddedObjectProtocol = OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.EmbeddedObject:" ) );
432 
433     if (mxModel.is() && !mxEventListener.is())
434     {
435         mxEventListener.set( new SvXMLExportEventListener(this));
436         mxModel->addEventListener(mxEventListener);
437     }
438 
439     // --> OD 2006-03-10 #i51726# - determine model type
440     _DetermineModelType();
441     // <--
442 
443     mbEnableExperimentalOdfExport = getenv("ENABLE_EXPERIMENTAL_ODF_EXPORT") != NULL;
444 
445     // --> PB 2007-07-06 #146851# - load mbSaveBackwardCompatibleODF from configuration
446 
447     // cl: but only if we do export to current oasis format, old openoffice format *must* always be compatible
448     if( (getExportFlags() & EXPORT_OASIS) != 0 )
449     {
450         sal_Bool bTemp = sal_True;
451         if ( ::comphelper::ConfigurationHelper::readDirectKey(
452                 getServiceFactory(),
453                 C2U("org.openoffice.Office.Common/"), C2U("Save/Document"), C2U("SaveBackwardCompatibleODF"),
454                 ::comphelper::ConfigurationHelper::E_READONLY ) >>= bTemp )
455         {
456             mpImpl->mbSaveBackwardCompatibleODF = bTemp;
457         }
458     }
459     // <--
460 }
461 
462 // --> OD 2006-03-14 #i51726#
463 void SvXMLExport::_DetermineModelType()
464 {
465     meModelType = SvtModuleOptions::E_UNKNOWN_FACTORY;
466 
467     if ( mxModel.is() )
468     {
469         meModelType = SvtModuleOptions::ClassifyFactoryByModel( mxModel );
470     }
471 }
472 // <--
473 
474 // #110680#
475 SvXMLExport::SvXMLExport(
476     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
477     MapUnit eDfltUnit, const enum XMLTokenEnum eClass, sal_uInt16 nExportFlags )
478 :   mpImpl( new SvXMLExport_Impl ),
479     // #110680#
480     mxServiceFactory(xServiceFactory),
481     mpAttrList( new SvXMLAttributeList ),
482     mpNamespaceMap( new SvXMLNamespaceMap ),
483     // #110680#
484     mpUnitConv( new SvXMLUnitConverter( MAP_100TH_MM, eDfltUnit, getServiceFactory() ) ),
485     mpNumExport(0L),
486     mpProgressBarHelper( NULL ),
487     mpEventExport( NULL ),
488     mpImageMapExport( NULL ),
489     mpXMLErrors( NULL ),
490     mbExtended( sal_False ),
491     meClass( eClass ),
492     mnExportFlags( nExportFlags ),
493     mnErrorFlags( ERROR_NO ),
494     msWS( GetXMLToken(XML_WS) ),
495     mbSaveLinkedSections(sal_True)
496 {
497     DBG_ASSERT( mxServiceFactory.is(), "got no service manager" );
498     _InitCtor();
499 }
500 
501 // #110680#
502 SvXMLExport::SvXMLExport(
503     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
504     const OUString &rFileName,
505     const uno::Reference< xml::sax::XDocumentHandler > & rHandler,
506     MapUnit eDfltUnit   )
507 :   mpImpl( new SvXMLExport_Impl ),
508     // #110680#
509     mxServiceFactory(xServiceFactory),
510     mxHandler( rHandler ),
511     mxExtHandler( rHandler, uno::UNO_QUERY ),
512     mpAttrList( new SvXMLAttributeList ),
513     msOrigFileName( rFileName ),
514     mpNamespaceMap( new SvXMLNamespaceMap ),
515     // #110680#
516     mpUnitConv( new SvXMLUnitConverter( MAP_100TH_MM, eDfltUnit, getServiceFactory() ) ),
517     mpNumExport(0L),
518     mpProgressBarHelper( NULL ),
519     mpEventExport( NULL ),
520     mpImageMapExport( NULL ),
521     mpXMLErrors( NULL ),
522     mbExtended( sal_False ),
523     meClass( XML_TOKEN_INVALID ),
524     mnExportFlags( 0 ),
525     mnErrorFlags( ERROR_NO ),
526     msWS( GetXMLToken(XML_WS) ),
527     mbSaveLinkedSections(sal_True)
528 {
529     mpImpl->SetSchemeOf( msOrigFileName );
530     DBG_ASSERT( mxServiceFactory.is(), "got no service manager" );
531     _InitCtor();
532 
533     if (mxNumberFormatsSupplier.is())
534         mpNumExport = new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier);
535 }
536 
537 // #110680#
538 SvXMLExport::SvXMLExport(
539     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
540     const OUString &rFileName,
541     const uno::Reference< xml::sax::XDocumentHandler > & rHandler,
542     const Reference< XModel >& rModel,
543     sal_Int16 eDfltUnit )
544 :   mpImpl( new SvXMLExport_Impl ),
545     // #110680#
546     mxServiceFactory(xServiceFactory),
547     mxModel( rModel ),
548     mxHandler( rHandler ),
549     mxExtHandler( rHandler, uno::UNO_QUERY ),
550     mxNumberFormatsSupplier (rModel, uno::UNO_QUERY),
551     mpAttrList( new SvXMLAttributeList ),
552     msOrigFileName( rFileName ),
553     mpNamespaceMap( new SvXMLNamespaceMap ),
554     // #110680#
555     // pUnitConv( new SvXMLUnitConverter( MAP_100TH_MM, SvXMLUnitConverter::GetMapUnit(eDfltUnit) ) ),
556     mpUnitConv( new SvXMLUnitConverter( MAP_100TH_MM, SvXMLUnitConverter::GetMapUnit(eDfltUnit), getServiceFactory() ) ),
557     mpNumExport(0L),
558     mpProgressBarHelper( NULL ),
559     mpEventExport( NULL ),
560     mpImageMapExport( NULL ),
561     mpXMLErrors( NULL ),
562     mbExtended( sal_False ),
563     meClass( XML_TOKEN_INVALID ),
564     mnExportFlags( 0 ),
565     mnErrorFlags( ERROR_NO ),
566     msWS( GetXMLToken(XML_WS) ),
567     mbSaveLinkedSections(sal_True)
568 {
569     mpImpl->SetSchemeOf( msOrigFileName );
570     DBG_ASSERT( mxServiceFactory.is(), "got no service manager" );
571     _InitCtor();
572 
573     if (mxNumberFormatsSupplier.is())
574         mpNumExport = new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier);
575 }
576 
577 // #110680#
578 SvXMLExport::SvXMLExport(
579     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
580     const OUString &rFileName,
581     const uno::Reference< xml::sax::XDocumentHandler > & rHandler,
582     const Reference< XModel >& rModel,
583     const Reference< document::XGraphicObjectResolver >& rEmbeddedGraphicObjects,
584     sal_Int16 eDfltUnit )
585 :   mpImpl( new SvXMLExport_Impl ),
586     // #110680#
587     mxServiceFactory(xServiceFactory),
588     mxModel( rModel ),
589     mxHandler( rHandler ),
590     mxExtHandler( rHandler, uno::UNO_QUERY ),
591     mxNumberFormatsSupplier (rModel, uno::UNO_QUERY),
592     mxGraphicResolver( rEmbeddedGraphicObjects ),
593     mpAttrList( new SvXMLAttributeList ),
594     msOrigFileName( rFileName ),
595     mpNamespaceMap( new SvXMLNamespaceMap ),
596     // #110680#
597     mpUnitConv( new SvXMLUnitConverter( MAP_100TH_MM, SvXMLUnitConverter::GetMapUnit(eDfltUnit), getServiceFactory() ) ),
598     mpNumExport(0L),
599     mpProgressBarHelper( NULL ),
600     mpEventExport( NULL ),
601     mpImageMapExport( NULL ),
602     mpXMLErrors( NULL ),
603     mbExtended( sal_False ),
604     meClass( XML_TOKEN_INVALID ),
605     mnExportFlags( 0 ),
606     mnErrorFlags( ERROR_NO ),
607     msWS( GetXMLToken(XML_WS) ),
608     mbSaveLinkedSections(sal_True)
609 {
610     mpImpl->SetSchemeOf( msOrigFileName );
611     DBG_ASSERT( mxServiceFactory.is(), "got no service manager" );
612     _InitCtor();
613 
614     if (mxNumberFormatsSupplier.is())
615         mpNumExport = new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier);
616 }
617 
618 SvXMLExport::~SvXMLExport()
619 {
620     delete mpXMLErrors;
621     delete mpImageMapExport;
622     delete mpEventExport;
623     delete mpNamespaceMap;
624     delete mpUnitConv;
625     if (mpProgressBarHelper || mpNumExport)
626     {
627         if (mxExportInfo.is())
628         {
629             uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
630             if (xPropertySetInfo.is())
631             {
632                 if (mpProgressBarHelper)
633                 {
634                     OUString sProgressMax(RTL_CONSTASCII_USTRINGPARAM(XML_PROGRESSMAX));
635                     OUString sProgressCurrent(RTL_CONSTASCII_USTRINGPARAM(XML_PROGRESSCURRENT));
636                     OUString sRepeat(RTL_CONSTASCII_USTRINGPARAM(XML_PROGRESSREPEAT));
637                     if (xPropertySetInfo->hasPropertyByName(sProgressMax) &&
638                         xPropertySetInfo->hasPropertyByName(sProgressCurrent))
639                     {
640                         sal_Int32 nProgressMax(mpProgressBarHelper->GetReference());
641                         sal_Int32 nProgressCurrent(mpProgressBarHelper->GetValue());
642                         uno::Any aAny;
643                         aAny <<= nProgressMax;
644                         mxExportInfo->setPropertyValue(sProgressMax, aAny);
645                         aAny <<= nProgressCurrent;
646                         mxExportInfo->setPropertyValue(sProgressCurrent, aAny);
647                     }
648                     if (xPropertySetInfo->hasPropertyByName(sRepeat))
649                         mxExportInfo->setPropertyValue(sRepeat, cppu::bool2any(mpProgressBarHelper->GetRepeat()));
650                 }
651                 if (mpNumExport && (mnExportFlags & (EXPORT_AUTOSTYLES | EXPORT_STYLES)))
652                 {
653                     OUString sWrittenNumberFormats(RTL_CONSTASCII_USTRINGPARAM(XML_WRITTENNUMBERSTYLES));
654                     if (xPropertySetInfo->hasPropertyByName(sWrittenNumberFormats))
655                     {
656                         uno::Sequence<sal_Int32> aWasUsed;
657                         mpNumExport->GetWasUsed(aWasUsed);
658                         uno::Any aAny;
659                         aAny <<= aWasUsed;
660                         mxExportInfo->setPropertyValue(sWrittenNumberFormats, aAny);
661                     }
662                 }
663             }
664         }
665         delete mpProgressBarHelper;
666         delete mpNumExport;
667     }
668 
669     xmloff::token::ResetTokens();
670 
671     if (mxEventListener.is() && mxModel.is())
672         mxModel->removeEventListener(mxEventListener);
673 
674     delete mpImpl;
675 }
676 
677 ///////////////////////////////////////////////////////////////////////
678 
679 // XExporter
680 void SAL_CALL SvXMLExport::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
681     throw(lang::IllegalArgumentException, uno::RuntimeException)
682 {
683     mxModel = uno::Reference< frame::XModel >::query( xDoc );
684     if( !mxModel.is() )
685         throw lang::IllegalArgumentException();
686     if (mxModel.is() && ! mxEventListener.is())
687     {
688         mxEventListener.set( new SvXMLExportEventListener(this));
689         mxModel->addEventListener(mxEventListener);
690     }
691 
692     if(!mxNumberFormatsSupplier.is() )
693     {
694         mxNumberFormatsSupplier = mxNumberFormatsSupplier.query( mxModel );
695         if(mxNumberFormatsSupplier.is() && mxHandler.is())
696             mpNumExport = new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier);
697     }
698     if (mxExportInfo.is())
699     {
700         uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
701         if (xPropertySetInfo.is())
702         {
703             OUString sUsePrettyPrinting(RTL_CONSTASCII_USTRINGPARAM(XML_USEPRETTYPRINTING));
704             if (xPropertySetInfo->hasPropertyByName(sUsePrettyPrinting))
705             {
706                 uno::Any aAny = mxExportInfo->getPropertyValue(sUsePrettyPrinting);
707                 if (::cppu::any2bool(aAny))
708                     mnExportFlags |= EXPORT_PRETTY;
709                 else
710                     mnExportFlags &= ~EXPORT_PRETTY;
711             }
712 
713             if (mpNumExport && (mnExportFlags & (EXPORT_AUTOSTYLES | EXPORT_STYLES)))
714             {
715                 OUString sWrittenNumberFormats(RTL_CONSTASCII_USTRINGPARAM(XML_WRITTENNUMBERSTYLES));
716                 if (xPropertySetInfo->hasPropertyByName(sWrittenNumberFormats))
717                 {
718                     uno::Any aAny = mxExportInfo->getPropertyValue(sWrittenNumberFormats);
719                     uno::Sequence<sal_Int32> aWasUsed;
720                     if(aAny >>= aWasUsed)
721                         mpNumExport->SetWasUsed(aWasUsed);
722                 }
723             }
724         }
725     }
726 
727     // --> PB 2007-07-06 #i146851#
728     if ( mpImpl->mbSaveBackwardCompatibleODF )
729         mnExportFlags |= EXPORT_SAVEBACKWARDCOMPATIBLE;
730     else
731         mnExportFlags &= ~EXPORT_SAVEBACKWARDCOMPATIBLE;
732     // <--
733 
734     // namespaces for user defined attributes
735     Reference< XMultiServiceFactory > xFactory( mxModel,    UNO_QUERY );
736     if( xFactory.is() )
737     {
738         try
739         {
740             Reference < XInterface > xIfc =
741                 xFactory->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM(
742                                             "com.sun.star.xml.NamespaceMap")) );
743             if( xIfc.is() )
744             {
745                 Reference< XNameAccess > xNamespaceMap( xIfc, UNO_QUERY );
746                 if( xNamespaceMap.is() )
747                 {
748                     Sequence< OUString > aPrefixes( xNamespaceMap->getElementNames() );
749 
750                     OUString* pPrefix = aPrefixes.getArray();
751                     const sal_Int32 nCount = aPrefixes.getLength();
752                     sal_Int32 nIndex;
753                     OUString aURL;
754 
755                     for( nIndex = 0; nIndex < nCount; ++nIndex, ++pPrefix )
756                     {
757                         if( xNamespaceMap->getByName( *pPrefix ) >>= aURL )
758                             _GetNamespaceMap().Add( *pPrefix, aURL, XML_NAMESPACE_UNKNOWN );
759                     }
760                 }
761             }
762         }
763         catch( com::sun::star::uno::Exception& )
764         {
765         }
766     }
767 
768     // --> OD 2006-03-10 #i51726# - determine model type
769     _DetermineModelType();
770     // <--
771 }
772 
773 // XInitialize
774 void SAL_CALL SvXMLExport::initialize( const uno::Sequence< uno::Any >& aArguments )
775     throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
776 {
777     // #93186# we need to queryInterface every single Any with any expected outcome. This variable hold the queryInterface results.
778 
779     const sal_Int32 nAnyCount = aArguments.getLength();
780     const uno::Any* pAny = aArguments.getConstArray();
781 
782     for( sal_Int32 nIndex = 0; nIndex < nAnyCount; nIndex++, pAny++ )
783     {
784         Reference<XInterface> xValue;
785         *pAny >>= xValue;
786 
787         // status indicator
788         uno::Reference<task::XStatusIndicator> xTmpStatus( xValue, UNO_QUERY );
789         if ( xTmpStatus.is() )
790             mxStatusIndicator = xTmpStatus;
791 
792         // graphic resolver
793         uno::Reference<document::XGraphicObjectResolver> xTmpGraphic(
794             xValue, UNO_QUERY );
795         if ( xTmpGraphic.is() )
796             mxGraphicResolver = xTmpGraphic;
797 
798         // object resolver
799         uno::Reference<document::XEmbeddedObjectResolver> xTmpObjectResolver(
800             xValue, UNO_QUERY );
801         if ( xTmpObjectResolver.is() )
802             mxEmbeddedResolver = xTmpObjectResolver;
803 
804         // document handler
805         uno::Reference<xml::sax::XDocumentHandler> xTmpDocHandler(
806             xValue, UNO_QUERY );
807         if( xTmpDocHandler.is() )
808         {
809             mxHandler = xTmpDocHandler;
810             *pAny >>= mxExtHandler;
811 
812             if (mxNumberFormatsSupplier.is() && mpNumExport == NULL)
813                 mpNumExport = new SvXMLNumFmtExport(*this, mxNumberFormatsSupplier);
814         }
815 
816         // property set to transport data across
817         uno::Reference<beans::XPropertySet> xTmpPropertySet(
818             xValue, UNO_QUERY );
819         if( xTmpPropertySet.is() )
820             mxExportInfo = xTmpPropertySet;
821     }
822 
823     if( mxExportInfo.is() )
824     {
825         uno::Reference< beans::XPropertySetInfo > xPropertySetInfo =
826             mxExportInfo->getPropertySetInfo();
827         OUString sPropName(
828                 RTL_CONSTASCII_USTRINGPARAM("BaseURI" ) );
829         if( xPropertySetInfo->hasPropertyByName(sPropName) )
830         {
831             uno::Any aAny = mxExportInfo->getPropertyValue(sPropName);
832             aAny >>= msOrigFileName;
833             mpImpl->msPackageURI = msOrigFileName;
834             mpImpl->SetSchemeOf( msOrigFileName );
835         }
836         OUString sRelPath;
837         sPropName = OUString( RTL_CONSTASCII_USTRINGPARAM("StreamRelPath" ) );
838         if( xPropertySetInfo->hasPropertyByName(sPropName) )
839         {
840             uno::Any aAny = mxExportInfo->getPropertyValue(sPropName);
841             aAny >>= sRelPath;
842         }
843         OUString sName;
844         sPropName = OUString( RTL_CONSTASCII_USTRINGPARAM("StreamName" ) );
845         if( xPropertySetInfo->hasPropertyByName(sPropName) )
846         {
847             uno::Any aAny = mxExportInfo->getPropertyValue(sPropName);
848             aAny >>= sName;
849         }
850         if( msOrigFileName.getLength() && sName.getLength() )
851         {
852             INetURLObject aBaseURL( msOrigFileName );
853             if( sRelPath.getLength() )
854                 aBaseURL.insertName( sRelPath );
855             aBaseURL.insertName( sName );
856             msOrigFileName = aBaseURL.GetMainURL(INetURLObject::DECODE_TO_IURI);
857         }
858         mpImpl->mStreamName = sName; // Note: may be empty (XSLT)
859 
860         // --> OD 2006-09-26 #i69627#
861         const ::rtl::OUString sOutlineStyleAsNormalListStyle(
862                 RTL_CONSTASCII_USTRINGPARAM("OutlineStyleAsNormalListStyle") );
863         if( xPropertySetInfo->hasPropertyByName( sOutlineStyleAsNormalListStyle ) )
864         {
865             uno::Any aAny = mxExportInfo->getPropertyValue( sOutlineStyleAsNormalListStyle );
866             aAny >>= (mpImpl->mbOutlineStyleAsNormalListStyle);
867         }
868         // <--
869 
870         OUString sTargetStorage( RTL_CONSTASCII_USTRINGPARAM("TargetStorage") );
871         if( xPropertySetInfo->hasPropertyByName( sTargetStorage ) )
872             mxExportInfo->getPropertyValue( sTargetStorage ) >>= mpImpl->mxTargetStorage;
873 
874         // --> OD 2008-11-26 #158694#
875         const ::rtl::OUString sExportTextNumberElement(
876                 RTL_CONSTASCII_USTRINGPARAM("ExportTextNumberElement") );
877         if( xPropertySetInfo->hasPropertyByName( sExportTextNumberElement ) )
878         {
879             uno::Any aAny = mxExportInfo->getPropertyValue( sExportTextNumberElement );
880             aAny >>= (mpImpl->mbExportTextNumberElement);
881         }
882         // <--
883     }
884 
885 }
886 
887 // XFilter
888 sal_Bool SAL_CALL SvXMLExport::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor ) throw(uno::RuntimeException)
889 {
890     RTL_LOGFILE_CONTEXT_AUTHOR( aLogContext, "xmloff", LOGFILE_AUTHOR,
891                                 "SvXMLExport::filter" );
892 
893     // check for xHandler first... should have been supplied in initialize
894     if( !mxHandler.is() )
895         return sal_False;
896 
897     try
898     {
899         const sal_uInt32 nTest =
900             EXPORT_META|EXPORT_STYLES|EXPORT_CONTENT|EXPORT_SETTINGS;
901         if( (mnExportFlags & nTest) == nTest && !msOrigFileName.getLength() )
902         {
903             // evaluate descriptor only for flat files and if a base URI
904             // has not been provided already
905             const sal_Int32 nPropCount = aDescriptor.getLength();
906             const beans::PropertyValue* pProps = aDescriptor.getConstArray();
907 
908             for( sal_Int32 nIndex = 0; nIndex < nPropCount; nIndex++, pProps++ )
909             {
910                 const OUString& rPropName = pProps->Name;
911                 const Any& rValue = pProps->Value;
912 
913                 if( rPropName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FileName" ) ) )
914                 {
915                     if( !(rValue >>= msOrigFileName ) )
916                         return sal_False;
917                 }
918                 else if (rPropName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FilterName" ) ) )
919                 {
920                     if( !(rValue >>= msFilterName ) )
921                         return sal_False;
922                 }
923             }
924         }
925 
926 #ifdef TIMELOG
927         if (GetModel().is())
928         {
929             // print a trace message with the URL
930             ByteString aUrl( (String) GetModel()->getURL(),
931                              RTL_TEXTENCODING_ASCII_US );
932             RTL_LOGFILE_CONTEXT_TRACE1( aLogContext, "%s", aUrl.GetBuffer() );
933 
934             // we also want a trace message with the document class
935             ByteString aClass( (String)GetXMLToken(meClass),
936                                RTL_TEXTENCODING_ASCII_US );
937             RTL_LOGFILE_CONTEXT_TRACE1( aLogContext, "class=\"%s\"",
938                                         aClass.GetBuffer() );
939         }
940 #endif
941 
942         exportDoc( meClass );
943     }
944     catch( uno::Exception e )
945     {
946         // We must catch exceptions, because according to the
947         // API definition export must not throw one!
948         Sequence<OUString> aSeq(0);
949         SetError( XMLERROR_FLAG_ERROR | XMLERROR_FLAG_SEVERE | XMLERROR_API,
950                   aSeq, e.Message, NULL );
951     }
952 
953     // return true only if no error occured
954     return (GetErrorFlags() & (ERROR_DO_NOTHING|ERROR_ERROR_OCCURED)) == 0;
955 }
956 
957 void SAL_CALL SvXMLExport::cancel() throw(uno::RuntimeException)
958 {
959     // stop export
960     Sequence<OUString> aEmptySeq;
961     SetError(XMLERROR_CANCEL|XMLERROR_FLAG_SEVERE, aEmptySeq);
962 }
963 
964 ::rtl::OUString SAL_CALL SvXMLExport::getName(  )
965     throw (::com::sun::star::uno::RuntimeException)
966 {
967     return msFilterName;
968 }
969 
970 void SAL_CALL SvXMLExport::setName( const ::rtl::OUString& )
971     throw (::com::sun::star::uno::RuntimeException)
972 {
973     // do nothing, because it is not possible to set the FilterName
974 }
975 
976 
977 // XServiceInfo
978 OUString SAL_CALL SvXMLExport::getImplementationName(  ) throw(uno::RuntimeException)
979 {
980     OUString aStr;
981     return aStr;
982 }
983 
984 sal_Bool SAL_CALL SvXMLExport::supportsService( const OUString& rServiceName ) throw(uno::RuntimeException)
985 {
986     return
987         rServiceName.equalsAsciiL(
988             "com.sun.star.document.ExportFilter",
989             sizeof("com.sun.star.document.ExportFilter")-1 ) ||
990         rServiceName.equalsAsciiL(
991             "com.sun.star.xml.XMLExportFilter",
992             sizeof("com.sun.star.xml.XMLExportFilter")-1);
993 }
994 
995 uno::Sequence< OUString > SAL_CALL SvXMLExport::getSupportedServiceNames(  )
996     throw(uno::RuntimeException)
997 {
998     uno::Sequence<OUString> aSeq(2);
999     aSeq[0] = OUString(
1000         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.ExportFilter"));
1001     aSeq[1] = OUString(
1002         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.XMLExportFilter"));
1003     return aSeq;
1004 }
1005 
1006 ///////////////////////////////////////////////////////////////////////
1007 
1008 ::rtl::OUString
1009 SvXMLExport::EnsureNamespace(::rtl::OUString const & i_rNamespace,
1010     ::rtl::OUString const & i_rPreferredPrefix)
1011 {
1012     ::rtl::OUString sPrefix;
1013     sal_uInt16 nKey( _GetNamespaceMap().GetKeyByName( i_rNamespace ) );
1014     if( XML_NAMESPACE_UNKNOWN == nKey )
1015     {
1016         // There is no prefix for the namespace, so
1017         // we have to generate one and have to add it.
1018         sPrefix = i_rPreferredPrefix;
1019         nKey = _GetNamespaceMap().GetKeyByPrefix( sPrefix );
1020         sal_Int32 n( 0 );
1021         ::rtl::OUStringBuffer buf;
1022         while( nKey != USHRT_MAX )
1023         {
1024             buf.append( i_rPreferredPrefix );
1025             buf.append( ++n );
1026             sPrefix = buf.makeStringAndClear();
1027             nKey = _GetNamespaceMap().GetKeyByPrefix( sPrefix );
1028         }
1029 
1030         if (mpImpl->mNamespaceMaps.empty()
1031             || (mpImpl->mNamespaceMaps.top().second != mpImpl->mDepth))
1032         {
1033             // top was created for lower depth... need a new namespace map!
1034             mpImpl->mNamespaceMaps.push(
1035                 ::std::make_pair(mpNamespaceMap, mpImpl->mDepth) );
1036             mpNamespaceMap = new SvXMLNamespaceMap( *mpNamespaceMap );
1037         }
1038 
1039         // add the namespace to the map and as attribute
1040         mpNamespaceMap->Add( sPrefix, i_rNamespace );
1041         buf.append( GetXMLToken(XML_XMLNS) );
1042         buf.append( sal_Unicode(':') );
1043         buf.append( sPrefix );
1044         AddAttribute( buf.makeStringAndClear(), i_rNamespace );
1045     }
1046     else
1047     {
1048         // If there is a prefix for the namespace, reuse that.
1049         sPrefix = _GetNamespaceMap().GetPrefixByKey( nKey );
1050     }
1051     return sPrefix;
1052 }
1053 
1054 ///////////////////////////////////////////////////////////////////////
1055 
1056 void SvXMLExport::AddAttributeASCII( sal_uInt16 nPrefixKey,
1057                                      const sal_Char *pName,
1058                                      const sal_Char *pValue )
1059 {
1060     OUString sName( OUString::createFromAscii( pName ) );
1061     OUString sValue( OUString::createFromAscii( pValue ) );
1062 
1063     mpAttrList->AddAttribute(
1064         _GetNamespaceMap().GetQNameByKey( nPrefixKey, sName ), sValue );
1065 }
1066 
1067 void SvXMLExport::AddAttribute( sal_uInt16 nPrefixKey, const sal_Char *pName,
1068                               const OUString& rValue )
1069 {
1070     OUString sName( OUString::createFromAscii( pName ) );
1071 
1072     mpAttrList->AddAttribute(
1073         _GetNamespaceMap().GetQNameByKey( nPrefixKey, sName ), rValue );
1074 }
1075 
1076 void SvXMLExport::AddAttribute( sal_uInt16 nPrefixKey, const OUString& rName,
1077                               const OUString& rValue )
1078 {
1079     mpAttrList->AddAttribute(
1080         _GetNamespaceMap().GetQNameByKey( nPrefixKey, rName ), rValue );
1081 }
1082 
1083 void SvXMLExport::AddAttribute( sal_uInt16 nPrefixKey,
1084                                 enum XMLTokenEnum eName,
1085                                 const OUString& rValue )
1086 {
1087     mpAttrList->AddAttribute(
1088         _GetNamespaceMap().GetQNameByKey( nPrefixKey, GetXMLToken(eName) ),
1089         rValue );
1090 }
1091 
1092 void SvXMLExport::AddAttribute( sal_uInt16 nPrefixKey,
1093                                 enum XMLTokenEnum eName,
1094                                 enum XMLTokenEnum eValue)
1095 {
1096     mpAttrList->AddAttribute(
1097         _GetNamespaceMap().GetQNameByKey( nPrefixKey, GetXMLToken(eName) ),
1098         GetXMLToken(eValue) );
1099 }
1100 
1101 void SvXMLExport::AddAttribute( const ::rtl::OUString& rQName,
1102                                 const ::rtl::OUString& rValue )
1103 {
1104       mpAttrList->AddAttribute(
1105         rQName,
1106         rValue );
1107 }
1108 
1109 void SvXMLExport::AddAttribute( const ::rtl::OUString& rQName,
1110                                 enum ::xmloff::token::XMLTokenEnum eValue )
1111 {
1112       mpAttrList->AddAttribute(
1113         rQName,
1114         GetXMLToken(eValue) );
1115 }
1116 
1117 void SvXMLExport::AddAttributeList( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
1118 {
1119     if( xAttrList.is())
1120         mpAttrList->AppendAttributeList( xAttrList );
1121 }
1122 
1123 void SvXMLExport::ClearAttrList()
1124 {
1125     mpAttrList->Clear();
1126 }
1127 
1128 #ifdef DBG_UTIL
1129 void SvXMLExport::CheckAttrList()
1130 {
1131     DBG_ASSERT( !mpAttrList->getLength(),
1132                 "XMLExport::CheckAttrList: list is not empty" );
1133 }
1134 #endif
1135 
1136 void SvXMLExport::ImplExportMeta()
1137 {
1138     CheckAttrList();
1139 
1140     _ExportMeta();
1141 }
1142 
1143 void SvXMLExport::ImplExportSettings()
1144 {
1145     CheckAttrList();
1146 
1147     ::std::list< SettingsGroup > aSettings;
1148     sal_Int32 nSettingsCount = 0;
1149 
1150     // view settings
1151     uno::Sequence< beans::PropertyValue > aViewSettings;
1152     GetViewSettingsAndViews( aViewSettings );
1153     aSettings.push_back( SettingsGroup( XML_VIEW_SETTINGS, aViewSettings ) );
1154     nSettingsCount += aViewSettings.getLength();
1155 
1156     // configuration settings
1157     uno::Sequence<beans::PropertyValue> aConfigSettings;
1158     GetConfigurationSettings( aConfigSettings );
1159     aSettings.push_back( SettingsGroup( XML_CONFIGURATION_SETTINGS, aConfigSettings ) );
1160     nSettingsCount += aConfigSettings.getLength();
1161 
1162     // any document specific settings
1163     nSettingsCount += GetDocumentSpecificSettings( aSettings );
1164 
1165     {
1166         SvXMLElementExport aElem( *this,
1167                                 nSettingsCount != 0,
1168                                 XML_NAMESPACE_OFFICE, XML_SETTINGS,
1169                                 sal_True, sal_True );
1170 
1171         SettingsExportFacade aSettingsExportContext( *this );
1172         XMLSettingsExportHelper aSettingsExportHelper( aSettingsExportContext );
1173 
1174         for (   ::std::list< SettingsGroup >::const_iterator settings = aSettings.begin();
1175                 settings != aSettings.end();
1176                 ++settings
1177             )
1178         {
1179             if ( !settings->aSettings.getLength() )
1180                 continue;
1181 
1182             OUString sSettingsName( GetXMLToken( settings->eGroupName ) );
1183             OUString sQName = GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_OOO, sSettingsName );
1184             aSettingsExportHelper.exportAllSettings( settings->aSettings, sQName );
1185         }
1186     }
1187 }
1188 
1189 void SvXMLExport::ImplExportStyles( sal_Bool )
1190 {
1191     CheckAttrList();
1192 
1193 //  AddAttribute( XML_NAMESPACE_NONE, XML_ID, XML_STYLES_ID );
1194     {
1195         // <style:styles>
1196         SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_STYLES,
1197                                 sal_True, sal_True );
1198 
1199         _ExportStyles( sal_False );
1200     }
1201 
1202     // transfer style names (+ families) TO other components (if appropriate)
1203     if( ( ( mnExportFlags & EXPORT_CONTENT ) == 0 ) && mxExportInfo.is() )
1204     {
1205         static OUString sStyleNames( RTL_CONSTASCII_USTRINGPARAM("StyleNames") );
1206         static OUString sStyleFamilies( RTL_CONSTASCII_USTRINGPARAM("StyleFamilies") );
1207         uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
1208         if ( xPropertySetInfo->hasPropertyByName( sStyleNames ) && xPropertySetInfo->hasPropertyByName( sStyleFamilies ) )
1209         {
1210             Sequence<sal_Int32> aStyleFamilies;
1211             Sequence<OUString> aStyleNames;
1212             mxAutoStylePool->GetRegisteredNames( aStyleFamilies, aStyleNames );
1213             mxExportInfo->setPropertyValue( sStyleNames, makeAny( aStyleNames ) );
1214             mxExportInfo->setPropertyValue( sStyleFamilies,
1215                                            makeAny( aStyleFamilies ) );
1216         }
1217     }
1218 }
1219 
1220 void SvXMLExport::ImplExportAutoStyles( sal_Bool )
1221 {
1222     // transfer style names (+ families) FROM other components (if appropriate)
1223     OUString sStyleNames( RTL_CONSTASCII_USTRINGPARAM("StyleNames") );
1224     OUString sStyleFamilies( RTL_CONSTASCII_USTRINGPARAM("StyleFamilies") );
1225     if( ( ( mnExportFlags & EXPORT_STYLES ) == 0 )
1226         && mxExportInfo.is()
1227         && mxExportInfo->getPropertySetInfo()->hasPropertyByName( sStyleNames )
1228         && mxExportInfo->getPropertySetInfo()->hasPropertyByName( sStyleFamilies ) )
1229     {
1230         Sequence<sal_Int32> aStyleFamilies;
1231         mxExportInfo->getPropertyValue( sStyleFamilies ) >>= aStyleFamilies;
1232         Sequence<OUString> aStyleNames;
1233         mxExportInfo->getPropertyValue( sStyleNames ) >>= aStyleNames;
1234         mxAutoStylePool->RegisterNames( aStyleFamilies, aStyleNames );
1235     }
1236 
1237 //  AddAttributeASCII( XML_NAMESPACE_NONE, XML_ID, XML_AUTO_STYLES_ID );
1238     {
1239         // <style:automatic-styles>
1240         SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE,
1241                                   XML_AUTOMATIC_STYLES, sal_True, sal_True );
1242 
1243 #if 0
1244         AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
1245         AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, XML_STYLES_HREF );
1246         AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONLOAD );
1247         AddAttribute( XML_NAMESPACE_XLINK, XML_ROLE,
1248                       pNamespaceMap->GetQNameByKey( XML_NAMESPACE_OFFICE,
1249                                                GetXMLToken(XML_STYLESHEET)) );
1250         {
1251             // <style:use-styles>
1252             SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE,
1253                                       XML_USE_STYLES, sal_True, sal_True );
1254         }
1255 #endif
1256         _ExportAutoStyles();
1257     }
1258 }
1259 
1260 void SvXMLExport::ImplExportMasterStyles( sal_Bool )
1261 {
1262     {
1263         // <style:master-styles>
1264         SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_MASTER_STYLES,
1265                                 sal_True, sal_True );
1266 
1267         _ExportMasterStyles();
1268     }
1269 
1270 #if 0
1271     AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
1272     AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, XML_AUTO_STYLES_HREF );
1273     AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONLOAD );
1274     AddAttribute( XML_NAMESPACE_XLINK, XML_ROLE,
1275                   pNamespaceMap->GetQNameByKey( XML_NAMESPACE_OFFICE,
1276                                                 GetXMLToken(XML_STYLESHEET) ) );
1277     {
1278         // <style:use-styles>
1279         SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE,
1280                                   XML_USE_STYLES, sal_True, sal_True );
1281     }
1282 #endif
1283 }
1284 
1285 void SvXMLExport::ImplExportContent()
1286 {
1287     CheckAttrList();
1288 
1289     CheckAttrList();
1290 
1291     {
1292         SvXMLElementExport aElemrnt( *this, XML_NAMESPACE_OFFICE, XML_BODY,
1293                                   sal_True, sal_True );
1294         {
1295             XMLTokenEnum eClass = meClass;
1296             if( XML_TEXT_GLOBAL == eClass )
1297             {
1298                 AddAttribute( XML_NAMESPACE_TEXT, XML_GLOBAL,
1299                       GetXMLToken( XML_TRUE ) );
1300                 eClass = XML_TEXT;
1301             }
1302             // <office:body ...>
1303             SetBodyAttributes();
1304             SvXMLElementExport aElem( *this, meClass != XML_TOKEN_INVALID,
1305                                       XML_NAMESPACE_OFFICE, eClass,
1306                                       sal_True, sal_True );
1307 
1308             _ExportContent();
1309         }
1310     }
1311 }
1312 
1313 void SvXMLExport::SetBodyAttributes()
1314 {
1315 }
1316 
1317 static void
1318 lcl_AddGrddl(SvXMLExport & rExport, const sal_Int32 /*nExportMode*/)
1319 {
1320     // check version >= 1.2
1321     switch (rExport.getDefaultVersion()) {
1322         case SvtSaveOptions::ODFVER_011: // fall thru
1323         case SvtSaveOptions::ODFVER_010: return;
1324         default: break;
1325     }
1326 
1327     // #i115030#: disabled
1328 #if 0
1329     if (EXPORT_SETTINGS != nExportMode) // meta, content, styles
1330     {
1331         rExport.AddAttribute( XML_NAMESPACE_GRDDL, XML_TRANSFORMATION,
1332             OUString::createFromAscii(s_grddl_xsl) );
1333     }
1334 #endif
1335 }
1336 
1337 sal_uInt32 SvXMLExport::exportDoc( enum ::xmloff::token::XMLTokenEnum eClass )
1338 {
1339     bool bOwnGraphicResolver = false;
1340     bool bOwnEmbeddedResolver = false;
1341 
1342     if( !mxGraphicResolver.is() || !mxEmbeddedResolver.is() )
1343     {
1344         Reference< XMultiServiceFactory > xFactory( mxModel,    UNO_QUERY );
1345         if( xFactory.is() )
1346         {
1347             try
1348             {
1349                 if( !mxGraphicResolver.is() )
1350                 {
1351                     mxGraphicResolver = Reference< XGraphicObjectResolver >::query(
1352                         xFactory->createInstance(
1353                             OUString(RTL_CONSTASCII_USTRINGPARAM(
1354                                 "com.sun.star.document.ExportGraphicObjectResolver"))));
1355                     bOwnGraphicResolver = mxGraphicResolver.is();
1356                 }
1357 
1358                 if( !mxEmbeddedResolver.is() )
1359                 {
1360                     mxEmbeddedResolver = Reference< XEmbeddedObjectResolver >::query(
1361                         xFactory->createInstance(
1362                             OUString(RTL_CONSTASCII_USTRINGPARAM(
1363                                 "com.sun.star.document.ExportEmbeddedObjectResolver"))));
1364                     bOwnEmbeddedResolver = mxEmbeddedResolver.is();
1365                 }
1366             }
1367             catch( com::sun::star::uno::Exception& )
1368             {
1369             }
1370         }
1371     }
1372     if( (getExportFlags() & EXPORT_OASIS) == 0 )
1373     {
1374         Reference< lang::XMultiServiceFactory > xFactory = getServiceFactory();
1375         if( xFactory.is() )
1376         {
1377             try
1378             {
1379                 ::comphelper::PropertyMapEntry aInfoMap[] =
1380                 {
1381                     { "Class", sizeof("Class")-1, 0,
1382                         &::getCppuType((::rtl::OUString*)0),
1383                         PropertyAttribute::MAYBEVOID, 0},
1384                     { NULL, 0, 0, NULL, 0, 0 }
1385                 };
1386                 Reference< XPropertySet > xConvPropSet(
1387                     ::comphelper::GenericPropertySet_CreateInstance(
1388                             new ::comphelper::PropertySetInfo( aInfoMap ) ) );
1389 
1390                 Any aAny;
1391                 aAny <<= GetXMLToken( eClass );
1392                 xConvPropSet->setPropertyValue(
1393                         OUString(RTL_CONSTASCII_USTRINGPARAM("Class")), aAny );
1394 
1395                 Reference< XPropertySet > xPropSet =
1396                     mxExportInfo.is()
1397                     ?  PropertySetMerger_CreateInstance( mxExportInfo,
1398                                                          xConvPropSet )
1399                     : xConvPropSet;
1400 
1401                 Sequence<Any> aArgs( 3 );
1402                 aArgs[0] <<= mxHandler;
1403                 aArgs[1] <<= xPropSet;
1404                 aArgs[2] <<= mxModel;
1405 
1406                 // get filter component
1407                 Reference< xml::sax::XDocumentHandler > xTmpDocHandler(
1408                     xFactory->createInstanceWithArguments(
1409                     OUString::createFromAscii("com.sun.star.comp.Oasis2OOoTransformer"),
1410                                 aArgs), UNO_QUERY);
1411                 OSL_ENSURE( xTmpDocHandler.is(),
1412                     "can't instantiate OASIS transformer component" );
1413                 if( xTmpDocHandler.is() )
1414                 {
1415                     mxHandler = xTmpDocHandler;
1416                     mxExtHandler = uno::Reference<xml::sax::XExtendedDocumentHandler>( mxHandler, UNO_QUERY );
1417                 }
1418             }
1419             catch( com::sun::star::uno::Exception& )
1420             {
1421             }
1422         }
1423     }
1424 
1425 
1426     mxHandler->startDocument();
1427 
1428     // <?xml version="1.0" encoding="UTF-8"?>
1429 //  xHandler->processingInstruction( S2U( sXML_xml ), S2U( sXML_xml_pi ) );
1430 
1431     // <office:document ...>
1432     CheckAttrList();
1433 
1434     // namespace attributes
1435     // ( The namespace decls should be first attributes in the element;
1436     //   some faulty XML parsers (JAXP1.1) have a problem with this,
1437     //   also it's more elegant )
1438     sal_uInt16 nPos = mpNamespaceMap->GetFirstKey();
1439     while( USHRT_MAX != nPos )
1440     {
1441         mpAttrList->AddAttribute( mpNamespaceMap->GetAttrNameByKey( nPos ),
1442                                   mpNamespaceMap->GetNameByKey( nPos ) );
1443         nPos = mpNamespaceMap->GetNextKey( nPos );
1444     }
1445 
1446 
1447 
1448     // office:version = ...
1449     if( !mbExtended )
1450     {
1451         const sal_Char* pVersion = 0;
1452         switch( getDefaultVersion() )
1453         {
1454         case SvtSaveOptions::ODFVER_LATEST: pVersion = sXML_1_2; break;
1455         case SvtSaveOptions::ODFVER_012: pVersion = sXML_1_2; break;
1456         case SvtSaveOptions::ODFVER_011: pVersion = sXML_1_1; break;
1457         case SvtSaveOptions::ODFVER_010: break;
1458 
1459         default:
1460             DBG_ERROR("xmloff::SvXMLExport::exportDoc(), unexpected odf default version!");
1461         }
1462 
1463         if( pVersion )
1464             AddAttribute( XML_NAMESPACE_OFFICE, XML_VERSION,
1465                               OUString::createFromAscii(pVersion) );
1466     }
1467 
1468     {
1469         enum XMLTokenEnum eRootService = XML_TOKEN_INVALID;
1470         const sal_Int32 nExportMode = mnExportFlags & (EXPORT_META|EXPORT_STYLES|EXPORT_CONTENT|EXPORT_SETTINGS);
1471 
1472         lcl_AddGrddl(*this, nExportMode);
1473 
1474         if( EXPORT_META == nExportMode )
1475         {
1476             // export only meta
1477             eRootService = XML_DOCUMENT_META;
1478         }
1479         else if ( EXPORT_SETTINGS == nExportMode )
1480         {
1481             // export only settings
1482             eRootService = XML_DOCUMENT_SETTINGS;
1483         }
1484         else if( EXPORT_STYLES == nExportMode )
1485         {
1486             // export only styles
1487             eRootService = XML_DOCUMENT_STYLES;
1488         }
1489         else if( EXPORT_CONTENT == nExportMode )
1490         {
1491             // export only content
1492             eRootService = XML_DOCUMENT_CONTENT;
1493         }
1494         else
1495         {
1496             // the god'ol one4all element
1497             eRootService = XML_DOCUMENT;
1498             // office:mimetype = ... (only for stream containing the content)
1499             if( eClass != XML_TOKEN_INVALID )
1500             {
1501                 OUString aTmp( RTL_CONSTASCII_USTRINGPARAM("application/vnd.oasis.opendocument.") );
1502                 aTmp += GetXMLToken( eClass );
1503                 AddAttribute( XML_NAMESPACE_OFFICE, XML_MIMETYPE, aTmp );
1504             }
1505         }
1506 
1507 //      if( (getExportFlags() & EXPORT_NODOCTYPE) == 0 &&
1508 //          xExtHandler.is() )
1509 //      {
1510 //          OUStringBuffer aDocType(
1511 //               GetXMLToken(XML_XML_DOCTYPE_PREFIX).getLength() +
1512 //              GetXMLToken(XML_XML_DOCTYPE_SUFFIX).getLength() + 30 );
1513 //
1514 //          aDocType.append( GetXMLToken(XML_XML_DOCTYPE_PREFIX) );
1515 //          aDocType.append( GetNamespaceMap().GetQNameByKey(
1516 //                         XML_NAMESPACE_OFFICE, GetXMLToken(eRootService) ) );
1517 //          aDocType.append( GetXMLToken(XML_XML_DOCTYPE_SUFFIX) );
1518 //          xExtHandler->unknown( aDocType.makeStringAndClear() );
1519 //      }
1520 
1521         SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, eRootService, sal_True, sal_True );
1522 
1523         // meta information
1524         if( mnExportFlags & EXPORT_META )
1525             ImplExportMeta();
1526 
1527         // settings
1528         if( mnExportFlags & EXPORT_SETTINGS )
1529             ImplExportSettings();
1530 
1531         // scripts
1532         if( mnExportFlags & EXPORT_SCRIPTS )
1533             _ExportScripts();
1534 
1535         // font declerations
1536         if( mnExportFlags & EXPORT_FONTDECLS )
1537             _ExportFontDecls();
1538 
1539         // styles
1540         if( mnExportFlags & EXPORT_STYLES )
1541             ImplExportStyles( sal_False );
1542 
1543         // autostyles
1544         if( mnExportFlags & EXPORT_AUTOSTYLES )
1545             ImplExportAutoStyles( sal_False );
1546 
1547         // masterstyles
1548         if( mnExportFlags & EXPORT_MASTERSTYLES )
1549             ImplExportMasterStyles( sal_False );
1550 
1551         // contnt
1552         if( mnExportFlags & EXPORT_CONTENT )
1553             ImplExportContent();
1554     }
1555 
1556 
1557     mxHandler->endDocument();
1558 
1559     if( bOwnGraphicResolver )
1560     {
1561         Reference< XComponent > xComp( mxGraphicResolver, UNO_QUERY );
1562         xComp->dispose();
1563     }
1564 
1565     if( bOwnEmbeddedResolver )
1566     {
1567         Reference< XComponent > xComp( mxEmbeddedResolver, UNO_QUERY );
1568         xComp->dispose();
1569     }
1570 
1571     return 0;
1572 }
1573 
1574 void SvXMLExport::ResetNamespaceMap()
1575 {
1576     delete mpNamespaceMap;    mpNamespaceMap = new SvXMLNamespaceMap;
1577 }
1578 
1579 void SvXMLExport::_ExportMeta()
1580 {
1581     OUString generator( ::utl::DocInfoHelper::GetGeneratorString() );
1582     Reference< XDocumentPropertiesSupplier > xDocPropsSupplier(mxModel,
1583         UNO_QUERY);
1584     if (xDocPropsSupplier.is()) {
1585         Reference<XDocumentProperties> xDocProps(
1586             xDocPropsSupplier->getDocumentProperties());
1587         if (!xDocProps.is()) throw;
1588         // update generator here
1589         xDocProps->setGenerator(generator);
1590         SvXMLMetaExport * pMeta = new SvXMLMetaExport(*this, xDocProps);
1591         uno::Reference<xml::sax::XDocumentHandler> xMeta(pMeta);
1592         pMeta->Export();
1593     } else {
1594         // office:meta
1595         SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_META,
1596                                 sal_True, sal_True );
1597         {
1598     // BM: #i60323# export generator even if xInfoProp is empty (which is the
1599     // case for charts). The generator does not depend on xInfoProp
1600             SvXMLElementExport anElem( *this, XML_NAMESPACE_META, XML_GENERATOR,
1601                                       sal_True, sal_True );
1602             Characters(generator);
1603         }
1604     }
1605 }
1606 
1607 void SvXMLExport::_ExportScripts()
1608 {
1609     SvXMLElementExport aElement( *this, XML_NAMESPACE_OFFICE, XML_SCRIPTS, sal_True, sal_True );
1610 
1611     // export Basic macros (only for FlatXML)
1612     if ( mnExportFlags & EXPORT_EMBEDDED )
1613     {
1614         ::rtl::OUString aValue( GetNamespaceMap().GetPrefixByKey( XML_NAMESPACE_OOO ) );
1615         aValue += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":Basic" ) );
1616         AddAttribute( XML_NAMESPACE_SCRIPT, XML_LANGUAGE, aValue );
1617 
1618         SvXMLElementExport aElem( *this, XML_NAMESPACE_OFFICE, XML_SCRIPT, sal_True, sal_True );
1619 
1620         // initialize Basic
1621         if ( mxModel.is() )
1622         {
1623             Reference< beans::XPropertySet > xPSet( mxModel, UNO_QUERY );
1624             if ( xPSet.is() )
1625                 xPSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicLibraries" ) ) );
1626         }
1627 
1628         Reference< document::XExporter > xExporter;
1629         Reference< lang::XMultiServiceFactory > xMSF( getServiceFactory() );
1630         if ( xMSF.is() )
1631         {
1632             Reference < XDocumentHandler > xHdl( new XMLBasicExportFilter( mxHandler ) );
1633             Sequence < Any > aArgs( 1 );
1634             aArgs[0] <<= xHdl;
1635             xExporter.set( xMSF->createInstanceWithArguments(
1636                 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLOasisBasicExporter" ) ), aArgs ),
1637                 UNO_QUERY );
1638         }
1639 
1640         OSL_ENSURE( xExporter.is(),
1641             "SvXMLExport::_ExportScripts: can't instantiate export filter component for Basic macros" );
1642 
1643         if ( xExporter.is() )
1644         {
1645             Reference< XComponent > xComp( mxModel, UNO_QUERY );
1646             xExporter->setSourceDocument( xComp );
1647             Reference< XFilter > xFilter( xExporter, UNO_QUERY );
1648             if ( xFilter.is() )
1649             {
1650                 Sequence < PropertyValue > aMediaDesc( 0 );
1651                 xFilter->filter( aMediaDesc );
1652             }
1653         }
1654     }
1655 
1656     // export document events
1657     Reference< document::XEventsSupplier > xEvents( GetModel(), UNO_QUERY );
1658     GetEventExport().Export( xEvents, sal_True );
1659 }
1660 
1661 void SvXMLExport::_ExportFontDecls()
1662 {
1663     if( mxFontAutoStylePool.is() )
1664         mxFontAutoStylePool->exportXML();
1665 }
1666 
1667 void SvXMLExport::_ExportStyles( sal_Bool )
1668 {
1669     uno::Reference< lang::XMultiServiceFactory > xFact( GetModel(), uno::UNO_QUERY );
1670     if( xFact.is())
1671     {
1672         // export (fill-)gradient-styles
1673         try
1674         {
1675             uno::Reference< container::XNameAccess > xGradient( xFact->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.GradientTable") ) ), uno::UNO_QUERY );
1676             if( xGradient.is() )
1677             {
1678                 XMLGradientStyleExport aGradientStyle( *this );
1679 
1680                 if( xGradient->hasElements() )
1681                 {
1682                     uno::Sequence< OUString > aNamesSeq ( xGradient->getElementNames() );
1683                     sal_Int32 nCount = aNamesSeq.getLength();
1684                     for( sal_Int32 i=0; i<nCount; i++ )
1685                     {
1686                         const OUString& rStrName = aNamesSeq[ i ];
1687 
1688                         try
1689                         {
1690                             uno::Any aValue = xGradient->getByName( rStrName );
1691 
1692                             aGradientStyle.exportXML( rStrName, aValue );
1693                         }
1694                         catch( container::NoSuchElementException& )
1695                         {}
1696                     }
1697                 }
1698             }
1699         }
1700         catch( lang::ServiceNotRegisteredException& )
1701         {}
1702 
1703         // export (fill-)hatch-styles
1704         try
1705         {
1706             uno::Reference< container::XNameAccess > xHatch( xFact->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.HatchTable") ) ), uno::UNO_QUERY );
1707             if( xHatch.is() )
1708             {
1709                 XMLHatchStyleExport aHatchStyle( *this );
1710 
1711                 if( xHatch->hasElements() )
1712                 {
1713                     uno::Sequence< OUString > aNamesSeq ( xHatch->getElementNames() );
1714                     sal_Int32 nCount = aNamesSeq.getLength();
1715                     for( sal_Int32 i=0; i<nCount; i++ )
1716                     {
1717                         const OUString& rStrName = aNamesSeq[ i ];
1718 
1719                         try
1720                         {
1721                             uno::Any aValue = xHatch->getByName( rStrName );
1722 
1723                             aHatchStyle.exportXML( rStrName, aValue );
1724                         }
1725                         catch( container::NoSuchElementException& )
1726                         {}
1727                     }
1728                 }
1729             }
1730         }
1731         catch( lang::ServiceNotRegisteredException& )
1732         {}
1733 
1734         // export (fill-)bitmap-styles
1735         try
1736         {
1737             uno::Reference< container::XNameAccess > xBitmap( xFact->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.BitmapTable") ) ), uno::UNO_QUERY );
1738             if( xBitmap.is() )
1739             {
1740                 XMLImageStyle aImageStyle;
1741 
1742                 if( xBitmap->hasElements() )
1743                 {
1744                     uno::Sequence< OUString > aNamesSeq ( xBitmap->getElementNames() );
1745                     sal_Int32 nCount = aNamesSeq.getLength();
1746                     for( sal_Int32 i=0; i<nCount; i++ )
1747                     {
1748                         const OUString& rStrName = aNamesSeq[ i ];
1749 
1750                         try
1751                         {
1752                             uno::Any aValue = xBitmap->getByName( rStrName );
1753 
1754                             aImageStyle.exportXML( rStrName, aValue, *this );
1755                         }
1756                         catch( container::NoSuchElementException& )
1757                         {}
1758                     }
1759                 }
1760             }
1761         }
1762         catch( lang::ServiceNotRegisteredException& )
1763         {}
1764 
1765         // export transparency-gradient -styles
1766         try
1767         {
1768             uno::Reference< container::XNameAccess > xTransGradient( xFact->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.TransparencyGradientTable") ) ), uno::UNO_QUERY );
1769             if( xTransGradient.is() )
1770             {
1771                 XMLTransGradientStyleExport aTransGradientstyle( *this );
1772 
1773                 if( xTransGradient->hasElements() )
1774                 {
1775                     uno::Sequence< OUString > aNamesSeq ( xTransGradient->getElementNames() );
1776                     sal_Int32 nCount = aNamesSeq.getLength();
1777                     for( sal_Int32 i=0; i<nCount; i++ )
1778                     {
1779                         const OUString& rStrName = aNamesSeq[ i ];
1780 
1781                         try
1782                         {
1783                             uno::Any aValue = xTransGradient->getByName( rStrName );
1784 
1785                             aTransGradientstyle.exportXML( rStrName, aValue );
1786                         }
1787                         catch( container::NoSuchElementException& )
1788                         {}
1789                     }
1790                 }
1791             }
1792         }
1793         catch( lang::ServiceNotRegisteredException& )
1794         {}
1795 
1796         // export marker-styles
1797         try
1798         {
1799             uno::Reference< container::XNameAccess > xMarker( xFact->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.MarkerTable") ) ), uno::UNO_QUERY );
1800             if( xMarker.is() )
1801             {
1802                 XMLMarkerStyleExport aMarkerStyle( *this );
1803 
1804                 if( xMarker->hasElements() )
1805                 {
1806                     uno::Sequence< OUString > aNamesSeq ( xMarker->getElementNames() );
1807                     sal_Int32 nCount = aNamesSeq.getLength();
1808                     for( sal_Int32 i=0; i<nCount; i++ )
1809                     {
1810                         const OUString& rStrName = aNamesSeq[ i ];
1811 
1812                         try
1813                         {
1814                             uno::Any aValue = xMarker->getByName( rStrName );
1815 
1816                             aMarkerStyle.exportXML( rStrName, aValue );
1817                         }
1818                         catch( container::NoSuchElementException& )
1819                         {}
1820                     }
1821                 }
1822             }
1823         }
1824         catch( lang::ServiceNotRegisteredException& )
1825         {}
1826 
1827         // export dash-styles
1828         try
1829         {
1830             uno::Reference< container::XNameAccess > xDashes( xFact->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.DashTable") ) ), uno::UNO_QUERY );
1831             if( xDashes.is() )
1832             {
1833                 XMLDashStyleExport aDashStyle( *this );
1834 
1835                 if( xDashes->hasElements() )
1836                 {
1837                     uno::Sequence< OUString > aNamesSeq ( xDashes->getElementNames() );
1838                     sal_Int32 nCount = aNamesSeq.getLength();
1839                     for( sal_Int32 i=0; i<nCount; i++ )
1840                     {
1841                         const OUString& rStrName = aNamesSeq[ i ];
1842 
1843                         try
1844                         {
1845                             uno::Any aValue = xDashes->getByName( rStrName );
1846 
1847                             aDashStyle.exportXML( rStrName, aValue );
1848                         }
1849                         catch( container::NoSuchElementException& )
1850                         {}
1851                     }
1852                 }
1853             }
1854         }
1855         catch( lang::ServiceNotRegisteredException& )
1856         {}
1857     }
1858 }
1859 
1860 XMLTextParagraphExport* SvXMLExport::CreateTextParagraphExport()
1861 {
1862     return new XMLTextParagraphExport( *this, *(GetAutoStylePool().get()) );
1863 }
1864 
1865 XMLShapeExport* SvXMLExport::CreateShapeExport()
1866 {
1867     return new XMLShapeExport(*this);
1868 }
1869 
1870 SvXMLAutoStylePoolP* SvXMLExport::CreateAutoStylePool()
1871 {
1872     return new SvXMLAutoStylePoolP(*this);
1873 }
1874 
1875 XMLPageExport* SvXMLExport::CreatePageExport()
1876 {
1877     return new XMLPageExport( *this );
1878 }
1879 
1880 SchXMLExportHelper* SvXMLExport::CreateChartExport()
1881 {
1882     return new SchXMLExportHelper(*this,*GetAutoStylePool().get());
1883 }
1884 
1885 XMLFontAutoStylePool* SvXMLExport::CreateFontAutoStylePool()
1886 {
1887     return new XMLFontAutoStylePool( *this );
1888 }
1889 
1890 xmloff::OFormLayerXMLExport* SvXMLExport::CreateFormExport()
1891 {
1892     return new xmloff::OFormLayerXMLExport(*this);
1893 }
1894 
1895 void SvXMLExport::GetViewSettingsAndViews(uno::Sequence<beans::PropertyValue>& rProps)
1896 {
1897     GetViewSettings(rProps);
1898     uno::Reference<document::XViewDataSupplier> xViewDataSupplier(GetModel(), uno::UNO_QUERY);
1899     if(xViewDataSupplier.is())
1900     {
1901         uno::Reference<container::XIndexAccess> xIndexAccess;
1902         xViewDataSupplier->setViewData( xIndexAccess ); // make sure we get a newly created sequence
1903         xIndexAccess = xViewDataSupplier->getViewData();
1904         sal_Bool bAdd = sal_False;
1905         uno::Any aAny;
1906         if(xIndexAccess.is() && xIndexAccess->hasElements() )
1907         {
1908             sal_Int32 nCount = xIndexAccess->getCount();
1909             for (sal_Int32 i = 0; i < nCount; i++)
1910             {
1911                 aAny = xIndexAccess->getByIndex(i);
1912                 uno::Sequence<beans::PropertyValue> aProps;
1913                 if( aAny >>= aProps )
1914                 {
1915                     if( aProps.getLength() > 0 )
1916                     {
1917                         bAdd = sal_True;
1918                         break;
1919                     }
1920                 }
1921             }
1922         }
1923 
1924         if( bAdd )
1925         {
1926             sal_Int32 nOldLength(rProps.getLength());
1927             rProps.realloc(nOldLength + 1);
1928             beans::PropertyValue aProp;
1929             aProp.Name = OUString(RTL_CONSTASCII_USTRINGPARAM("Views"));
1930             aProp.Value <<= xIndexAccess;
1931             rProps[nOldLength] = aProp;
1932         }
1933     }
1934 }
1935 
1936 void SvXMLExport::GetViewSettings(uno::Sequence<beans::PropertyValue>&)
1937 {
1938 }
1939 
1940 void SvXMLExport::GetConfigurationSettings(uno::Sequence<beans::PropertyValue>&)
1941 {
1942 }
1943 
1944 sal_Int32 SvXMLExport::GetDocumentSpecificSettings( ::std::list< SettingsGroup >& _out_rSettings )
1945 {
1946     (void)_out_rSettings;
1947     return 0;
1948 }
1949 
1950 void SvXMLExport::addDataStyle(const sal_Int32 nNumberFormat, sal_Bool /*bTimeFormat*/ )
1951 {
1952     if(mpNumExport)
1953         mpNumExport->SetUsed(nNumberFormat);
1954 }
1955 
1956 void SvXMLExport::exportDataStyles()
1957 {
1958     if(mpNumExport)
1959         mpNumExport->Export(sal_False);
1960 }
1961 
1962 void SvXMLExport::exportAutoDataStyles()
1963 {
1964     if(mpNumExport)
1965         mpNumExport->Export(sal_True);
1966 
1967     if (mxFormExport.is())
1968         mxFormExport->exportAutoControlNumberStyles();
1969 }
1970 
1971 OUString SvXMLExport::getDataStyleName(const sal_Int32 nNumberFormat, sal_Bool /*bTimeFormat*/ ) const
1972 {
1973     OUString sTemp;
1974     if(mpNumExport)
1975         sTemp = mpNumExport->GetStyleName(nNumberFormat);
1976     return sTemp;
1977 }
1978 
1979 void SvXMLExport::exportAnnotationMeta(const uno::Reference<drawing::XShape>&)
1980 {
1981 }
1982 
1983 sal_Int32 SvXMLExport::dataStyleForceSystemLanguage(sal_Int32 nFormat) const
1984 {
1985     return ( mpNumExport != NULL )
1986                  ? mpNumExport->ForceSystemLanguage( nFormat ) : nFormat;
1987 }
1988 
1989 
1990 OUString SvXMLExport::AddEmbeddedGraphicObject( const OUString& rGraphicObjectURL )
1991 {
1992     OUString sRet( rGraphicObjectURL );
1993     if( 0 == rGraphicObjectURL.compareTo( msGraphicObjectProtocol,
1994                                           msGraphicObjectProtocol.getLength() ) &&
1995         mxGraphicResolver.is() )
1996     {
1997         if( (getExportFlags() & EXPORT_EMBEDDED) == 0 )
1998             sRet = mxGraphicResolver->resolveGraphicObjectURL( rGraphicObjectURL );
1999         else
2000             sRet = OUString();
2001     }
2002     else
2003         sRet = GetRelativeReference( sRet );
2004 
2005     return sRet;
2006 }
2007 
2008 sal_Bool SvXMLExport::AddEmbeddedGraphicObjectAsBase64( const OUString& rGraphicObjectURL )
2009 {
2010     sal_Bool bRet = sal_False;
2011 
2012     if( (getExportFlags() & EXPORT_EMBEDDED) != 0 &&
2013         0 == rGraphicObjectURL.compareTo( msGraphicObjectProtocol,
2014                                           msGraphicObjectProtocol.getLength() ) &&
2015         mxGraphicResolver.is() )
2016     {
2017         Reference< XBinaryStreamResolver > xStmResolver( mxGraphicResolver, UNO_QUERY );
2018 
2019         if( xStmResolver.is() )
2020         {
2021             Reference< XInputStream > xIn( xStmResolver->getInputStream( rGraphicObjectURL ) );
2022 
2023             if( xIn.is() )
2024             {
2025                 XMLBase64Export aBase64Exp( *this );
2026                 bRet = aBase64Exp.exportOfficeBinaryDataElement( xIn );
2027             }
2028         }
2029     }
2030 
2031     return bRet;
2032 }
2033 
2034 OUString SvXMLExport::AddEmbeddedObject( const OUString& rEmbeddedObjectURL )
2035 {
2036     OUString sRet;
2037     if( (0 == rEmbeddedObjectURL.compareTo( msEmbeddedObjectProtocol,
2038                 msEmbeddedObjectProtocol.getLength() ) ||
2039          0 == rEmbeddedObjectURL.compareTo( msGraphicObjectProtocol,
2040                 msGraphicObjectProtocol.getLength() ) ) &&
2041         mxEmbeddedResolver.is() )
2042     {
2043         sRet =
2044             mxEmbeddedResolver->resolveEmbeddedObjectURL( rEmbeddedObjectURL );
2045     }
2046     else
2047         sRet = GetRelativeReference( rEmbeddedObjectURL );
2048 
2049     return sRet;
2050 }
2051 
2052 sal_Bool SvXMLExport::AddEmbeddedObjectAsBase64( const OUString& rEmbeddedObjectURL )
2053 {
2054     sal_Bool bRet = sal_False;
2055     if( (0 == rEmbeddedObjectURL.compareTo( msEmbeddedObjectProtocol,
2056                 msEmbeddedObjectProtocol.getLength() ) ||
2057          0 == rEmbeddedObjectURL.compareTo( msGraphicObjectProtocol,
2058                 msGraphicObjectProtocol.getLength() ) ) &&
2059         mxEmbeddedResolver.is() )
2060     {
2061         Reference < XNameAccess > xNA( mxEmbeddedResolver, UNO_QUERY );
2062         if( xNA.is() )
2063         {
2064             Any aAny = xNA->getByName( rEmbeddedObjectURL );
2065             Reference < XInputStream > xIn;
2066             aAny >>= xIn;
2067             if( xIn.is() )
2068             {
2069                 XMLBase64Export aBase64Exp( *this );
2070                 bRet = aBase64Exp.exportOfficeBinaryDataElement( xIn );
2071             }
2072         }
2073     }
2074 
2075     return bRet;
2076 }
2077 
2078 OUString SvXMLExport::EncodeStyleName(
2079         const OUString& rName,
2080         sal_Bool *pEncoded ) const
2081 {
2082     return GetMM100UnitConverter().encodeStyleName( rName, pEncoded );
2083 }
2084 
2085 ProgressBarHelper*  SvXMLExport::GetProgressBarHelper()
2086 {
2087     if (!mpProgressBarHelper)
2088     {
2089         mpProgressBarHelper = new ProgressBarHelper(mxStatusIndicator, sal_True);
2090 
2091         if (mxExportInfo.is())
2092         {
2093             uno::Reference< beans::XPropertySetInfo > xPropertySetInfo = mxExportInfo->getPropertySetInfo();
2094             if (xPropertySetInfo.is())
2095             {
2096                 OUString sProgressRange(RTL_CONSTASCII_USTRINGPARAM(XML_PROGRESSRANGE));
2097                 OUString sProgressMax(RTL_CONSTASCII_USTRINGPARAM(XML_PROGRESSMAX));
2098                 OUString sProgressCurrent(RTL_CONSTASCII_USTRINGPARAM(XML_PROGRESSCURRENT));
2099                 OUString sRepeat(RTL_CONSTASCII_USTRINGPARAM(XML_PROGRESSREPEAT));
2100                 if (xPropertySetInfo->hasPropertyByName(sProgressMax) &&
2101                     xPropertySetInfo->hasPropertyByName(sProgressCurrent) &&
2102                     xPropertySetInfo->hasPropertyByName(sProgressRange))
2103                 {
2104                     uno::Any aAny;
2105                     sal_Int32 nProgressMax(0);
2106                     sal_Int32 nProgressCurrent(0);
2107                     sal_Int32 nProgressRange(0);
2108                     aAny = mxExportInfo->getPropertyValue(sProgressRange);
2109                     if (aAny >>= nProgressRange)
2110                         mpProgressBarHelper->SetRange(nProgressRange);
2111                     aAny = mxExportInfo->getPropertyValue(sProgressMax);
2112                     if (aAny >>= nProgressMax)
2113                         mpProgressBarHelper->SetReference(nProgressMax);
2114                     aAny = mxExportInfo->getPropertyValue(sProgressCurrent);
2115                     if (aAny >>= nProgressCurrent)
2116                         mpProgressBarHelper->SetValue(nProgressCurrent);
2117                 }
2118                 if (xPropertySetInfo->hasPropertyByName(sRepeat))
2119                 {
2120                     uno::Any aAny = mxExportInfo->getPropertyValue(sRepeat);
2121                     if (aAny.getValueType() == getBooleanCppuType())
2122                         mpProgressBarHelper->SetRepeat(::cppu::any2bool(aAny));
2123                     else {
2124                         DBG_ERRORFILE("why is it no boolean?");
2125                     }
2126                 }
2127             }
2128         }
2129     }
2130     return mpProgressBarHelper;
2131 }
2132 
2133 XMLEventExport& SvXMLExport::GetEventExport()
2134 {
2135     if( NULL == mpEventExport)
2136     {
2137         // create EventExport on demand
2138         mpEventExport = new XMLEventExport(*this, NULL);
2139 
2140         // and register standard handlers + names
2141         OUString sStarBasic(RTL_CONSTASCII_USTRINGPARAM("StarBasic"));
2142         mpEventExport->AddHandler(sStarBasic, new XMLStarBasicExportHandler());
2143         OUString sScript(RTL_CONSTASCII_USTRINGPARAM("Script"));
2144         mpEventExport->AddHandler(sScript, new XMLScriptExportHandler());
2145         mpEventExport->AddTranslationTable(aStandardEventTable);
2146     }
2147 
2148     return *mpEventExport;
2149 }
2150 
2151 XMLImageMapExport& SvXMLExport::GetImageMapExport()
2152 {
2153     // image map export, create on-demand
2154     if( NULL == mpImageMapExport )
2155     {
2156         mpImageMapExport = new XMLImageMapExport(*this);
2157     }
2158 
2159     return *mpImageMapExport;
2160 }
2161 
2162 // XUnoTunnel & co
2163 const uno::Sequence< sal_Int8 > & SvXMLExport::getUnoTunnelId() throw()
2164 {
2165     static uno::Sequence< sal_Int8 > * pSeq = 0;
2166     if( !pSeq )
2167     {
2168         Guard< Mutex > aGuard( Mutex::getGlobalMutex() );
2169         if( !pSeq )
2170         {
2171             static uno::Sequence< sal_Int8 > aSeq( 16 );
2172             rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
2173             pSeq = &aSeq;
2174         }
2175     }
2176     return *pSeq;
2177 }
2178 
2179 SvXMLExport* SvXMLExport::getImplementation( uno::Reference< uno::XInterface > xInt ) throw()
2180 {
2181     uno::Reference< lang::XUnoTunnel > xUT( xInt, uno::UNO_QUERY );
2182     if( xUT.is() )
2183     {
2184         return
2185             reinterpret_cast<SvXMLExport*>(
2186                 sal::static_int_cast<sal_IntPtr>(
2187                     xUT->getSomething( SvXMLExport::getUnoTunnelId())));
2188     }
2189     else
2190         return NULL;
2191 }
2192 
2193 // XUnoTunnel
2194 sal_Int64 SAL_CALL SvXMLExport::getSomething( const uno::Sequence< sal_Int8 >& rId )
2195     throw( uno::RuntimeException )
2196 {
2197     if( rId.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
2198                                                          rId.getConstArray(), 16 ) )
2199     {
2200         return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
2201     }
2202     return 0;
2203 }
2204 
2205 sal_Bool SvXMLExport::ExportEmbeddedOwnObject( Reference< XComponent >& rComp )
2206 {
2207     OUString sFilterService;
2208     bool bIsChart = false;
2209 
2210     Reference < lang::XServiceInfo > xServiceInfo( rComp, UNO_QUERY );
2211     if( xServiceInfo.is() )
2212     {
2213         const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
2214         while( pEntry->sModelService )
2215         {
2216             OUString sModelService( pEntry->sModelService,
2217                                     pEntry->nModelServiceLen,
2218                                     RTL_TEXTENCODING_ASCII_US );
2219             if( xServiceInfo->supportsService( sModelService ) )
2220             {
2221                 sFilterService = OUString( pEntry->sFilterService,
2222                                            pEntry->nFilterServiceLen,
2223                                            RTL_TEXTENCODING_ASCII_US );
2224                 bIsChart = sModelService.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( XML_MODEL_SERVICE_CHART ));
2225                 break;
2226             }
2227             pEntry++;
2228         }
2229     }
2230 
2231     OSL_ENSURE( sFilterService.getLength(), "no export filter for own object" );
2232 
2233     if( !sFilterService.getLength() )
2234         return sal_False;
2235 
2236     Reference < XDocumentHandler > xHdl =
2237         new XMLEmbeddedObjectExportFilter( mxHandler );
2238 
2239     Sequence < Any > aArgs( 1 );
2240     // #144135# the filters for embedded objects in flat format are always
2241     // instantiated as Oasis filters and transformed afterwards. Therefore, all
2242     // special handling that is done if the exportFlags do not contain
2243     // EXPORT_OASIS must be changed to properties being passed in the info
2244     // propertyset
2245 
2246     if( ! (getExportFlags() & EXPORT_OASIS) &&
2247         bIsChart )
2248     {
2249         static ::comphelper::PropertyMapEntry aInfoMap[] =
2250         {
2251             { RTL_CONSTASCII_STRINGPARAM("ExportTableNumberList"), 0, &::getBooleanCppuType(), PropertyAttribute::MAYBEVOID, 0},
2252             { NULL, 0, 0, NULL, 0, 0 }
2253         };
2254         Reference< XPropertySet > xInfoProp(
2255             ::comphelper::GenericPropertySet_CreateInstance(
2256                 new ::comphelper::PropertySetInfo( aInfoMap )));
2257 
2258         if( bIsChart )
2259             xInfoProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("ExportTableNumberList")), makeAny( true ));
2260 
2261         aArgs.realloc( 2 );
2262         aArgs[1] <<= xInfoProp;
2263     }
2264     aArgs[0] <<= xHdl;
2265 
2266     // #110680#
2267     // Reference< lang::XMultiServiceFactory > xServiceFactory = comphelper::getProcessServiceFactory();
2268     Reference< lang::XMultiServiceFactory > xServiceFactory = getServiceFactory();
2269 
2270     Reference< document::XExporter > xExporter(
2271         xServiceFactory->createInstanceWithArguments( sFilterService, aArgs),
2272                                                UNO_QUERY);
2273     OSL_ENSURE( xExporter.is(),
2274                 "can't instantiate export filter component for own object" );
2275     if( !xExporter.is() )
2276         return sal_False;
2277 
2278     xExporter->setSourceDocument( rComp );
2279 
2280     Reference<XFilter> xFilter( xExporter, UNO_QUERY );
2281 
2282     Sequence < PropertyValue > aMediaDesc( 0 );
2283     return xFilter->filter( aMediaDesc );
2284 }
2285 
2286 OUString SvXMLExport::GetRelativeReference(const OUString& rValue)
2287 {
2288     OUString sValue( rValue );
2289     // #i65474# handling of fragment URLs ("#....") is undefined
2290     // they are stored 'as is'
2291     uno::Reference< uri::XUriReference > xUriRef;
2292     if(sValue.getLength() && sValue.getStr()[0] != '#')
2293     {
2294         try
2295         {
2296             xUriRef = mpImpl->mxUriReferenceFactory->parse( rValue );
2297             if( xUriRef.is() && !xUriRef->isAbsolute() )
2298             {
2299                 //#i61943# relative URLs need special handling
2300                 INetURLObject aTemp( mpImpl->msPackageURI );
2301                 bool bWasAbsolute = false;
2302                 sValue = aTemp.smartRel2Abs(sValue, bWasAbsolute ).GetMainURL(INetURLObject::DECODE_TO_IURI);
2303             }
2304         }
2305         catch( uno::Exception& )
2306         {
2307         }
2308     }
2309     OUString sRet = sValue;
2310     if( xUriRef.is() )//no conversion for empty values or for fragments
2311     {
2312         //conversion for matching schemes only
2313         if( xUriRef->getScheme() == mpImpl->msPackageURIScheme )
2314         {
2315             sValue = INetURLObject::GetRelURL( msOrigFileName, sValue,
2316                 INetURLObject::WAS_ENCODED, INetURLObject::DECODE_TO_IURI, RTL_TEXTENCODING_UTF8, INetURLObject::FSYS_DETECT);
2317         }
2318     }
2319     return sValue;
2320 }
2321 
2322 void SvXMLExport::StartElement(sal_uInt16 nPrefix,
2323                         enum ::xmloff::token::XMLTokenEnum eName,
2324                         sal_Bool bIgnWSOutside )
2325 {
2326     StartElement(_GetNamespaceMap().GetQNameByKey( nPrefix,
2327         GetXMLToken(eName) ), bIgnWSOutside);
2328 }
2329 
2330 void SvXMLExport::StartElement(const OUString& rName,
2331                         sal_Bool bIgnWSOutside )
2332 {
2333     if ((mnErrorFlags & ERROR_DO_NOTHING) != ERROR_DO_NOTHING)
2334     {
2335         try
2336         {
2337             if( bIgnWSOutside && ((mnExportFlags & EXPORT_PRETTY) == EXPORT_PRETTY))
2338                 mxHandler->ignorableWhitespace( msWS );
2339             mxHandler->startElement( rName, GetXAttrList() );
2340         }
2341         catch ( SAXInvalidCharacterException& e )
2342         {
2343             Sequence<OUString> aPars(1);
2344             aPars[0] = rName;
2345             SetError( XMLERROR_SAX|XMLERROR_FLAG_WARNING, aPars, e.Message, NULL );
2346         }
2347         catch ( SAXException& e )
2348         {
2349             Sequence<OUString> aPars(1);
2350             aPars[0] = rName;
2351             SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
2352                       aPars, e.Message, NULL );
2353         }
2354     }
2355     ClearAttrList();
2356     ++mpImpl->mDepth; // increment nesting depth counter
2357 }
2358 
2359 void SvXMLExport::Characters(const ::rtl::OUString& rChars)
2360 {
2361     if ((mnErrorFlags & ERROR_DO_NOTHING) != ERROR_DO_NOTHING)
2362     {
2363         try
2364         {
2365             mxHandler->characters(rChars);
2366         }
2367         catch ( SAXInvalidCharacterException& e )
2368         {
2369             Sequence<OUString> aPars(1);
2370             aPars[0] = rChars;
2371             SetError( XMLERROR_SAX|XMLERROR_FLAG_WARNING, aPars, e.Message, NULL );
2372         }
2373         catch ( SAXException& e )
2374         {
2375             Sequence<OUString> aPars(1);
2376             aPars[0] = rChars;
2377             SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
2378                       aPars, e.Message, NULL );
2379         }
2380     }
2381 }
2382 
2383 void SvXMLExport::EndElement(sal_uInt16 nPrefix,
2384                         enum ::xmloff::token::XMLTokenEnum eName,
2385                         sal_Bool bIgnWSInside )
2386 {
2387     EndElement(_GetNamespaceMap().GetQNameByKey( nPrefix, GetXMLToken(eName) ),
2388         bIgnWSInside);
2389 }
2390 
2391 void SvXMLExport::EndElement(const OUString& rName,
2392                         sal_Bool bIgnWSInside )
2393 {
2394     // decrement nesting depth counter & (maybe) restore namespace map
2395     --mpImpl->mDepth;
2396     if (!mpImpl->mNamespaceMaps.empty() &&
2397         (mpImpl->mNamespaceMaps.top().second == mpImpl->mDepth))
2398     {
2399         delete mpNamespaceMap;
2400         mpNamespaceMap = mpImpl->mNamespaceMaps.top().first;
2401         mpImpl->mNamespaceMaps.pop();
2402     }
2403     OSL_ENSURE(mpImpl->mNamespaceMaps.empty() ||
2404         (mpImpl->mNamespaceMaps.top().second < mpImpl->mDepth),
2405         "SvXMLExport: NamespaceMaps corrupted");
2406 
2407     if ((mnErrorFlags & ERROR_DO_NOTHING) != ERROR_DO_NOTHING)
2408     {
2409         try
2410         {
2411             if( bIgnWSInside && ((mnExportFlags & EXPORT_PRETTY) == EXPORT_PRETTY))
2412                 mxHandler->ignorableWhitespace( msWS );
2413             mxHandler->endElement( rName );
2414         }
2415         catch ( SAXException& e )
2416         {
2417             Sequence<OUString> aPars(1);
2418             aPars[0] = rName;
2419             SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
2420                       aPars, e.Message, NULL );
2421         }
2422     }
2423 }
2424 
2425 void SvXMLExport::IgnorableWhitespace()
2426 {
2427     if ((mnExportFlags & EXPORT_PRETTY) != EXPORT_PRETTY)
2428         return;
2429 
2430     if ((mnErrorFlags & ERROR_DO_NOTHING) != ERROR_DO_NOTHING)
2431     {
2432         try
2433         {
2434             mxHandler->ignorableWhitespace( msWS );
2435         }
2436         catch ( SAXException& e )
2437         {
2438             Sequence<OUString> aPars(0);
2439             SetError( XMLERROR_SAX|XMLERROR_FLAG_ERROR|XMLERROR_FLAG_SEVERE,
2440                       aPars, e.Message, NULL );
2441         }
2442     }
2443 }
2444 
2445 
2446 void SvXMLExport::SetError(
2447     sal_Int32 nId,
2448     const Sequence<OUString>& rMsgParams,
2449     const OUString& rExceptionMessage,
2450     const Reference<XLocator>& rLocator )
2451 {
2452     // allow multi-threaded access to the cancel() method
2453     static ::vos::OMutex aMutex;
2454     ::vos::OGuard aGuard(aMutex);
2455 
2456     // maintain error flags
2457     if ( ( nId & XMLERROR_FLAG_ERROR ) != 0 )
2458         mnErrorFlags |= ERROR_ERROR_OCCURED;
2459     if ( ( nId & XMLERROR_FLAG_WARNING ) != 0 )
2460         mnErrorFlags |= ERROR_WARNING_OCCURED;
2461     if ( ( nId & XMLERROR_FLAG_SEVERE ) != 0 )
2462         mnErrorFlags |= ERROR_DO_NOTHING;
2463 
2464     // create error lsit on demand
2465     if ( mpXMLErrors == NULL )
2466         mpXMLErrors = new XMLErrors();
2467 
2468     // save error information
2469     mpXMLErrors->AddRecord( nId, rMsgParams, rExceptionMessage, rLocator );
2470 }
2471 
2472 void SvXMLExport::SetError(
2473     sal_Int32 nId,
2474     const Sequence<OUString>& rMsgParams)
2475 {
2476     OUString sEmpty;
2477     SetError( nId, rMsgParams, sEmpty, NULL );
2478 }
2479 
2480 
2481 XMLErrors* SvXMLExport::GetErrors()
2482 {
2483     return mpXMLErrors;
2484 }
2485 
2486 void SvXMLExport::DisposingModel()
2487 {
2488     mxModel.clear();
2489     // --> OD 2006-03-13 #i51726#
2490     meModelType = SvtModuleOptions::E_UNKNOWN_FACTORY;;
2491     // <--
2492     mxEventListener.clear();
2493 }
2494 
2495 // #110680#
2496 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > SvXMLExport::getServiceFactory()
2497 {
2498     // #110680#
2499     return mxServiceFactory;
2500 }
2501 
2502 uno::Reference< uno::XComponentContext >
2503 SvXMLExport::GetComponentContext() const
2504 {
2505     return mpImpl->mxComponentContext;
2506 }
2507 
2508 ::comphelper::UnoInterfaceToUniqueIdentifierMapper& SvXMLExport::getInterfaceToIdentifierMapper()
2509 {
2510     return mpImpl->maInterfaceToIdentifierMapper;
2511 }
2512 
2513 // --> OD 2006-09-27 #i69627#
2514 sal_Bool SvXMLExport::writeOutlineStyleAsNormalListStyle() const
2515 {
2516     return mpImpl->mbOutlineStyleAsNormalListStyle;
2517 }
2518 // <--
2519 
2520 uno::Reference< embed::XStorage > SvXMLExport::GetTargetStorage()
2521 {
2522     return mpImpl->mxTargetStorage;
2523 }
2524 
2525 /// returns the currently configured default version for odf export
2526 SvtSaveOptions::ODFDefaultVersion SvXMLExport::getDefaultVersion() const
2527 {
2528     if( mpImpl )
2529         return mpImpl->maSaveOptions.GetODFDefaultVersion();
2530 
2531     // fatal error, use current version as default
2532     return SvtSaveOptions::ODFVER_012;
2533 }
2534 
2535 ::rtl::OUString SvXMLExport::GetStreamName() const
2536 {
2537     return mpImpl->mStreamName;
2538 }
2539 
2540 void
2541 SvXMLExport::AddAttributeIdLegacy(
2542         sal_uInt16 const nLegacyPrefix, ::rtl::OUString const& rValue)
2543 {
2544     switch (getDefaultVersion()) {
2545         case SvtSaveOptions::ODFVER_011: // fall thru
2546         case SvtSaveOptions::ODFVER_010: break;
2547         default: // ODF 1.2: xml:id
2548             AddAttribute(XML_NAMESPACE_XML, XML_ID, rValue);
2549     }
2550     // in ODF 1.1 this was form:id, anim:id, draw:id, or text:id
2551     // backward compatibility: in ODF 1.2 write _both_ id attrs
2552     AddAttribute(nLegacyPrefix, XML_ID, rValue);
2553     // FIXME: this function simply assumes that rValue is unique
2554 }
2555 
2556 void
2557 SvXMLExport::AddAttributeXmlId(uno::Reference<uno::XInterface> const & i_xIfc)
2558 {
2559     // check version >= 1.2
2560     switch (getDefaultVersion()) {
2561         case SvtSaveOptions::ODFVER_011: // fall thru
2562         case SvtSaveOptions::ODFVER_010: return;
2563         default: break;
2564     }
2565     const uno::Reference<rdf::XMetadatable> xMeta(i_xIfc,
2566         uno::UNO_QUERY);
2567 //FIXME not yet...
2568 //    OSL_ENSURE(xMeta.is(), "xml:id: not XMetadatable");
2569     if ( xMeta.is() )
2570     {
2571         const beans::StringPair mdref( xMeta->getMetadataReference() );
2572         if ( !mdref.Second.equalsAscii("") )
2573         {
2574             const ::rtl::OUString streamName( GetStreamName() );
2575             if ( streamName.getLength() )
2576             {
2577                 if ( streamName.equals(mdref.First) )
2578                 {
2579                     AddAttribute( XML_NAMESPACE_XML, XML_ID, mdref.Second );
2580                 }
2581                 else
2582                 {
2583                     OSL_ENSURE(false, "SvXMLExport::AddAttributeXmlId: "
2584                          "invalid stream name");
2585                 }
2586             }
2587             else
2588             {
2589                 // FIXME: this is ugly
2590                 // there is no stream name (e.g. XSLT, flat-xml format)!
2591                 // but how do we ensure uniqueness in this case?
2592                 // a) just omit styles.xml ids -- they are unlikely anyway...
2593                 // b) somehow find out whether we are currently exporting styles
2594                 //    or content, and prefix "s" or "c" => unique
2595                 if ( mdref.First.equalsAscii("content.xml") )
2596                 {
2597                     AddAttribute( XML_NAMESPACE_XML, XML_ID, mdref.Second );
2598                 }
2599                 else
2600                 {
2601                     OSL_TRACE("SvXMLExport::AddAttributeXmlId: "
2602                         "no stream name given: dropping styles.xml xml:id");
2603                 }
2604             }
2605         }
2606     }
2607 }
2608 
2609 void
2610 SvXMLExport::AddAttributesRDFa(
2611     uno::Reference<text::XTextContent> const & i_xTextContent)
2612 {
2613     // check version >= 1.2
2614     switch (getDefaultVersion()) {
2615         case SvtSaveOptions::ODFVER_011: // fall thru
2616         case SvtSaveOptions::ODFVER_010: return;
2617         default: break;
2618     }
2619 
2620     const uno::Reference<rdf::XMetadatable> xMeta(
2621         i_xTextContent, uno::UNO_QUERY);
2622     if (!xMeta.is() || !xMeta->getMetadataReference().Second.getLength())
2623     {
2624         return; // no xml:id => no RDFa
2625     }
2626 
2627     if (!mpImpl->mpRDFaHelper.get())
2628     {
2629         mpImpl->mpRDFaHelper.reset( new ::xmloff::RDFaExportHelper(*this) );
2630     }
2631     mpImpl->mpRDFaHelper->AddRDFa(xMeta);
2632 }
2633 
2634 // --> OD 2008-11-26 #158694#
2635 sal_Bool SvXMLExport::exportTextNumberElement() const
2636 {
2637     return mpImpl->mbExportTextNumberElement;
2638 }
2639 // <--
2640 
2641 sal_Bool SvXMLExport::SetNullDateOnUnitConverter()
2642 {
2643     // if the null date has already been set, don't set it again (performance)
2644     if (!mpImpl->mbNullDateInitialized)
2645         mpImpl->mbNullDateInitialized = GetMM100UnitConverter().setNullDate(GetModel());
2646 
2647     return mpImpl->mbNullDateInitialized;
2648 }
2649 
2650 //=============================================================================
2651 
2652 void SvXMLElementExport::StartElement( SvXMLExport& rExp,
2653                                        sal_uInt16 nPrefixKey,
2654                                        const OUString& rLName,
2655                                        sal_Bool bIWSOutside )
2656 {
2657     aName = rExp.GetNamespaceMap().GetQNameByKey(nPrefixKey, rLName);
2658     rExp.StartElement(aName, bIWSOutside);
2659 }
2660 
2661 SvXMLElementExport::SvXMLElementExport( SvXMLExport& rExp,
2662                                         sal_uInt16 nPrefixKey,
2663                                         const sal_Char *pLName,
2664                                         sal_Bool bIWSOutside,
2665                                         sal_Bool bIWSInside ) :
2666     rExport( rExp ),
2667     bIgnWS( bIWSInside ),
2668     bDoSomething( sal_True )
2669 {
2670     OUString sLName( OUString::createFromAscii(pLName) );
2671     StartElement( rExp, nPrefixKey, sLName, bIWSOutside );
2672 }
2673 
2674 SvXMLElementExport::SvXMLElementExport( SvXMLExport& rExp,
2675                                         sal_uInt16 nPrefixKey,
2676                                         const OUString& rLName,
2677                                         sal_Bool bIWSOutside,
2678                                         sal_Bool bIWSInside ) :
2679     rExport( rExp ),
2680     bIgnWS( bIWSInside ),
2681     bDoSomething( sal_True )
2682 {
2683     StartElement( rExp, nPrefixKey, rLName, bIWSOutside );
2684 }
2685 
2686 SvXMLElementExport::SvXMLElementExport( SvXMLExport& rExp,
2687                                         sal_uInt16 nPrefixKey,
2688                                         enum XMLTokenEnum eLName,
2689                                         sal_Bool bIWSOutside,
2690                                         sal_Bool bIWSInside ) :
2691     rExport( rExp ),
2692     bIgnWS( bIWSInside ),
2693     bDoSomething( sal_True )
2694 {
2695     StartElement( rExp, nPrefixKey, GetXMLToken(eLName), bIWSOutside );
2696 }
2697 
2698 SvXMLElementExport::SvXMLElementExport( SvXMLExport& rExp,
2699                                         sal_Bool bDoSth,
2700                                         sal_uInt16 nPrefixKey,
2701                                         const sal_Char *pLName,
2702                                         sal_Bool bIWSOutside,
2703                                         sal_Bool bIWSInside ) :
2704     rExport( rExp ),
2705     bIgnWS( bIWSInside ),
2706     bDoSomething( bDoSth )
2707 {
2708     if( bDoSomething )
2709     {
2710         OUString sLName( OUString::createFromAscii(pLName) );
2711         StartElement( rExp, nPrefixKey, sLName, bIWSOutside );
2712     }
2713 }
2714 
2715 SvXMLElementExport::SvXMLElementExport( SvXMLExport& rExp,
2716                                         sal_Bool bDoSth,
2717                                         sal_uInt16 nPrefixKey,
2718                                         const OUString& rLName,
2719                                         sal_Bool bIWSOutside,
2720                                         sal_Bool bIWSInside ) :
2721     rExport( rExp ),
2722     bIgnWS( bIWSInside ),
2723     bDoSomething( bDoSth )
2724 {
2725     if( bDoSomething )
2726         StartElement( rExp, nPrefixKey, rLName, bIWSOutside );
2727 }
2728 
2729 SvXMLElementExport::SvXMLElementExport( SvXMLExport& rExp,
2730                                         sal_Bool bDoSth,
2731                                         sal_uInt16 nPrefixKey,
2732                                         enum XMLTokenEnum eLName,
2733                                         sal_Bool bIWSOutside,
2734                                         sal_Bool bIWSInside ) :
2735     rExport( rExp ),
2736     bIgnWS( bIWSInside ),
2737     bDoSomething( bDoSth )
2738 {
2739     if( bDoSomething )
2740         StartElement( rExport, nPrefixKey, GetXMLToken(eLName), bIWSOutside );
2741 }
2742 
2743 SvXMLElementExport::SvXMLElementExport( SvXMLExport& rExp,
2744                                         const OUString& rQName,
2745                                         sal_Bool bIWSOutside,
2746                                         sal_Bool bIWSInside ) :
2747     rExport( rExp ),
2748     bIgnWS( bIWSInside ),
2749     bDoSomething( sal_True )
2750 {
2751     aName = rQName;
2752     rExp.StartElement( rQName, bIWSOutside );
2753 }
2754 
2755 SvXMLElementExport::~SvXMLElementExport()
2756 {
2757     if( bDoSomething )
2758     {
2759         rExport.EndElement( aName, bIgnWS );
2760     }
2761 }
2762 
2763