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 <sal/config.h>
21
23#include <tools/urlobj.hxx>
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/container/XNameContainer.hpp>
26#include <com/sun/star/xml/sax/Writer.hpp>
27#include <com/sun/star/uno/Sequence.hxx>
28#include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
29#include <com/sun/star/drawing/LineDash.hpp>
30#include <com/sun/star/awt/Gradient.hpp>
31#include <com/sun/star/awt/XBitmap.hpp>
32#include <com/sun/star/drawing/Hatch.hpp>
33#include <com/sun/star/embed/ElementModes.hpp>
34
36#include <sfx2/docfile.hxx>
37#include <rtl/ustrbuf.hxx>
40
41#include <xmloff/xmltoken.hxx>
42#include <xmloff/xmlmetae.hxx>
43#include <xmloff/DashStyle.hxx>
45#include <xmloff/HatchStyle.hxx>
46#include <xmloff/ImageStyle.hxx>
48#include <com/sun/star/embed/XTransactedObject.hpp>
51#include <svx/xmlgrhlp.hxx>
52
53#include <xmlxtexp.hxx>
54
56#include <memory>
57
58using namespace com::sun::star;
59using namespace com::sun::star::container;
60using namespace com::sun::star::document;
61using namespace com::sun::star::uno;
62using namespace com::sun::star::awt;
63using namespace com::sun::star::lang;
64using namespace com::sun::star::xml::sax;
65using namespace ::xmloff::token;
66using namespace cppu;
67
68using com::sun::star::embed::XTransactedObject;
69
70namespace {
71
72class SvxXMLTableEntryExporter
73{
74public:
75 explicit SvxXMLTableEntryExporter( SvXMLExport& rExport ) : mrExport( rExport ) {}
76 virtual ~SvxXMLTableEntryExporter();
77
78 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) = 0;
79
80protected:
81 SvXMLExport& mrExport;
82};
83
84class SvxXMLColorEntryExporter : public SvxXMLTableEntryExporter
85{
86public:
87 explicit SvxXMLColorEntryExporter( SvXMLExport& rExport );
88
89 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
90};
91
92class SvxXMLLineEndEntryExporter : public SvxXMLTableEntryExporter
93{
94public:
95 explicit SvxXMLLineEndEntryExporter( SvXMLExport& rExport );
96
97 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
98private:
99 XMLMarkerStyleExport maMarkerStyle;
100};
101
102class SvxXMLDashEntryExporter : public SvxXMLTableEntryExporter
103{
104public:
105 explicit SvxXMLDashEntryExporter( SvXMLExport& rExport );
106
107 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
108
109private:
110 XMLDashStyleExport maDashStyle;
111};
112
113class SvxXMLHatchEntryExporter : public SvxXMLTableEntryExporter
114{
115public:
116 explicit SvxXMLHatchEntryExporter( SvXMLExport& rExport );
117
118 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
119private:
120 XMLHatchStyleExport maHatchStyle;
121};
122
123class SvxXMLGradientEntryExporter : public SvxXMLTableEntryExporter
124{
125public:
126 explicit SvxXMLGradientEntryExporter( SvXMLExport& rExport );
127
128 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
129private:
130 XMLGradientStyleExport maGradientStyle;
131};
132
133class SvxXMLBitmapEntryExporter : public SvxXMLTableEntryExporter
134{
135public:
136 explicit SvxXMLBitmapEntryExporter( SvXMLExport& rExport );
137
138 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
139};
140
141}
142
144 const css::uno::Reference< css::uno::XComponentContext >& rContext,
145 const uno::Reference<xml::sax::XDocumentHandler> & rHandler,
146 const uno::Reference<container::XNameContainer >& xTable,
147 uno::Reference<document::XGraphicStorageHandler> const & xGraphicStorageHandler)
148: SvXMLExport(rContext, "", /*rFileName*/"", rHandler, nullptr, FieldUnit::MM_100TH, SvXMLExportFlags::NONE),
149 mxTable( xTable )
150{
151
158 SetGraphicStorageHandler(xGraphicStorageHandler);
159}
160
162{
163}
164
165static void initializeStreamMetadata( const uno::Reference< uno::XInterface > &xOut )
166{
167 uno::Reference< beans::XPropertySet > xProps( xOut, uno::UNO_QUERY );
168 if( !xProps.is() )
169 {
170 OSL_FAIL( "Missing stream metadata interface" );
171 return;
172 }
173
174 try
175 {
176 xProps->setPropertyValue("MediaType", uno::Any( OUString( "text/xml" ) ) );
177
178 // use stock encryption
179 xProps->setPropertyValue("UseCommonStoragePasswordEncryption", uno::Any( true ) );
180 } catch ( const uno::Exception & )
181 {
182 TOOLS_WARN_EXCEPTION("svx", "exception setting stream metadata");
183 }
184}
185
186static void createStorageStream( uno::Reference < io::XOutputStream > *xOut,
187 rtl::Reference<SvXMLGraphicHelper>& rxGraphicHelper,
188 const uno::Reference < embed::XStorage >& xSubStorage )
189{
190 uno::Reference < io::XStream > xStream = xSubStorage->openStreamElement(
191 "Content.xml",
192 embed::ElementModes::WRITE );
193 rxGraphicHelper = SvXMLGraphicHelper::Create( xSubStorage, SvXMLGraphicHelperMode::Write );
195 *xOut = xStream->getOutputStream();
196}
197
199 const OUString& rURL,
200 const uno::Reference<container::XNameContainer >& xTable,
201 const uno::Reference<embed::XStorage >& xStorage,
202 OUString *pOptName )
203{
204 bool bRet = false;
205 std::unique_ptr<SfxMedium> pMedium;
207
208 INetURLObject aURLObj( rURL );
209 bool bToStorage = aURLObj.GetProtocol() == INetProtocol::NotValid; // a relative path
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 const sal_Int32 eCreate = embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE;
226
227 if( !bToStorage || !xStorage.is() )
228 { // local URL -> SfxMedium route
229 if( bSaveAsStorage )
230 xSubStorage = ::comphelper::OStorageHelper::GetStorageFromURL( rURL, eCreate );
231 else
232 {
233 pMedium.reset(new SfxMedium( rURL, StreamMode::WRITE | StreamMode::TRUNC ));
234
235 SvStream* pStream = pMedium->GetOutStream();
236 if( !pStream )
237 {
238 OSL_FAIL( "no output stream!" );
239 return false;
240 }
241
242 xOut = new utl::OOutputStreamWrapper( *pStream );
243 }
244 }
245 else // save into the xSubStorage
246 {
247 OUString aPath = rURL;
248
249 if( bSaveAsStorage )
250 {
251 try {
252 xSubStorage = xStorage->openStorageElement( aPath, eCreate );
253 } catch (uno::Exception &) {
254 OSL_FAIL( "no output storage!" );
255 return false;
256 }
257 }
258 else
259 {
260 aPath += ".xml";
261 try {
262 xStream = xStorage->openStreamElement( aPath, eCreate );
263 if( !xStream.is() )
264 return false;
266 xOut = xStream->getOutputStream();
267 } catch (uno::Exception &) {
268 OSL_FAIL( "no output stream!" );
269 return false;
270 }
271 if( pOptName )
272 *pOptName = aPath;
273 }
274 }
275
276 if( !xOut.is() && xSubStorage.is() )
277 createStorageStream( &xOut, xGraphicHelper, xSubStorage );
278 if( !xOut.is() )
279 return false;
280
281 xWriter->setOutputStream( xOut );
282 if( xGraphicHelper.is() )
283 xGraphicStorageHandler = xGraphicHelper.get();
284
285 // Finally do the export
286 rtl::Reference< SvxXMLXTableExportComponent > xExporter( new SvxXMLXTableExportComponent( xContext, 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 ) );
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
407SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
408{
409}
410
411
412SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
413: SvxXMLTableEntryExporter( rExport )
414{
415}
416
417void 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
432SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
433: SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
434{
435}
436
437void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
438{
439 maMarkerStyle.exportXML( rStrName, rValue );
440}
441
442
443SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
444: SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
445{
446}
447
448void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
449{
450 maDashStyle.exportXML( rStrName, rValue );
451}
452
453
454SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
455: SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
456{
457}
458
459void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
460{
461 maHatchStyle.exportXML( rStrName, rValue );
462}
463
464
465SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
466: SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
467{
468}
469
470void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
471{
472 maGradientStyle.exportXML( rStrName, rValue );
473}
474
475
476SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
477: SvxXMLTableEntryExporter( rExport )
478{
479}
480
481void 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: */
Reference< XInputStream > xStream
INetProtocol GetProtocol() const
const SvXMLNamespaceMap & GetNamespaceMap() const
SvXMLNamespaceMap & GetNamespaceMap_()
void addChaffWhenEncryptedStorage()
const css::uno::Reference< css::xml::sax::XDocumentHandler > & GetDocHandler() const
comphelper::AttributeList & GetAttrList()
void SetGraphicStorageHandler(css::uno::Reference< css::document::XGraphicStorageHandler > const &rxGraphicStorageHandler)
static rtl::Reference< SvXMLGraphicHelper > Create(const css::uno::Reference< css::embed::XStorage > &rXMLStorage, SvXMLGraphicHelperMode eCreateMode)
sal_uInt16 GetFirstKey() const
sal_uInt16 GetNextKey(sal_uInt16 nOldKey) const
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
const css::uno::Reference< css::container::XNameContainer > & mxTable
Definition: xmlxtexp.hxx:58
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:198
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:143
virtual void ExportAutoStyles_() override
Definition: xmlxtexp.cxx:402
virtual void ExportMasterStyles_() override
Definition: xmlxtexp.cxx:403
virtual ~SvxXMLXTableExportComponent() override
Definition: xmlxtexp.cxx:161
virtual void ExportContent_() override
Definition: xmlxtexp.cxx:404
bool exportTable() noexcept
Definition: xmlxtexp.cxx:313
void AddAttribute(const OUString &sName, const OUString &sValue)
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 >())
css::uno::Type const & get()
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
#define TOOLS_WARN_EXCEPTION(area, stream)
FieldUnit
sal_uInt16 nPos
NONE
XMLOFF_DLLPUBLIC void exportXML(OUString const &rStrName, css::uno::Any const &rValue, SvXMLExport &rExport)
@ Exception
Type
XML_N_XLINK
XML_N_OFFICE
XML_N_LO_EXT
XML_NP_DRAW
XML_NP_XLINK
XML_NP_OFFICE
XML_NP_OOO
XML_NP_LO_EXT
XML_N_SVG
XML_NP_SVG
XML_N_DRAW
XML_N_OOO
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
SvXMLExportFlags
constexpr sal_uInt16 XML_NAMESPACE_DRAW
constexpr sal_uInt16 XML_NAMESPACE_XLINK
constexpr sal_uInt16 XML_NAMESPACE_SVG
constexpr sal_uInt16 XML_NAMESPACE_LO_EXT
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
constexpr sal_uInt16 XML_NAMESPACE_OOO
static void initializeStreamMetadata(const uno::Reference< uno::XInterface > &xOut)
Definition: xmlxtexp.cxx:165
static void createStorageStream(uno::Reference< io::XOutputStream > *xOut, rtl::Reference< SvXMLGraphicHelper > &rxGraphicHelper, const uno::Reference< embed::XStorage > &xSubStorage)
Definition: xmlxtexp.cxx:186