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