LCOV - code coverage report
Current view: top level - svx/source/xml - xmlexport.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 109 0.9 %
Date: 2014-11-03 Functions: 2 8 25.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 <tools/debug.hxx>
      21             : #include <tools/diagnose_ex.h>
      22             : #include <com/sun/star/xml/sax/InputSource.hpp>
      23             : #include <com/sun/star/xml/sax/Parser.hpp>
      24             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      25             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      26             : #include <com/sun/star/xml/sax/Writer.hpp>
      27             : #include <com/sun/star/lang/XComponent.hpp>
      28             : #include <com/sun/star/io/XActiveDataSource.hpp>
      29             : #include <com/sun/star/xml/sax/SAXParseException.hpp>
      30             : #include <com/sun/star/io/XOutputStream.hpp>
      31             : #include <com/sun/star/document/XFilter.hpp>
      32             : #include <com/sun/star/document/XExporter.hpp>
      33             : #include <com/sun/star/document/XImporter.hpp>
      34             : #include <comphelper/processfactory.hxx>
      35             : #include <unotools/streamwrap.hxx>
      36             : #include <sot/storage.hxx>
      37             : #include <svx/svdmodel.hxx>
      38             : #include <svx/xmleohlp.hxx>
      39             : #include <svx/xmlgrhlp.hxx>
      40             : 
      41             : #include <svx/unomodel.hxx>
      42             : 
      43             : using namespace ::com::sun::star;
      44             : using namespace ::com::sun::star::uno;
      45             : 
      46           0 : bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent )
      47             : {
      48           0 :     return SvxDrawingLayerExport( pModel, xOut, xComponent, "com.sun.star.comp.DrawingLayer.XMLExporter" );
      49             : }
      50             : 
      51           0 : bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent, const char* pExportService )
      52             : {
      53           0 :     bool bDocRet = xOut.is();
      54             : 
      55           0 :     Reference< document::XGraphicObjectResolver > xGraphicResolver;
      56           0 :     SvXMLGraphicHelper *pGraphicHelper = 0;
      57             : 
      58           0 :     Reference< document::XEmbeddedObjectResolver > xObjectResolver;
      59           0 :     SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
      60             : 
      61           0 :     Reference< lang::XComponent > xSourceDoc( xComponent );
      62             :     try
      63             :     {
      64           0 :         if( !xSourceDoc.is() )
      65             :         {
      66           0 :             xSourceDoc = new SvxUnoDrawingModel( pModel );
      67           0 :             pModel->setUnoModel( Reference< XInterface >::query( xSourceDoc ) );
      68             :         }
      69             : 
      70           0 :         uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
      71             : 
      72           0 :         if( bDocRet )
      73             :         {
      74           0 :             uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
      75             : 
      76           0 :             ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
      77           0 :             if( pPersist )
      78             :             {
      79           0 :                 pObjectHelper = SvXMLEmbeddedObjectHelper::Create( *pPersist, EMBEDDEDOBJECTHELPER_MODE_WRITE );
      80           0 :                 xObjectResolver = pObjectHelper;
      81             :             }
      82             : 
      83           0 :             pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_WRITE );
      84           0 :             xGraphicResolver = pGraphicHelper;
      85             : 
      86           0 :             if( bDocRet )
      87             :             {
      88           0 :                 uno::Reference<xml::sax::XDocumentHandler>  xHandler( xWriter, uno::UNO_QUERY );
      89             : 
      90             :                 // doc export
      91           0 :                 uno::Reference< io::XActiveDataSource > xDocSrc( xWriter, uno::UNO_QUERY );
      92           0 :                 xDocSrc->setOutputStream( xOut );
      93             : 
      94           0 :                 uno::Sequence< uno::Any > aArgs( xObjectResolver.is() ? 3 : 2 );
      95           0 :                 aArgs[0] <<= xHandler;
      96           0 :                 aArgs[1] <<= xGraphicResolver;
      97           0 :                 if( xObjectResolver.is() )
      98           0 :                     aArgs[2] <<= xObjectResolver;
      99             : 
     100           0 :                 uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pExportService ), aArgs, xContext ), uno::UNO_QUERY );
     101           0 :                 if( !xFilter.is() )
     102             :                 {
     103             :                     OSL_FAIL( "com.sun.star.comp.Draw.XMLExporter service missing" );
     104           0 :                     bDocRet = false;
     105             :                 }
     106             : 
     107           0 :                 if( bDocRet )
     108             :                 {
     109           0 :                     uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
     110           0 :                     if( xExporter.is() )
     111             :                     {
     112           0 :                         xExporter->setSourceDocument( xSourceDoc );
     113             : 
     114           0 :                         uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
     115           0 :                         bDocRet = xFilter->filter( aDescriptor );
     116           0 :                     }
     117           0 :                 }
     118           0 :             }
     119           0 :         }
     120             :     }
     121           0 :     catch(uno::Exception const&)
     122             :     {
     123             :         DBG_UNHANDLED_EXCEPTION();
     124           0 :         bDocRet = false;
     125             :     }
     126             : 
     127           0 :     if( pGraphicHelper )
     128           0 :         SvXMLGraphicHelper::Destroy( pGraphicHelper );
     129           0 :     xGraphicResolver = 0;
     130             : 
     131           0 :     if( pObjectHelper )
     132           0 :         SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
     133           0 :     xObjectResolver = 0;
     134             : 
     135           0 :     return bDocRet;
     136             : }
     137             : 
     138           0 : bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
     139             : {
     140           0 :     Reference< lang::XComponent > xComponent;
     141           0 :     return SvxDrawingLayerExport( pModel, xOut, xComponent );
     142             : }
     143             : 
     144             : //-
     145             : 
     146           0 : bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
     147             : {
     148           0 :     return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
     149             : }
     150             : 
     151           0 : bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService  )
     152             : {
     153           0 :     sal_uInt32  nRet = 0;
     154             : 
     155           0 :     Reference< document::XGraphicObjectResolver > xGraphicResolver;
     156           0 :     SvXMLGraphicHelper *pGraphicHelper = 0;
     157             : 
     158           0 :     Reference< document::XEmbeddedObjectResolver > xObjectResolver;
     159           0 :     SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
     160             : 
     161           0 :     Reference< lang::XComponent > xTargetDocument( xComponent );
     162           0 :     if( !xTargetDocument.is() )
     163             :     {
     164           0 :         xTargetDocument = new SvxUnoDrawingModel( pModel );
     165           0 :         pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
     166             :     }
     167             : 
     168           0 :     Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
     169             : 
     170             :     try
     171             :     {
     172             :         // Get service factory
     173           0 :         Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
     174             : 
     175           0 :         if ( xTargetModel.is() )
     176           0 :             xTargetModel->lockControllers();
     177             : 
     178             : 
     179             : 
     180           0 :         pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_READ );
     181           0 :         xGraphicResolver = pGraphicHelper;
     182             : 
     183           0 :         ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
     184           0 :         if( pPersist )
     185             :         {
     186             :             pObjectHelper = SvXMLEmbeddedObjectHelper::Create(
     187             :                                         *pPersist,
     188           0 :                                         EMBEDDEDOBJECTHELPER_MODE_READ );
     189           0 :             xObjectResolver = pObjectHelper;
     190             :         }
     191             : 
     192             : 
     193             : 
     194           0 :         if( 0 == nRet )
     195             :         {
     196             : 
     197             :             // parse
     198             :             // prepare ParserInputSrouce
     199           0 :             xml::sax::InputSource aParserInput;
     200           0 :             aParserInput.aInputStream = xInputStream;
     201             : 
     202             :             // get parser
     203           0 :             Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
     204             : 
     205             :             // prepare filter arguments
     206           0 :             Sequence<Any> aFilterArgs( 2 );
     207           0 :             Any *pArgs = aFilterArgs.getArray();
     208           0 :             *pArgs++ <<= xGraphicResolver;
     209           0 :             *pArgs++ <<= xObjectResolver;
     210             : 
     211             :             // get filter
     212           0 :             Reference< xml::sax::XDocumentHandler > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pImportService ), aFilterArgs, xContext), UNO_QUERY );
     213             :             DBG_ASSERT( xFilter.is(), "Can't instantiate filter component." );
     214             : 
     215           0 :             nRet = 1;
     216           0 :             if( xParser.is() && xFilter.is() )
     217             :             {
     218             :                 // connect parser and filter
     219           0 :                 xParser->setDocumentHandler( xFilter );
     220             : 
     221             :                 // connect model and filter
     222           0 :                 uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
     223           0 :                 xImporter->setTargetDocument( xTargetDocument );
     224             : 
     225             :                 // finally, parser the stream
     226           0 :                 xParser->parseStream( aParserInput );
     227             : 
     228           0 :                 nRet = 0;
     229           0 :             }
     230           0 :         }
     231             :     }
     232           0 :     catch( uno::Exception& )
     233             :     {
     234             :         DBG_UNHANDLED_EXCEPTION();
     235             :     }
     236             : 
     237           0 :     if( pGraphicHelper )
     238           0 :         SvXMLGraphicHelper::Destroy( pGraphicHelper );
     239           0 :     xGraphicResolver = 0;
     240             : 
     241           0 :     if( pObjectHelper )
     242           0 :         SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
     243           0 :     xObjectResolver = 0;
     244             : 
     245           0 :     if ( xTargetModel.is() )
     246           0 :         xTargetModel->unlockControllers();
     247             : 
     248           0 :     return nRet == 0;
     249             : }
     250             : 
     251           0 : bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
     252             : {
     253           0 :     Reference< lang::XComponent > xComponent;
     254           0 :     return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
     255         651 : }
     256             : 
     257             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10