LibreOffice Module svx (master)  1
xmlxtexp.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <tools/urlobj.hxx>
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/container/XNameContainer.hpp>
23 #include <com/sun/star/xml/sax/Writer.hpp>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
26 #include <com/sun/star/drawing/LineDash.hpp>
27 #include <com/sun/star/awt/Gradient.hpp>
28 #include <com/sun/star/awt/XBitmap.hpp>
29 #include <com/sun/star/drawing/Hatch.hpp>
30 #include <com/sun/star/io/XActiveDataSource.hpp>
31 #include <com/sun/star/embed/ElementModes.hpp>
32 
33 #include <sax/tools/converter.hxx>
34 #include <sfx2/docfile.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <xmloff/xmlnmspe.hxx>
37 #include <xmloff/nmspmap.hxx>
38 
39 #include <xmloff/xmltoken.hxx>
40 #include <xmloff/xmlmetae.hxx>
41 #include <xmloff/DashStyle.hxx>
42 #include <xmloff/GradientStyle.hxx>
43 #include <xmloff/HatchStyle.hxx>
44 #include <xmloff/ImageStyle.hxx>
45 #include <xmloff/MarkerStyle.hxx>
46 #include <com/sun/star/embed/XTransactedObject.hpp>
48 #include <unotools/streamwrap.hxx>
49 #include <svx/xmlgrhlp.hxx>
50 
51 #include <xmlxtexp.hxx>
52 
54 #include <memory>
55 
56 using namespace com::sun::star;
57 using namespace com::sun::star::container;
58 using namespace com::sun::star::document;
59 using namespace com::sun::star::uno;
60 using namespace com::sun::star::awt;
61 using namespace com::sun::star::lang;
62 using namespace com::sun::star::xml::sax;
63 using namespace ::xmloff::token;
64 using namespace cppu;
65 
66 using com::sun::star::embed::XTransactedObject;
67 
68 namespace {
69 
70 class SvxXMLTableEntryExporter
71 {
72 public:
73  explicit SvxXMLTableEntryExporter( SvXMLExport& rExport ) : mrExport( rExport ) {}
74  virtual ~SvxXMLTableEntryExporter();
75 
76  virtual void exportEntry( const OUString& rStrName, const Any& rValue ) = 0;
77 
78 protected:
79  SvXMLExport& mrExport;
80 };
81 
82 class SvxXMLColorEntryExporter : public SvxXMLTableEntryExporter
83 {
84 public:
85  explicit SvxXMLColorEntryExporter( SvXMLExport& rExport );
86 
87  virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
88 };
89 
90 class SvxXMLLineEndEntryExporter : public SvxXMLTableEntryExporter
91 {
92 public:
93  explicit SvxXMLLineEndEntryExporter( SvXMLExport& rExport );
94 
95  virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
96 private:
97  XMLMarkerStyleExport maMarkerStyle;
98 };
99 
100 class SvxXMLDashEntryExporter : public SvxXMLTableEntryExporter
101 {
102 public:
103  explicit SvxXMLDashEntryExporter( SvXMLExport& rExport );
104 
105  virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
106 
107 private:
108  XMLDashStyleExport maDashStyle;
109 };
110 
111 class SvxXMLHatchEntryExporter : public SvxXMLTableEntryExporter
112 {
113 public:
114  explicit SvxXMLHatchEntryExporter( SvXMLExport& rExport );
115 
116  virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
117 private:
118  XMLHatchStyleExport maHatchStyle;
119 };
120 
121 class SvxXMLGradientEntryExporter : public SvxXMLTableEntryExporter
122 {
123 public:
124  explicit SvxXMLGradientEntryExporter( SvXMLExport& rExport );
125 
126  virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
127 private:
128  XMLGradientStyleExport maGradientStyle;
129 };
130 
131 class SvxXMLBitmapEntryExporter : public SvxXMLTableEntryExporter
132 {
133 public:
134  explicit SvxXMLBitmapEntryExporter( SvXMLExport& rExport );
135 
136  virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
137 };
138 
139 }
140 
142  const css::uno::Reference< css::uno::XComponentContext >& rContext,
143  const OUString& rFileName,
144  const uno::Reference<xml::sax::XDocumentHandler> & rHandler,
145  const uno::Reference<container::XNameContainer >& xTable,
146  uno::Reference<document::XGraphicStorageHandler> const & xGraphicStorageHandler)
147 : SvXMLExport(rContext, "", rFileName, rHandler, nullptr, FieldUnit::MM_100TH, SvXMLExportFlags::NONE),
148  mxTable( xTable )
149 {
150 
155  GetNamespaceMap_().Add( GetXMLToken(XML_NP_SVG), GetXMLToken(XML_N_SVG), XML_NAMESPACE_SVG );
156  SetGraphicStorageHandler(xGraphicStorageHandler);
157 }
158 
160 {
161 }
162 
163 static void initializeStreamMetadata( const uno::Reference< uno::XInterface > &xOut )
164 {
165  uno::Reference< beans::XPropertySet > xProps( xOut, uno::UNO_QUERY );
166  if( !xProps.is() )
167  {
168  OSL_FAIL( "Missing stream metadata interface" );
169  return;
170  }
171 
172  try
173  {
174  xProps->setPropertyValue("MediaType", uno::makeAny( OUString( "text/xml" ) ) );
175 
176  // use stock encryption
177  xProps->setPropertyValue("UseCommonStoragePasswordEncryption", uno::makeAny( true ) );
178  } catch ( const uno::Exception & )
179  {
180  OSL_FAIL( "exception setting stream metadata" );
181  }
182 }
183 
184 static void createStorageStream( uno::Reference < io::XOutputStream > *xOut,
185  rtl::Reference<SvXMLGraphicHelper>& rxGraphicHelper,
186  const uno::Reference < embed::XStorage >& xSubStorage )
187 {
188  uno::Reference < io::XStream > xStream = xSubStorage->openStreamElement(
189  "Content.xml",
190  embed::ElementModes::WRITE );
191  rxGraphicHelper = SvXMLGraphicHelper::Create( xSubStorage, SvXMLGraphicHelperMode::Write );
192  initializeStreamMetadata( xStream );
193  *xOut = xStream->getOutputStream();
194 }
195 
197  const OUString& rURL,
198  const uno::Reference<container::XNameContainer >& xTable,
199  const uno::Reference<embed::XStorage >& xStorage,
200  OUString *pOptName )
201 {
202  bool bRet = false;
203  std::unique_ptr<SfxMedium> pMedium;
204  rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
205  sal_Int32 eCreate = embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE;
206 
207  INetURLObject aURLObj( rURL );
208  bool bToStorage = aURLObj.GetProtocol() == INetProtocol::NotValid; // a relative path
209 
210  bool bSaveAsStorage = xTable->getElementType() == cppu::UnoType<awt::XBitmap>::get();
211 
212  if( pOptName )
213  *pOptName = rURL;
214 
215  try
216  {
217  uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
218 
219  uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
220 
221  uno::Reference < io::XStream > xStream;
222  uno::Reference < io::XOutputStream > xOut;
223  uno::Reference<embed::XStorage > xSubStorage;
224  uno::Reference<XGraphicStorageHandler> xGraphicStorageHandler;
225 
226  if( !bToStorage || !xStorage.is() )
227  { // local URL -> SfxMedium route
228  if( bSaveAsStorage )
229  xSubStorage = ::comphelper::OStorageHelper::GetStorageFromURL( rURL, eCreate );
230  else
231  {
232  pMedium.reset(new SfxMedium( rURL, StreamMode::WRITE | StreamMode::TRUNC ));
233 
234  SvStream* pStream = pMedium->GetOutStream();
235  if( !pStream )
236  {
237  OSL_FAIL( "no output stream!" );
238  return false;
239  }
240 
241  xOut = new utl::OOutputStreamWrapper( *pStream );
242  }
243  }
244  else // save into the xSubStorage
245  {
246  OUString aPath = rURL;
247 
248  if( bSaveAsStorage )
249  {
250  try {
251  xSubStorage = xStorage->openStorageElement( aPath, eCreate );
252  } catch (uno::Exception &) {
253  OSL_FAIL( "no output storage!" );
254  return false;
255  }
256  }
257  else
258  {
259  aPath += ".xml";
260  try {
261  xStream = xStorage->openStreamElement( aPath, eCreate );
262  if( !xStream.is() )
263  return false;
264  initializeStreamMetadata( xStream );
265  xOut = xStream->getOutputStream();
266  } catch (uno::Exception &) {
267  OSL_FAIL( "no output stream!" );
268  return false;
269  }
270  if( pOptName )
271  *pOptName = aPath;
272  }
273  }
274 
275  if( !xOut.is() && xSubStorage.is() )
276  createStorageStream( &xOut, xGraphicHelper, xSubStorage );
277  if( !xOut.is() )
278  return false;
279 
280  xWriter->setOutputStream( xOut );
281  if( xGraphicHelper.is() )
282  xGraphicStorageHandler = xGraphicHelper.get();
283 
284  // Finally do the export
285  const OUString aName;
286  rtl::Reference< SvxXMLXTableExportComponent > xExporter( new SvxXMLXTableExportComponent( xContext, aName, xWriter, xTable, xGraphicStorageHandler ) );
287  bRet = xExporter->exportTable();
288 
289  if( xGraphicHelper )
290  xGraphicHelper->dispose();
291  xGraphicHelper.clear();
292 
293  if( xSubStorage.is() )
294  {
295  uno::Reference< XTransactedObject > xTrans( xSubStorage, UNO_QUERY );
296  if( xTrans.is() )
297  xTrans->commit();
298 
299  xSubStorage->dispose();
300  }
301  }
302  catch( uno::Exception& )
303  {
304  bRet = false;
305  }
306 
307  if( pMedium )
308  pMedium->Commit();
309 
310  return bRet;
311 }
312 
314 {
315  bool bRet = false;
316 
317  try
318  {
319  GetDocHandler()->startDocument();
320 
322 
323  // export namespaces
324  sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
325  while( USHRT_MAX != nPos )
326  {
327  GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
328  nPos = GetNamespaceMap().GetNextKey( nPos );
329  }
330 
331  do
332  {
333  if( !mxTable.is() )
334  break;
335 
336  char const* pEleName;
337  Type aExportType = mxTable->getElementType();
338  std::unique_ptr<SvxXMLTableEntryExporter> pExporter;
339 
340  if( aExportType == cppu::UnoType<sal_Int32>::get() )
341  {
342  pExporter.reset(new SvxXMLColorEntryExporter(*this));
343  pEleName = "color-table";
344  }
346  {
347  pExporter.reset(new SvxXMLLineEndEntryExporter(*this));
348  pEleName = "marker-table";
349  }
350  else if( aExportType == cppu::UnoType< drawing::LineDash >::get() )
351  {
352  pExporter.reset(new SvxXMLDashEntryExporter(*this));
353  pEleName = "dash-table";
354  }
355  else if( aExportType == cppu::UnoType< drawing::Hatch >::get() )
356  {
357  pExporter.reset(new SvxXMLHatchEntryExporter(*this));
358  pEleName = "hatch-table";
359  }
360  else if( aExportType == cppu::UnoType< awt::Gradient >::get() )
361  {
362  pExporter.reset(new SvxXMLGradientEntryExporter(*this));
363  pEleName = "gradient-table";
364  }
365  else if( aExportType == cppu::UnoType<awt::XBitmap>::get())
366  {
367  pExporter.reset(new SvxXMLBitmapEntryExporter(*this));
368  pEleName = "bitmap-table";
369  }
370  else
371  {
372  OSL_FAIL( "unknown type for export");
373  break;
374  }
375 
376  SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, true, true );
377 
378  const Sequence< OUString > aNames = mxTable->getElementNames();
379  Any aAny;
380 
381  for( const OUString& rName : aNames )
382  {
383  aAny = mxTable->getByName( rName );
384  pExporter->exportEntry( rName, aAny );
385  }
386 
387  bRet = true;
388  }
389  while(false);
390 
391  GetDocHandler()->endDocument();
392  }
393  catch( Exception const& )
394  {
395  bRet = false;
396  }
397 
398  return bRet;
399 }
400 
401 // methods without content:
405 
406 
407 SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
408 {
409 }
410 
411 
412 SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
413 : SvxXMLTableEntryExporter( rExport )
414 {
415 }
416 
417 void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
418 {
419  mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
420 
421  sal_Int32 nColor = 0;
422  rValue >>= nColor;
423 
424  OUStringBuffer aOut;
425  ::sax::Converter::convertColor( aOut, nColor );
426  mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
427 
428  SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, true, true );
429 }
430 
431 
432 SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
433 : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
434 {
435 }
436 
437 void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
438 {
439  maMarkerStyle.exportXML( rStrName, rValue );
440 }
441 
442 
443 SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
444 : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
445 {
446 }
447 
448 void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
449 {
450  maDashStyle.exportXML( rStrName, rValue );
451 }
452 
453 
454 SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
455 : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
456 {
457 }
458 
459 void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
460 {
461  maHatchStyle.exportXML( rStrName, rValue );
462 }
463 
464 
465 SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
466 : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
467 {
468 }
469 
470 void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
471 {
472  maGradientStyle.exportXML( rStrName, rValue );
473 }
474 
475 
476 SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
477 : SvxXMLTableEntryExporter( rExport )
478 {
479 }
480 
481 void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
482 {
483  XMLImageStyle::exportXML(rStrName, rValue, mrExport);
484 }
485 
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
virtual void ExportMasterStyles_() override
Definition: xmlxtexp.cxx:403
FieldUnit
static void createStorageStream(uno::Reference< io::XOutputStream > *xOut, rtl::Reference< SvXMLGraphicHelper > &rxGraphicHelper, const uno::Reference< embed::XStorage > &xSubStorage)
Definition: xmlxtexp.cxx:184
static void initializeStreamMetadata(const uno::Reference< uno::XInterface > &xOut)
Definition: xmlxtexp.cxx:163
static bool save(const OUString &rURL, const css::uno::Reference< css::container::XNameContainer > &xTable, const css::uno::Reference< css::embed::XStorage > &xStorage, OUString *pOptName)
Definition: xmlxtexp.cxx:196
void addChaffWhenEncryptedStorage()
SvXMLExportFlags
sal_uInt16 GetFirstKey() const
XML_N_OOO
const css::uno::Reference< css::xml::sax::XDocumentHandler > & GetDocHandler() const
NONE
SvXMLAttributeList & GetAttrList()
Reference< XInputStream > xStream
XML_NP_XLINK
XML_NP_OFFICE
XMLOFF_DLLPUBLIC void exportXML(OUString const &rStrName, css::uno::Any const &rValue, SvXMLExport &rExport)
SvxXMLXTableExportComponent(const css::uno::Reference< css::uno::XComponentContext > &xContext, const OUString &rFileName, const css::uno::Reference< css::xml::sax::XDocumentHandler > &xHandler, const css::uno::Reference< css::container::XNameContainer > &xTable, css::uno::Reference< css::document::XGraphicStorageHandler > const &xGraphicStorageHandler)
Definition: xmlxtexp.cxx:141
XML_N_OFFICE
#define XML_NAMESPACE_OOO(prefix, index)
XML_N_DRAW
XML_NP_DRAW
static css::uno::Reference< css::embed::XStorage > GetStorageFromURL(const OUString &aURL, sal_Int32 nStorageMode, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
virtual ~SvxXMLXTableExportComponent() override
Definition: xmlxtexp.cxx:159
css::uno::Type const & get()
XML_N_SVG
sal_uInt16 GetNextKey(sal_uInt16 nOldKey) const
const SvXMLNamespaceMap & GetNamespaceMap() const
void SetGraphicStorageHandler(css::uno::Reference< css::document::XGraphicStorageHandler > const &rxGraphicStorageHandler)
virtual void ExportContent_() override
Definition: xmlxtexp.cxx:404
SvXMLNamespaceMap & GetNamespaceMap_()
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
static bool convertColor(sal_Int32 &rColor, const OUString &rValue)
XML_NP_SVG
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
XML_N_XLINK
OUString aName
INetProtocol GetProtocol() const
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
Reference< XComponentContext > getProcessComponentContext()
void AddAttribute(const OUString &sName, const OUString &sValue)
static rtl::Reference< SvXMLGraphicHelper > Create(const css::uno::Reference< css::embed::XStorage > &rXMLStorage, SvXMLGraphicHelperMode eCreateMode)
XML_NP_OOO
const css::uno::Reference< css::container::XNameContainer > & mxTable
Definition: xmlxtexp.hxx:59
sal_uInt16 nPos
virtual void ExportAutoStyles_() override
Definition: xmlxtexp.cxx:402