Update ooo320-m1
[ooovba.git] / sw / source / core / swg / SwXMLTextBlocks1.cxx
blob0c329ee5b3d0c7ad8a77cca42fc5501a4a4d2346
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SwXMLTextBlocks1.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #include <com/sun/star/embed/ElementModes.hpp>
34 #include <com/sun/star/embed/XTransactedObject.hpp>
35 #include <svtools/macitem.hxx>
36 #include <svtools/unoevent.hxx>
37 #include <sfx2/docfile.hxx>
38 #include <unotools/streamwrap.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <com/sun/star/xml/sax/InputSource.hpp>
41 #include <com/sun/star/io/XActiveDataSource.hpp>
42 #include <com/sun/star/xml/sax/XParser.hpp>
43 #include <com/sun/star/document/XStorageBasedDocument.hpp>
44 #include <doc.hxx>
45 #ifndef _DOCSH_HXX
46 #include <docsh.hxx>
47 #endif
48 #include <shellio.hxx>
49 #include <SwXMLTextBlocks.hxx>
50 #include <SwXMLBlockImport.hxx>
51 #include <SwXMLBlockExport.hxx>
52 #include <swevent.hxx>
53 #include <swerror.h>
54 #include <errhdl.hxx>
57 #define STREAM_STGREAD ( STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE )
58 #define STREAM_STGWRITE ( STREAM_READ | STREAM_WRITE | STREAM_SHARE_DENYWRITE )
60 sal_Char __FAR_DATA XMLN_BLOCKLIST[] = "BlockList.xml";
62 using namespace ::com::sun::star;
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::container;
65 using ::rtl::OUString;
67 using ::xmloff::token::XML_BLOCK_LIST;
68 using ::xmloff::token::XML_UNFORMATTED_TEXT;
69 using ::xmloff::token::GetXMLToken;
71 ULONG SwXMLTextBlocks::GetDoc( USHORT nIdx )
73 String aFolderName ( GetPackageName ( nIdx ) );
75 if (!IsOnlyTextBlock ( nIdx ) )
77 try
79 xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
80 xMedium = new SfxMedium(xRoot, GetBaseURL());
81 SwReader aReader(*xMedium,aFolderName, pDoc );
82 ReadXML->SetBlockMode( sal_True );
83 aReader.Read( *ReadXML );
84 ReadXML->SetBlockMode( sal_False );
85 // Ole objects fails to display when inserted into document
86 // because the ObjectReplacement folder ( and contents are missing )
87 rtl::OUString sObjReplacements( RTL_CONSTASCII_USTRINGPARAM( "ObjectReplacements" ) );
88 if ( xRoot->hasByName( sObjReplacements ) )
90 uno::Reference< document::XStorageBasedDocument > xDocStor( pDoc->GetDocShell()->GetModel(), uno::UNO_QUERY_THROW );
91 uno::Reference< embed::XStorage > xStr( xDocStor->getDocumentStorage() );
92 if ( xStr.is() )
94 xRoot->copyElementTo( sObjReplacements, xStr, sObjReplacements );
95 uno::Reference< embed::XTransactedObject > xTrans( xStr, uno::UNO_QUERY );
96 if ( xTrans.is() )
97 xTrans->commit();
101 catch( uno::Exception& )
105 xRoot = 0;
107 else
109 String aStreamName = aFolderName + (OUString) String::CreateFromAscii(".xml");
112 xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
113 uno::Reference < io::XStream > xStream = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
115 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
116 comphelper::getProcessServiceFactory();
117 ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
118 if( !xServiceFactory.is() )
120 // Throw an exception ?
123 xml::sax::InputSource aParserInput;
124 aParserInput.sSystemId = aNames [ nIdx ] ->aPackageName;
126 aParserInput.aInputStream = xStream->getInputStream();
128 // get parser
129 uno::Reference< XInterface > xXMLParser = xServiceFactory->createInstance(
130 OUString::createFromAscii("com.sun.star.xml.sax.Parser") );
131 ASSERT( xXMLParser.is(),
132 "XMLReader::Read: com.sun.star.xml.sax.Parser service missing" );
133 if( !xXMLParser.is() )
135 // Maybe throw an exception?
138 // get filter
139 // #110680#
140 // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, aCur, sal_True );
141 uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xServiceFactory, *this, aCur, sal_True );
143 // connect parser and filter
144 uno::Reference< xml::sax::XParser > xParser( xXMLParser, UNO_QUERY );
145 xParser->setDocumentHandler( xFilter );
147 // parse
150 xParser->parseStream( aParserInput );
152 catch( xml::sax::SAXParseException& )
154 // re throw ?
156 catch( xml::sax::SAXException& )
158 // re throw ?
160 catch( io::IOException& )
162 // re throw ?
165 bInfoChanged = FALSE;
166 MakeBlockText(aCur);
168 catch( uno::Exception& )
172 xRoot = 0;
174 return 0;
177 // event description for autotext events; this constant should really be
178 // taken from unocore/unoevents.cxx or ui/unotxt.cxx
179 const struct SvEventDescription aAutotextEvents[] =
181 { SW_EVENT_START_INS_GLOSSARY, "OnInsertStart" },
182 { SW_EVENT_END_INS_GLOSSARY, "OnInsertDone" },
183 { 0, NULL }
186 ULONG SwXMLTextBlocks::GetMacroTable( USHORT nIdx,
187 SvxMacroTableDtor& rMacroTbl,
188 sal_Bool bFileAlreadyOpen )
190 // set current auto text
192 aShort = aNames[ nIdx ]->aShort;
193 aLong = aNames[ nIdx ]->aLong;
194 aPackageName = aNames[ nIdx ]->aPackageName;
196 ULONG nRet = 0;
198 // open stream in proper sub-storage
199 if( !bFileAlreadyOpen )
201 CloseFile();
202 nRet = OpenFile ( TRUE );
204 if ( 0 == nRet )
208 xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::READ );
209 long nTmp = SOT_FORMATSTR_ID_STARWRITER_60;
210 sal_Bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp );
212 OUString sStreamName = OUString::createFromAscii("atevent.xml");
213 uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement(
214 sStreamName, embed::ElementModes::READ );
215 DBG_ASSERT(xDocStream.is(), "Can't create stream");
216 if ( xDocStream.is() )
218 uno::Reference<io::XInputStream> xInputStream = xDocStream->getInputStream();
220 // prepare ParserInputSrouce
221 xml::sax::InputSource aParserInput;
222 aParserInput.sSystemId = aName;
223 aParserInput.aInputStream = xInputStream;
225 // get service factory
226 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
227 comphelper::getProcessServiceFactory();
228 if ( xServiceFactory.is() )
231 // get parser
232 OUString sParserService( RTL_CONSTASCII_USTRINGPARAM(
233 "com.sun.star.xml.sax.Parser" ) );
234 uno::Reference< xml::sax::XParser > xParser(
235 xServiceFactory->createInstance(sParserService),
236 UNO_QUERY );
237 DBG_ASSERT( xParser.is(), "Can't create parser" );
238 if( xParser.is() )
240 // create descriptor and reference to it. Either
241 // both or neither must be kept because of the
242 // reference counting!
243 SvMacroTableEventDescriptor* pDescriptor =
244 new SvMacroTableEventDescriptor(aAutotextEvents);
245 uno::Reference<XNameReplace> xReplace = pDescriptor;
246 Sequence<Any> aFilterArguments( 1 );
247 aFilterArguments[0] <<= xReplace;
249 // get filter
250 OUString sFilterComponent( OUString::createFromAscii(
251 bOasis
252 ? "com.sun.star.comp.Writer.XMLOasisAutotextEventsImporter"
253 : "com.sun.star.comp.Writer.XMLAutotextEventsImporter"));
254 uno::Reference< xml::sax::XDocumentHandler > xFilter(
255 xServiceFactory->createInstanceWithArguments(
256 sFilterComponent, aFilterArguments),
257 UNO_QUERY );
258 DBG_ASSERT( xFilter.is(),
259 "can't instantiate atevents filter");
260 if ( xFilter.is() )
262 // connect parser and filter
263 xParser->setDocumentHandler( xFilter );
265 // connect model and filter
266 uno::Reference<document::XImporter> xImporter( xFilter,
267 UNO_QUERY );
269 // we don't need a model
270 // xImporter->setTargetDocument( xModelComponent );
272 // parse the stream
275 xParser->parseStream( aParserInput );
277 catch( xml::sax::SAXParseException& )
279 // workaround for #83452#: SetSize doesn't work
280 // nRet = ERR_SWG_READ_ERROR;
282 catch( xml::sax::SAXException& )
284 nRet = ERR_SWG_READ_ERROR;
286 catch( io::IOException& )
288 nRet = ERR_SWG_READ_ERROR;
291 // and finally, copy macro into table
292 if (0 == nRet)
293 pDescriptor->copyMacrosIntoTable(rMacroTbl);
295 else
296 nRet = ERR_SWG_READ_ERROR;
298 else
299 nRet = ERR_SWG_READ_ERROR;
302 else
303 nRet = ERR_SWG_READ_ERROR;
305 else
306 nRet = ERR_SWG_READ_ERROR;
308 catch( uno::Exception& )
310 nRet = ERR_SWG_READ_ERROR;
313 else
314 nRet = ERR_SWG_READ_ERROR;
316 // success!
317 return nRet;
321 ULONG SwXMLTextBlocks::GetBlockText( const String& rShort, String& rText )
323 ULONG n = 0;
324 sal_Bool bTextOnly = sal_True;
325 String aFolderName;
326 GeneratePackageName ( rShort, aFolderName );
327 String aStreamName = aFolderName + (OUString) String::CreateFromAscii(".xml");
328 rText.Erase();
332 xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
333 uno::Reference < container::XNameAccess > xAccess( xRoot, uno::UNO_QUERY );
334 if ( !xAccess->hasByName( aStreamName ) || !xRoot->isStreamElement( aStreamName ) )
336 bTextOnly = sal_False;
337 aStreamName = String::CreateFromAscii("content.xml");
340 uno::Reference < io::XStream > xContents = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
341 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
342 comphelper::getProcessServiceFactory();
343 ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
344 if( !xServiceFactory.is() )
346 // Throw an exception ?
349 xml::sax::InputSource aParserInput;
350 aParserInput.sSystemId = aName;
351 aParserInput.aInputStream = xContents->getInputStream();
353 // get parser
354 uno::Reference< XInterface > xXMLParser = xServiceFactory->createInstance(
355 OUString::createFromAscii("com.sun.star.xml.sax.Parser") );
356 ASSERT( xXMLParser.is(),
357 "XMLReader::Read: com.sun.star.xml.sax.Parser service missing" );
358 if( !xXMLParser.is() )
360 // Maybe throw an exception?
363 // get filter
364 // #110680#
365 // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, rText, bTextOnly );
366 uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xServiceFactory, *this, rText, bTextOnly );
368 // connect parser and filter
369 uno::Reference< xml::sax::XParser > xParser( xXMLParser, UNO_QUERY );
370 xParser->setDocumentHandler( xFilter );
372 // parse
375 xParser->parseStream( aParserInput );
377 catch( xml::sax::SAXParseException& )
379 // re throw ?
381 catch( xml::sax::SAXException& )
383 // re throw ?
385 catch( io::IOException& )
387 // re throw ?
390 xRoot = 0;
392 catch ( uno::Exception& )
394 ASSERT( sal_False, "Tried to open non-existent folder or stream!");
397 return n;
400 ULONG SwXMLTextBlocks::PutBlockText( const String& rShort, const String& ,
401 const String& rText, const String& rPackageName )
403 GetIndex ( rShort );
405 if (xBlkRoot->IsContained ( rPackageName ) )
407 xBlkRoot->Remove ( rPackageName );
408 xBlkRoot->Commit ( );
411 String aFolderName( rPackageName );
412 String aStreamName = aFolderName + (OUString) String::CreateFromAscii(".xml");
414 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
415 comphelper::getProcessServiceFactory();
416 ASSERT( xServiceFactory.is(),
417 "XMLReader::Read: got no service manager" );
418 if( !xServiceFactory.is() )
420 // Throw an exception ?
423 uno::Reference < XInterface > xWriter (xServiceFactory->createInstance(
424 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer"))));
425 DBG_ASSERT(xWriter.is(),"com.sun.star.xml.sax.Writer service missing");
426 ULONG nRes = 0;
430 xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::WRITE );
431 uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( aStreamName,
432 embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
434 uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
435 String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
436 OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") );
437 Any aAny;
438 aAny <<= aMime;
439 xSet->setPropertyValue( aPropName, aAny );
440 uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
441 uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY);
442 xSrc->setOutputStream(xOut);
444 uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter,
445 uno::UNO_QUERY);
447 // #110680#
448 // SwXMLTextBlockExport aExp(*this, GetXMLToken ( XML_UNFORMATTED_TEXT ), xHandler);
449 SwXMLTextBlockExport aExp( xServiceFactory, *this, GetXMLToken ( XML_UNFORMATTED_TEXT ), xHandler);
451 aExp.exportDoc( rText );
453 uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY );
454 if ( xTrans.is() )
455 xTrans->commit();
457 if (! (nFlags & SWXML_NOROOTCOMMIT) )
459 uno::Reference < embed::XTransactedObject > xTmpTrans( xBlkRoot, uno::UNO_QUERY );
460 if ( xTmpTrans.is() )
461 xTmpTrans->commit();
464 catch ( uno::Exception& )
466 nRes = ERR_SWG_WRITE_ERROR;
469 xRoot = 0;
471 //TODO/LATER: error handling
473 ULONG nErr = xBlkRoot->GetError();
474 ULONG nRes = 0;
475 if( nErr == SVSTREAM_DISK_FULL )
476 nRes = ERR_W4W_WRITE_FULL;
477 else if( nErr != SVSTREAM_OK )
478 nRes = ERR_SWG_WRITE_ERROR;
480 if( !nRes ) // damit ueber GetText & nCur aufs Doc zugegriffen
481 MakeBlockText( rText );
483 return nRes;
486 void SwXMLTextBlocks::ReadInfo( void )
490 const OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_BLOCKLIST ) );
491 uno::Reference < container::XNameAccess > xAccess( xBlkRoot, uno::UNO_QUERY );
492 if ( xAccess.is() && xAccess->hasByName( sDocName ) && xBlkRoot->isStreamElement( sDocName ) )
494 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
495 comphelper::getProcessServiceFactory();
496 ASSERT( xServiceFactory.is(),
497 "XMLReader::Read: got no service manager" );
498 if( !xServiceFactory.is() )
500 // Throw an exception ?
503 xml::sax::InputSource aParserInput;
504 aParserInput.sSystemId = sDocName;
506 uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName, embed::ElementModes::READ );
507 aParserInput.aInputStream = xDocStream->getInputStream();
509 // get parser
510 uno::Reference< XInterface > xXMLParser = xServiceFactory->createInstance(
511 OUString::createFromAscii("com.sun.star.xml.sax.Parser") );
512 ASSERT( xXMLParser.is(),
513 "XMLReader::Read: com.sun.star.xml.sax.Parser service missing" );
514 if( !xXMLParser.is() )
516 // Maybe throw an exception?
519 // get filter
520 // #110680#
521 // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLBlockListImport( *this );
522 uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLBlockListImport( xServiceFactory, *this );
524 // connect parser and filter
525 uno::Reference< xml::sax::XParser > xParser( xXMLParser, UNO_QUERY );
526 xParser->setDocumentHandler( xFilter );
528 // parse
531 xParser->parseStream( aParserInput );
533 catch( xml::sax::SAXParseException& )
535 // re throw ?
537 catch( xml::sax::SAXException& )
539 // re throw ?
541 catch( io::IOException& )
543 // re throw ?
547 catch ( uno::Exception& )
551 void SwXMLTextBlocks::WriteInfo( void )
553 if ( xBlkRoot.is() || 0 == OpenFile ( FALSE ) )
555 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
556 comphelper::getProcessServiceFactory();
557 DBG_ASSERT( xServiceFactory.is(),
558 "XMLReader::Read: got no service manager" );
559 if( !xServiceFactory.is() )
561 // Throw an exception ?
564 uno::Reference < XInterface > xWriter (xServiceFactory->createInstance(
565 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer"))));
566 DBG_ASSERT(xWriter.is(),"com.sun.star.xml.sax.Writer service missing");
567 OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_BLOCKLIST ) );
570 if ( xBlkRoot->IsContained( sDocName) )
572 xBlkRoot->Remove ( sDocName );
573 xBlkRoot->Commit();
579 uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName,
580 embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
582 uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
583 String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
584 OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") );
585 Any aAny;
586 aAny <<= aMime;
587 xSet->setPropertyValue( aPropName, aAny );
588 uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
589 uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY);
590 xSrc->setOutputStream(xOut);
592 uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter, uno::UNO_QUERY);
594 // #110680#
595 // SwXMLBlockListExport aExp(*this, OUString::createFromAscii(XMLN_BLOCKLIST), xHandler);
596 SwXMLBlockListExport aExp( xServiceFactory, *this, OUString::createFromAscii(XMLN_BLOCKLIST), xHandler);
598 aExp.exportDoc( XML_BLOCK_LIST );
600 uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
601 if ( xTrans.is() )
602 xTrans->commit();
604 catch ( uno::Exception& )
608 bInfoChanged = FALSE;
609 return;
613 ULONG SwXMLTextBlocks::SetMacroTable(
614 USHORT nIdx,
615 const SvxMacroTableDtor& rMacroTbl,
616 sal_Bool bFileAlreadyOpen )
618 // set current autotext
619 aShort = aNames[ nIdx ]->aShort;
620 aLong = aNames[ nIdx ]->aLong;
621 aPackageName = aNames[ nIdx ]->aPackageName;
623 // start XML autotext event export
624 ULONG nRes = 0;
626 uno::Reference< lang::XMultiServiceFactory > xServiceFactory =
627 comphelper::getProcessServiceFactory();
628 ASSERT( xServiceFactory.is(),
629 "XML autotext event write:: got no service manager" );
630 if( !xServiceFactory.is() )
631 return ERR_SWG_WRITE_ERROR;
633 // Get model
634 uno::Reference< lang::XComponent > xModelComp(
635 pDoc->GetDocShell()->GetModel(), UNO_QUERY );
636 ASSERT( xModelComp.is(), "XMLWriter::Write: got no model" );
637 if( !xModelComp.is() )
638 return ERR_SWG_WRITE_ERROR;
640 // open stream in proper sub-storage
641 if( !bFileAlreadyOpen )
643 CloseFile(); // close (it may be open in read-only-mode)
644 nRes = OpenFile ( FALSE );
647 if ( 0 == nRes )
651 xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::WRITE );
652 OUString sStreamName( RTL_CONSTASCII_USTRINGPARAM("atevent.xml") );
653 long nTmp = SOT_FORMATSTR_ID_STARWRITER_60;
654 sal_Bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp );
656 uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( sStreamName,
657 embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
659 uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
660 String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) );
661 OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") );
662 Any aAny;
663 aAny <<= aMime;
664 xSet->setPropertyValue( aPropName, aAny );
665 uno::Reference < io::XOutputStream > xOutputStream = xDocStream->getOutputStream();
667 // get XML writer
668 uno::Reference< io::XActiveDataSource > xSaxWriter(
669 xServiceFactory->createInstance(
670 OUString::createFromAscii("com.sun.star.xml.sax.Writer") ),
671 UNO_QUERY );
672 ASSERT( xSaxWriter.is(), "can't instantiate XML writer" );
673 if( xSaxWriter.is() )
676 // connect XML writer to output stream
677 xSaxWriter->setOutputStream( xOutputStream );
678 uno::Reference<xml::sax::XDocumentHandler> xDocHandler(
679 xSaxWriter, UNO_QUERY);
681 // construct events object
682 uno::Reference<XNameAccess> xEvents =
683 new SvMacroTableEventDescriptor(rMacroTbl,aAutotextEvents);
685 // prepare arguments (prepend doc handler to given arguments)
686 Sequence<Any> aParams(2);
687 aParams[0] <<= xDocHandler;
688 aParams[1] <<= xEvents;
690 // get filter component
691 uno::Reference< document::XExporter > xExporter(
692 xServiceFactory->createInstanceWithArguments(
693 OUString::createFromAscii(
694 bOasis
695 ? "com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter"
696 : "com.sun.star.comp.Writer.XMLAutotextEventsExporter"),
697 aParams), UNO_QUERY);
698 ASSERT( xExporter.is(),
699 "can't instantiate export filter component" );
700 if( xExporter.is() )
702 // connect model and filter
703 xExporter->setSourceDocument( xModelComp );
705 // filter!
706 Sequence<beans::PropertyValue> aFilterProps( 0 );
707 uno::Reference < document::XFilter > xFilter( xExporter,
708 UNO_QUERY );
709 xFilter->filter( aFilterProps );
711 else
712 nRes = ERR_SWG_WRITE_ERROR;
714 else
715 nRes = ERR_SWG_WRITE_ERROR;
717 // finally, commit stream, sub-storage and storage
718 uno::Reference < embed::XTransactedObject > xTmpTrans( xRoot, uno::UNO_QUERY );
719 if ( xTmpTrans.is() )
720 xTmpTrans->commit();
722 if ( !bFileAlreadyOpen )
724 uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
725 if ( xTrans.is() )
726 xTrans->commit();
729 xRoot = 0;
731 catch ( uno::Exception& )
733 nRes = ERR_SWG_WRITE_ERROR;
736 if( !bFileAlreadyOpen )
737 CloseFile();
739 else
740 nRes = ERR_SWG_WRITE_ERROR;
742 return nRes;