xref: /aoo41x/main/svx/source/xml/xmlxtexp.cxx (revision cdf0e10c)
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_svx.hxx"
30 #include <tools/debug.hxx>
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
35 #include <com/sun/star/drawing/LineDash.hpp>
36 #include <com/sun/star/awt/Gradient.hpp>
37 #include <com/sun/star/drawing/Hatch.hpp>
38 #include <com/sun/star/io/XActiveDataSource.hpp>
39 #include <com/sun/star/embed/ElementModes.hpp>
40 #include <sfx2/docfile.hxx>
41 #include <rtl/ustrbuf.hxx>
42 #include <xmloff/xmluconv.hxx>
43 #include "xmloff/xmlnmspe.hxx"
44 #include "xmloff/nmspmap.hxx"
45 
46 #include "xmloff/xmltoken.hxx"
47 #include "xmloff/xmlmetae.hxx"
48 #include "xmloff/DashStyle.hxx"
49 #include "xmloff/GradientStyle.hxx"
50 #include "xmloff/HatchStyle.hxx"
51 #include "xmloff/ImageStyle.hxx"
52 #include "xmloff/MarkerStyle.hxx"
53 #include <com/sun/star/container/XNameContainer.hpp>
54 #include <com/sun/star/embed/XTransactedObject.hpp>
55 #include <comphelper/processfactory.hxx>
56 #include <unotools/streamwrap.hxx>
57 #include "svx/xmlgrhlp.hxx"
58 
59 #include "xmlxtexp.hxx"
60 
61 #include <comphelper/storagehelper.hxx>
62 
63 using namespace com::sun::star;
64 using namespace com::sun::star::container;
65 using namespace com::sun::star::document;
66 using namespace com::sun::star::uno;
67 using namespace com::sun::star::awt;
68 using namespace com::sun::star::lang;
69 using namespace com::sun::star::xml::sax;
70 using namespace ::xmloff::token;
71 using namespace ::rtl;
72 using namespace cppu;
73 
74 using com::sun::star::embed::XTransactedObject;
75 
76 class SvxXMLTableEntryExporter
77 {
78 public:
79 	SvxXMLTableEntryExporter( SvXMLExport& rExport ) : mrExport( rExport ) {}
80 	virtual ~SvxXMLTableEntryExporter();
81 
82 	virtual void exportEntry( const OUString& rStrName, const Any& rValue ) = 0;
83 
84 protected:
85 	SvXMLExport& mrExport;
86 };
87 
88 class SvxXMLColorEntryExporter : public SvxXMLTableEntryExporter
89 {
90 public:
91 	SvxXMLColorEntryExporter( SvXMLExport& rExport );
92 	virtual ~SvxXMLColorEntryExporter();
93 
94 	virtual void exportEntry( const OUString& rStrName, const Any& rValue );
95 };
96 
97 class SvxXMLLineEndEntryExporter : public SvxXMLTableEntryExporter
98 {
99 public:
100 	SvxXMLLineEndEntryExporter( SvXMLExport& rExport );
101 	SvxXMLLineEndEntryExporter();
102 	virtual ~SvxXMLLineEndEntryExporter();
103 
104 	virtual void exportEntry( const OUString& rStrName, const Any& rValue );
105 private:
106 	XMLMarkerStyleExport maMarkerStyle;
107 };
108 
109 class SvxXMLDashEntryExporter : public SvxXMLTableEntryExporter
110 {
111 public:
112 	SvxXMLDashEntryExporter( SvXMLExport& rExport );
113 	virtual ~SvxXMLDashEntryExporter();
114 
115 	virtual void exportEntry( const OUString& rStrName, const Any& rValue );
116 
117 private:
118 	XMLDashStyleExport maDashStyle;
119 };
120 
121 class SvxXMLHatchEntryExporter : public SvxXMLTableEntryExporter
122 {
123 public:
124 	SvxXMLHatchEntryExporter( SvXMLExport& rExport );
125 	virtual ~SvxXMLHatchEntryExporter();
126 
127 	virtual void exportEntry( const OUString& rStrName, const Any& rValue );
128 private:
129 	XMLHatchStyleExport maHatchStyle;
130 };
131 
132 class SvxXMLGradientEntryExporter : public SvxXMLTableEntryExporter
133 {
134 public:
135 	SvxXMLGradientEntryExporter( SvXMLExport& rExport );
136 	virtual ~SvxXMLGradientEntryExporter();
137 
138 	virtual void exportEntry( const OUString& rStrName, const Any& rValue );
139 private:
140 	XMLGradientStyleExport maGradientStyle;
141 };
142 
143 class SvxXMLBitmapEntryExporter : public SvxXMLTableEntryExporter
144 {
145 public:
146 	SvxXMLBitmapEntryExporter( SvXMLExport& rExport );
147 	virtual ~SvxXMLBitmapEntryExporter();
148 
149 	virtual void exportEntry( const OUString& rStrName, const Any& rValue );
150 
151 private:
152 	XMLImageStyle maImageStyle;
153 };
154 
155 ///////////////////////////////////////////////////////////////////////
156 
157 // #110680#
158 SvxXMLXTableExportComponent::SvxXMLXTableExportComponent(
159 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
160 	const OUString& rFileName,
161 	const uno::Reference<xml::sax::XDocumentHandler> & rHandler,
162 	const uno::Reference<container::XNameContainer >& xTable,
163 	uno::Reference<document::XGraphicObjectResolver >& xGrfResolver )
164 :	SvXMLExport( xServiceFactory, rFileName, rHandler, NULL, MAP_100TH_MM),
165 	mxTable( xTable )
166 {
167 
168 	_GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
169 	_GetNamespaceMap().Add( GetXMLToken(XML_NP_OFFICE), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
170 	_GetNamespaceMap().Add( GetXMLToken(XML_NP_DRAW), GetXMLToken(XML_N_DRAW), XML_NAMESPACE_DRAW );
171 	_GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
172 	_GetNamespaceMap().Add( GetXMLToken(XML_NP_SVG), GetXMLToken(XML_N_SVG),  XML_NAMESPACE_SVG );
173 	SetGraphicResolver( xGrfResolver );
174 	setExportFlags( 0 );
175 }
176 
177 SvxXMLXTableExportComponent::~SvxXMLXTableExportComponent()
178 {
179 }
180 
181 sal_Bool SvxXMLXTableExportComponent::save( const OUString& rURL, const uno::Reference<container::XNameContainer >& xTable ) throw()
182 {
183     uno::Reference < embed::XStorage > xStorage;
184 	SfxMedium* pMedium = NULL;
185 	sal_Bool bRet = sal_False;
186 
187 	uno::Reference< XGraphicObjectResolver >	xGrfResolver;
188 	SvXMLGraphicHelper*	pGraphicHelper = 0;
189 
190 	try
191 	{
192 		do
193 		{
194             uno::Reference < io::XOutputStream > xOut;
195             uno::Reference < io::XStream > xStream;
196 
197 			sal_Bool bNeedStorage = xTable->getElementType() == ::getCppuType((const OUString*)0);
198 
199 			uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
200 			if( !xServiceFactory.is() )
201 			{
202 				DBG_ERROR( "got no service manager" );
203 				return sal_False;
204 			}
205 
206 			uno::Reference< uno::XInterface > xWriter( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ) );
207 			if( !xWriter.is() )
208 			{
209 				DBG_ERROR( "com.sun.star.xml.sax.Writer service missing" );
210 				return sal_False;
211 			}
212 
213 			uno::Reference<xml::sax::XDocumentHandler>	xHandler( xWriter, uno::UNO_QUERY );
214 
215 			if( bNeedStorage )
216 			{
217                 xStorage =
218                   ::comphelper::OStorageHelper::GetStorageFromURL( rURL, embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
219 
220                 if( !xStorage.is() )
221 				{
222 					DBG_ERROR( "no storage!" );
223 					break;
224 				}
225 
226 				OUString sMetaName( RTL_CONSTASCII_USTRINGPARAM( "Content.xml" ) );
227                 xStream = xStorage->openStreamElement( sMetaName, embed::ElementModes::WRITE );
228                 pGraphicHelper = SvXMLGraphicHelper::Create( xStorage, GRAPHICHELPER_MODE_WRITE );
229 				xGrfResolver = pGraphicHelper;
230                 xOut = xStream->getOutputStream();
231 			}
232 			else
233 			{
234 				pMedium = new SfxMedium( rURL, STREAM_WRITE | STREAM_TRUNC, sal_True );
235 				pMedium->IsRemote();
236 
237 				SvStream* pStream = pMedium->GetOutStream();
238 				if( NULL == pStream )
239 				{
240 					DBG_ERROR( "no output stream!" );
241 					break;
242 				}
243 
244 				xOut = new utl::OOutputStreamWrapper( *pStream );
245 			}
246 
247 			uno::Reference<io::XActiveDataSource> xMetaSrc( xWriter, uno::UNO_QUERY );
248 			xMetaSrc->setOutputStream( xOut );
249 
250 			const OUString aName;
251 
252 			// #110680#
253 			// SvxXMLXTableExportComponent aExporter( aName, xHandler, xTable, xGrfResolver );
254 			SvxXMLXTableExportComponent aExporter( xServiceFactory, aName, xHandler, xTable, xGrfResolver );
255 
256 			bRet = aExporter.exportTable();
257 
258 		}
259 		while( 0 );
260 
261 		if( pGraphicHelper )
262 			SvXMLGraphicHelper::Destroy( pGraphicHelper );
263 
264 		if( xStorage.is() )
265 		{
266 			uno::Reference< XTransactedObject > xTrans( xStorage, UNO_QUERY );
267 			if( xTrans.is() )
268 				xTrans->commit();
269 
270 			uno::Reference< XComponent > xComp( xStorage, UNO_QUERY );
271 			if( xComp.is() )
272 				xStorage->dispose();
273 		}
274 	}
275 	catch( uno::Exception& )
276 	{
277 		bRet = sal_False;
278 	}
279 
280 	if( pMedium )
281 	{
282 		pMedium->Commit();
283 		delete pMedium;
284 	}
285 
286 	return bRet;
287 }
288 
289 sal_Bool SvxXMLXTableExportComponent::exportTable() throw()
290 {
291 	sal_Bool bRet = sal_False;
292 
293 	try
294 	{
295 		GetDocHandler()->startDocument();
296 
297 		// export namespaces
298 		sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
299 		while( USHRT_MAX != nPos )
300 		{
301 			GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
302 			nPos = GetNamespaceMap().GetNextKey( nPos );
303 		}
304 
305 		do
306 		{
307 			if( !mxTable.is() )
308 				break;
309 
310 			char const* pEleName;
311 			Type aExportType = mxTable->getElementType();
312 			SvxXMLTableEntryExporter* pExporter = NULL;
313 
314 			if( aExportType == ::getCppuType((const sal_Int32*)0) )
315 			{
316 				pExporter = new SvxXMLColorEntryExporter(*this);
317 				pEleName = "color-table";
318 			}
319 			else if( aExportType == ::getCppuType((const drawing::PolyPolygonBezierCoords*)0) )
320 			{
321 				pExporter = new SvxXMLLineEndEntryExporter(*this);
322 				pEleName = "marker-table";
323 			}
324 			else if( aExportType == ::getCppuType((const drawing::LineDash*)0) )
325 			{
326 				pExporter = new SvxXMLDashEntryExporter(*this);
327 				pEleName = "dash-table";
328 			}
329 			else if( aExportType == ::getCppuType((const drawing::Hatch*)0) )
330 			{
331 				pExporter = new SvxXMLHatchEntryExporter(*this);
332 				pEleName = "hatch-table";
333 			}
334 			else if( aExportType == ::getCppuType((const awt::Gradient*)0))
335 			{
336 				pExporter = new SvxXMLGradientEntryExporter(*this);
337 				pEleName = "gradient-table";
338 			}
339 			else if( aExportType == ::getCppuType((const OUString*)0))
340 			{
341 				pExporter = new SvxXMLBitmapEntryExporter(*this);
342 				pEleName = "bitmap-table";
343 			}
344 			else
345 			{
346 				DBG_ERROR( "unknown type for export");
347 				break;
348 			}
349 
350 			SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, sal_True, sal_True );
351 
352 			Sequence< OUString > aNames = mxTable->getElementNames();
353 			const sal_Int32 nCount = aNames.getLength();
354 			const OUString* pNames = aNames.getConstArray();
355 			Any aAny;
356 
357 			sal_Int32 nIndex;
358 			for( nIndex = 0; nIndex < nCount; nIndex++, pNames++ )
359 			{
360 				aAny = mxTable->getByName( *pNames );
361 				pExporter->exportEntry( *pNames, aAny );
362 			}
363 
364 			bRet = sal_True;
365 		}
366 		while(0);
367 
368 		GetDocHandler()->endDocument();
369 	}
370 	catch( Exception e )
371 	{
372 		bRet = sal_False;
373 	}
374 
375 	return bRet;
376 }
377 
378 // methods without content:
379 void SvxXMLXTableExportComponent::_ExportAutoStyles() {}
380 void SvxXMLXTableExportComponent::_ExportMasterStyles() {}
381 void SvxXMLXTableExportComponent::_ExportContent() {}
382 
383 ///////////////////////////////////////////////////////////////////////
384 
385 SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
386 {
387 }
388 
389 ///////////////////////////////////////////////////////////////////////
390 
391 SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
392 : SvxXMLTableEntryExporter( rExport )
393 {
394 }
395 
396 SvxXMLColorEntryExporter::~SvxXMLColorEntryExporter()
397 {
398 }
399 
400 void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
401 {
402 	mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
403 
404 	sal_Int32 nColor = 0;
405 	rValue >>= nColor;
406 
407 	OUStringBuffer aOut;
408 	mrExport.GetMM100UnitConverter().convertColor( aOut, nColor );
409 	mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
410 
411 	SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, sal_True, sal_True );
412 }
413 
414 ///////////////////////////////////////////////////////////////////////
415 
416 SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
417 : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
418 {
419 }
420 
421 SvxXMLLineEndEntryExporter::~SvxXMLLineEndEntryExporter()
422 {
423 }
424 
425 void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
426 {
427 	maMarkerStyle.exportXML( rStrName, rValue );
428 }
429 
430 ///////////////////////////////////////////////////////////////////////
431 
432 SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
433 : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
434 {
435 }
436 
437 SvxXMLDashEntryExporter::~SvxXMLDashEntryExporter()
438 {
439 }
440 
441 void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
442 {
443 	maDashStyle.exportXML( rStrName, rValue );
444 }
445 
446 ///////////////////////////////////////////////////////////////////////
447 
448 SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
449 : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
450 {
451 }
452 
453 SvxXMLHatchEntryExporter::~SvxXMLHatchEntryExporter()
454 {
455 }
456 
457 void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
458 {
459 	maHatchStyle.exportXML( rStrName, rValue );
460 }
461 
462 ///////////////////////////////////////////////////////////////////////
463 
464 SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
465 : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
466 {
467 }
468 
469 SvxXMLGradientEntryExporter::~SvxXMLGradientEntryExporter()
470 {
471 }
472 
473 void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
474 {
475 	maGradientStyle.exportXML( rStrName, rValue );
476 }
477 
478 ///////////////////////////////////////////////////////////////////////
479 
480 SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
481 : SvxXMLTableEntryExporter( rExport )
482 {
483 }
484 
485 SvxXMLBitmapEntryExporter::~SvxXMLBitmapEntryExporter()
486 {
487 }
488 
489 void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
490 {
491 	maImageStyle.exportXML( rStrName, rValue, mrExport );
492 }
493