Line data Source code
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/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>
30 :
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"
36 :
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"
48 :
49 : #include "xmlxtexp.hxx"
50 :
51 : #include <comphelper/storagehelper.hxx>
52 :
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;
62 : using namespace cppu;
63 :
64 : using com::sun::star::embed::XTransactedObject;
65 :
66 : class SvxXMLTableEntryExporter
67 : {
68 : public:
69 0 : SvxXMLTableEntryExporter( SvXMLExport& rExport ) : mrExport( rExport ) {}
70 : virtual ~SvxXMLTableEntryExporter();
71 :
72 : virtual void exportEntry( const OUString& rStrName, const Any& rValue ) = 0;
73 :
74 : protected:
75 : SvXMLExport& mrExport;
76 : };
77 :
78 : class SvxXMLColorEntryExporter : public SvxXMLTableEntryExporter
79 : {
80 : public:
81 : SvxXMLColorEntryExporter( SvXMLExport& rExport );
82 : virtual ~SvxXMLColorEntryExporter();
83 :
84 : virtual void exportEntry( const OUString& rStrName, const Any& rValue );
85 : };
86 :
87 : class SvxXMLLineEndEntryExporter : public SvxXMLTableEntryExporter
88 : {
89 : public:
90 : SvxXMLLineEndEntryExporter( SvXMLExport& rExport );
91 : SvxXMLLineEndEntryExporter();
92 : virtual ~SvxXMLLineEndEntryExporter();
93 :
94 : virtual void exportEntry( const OUString& rStrName, const Any& rValue );
95 : private:
96 : XMLMarkerStyleExport maMarkerStyle;
97 : };
98 :
99 : class SvxXMLDashEntryExporter : public SvxXMLTableEntryExporter
100 : {
101 : public:
102 : SvxXMLDashEntryExporter( SvXMLExport& rExport );
103 : virtual ~SvxXMLDashEntryExporter();
104 :
105 : virtual void exportEntry( const OUString& rStrName, const Any& rValue );
106 :
107 : private:
108 : XMLDashStyleExport maDashStyle;
109 : };
110 :
111 : class SvxXMLHatchEntryExporter : public SvxXMLTableEntryExporter
112 : {
113 : public:
114 : SvxXMLHatchEntryExporter( SvXMLExport& rExport );
115 : virtual ~SvxXMLHatchEntryExporter();
116 :
117 : virtual void exportEntry( const OUString& rStrName, const Any& rValue );
118 : private:
119 : XMLHatchStyleExport maHatchStyle;
120 : };
121 :
122 : class SvxXMLGradientEntryExporter : public SvxXMLTableEntryExporter
123 : {
124 : public:
125 : SvxXMLGradientEntryExporter( SvXMLExport& rExport );
126 : virtual ~SvxXMLGradientEntryExporter();
127 :
128 : virtual void exportEntry( const OUString& rStrName, const Any& rValue );
129 : private:
130 : XMLGradientStyleExport maGradientStyle;
131 : };
132 :
133 : class SvxXMLBitmapEntryExporter : public SvxXMLTableEntryExporter
134 : {
135 : public:
136 : SvxXMLBitmapEntryExporter( SvXMLExport& rExport );
137 : virtual ~SvxXMLBitmapEntryExporter();
138 :
139 : virtual void exportEntry( const OUString& rStrName, const Any& rValue );
140 :
141 : private:
142 : XMLImageStyle maImageStyle;
143 : };
144 :
145 : ///////////////////////////////////////////////////////////////////////
146 :
147 : // #110680#
148 0 : SvxXMLXTableExportComponent::SvxXMLXTableExportComponent(
149 : const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
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( xServiceFactory, rFileName, rHandler, NULL, MAP_100TH_MM),
155 0 : mxTable( xTable )
156 : {
157 :
158 0 : _GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
159 0 : _GetNamespaceMap().Add( GetXMLToken(XML_NP_OFFICE), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
160 0 : _GetNamespaceMap().Add( GetXMLToken(XML_NP_DRAW), GetXMLToken(XML_N_DRAW), XML_NAMESPACE_DRAW );
161 0 : _GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
162 0 : _GetNamespaceMap().Add( GetXMLToken(XML_NP_SVG), GetXMLToken(XML_N_SVG), XML_NAMESPACE_SVG );
163 0 : SetGraphicResolver( xGrfResolver );
164 0 : setExportFlags( 0 );
165 0 : }
166 :
167 0 : SvxXMLXTableExportComponent::~SvxXMLXTableExportComponent()
168 : {
169 0 : }
170 :
171 0 : static void initializeStreamMetadata( const uno::Reference< uno::XInterface > &xOut )
172 : {
173 0 : uno::Reference< beans::XPropertySet > xProps( xOut, uno::UNO_QUERY );
174 0 : if( !xProps.is() )
175 : {
176 : OSL_FAIL( "Missing stream metadata interface" );
177 0 : return;
178 : }
179 :
180 : try
181 : {
182 0 : xProps->setPropertyValue(
183 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ),
184 0 : uno::makeAny( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "text/xml" ) ) ) );
185 :
186 : // use stock encryption
187 0 : xProps->setPropertyValue(
188 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseCommonStoragePasswordEncryption" ) ),
189 0 : uno::makeAny( sal_True ) );
190 0 : } catch ( const uno::Exception & )
191 : {
192 : OSL_FAIL( "exception setting stream metadata" );
193 0 : }
194 : }
195 :
196 0 : static void createStorageStream( uno::Reference < io::XOutputStream > *xOut,
197 : SvXMLGraphicHelper **ppGraphicHelper,
198 : uno::Reference < embed::XStorage > xSubStorage )
199 : {
200 0 : uno::Reference < io::XStream > xStream;
201 0 : xStream = xSubStorage->openStreamElement(
202 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Content.xml" ) ),
203 0 : embed::ElementModes::WRITE );
204 0 : *ppGraphicHelper = SvXMLGraphicHelper::Create( xSubStorage, GRAPHICHELPER_MODE_WRITE );
205 0 : initializeStreamMetadata( xStream );
206 0 : *xOut = xStream->getOutputStream();
207 0 : }
208 :
209 0 : bool SvxXMLXTableExportComponent::save(
210 : const OUString& rURL,
211 : const uno::Reference<container::XNameContainer >& xTable,
212 : const uno::Reference<embed::XStorage >& xStorage,
213 : rtl::OUString *pOptName ) throw()
214 : {
215 0 : bool bRet = false;
216 0 : SfxMedium* pMedium = NULL;
217 0 : SvXMLGraphicHelper* pGraphicHelper = NULL;
218 0 : sal_Int32 eCreate = embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE;
219 :
220 0 : INetURLObject aURLObj( rURL );
221 0 : bool bToStorage = aURLObj.GetProtocol() == INET_PROT_NOT_VALID; // a relative path
222 :
223 0 : sal_Bool bSaveAsStorage = xTable->getElementType() == ::getCppuType((const OUString*)0);
224 :
225 0 : if( pOptName )
226 0 : *pOptName = rURL;
227 :
228 : try
229 : {
230 0 : uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
231 0 : uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
232 0 : if( !xServiceFactory.is() )
233 : {
234 : OSL_FAIL( "got no service manager" );
235 0 : return false;
236 : }
237 :
238 0 : uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
239 :
240 0 : uno::Reference < io::XStream > xStream;
241 0 : uno::Reference < io::XOutputStream > xOut;
242 0 : uno::Reference<embed::XStorage > xSubStorage;
243 0 : uno::Reference< XGraphicObjectResolver > xGrfResolver;
244 :
245 0 : uno::Reference<xml::sax::XDocumentHandler> xHandler( xWriter, uno::UNO_QUERY );
246 :
247 0 : if( !bToStorage || !xStorage.is() )
248 : { // local URL -> SfxMedium route
249 0 : if( bSaveAsStorage )
250 0 : xSubStorage = ::comphelper::OStorageHelper::GetStorageFromURL( rURL, eCreate );
251 : else
252 : {
253 0 : pMedium = new SfxMedium( rURL, STREAM_WRITE | STREAM_TRUNC );
254 0 : pMedium->IsRemote();
255 :
256 0 : SvStream* pStream = pMedium->GetOutStream();
257 0 : if( !pStream )
258 : {
259 : OSL_FAIL( "no output stream!" );
260 0 : return false;
261 : }
262 :
263 0 : xOut = new utl::OOutputStreamWrapper( *pStream );
264 : }
265 : }
266 : else // save into the xSubStorage
267 : {
268 0 : rtl::OUString aPath = rURL;
269 :
270 0 : if( bSaveAsStorage )
271 : {
272 : try {
273 0 : xSubStorage = xStorage->openStorageElement( aPath, eCreate );
274 0 : } catch (uno::Exception &) {
275 : OSL_FAIL( "no output storage!" );
276 0 : return false;
277 : }
278 : }
279 : else
280 : {
281 0 : aPath += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".xml" ) );
282 : try {
283 0 : xStream = xStorage->openStreamElement( aPath, eCreate );
284 0 : if( !xStream.is() )
285 0 : return false;
286 0 : initializeStreamMetadata( xStream );
287 0 : xOut = xStream->getOutputStream();
288 0 : } catch (uno::Exception &) {
289 : OSL_FAIL( "no output stream!" );
290 0 : return false;
291 : }
292 0 : if( pOptName )
293 0 : *pOptName = aPath;
294 0 : }
295 : }
296 :
297 0 : if( !xOut.is() && xSubStorage.is() )
298 0 : createStorageStream( &xOut, &pGraphicHelper, xSubStorage );
299 0 : if( !xOut.is() )
300 0 : return false;
301 :
302 0 : uno::Reference<io::XActiveDataSource> xMetaSrc( xWriter, uno::UNO_QUERY );
303 0 : xMetaSrc->setOutputStream( xOut );
304 0 : if( pGraphicHelper )
305 0 : xGrfResolver = pGraphicHelper;
306 :
307 : // Finally do the export
308 0 : const OUString aName;
309 0 : SvxXMLXTableExportComponent aExporter( xServiceFactory, aName, xHandler, xTable, xGrfResolver );
310 0 : bRet = aExporter.exportTable();
311 :
312 0 : if( pGraphicHelper )
313 0 : SvXMLGraphicHelper::Destroy( pGraphicHelper );
314 :
315 0 : if( xSubStorage.is() )
316 : {
317 0 : uno::Reference< XTransactedObject > xTrans( xSubStorage, UNO_QUERY );
318 0 : if( xTrans.is() )
319 0 : xTrans->commit();
320 :
321 0 : uno::Reference< XComponent > xComp( xSubStorage, UNO_QUERY );
322 0 : if( xComp.is() )
323 0 : xSubStorage->dispose();
324 0 : }
325 : }
326 0 : catch( uno::Exception& )
327 : {
328 0 : bRet = false;
329 : }
330 :
331 0 : if( pMedium )
332 : {
333 0 : pMedium->Commit();
334 0 : delete pMedium;
335 : }
336 :
337 0 : return bRet;
338 : }
339 :
340 0 : bool SvxXMLXTableExportComponent::exportTable() throw()
341 : {
342 0 : bool bRet = false;
343 :
344 : try
345 : {
346 0 : GetDocHandler()->startDocument();
347 :
348 0 : addChaffWhenEncryptedStorage();
349 :
350 : // export namespaces
351 0 : sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
352 0 : while( USHRT_MAX != nPos )
353 : {
354 0 : GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
355 0 : nPos = GetNamespaceMap().GetNextKey( nPos );
356 : }
357 :
358 : do
359 : {
360 0 : if( !mxTable.is() )
361 : break;
362 :
363 : char const* pEleName;
364 0 : Type aExportType = mxTable->getElementType();
365 0 : SvxXMLTableEntryExporter* pExporter = NULL;
366 :
367 0 : if( aExportType == ::getCppuType((const sal_Int32*)0) )
368 : {
369 0 : pExporter = new SvxXMLColorEntryExporter(*this);
370 0 : pEleName = "color-table";
371 : }
372 0 : else if( aExportType == ::getCppuType((const drawing::PolyPolygonBezierCoords*)0) )
373 : {
374 0 : pExporter = new SvxXMLLineEndEntryExporter(*this);
375 0 : pEleName = "marker-table";
376 : }
377 0 : else if( aExportType == ::getCppuType((const drawing::LineDash*)0) )
378 : {
379 0 : pExporter = new SvxXMLDashEntryExporter(*this);
380 0 : pEleName = "dash-table";
381 : }
382 0 : else if( aExportType == ::getCppuType((const drawing::Hatch*)0) )
383 : {
384 0 : pExporter = new SvxXMLHatchEntryExporter(*this);
385 0 : pEleName = "hatch-table";
386 : }
387 0 : else if( aExportType == ::getCppuType((const awt::Gradient*)0))
388 : {
389 0 : pExporter = new SvxXMLGradientEntryExporter(*this);
390 0 : pEleName = "gradient-table";
391 : }
392 0 : else if( aExportType == ::getCppuType((const OUString*)0))
393 : {
394 0 : pExporter = new SvxXMLBitmapEntryExporter(*this);
395 0 : pEleName = "bitmap-table";
396 : }
397 : else
398 : {
399 : OSL_FAIL( "unknown type for export");
400 : break;
401 : }
402 :
403 0 : SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, sal_True, sal_True );
404 :
405 0 : Sequence< OUString > aNames = mxTable->getElementNames();
406 0 : const sal_Int32 nCount = aNames.getLength();
407 0 : const OUString* pNames = aNames.getConstArray();
408 0 : Any aAny;
409 :
410 : sal_Int32 nIndex;
411 0 : for( nIndex = 0; nIndex < nCount; nIndex++, pNames++ )
412 : {
413 0 : aAny = mxTable->getByName( *pNames );
414 0 : pExporter->exportEntry( *pNames, aAny );
415 : }
416 :
417 0 : bRet = true;
418 : }
419 : while(0);
420 :
421 0 : GetDocHandler()->endDocument();
422 : }
423 0 : catch( Exception const& )
424 : {
425 0 : bRet = false;
426 : }
427 :
428 0 : return bRet;
429 : }
430 :
431 : // methods without content:
432 0 : void SvxXMLXTableExportComponent::_ExportAutoStyles() {}
433 0 : void SvxXMLXTableExportComponent::_ExportMasterStyles() {}
434 0 : void SvxXMLXTableExportComponent::_ExportContent() {}
435 :
436 : ///////////////////////////////////////////////////////////////////////
437 :
438 0 : SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
439 : {
440 0 : }
441 :
442 : ///////////////////////////////////////////////////////////////////////
443 :
444 0 : SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
445 0 : : SvxXMLTableEntryExporter( rExport )
446 : {
447 0 : }
448 :
449 0 : SvxXMLColorEntryExporter::~SvxXMLColorEntryExporter()
450 : {
451 0 : }
452 :
453 0 : void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
454 : {
455 0 : mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
456 :
457 0 : sal_Int32 nColor = 0;
458 0 : rValue >>= nColor;
459 :
460 0 : OUStringBuffer aOut;
461 0 : ::sax::Converter::convertColor( aOut, nColor );
462 0 : mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
463 :
464 0 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, sal_True, sal_True );
465 0 : }
466 :
467 : ///////////////////////////////////////////////////////////////////////
468 :
469 0 : SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
470 0 : : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
471 : {
472 0 : }
473 :
474 0 : SvxXMLLineEndEntryExporter::~SvxXMLLineEndEntryExporter()
475 : {
476 0 : }
477 :
478 0 : void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
479 : {
480 0 : maMarkerStyle.exportXML( rStrName, rValue );
481 0 : }
482 :
483 : ///////////////////////////////////////////////////////////////////////
484 :
485 0 : SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
486 0 : : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
487 : {
488 0 : }
489 :
490 0 : SvxXMLDashEntryExporter::~SvxXMLDashEntryExporter()
491 : {
492 0 : }
493 :
494 0 : void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
495 : {
496 0 : maDashStyle.exportXML( rStrName, rValue );
497 0 : }
498 :
499 : ///////////////////////////////////////////////////////////////////////
500 :
501 0 : SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
502 0 : : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
503 : {
504 0 : }
505 :
506 0 : SvxXMLHatchEntryExporter::~SvxXMLHatchEntryExporter()
507 : {
508 0 : }
509 :
510 0 : void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
511 : {
512 0 : maHatchStyle.exportXML( rStrName, rValue );
513 0 : }
514 :
515 : ///////////////////////////////////////////////////////////////////////
516 :
517 0 : SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
518 0 : : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
519 : {
520 0 : }
521 :
522 0 : SvxXMLGradientEntryExporter::~SvxXMLGradientEntryExporter()
523 : {
524 0 : }
525 :
526 0 : void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
527 : {
528 0 : maGradientStyle.exportXML( rStrName, rValue );
529 0 : }
530 :
531 : ///////////////////////////////////////////////////////////////////////
532 :
533 0 : SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
534 0 : : SvxXMLTableEntryExporter( rExport )
535 : {
536 0 : }
537 :
538 0 : SvxXMLBitmapEntryExporter::~SvxXMLBitmapEntryExporter()
539 : {
540 0 : }
541 :
542 0 : void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
543 : {
544 0 : maImageStyle.exportXML( rStrName, rValue, mrExport );
545 0 : }
546 :
547 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|