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