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 :
21 : /** this file implements an export of a selected EditEngine content into
22 : a xml stream. See editeng/source/inc/xmledit.hxx for interface */
23 : #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
24 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/io/XActiveDataSource.hpp>
27 : #include <com/sun/star/xml/sax/Writer.hpp>
28 : #include <svl/itemprop.hxx>
29 : #include <svl/brdcst.hxx>
30 : #include <com/sun/star/uno/Sequence.hxx>
31 : #include <sot/storage.hxx>
32 : #include <rtl/ustrbuf.hxx>
33 : #include <xmloff/xmluconv.hxx>
34 : #include <xmloff/xmlnmspe.hxx>
35 : #include <xmloff/nmspmap.hxx>
36 : #include <xmloff/xmlmetae.hxx>
37 : #include <cppuhelper/implbase4.hxx>
38 : #include <comphelper/processfactory.hxx>
39 : #include <unotools/streamwrap.hxx>
40 : #include <xmloff/xmlexp.hxx>
41 : #include <editeng/unoedsrc.hxx>
42 : #include <editeng/unofored.hxx>
43 : #include <editeng/unotext.hxx>
44 : #include <editeng/unoprnms.hxx>
45 : #include <editeng/unofield.hxx>
46 : #include <editeng/editeng.hxx>
47 : #include "editsource.hxx"
48 : #include "editxml.hxx"
49 : #include <editeng/unonrule.hxx>
50 : #include <editeng/unoipset.hxx>
51 :
52 : using namespace com::sun::star;
53 : using namespace com::sun::star::container;
54 : using namespace com::sun::star::document;
55 : using namespace com::sun::star::uno;
56 : using namespace com::sun::star::awt;
57 : using namespace com::sun::star::lang;
58 : using namespace com::sun::star::xml::sax;
59 : using namespace ::rtl;
60 : using namespace cppu;
61 :
62 :
63 :
64 : class SvxEditEngineSourceImpl;
65 :
66 :
67 :
68 : class SvxEditEngineSourceImpl
69 : {
70 : private:
71 : oslInterlockedCount maRefCount;
72 :
73 : EditEngine* mpEditEngine;
74 : SvxTextForwarder* mpTextForwarder;
75 :
76 : ~SvxEditEngineSourceImpl();
77 :
78 : public:
79 : SvxEditEngineSourceImpl( EditEngine* pEditEngine );
80 :
81 : void SAL_CALL acquire();
82 : void SAL_CALL release();
83 :
84 : SvxTextForwarder* GetTextForwarder();
85 : };
86 :
87 :
88 :
89 :
90 :
91 :
92 0 : SvxEditEngineSourceImpl::SvxEditEngineSourceImpl( EditEngine* pEditEngine )
93 : : maRefCount(0),
94 : mpEditEngine( pEditEngine ),
95 0 : mpTextForwarder(NULL)
96 : {
97 0 : }
98 :
99 :
100 :
101 0 : SvxEditEngineSourceImpl::~SvxEditEngineSourceImpl()
102 : {
103 0 : delete mpTextForwarder;
104 0 : }
105 :
106 :
107 :
108 0 : void SAL_CALL SvxEditEngineSourceImpl::acquire()
109 : {
110 0 : osl_atomic_increment( &maRefCount );
111 0 : }
112 :
113 :
114 :
115 0 : void SAL_CALL SvxEditEngineSourceImpl::release()
116 : {
117 0 : if( ! osl_atomic_decrement( &maRefCount ) )
118 0 : delete this;
119 0 : }
120 :
121 :
122 :
123 0 : SvxTextForwarder* SvxEditEngineSourceImpl::GetTextForwarder()
124 : {
125 0 : if (!mpTextForwarder)
126 0 : mpTextForwarder = new SvxEditEngineForwarder( *mpEditEngine );
127 :
128 0 : return mpTextForwarder;
129 : }
130 :
131 :
132 : // SvxTextEditSource
133 :
134 :
135 0 : SvxEditEngineSource::SvxEditEngineSource( EditEngine* pEditEngine )
136 : {
137 0 : mpImpl = new SvxEditEngineSourceImpl( pEditEngine );
138 0 : mpImpl->acquire();
139 0 : }
140 :
141 :
142 :
143 0 : SvxEditEngineSource::SvxEditEngineSource( SvxEditEngineSourceImpl* pImpl )
144 : {
145 0 : mpImpl = pImpl;
146 0 : mpImpl->acquire();
147 0 : }
148 :
149 :
150 :
151 0 : SvxEditEngineSource::~SvxEditEngineSource()
152 : {
153 0 : mpImpl->release();
154 0 : }
155 :
156 :
157 :
158 0 : SvxEditSource* SvxEditEngineSource::Clone() const
159 : {
160 0 : return new SvxEditEngineSource( mpImpl );
161 : }
162 :
163 :
164 :
165 0 : SvxTextForwarder* SvxEditEngineSource::GetTextForwarder()
166 : {
167 0 : return mpImpl->GetTextForwarder();
168 : }
169 :
170 :
171 :
172 0 : void SvxEditEngineSource::UpdateData()
173 : {
174 0 : }
175 :
176 : class SvxSimpleUnoModel : public cppu::WeakAggImplHelper4<
177 : ::com::sun::star::frame::XModel,
178 : ::com::sun::star::ucb::XAnyCompareFactory,
179 : ::com::sun::star::style::XStyleFamiliesSupplier,
180 : ::com::sun::star::lang::XMultiServiceFactory >
181 : {
182 : public:
183 : SvxSimpleUnoModel();
184 : virtual ~SvxSimpleUnoModel();
185 :
186 :
187 : // XMultiServiceFactory
188 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL createInstance( const OUString& aServiceSpecifier ) throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
189 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL createInstanceWithArguments( const OUString& ServiceSpecifier, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& Arguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
190 : virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getAvailableServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
191 :
192 : // XStyleFamiliesSupplier
193 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getStyleFamilies( ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
194 :
195 : // XAnyCompareFactory
196 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XAnyCompare > SAL_CALL createAnyCompareByName( const OUString& PropertyName ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
197 :
198 : // XModel
199 : virtual sal_Bool SAL_CALL attachResource( const OUString& aURL, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aArgs ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
200 : virtual OUString SAL_CALL getURL( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
201 : virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > SAL_CALL getArgs( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
202 : virtual void SAL_CALL connectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& xController ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
203 : virtual void SAL_CALL disconnectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& xController ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
204 : virtual void SAL_CALL lockControllers( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
205 : virtual void SAL_CALL unlockControllers( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
206 : virtual sal_Bool SAL_CALL hasControllersLocked( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
207 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController > SAL_CALL getCurrentController( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
208 : virtual void SAL_CALL setCurrentController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& xController ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
209 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getCurrentSelection( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
210 :
211 : // XComponent
212 : virtual void SAL_CALL dispose( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
213 : virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
214 : virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
215 :
216 : };
217 :
218 0 : SvxSimpleUnoModel::SvxSimpleUnoModel()
219 : {
220 0 : }
221 :
222 0 : SvxSimpleUnoModel::~SvxSimpleUnoModel()
223 : {
224 0 : }
225 :
226 : // XMultiServiceFactory ( SvxFmMSFactory )
227 0 : uno::Reference< uno::XInterface > SAL_CALL SvxSimpleUnoModel::createInstance( const OUString& aServiceSpecifier )
228 : throw(uno::Exception, uno::RuntimeException, std::exception)
229 : {
230 0 : if( aServiceSpecifier == "com.sun.star.text.NumberingRules" )
231 : {
232 : return uno::Reference< uno::XInterface >(
233 0 : SvxCreateNumRule(), uno::UNO_QUERY );
234 : }
235 0 : if ( aServiceSpecifier == "com.sun.star.text.textfield.DateTime"
236 0 : || aServiceSpecifier == "com.sun.star.text.TextField.DateTime"
237 : )
238 : {
239 0 : return (::cppu::OWeakObject * )new SvxUnoTextField( text::textfield::Type::DATE );
240 : }
241 :
242 0 : return SvxUnoTextCreateTextField( aServiceSpecifier );
243 :
244 : }
245 :
246 0 : uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL SvxSimpleUnoModel::createInstanceWithArguments( const OUString& ServiceSpecifier, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception)
247 : {
248 0 : return createInstance( ServiceSpecifier );
249 : }
250 :
251 0 : Sequence< OUString > SAL_CALL SvxSimpleUnoModel::getAvailableServiceNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
252 : {
253 0 : Sequence< OUString > aSeq;
254 0 : return aSeq;
255 : }
256 :
257 : // XAnyCompareFactory
258 0 : uno::Reference< com::sun::star::ucb::XAnyCompare > SAL_CALL SvxSimpleUnoModel::createAnyCompareByName( const OUString& PropertyName )
259 : throw(uno::RuntimeException, std::exception)
260 : {
261 : (void)PropertyName;
262 0 : return SvxCreateNumRuleCompare();
263 : }
264 :
265 : // XStyleFamiliesSupplier
266 0 : uno::Reference< container::XNameAccess > SAL_CALL SvxSimpleUnoModel::getStyleFamilies( )
267 : throw(uno::RuntimeException, std::exception)
268 : {
269 0 : uno::Reference< container::XNameAccess > xStyles;
270 0 : return xStyles;
271 : }
272 :
273 : // XModel
274 0 : sal_Bool SAL_CALL SvxSimpleUnoModel::attachResource( const OUString& aURL, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aArgs ) throw (::com::sun::star::uno::RuntimeException, std::exception)
275 : {
276 : (void)aURL;
277 : (void)aArgs;
278 0 : return sal_False;
279 : }
280 :
281 0 : OUString SAL_CALL SvxSimpleUnoModel::getURL( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
282 : {
283 0 : OUString aStr;
284 0 : return aStr;
285 : }
286 :
287 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > SAL_CALL SvxSimpleUnoModel::getArgs( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
288 : {
289 0 : Sequence< beans::PropertyValue > aSeq;
290 0 : return aSeq;
291 : }
292 :
293 0 : void SAL_CALL SvxSimpleUnoModel::connectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
294 : {
295 0 : }
296 :
297 0 : void SAL_CALL SvxSimpleUnoModel::disconnectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
298 : {
299 0 : }
300 :
301 0 : void SAL_CALL SvxSimpleUnoModel::lockControllers( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
302 : {
303 0 : }
304 :
305 0 : void SAL_CALL SvxSimpleUnoModel::unlockControllers( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
306 : {
307 0 : }
308 :
309 0 : sal_Bool SAL_CALL SvxSimpleUnoModel::hasControllersLocked( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
310 : {
311 0 : return sal_True;
312 : }
313 :
314 0 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController > SAL_CALL SvxSimpleUnoModel::getCurrentController( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
315 : {
316 0 : uno::Reference< frame::XController > xRet;
317 0 : return xRet;
318 : }
319 :
320 0 : void SAL_CALL SvxSimpleUnoModel::setCurrentController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException, std::exception)
321 : {
322 0 : }
323 :
324 0 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL SvxSimpleUnoModel::getCurrentSelection( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
325 : {
326 0 : uno::Reference< XInterface > xRet;
327 0 : return xRet;
328 : }
329 :
330 :
331 : // XComponent
332 0 : void SAL_CALL SvxSimpleUnoModel::dispose( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
333 : {
334 0 : }
335 :
336 0 : void SAL_CALL SvxSimpleUnoModel::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
337 : {
338 0 : }
339 :
340 0 : void SAL_CALL SvxSimpleUnoModel::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& ) throw (::com::sun::star::uno::RuntimeException, std::exception)
341 : {
342 0 : }
343 :
344 :
345 :
346 : class SvxXMLTextExportComponent : public SvXMLExport
347 : {
348 : public:
349 : SvxXMLTextExportComponent(
350 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > xContext,
351 : EditEngine* pEditEngine,
352 : const ESelection& rSel,
353 : const OUString& rFileName,
354 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler > & xHandler );
355 :
356 : virtual ~SvxXMLTextExportComponent();
357 :
358 : // methods without content:
359 : virtual void _ExportAutoStyles() SAL_OVERRIDE;
360 : virtual void _ExportMasterStyles() SAL_OVERRIDE;
361 : virtual void _ExportContent() SAL_OVERRIDE;
362 :
363 : private:
364 : com::sun::star::uno::Reference< com::sun::star::text::XText > mxText;
365 : ESelection maSelection;
366 : };
367 :
368 :
369 :
370 0 : SvxXMLTextExportComponent::SvxXMLTextExportComponent(
371 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > xContext,
372 : EditEngine* pEditEngine,
373 : const ESelection& rSel,
374 : const OUString& rFileName,
375 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler > & xHandler)
376 0 : : SvXMLExport( xContext, "", rFileName, xHandler, ((frame::XModel*)new SvxSimpleUnoModel()), MAP_CM ),
377 0 : maSelection( rSel )
378 : {
379 0 : SvxEditEngineSource aEditSource( pEditEngine );
380 :
381 : static const SfxItemPropertyMapEntry SvxXMLTextExportComponentPropertyMap[] =
382 : {
383 0 : SVX_UNOEDIT_CHAR_PROPERTIES,
384 0 : SVX_UNOEDIT_FONT_PROPERTIES,
385 : // SVX_UNOEDIT_OUTLINER_PROPERTIES,
386 0 : {OUString(UNO_NAME_NUMBERING_RULES), EE_PARA_NUMBULLET, ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexReplace>*)0), 0, 0 },
387 0 : {OUString(UNO_NAME_NUMBERING), EE_PARA_BULLETSTATE,::getBooleanCppuType(), 0, 0 },
388 0 : {OUString(UNO_NAME_NUMBERING_LEVEL), EE_PARA_OUTLLEVEL, ::getCppuType((const sal_Int16*)0), 0, 0 },
389 0 : SVX_UNOEDIT_PARA_PROPERTIES,
390 : { OUString(), 0, css::uno::Type(), 0, 0 }
391 0 : };
392 0 : static SvxItemPropertySet aSvxXMLTextExportComponentPropertySet( SvxXMLTextExportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
393 :
394 0 : SvxUnoText* pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextExportComponentPropertySet, mxText );
395 0 : pUnoText->SetSelection( rSel );
396 0 : mxText = pUnoText;
397 :
398 0 : setExportFlags( EXPORT_AUTOSTYLES|EXPORT_CONTENT );
399 0 : }
400 :
401 0 : SvxXMLTextExportComponent::~SvxXMLTextExportComponent()
402 : {
403 0 : }
404 :
405 0 : void SvxWriteXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
406 : {
407 : try
408 : {
409 : do
410 : {
411 : // create service factory
412 0 : uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
413 :
414 : // create document handler
415 0 : uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
416 :
417 : // create output stream and active data source
418 0 : uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( rStream ) );
419 :
420 : /* testcode
421 : const OUString aURL( "file:///e:/test.xml" );
422 : SfxMedium aMedium( aURL, STREAM_WRITE | STREAM_TRUNC, sal_True );
423 : aMedium.IsRemote();
424 : uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
425 : */
426 :
427 :
428 0 : xWriter->setOutputStream( xOut );
429 :
430 : // export text
431 0 : const OUString aName;
432 :
433 : // SvxXMLTextExportComponent aExporter( &rEditEngine, rSel, aName, xHandler );
434 0 : uno::Reference< xml::sax::XDocumentHandler > xHandler(xWriter, UNO_QUERY_THROW);
435 0 : SvxXMLTextExportComponent aExporter( xContext, &rEditEngine, rSel, aName, xHandler );
436 :
437 0 : aExporter.exportDoc();
438 :
439 : /* testcode
440 : aMedium.Commit();
441 : */
442 :
443 : }
444 : while( false );
445 : }
446 0 : catch( const uno::Exception& )
447 : {
448 : OSL_FAIL("exception during xml export");
449 : }
450 0 : }
451 :
452 : // methods without content:
453 0 : void SvxXMLTextExportComponent::_ExportAutoStyles()
454 : {
455 0 : UniReference< XMLTextParagraphExport > xTextExport( GetTextParagraphExport() );
456 :
457 0 : xTextExport->collectTextAutoStyles( mxText );
458 0 : xTextExport->exportTextAutoStyles();
459 0 : }
460 :
461 0 : void SvxXMLTextExportComponent::_ExportContent()
462 : {
463 0 : UniReference< XMLTextParagraphExport > xTextExport( GetTextParagraphExport() );
464 :
465 0 : xTextExport->exportText( mxText );
466 0 : }
467 :
468 0 : void SvxXMLTextExportComponent::_ExportMasterStyles() {}
469 :
470 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|