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