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

Generated by: LCOV version 1.10