1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <tools/urlobj.hxx>
21 #include <com/sun/star/container/XNameContainer.hpp>
22 #include <com/sun/star/xml/sax/Writer.hpp>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
25 #include <com/sun/star/drawing/LineDash.hpp>
26 #include <com/sun/star/awt/Gradient.hpp>
27 #include <com/sun/star/drawing/Hatch.hpp>
28 #include <com/sun/star/io/XActiveDataSource.hpp>
29 #include <com/sun/star/embed/ElementModes.hpp>
31 #include <sax/tools/converter.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include "xmloff/xmlnmspe.hxx"
35 #include "xmloff/nmspmap.hxx"
37 #include "xmloff/xmltoken.hxx"
38 #include "xmloff/xmlmetae.hxx"
39 #include "xmloff/DashStyle.hxx"
40 #include "xmloff/GradientStyle.hxx"
41 #include "xmloff/HatchStyle.hxx"
42 #include "xmloff/ImageStyle.hxx"
43 #include "xmloff/MarkerStyle.hxx"
44 #include <com/sun/star/embed/XTransactedObject.hpp>
45 #include <comphelper/processfactory.hxx>
46 #include <unotools/streamwrap.hxx>
47 #include "svx/xmlgrhlp.hxx"
49 #include "xmlxtexp.hxx"
51 #include <comphelper/storagehelper.hxx>
53 using namespace com::sun::star
;
54 using namespace com::sun::star::container
;
55 using namespace com::sun::star::document
;
56 using namespace com::sun::star::uno
;
57 using namespace com::sun::star::awt
;
58 using namespace com::sun::star::lang
;
59 using namespace com::sun::star::xml::sax
;
60 using namespace ::xmloff::token
;
61 using namespace ::rtl
;
64 using com::sun::star::embed::XTransactedObject
;
66 class SvxXMLTableEntryExporter
69 SvxXMLTableEntryExporter( SvXMLExport
& rExport
) : mrExport( rExport
) {}
70 virtual ~SvxXMLTableEntryExporter();
72 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
) = 0;
75 SvXMLExport
& mrExport
;
78 class SvxXMLColorEntryExporter
: public SvxXMLTableEntryExporter
81 SvxXMLColorEntryExporter( SvXMLExport
& rExport
);
82 virtual ~SvxXMLColorEntryExporter();
84 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
87 class SvxXMLLineEndEntryExporter
: public SvxXMLTableEntryExporter
90 SvxXMLLineEndEntryExporter( SvXMLExport
& rExport
);
91 SvxXMLLineEndEntryExporter();
92 virtual ~SvxXMLLineEndEntryExporter();
94 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
96 XMLMarkerStyleExport maMarkerStyle
;
99 class SvxXMLDashEntryExporter
: public SvxXMLTableEntryExporter
102 SvxXMLDashEntryExporter( SvXMLExport
& rExport
);
103 virtual ~SvxXMLDashEntryExporter();
105 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
108 XMLDashStyleExport maDashStyle
;
111 class SvxXMLHatchEntryExporter
: public SvxXMLTableEntryExporter
114 SvxXMLHatchEntryExporter( SvXMLExport
& rExport
);
115 virtual ~SvxXMLHatchEntryExporter();
117 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
119 XMLHatchStyleExport maHatchStyle
;
122 class SvxXMLGradientEntryExporter
: public SvxXMLTableEntryExporter
125 SvxXMLGradientEntryExporter( SvXMLExport
& rExport
);
126 virtual ~SvxXMLGradientEntryExporter();
128 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
130 XMLGradientStyleExport maGradientStyle
;
133 class SvxXMLBitmapEntryExporter
: public SvxXMLTableEntryExporter
136 SvxXMLBitmapEntryExporter( SvXMLExport
& rExport
);
137 virtual ~SvxXMLBitmapEntryExporter();
139 virtual void exportEntry( const OUString
& rStrName
, const Any
& rValue
);
142 XMLImageStyle maImageStyle
;
145 ///////////////////////////////////////////////////////////////////////
148 SvxXMLXTableExportComponent::SvxXMLXTableExportComponent(
149 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> xContext
,
150 const OUString
& rFileName
,
151 const uno::Reference
<xml::sax::XDocumentHandler
> & rHandler
,
152 const uno::Reference
<container::XNameContainer
>& xTable
,
153 uno::Reference
<document::XGraphicObjectResolver
>& xGrfResolver
)
154 : SvXMLExport( xContext
, rFileName
, rHandler
, NULL
, MAP_100TH_MM
),
158 _GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO
), GetXMLToken(XML_N_OOO
), XML_NAMESPACE_OOO
);
159 _GetNamespaceMap().Add( GetXMLToken(XML_NP_OFFICE
), GetXMLToken(XML_N_OFFICE
), XML_NAMESPACE_OFFICE
);
160 _GetNamespaceMap().Add( GetXMLToken(XML_NP_DRAW
), GetXMLToken(XML_N_DRAW
), XML_NAMESPACE_DRAW
);
161 _GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK
), GetXMLToken(XML_N_XLINK
), XML_NAMESPACE_XLINK
);
162 _GetNamespaceMap().Add( GetXMLToken(XML_NP_SVG
), GetXMLToken(XML_N_SVG
), XML_NAMESPACE_SVG
);
163 SetGraphicResolver( xGrfResolver
);
167 SvxXMLXTableExportComponent::~SvxXMLXTableExportComponent()
171 static void initializeStreamMetadata( const uno::Reference
< uno::XInterface
> &xOut
)
173 uno::Reference
< beans::XPropertySet
> xProps( xOut
, uno::UNO_QUERY
);
176 OSL_FAIL( "Missing stream metadata interface" );
182 xProps
->setPropertyValue(
183 OUString( "MediaType" ),
184 uno::makeAny( OUString( "text/xml" ) ) );
186 // use stock encryption
187 xProps
->setPropertyValue(
188 OUString( "UseCommonStoragePasswordEncryption" ),
189 uno::makeAny( sal_True
) );
190 } catch ( const uno::Exception
& )
192 OSL_FAIL( "exception setting stream metadata" );
196 static void createStorageStream( uno::Reference
< io::XOutputStream
> *xOut
,
197 SvXMLGraphicHelper
**ppGraphicHelper
,
198 uno::Reference
< embed::XStorage
> xSubStorage
)
200 uno::Reference
< io::XStream
> xStream
;
201 xStream
= xSubStorage
->openStreamElement(
202 OUString( "Content.xml" ),
203 embed::ElementModes::WRITE
);
204 *ppGraphicHelper
= SvXMLGraphicHelper::Create( xSubStorage
, GRAPHICHELPER_MODE_WRITE
);
205 initializeStreamMetadata( xStream
);
206 *xOut
= xStream
->getOutputStream();
209 bool SvxXMLXTableExportComponent::save(
210 const OUString
& rURL
,
211 const uno::Reference
<container::XNameContainer
>& xTable
,
212 const uno::Reference
<embed::XStorage
>& xStorage
,
213 OUString
*pOptName
) throw()
216 SfxMedium
* pMedium
= NULL
;
217 SvXMLGraphicHelper
* pGraphicHelper
= NULL
;
218 sal_Int32 eCreate
= embed::ElementModes::WRITE
| embed::ElementModes::TRUNCATE
;
220 INetURLObject
aURLObj( rURL
);
221 bool bToStorage
= aURLObj
.GetProtocol() == INET_PROT_NOT_VALID
; // a relative path
223 bool bSaveAsStorage
= xTable
->getElementType() == ::getCppuType((const OUString
*)0);
230 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
232 uno::Reference
< xml::sax::XWriter
> xWriter
= xml::sax::Writer::create( xContext
);
234 uno::Reference
< io::XStream
> xStream
;
235 uno::Reference
< io::XOutputStream
> xOut
;
236 uno::Reference
<embed::XStorage
> xSubStorage
;
237 uno::Reference
< XGraphicObjectResolver
> xGrfResolver
;
239 uno::Reference
<xml::sax::XDocumentHandler
> xHandler( xWriter
, uno::UNO_QUERY
);
241 if( !bToStorage
|| !xStorage
.is() )
242 { // local URL -> SfxMedium route
244 xSubStorage
= ::comphelper::OStorageHelper::GetStorageFromURL( rURL
, eCreate
);
247 pMedium
= new SfxMedium( rURL
, STREAM_WRITE
| STREAM_TRUNC
);
250 SvStream
* pStream
= pMedium
->GetOutStream();
253 OSL_FAIL( "no output stream!" );
257 xOut
= new utl::OOutputStreamWrapper( *pStream
);
260 else // save into the xSubStorage
262 OUString aPath
= rURL
;
267 xSubStorage
= xStorage
->openStorageElement( aPath
, eCreate
);
268 } catch (uno::Exception
&) {
269 OSL_FAIL( "no output storage!" );
275 aPath
+= OUString( ".xml" );
277 xStream
= xStorage
->openStreamElement( aPath
, eCreate
);
280 initializeStreamMetadata( xStream
);
281 xOut
= xStream
->getOutputStream();
282 } catch (uno::Exception
&) {
283 OSL_FAIL( "no output stream!" );
291 if( !xOut
.is() && xSubStorage
.is() )
292 createStorageStream( &xOut
, &pGraphicHelper
, xSubStorage
);
296 uno::Reference
<io::XActiveDataSource
> xMetaSrc( xWriter
, uno::UNO_QUERY
);
297 xMetaSrc
->setOutputStream( xOut
);
299 xGrfResolver
= pGraphicHelper
;
301 // Finally do the export
302 const OUString aName
;
303 SvxXMLXTableExportComponent
aExporter( xContext
, aName
, xHandler
, xTable
, xGrfResolver
);
304 bRet
= aExporter
.exportTable();
307 SvXMLGraphicHelper::Destroy( pGraphicHelper
);
309 if( xSubStorage
.is() )
311 uno::Reference
< XTransactedObject
> xTrans( xSubStorage
, UNO_QUERY
);
315 uno::Reference
< XComponent
> xComp( xSubStorage
, UNO_QUERY
);
317 xSubStorage
->dispose();
320 catch( uno::Exception
& )
334 bool SvxXMLXTableExportComponent::exportTable() throw()
340 GetDocHandler()->startDocument();
342 addChaffWhenEncryptedStorage();
345 sal_uInt16 nPos
= GetNamespaceMap().GetFirstKey();
346 while( USHRT_MAX
!= nPos
)
348 GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos
), GetNamespaceMap().GetNameByKey( nPos
) );
349 nPos
= GetNamespaceMap().GetNextKey( nPos
);
357 char const* pEleName
;
358 Type aExportType
= mxTable
->getElementType();
359 SvxXMLTableEntryExporter
* pExporter
= NULL
;
361 if( aExportType
== ::getCppuType((const sal_Int32
*)0) )
363 pExporter
= new SvxXMLColorEntryExporter(*this);
364 pEleName
= "color-table";
366 else if( aExportType
== ::getCppuType((const drawing::PolyPolygonBezierCoords
*)0) )
368 pExporter
= new SvxXMLLineEndEntryExporter(*this);
369 pEleName
= "marker-table";
371 else if( aExportType
== ::getCppuType((const drawing::LineDash
*)0) )
373 pExporter
= new SvxXMLDashEntryExporter(*this);
374 pEleName
= "dash-table";
376 else if( aExportType
== ::getCppuType((const drawing::Hatch
*)0) )
378 pExporter
= new SvxXMLHatchEntryExporter(*this);
379 pEleName
= "hatch-table";
381 else if( aExportType
== ::getCppuType((const awt::Gradient
*)0))
383 pExporter
= new SvxXMLGradientEntryExporter(*this);
384 pEleName
= "gradient-table";
386 else if( aExportType
== ::getCppuType((const OUString
*)0))
388 pExporter
= new SvxXMLBitmapEntryExporter(*this);
389 pEleName
= "bitmap-table";
393 OSL_FAIL( "unknown type for export");
397 SvXMLElementExport
aElem( *this, XML_NAMESPACE_OOO
, pEleName
, sal_True
, sal_True
);
399 Sequence
< OUString
> aNames
= mxTable
->getElementNames();
400 const sal_Int32 nCount
= aNames
.getLength();
401 const OUString
* pNames
= aNames
.getConstArray();
405 for( nIndex
= 0; nIndex
< nCount
; nIndex
++, pNames
++ )
407 aAny
= mxTable
->getByName( *pNames
);
408 pExporter
->exportEntry( *pNames
, aAny
);
416 GetDocHandler()->endDocument();
418 catch( Exception
const& )
426 // methods without content:
427 void SvxXMLXTableExportComponent::_ExportAutoStyles() {}
428 void SvxXMLXTableExportComponent::_ExportMasterStyles() {}
429 void SvxXMLXTableExportComponent::_ExportContent() {}
431 ///////////////////////////////////////////////////////////////////////
433 SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
437 ///////////////////////////////////////////////////////////////////////
439 SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport
& rExport
)
440 : SvxXMLTableEntryExporter( rExport
)
444 SvxXMLColorEntryExporter::~SvxXMLColorEntryExporter()
448 void SvxXMLColorEntryExporter::exportEntry( const OUString
& rStrName
, const Any
& rValue
)
450 mrExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_NAME
, rStrName
);
452 sal_Int32 nColor
= 0;
456 ::sax::Converter::convertColor( aOut
, nColor
);
457 mrExport
.AddAttribute( XML_NAMESPACE_DRAW
, XML_COLOR
, aOut
.makeStringAndClear() );
459 SvXMLElementExport
aElem( mrExport
, XML_NAMESPACE_DRAW
, XML_COLOR
, sal_True
, sal_True
);
462 ///////////////////////////////////////////////////////////////////////
464 SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport
& rExport
)
465 : SvxXMLTableEntryExporter( rExport
), maMarkerStyle( rExport
)
469 SvxXMLLineEndEntryExporter::~SvxXMLLineEndEntryExporter()
473 void SvxXMLLineEndEntryExporter::exportEntry( const OUString
& rStrName
, const Any
& rValue
)
475 maMarkerStyle
.exportXML( rStrName
, rValue
);
478 ///////////////////////////////////////////////////////////////////////
480 SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport
& rExport
)
481 : SvxXMLTableEntryExporter( rExport
), maDashStyle( rExport
)
485 SvxXMLDashEntryExporter::~SvxXMLDashEntryExporter()
489 void SvxXMLDashEntryExporter::exportEntry( const OUString
& rStrName
, const Any
& rValue
)
491 maDashStyle
.exportXML( rStrName
, rValue
);
494 ///////////////////////////////////////////////////////////////////////
496 SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport
& rExport
)
497 : SvxXMLTableEntryExporter( rExport
), maHatchStyle( rExport
)
501 SvxXMLHatchEntryExporter::~SvxXMLHatchEntryExporter()
505 void SvxXMLHatchEntryExporter::exportEntry( const OUString
& rStrName
, const Any
& rValue
)
507 maHatchStyle
.exportXML( rStrName
, rValue
);
510 ///////////////////////////////////////////////////////////////////////
512 SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport
& rExport
)
513 : SvxXMLTableEntryExporter( rExport
), maGradientStyle( rExport
)
517 SvxXMLGradientEntryExporter::~SvxXMLGradientEntryExporter()
521 void SvxXMLGradientEntryExporter::exportEntry( const OUString
& rStrName
, const Any
& rValue
)
523 maGradientStyle
.exportXML( rStrName
, rValue
);
526 ///////////////////////////////////////////////////////////////////////
528 SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport
& rExport
)
529 : SvxXMLTableEntryExporter( rExport
)
533 SvxXMLBitmapEntryExporter::~SvxXMLBitmapEntryExporter()
537 void SvxXMLBitmapEntryExporter::exportEntry( const OUString
& rStrName
, const Any
& rValue
)
539 maImageStyle
.exportXML( rStrName
, rValue
, mrExport
);
542 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */