LCOV - code coverage report
Current view: top level - filter/source/xmlfilteradaptor - XmlFilterAdaptor.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 122 162 75.3 %
Date: 2015-06-13 12:38:46 Functions: 13 15 86.7 %
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 <iostream>
      21             : #include <stdlib.h>
      22             : #include <ctype.h>
      23             : #include <rtl/ustring.hxx>
      24             : #include <tools/urlobj.hxx>
      25             : #include "XmlFilterAdaptor.hxx"
      26             : #include <osl/diagnose.h>
      27             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      28             : #include <com/sun/star/uno/RuntimeException.hpp>
      29             : #include <com/sun/star/io/XActiveDataSource.hpp>
      30             : #include <com/sun/star/io/XOutputStream.hpp>
      31             : #include <com/sun/star/io/XInputStream.hpp>
      32             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      33             : #include <com/sun/star/xml/sax/InputSource.hpp>
      34             : #include <com/sun/star/xml/sax/Parser.hpp>
      35             : #include <com/sun/star/frame/XConfigManager.hpp>
      36             : #include <com/sun/star/xml/XImportFilter.hpp>
      37             : #include <com/sun/star/xml/XExportFilter.hpp>
      38             : #include <com/sun/star/frame/XController.hpp>
      39             : #include <com/sun/star/task/XStatusIndicator.hpp>
      40             : #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
      41             : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
      42             : #include <com/sun/star/style/XStyleLoader.hpp>
      43             : #include <com/sun/star/container/XNameAccess.hpp>
      44             : #include <comphelper/processfactory.hxx>
      45             : #include <comphelper/sequenceashashmap.hxx>
      46             : #include <unotools/mediadescriptor.hxx>
      47             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      48             : #include <comphelper/genericpropertyset.hxx>
      49             : #include <comphelper/propertysetinfo.hxx>
      50             : #include <cppuhelper/supportsservice.hxx>
      51             : #include <unotools/pathoptions.hxx>
      52             : 
      53             : using namespace comphelper;
      54             : using namespace com::sun::star::uno;
      55             : using namespace com::sun::star::lang;
      56             : using namespace com::sun::star::io;
      57             : using namespace com::sun::star::beans;
      58             : using namespace com::sun::star::container;
      59             : using namespace com::sun::star::document;
      60             : using namespace com::sun::star::style;
      61             : using namespace com::sun::star::xml;
      62             : using namespace com::sun::star::xml::sax;
      63             : using namespace ::com::sun::star::frame;
      64             : using namespace ::com::sun::star::task;
      65             : 
      66          15 : bool SAL_CALL XmlFilterAdaptor::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
      67             :     throw (RuntimeException)
      68             : {
      69          15 :     OUString udConvertClass=msUserData[0];
      70          30 :     OUString udImport =msUserData[2];
      71          15 :     sal_Int32 nSteps= 0;
      72          15 :     sal_Int32 nProgressRange = 4;
      73             : 
      74          30 :     utl::MediaDescriptor aMediaMap(aDescriptor);
      75             :     Reference< XStatusIndicator > xStatusIndicator(aMediaMap.getUnpackedValueOrDefault(
      76          30 :         utl::MediaDescriptor::PROP_STATUSINDICATOR(), Reference< XStatusIndicator >()));
      77             : 
      78          15 :     if (xStatusIndicator.is()){
      79          13 :         xStatusIndicator->start(OUString( "Loading :" ),nProgressRange);
      80             :     }
      81             : 
      82          30 :     OUString sXMLImportService (  udImport  );
      83          30 :     Reference < XParser > xSaxParser = Parser::create( mxContext );
      84             : 
      85          30 :     Sequence< Any > aAnys(1);
      86          30 :     OUString aBaseURI;
      87          15 :     if (aMediaMap.find(OUString( "URL" ))->second >>= aBaseURI)
      88             :     {
      89          15 :         INetURLObject aURLObj(aBaseURI);
      90             :         // base URI in this case is the URI of the actual saving location
      91             :         // aURLObj.removeSegment();
      92          15 :         aBaseURI = aURLObj.GetMainURL(INetURLObject::NO_DECODE);
      93             :     }
      94             : 
      95             :     // create an XProperty set to configure the exporter for pretty printing
      96             :     PropertyMapEntry aImportInfoMap[] =
      97             :      {
      98          15 :         { OUString("BaseURI"), 0, ::cppu::UnoType<OUString>::get(), PropertyAttribute::MAYBEVOID, 0},
      99             :          { OUString(), 0, css::uno::Type(), 0, 0 }
     100          45 :      };
     101             : 
     102             :      Reference< XPropertySet > xInfoSet(
     103          30 :         GenericPropertySet_CreateInstance( new PropertySetInfo( aImportInfoMap ) ) );
     104          15 :      xInfoSet->setPropertyValue(
     105          15 :         OUString( "BaseURI" ), makeAny( aBaseURI ));
     106          15 :     aAnys[0] <<= xInfoSet;
     107             : 
     108             : 
     109          30 :     Reference < XDocumentHandler > xHandler( mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( sXMLImportService, aAnys, mxContext ), UNO_QUERY );
     110          15 :     if(! xHandler.is()) {
     111             :         OSL_FAIL("XMLReader::Read: %s Unable to create service instance xHandler\n" );
     112           0 :         return false;
     113             :     }
     114          30 :     Reference < XImporter > xImporter( xHandler, UNO_QUERY );
     115          15 :     xImporter->setTargetDocument ( mxDoc );
     116             : 
     117          15 :     if (xStatusIndicator.is()){
     118          13 :         xStatusIndicator->setValue(nSteps++);
     119             :     }
     120             : 
     121             : 
     122             :     // Creating a ConverterBridge instance
     123             : 
     124          30 :     Reference< XInterface > xConvBridge(mxContext->getServiceManager()->createInstanceWithContext(udConvertClass, mxContext), UNO_QUERY);
     125          15 :     if(! xConvBridge.is()){
     126             :         OSL_FAIL( "XMLReader::Read: %s service missing\n" );
     127           0 :         return false;
     128             :     }
     129          15 :     if (xStatusIndicator.is())
     130          13 :         xStatusIndicator->setValue(nSteps++);
     131             : 
     132          30 :     Reference< XImportFilter > xConverter( xConvBridge, UNO_QUERY );
     133             : 
     134             : 
     135             :     //Template Loading if Required
     136             : 
     137          15 :     if (!msTemplateName.isEmpty()){
     138           0 :         Reference< XStyleFamiliesSupplier > xstylefamiliessupplier(mxDoc, UNO_QUERY);
     139           0 :         Reference< XStyleLoader > xstyleLoader (xstylefamiliessupplier->getStyleFamilies(), UNO_QUERY);
     140           0 :         if(xstyleLoader.is()){
     141           0 :             Sequence<com::sun::star::beans::PropertyValue> pValue=xstyleLoader->getStyleLoaderOptions();
     142             : 
     143             :             //Load the Styles from the Template URL Supplied in the TypeDetection file
     144           0 :             if(msTemplateName.indexOf(OUString( "file:" ))==-1)
     145             :             {
     146           0 :                 SvtPathOptions aOptions;
     147           0 :                 OUString PathString = aOptions.SubstituteVariable(OUString("$(progurl)"));
     148           0 :                 PathString = PathString.concat(OUString( "/" ));
     149           0 :                 msTemplateName=PathString.concat(msTemplateName);
     150             :             }
     151             : 
     152           0 :             xstyleLoader->loadStylesFromURL(msTemplateName,pValue);
     153           0 :         }
     154             :     }
     155             : 
     156             : //    sal_Bool xconv_ret = sal_True;
     157             : 
     158          15 :     if (xStatusIndicator.is()){
     159          13 :         xStatusIndicator->setValue(nSteps++);
     160             :     }
     161             : 
     162             :     // Calling Filtering Component
     163             : 
     164             :     try {
     165          15 :         if (!xConverter->importer(aDescriptor,xHandler,msUserData)) {
     166           0 :             if (xStatusIndicator.is())
     167           0 :                    xStatusIndicator->end();
     168           0 :             return false;
     169             :         }
     170             :     }
     171             : #if OSL_DEBUG_LEVEL > 0
     172             :     catch( const Exception& e )
     173             : #else
     174           0 :     catch( const Exception& )
     175             : #endif
     176             :     {
     177           0 :         if (xStatusIndicator.is())
     178           0 :                xStatusIndicator->end();
     179             : 
     180             :         OSL_FAIL( OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
     181           0 :         return false;
     182             :     }
     183          15 :     if (xStatusIndicator.is()) {
     184          13 :         xStatusIndicator->setValue(nSteps++);
     185          13 :         xStatusIndicator->end();
     186             :     }
     187          30 :     return true;
     188             : }
     189             : 
     190           2 : bool SAL_CALL XmlFilterAdaptor::exportImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
     191             :     throw (RuntimeException)
     192             : {
     193             : 
     194           2 :     OUString udConvertClass = msUserData[0];
     195           4 :     OUString udExport = msUserData[3];
     196             : 
     197             :     // Status Bar
     198           2 :     sal_Int32 nSteps= 1;
     199           2 :     sal_Int32 nProgressRange(3);
     200           4 :     utl::MediaDescriptor aMediaMap(aDescriptor);
     201             :     Reference< XStatusIndicator > xStatusIndicator(aMediaMap.getUnpackedValueOrDefault(
     202           4 :         utl::MediaDescriptor::PROP_STATUSINDICATOR(), Reference< XStatusIndicator >()));
     203             : 
     204           2 :     if (xStatusIndicator.is())
     205           0 :        xStatusIndicator->start(OUString( "Saving :" ),nProgressRange);
     206             : 
     207             :     // Set up converter bridge.
     208           4 :     Reference< com::sun::star::xml::XExportFilter > xConverter(mxContext->getServiceManager()->createInstanceWithContext( udConvertClass, mxContext ), UNO_QUERY);
     209           2 :     if(! xConverter.is()){
     210             :       OSL_FAIL( "xml export sub service missing" );
     211           0 :       return false;
     212             :     }
     213             : 
     214           2 :     if (xStatusIndicator.is())
     215           0 :         xStatusIndicator->setValue(nSteps++);
     216             : 
     217             :     //put filter component into exporting state
     218           2 :     if (!xConverter->exporter(aDescriptor, msUserData)) {
     219           0 :         if (xStatusIndicator.is())
     220           0 :             xStatusIndicator->end();
     221           0 :         return false;
     222             :     }
     223           2 :     if (xStatusIndicator.is())
     224           0 :         xStatusIndicator->setValue(nSteps++);
     225             : 
     226             :     try{
     227             :         // create the xml exporter service and supply the converter component
     228             :         // which implements the document handler
     229           2 :         Sequence < Any > aAnys (2);
     230           2 :         aAnys[0] <<= xConverter;
     231             : 
     232             : 
     233             :         // pretty printing is confusing for some filters so it is disabled by default
     234             :         bool bPrettyPrint =
     235           2 :             (msUserData.getLength() > 6 && msUserData[6].equalsIgnoreAsciiCase("true"));
     236             : 
     237             :         // export of <text:number> element for <text:list-item> elements are
     238             :         // needed for certain filters.
     239             :         bool bExportTextNumberElementForListItems =
     240           2 :                             ( msUserData.getLength() > 7 &&
     241           2 :                               msUserData[7].equalsIgnoreAsciiCase("true") );
     242             : 
     243             :         // get the base URI, so we can use relative links
     244           4 :         OUString aBaseURI;
     245           2 :         if (aMediaMap.find(OUString( "URL" ))->second >>= aBaseURI)
     246             :         {
     247           2 :             INetURLObject aURLObj(aBaseURI);
     248             :             // base URI in this case is the URI of the actual saving location
     249             :             // aURLObj.removeSegment();
     250           2 :             aBaseURI = aURLObj.GetMainURL(INetURLObject::NO_DECODE);
     251             :         }
     252             : 
     253             :         // create an XProperty set to configure the exporter for pretty printing
     254             :          PropertyMapEntry aImportInfoMap[] =
     255             :          {
     256           2 :              { OUString("UsePrettyPrinting"), 0, cppu::UnoType<sal_Bool>::get(), PropertyAttribute::MAYBEVOID, 0},
     257           2 :              { OUString("ExportTextNumberElement"), 0, cppu::UnoType<sal_Bool>::get(), PropertyAttribute::MAYBEVOID, 0},
     258           2 :              { OUString("BaseURI"), 0, ::cppu::UnoType<OUString>::get(), PropertyAttribute::MAYBEVOID, 0},
     259             :              { OUString(), 0, css::uno::Type(), 0, 0 }
     260          16 :          };
     261             : 
     262             :          Reference< XPropertySet > xInfoSet(
     263           4 :             GenericPropertySet_CreateInstance( new PropertySetInfo( aImportInfoMap ) ) );
     264           2 :          xInfoSet->setPropertyValue(
     265           2 :             OUString( "UsePrettyPrinting" ), makeAny( bPrettyPrint ));
     266           2 :         xInfoSet->setPropertyValue(
     267             :                         OUString( "ExportTextNumberElement" ),
     268           2 :                         makeAny( bExportTextNumberElementForListItems ));
     269           2 :          xInfoSet->setPropertyValue(
     270           2 :             OUString( "BaseURI" ), makeAny( aBaseURI ));
     271           2 :         aAnys[1] <<= xInfoSet;
     272             : 
     273           4 :         Reference< XExporter > xExporter( mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
     274           4 :                        udExport, aAnys, mxContext ), UNO_QUERY_THROW );
     275             : 
     276             :         // attach to source document
     277           2 :         xExporter->setSourceDocument( mxDoc );
     278             : 
     279             :         // get XFilter interface
     280           4 :         Reference< XFilter > xFilter( xExporter, UNO_QUERY_THROW );
     281             : 
     282           2 :         if (xStatusIndicator.is())
     283           0 :             xStatusIndicator->setValue(nSteps++);
     284             : 
     285             :         // call the actual filtering component
     286           2 :         if (!xFilter->filter(aDescriptor))
     287             :         {
     288           0 :             if (xStatusIndicator.is())
     289           0 :                    xStatusIndicator->end();
     290           0 :             return false;
     291           2 :         }
     292             :     }
     293             : #if OSL_DEBUG_LEVEL > 0
     294             :     catch( const Exception& exE )
     295             : #else
     296           0 :     catch( const Exception& )
     297             : #endif
     298             :     {
     299             :         OSL_FAIL( OUStringToOString( exE.Message, RTL_TEXTENCODING_ASCII_US).getStr());
     300           0 :         if (xStatusIndicator.is())
     301           0 :             xStatusIndicator->end();
     302           0 :         return false;
     303             :     }
     304             : 
     305             :     // done
     306           2 :     if (xStatusIndicator.is())
     307           0 :         xStatusIndicator->end();
     308           4 :     return true;
     309             : }
     310             : 
     311          17 : sal_Bool SAL_CALL XmlFilterAdaptor::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
     312             :   throw (RuntimeException, std::exception)
     313             : {
     314          17 :     return meType == FILTER_EXPORT ? exportImpl ( aDescriptor ) : importImpl ( aDescriptor );
     315             : }
     316           0 : void SAL_CALL XmlFilterAdaptor::cancel(  )
     317             :     throw (RuntimeException, std::exception)
     318             : {
     319           0 : }
     320             : // XExporter
     321           2 : void SAL_CALL XmlFilterAdaptor::setSourceDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
     322             :     throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
     323             : {
     324           2 :     meType = FILTER_EXPORT;
     325           2 :     mxDoc = xDoc;
     326           2 : }
     327             : 
     328             : // XImporter
     329          15 : void SAL_CALL XmlFilterAdaptor::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
     330             :     throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
     331             : {
     332          15 :     meType = FILTER_IMPORT;
     333          15 :     mxDoc = xDoc;
     334          15 : }
     335             : // XInitialization
     336          17 : void SAL_CALL XmlFilterAdaptor::initialize( const Sequence< Any >& aArguments )
     337             :     throw (Exception, RuntimeException, std::exception)
     338             : {
     339          17 :     Sequence < PropertyValue > aAnySeq;
     340          17 :     sal_Int32 nLength = aArguments.getLength();
     341          17 :     if ( nLength && ( aArguments[0] >>= aAnySeq ) )
     342             :     {
     343          17 :         comphelper::SequenceAsHashMap aMap(aAnySeq);
     344          34 :         msFilterName = aMap.getUnpackedValueOrDefault(
     345          17 :             OUString( "Type" ), OUString());
     346          34 :         msUserData = aMap.getUnpackedValueOrDefault(
     347          17 :             OUString( "UserData" ), Sequence< OUString >());
     348          34 :         msTemplateName = aMap.getUnpackedValueOrDefault(
     349          34 :             OUString( "TemplateName" ), OUString());
     350          17 :     }
     351          17 : }
     352          10 : OUString XmlFilterAdaptor_getImplementationName ()
     353             :     throw (RuntimeException)
     354             : {
     355          10 :     return OUString( "com.sun.star.comp.Writer.XmlFilterAdaptor" );
     356             : }
     357             : 
     358          10 : Sequence< OUString > SAL_CALL XmlFilterAdaptor_getSupportedServiceNames(  )
     359             :     throw (RuntimeException)
     360             : {
     361          10 :     Sequence < OUString > aRet(2);
     362          10 :     OUString* pArray = aRet.getArray();
     363          10 :     pArray[0] =  "com.sun.star.document.ExportFilter";
     364          10 :     pArray[1] =  "com.sun.star.document.ImportFilter";
     365          10 :     return aRet;
     366             : }
     367             : 
     368          18 : Reference< XInterface > SAL_CALL XmlFilterAdaptor_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
     369             :     throw( Exception )
     370             : {
     371          18 :     return static_cast<cppu::OWeakObject*>(new XmlFilterAdaptor( comphelper::getComponentContext(rSMgr) ));
     372             : }
     373             : 
     374             : // XServiceInfo
     375           1 : OUString SAL_CALL XmlFilterAdaptor::getImplementationName(  )
     376             :     throw (RuntimeException, std::exception)
     377             : {
     378           1 :     return XmlFilterAdaptor_getImplementationName();
     379             : }
     380             : 
     381           0 : sal_Bool SAL_CALL XmlFilterAdaptor::supportsService( const OUString& rServiceName )
     382             :     throw (RuntimeException, std::exception)
     383             : {
     384           0 :     return cppu::supportsService( this, rServiceName );
     385             : }
     386             : 
     387           1 : Sequence< OUString > SAL_CALL XmlFilterAdaptor::getSupportedServiceNames(  )
     388             :     throw (RuntimeException, std::exception)
     389             : {
     390           1 :     return XmlFilterAdaptor_getSupportedServiceNames();
     391          27 : }
     392             : 
     393             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11