Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / xml / xmlxtexp.cxx
blobbd03967bd8329ecfcb8c599bf2a588d3af9c8863
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 <sal/config.h>
22 #include <comphelper/diagnose_ex.hxx>
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>
35 #include <sax/tools/converter.hxx>
36 #include <sfx2/docfile.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <xmloff/xmlnamespace.hxx>
39 #include <xmloff/namespacemap.hxx>
41 #include <xmloff/xmltoken.hxx>
42 #include <xmloff/xmlmetae.hxx>
43 #include <xmloff/DashStyle.hxx>
44 #include <xmloff/GradientStyle.hxx>
45 #include <xmloff/HatchStyle.hxx>
46 #include <xmloff/ImageStyle.hxx>
47 #include <xmloff/MarkerStyle.hxx>
48 #include <com/sun/star/embed/XTransactedObject.hpp>
49 #include <comphelper/processfactory.hxx>
50 #include <unotools/streamwrap.hxx>
51 #include <svx/xmlgrhlp.hxx>
53 #include <xmlxtexp.hxx>
55 #include <comphelper/storagehelper.hxx>
56 #include <memory>
58 using namespace com::sun::star;
59 using namespace com::sun::star::container;
60 using namespace com::sun::star::document;
61 using namespace com::sun::star::uno;
62 using namespace com::sun::star::awt;
63 using namespace com::sun::star::lang;
64 using namespace com::sun::star::xml::sax;
65 using namespace ::xmloff::token;
66 using namespace cppu;
68 using com::sun::star::embed::XTransactedObject;
70 namespace {
72 class SvxXMLTableEntryExporter
74 public:
75 explicit SvxXMLTableEntryExporter( SvXMLExport& rExport ) : mrExport( rExport ) {}
76 virtual ~SvxXMLTableEntryExporter();
78 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) = 0;
80 protected:
81 SvXMLExport& mrExport;
84 class SvxXMLColorEntryExporter : public SvxXMLTableEntryExporter
86 public:
87 explicit SvxXMLColorEntryExporter( SvXMLExport& rExport );
89 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
92 class SvxXMLLineEndEntryExporter : public SvxXMLTableEntryExporter
94 public:
95 explicit SvxXMLLineEndEntryExporter( SvXMLExport& rExport );
97 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
98 private:
99 XMLMarkerStyleExport maMarkerStyle;
102 class SvxXMLDashEntryExporter : public SvxXMLTableEntryExporter
104 public:
105 explicit SvxXMLDashEntryExporter( SvXMLExport& rExport );
107 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
109 private:
110 XMLDashStyleExport maDashStyle;
113 class SvxXMLHatchEntryExporter : public SvxXMLTableEntryExporter
115 public:
116 explicit SvxXMLHatchEntryExporter( SvXMLExport& rExport );
118 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
119 private:
120 XMLHatchStyleExport maHatchStyle;
123 class SvxXMLGradientEntryExporter : public SvxXMLTableEntryExporter
125 public:
126 explicit SvxXMLGradientEntryExporter( SvXMLExport& rExport );
128 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
129 private:
130 XMLGradientStyleExport maGradientStyle;
133 class SvxXMLBitmapEntryExporter : public SvxXMLTableEntryExporter
135 public:
136 explicit SvxXMLBitmapEntryExporter( SvXMLExport& rExport );
138 virtual void exportEntry( const OUString& rStrName, const Any& rValue ) override;
143 SvxXMLXTableExportComponent::SvxXMLXTableExportComponent(
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 )
152 GetNamespaceMap_().Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
153 GetNamespaceMap_().Add( GetXMLToken(XML_NP_OFFICE), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
154 GetNamespaceMap_().Add( GetXMLToken(XML_NP_DRAW), GetXMLToken(XML_N_DRAW), XML_NAMESPACE_DRAW );
155 GetNamespaceMap_().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
156 GetNamespaceMap_().Add( GetXMLToken(XML_NP_SVG), GetXMLToken(XML_N_SVG), XML_NAMESPACE_SVG );
157 GetNamespaceMap_().Add( GetXMLToken(XML_NP_LO_EXT), GetXMLToken(XML_N_LO_EXT), XML_NAMESPACE_LO_EXT);
158 SetGraphicStorageHandler(xGraphicStorageHandler);
161 SvxXMLXTableExportComponent::~SvxXMLXTableExportComponent()
165 static void initializeStreamMetadata( const uno::Reference< uno::XInterface > &xOut )
167 uno::Reference< beans::XPropertySet > xProps( xOut, uno::UNO_QUERY );
168 if( !xProps.is() )
170 OSL_FAIL( "Missing stream metadata interface" );
171 return;
176 xProps->setPropertyValue("MediaType", uno::Any( OUString( "text/xml" ) ) );
178 // use stock encryption
179 xProps->setPropertyValue("UseCommonStoragePasswordEncryption", uno::Any( true ) );
180 } catch ( const uno::Exception & )
182 TOOLS_WARN_EXCEPTION("svx", "exception setting stream metadata");
186 static void createStorageStream( uno::Reference < io::XOutputStream > *xOut,
187 rtl::Reference<SvXMLGraphicHelper>& rxGraphicHelper,
188 const uno::Reference < embed::XStorage >& xSubStorage )
190 uno::Reference < io::XStream > xStream = xSubStorage->openStreamElement(
191 "Content.xml",
192 embed::ElementModes::WRITE );
193 rxGraphicHelper = SvXMLGraphicHelper::Create( xSubStorage, SvXMLGraphicHelperMode::Write );
194 initializeStreamMetadata( xStream );
195 *xOut = xStream->getOutputStream();
198 bool SvxXMLXTableExportComponent::save(
199 const OUString& rURL,
200 const uno::Reference<container::XNameContainer >& xTable,
201 const uno::Reference<embed::XStorage >& xStorage,
202 OUString *pOptName )
204 bool bRet = false;
205 std::unique_ptr<SfxMedium> pMedium;
206 rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
208 INetURLObject aURLObj( rURL );
209 bool bToStorage = aURLObj.GetProtocol() == INetProtocol::NotValid; // a relative path
210 bool bSaveAsStorage = xTable->getElementType() == cppu::UnoType<awt::XBitmap>::get();
212 if( pOptName )
213 *pOptName = rURL;
217 uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
219 uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
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;
227 if( !bToStorage || !xStorage.is() )
228 { // local URL -> SfxMedium route
229 if( bSaveAsStorage )
230 xSubStorage = ::comphelper::OStorageHelper::GetStorageFromURL( rURL, eCreate );
231 else
233 pMedium.reset(new SfxMedium( rURL, StreamMode::WRITE | StreamMode::TRUNC ));
235 SvStream* pStream = pMedium->GetOutStream();
236 if( !pStream )
238 OSL_FAIL( "no output stream!" );
239 return false;
242 xOut = new utl::OOutputStreamWrapper( *pStream );
245 else // save into the xSubStorage
247 OUString aPath = rURL;
249 if( bSaveAsStorage )
251 try {
252 xSubStorage = xStorage->openStorageElement( aPath, eCreate );
253 } catch (uno::Exception &) {
254 OSL_FAIL( "no output storage!" );
255 return false;
258 else
260 aPath += ".xml";
261 try {
262 xStream = xStorage->openStreamElement( aPath, eCreate );
263 if( !xStream.is() )
264 return false;
265 initializeStreamMetadata( xStream );
266 xOut = xStream->getOutputStream();
267 } catch (uno::Exception &) {
268 OSL_FAIL( "no output stream!" );
269 return false;
271 if( pOptName )
272 *pOptName = aPath;
276 if( !xOut.is() && xSubStorage.is() )
277 createStorageStream( &xOut, xGraphicHelper, xSubStorage );
278 if( !xOut.is() )
279 return false;
281 xWriter->setOutputStream( xOut );
282 if( xGraphicHelper.is() )
283 xGraphicStorageHandler = xGraphicHelper.get();
285 // Finally do the export
286 rtl::Reference< SvxXMLXTableExportComponent > xExporter( new SvxXMLXTableExportComponent( xContext, xWriter, xTable, xGraphicStorageHandler ) );
287 bRet = xExporter->exportTable();
289 if( xGraphicHelper )
290 xGraphicHelper->dispose();
291 xGraphicHelper.clear();
293 if( xSubStorage.is() )
295 uno::Reference< XTransactedObject > xTrans( xSubStorage, UNO_QUERY );
296 if( xTrans.is() )
297 xTrans->commit();
299 xSubStorage->dispose();
302 catch( uno::Exception& )
304 bRet = false;
307 if( pMedium )
308 pMedium->Commit();
310 return bRet;
313 bool SvxXMLXTableExportComponent::exportTable() noexcept
315 bool bRet = false;
319 GetDocHandler()->startDocument();
321 addChaffWhenEncryptedStorage();
323 // export namespaces
324 sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
325 while( USHRT_MAX != nPos )
327 GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
328 nPos = GetNamespaceMap().GetNextKey( nPos );
333 if( !mxTable.is() )
334 break;
336 char const* pEleName;
337 Type aExportType = mxTable->getElementType();
338 std::unique_ptr<SvxXMLTableEntryExporter> pExporter;
340 if( aExportType == cppu::UnoType<sal_Int32>::get() )
342 pExporter.reset(new SvxXMLColorEntryExporter(*this));
343 pEleName = "color-table";
345 else if( aExportType == cppu::UnoType< drawing::PolyPolygonBezierCoords >::get() )
347 pExporter.reset(new SvxXMLLineEndEntryExporter(*this));
348 pEleName = "marker-table";
350 else if( aExportType == cppu::UnoType< drawing::LineDash >::get() )
352 pExporter.reset(new SvxXMLDashEntryExporter(*this));
353 pEleName = "dash-table";
355 else if( aExportType == cppu::UnoType< drawing::Hatch >::get() )
357 pExporter.reset(new SvxXMLHatchEntryExporter(*this));
358 pEleName = "hatch-table";
360 else if( aExportType == cppu::UnoType< awt::Gradient >::get() )
362 pExporter.reset(new SvxXMLGradientEntryExporter(*this));
363 pEleName = "gradient-table";
365 else if( aExportType == cppu::UnoType<awt::XBitmap>::get())
367 pExporter.reset(new SvxXMLBitmapEntryExporter(*this));
368 pEleName = "bitmap-table";
370 else
372 OSL_FAIL( "unknown type for export");
373 break;
376 SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, true, true );
378 const Sequence< OUString > aNames = mxTable->getElementNames();
379 Any aAny;
381 for( const OUString& rName : aNames )
383 aAny = mxTable->getByName( rName );
384 pExporter->exportEntry( rName, aAny );
387 bRet = true;
389 while(false);
391 GetDocHandler()->endDocument();
393 catch( Exception const& )
395 bRet = false;
398 return bRet;
401 // methods without content:
402 void SvxXMLXTableExportComponent::ExportAutoStyles_() {}
403 void SvxXMLXTableExportComponent::ExportMasterStyles_() {}
404 void SvxXMLXTableExportComponent::ExportContent_() {}
407 SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
412 SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
413 : SvxXMLTableEntryExporter( rExport )
417 void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
419 mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
421 sal_Int32 nColor = 0;
422 rValue >>= nColor;
424 OUStringBuffer aOut;
425 ::sax::Converter::convertColor( aOut, nColor );
426 mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
428 SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, true, true );
432 SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
433 : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
437 void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
439 maMarkerStyle.exportXML( rStrName, rValue );
443 SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
444 : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
448 void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
450 maDashStyle.exportXML( rStrName, rValue );
454 SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
455 : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
459 void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
461 maHatchStyle.exportXML( rStrName, rValue );
465 SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
466 : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
470 void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
472 maGradientStyle.exportXML( rStrName, rValue );
476 SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
477 : SvxXMLTableEntryExporter( rExport )
481 void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
483 XMLImageStyle::exportXML(rStrName, rValue, mrExport);
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */