1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
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
;
61 ///////////////////////////////////////////////////////////////////////
63 class SvxEditEngineSourceImpl
;
65 ///////////////////////////////////////////////////////////////////////
67 class SvxEditEngineSourceImpl
70 oslInterlockedCount maRefCount
;
72 EditEngine
* mpEditEngine
;
73 SvxTextForwarder
* mpTextForwarder
;
75 ~SvxEditEngineSourceImpl();
78 SvxEditEngineSourceImpl( EditEngine
* pEditEngine
);
80 void SAL_CALL
acquire();
81 void SAL_CALL
release();
83 SvxTextForwarder
* GetTextForwarder();
86 ///////////////////////////////////////////////////////////////////////
89 //------------------------------------------------------------------------
91 SvxEditEngineSourceImpl::SvxEditEngineSourceImpl( EditEngine
* pEditEngine
)
93 mpEditEngine( pEditEngine
),
98 //------------------------------------------------------------------------
100 SvxEditEngineSourceImpl::~SvxEditEngineSourceImpl()
102 delete mpTextForwarder
;
105 //------------------------------------------------------------------------
107 void SAL_CALL
SvxEditEngineSourceImpl::acquire()
109 osl_atomic_increment( &maRefCount
);
112 //------------------------------------------------------------------------
114 void SAL_CALL
SvxEditEngineSourceImpl::release()
116 if( ! osl_atomic_decrement( &maRefCount
) )
120 //------------------------------------------------------------------------
122 SvxTextForwarder
* SvxEditEngineSourceImpl::GetTextForwarder()
124 if (!mpTextForwarder
)
125 mpTextForwarder
= new SvxEditEngineForwarder( *mpEditEngine
);
127 return mpTextForwarder
;
130 // --------------------------------------------------------------------
132 // --------------------------------------------------------------------
134 SvxEditEngineSource::SvxEditEngineSource( EditEngine
* pEditEngine
)
136 mpImpl
= new SvxEditEngineSourceImpl( pEditEngine
);
140 // --------------------------------------------------------------------
142 SvxEditEngineSource::SvxEditEngineSource( SvxEditEngineSourceImpl
* pImpl
)
148 //------------------------------------------------------------------------
150 SvxEditEngineSource::~SvxEditEngineSource()
155 //------------------------------------------------------------------------
157 SvxEditSource
* SvxEditEngineSource::Clone() const
159 return new SvxEditEngineSource( mpImpl
);
162 //------------------------------------------------------------------------
164 SvxTextForwarder
* SvxEditEngineSource::GetTextForwarder()
166 return mpImpl
->GetTextForwarder();
169 //------------------------------------------------------------------------
171 void SvxEditEngineSource::UpdateData()
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
>
183 virtual ~SvxSimpleUnoModel();
186 // XMultiServiceFactory
187 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
);
188 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
);
189 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
getAvailableServiceNames( ) throw(::com::sun::star::uno::RuntimeException
);
191 // XStyleFamiliesSupplier
192 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
getStyleFamilies( ) throw(::com::sun::star::uno::RuntimeException
);
194 // XAnyCompareFactory
195 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XAnyCompare
> SAL_CALL
createAnyCompareByName( const OUString
& PropertyName
) throw(::com::sun::star::uno::RuntimeException
);
198 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
);
199 virtual 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
);
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
);
217 SvxSimpleUnoModel::SvxSimpleUnoModel()
221 SvxSimpleUnoModel::~SvxSimpleUnoModel()
225 // XMultiServiceFactory ( SvxFmMSFactory )
226 uno::Reference
< uno::XInterface
> SAL_CALL
SvxSimpleUnoModel::createInstance( const OUString
& aServiceSpecifier
)
227 throw(uno::Exception
, uno::RuntimeException
)
229 if( 0 == aServiceSpecifier
.reverseCompareToAsciiL( RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.NumberingRules" ) ) )
231 return uno::Reference
< uno::XInterface
>(
232 SvxCreateNumRule(), uno::UNO_QUERY
);
234 if ( (0 == aServiceSpecifier
.reverseCompareToAsciiL(
235 RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.textfield.DateTime")))
236 || (0 == aServiceSpecifier
.reverseCompareToAsciiL(
237 RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.TextField.DateTime")))
240 return (::cppu::OWeakObject
* )new SvxUnoTextField( text::textfield::Type::DATE
);
243 return SvxUnoTextCreateTextField( aServiceSpecifier
);
247 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
)
249 return createInstance( ServiceSpecifier
);
252 Sequence
< OUString
> SAL_CALL
SvxSimpleUnoModel::getAvailableServiceNames( ) throw(::com::sun::star::uno::RuntimeException
)
254 Sequence
< OUString
> aSeq
;
258 // XAnyCompareFactory
259 uno::Reference
< com::sun::star::ucb::XAnyCompare
> SAL_CALL
SvxSimpleUnoModel::createAnyCompareByName( const OUString
& PropertyName
)
260 throw(uno::RuntimeException
)
263 return SvxCreateNumRuleCompare();
266 // XStyleFamiliesSupplier
267 uno::Reference
< container::XNameAccess
> SAL_CALL
SvxSimpleUnoModel::getStyleFamilies( )
268 throw(uno::RuntimeException
)
270 uno::Reference
< container::XNameAccess
> xStyles
;
275 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
)
282 OUString SAL_CALL
SvxSimpleUnoModel::getURL( ) throw (::com::sun::star::uno::RuntimeException
)
288 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> SAL_CALL
SvxSimpleUnoModel::getArgs( ) throw (::com::sun::star::uno::RuntimeException
)
290 Sequence
< beans::PropertyValue
> aSeq
;
294 void SAL_CALL
SvxSimpleUnoModel::connectController( const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XController
>& ) throw (::com::sun::star::uno::RuntimeException
)
298 void SAL_CALL
SvxSimpleUnoModel::disconnectController( const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XController
>& ) throw (::com::sun::star::uno::RuntimeException
)
302 void SAL_CALL
SvxSimpleUnoModel::lockControllers( ) throw (::com::sun::star::uno::RuntimeException
)
306 void SAL_CALL
SvxSimpleUnoModel::unlockControllers( ) throw (::com::sun::star::uno::RuntimeException
)
310 sal_Bool SAL_CALL
SvxSimpleUnoModel::hasControllersLocked( ) throw (::com::sun::star::uno::RuntimeException
)
315 ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XController
> SAL_CALL
SvxSimpleUnoModel::getCurrentController( ) throw (::com::sun::star::uno::RuntimeException
)
317 uno::Reference
< frame::XController
> xRet
;
321 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
)
325 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> SAL_CALL
SvxSimpleUnoModel::getCurrentSelection( ) throw (::com::sun::star::uno::RuntimeException
)
327 uno::Reference
< XInterface
> xRet
;
333 void SAL_CALL
SvxSimpleUnoModel::dispose( ) throw (::com::sun::star::uno::RuntimeException
)
337 void SAL_CALL
SvxSimpleUnoModel::addEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& ) throw (::com::sun::star::uno::RuntimeException
)
341 void SAL_CALL
SvxSimpleUnoModel::removeEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& ) throw (::com::sun::star::uno::RuntimeException
)
345 ///////////////////////////////////////////////////////////////////////
347 class SvxXMLTextExportComponent
: public SvXMLExport
350 SvxXMLTextExportComponent(
351 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> xContext
,
352 EditEngine
* pEditEngine
,
353 const ESelection
& rSel
,
354 const OUString
& rFileName
,
355 const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XDocumentHandler
> & xHandler
);
357 ~SvxXMLTextExportComponent();
359 // methods without content:
360 virtual void _ExportAutoStyles();
361 virtual void _ExportMasterStyles();
362 virtual void _ExportContent();
365 com::sun::star::uno::Reference
< com::sun::star::text::XText
> mxText
;
366 ESelection maSelection
;
369 ///////////////////////////////////////////////////////////////////////
371 SvxXMLTextExportComponent::SvxXMLTextExportComponent(
372 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> xContext
,
373 EditEngine
* pEditEngine
,
374 const ESelection
& rSel
,
375 const OUString
& rFileName
,
376 const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XDocumentHandler
> & xHandler
)
377 : SvXMLExport( xContext
, rFileName
, xHandler
, ((frame::XModel
*)new SvxSimpleUnoModel()), MAP_CM
),
380 SvxEditEngineSource
aEditSource( pEditEngine
);
382 static const SfxItemPropertyMapEntry SvxXMLTextExportComponentPropertyMap
[] =
384 SVX_UNOEDIT_CHAR_PROPERTIES
,
385 SVX_UNOEDIT_FONT_PROPERTIES
,
386 // SVX_UNOEDIT_OUTLINER_PROPERTIES,
387 {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 {MAP_CHAR_LEN(UNO_NAME_NUMBERING
), EE_PARA_BULLETSTATE
,&::getBooleanCppuType(), 0, 0 },
389 {MAP_CHAR_LEN(UNO_NAME_NUMBERING_LEVEL
), EE_PARA_OUTLLEVEL
, &::getCppuType((const sal_Int16
*)0), 0, 0 },
390 SVX_UNOEDIT_PARA_PROPERTIES
,
393 static SvxItemPropertySet
aSvxXMLTextExportComponentPropertySet( SvxXMLTextExportComponentPropertyMap
, EditEngine::GetGlobalItemPool() );
395 SvxUnoText
* pUnoText
= new SvxUnoText( &aEditSource
, &aSvxXMLTextExportComponentPropertySet
, mxText
);
396 pUnoText
->SetSelection( rSel
);
399 setExportFlags( EXPORT_AUTOSTYLES
|EXPORT_CONTENT
);
402 SvxXMLTextExportComponent::~SvxXMLTextExportComponent()
406 void SvxWriteXML( EditEngine
& rEditEngine
, SvStream
& rStream
, const ESelection
& rSel
)
412 // create service factory
413 uno::Reference
<uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
415 // create document handler
416 uno::Reference
< xml::sax::XWriter
> xWriter
= xml::sax::Writer::create( xContext
);
418 // create output stream and active data source
419 uno::Reference
<io::XOutputStream
> xOut( new utl::OOutputStreamWrapper( rStream
) );
422 const OUString aURL( "file:///e:/test.xml" );
423 SfxMedium aMedium( aURL, STREAM_WRITE | STREAM_TRUNC, sal_True );
425 uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
429 xWriter
->setOutputStream( xOut
);
432 const OUString aName
;
434 // SvxXMLTextExportComponent aExporter( &rEditEngine, rSel, aName, xHandler );
435 uno::Reference
< xml::sax::XDocumentHandler
> xHandler(xWriter
, UNO_QUERY_THROW
);
436 SvxXMLTextExportComponent
aExporter( xContext
, &rEditEngine
, rSel
, aName
, xHandler
);
438 aExporter
.exportDoc();
447 catch( const uno::Exception
& )
449 OSL_FAIL("exception during xml export");
453 // methods without content:
454 void SvxXMLTextExportComponent::_ExportAutoStyles()
456 UniReference
< XMLTextParagraphExport
> xTextExport( GetTextParagraphExport() );
458 xTextExport
->collectTextAutoStyles( mxText
);
459 xTextExport
->exportTextAutoStyles();
462 void SvxXMLTextExportComponent::_ExportContent()
464 UniReference
< XMLTextParagraphExport
> xTextExport( GetTextParagraphExport() );
466 xTextExport
->exportText( mxText
);
469 void SvxXMLTextExportComponent::_ExportMasterStyles() {}
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */