xref: /aoo42x/main/editeng/source/xml/xmltxtexp.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_editeng.hxx"
30 
31 /** this file implements an export of a selected EditEngine content into
32 	a xml stream. See editeng/source/inc/xmledit.hxx for interface */
33 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
34 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/io/XActiveDataSource.hpp>
37 #include <tools/debug.hxx>
38 #include <svl/itemprop.hxx>
39 #include <svl/brdcst.hxx>
40 #include <com/sun/star/uno/Sequence.hxx>
41 #include <sot/storage.hxx>
42 #include <rtl/ustrbuf.hxx>
43 #include <xmloff/xmluconv.hxx>
44 #include <xmloff/xmlnmspe.hxx>
45 #include <xmloff/nmspmap.hxx>
46 #include <xmloff/xmlmetae.hxx>
47 #include <cppuhelper/implbase4.hxx>
48 #include <comphelper/processfactory.hxx>
49 #include <unotools/streamwrap.hxx>
50 #include <xmloff/xmlexp.hxx>
51 #include <editeng/unoedsrc.hxx>
52 #include <editeng/unofored.hxx>
53 #include <editeng/unotext.hxx>
54 #include <editeng/unoprnms.hxx>
55 #include <editeng/unofield.hxx>
56 #include <editeng/editeng.hxx>
57 #include "editsource.hxx"
58 #include <editeng/unonrule.hxx>
59 #include <editeng/unoipset.hxx>
60 
61 using namespace com::sun::star;
62 using namespace com::sun::star::container;
63 using namespace com::sun::star::document;
64 using namespace com::sun::star::uno;
65 using namespace com::sun::star::awt;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::xml::sax;
68 using namespace ::rtl;
69 using namespace cppu;
70 
71 ///////////////////////////////////////////////////////////////////////
72 
73 class SvxEditEngineSourceImpl;
74 
75 ///////////////////////////////////////////////////////////////////////
76 
77 class SvxEditEngineSourceImpl
78 {
79 private:
80 	oslInterlockedCount	maRefCount;
81 
82 	EditEngine*				mpEditEngine;
83 	SvxTextForwarder*		mpTextForwarder;
84 
85 	~SvxEditEngineSourceImpl();
86 
87 public:
88 	SvxEditEngineSourceImpl( EditEngine* pEditEngine );
89 
90 	void SAL_CALL acquire();
91 	void SAL_CALL release();
92 
93 	SvxTextForwarder*		GetTextForwarder();
94 };
95 
96 ///////////////////////////////////////////////////////////////////////
97 
98 
99 //------------------------------------------------------------------------
100 
101 SvxEditEngineSourceImpl::SvxEditEngineSourceImpl( EditEngine* pEditEngine )
102 : maRefCount(0),
103   mpEditEngine( pEditEngine ),
104   mpTextForwarder(NULL)
105 {
106 }
107 
108 //------------------------------------------------------------------------
109 
110 SvxEditEngineSourceImpl::~SvxEditEngineSourceImpl()
111 {
112 	delete mpTextForwarder;
113 }
114 
115 //------------------------------------------------------------------------
116 
117 void SAL_CALL SvxEditEngineSourceImpl::acquire()
118 {
119 	osl_incrementInterlockedCount( &maRefCount );
120 }
121 
122 //------------------------------------------------------------------------
123 
124 void SAL_CALL SvxEditEngineSourceImpl::release()
125 {
126 	if( ! osl_decrementInterlockedCount( &maRefCount ) )
127 		delete this;
128 }
129 
130 //------------------------------------------------------------------------
131 
132 SvxTextForwarder* SvxEditEngineSourceImpl::GetTextForwarder()
133 {
134 	if (!mpTextForwarder)
135 		mpTextForwarder = new SvxEditEngineForwarder( *mpEditEngine );
136 
137 	return mpTextForwarder;
138 }
139 
140 // --------------------------------------------------------------------
141 // SvxTextEditSource
142 // --------------------------------------------------------------------
143 
144 SvxEditEngineSource::SvxEditEngineSource( EditEngine* pEditEngine )
145 {
146 	mpImpl = new SvxEditEngineSourceImpl( pEditEngine );
147 	mpImpl->acquire();
148 }
149 
150 // --------------------------------------------------------------------
151 
152 SvxEditEngineSource::SvxEditEngineSource( SvxEditEngineSourceImpl* pImpl )
153 {
154 	mpImpl = pImpl;
155 	mpImpl->acquire();
156 }
157 
158 //------------------------------------------------------------------------
159 
160 SvxEditEngineSource::~SvxEditEngineSource()
161 {
162 	mpImpl->release();
163 }
164 
165 //------------------------------------------------------------------------
166 
167 SvxEditSource* SvxEditEngineSource::Clone() const
168 {
169 	return new SvxEditEngineSource( mpImpl );
170 }
171 
172 //------------------------------------------------------------------------
173 
174 SvxTextForwarder* SvxEditEngineSource::GetTextForwarder()
175 {
176     return mpImpl->GetTextForwarder();
177 }
178 
179 //------------------------------------------------------------------------
180 
181 void SvxEditEngineSource::UpdateData()
182 {
183 }
184 
185 class SvxSimpleUnoModel : public cppu::WeakAggImplHelper4<
186 									::com::sun::star::frame::XModel,
187 									::com::sun::star::ucb::XAnyCompareFactory,
188 									::com::sun::star::style::XStyleFamiliesSupplier,
189 									::com::sun::star::lang::XMultiServiceFactory >
190 {
191 public:
192 	SvxSimpleUnoModel();
193 	virtual ~SvxSimpleUnoModel();
194 
195 
196 	// XMultiServiceFactory
197     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);
198     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);
199     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getAvailableServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
200 
201 	// XStyleFamiliesSupplier
202     virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getStyleFamilies(  ) throw(::com::sun::star::uno::RuntimeException);
203 
204 	// XAnyCompareFactory
205     virtual ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XAnyCompare > SAL_CALL createAnyCompareByName( const ::rtl::OUString& PropertyName ) throw(::com::sun::star::uno::RuntimeException);
206 
207     // XModel
208     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);
209     virtual ::rtl::OUString SAL_CALL getURL(  ) throw (::com::sun::star::uno::RuntimeException);
210     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > SAL_CALL getArgs(  ) throw (::com::sun::star::uno::RuntimeException);
211     virtual void SAL_CALL connectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& xController ) throw (::com::sun::star::uno::RuntimeException);
212     virtual void SAL_CALL disconnectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& xController ) throw (::com::sun::star::uno::RuntimeException);
213     virtual void SAL_CALL lockControllers(  ) throw (::com::sun::star::uno::RuntimeException);
214     virtual void SAL_CALL unlockControllers(  ) throw (::com::sun::star::uno::RuntimeException);
215     virtual sal_Bool SAL_CALL hasControllersLocked(  ) throw (::com::sun::star::uno::RuntimeException);
216     virtual ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController > SAL_CALL getCurrentController(  ) throw (::com::sun::star::uno::RuntimeException);
217     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);
218     virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getCurrentSelection(  ) throw (::com::sun::star::uno::RuntimeException);
219 
220     // XComponent
221     virtual void SAL_CALL dispose(  ) throw (::com::sun::star::uno::RuntimeException);
222     virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException);
223     virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException);
224 
225 };
226 
227 SvxSimpleUnoModel::SvxSimpleUnoModel()
228 {
229 }
230 
231 SvxSimpleUnoModel::~SvxSimpleUnoModel()
232 {
233 }
234 
235 // XMultiServiceFactory ( SvxFmMSFactory )
236 uno::Reference< uno::XInterface > SAL_CALL SvxSimpleUnoModel::createInstance( const OUString& aServiceSpecifier )
237 	throw(uno::Exception, uno::RuntimeException)
238 {
239 	if( 0 == aServiceSpecifier.reverseCompareToAsciiL( RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.NumberingRules" ) ) )
240 	{
241 		return uno::Reference< uno::XInterface >(
242             SvxCreateNumRule(), uno::UNO_QUERY );
243 	}
244     if (   (0 == aServiceSpecifier.reverseCompareToAsciiL(
245             RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.textfield.DateTime")))
246         || (0 == aServiceSpecifier.reverseCompareToAsciiL(
247             RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.TextField.DateTime")))
248        )
249 	{
250 		return (::cppu::OWeakObject * )new SvxUnoTextField( ID_EXT_DATEFIELD );
251 	}
252 
253 	return SvxUnoTextCreateTextField( aServiceSpecifier );
254 
255 }
256 
257 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)
258 {
259 	return createInstance( ServiceSpecifier );
260 }
261 
262 Sequence< ::rtl::OUString > SAL_CALL SvxSimpleUnoModel::getAvailableServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
263 {
264 	Sequence< OUString > aSeq;
265 	return aSeq;
266 }
267 
268 // XAnyCompareFactory
269 uno::Reference< com::sun::star::ucb::XAnyCompare > SAL_CALL SvxSimpleUnoModel::createAnyCompareByName( const OUString& PropertyName )
270 	throw(uno::RuntimeException)
271 {
272 	(void)PropertyName;
273 	return SvxCreateNumRuleCompare();
274 }
275 
276 // XStyleFamiliesSupplier
277 uno::Reference< container::XNameAccess > SAL_CALL SvxSimpleUnoModel::getStyleFamilies(  )
278 	throw(uno::RuntimeException)
279 {
280 	uno::Reference< container::XNameAccess > xStyles;
281 	return xStyles;
282 }
283 
284 // XModel
285 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)
286 {
287 	(void)aURL;
288 	(void)aArgs;
289 	return sal_False;
290 }
291 
292 ::rtl::OUString SAL_CALL SvxSimpleUnoModel::getURL(  ) throw (::com::sun::star::uno::RuntimeException)
293 {
294 	OUString aStr;
295 	return aStr;
296 }
297 
298 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > SAL_CALL SvxSimpleUnoModel::getArgs(  ) throw (::com::sun::star::uno::RuntimeException)
299 {
300 	Sequence< beans::PropertyValue > aSeq;
301 	return aSeq;
302 }
303 
304 void SAL_CALL SvxSimpleUnoModel::connectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& ) throw (::com::sun::star::uno::RuntimeException)
305 {
306 }
307 
308 void SAL_CALL SvxSimpleUnoModel::disconnectController( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& ) throw (::com::sun::star::uno::RuntimeException)
309 {
310 }
311 
312 void SAL_CALL SvxSimpleUnoModel::lockControllers(  ) throw (::com::sun::star::uno::RuntimeException)
313 {
314 }
315 
316 void SAL_CALL SvxSimpleUnoModel::unlockControllers(  ) throw (::com::sun::star::uno::RuntimeException)
317 {
318 }
319 
320 sal_Bool SAL_CALL SvxSimpleUnoModel::hasControllersLocked(  ) throw (::com::sun::star::uno::RuntimeException)
321 {
322 	return sal_True;
323 }
324 
325 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController > SAL_CALL SvxSimpleUnoModel::getCurrentController(  ) throw (::com::sun::star::uno::RuntimeException)
326 {
327 	uno::Reference< frame::XController > xRet;
328 	return xRet;
329 }
330 
331 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)
332 {
333 }
334 
335 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL SvxSimpleUnoModel::getCurrentSelection(  ) throw (::com::sun::star::uno::RuntimeException)
336 {
337 	uno::Reference< XInterface > xRet;
338 	return xRet;
339 }
340 
341 
342 // XComponent
343 void SAL_CALL SvxSimpleUnoModel::dispose(  ) throw (::com::sun::star::uno::RuntimeException)
344 {
345 }
346 
347 void SAL_CALL SvxSimpleUnoModel::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& ) throw (::com::sun::star::uno::RuntimeException)
348 {
349 }
350 
351 void SAL_CALL SvxSimpleUnoModel::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& ) throw (::com::sun::star::uno::RuntimeException)
352 {
353 }
354 
355 ///////////////////////////////////////////////////////////////////////
356 
357 class SvxXMLTextExportComponent : public SvXMLExport
358 {
359 public:
360 	// #110680#
361 	SvxXMLTextExportComponent(
362 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
363 		EditEngine* pEditEngine,
364 		const ESelection& rSel,
365 		const ::rtl::OUString& rFileName,
366 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler > & xHandler );
367 
368 	~SvxXMLTextExportComponent();
369 
370 	// methods without content:
371 	virtual void _ExportAutoStyles();
372 	virtual void _ExportMasterStyles();
373 	virtual void _ExportContent();
374 
375 private:
376 	com::sun::star::uno::Reference< com::sun::star::text::XText > mxText;
377 	EditEngine* mpEditEngine;
378 	ESelection maSelection;
379 };
380 
381 ///////////////////////////////////////////////////////////////////////
382 
383 // #110680#
384 SvxXMLTextExportComponent::SvxXMLTextExportComponent(
385 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
386 	EditEngine* pEditEngine,
387 	const ESelection& rSel,
388 	const ::rtl::OUString& rFileName,
389 	const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler > & xHandler)
390 :	SvXMLExport( xServiceFactory, rFileName, xHandler, ((frame::XModel*)new SvxSimpleUnoModel()), MAP_CM ),
391 	mpEditEngine( pEditEngine ),
392 	maSelection( rSel )
393 {
394 	SvxEditEngineSource aEditSource( pEditEngine );
395 
396     static const SfxItemPropertyMapEntry SvxXMLTextExportComponentPropertyMap[] =
397 	{
398 		SVX_UNOEDIT_CHAR_PROPERTIES,
399 		SVX_UNOEDIT_FONT_PROPERTIES,
400 //		SVX_UNOEDIT_OUTLINER_PROPERTIES,
401 		{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 },
402 		{MAP_CHAR_LEN(UNO_NAME_NUMBERING),				EE_PARA_BULLETSTATE,&::getBooleanCppuType(), 0, 0 },
403 		{MAP_CHAR_LEN(UNO_NAME_NUMBERING_LEVEL),		EE_PARA_OUTLLEVEL,	&::getCppuType((const sal_Int16*)0), 0, 0 },
404 		SVX_UNOEDIT_PARA_PROPERTIES,
405 		{0,0,0,0,0,0}
406 	};
407 	static SvxItemPropertySet aSvxXMLTextExportComponentPropertySet( SvxXMLTextExportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
408 
409 	SvxUnoText* pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextExportComponentPropertySet, mxText );
410 	pUnoText->SetSelection( rSel );
411 	mxText = pUnoText;
412 
413 	setExportFlags( EXPORT_AUTOSTYLES|EXPORT_CONTENT );
414 }
415 
416 SvxXMLTextExportComponent::~SvxXMLTextExportComponent()
417 {
418 }
419 
420 void SvxWriteXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
421 {
422 	try
423 	{
424 		do
425 		{
426 			// create service factory
427 
428 			uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
429 
430 			if( !xServiceFactory.is() )
431 			{
432 				DBG_ERROR( "got no service manager" );
433 				break;
434 			}
435 
436 			// create document handler
437 
438 			uno::Reference< uno::XInterface > xWriter( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ) );
439 
440 			if( !xWriter.is() )
441 			{
442 				DBG_ERROR( "com.sun.star.xml.sax.Writer service missing" );
443 				break;
444 			}
445 
446 			uno::Reference<xml::sax::XDocumentHandler>	xHandler( xWriter, uno::UNO_QUERY );
447 
448 			// create output stream and active data source
449 			uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( rStream ) );
450 
451 /* testcode
452 			const OUString aURL( RTL_CONSTASCII_USTRINGPARAM( "file:///e:/test.xml" ) );
453 			SfxMedium aMedium( aURL, STREAM_WRITE | STREAM_TRUNC, sal_True );
454 			aMedium.IsRemote();
455 			uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
456 */
457 
458 
459 			uno::Reference<io::XActiveDataSource> xMetaSrc( xWriter, uno::UNO_QUERY );
460 			xMetaSrc->setOutputStream( xOut );
461 
462 			// export text
463 			const OUString aName;
464 
465 			// #110680#
466 			// SvxXMLTextExportComponent aExporter( &rEditEngine, rSel, aName, xHandler );
467 			SvxXMLTextExportComponent aExporter( xServiceFactory, &rEditEngine, rSel, aName, xHandler );
468 
469 			aExporter.exportDoc();
470 
471 /* testcode
472 			aMedium.Commit();
473 */
474 
475 		}
476 		while( 0 );
477 	}
478 	catch( uno::Exception& )
479 	{
480 		DBG_ERROR("exception during xml export");
481 	}
482 }
483 
484 // methods without content:
485 void SvxXMLTextExportComponent::_ExportAutoStyles()
486 {
487 	UniReference< XMLTextParagraphExport > xTextExport( GetTextParagraphExport() );
488 
489 	xTextExport->collectTextAutoStyles( mxText );
490 	xTextExport->exportTextAutoStyles();
491 }
492 
493 void SvxXMLTextExportComponent::_ExportContent()
494 {
495 	UniReference< XMLTextParagraphExport > xTextExport( GetTextParagraphExport() );
496 
497 	xTextExport->exportText( mxText );
498 }
499 
500 void SvxXMLTextExportComponent::_ExportMasterStyles() {}
501