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