Update ooo320-m1
[ooovba.git] / svx / source / xml / xmlxtexp.cxx
blob4106cc00a72f0eb7810b987f28885c0d7a521646
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlxtexp.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <tools/debug.hxx>
34 #include <com/sun/star/container/XNameContainer.hpp>
35 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
36 #include <com/sun/star/uno/Sequence.hxx>
37 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
38 #include <com/sun/star/drawing/LineDash.hpp>
39 #include <com/sun/star/awt/Gradient.hpp>
40 #include <com/sun/star/drawing/Hatch.hpp>
41 #include <com/sun/star/io/XActiveDataSource.hpp>
42 #include <com/sun/star/embed/ElementModes.hpp>
43 #include <sfx2/docfile.hxx>
44 #include <rtl/ustrbuf.hxx>
45 #include <xmloff/xmluconv.hxx>
46 #include "xmloff/xmlnmspe.hxx"
47 #include "xmloff/nmspmap.hxx"
49 #include "xmloff/xmltoken.hxx"
50 #include "xmloff/xmlmetae.hxx"
51 #include "xmloff/DashStyle.hxx"
52 #include "xmloff/GradientStyle.hxx"
53 #include "xmloff/HatchStyle.hxx"
54 #include "xmloff/ImageStyle.hxx"
55 #include "xmloff/MarkerStyle.hxx"
56 #include <com/sun/star/container/XNameContainer.hpp>
57 #include <com/sun/star/embed/XTransactedObject.hpp>
58 #include <comphelper/processfactory.hxx>
59 #include <unotools/streamwrap.hxx>
60 #include "xmlgrhlp.hxx"
62 #include "xmlxtexp.hxx"
64 #include <comphelper/storagehelper.hxx>
66 using namespace com::sun::star;
67 using namespace com::sun::star::container;
68 using namespace com::sun::star::document;
69 using namespace com::sun::star::uno;
70 using namespace com::sun::star::awt;
71 using namespace com::sun::star::lang;
72 using namespace com::sun::star::xml::sax;
73 using namespace ::xmloff::token;
74 using namespace ::rtl;
75 using namespace cppu;
77 using com::sun::star::embed::XTransactedObject;
79 class SvxXMLTableEntryExporter
81 public:
82 SvxXMLTableEntryExporter( SvXMLExport& rExport ) : mrExport( rExport ) {}
83 virtual ~SvxXMLTableEntryExporter();
85 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) = 0;
87 protected:
88 SvXMLExport& mrExport;
91 class SvxXMLColorEntryExporter : public SvxXMLTableEntryExporter
93 public:
94 SvxXMLColorEntryExporter( SvXMLExport& rExport );
95 virtual ~SvxXMLColorEntryExporter();
97 virtual void exportEntry( const OUString& rStrName, const Any& rValue );
100 class SvxXMLLineEndEntryExporter : public SvxXMLTableEntryExporter
102 public:
103 SvxXMLLineEndEntryExporter( SvXMLExport& rExport );
104 SvxXMLLineEndEntryExporter();
105 virtual ~SvxXMLLineEndEntryExporter();
107 virtual void exportEntry( const OUString& rStrName, const Any& rValue );
108 private:
109 XMLMarkerStyleExport maMarkerStyle;
112 class SvxXMLDashEntryExporter : public SvxXMLTableEntryExporter
114 public:
115 SvxXMLDashEntryExporter( SvXMLExport& rExport );
116 virtual ~SvxXMLDashEntryExporter();
118 virtual void exportEntry( const OUString& rStrName, const Any& rValue );
120 private:
121 XMLDashStyleExport maDashStyle;
124 class SvxXMLHatchEntryExporter : public SvxXMLTableEntryExporter
126 public:
127 SvxXMLHatchEntryExporter( SvXMLExport& rExport );
128 virtual ~SvxXMLHatchEntryExporter();
130 virtual void exportEntry( const OUString& rStrName, const Any& rValue );
131 private:
132 XMLHatchStyleExport maHatchStyle;
135 class SvxXMLGradientEntryExporter : public SvxXMLTableEntryExporter
137 public:
138 SvxXMLGradientEntryExporter( SvXMLExport& rExport );
139 virtual ~SvxXMLGradientEntryExporter();
141 virtual void exportEntry( const OUString& rStrName, const Any& rValue );
142 private:
143 XMLGradientStyleExport maGradientStyle;
146 class SvxXMLBitmapEntryExporter : public SvxXMLTableEntryExporter
148 public:
149 SvxXMLBitmapEntryExporter( SvXMLExport& rExport );
150 virtual ~SvxXMLBitmapEntryExporter();
152 virtual void exportEntry( const OUString& rStrName, const Any& rValue );
154 private:
155 XMLImageStyle maImageStyle;
158 ///////////////////////////////////////////////////////////////////////
160 // #110680#
161 SvxXMLXTableExportComponent::SvxXMLXTableExportComponent(
162 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
163 const OUString& rFileName,
164 const uno::Reference<xml::sax::XDocumentHandler> & rHandler,
165 const uno::Reference<container::XNameContainer >& xTable,
166 uno::Reference<document::XGraphicObjectResolver >& xGrfResolver )
167 : SvXMLExport( xServiceFactory, rFileName, rHandler, NULL, MAP_100TH_MM),
168 mxTable( xTable )
171 _GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
172 _GetNamespaceMap().Add( GetXMLToken(XML_NP_OFFICE), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
173 _GetNamespaceMap().Add( GetXMLToken(XML_NP_DRAW), GetXMLToken(XML_N_DRAW), XML_NAMESPACE_DRAW );
174 _GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
175 _GetNamespaceMap().Add( GetXMLToken(XML_NP_SVG), GetXMLToken(XML_N_SVG), XML_NAMESPACE_SVG );
176 SetGraphicResolver( xGrfResolver );
177 setExportFlags( 0 );
180 SvxXMLXTableExportComponent::~SvxXMLXTableExportComponent()
184 sal_Bool SvxXMLXTableExportComponent::save( const OUString& rURL, const uno::Reference<container::XNameContainer >& xTable ) throw()
186 uno::Reference < embed::XStorage > xStorage;
187 SfxMedium* pMedium = NULL;
188 sal_Bool bRet = sal_False;
190 uno::Reference< XGraphicObjectResolver > xGrfResolver;
191 SvXMLGraphicHelper* pGraphicHelper = 0;
197 uno::Reference < io::XOutputStream > xOut;
198 uno::Reference < io::XStream > xStream;
200 sal_Bool bNeedStorage = xTable->getElementType() == ::getCppuType((const OUString*)0);
202 uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
203 if( !xServiceFactory.is() )
205 DBG_ERROR( "got no service manager" );
206 return FALSE;
209 uno::Reference< uno::XInterface > xWriter( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ) );
210 if( !xWriter.is() )
212 DBG_ERROR( "com.sun.star.xml.sax.Writer service missing" );
213 return FALSE;
216 uno::Reference<xml::sax::XDocumentHandler> xHandler( xWriter, uno::UNO_QUERY );
218 if( bNeedStorage )
220 xStorage =
221 ::comphelper::OStorageHelper::GetStorageFromURL( rURL, embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
223 if( !xStorage.is() )
225 DBG_ERROR( "no storage!" );
226 break;
229 OUString sMetaName( RTL_CONSTASCII_USTRINGPARAM( "Content.xml" ) );
230 xStream = xStorage->openStreamElement( sMetaName, embed::ElementModes::WRITE );
231 pGraphicHelper = SvXMLGraphicHelper::Create( xStorage, GRAPHICHELPER_MODE_WRITE );
232 xGrfResolver = pGraphicHelper;
233 xOut = xStream->getOutputStream();
235 else
237 pMedium = new SfxMedium( rURL, STREAM_WRITE | STREAM_TRUNC, TRUE );
238 pMedium->IsRemote();
240 SvStream* pStream = pMedium->GetOutStream();
241 if( NULL == pStream )
243 DBG_ERROR( "no output stream!" );
244 break;
247 xOut = new utl::OOutputStreamWrapper( *pStream );
250 uno::Reference<io::XActiveDataSource> xMetaSrc( xWriter, uno::UNO_QUERY );
251 xMetaSrc->setOutputStream( xOut );
253 const OUString aName;
255 // #110680#
256 // SvxXMLXTableExportComponent aExporter( aName, xHandler, xTable, xGrfResolver );
257 SvxXMLXTableExportComponent aExporter( xServiceFactory, aName, xHandler, xTable, xGrfResolver );
259 bRet = aExporter.exportTable();
262 while( 0 );
264 if( pGraphicHelper )
265 SvXMLGraphicHelper::Destroy( pGraphicHelper );
267 if( xStorage.is() )
269 uno::Reference< XTransactedObject > xTrans( xStorage, UNO_QUERY );
270 if( xTrans.is() )
271 xTrans->commit();
273 uno::Reference< XComponent > xComp( xStorage, UNO_QUERY );
274 if( xComp.is() )
275 xStorage->dispose();
278 catch( uno::Exception& )
280 bRet = sal_False;
283 if( pMedium )
285 pMedium->Commit();
286 delete pMedium;
289 return bRet;
292 sal_Bool SvxXMLXTableExportComponent::exportTable() throw()
294 sal_Bool bRet = sal_False;
298 GetDocHandler()->startDocument();
300 // export namespaces
301 sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
302 while( USHRT_MAX != nPos )
304 GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
305 nPos = GetNamespaceMap().GetNextKey( nPos );
310 if( !mxTable.is() )
311 break;
313 char const* pEleName;
314 Type aExportType = mxTable->getElementType();
315 SvxXMLTableEntryExporter* pExporter = NULL;
317 if( aExportType == ::getCppuType((const sal_Int32*)0) )
319 pExporter = new SvxXMLColorEntryExporter(*this);
320 pEleName = "color-table";
322 else if( aExportType == ::getCppuType((const drawing::PolyPolygonBezierCoords*)0) )
324 pExporter = new SvxXMLLineEndEntryExporter(*this);
325 pEleName = "marker-table";
327 else if( aExportType == ::getCppuType((const drawing::LineDash*)0) )
329 pExporter = new SvxXMLDashEntryExporter(*this);
330 pEleName = "dash-table";
332 else if( aExportType == ::getCppuType((const drawing::Hatch*)0) )
334 pExporter = new SvxXMLHatchEntryExporter(*this);
335 pEleName = "hatch-table";
337 else if( aExportType == ::getCppuType((const awt::Gradient*)0))
339 pExporter = new SvxXMLGradientEntryExporter(*this);
340 pEleName = "gradient-table";
342 else if( aExportType == ::getCppuType((const OUString*)0))
344 pExporter = new SvxXMLBitmapEntryExporter(*this);
345 pEleName = "bitmap-table";
347 else
349 DBG_ERROR( "unknown type for export");
350 break;
353 SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, sal_True, sal_True );
355 Sequence< OUString > aNames = mxTable->getElementNames();
356 const sal_Int32 nCount = aNames.getLength();
357 const OUString* pNames = aNames.getConstArray();
358 Any aAny;
360 sal_Int32 nIndex;
361 for( nIndex = 0; nIndex < nCount; nIndex++, pNames++ )
363 aAny = mxTable->getByName( *pNames );
364 pExporter->exportEntry( *pNames, aAny );
367 bRet = sal_True;
369 while(0);
371 GetDocHandler()->endDocument();
373 catch( Exception e )
375 bRet = sal_False;
378 return bRet;
381 // methods without content:
382 void SvxXMLXTableExportComponent::_ExportAutoStyles() {}
383 void SvxXMLXTableExportComponent::_ExportMasterStyles() {}
384 void SvxXMLXTableExportComponent::_ExportContent() {}
386 ///////////////////////////////////////////////////////////////////////
388 SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
392 ///////////////////////////////////////////////////////////////////////
394 SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
395 : SvxXMLTableEntryExporter( rExport )
399 SvxXMLColorEntryExporter::~SvxXMLColorEntryExporter()
403 void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
405 mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
407 sal_Int32 nColor = 0;
408 rValue >>= nColor;
410 OUStringBuffer aOut;
411 mrExport.GetMM100UnitConverter().convertColor( aOut, nColor );
412 mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
414 SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, sal_True, sal_True );
417 ///////////////////////////////////////////////////////////////////////
419 SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
420 : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
424 SvxXMLLineEndEntryExporter::~SvxXMLLineEndEntryExporter()
428 void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
430 maMarkerStyle.exportXML( rStrName, rValue );
433 ///////////////////////////////////////////////////////////////////////
435 SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
436 : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
440 SvxXMLDashEntryExporter::~SvxXMLDashEntryExporter()
444 void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
446 maDashStyle.exportXML( rStrName, rValue );
449 ///////////////////////////////////////////////////////////////////////
451 SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
452 : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
456 SvxXMLHatchEntryExporter::~SvxXMLHatchEntryExporter()
460 void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
462 maHatchStyle.exportXML( rStrName, rValue );
465 ///////////////////////////////////////////////////////////////////////
467 SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
468 : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
472 SvxXMLGradientEntryExporter::~SvxXMLGradientEntryExporter()
476 void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
478 maGradientStyle.exportXML( rStrName, rValue );
481 ///////////////////////////////////////////////////////////////////////
483 SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
484 : SvxXMLTableEntryExporter( rExport )
488 SvxXMLBitmapEntryExporter::~SvxXMLBitmapEntryExporter()
492 void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
494 maImageStyle.exportXML( rStrName, rValue, mrExport );