Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / xml / xmlxtexp.cxx
blob48743e919512ca14e2445b5cf8560c25a68e42f3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/urlobj.hxx>
21 #include <com/sun/star/container/XNameContainer.hpp>
22 #include <com/sun/star/xml/sax/Writer.hpp>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
25 #include <com/sun/star/drawing/LineDash.hpp>
26 #include <com/sun/star/awt/Gradient.hpp>
27 #include <com/sun/star/drawing/Hatch.hpp>
28 #include <com/sun/star/io/XActiveDataSource.hpp>
29 #include <com/sun/star/embed/ElementModes.hpp>
31 #include <sax/tools/converter.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include "xmloff/xmlnmspe.hxx"
35 #include "xmloff/nmspmap.hxx"
37 #include "xmloff/xmltoken.hxx"
38 #include "xmloff/xmlmetae.hxx"
39 #include "xmloff/DashStyle.hxx"
40 #include "xmloff/GradientStyle.hxx"
41 #include "xmloff/HatchStyle.hxx"
42 #include "xmloff/ImageStyle.hxx"
43 #include "xmloff/MarkerStyle.hxx"
44 #include <com/sun/star/embed/XTransactedObject.hpp>
45 #include <comphelper/processfactory.hxx>
46 #include <unotools/streamwrap.hxx>
47 #include "svx/xmlgrhlp.hxx"
49 #include "xmlxtexp.hxx"
51 #include <comphelper/storagehelper.hxx>
53 using namespace com::sun::star;
54 using namespace com::sun::star::container;
55 using namespace com::sun::star::document;
56 using namespace com::sun::star::uno;
57 using namespace com::sun::star::awt;
58 using namespace com::sun::star::lang;
59 using namespace com::sun::star::xml::sax;
60 using namespace ::xmloff::token;
61 using namespace ::rtl;
62 using namespace cppu;
64 using com::sun::star::embed::XTransactedObject;
66 class SvxXMLTableEntryExporter
68 public:
69 SvxXMLTableEntryExporter( SvXMLExport& rExport ) : mrExport( rExport ) {}
70 virtual ~SvxXMLTableEntryExporter();
72 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) = 0;
74 protected:
75 SvXMLExport& mrExport;
78 class SvxXMLColorEntryExporter : public SvxXMLTableEntryExporter
80 public:
81 SvxXMLColorEntryExporter( SvXMLExport& rExport );
82 virtual ~SvxXMLColorEntryExporter();
84 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) SAL_OVERRIDE;
87 class SvxXMLLineEndEntryExporter : public SvxXMLTableEntryExporter
89 public:
90 SvxXMLLineEndEntryExporter( SvXMLExport& rExport );
91 virtual ~SvxXMLLineEndEntryExporter();
93 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) SAL_OVERRIDE;
94 private:
95 XMLMarkerStyleExport maMarkerStyle;
98 class SvxXMLDashEntryExporter : public SvxXMLTableEntryExporter
100 public:
101 SvxXMLDashEntryExporter( SvXMLExport& rExport );
102 virtual ~SvxXMLDashEntryExporter();
104 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) SAL_OVERRIDE;
106 private:
107 XMLDashStyleExport maDashStyle;
110 class SvxXMLHatchEntryExporter : public SvxXMLTableEntryExporter
112 public:
113 SvxXMLHatchEntryExporter( SvXMLExport& rExport );
114 virtual ~SvxXMLHatchEntryExporter();
116 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) SAL_OVERRIDE;
117 private:
118 XMLHatchStyleExport maHatchStyle;
121 class SvxXMLGradientEntryExporter : public SvxXMLTableEntryExporter
123 public:
124 SvxXMLGradientEntryExporter( SvXMLExport& rExport );
125 virtual ~SvxXMLGradientEntryExporter();
127 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) SAL_OVERRIDE;
128 private:
129 XMLGradientStyleExport maGradientStyle;
132 class SvxXMLBitmapEntryExporter : public SvxXMLTableEntryExporter
134 public:
135 SvxXMLBitmapEntryExporter( SvXMLExport& rExport );
136 virtual ~SvxXMLBitmapEntryExporter();
138 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) SAL_OVERRIDE;
140 private:
141 XMLImageStyle maImageStyle;
146 // #110680#
147 SvxXMLXTableExportComponent::SvxXMLXTableExportComponent(
148 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > xContext,
149 const OUString& rFileName,
150 const uno::Reference<xml::sax::XDocumentHandler> & rHandler,
151 const uno::Reference<container::XNameContainer >& xTable,
152 uno::Reference<document::XGraphicObjectResolver >& xGrfResolver )
153 : SvXMLExport( xContext, "", rFileName, rHandler, NULL, MAP_100TH_MM),
154 mxTable( xTable )
157 _GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
158 _GetNamespaceMap().Add( GetXMLToken(XML_NP_OFFICE), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
159 _GetNamespaceMap().Add( GetXMLToken(XML_NP_DRAW), GetXMLToken(XML_N_DRAW), XML_NAMESPACE_DRAW );
160 _GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
161 _GetNamespaceMap().Add( GetXMLToken(XML_NP_SVG), GetXMLToken(XML_N_SVG), XML_NAMESPACE_SVG );
162 SetGraphicResolver( xGrfResolver );
163 setExportFlags( 0 );
166 SvxXMLXTableExportComponent::~SvxXMLXTableExportComponent()
170 static void initializeStreamMetadata( const uno::Reference< uno::XInterface > &xOut )
172 uno::Reference< beans::XPropertySet > xProps( xOut, uno::UNO_QUERY );
173 if( !xProps.is() )
175 OSL_FAIL( "Missing stream metadata interface" );
176 return;
181 xProps->setPropertyValue(
182 OUString( "MediaType" ),
183 uno::makeAny( OUString( "text/xml" ) ) );
185 // use stock encryption
186 xProps->setPropertyValue(
187 OUString( "UseCommonStoragePasswordEncryption" ),
188 uno::makeAny( sal_True ) );
189 } catch ( const uno::Exception & )
191 OSL_FAIL( "exception setting stream metadata" );
195 static void createStorageStream( uno::Reference < io::XOutputStream > *xOut,
196 SvXMLGraphicHelper **ppGraphicHelper,
197 uno::Reference < embed::XStorage > xSubStorage )
199 uno::Reference < io::XStream > xStream;
200 xStream = xSubStorage->openStreamElement(
201 OUString( "Content.xml" ),
202 embed::ElementModes::WRITE );
203 *ppGraphicHelper = SvXMLGraphicHelper::Create( xSubStorage, GRAPHICHELPER_MODE_WRITE );
204 initializeStreamMetadata( xStream );
205 *xOut = xStream->getOutputStream();
208 bool SvxXMLXTableExportComponent::save(
209 const OUString& rURL,
210 const uno::Reference<container::XNameContainer >& xTable,
211 const uno::Reference<embed::XStorage >& xStorage,
212 OUString *pOptName ) throw()
214 bool bRet = false;
215 SfxMedium* pMedium = NULL;
216 SvXMLGraphicHelper* pGraphicHelper = NULL;
217 sal_Int32 eCreate = embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE;
219 INetURLObject aURLObj( rURL );
220 bool bToStorage = aURLObj.GetProtocol() == INET_PROT_NOT_VALID; // a relative path
222 bool bSaveAsStorage = xTable->getElementType() == cppu::UnoType<OUString>::get();
224 if( pOptName )
225 *pOptName = rURL;
229 uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
231 uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
233 uno::Reference < io::XStream > xStream;
234 uno::Reference < io::XOutputStream > xOut;
235 uno::Reference<embed::XStorage > xSubStorage;
236 uno::Reference< XGraphicObjectResolver > xGrfResolver;
238 uno::Reference<xml::sax::XDocumentHandler> xHandler( xWriter, uno::UNO_QUERY );
240 if( !bToStorage || !xStorage.is() )
241 { // local URL -> SfxMedium route
242 if( bSaveAsStorage )
243 xSubStorage = ::comphelper::OStorageHelper::GetStorageFromURL( rURL, eCreate );
244 else
246 pMedium = new SfxMedium( rURL, STREAM_WRITE | STREAM_TRUNC );
247 pMedium->IsRemote();
249 SvStream* pStream = pMedium->GetOutStream();
250 if( !pStream )
252 OSL_FAIL( "no output stream!" );
253 return false;
256 xOut = new utl::OOutputStreamWrapper( *pStream );
259 else // save into the xSubStorage
261 OUString aPath = rURL;
263 if( bSaveAsStorage )
265 try {
266 xSubStorage = xStorage->openStorageElement( aPath, eCreate );
267 } catch (uno::Exception &) {
268 OSL_FAIL( "no output storage!" );
269 return false;
272 else
274 aPath += OUString( ".xml" );
275 try {
276 xStream = xStorage->openStreamElement( aPath, eCreate );
277 if( !xStream.is() )
278 return false;
279 initializeStreamMetadata( xStream );
280 xOut = xStream->getOutputStream();
281 } catch (uno::Exception &) {
282 OSL_FAIL( "no output stream!" );
283 return false;
285 if( pOptName )
286 *pOptName = aPath;
290 if( !xOut.is() && xSubStorage.is() )
291 createStorageStream( &xOut, &pGraphicHelper, xSubStorage );
292 if( !xOut.is() )
293 return false;
295 uno::Reference<io::XActiveDataSource> xMetaSrc( xWriter, uno::UNO_QUERY );
296 xMetaSrc->setOutputStream( xOut );
297 if( pGraphicHelper )
298 xGrfResolver = pGraphicHelper;
300 // Finally do the export
301 const OUString aName;
302 SvxXMLXTableExportComponent aExporter( xContext, aName, xHandler, xTable, xGrfResolver );
303 bRet = aExporter.exportTable();
305 if( pGraphicHelper )
306 SvXMLGraphicHelper::Destroy( pGraphicHelper );
308 if( xSubStorage.is() )
310 uno::Reference< XTransactedObject > xTrans( xSubStorage, UNO_QUERY );
311 if( xTrans.is() )
312 xTrans->commit();
314 uno::Reference< XComponent > xComp( xSubStorage, UNO_QUERY );
315 if( xComp.is() )
316 xSubStorage->dispose();
319 catch( uno::Exception& )
321 bRet = false;
324 if( pMedium )
326 pMedium->Commit();
327 delete pMedium;
330 return bRet;
333 bool SvxXMLXTableExportComponent::exportTable() throw()
335 bool bRet = false;
339 GetDocHandler()->startDocument();
341 addChaffWhenEncryptedStorage();
343 // export namespaces
344 sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
345 while( USHRT_MAX != nPos )
347 GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
348 nPos = GetNamespaceMap().GetNextKey( nPos );
353 if( !mxTable.is() )
354 break;
356 char const* pEleName;
357 Type aExportType = mxTable->getElementType();
358 SvxXMLTableEntryExporter* pExporter = NULL;
360 if( aExportType == cppu::UnoType<sal_Int32>::get() )
362 pExporter = new SvxXMLColorEntryExporter(*this);
363 pEleName = "color-table";
365 else if( aExportType == cppu::UnoType< drawing::PolyPolygonBezierCoords >::get() )
367 pExporter = new SvxXMLLineEndEntryExporter(*this);
368 pEleName = "marker-table";
370 else if( aExportType == cppu::UnoType< drawing::LineDash >::get() )
372 pExporter = new SvxXMLDashEntryExporter(*this);
373 pEleName = "dash-table";
375 else if( aExportType == cppu::UnoType< drawing::Hatch >::get() )
377 pExporter = new SvxXMLHatchEntryExporter(*this);
378 pEleName = "hatch-table";
380 else if( aExportType == cppu::UnoType< awt::Gradient >::get() )
382 pExporter = new SvxXMLGradientEntryExporter(*this);
383 pEleName = "gradient-table";
385 else if( aExportType == cppu::UnoType<OUString>::get())
387 pExporter = new SvxXMLBitmapEntryExporter(*this);
388 pEleName = "bitmap-table";
390 else
392 OSL_FAIL( "unknown type for export");
393 break;
396 SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, true, true );
398 Sequence< OUString > aNames = mxTable->getElementNames();
399 const sal_Int32 nCount = aNames.getLength();
400 const OUString* pNames = aNames.getConstArray();
401 Any aAny;
403 sal_Int32 nIndex;
404 for( nIndex = 0; nIndex < nCount; nIndex++, pNames++ )
406 aAny = mxTable->getByName( *pNames );
407 pExporter->exportEntry( *pNames, aAny );
409 delete pExporter;
411 bRet = true;
413 while(false);
415 GetDocHandler()->endDocument();
417 catch( Exception const& )
419 bRet = false;
422 return bRet;
425 // methods without content:
426 void SvxXMLXTableExportComponent::_ExportAutoStyles() {}
427 void SvxXMLXTableExportComponent::_ExportMasterStyles() {}
428 void SvxXMLXTableExportComponent::_ExportContent() {}
432 SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
438 SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
439 : SvxXMLTableEntryExporter( rExport )
443 SvxXMLColorEntryExporter::~SvxXMLColorEntryExporter()
447 void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
449 mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
451 sal_Int32 nColor = 0;
452 rValue >>= nColor;
454 OUStringBuffer aOut;
455 ::sax::Converter::convertColor( aOut, nColor );
456 mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
458 SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, true, true );
463 SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
464 : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
468 SvxXMLLineEndEntryExporter::~SvxXMLLineEndEntryExporter()
472 void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
474 maMarkerStyle.exportXML( rStrName, rValue );
479 SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
480 : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
484 SvxXMLDashEntryExporter::~SvxXMLDashEntryExporter()
488 void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
490 maDashStyle.exportXML( rStrName, rValue );
495 SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
496 : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
500 SvxXMLHatchEntryExporter::~SvxXMLHatchEntryExporter()
504 void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
506 maHatchStyle.exportXML( rStrName, rValue );
511 SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
512 : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
516 SvxXMLGradientEntryExporter::~SvxXMLGradientEntryExporter()
520 void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
522 maGradientStyle.exportXML( rStrName, rValue );
527 SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
528 : SvxXMLTableEntryExporter( rExport )
532 SvxXMLBitmapEntryExporter::~SvxXMLBitmapEntryExporter()
536 void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
538 maImageStyle.exportXML( rStrName, rValue, mrExport );
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */