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

Generated by: LCOV version 1.11