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/xmlnamespace.hxx>
36 #include <xmloff/namespacemap.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 uno::Reference<xml::sax::XDocumentHandler> & rHandler,
143  const uno::Reference<container::XNameContainer >& xTable,
144  uno::Reference<document::XGraphicStorageHandler> const & xGraphicStorageHandler)
145 : SvXMLExport(rContext, "", /*rFileName*/"", rHandler, nullptr, FieldUnit::MM_100TH, SvXMLExportFlags::NONE),
146  mxTable( xTable )
147 {
148 
154  SetGraphicStorageHandler(xGraphicStorageHandler);
155 }
156 
158 {
159 }
160 
161 static void initializeStreamMetadata( const uno::Reference< uno::XInterface > &xOut )
162 {
163  uno::Reference< beans::XPropertySet > xProps( xOut, uno::UNO_QUERY );
164  if( !xProps.is() )
165  {
166  OSL_FAIL( "Missing stream metadata interface" );
167  return;
168  }
169 
170  try
171  {
172  xProps->setPropertyValue("MediaType", uno::makeAny( OUString( "text/xml" ) ) );
173 
174  // use stock encryption
175  xProps->setPropertyValue("UseCommonStoragePasswordEncryption", uno::makeAny( true ) );
176  } catch ( const uno::Exception & )
177  {
178  OSL_FAIL( "exception setting stream metadata" );
179  }
180 }
181 
182 static void createStorageStream( uno::Reference < io::XOutputStream > *xOut,
183  rtl::Reference<SvXMLGraphicHelper>& rxGraphicHelper,
184  const uno::Reference < embed::XStorage >& xSubStorage )
185 {
186  uno::Reference < io::XStream > xStream = xSubStorage->openStreamElement(
187  "Content.xml",
188  embed::ElementModes::WRITE );
189  rxGraphicHelper = SvXMLGraphicHelper::Create( xSubStorage, SvXMLGraphicHelperMode::Write );
190  initializeStreamMetadata( xStream );
191  *xOut = xStream->getOutputStream();
192 }
193 
195  const OUString& rURL,
196  const uno::Reference<container::XNameContainer >& xTable,
197  const uno::Reference<embed::XStorage >& xStorage,
198  OUString *pOptName )
199 {
200  bool bRet = false;
201  std::unique_ptr<SfxMedium> pMedium;
202  rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
203  sal_Int32 eCreate = embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE;
204 
205  INetURLObject aURLObj( rURL );
206  bool bToStorage = aURLObj.GetProtocol() == INetProtocol::NotValid; // a relative path
207 
208  bool bSaveAsStorage = xTable->getElementType() == cppu::UnoType<awt::XBitmap>::get();
209 
210  if( pOptName )
211  *pOptName = rURL;
212 
213  try
214  {
215  uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
216 
217  uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
218 
219  uno::Reference < io::XStream > xStream;
220  uno::Reference < io::XOutputStream > xOut;
221  uno::Reference<embed::XStorage > xSubStorage;
222  uno::Reference<XGraphicStorageHandler> xGraphicStorageHandler;
223 
224  if( !bToStorage || !xStorage.is() )
225  { // local URL -> SfxMedium route
226  if( bSaveAsStorage )
227  xSubStorage = ::comphelper::OStorageHelper::GetStorageFromURL( rURL, eCreate );
228  else
229  {
230  pMedium.reset(new SfxMedium( rURL, StreamMode::WRITE | StreamMode::TRUNC ));
231 
232  SvStream* pStream = pMedium->GetOutStream();
233  if( !pStream )
234  {
235  OSL_FAIL( "no output stream!" );
236  return false;
237  }
238 
239  xOut = new utl::OOutputStreamWrapper( *pStream );
240  }
241  }
242  else // save into the xSubStorage
243  {
244  OUString aPath = rURL;
245 
246  if( bSaveAsStorage )
247  {
248  try {
249  xSubStorage = xStorage->openStorageElement( aPath, eCreate );
250  } catch (uno::Exception &) {
251  OSL_FAIL( "no output storage!" );
252  return false;
253  }
254  }
255  else
256  {
257  aPath += ".xml";
258  try {
259  xStream = xStorage->openStreamElement( aPath, eCreate );
260  if( !xStream.is() )
261  return false;
262  initializeStreamMetadata( xStream );
263  xOut = xStream->getOutputStream();
264  } catch (uno::Exception &) {
265  OSL_FAIL( "no output stream!" );
266  return false;
267  }
268  if( pOptName )
269  *pOptName = aPath;
270  }
271  }
272 
273  if( !xOut.is() && xSubStorage.is() )
274  createStorageStream( &xOut, xGraphicHelper, xSubStorage );
275  if( !xOut.is() )
276  return false;
277 
278  xWriter->setOutputStream( xOut );
279  if( xGraphicHelper.is() )
280  xGraphicStorageHandler = xGraphicHelper.get();
281 
282  // Finally do the export
283  rtl::Reference< SvxXMLXTableExportComponent > xExporter( new SvxXMLXTableExportComponent( xContext, xWriter, xTable, xGraphicStorageHandler ) );
284  bRet = xExporter->exportTable();
285 
286  if( xGraphicHelper )
287  xGraphicHelper->dispose();
288  xGraphicHelper.clear();
289 
290  if( xSubStorage.is() )
291  {
292  uno::Reference< XTransactedObject > xTrans( xSubStorage, UNO_QUERY );
293  if( xTrans.is() )
294  xTrans->commit();
295 
296  xSubStorage->dispose();
297  }
298  }
299  catch( uno::Exception& )
300  {
301  bRet = false;
302  }
303 
304  if( pMedium )
305  pMedium->Commit();
306 
307  return bRet;
308 }
309 
311 {
312  bool bRet = false;
313 
314  try
315  {
316  GetDocHandler()->startDocument();
317 
319 
320  // export namespaces
321  sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
322  while( USHRT_MAX != nPos )
323  {
324  GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
325  nPos = GetNamespaceMap().GetNextKey( nPos );
326  }
327 
328  do
329  {
330  if( !mxTable.is() )
331  break;
332 
333  char const* pEleName;
334  Type aExportType = mxTable->getElementType();
335  std::unique_ptr<SvxXMLTableEntryExporter> pExporter;
336 
337  if( aExportType == cppu::UnoType<sal_Int32>::get() )
338  {
339  pExporter.reset(new SvxXMLColorEntryExporter(*this));
340  pEleName = "color-table";
341  }
343  {
344  pExporter.reset(new SvxXMLLineEndEntryExporter(*this));
345  pEleName = "marker-table";
346  }
347  else if( aExportType == cppu::UnoType< drawing::LineDash >::get() )
348  {
349  pExporter.reset(new SvxXMLDashEntryExporter(*this));
350  pEleName = "dash-table";
351  }
352  else if( aExportType == cppu::UnoType< drawing::Hatch >::get() )
353  {
354  pExporter.reset(new SvxXMLHatchEntryExporter(*this));
355  pEleName = "hatch-table";
356  }
357  else if( aExportType == cppu::UnoType< awt::Gradient >::get() )
358  {
359  pExporter.reset(new SvxXMLGradientEntryExporter(*this));
360  pEleName = "gradient-table";
361  }
362  else if( aExportType == cppu::UnoType<awt::XBitmap>::get())
363  {
364  pExporter.reset(new SvxXMLBitmapEntryExporter(*this));
365  pEleName = "bitmap-table";
366  }
367  else
368  {
369  OSL_FAIL( "unknown type for export");
370  break;
371  }
372 
373  SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, true, true );
374 
375  const Sequence< OUString > aNames = mxTable->getElementNames();
376  Any aAny;
377 
378  for( const OUString& rName : aNames )
379  {
380  aAny = mxTable->getByName( rName );
381  pExporter->exportEntry( rName, aAny );
382  }
383 
384  bRet = true;
385  }
386  while(false);
387 
388  GetDocHandler()->endDocument();
389  }
390  catch( Exception const& )
391  {
392  bRet = false;
393  }
394 
395  return bRet;
396 }
397 
398 // methods without content:
402 
403 
404 SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
405 {
406 }
407 
408 
409 SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
410 : SvxXMLTableEntryExporter( rExport )
411 {
412 }
413 
414 void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
415 {
416  mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
417 
418  sal_Int32 nColor = 0;
419  rValue >>= nColor;
420 
421  OUStringBuffer aOut;
422  ::sax::Converter::convertColor( aOut, nColor );
423  mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
424 
425  SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, true, true );
426 }
427 
428 
429 SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
430 : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
431 {
432 }
433 
434 void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
435 {
436  maMarkerStyle.exportXML( rStrName, rValue );
437 }
438 
439 
440 SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
441 : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
442 {
443 }
444 
445 void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
446 {
447  maDashStyle.exportXML( rStrName, rValue );
448 }
449 
450 
451 SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
452 : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
453 {
454 }
455 
456 void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
457 {
458  maHatchStyle.exportXML( rStrName, rValue );
459 }
460 
461 
462 SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
463 : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
464 {
465 }
466 
467 void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
468 {
469  maGradientStyle.exportXML( rStrName, rValue );
470 }
471 
472 
473 SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
474 : SvxXMLTableEntryExporter( rExport )
475 {
476 }
477 
478 void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
479 {
480  XMLImageStyle::exportXML(rStrName, rValue, mrExport);
481 }
482 
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
virtual void ExportMasterStyles_() override
Definition: xmlxtexp.cxx:400
FieldUnit
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
static void createStorageStream(uno::Reference< io::XOutputStream > *xOut, rtl::Reference< SvXMLGraphicHelper > &rxGraphicHelper, const uno::Reference< embed::XStorage > &xSubStorage)
Definition: xmlxtexp.cxx:182
static void initializeStreamMetadata(const uno::Reference< uno::XInterface > &xOut)
Definition: xmlxtexp.cxx:161
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:194
void addChaffWhenEncryptedStorage()
SvXMLExportFlags
sal_uInt16 GetFirstKey() const
constexpr sal_uInt16 XML_NAMESPACE_XLINK
constexpr sal_uInt16 XML_NAMESPACE_OOO
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
XML_N_OFFICE
constexpr sal_uInt16 XML_NAMESPACE_SVG
XML_N_DRAW
XML_NP_DRAW
SvxXMLXTableExportComponent(const css::uno::Reference< css::uno::XComponentContext > &xContext, 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
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:157
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:401
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
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:58
sal_uInt16 nPos
virtual void ExportAutoStyles_() override
Definition: xmlxtexp.cxx:399