LCOV - code coverage report
Current view: top level - sw/source/core/swg - SwXMLTextBlocks1.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 110 249 44.2 %
Date: 2014-11-03 Functions: 7 9 77.8 %
Legend: Lines: hit not hit

          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             : #include <com/sun/star/embed/ElementModes.hpp>
      21             : #include <com/sun/star/embed/XTransactedObject.hpp>
      22             : #include <svl/macitem.hxx>
      23             : #include <svtools/unoevent.hxx>
      24             : #include <sfx2/docfile.hxx>
      25             : #include <unotools/streamwrap.hxx>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <com/sun/star/xml/sax/InputSource.hpp>
      28             : #include <com/sun/star/io/XActiveDataSource.hpp>
      29             : #include <com/sun/star/xml/sax/Parser.hpp>
      30             : #include <com/sun/star/xml/sax/Writer.hpp>
      31             : #include <com/sun/star/document/XStorageBasedDocument.hpp>
      32             : #include <doc.hxx>
      33             : #include <docsh.hxx>
      34             : #include <shellio.hxx>
      35             : #include <SwXMLTextBlocks.hxx>
      36             : #include <SwXMLBlockImport.hxx>
      37             : #include <SwXMLBlockExport.hxx>
      38             : #include <swevent.hxx>
      39             : #include <swerror.h>
      40             : 
      41             : const char XMLN_BLOCKLIST[] = "BlockList.xml";
      42             : 
      43             : using namespace ::com::sun::star;
      44             : using namespace ::com::sun::star::uno;
      45             : using namespace ::com::sun::star::container;
      46             : 
      47             : using ::xmloff::token::XML_BLOCK_LIST;
      48             : using ::xmloff::token::XML_UNFORMATTED_TEXT;
      49             : using ::xmloff::token::GetXMLToken;
      50             : 
      51           8 : sal_uLong SwXMLTextBlocks::GetDoc( sal_uInt16 nIdx )
      52             : {
      53           8 :     OUString aFolderName ( GetPackageName ( nIdx ) );
      54             : 
      55           8 :     if (!IsOnlyTextBlock ( nIdx ) )
      56             :     {
      57             :         try
      58             :         {
      59           0 :             xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
      60           0 :             xMedium = new SfxMedium( xRoot, GetBaseURL(), OUString( "writer8" ) );
      61           0 :             SwReader aReader( *xMedium, aFolderName, pDoc );
      62           0 :             ReadXML->SetBlockMode( true );
      63           0 :             aReader.Read( *ReadXML );
      64           0 :             ReadXML->SetBlockMode( false );
      65             :             // Ole objects fail to display when inserted into the document, as
      66             :             // the ObjectReplacement folder and contents are missing
      67           0 :             OUString sObjReplacements( "ObjectReplacements" );
      68           0 :             if ( xRoot->hasByName( sObjReplacements ) )
      69             :             {
      70           0 :                 uno::Reference< document::XStorageBasedDocument > xDocStor( pDoc->GetDocShell()->GetModel(), uno::UNO_QUERY_THROW );
      71           0 :                 uno::Reference< embed::XStorage > xStr( xDocStor->getDocumentStorage() );
      72           0 :                 if ( xStr.is() )
      73             :                 {
      74           0 :                     xRoot->copyElementTo( sObjReplacements, xStr, sObjReplacements );
      75           0 :                     uno::Reference< embed::XTransactedObject > xTrans( xStr, uno::UNO_QUERY );
      76           0 :                     if ( xTrans.is() )
      77           0 :                         xTrans->commit();
      78           0 :                 }
      79           0 :             }
      80             :         }
      81           0 :         catch( uno::Exception& )
      82             :         {
      83             :         }
      84             : 
      85           0 :         xRoot = 0;
      86             :     }
      87             :     else
      88             :     {
      89           8 :         OUString aStreamName = aFolderName + ".xml";
      90             :         try
      91             :         {
      92           8 :             xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
      93           8 :             uno::Reference < io::XStream > xStream = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
      94             : 
      95             :             uno::Reference< uno::XComponentContext > xContext =
      96          16 :                 comphelper::getProcessComponentContext();
      97             : 
      98          16 :             xml::sax::InputSource aParserInput;
      99           8 :             aParserInput.sSystemId = aNames[nIdx]->aPackageName;
     100             : 
     101           8 :             aParserInput.aInputStream = xStream->getInputStream();
     102             : 
     103             :             // get filter
     104             :             // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, aCur, sal_True );
     105          16 :             uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xContext, *this, aCur, true );
     106             : 
     107             :             // connect parser and filter
     108          16 :             uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(xContext);
     109           8 :             xParser->setDocumentHandler( xFilter );
     110             : 
     111             :             // parse
     112             :             try
     113             :             {
     114           8 :                 xParser->parseStream( aParserInput );
     115             :             }
     116           0 :             catch( xml::sax::SAXParseException&  )
     117             :             {
     118             :                 // re throw ?
     119             :             }
     120           0 :             catch( xml::sax::SAXException&  )
     121             :             {
     122             :                 // re throw ?
     123             :             }
     124           0 :             catch( io::IOException& )
     125             :             {
     126             :                 // re throw ?
     127             :             }
     128             : 
     129           8 :             bInfoChanged = false;
     130          16 :             MakeBlockText(aCur);
     131             :         }
     132           0 :         catch( uno::Exception& )
     133             :         {
     134             :         }
     135             : 
     136           8 :         xRoot = 0;
     137             :     }
     138           8 :     return 0;
     139             : }
     140             : 
     141             : // event description for autotext events; this constant should really be
     142             : // taken from unocore/unoevents.cxx or ui/unotxt.cxx
     143             : const struct SvEventDescription aAutotextEvents[] =
     144             : {
     145             :     { SW_EVENT_START_INS_GLOSSARY,  "OnInsertStart" },
     146             :     { SW_EVENT_END_INS_GLOSSARY,    "OnInsertDone" },
     147             :     { 0, NULL }
     148             : };
     149             : 
     150           2 : sal_uLong SwXMLTextBlocks::GetMacroTable( sal_uInt16 nIdx,
     151             :                                       SvxMacroTableDtor& rMacroTbl,
     152             :                                       bool bFileAlreadyOpen )
     153             : {
     154             :     // set current auto text
     155           2 :     aShort = aNames[nIdx]->aShort;
     156           2 :     aLong = aNames[nIdx]->aLong;
     157           2 :     aPackageName = aNames[nIdx]->aPackageName;
     158             : 
     159           2 :     sal_uLong nRet = 0;
     160             : 
     161             :     // open stream in proper sub-storage
     162           2 :     if( !bFileAlreadyOpen )
     163             :     {
     164           2 :         CloseFile();
     165           2 :         nRet = OpenFile ( true );
     166             :     }
     167           2 :     if ( 0 == nRet )
     168             :     {
     169             :         try
     170             :         {
     171           2 :             xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::READ );
     172           2 :             long nTmp = SOT_FORMATSTR_ID_STARWRITER_60;
     173           2 :             bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp );
     174             : 
     175           2 :             OUString sStreamName("atevent.xml");
     176           2 :             uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement(
     177           2 :                 sStreamName, embed::ElementModes::READ );
     178             :             OSL_ENSURE(xDocStream.is(), "Can't create stream");
     179           0 :             if ( xDocStream.is() )
     180             :             {
     181           0 :                 uno::Reference<io::XInputStream> xInputStream = xDocStream->getInputStream();
     182             : 
     183             :                 // prepare ParserInputSrouce
     184           0 :                 xml::sax::InputSource aParserInput;
     185           0 :                 aParserInput.sSystemId = aName;
     186           0 :                 aParserInput.aInputStream = xInputStream;
     187             : 
     188             :                 // get service factory
     189             :                 uno::Reference< uno::XComponentContext > xContext =
     190           0 :                     comphelper::getProcessComponentContext();
     191             : 
     192             :                 // get parser
     193           0 :                 uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
     194             : 
     195             :                 // create descriptor and reference to it. Either
     196             :                 // both or neither must be kept because of the
     197             :                 // reference counting!
     198             :                 SvMacroTableEventDescriptor* pDescriptor =
     199           0 :                     new SvMacroTableEventDescriptor(aAutotextEvents);
     200           0 :                 uno::Reference<XNameReplace> xReplace = pDescriptor;
     201           0 :                 Sequence<Any> aFilterArguments( 1 );
     202           0 :                 aFilterArguments[0] <<= xReplace;
     203             : 
     204             :                 // get filter
     205             :                 OUString sFilterComponent = bOasis
     206             :                     ? OUString("com.sun.star.comp.Writer.XMLOasisAutotextEventsImporter")
     207           0 :                     : OUString("com.sun.star.comp.Writer.XMLAutotextEventsImporter");
     208             :                 uno::Reference< xml::sax::XDocumentHandler > xFilter(
     209           0 :                     xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
     210           0 :                         sFilterComponent, aFilterArguments, xContext),
     211           0 :                     UNO_QUERY );
     212             :                 OSL_ENSURE( xFilter.is(),
     213             :                             "can't instantiate atevents filter");
     214           0 :                 if ( xFilter.is() )
     215             :                 {
     216             :                     // connect parser and filter
     217           0 :                     xParser->setDocumentHandler( xFilter );
     218             : 
     219             :                     // connect model and filter
     220           0 :                     uno::Reference<document::XImporter> xImporter( xFilter, UNO_QUERY );
     221             : 
     222             :                     // we don't need a model
     223             :                     // xImporter->setTargetDocument( xModelComponent );
     224             : 
     225             :                     // parse the stream
     226             :                     try
     227             :                     {
     228           0 :                         xParser->parseStream( aParserInput );
     229             :                     }
     230           0 :                     catch( xml::sax::SAXParseException& )
     231             :                     {
     232             :                         // workaround for #83452#: SetSize doesn't work
     233             :                         // nRet = ERR_SWG_READ_ERROR;
     234             :                     }
     235           0 :                     catch( xml::sax::SAXException& )
     236             :                     {
     237           0 :                         nRet = ERR_SWG_READ_ERROR;
     238             :                     }
     239           0 :                     catch( io::IOException& )
     240             :                     {
     241           0 :                         nRet = ERR_SWG_READ_ERROR;
     242             :                     }
     243             : 
     244             :                     // and finally, copy macro into table
     245           0 :                     if (0 == nRet)
     246           0 :                         pDescriptor->copyMacrosIntoTable(rMacroTbl);
     247             :                 }
     248             :                 else
     249           0 :                     nRet = ERR_SWG_READ_ERROR;
     250             :             }
     251             :             else
     252           2 :                 nRet = ERR_SWG_READ_ERROR;
     253             :         }
     254           4 :         catch( uno::Exception& )
     255             :         {
     256           2 :             nRet = ERR_SWG_READ_ERROR;
     257             :         }
     258             :     }
     259             :     else
     260           0 :         nRet = ERR_SWG_READ_ERROR;
     261             : 
     262             :     // success!
     263           2 :     return nRet;
     264             : }
     265             : 
     266           0 : sal_uLong SwXMLTextBlocks::GetBlockText( const OUString& rShort, OUString& rText )
     267             : {
     268           0 :     sal_uLong n = 0;
     269           0 :     bool bTextOnly = true;
     270           0 :     OUString aFolderName = GeneratePackageName ( rShort );
     271           0 :     OUString aStreamName = aFolderName + ".xml";
     272           0 :     rText = "";
     273             : 
     274             :     try
     275             :     {
     276           0 :         xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ );
     277           0 :         uno::Reference < container::XNameAccess > xAccess( xRoot, uno::UNO_QUERY );
     278           0 :         if ( !xAccess->hasByName( aStreamName ) || !xRoot->isStreamElement( aStreamName ) )
     279             :         {
     280           0 :             bTextOnly = false;
     281           0 :             aStreamName = "content.xml";
     282             :         }
     283             : 
     284           0 :         uno::Reference < io::XStream > xContents = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ );
     285             :         uno::Reference< uno::XComponentContext > xContext =
     286           0 :             comphelper::getProcessComponentContext();
     287             : 
     288           0 :         xml::sax::InputSource aParserInput;
     289           0 :         aParserInput.sSystemId = aName;
     290           0 :         aParserInput.aInputStream = xContents->getInputStream();
     291             : 
     292             :         // get filter
     293             :         // #110680#
     294             :         // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, rText, bTextOnly );
     295           0 :         uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xContext, *this, rText, bTextOnly );
     296             : 
     297             :         // connect parser and filter
     298           0 :         uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(xContext);
     299           0 :         xParser->setDocumentHandler( xFilter );
     300             : 
     301             :         // parse
     302             :         try
     303             :         {
     304           0 :             xParser->parseStream( aParserInput );
     305             :         }
     306           0 :         catch( xml::sax::SAXParseException&  )
     307             :         {
     308             :             // re throw ?
     309             :         }
     310           0 :         catch( xml::sax::SAXException&  )
     311             :         {
     312             :             // re throw ?
     313             :         }
     314           0 :         catch( io::IOException& )
     315             :         {
     316             :             // re throw ?
     317             :         }
     318             : 
     319           0 :         xRoot = 0;
     320             :     }
     321           0 :     catch ( uno::Exception& )
     322             :     {
     323             :         OSL_FAIL( "Tried to open non-existent folder or stream!");
     324             :     }
     325             : 
     326           0 :     return n;
     327             : }
     328             : 
     329          12 : sal_uLong SwXMLTextBlocks::PutBlockText( const OUString& rShort, const OUString& ,
     330             :                                          const OUString& rText,  const OUString& rPackageName )
     331             : {
     332          12 :     GetIndex ( rShort );
     333             :     /*
     334             :     if (xBlkRoot->IsContained ( rPackageName ) )
     335             :     {
     336             :         xBlkRoot->Remove ( rPackageName );
     337             :         xBlkRoot->Commit ( );
     338             :     }
     339             :     */
     340          12 :     OUString aFolderName( rPackageName );
     341          24 :     OUString aStreamName = aFolderName + ".xml";
     342             : 
     343             :     uno::Reference< uno::XComponentContext > xContext =
     344          24 :         comphelper::getProcessComponentContext();
     345             : 
     346          24 :     uno::Reference < xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
     347          12 :     sal_uLong nRes = 0;
     348             : 
     349             :     try
     350             :     {
     351          12 :     xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::WRITE );
     352          12 :     uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( aStreamName,
     353          12 :                 embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
     354             : 
     355          24 :     uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
     356          24 :     OUString aMime ( "text/xml" );
     357          24 :     Any aAny;
     358          12 :     aAny <<= aMime;
     359          12 :     xSet->setPropertyValue("MediaType", aAny );
     360          24 :     uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
     361          24 :        uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY);
     362          12 :        xSrc->setOutputStream(xOut);
     363             : 
     364             :        uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter,
     365          24 :         uno::UNO_QUERY);
     366             : 
     367          24 :    SwXMLTextBlockExport aExp( xContext, *this, GetXMLToken ( XML_UNFORMATTED_TEXT ), xHandler);
     368             : 
     369          12 :     aExp.exportDoc( rText );
     370             : 
     371          24 :     uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY );
     372          12 :     if ( xTrans.is() )
     373          12 :         xTrans->commit();
     374             : 
     375          12 :     if (! (nFlags & SWXML_NOROOTCOMMIT) )
     376             :     {
     377          12 :         uno::Reference < embed::XTransactedObject > xTmpTrans( xBlkRoot, uno::UNO_QUERY );
     378          12 :         if ( xTmpTrans.is() )
     379          12 :             xTmpTrans->commit();
     380          12 :     }
     381             :     }
     382           0 :     catch ( uno::Exception& )
     383             :     {
     384           0 :         nRes = ERR_SWG_WRITE_ERROR;
     385             :     }
     386             : 
     387          12 :     xRoot = 0;
     388             : 
     389             :     //TODO/LATER: error handling
     390             :     /*
     391             :     sal_uLong nErr = xBlkRoot->GetError();
     392             :     sal_uLong nRes = 0;
     393             :     if( nErr == SVSTREAM_DISK_FULL )
     394             :         nRes = ERR_W4W_WRITE_FULL;
     395             :     else if( nErr != SVSTREAM_OK )
     396             :         nRes = ERR_SWG_WRITE_ERROR;
     397             :     */
     398          12 :     if( !nRes ) // So that we can access the Doc via GetText & nCur
     399          12 :         MakeBlockText( rText );
     400             : 
     401          24 :     return nRes;
     402             : }
     403             : 
     404         118 : void SwXMLTextBlocks::ReadInfo( void )
     405             : {
     406             :     try
     407             :     {
     408         118 :     const OUString sDocName( XMLN_BLOCKLIST );
     409         236 :     uno::Reference < container::XNameAccess > xAccess( xBlkRoot, uno::UNO_QUERY );
     410         118 :     if ( xAccess.is() && xAccess->hasByName( sDocName ) && xBlkRoot->isStreamElement( sDocName ) )
     411             :     {
     412             :         uno::Reference< uno::XComponentContext > xContext =
     413         114 :                 comphelper::getProcessComponentContext();
     414             : 
     415         228 :         xml::sax::InputSource aParserInput;
     416         114 :         aParserInput.sSystemId = sDocName;
     417             : 
     418         228 :         uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName, embed::ElementModes::READ );
     419         114 :         aParserInput.aInputStream = xDocStream->getInputStream();
     420             : 
     421             :         // get filter
     422         228 :         uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLBlockListImport( xContext, *this );
     423             : 
     424             :         // connect parser and filter
     425         228 :         uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
     426         114 :         xParser->setDocumentHandler( xFilter );
     427             : 
     428             :         // parse
     429             :         try
     430             :         {
     431         114 :             xParser->parseStream( aParserInput );
     432             :         }
     433           0 :         catch( xml::sax::SAXParseException&  )
     434             :         {
     435             :             // re throw ?
     436             :         }
     437           0 :         catch( xml::sax::SAXException&  )
     438             :         {
     439             :             // re throw ?
     440             :         }
     441           0 :         catch( io::IOException& )
     442             :         {
     443             :             // re throw ?
     444         114 :         }
     445         118 :     }
     446             :     }
     447           0 :     catch ( uno::Exception& )
     448             :     {
     449             :     }
     450         118 : }
     451          34 : void SwXMLTextBlocks::WriteInfo( void )
     452             : {
     453          34 :     if ( xBlkRoot.is() || 0 == OpenFile ( false ) )
     454             :     {
     455             :         uno::Reference< uno::XComponentContext > xContext =
     456          34 :             comphelper::getProcessComponentContext();
     457             : 
     458          68 :         uno::Reference < xml::sax::XWriter > xWriter = xml::sax::Writer::create(xContext);
     459          68 :         OUString sDocName( XMLN_BLOCKLIST );
     460             : 
     461             :         /*
     462             :         if ( xBlkRoot->IsContained( sDocName) )
     463             :         {
     464             :             xBlkRoot->Remove ( sDocName );
     465             :             xBlkRoot->Commit();
     466             :         }
     467             :         */
     468             : 
     469             :         try
     470             :         {
     471          34 :         uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName,
     472          34 :                     embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
     473             : 
     474          68 :         uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
     475          68 :         OUString aMime ( "text/xml" );
     476          68 :         Any aAny;
     477          34 :         aAny <<= aMime;
     478          34 :         xSet->setPropertyValue("MediaType", aAny );
     479          68 :         uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream();
     480          68 :         uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY);
     481          34 :         xSrc->setOutputStream(xOut);
     482             : 
     483          68 :         uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter, uno::UNO_QUERY);
     484             : 
     485          68 :         SwXMLBlockListExport aExp( xContext, *this, OUString(XMLN_BLOCKLIST), xHandler);
     486             : 
     487          34 :         aExp.exportDoc( XML_BLOCK_LIST );
     488             : 
     489          68 :         uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
     490          34 :         if ( xTrans.is() )
     491          68 :             xTrans->commit();
     492             :         }
     493           0 :         catch ( uno::Exception& )
     494             :         {
     495             :         }
     496             : 
     497          34 :         bInfoChanged = false;
     498          68 :         return;
     499             :     }
     500             : }
     501             : 
     502           0 : sal_uLong SwXMLTextBlocks::SetMacroTable(
     503             :     sal_uInt16 nIdx,
     504             :     const SvxMacroTableDtor& rMacroTbl,
     505             :     bool bFileAlreadyOpen )
     506             : {
     507             :     // set current autotext
     508           0 :     aShort = aNames[nIdx]->aShort;
     509           0 :     aLong = aNames[nIdx]->aLong;
     510           0 :     aPackageName = aNames[nIdx]->aPackageName;
     511             : 
     512             :     // start XML autotext event export
     513           0 :     sal_uLong nRes = 0;
     514             : 
     515             :     uno::Reference< uno::XComponentContext > xContext =
     516           0 :         comphelper::getProcessComponentContext();
     517             : 
     518             :     // Get model
     519             :     uno::Reference< lang::XComponent > xModelComp(
     520           0 :         pDoc->GetDocShell()->GetModel(), UNO_QUERY );
     521             :     OSL_ENSURE( xModelComp.is(), "XMLWriter::Write: got no model" );
     522           0 :     if( !xModelComp.is() )
     523           0 :         return ERR_SWG_WRITE_ERROR;
     524             : 
     525             :     // open stream in proper sub-storage
     526           0 :     if( !bFileAlreadyOpen )
     527             :     {
     528           0 :         CloseFile(); // close (it may be open in read-only-mode)
     529           0 :         nRes = OpenFile ( false );
     530             :     }
     531             : 
     532           0 :     if ( 0 == nRes )
     533             :     {
     534             :         try
     535             :         {
     536           0 :             xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::WRITE );
     537           0 :             OUString sStreamName("atevent.xml" );
     538           0 :             long nTmp = SOT_FORMATSTR_ID_STARWRITER_60;
     539           0 :             bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp );
     540             : 
     541           0 :             uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( sStreamName,
     542           0 :                         embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE );
     543             : 
     544           0 :             uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
     545           0 :             OUString aMime( "text/xml" );
     546           0 :             Any aAny;
     547           0 :             aAny <<= aMime;
     548           0 :             xSet->setPropertyValue("MediaType", aAny );
     549           0 :             uno::Reference < io::XOutputStream > xOutputStream = xDocStream->getOutputStream();
     550             : 
     551             :             // get XML writer
     552             :             uno::Reference< xml::sax::XWriter > xSaxWriter =
     553           0 :                 xml::sax::Writer::create( xContext );
     554             : 
     555             :             // connect XML writer to output stream
     556           0 :             xSaxWriter->setOutputStream( xOutputStream );
     557             :             uno::Reference<xml::sax::XDocumentHandler> xDocHandler(
     558           0 :                 xSaxWriter, UNO_QUERY);
     559             : 
     560             :             // construct events object
     561             :             uno::Reference<XNameAccess> xEvents =
     562           0 :                 new SvMacroTableEventDescriptor(rMacroTbl,aAutotextEvents);
     563             : 
     564             :             // prepare arguments (prepend doc handler to given arguments)
     565           0 :             Sequence<Any> aParams(2);
     566           0 :             aParams[0] <<= xDocHandler;
     567           0 :             aParams[1] <<= xEvents;
     568             : 
     569             :             // get filter component
     570             :             OUString sFilterComponent = bOasis
     571             :                 ? OUString("com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter")
     572           0 :                 : OUString("com.sun.star.comp.Writer.XMLAutotextEventsExporter");
     573             :             uno::Reference< document::XExporter > xExporter(
     574           0 :                 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
     575           0 :                     sFilterComponent, aParams, xContext), UNO_QUERY);
     576             :             OSL_ENSURE( xExporter.is(),
     577             :                     "can't instantiate export filter component" );
     578           0 :             if( xExporter.is() )
     579             :             {
     580             :                 // connect model and filter
     581           0 :                 xExporter->setSourceDocument( xModelComp );
     582             : 
     583             :                 // filter!
     584           0 :                 Sequence<beans::PropertyValue> aFilterProps( 0 );
     585             :                 uno::Reference < document::XFilter > xFilter( xExporter,
     586           0 :                                                          UNO_QUERY );
     587           0 :                 xFilter->filter( aFilterProps );
     588             :             }
     589             :             else
     590           0 :                 nRes = ERR_SWG_WRITE_ERROR;
     591             : 
     592             :             // finally, commit stream, sub-storage and storage
     593           0 :             uno::Reference < embed::XTransactedObject > xTmpTrans( xRoot, uno::UNO_QUERY );
     594           0 :             if ( xTmpTrans.is() )
     595           0 :                 xTmpTrans->commit();
     596             : 
     597           0 :             if ( !bFileAlreadyOpen )
     598             :             {
     599           0 :                 uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY );
     600           0 :                 if ( xTrans.is() )
     601           0 :                     xTrans->commit();
     602             :             }
     603             : 
     604           0 :             xRoot = 0;
     605             :         }
     606           0 :         catch ( uno::Exception& )
     607             :         {
     608           0 :             nRes = ERR_SWG_WRITE_ERROR;
     609             :         }
     610             : 
     611           0 :         if( !bFileAlreadyOpen )
     612           0 :             CloseFile();
     613             :     }
     614             :     else
     615           0 :         nRes = ERR_SWG_WRITE_ERROR;
     616             : 
     617           0 :     return nRes;
     618         270 : }
     619             : 
     620             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10