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