1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlxtexp.cxx,v $
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
;
77 using com::sun::star::embed::XTransactedObject
;
79 class SvxXMLTableEntryExporter
82 SvxXMLTableEntryExporter( SvXMLExport
& rExport
) : mrExport( rExport
) {}
83 virtual ~SvxXMLTableEntryExporter();
85 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
) = 0;
88 SvXMLExport
& mrExport
;
91 class SvxXMLColorEntryExporter
: public SvxXMLTableEntryExporter
94 SvxXMLColorEntryExporter( SvXMLExport
& rExport
);
95 virtual ~SvxXMLColorEntryExporter();
97 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
100 class SvxXMLLineEndEntryExporter
: public SvxXMLTableEntryExporter
103 SvxXMLLineEndEntryExporter( SvXMLExport
& rExport
);
104 SvxXMLLineEndEntryExporter();
105 virtual ~SvxXMLLineEndEntryExporter();
107 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
109 XMLMarkerStyleExport maMarkerStyle
;
112 class SvxXMLDashEntryExporter
: public SvxXMLTableEntryExporter
115 SvxXMLDashEntryExporter( SvXMLExport
& rExport
);
116 virtual ~SvxXMLDashEntryExporter();
118 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
121 XMLDashStyleExport maDashStyle
;
124 class SvxXMLHatchEntryExporter
: public SvxXMLTableEntryExporter
127 SvxXMLHatchEntryExporter( SvXMLExport
& rExport
);
128 virtual ~SvxXMLHatchEntryExporter();
130 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
132 XMLHatchStyleExport maHatchStyle
;
135 class SvxXMLGradientEntryExporter
: public SvxXMLTableEntryExporter
138 SvxXMLGradientEntryExporter( SvXMLExport
& rExport
);
139 virtual ~SvxXMLGradientEntryExporter();
141 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
143 XMLGradientStyleExport maGradientStyle
;
146 class SvxXMLBitmapEntryExporter
: public SvxXMLTableEntryExporter
149 SvxXMLBitmapEntryExporter( SvXMLExport
& rExport
);
150 virtual ~SvxXMLBitmapEntryExporter();
152 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
155 XMLImageStyle maImageStyle
;
158 ///////////////////////////////////////////////////////////////////////
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
),
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
);
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" );
209 uno::Reference
< uno::XInterface
> xWriter( xServiceFactory
->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ) );
212 DBG_ERROR( "com.sun.star.xml.sax.Writer service missing" );
216 uno::Reference
<xml::sax::XDocumentHandler
> xHandler( xWriter
, uno::UNO_QUERY
);
221 ::comphelper::OStorageHelper::GetStorageFromURL( rURL
, embed::ElementModes::WRITE
| embed::ElementModes::TRUNCATE
);
225 DBG_ERROR( "no storage!" );
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();
237 pMedium
= new SfxMedium( rURL
, STREAM_WRITE
| STREAM_TRUNC
, TRUE
);
240 SvStream
* pStream
= pMedium
->GetOutStream();
241 if( NULL
== pStream
)
243 DBG_ERROR( "no output stream!" );
247 xOut
= new utl::OOutputStreamWrapper( *pStream
);
250 uno::Reference
<io::XActiveDataSource
> xMetaSrc( xWriter
, uno::UNO_QUERY
);
251 xMetaSrc
->setOutputStream( xOut
);
253 const OUString aName
;
256 // SvxXMLXTableExportComponent aExporter( aName, xHandler, xTable, xGrfResolver );
257 SvxXMLXTableExportComponent
aExporter( xServiceFactory
, aName
, xHandler
, xTable
, xGrfResolver
);
259 bRet
= aExporter
.exportTable();
265 SvXMLGraphicHelper::Destroy( pGraphicHelper
);
269 uno::Reference
< XTransactedObject
> xTrans( xStorage
, UNO_QUERY
);
273 uno::Reference
< XComponent
> xComp( xStorage
, UNO_QUERY
);
278 catch( uno::Exception
& )
292 sal_Bool
SvxXMLXTableExportComponent::exportTable() throw()
294 sal_Bool bRet
= sal_False
;
298 GetDocHandler()->startDocument();
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
);
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";
349 DBG_ERROR( "unknown type for export");
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();
361 for( nIndex
= 0; nIndex
< nCount
; nIndex
++, pNames
++ )
363 aAny
= mxTable
->getByName( *pNames
);
364 pExporter
->exportEntry( *pNames
, aAny
);
371 GetDocHandler()->endDocument();
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;
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
);