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::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),
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 : OUString( "MediaType" ),
184 0 : uno::makeAny( OUString( "text/xml" ) ) );
185 :
186 : // use stock encryption
187 0 : xProps->setPropertyValue(
188 : OUString( "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 : OUString( "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 : 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 : 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< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
231 :
232 0 : uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
233 :
234 0 : uno::Reference < io::XStream > xStream;
235 0 : uno::Reference < io::XOutputStream > xOut;
236 0 : uno::Reference<embed::XStorage > xSubStorage;
237 0 : uno::Reference< XGraphicObjectResolver > xGrfResolver;
238 :
239 0 : uno::Reference<xml::sax::XDocumentHandler> xHandler( xWriter, uno::UNO_QUERY );
240 :
241 0 : if( !bToStorage || !xStorage.is() )
242 : { // local URL -> SfxMedium route
243 0 : if( bSaveAsStorage )
244 0 : xSubStorage = ::comphelper::OStorageHelper::GetStorageFromURL( rURL, eCreate );
245 : else
246 : {
247 0 : pMedium = new SfxMedium( rURL, STREAM_WRITE | STREAM_TRUNC );
248 0 : pMedium->IsRemote();
249 :
250 0 : SvStream* pStream = pMedium->GetOutStream();
251 0 : if( !pStream )
252 : {
253 : OSL_FAIL( "no output stream!" );
254 0 : return false;
255 : }
256 :
257 0 : xOut = new utl::OOutputStreamWrapper( *pStream );
258 : }
259 : }
260 : else // save into the xSubStorage
261 : {
262 0 : OUString aPath = rURL;
263 :
264 0 : if( bSaveAsStorage )
265 : {
266 : try {
267 0 : xSubStorage = xStorage->openStorageElement( aPath, eCreate );
268 0 : } catch (uno::Exception &) {
269 : OSL_FAIL( "no output storage!" );
270 0 : return false;
271 : }
272 : }
273 : else
274 : {
275 0 : aPath += OUString( ".xml" );
276 : try {
277 0 : xStream = xStorage->openStreamElement( aPath, eCreate );
278 0 : if( !xStream.is() )
279 0 : return false;
280 0 : initializeStreamMetadata( xStream );
281 0 : xOut = xStream->getOutputStream();
282 0 : } catch (uno::Exception &) {
283 : OSL_FAIL( "no output stream!" );
284 0 : return false;
285 : }
286 0 : if( pOptName )
287 0 : *pOptName = aPath;
288 0 : }
289 : }
290 :
291 0 : if( !xOut.is() && xSubStorage.is() )
292 0 : createStorageStream( &xOut, &pGraphicHelper, xSubStorage );
293 0 : if( !xOut.is() )
294 0 : return false;
295 :
296 0 : uno::Reference<io::XActiveDataSource> xMetaSrc( xWriter, uno::UNO_QUERY );
297 0 : xMetaSrc->setOutputStream( xOut );
298 0 : if( pGraphicHelper )
299 0 : xGrfResolver = pGraphicHelper;
300 :
301 : // Finally do the export
302 0 : const OUString aName;
303 0 : SvxXMLXTableExportComponent aExporter( xContext, aName, xHandler, xTable, xGrfResolver );
304 0 : bRet = aExporter.exportTable();
305 :
306 0 : if( pGraphicHelper )
307 0 : SvXMLGraphicHelper::Destroy( pGraphicHelper );
308 :
309 0 : if( xSubStorage.is() )
310 : {
311 0 : uno::Reference< XTransactedObject > xTrans( xSubStorage, UNO_QUERY );
312 0 : if( xTrans.is() )
313 0 : xTrans->commit();
314 :
315 0 : uno::Reference< XComponent > xComp( xSubStorage, UNO_QUERY );
316 0 : if( xComp.is() )
317 0 : xSubStorage->dispose();
318 0 : }
319 : }
320 0 : catch( uno::Exception& )
321 : {
322 0 : bRet = false;
323 : }
324 :
325 0 : if( pMedium )
326 : {
327 0 : pMedium->Commit();
328 0 : delete pMedium;
329 : }
330 :
331 0 : return bRet;
332 : }
333 :
334 0 : bool SvxXMLXTableExportComponent::exportTable() throw()
335 : {
336 0 : bool bRet = false;
337 :
338 : try
339 : {
340 0 : GetDocHandler()->startDocument();
341 :
342 0 : addChaffWhenEncryptedStorage();
343 :
344 : // export namespaces
345 0 : sal_uInt16 nPos = GetNamespaceMap().GetFirstKey();
346 0 : while( USHRT_MAX != nPos )
347 : {
348 0 : GetAttrList().AddAttribute( GetNamespaceMap().GetAttrNameByKey( nPos ), GetNamespaceMap().GetNameByKey( nPos ) );
349 0 : nPos = GetNamespaceMap().GetNextKey( nPos );
350 : }
351 :
352 : do
353 : {
354 0 : if( !mxTable.is() )
355 0 : break;
356 :
357 : char const* pEleName;
358 0 : Type aExportType = mxTable->getElementType();
359 0 : SvxXMLTableEntryExporter* pExporter = NULL;
360 :
361 0 : if( aExportType == ::getCppuType((const sal_Int32*)0) )
362 : {
363 0 : pExporter = new SvxXMLColorEntryExporter(*this);
364 0 : pEleName = "color-table";
365 : }
366 0 : else if( aExportType == ::getCppuType((const drawing::PolyPolygonBezierCoords*)0) )
367 : {
368 0 : pExporter = new SvxXMLLineEndEntryExporter(*this);
369 0 : pEleName = "marker-table";
370 : }
371 0 : else if( aExportType == ::getCppuType((const drawing::LineDash*)0) )
372 : {
373 0 : pExporter = new SvxXMLDashEntryExporter(*this);
374 0 : pEleName = "dash-table";
375 : }
376 0 : else if( aExportType == ::getCppuType((const drawing::Hatch*)0) )
377 : {
378 0 : pExporter = new SvxXMLHatchEntryExporter(*this);
379 0 : pEleName = "hatch-table";
380 : }
381 0 : else if( aExportType == ::getCppuType((const awt::Gradient*)0))
382 : {
383 0 : pExporter = new SvxXMLGradientEntryExporter(*this);
384 0 : pEleName = "gradient-table";
385 : }
386 0 : else if( aExportType == ::getCppuType((const OUString*)0))
387 : {
388 0 : pExporter = new SvxXMLBitmapEntryExporter(*this);
389 0 : pEleName = "bitmap-table";
390 : }
391 : else
392 : {
393 : OSL_FAIL( "unknown type for export");
394 0 : break;
395 : }
396 :
397 0 : SvXMLElementExport aElem( *this, XML_NAMESPACE_OOO, pEleName, sal_True, sal_True );
398 :
399 0 : Sequence< OUString > aNames = mxTable->getElementNames();
400 0 : const sal_Int32 nCount = aNames.getLength();
401 0 : const OUString* pNames = aNames.getConstArray();
402 0 : Any aAny;
403 :
404 : sal_Int32 nIndex;
405 0 : for( nIndex = 0; nIndex < nCount; nIndex++, pNames++ )
406 : {
407 0 : aAny = mxTable->getByName( *pNames );
408 0 : pExporter->exportEntry( *pNames, aAny );
409 : }
410 0 : delete pExporter;
411 :
412 0 : bRet = true;
413 : }
414 : while(0);
415 :
416 0 : GetDocHandler()->endDocument();
417 : }
418 0 : catch( Exception const& )
419 : {
420 0 : bRet = false;
421 : }
422 :
423 0 : return bRet;
424 : }
425 :
426 : // methods without content:
427 0 : void SvxXMLXTableExportComponent::_ExportAutoStyles() {}
428 0 : void SvxXMLXTableExportComponent::_ExportMasterStyles() {}
429 0 : void SvxXMLXTableExportComponent::_ExportContent() {}
430 :
431 : ///////////////////////////////////////////////////////////////////////
432 :
433 0 : SvxXMLTableEntryExporter::~SvxXMLTableEntryExporter()
434 : {
435 0 : }
436 :
437 : ///////////////////////////////////////////////////////////////////////
438 :
439 0 : SvxXMLColorEntryExporter::SvxXMLColorEntryExporter( SvXMLExport& rExport )
440 0 : : SvxXMLTableEntryExporter( rExport )
441 : {
442 0 : }
443 :
444 0 : SvxXMLColorEntryExporter::~SvxXMLColorEntryExporter()
445 : {
446 0 : }
447 :
448 0 : void SvxXMLColorEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
449 : {
450 0 : mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME, rStrName );
451 :
452 0 : sal_Int32 nColor = 0;
453 0 : rValue >>= nColor;
454 :
455 0 : OUStringBuffer aOut;
456 0 : ::sax::Converter::convertColor( aOut, nColor );
457 0 : mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_COLOR, aOut.makeStringAndClear() );
458 :
459 0 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DRAW, XML_COLOR, sal_True, sal_True );
460 0 : }
461 :
462 : ///////////////////////////////////////////////////////////////////////
463 :
464 0 : SvxXMLLineEndEntryExporter::SvxXMLLineEndEntryExporter( SvXMLExport& rExport )
465 0 : : SvxXMLTableEntryExporter( rExport ), maMarkerStyle( rExport )
466 : {
467 0 : }
468 :
469 0 : SvxXMLLineEndEntryExporter::~SvxXMLLineEndEntryExporter()
470 : {
471 0 : }
472 :
473 0 : void SvxXMLLineEndEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
474 : {
475 0 : maMarkerStyle.exportXML( rStrName, rValue );
476 0 : }
477 :
478 : ///////////////////////////////////////////////////////////////////////
479 :
480 0 : SvxXMLDashEntryExporter::SvxXMLDashEntryExporter( SvXMLExport& rExport )
481 0 : : SvxXMLTableEntryExporter( rExport ), maDashStyle( rExport )
482 : {
483 0 : }
484 :
485 0 : SvxXMLDashEntryExporter::~SvxXMLDashEntryExporter()
486 : {
487 0 : }
488 :
489 0 : void SvxXMLDashEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
490 : {
491 0 : maDashStyle.exportXML( rStrName, rValue );
492 0 : }
493 :
494 : ///////////////////////////////////////////////////////////////////////
495 :
496 0 : SvxXMLHatchEntryExporter::SvxXMLHatchEntryExporter( SvXMLExport& rExport )
497 0 : : SvxXMLTableEntryExporter( rExport ), maHatchStyle( rExport )
498 : {
499 0 : }
500 :
501 0 : SvxXMLHatchEntryExporter::~SvxXMLHatchEntryExporter()
502 : {
503 0 : }
504 :
505 0 : void SvxXMLHatchEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
506 : {
507 0 : maHatchStyle.exportXML( rStrName, rValue );
508 0 : }
509 :
510 : ///////////////////////////////////////////////////////////////////////
511 :
512 0 : SvxXMLGradientEntryExporter::SvxXMLGradientEntryExporter( SvXMLExport& rExport )
513 0 : : SvxXMLTableEntryExporter( rExport ), maGradientStyle( rExport )
514 : {
515 0 : }
516 :
517 0 : SvxXMLGradientEntryExporter::~SvxXMLGradientEntryExporter()
518 : {
519 0 : }
520 :
521 0 : void SvxXMLGradientEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
522 : {
523 0 : maGradientStyle.exportXML( rStrName, rValue );
524 0 : }
525 :
526 : ///////////////////////////////////////////////////////////////////////
527 :
528 0 : SvxXMLBitmapEntryExporter::SvxXMLBitmapEntryExporter( SvXMLExport& rExport )
529 0 : : SvxXMLTableEntryExporter( rExport )
530 : {
531 0 : }
532 :
533 0 : SvxXMLBitmapEntryExporter::~SvxXMLBitmapEntryExporter()
534 : {
535 0 : }
536 :
537 0 : void SvxXMLBitmapEntryExporter::exportEntry( const OUString& rStrName, const Any& rValue )
538 : {
539 0 : maImageStyle.exportXML( rStrName, rValue, mrExport );
540 258 : }
541 :
542 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|