LCOV - code coverage report
Current view: top level - writerperfect/source/writer - EBookImportFilter.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 72 103 69.9 %
Date: 2014-04-11 Functions: 9 14 64.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /* EBookImportFilter: Sets up the filter, and calls DocumentCollector
       3             :  * to do the actual filtering
       4             :  *
       5             :  * This file is part of the LibreOffice project.
       6             :  *
       7             :  * This Source Code Form is subject to the terms of the Mozilla Public
       8             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       9             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      10             :  */
      11             : 
      12             : #include <osl/diagnose.h>
      13             : #include <rtl/tencinfo.h>
      14             : 
      15             : #include <com/sun/star/io/XInputStream.hpp>
      16             : #include <com/sun/star/xml/sax/XAttributeList.hpp>
      17             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      18             : #include <com/sun/star/xml/sax/InputSource.hpp>
      19             : #include <com/sun/star/xml/sax/XParser.hpp>
      20             : #include <com/sun/star/io/XSeekable.hpp>
      21             : #include <com/sun/star/uno/Reference.h>
      22             : #include <cppuhelper/supportsservice.hxx>
      23             : 
      24             : #include <xmloff/attrlist.hxx>
      25             : #include <ucbhelper/content.hxx>
      26             : 
      27             : #include <libe-book/libe-book.h>
      28             : 
      29             : #include <libodfgen/libodfgen.hxx>
      30             : 
      31             : #include "common/DocumentHandler.hxx"
      32             : #include "common/WPXSvStream.hxx"
      33             : #include "EBookImportFilter.hxx"
      34             : 
      35             : #include <iostream>
      36             : 
      37             : using namespace ::com::sun::star::uno;
      38             : using com::sun::star::uno::Sequence;
      39             : using com::sun::star::uno::Reference;
      40             : using com::sun::star::uno::Any;
      41             : using com::sun::star::uno::UNO_QUERY;
      42             : using com::sun::star::uno::XInterface;
      43             : using com::sun::star::uno::Exception;
      44             : using com::sun::star::uno::RuntimeException;
      45             : using com::sun::star::beans::PropertyValue;
      46             : using com::sun::star::document::XFilter;
      47             : using com::sun::star::document::XExtendedFilterDetection;
      48             : using com::sun::star::ucb::XCommandEnvironment;
      49             : 
      50             : using com::sun::star::io::XInputStream;
      51             : using com::sun::star::document::XImporter;
      52             : using com::sun::star::xml::sax::InputSource;
      53             : using com::sun::star::xml::sax::XAttributeList;
      54             : using com::sun::star::xml::sax::XDocumentHandler;
      55             : using com::sun::star::xml::sax::XParser;
      56             : 
      57             : 
      58           4 : sal_Bool SAL_CALL EBookImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
      59             : throw (RuntimeException)
      60             : {
      61             :     SAL_INFO("writerperfect", "EBookImportFilter::importImpl");
      62             : 
      63           4 :     sal_Int32 nLength = aDescriptor.getLength();
      64           4 :     const PropertyValue *pValue = aDescriptor.getConstArray();
      65           4 :     Reference < XInputStream > xInputStream;
      66           8 :     rtl::OUString sFilterName;
      67          20 :     for ( sal_Int32 i = 0 ; i < nLength; i++)
      68             :     {
      69          16 :         if ( pValue[i].Name == "FilterName" )
      70           4 :             pValue[i].Value >>= sFilterName;
      71          12 :         else if ( pValue[i].Name == "InputStream" )
      72           4 :             pValue[i].Value >>= xInputStream;
      73             :     }
      74           4 :     if ( sFilterName.isEmpty() )
      75             :     {
      76             :         OSL_ASSERT( false );
      77           0 :         return sal_False;
      78             :     }
      79           4 :     if ( !xInputStream.is() )
      80             :     {
      81             :         OSL_ASSERT( false );
      82           0 :         return sal_False;
      83             :     }
      84             : 
      85             :     // An XML import service: what we push sax messages to..
      86             :     Reference < XDocumentHandler > xInternalHandler(
      87           8 :         mxContext->getServiceManager()->createInstanceWithContext(
      88           4 :             "com.sun.star.comp.Writer.XMLOasisImporter", mxContext),
      89           8 :         css::uno::UNO_QUERY_THROW);
      90             : 
      91             :     // The XImporter sets up an empty target document for XDocumentHandler to write to..
      92           8 :     Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
      93           4 :     xImporter->setTargetDocument(mxDoc);
      94             : 
      95             :     // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
      96             :     // writes to in-memory target doc
      97           8 :     DocumentHandler xHandler(xInternalHandler);
      98             : 
      99           8 :     WPXSvInputStream input( xInputStream );
     100             : 
     101           8 :     OdtGenerator collector(&xHandler, ODF_FLAT_XML);
     102             : 
     103           4 :     if (sFilterName == "FictionBook 2")
     104           2 :         return libebook::FB2Document::parse(&input, &collector);
     105           2 :     else if (sFilterName == "PalmDoc")
     106           1 :         return libebook::PDBDocument::parse(&input, &collector);
     107           1 :     else if (sFilterName == "Plucker eBook")
     108           0 :         return libebook::PLKRDocument::parse(&input, &collector);
     109           1 :     else if (sFilterName == "eReader eBook")
     110           1 :         return libebook::PMLDocument::parse(&input, &collector);
     111           0 :     else if (sFilterName == "TealDoc")
     112           0 :         return libebook::TDDocument::parse(&input, &collector);
     113           0 :     else if (sFilterName == "zTXT")
     114           0 :         return libebook::ZTXTDocument::parse(&input, &collector);
     115             : 
     116           4 :     return sal_False;
     117             : }
     118             : 
     119           4 : sal_Bool SAL_CALL EBookImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
     120             : throw (RuntimeException, std::exception)
     121             : {
     122             :     SAL_INFO("writerperfect", "EBookImportFilter::filter");
     123           4 :     return importImpl ( aDescriptor );
     124             : }
     125           0 : void SAL_CALL EBookImportFilter::cancel(  )
     126             : throw (RuntimeException, std::exception)
     127             : {
     128             :     SAL_INFO("writerperfect", "EBookImportFilter::cancel");
     129           0 : }
     130             : 
     131             : // XImporter
     132           5 : void SAL_CALL EBookImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
     133             : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
     134             : {
     135             :     SAL_INFO("writerperfect", "EBookImportFilter::getTargetDocument");
     136           5 :     mxDoc = xDoc;
     137           5 : }
     138             : 
     139             : // XExtendedFilterDetection
     140          33 : OUString SAL_CALL EBookImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
     141             : throw( com::sun::star::uno::RuntimeException, std::exception )
     142             : {
     143             :     SAL_INFO("writerperfect", "EBookImportFilter::detect");
     144             : 
     145          33 :     OUString sTypeName;
     146          33 :     sal_Int32 nLength = Descriptor.getLength();
     147          33 :     sal_Int32 location = nLength;
     148          33 :     const PropertyValue *pValue = Descriptor.getConstArray();
     149          66 :     Reference < XInputStream > xInputStream;
     150         267 :     for ( sal_Int32 i = 0 ; i < nLength; i++)
     151             :     {
     152         234 :         if ( pValue[i].Name == "TypeName" )
     153          28 :             location=i;
     154         206 :         else if ( pValue[i].Name == "InputStream" )
     155          33 :             pValue[i].Value >>= xInputStream;
     156             :     }
     157             : 
     158          33 :     if (!xInputStream.is())
     159           0 :         return OUString();
     160             : 
     161          66 :     WPXSvInputStream input( xInputStream );
     162             : 
     163          33 :     if (libebook::FB2Document::isSupported(&input))
     164           2 :         sTypeName = "writer_FictionBook_2";
     165          31 :     else if (libebook::PDBDocument::isSupported(&input))
     166           1 :         sTypeName = "writer_PalmDoc";
     167          30 :     else if (libebook::PLKRDocument::isSupported(&input))
     168           0 :         sTypeName = "writer_Plucker_eBook";
     169          30 :     else if (libebook::PMLDocument::isSupported(&input))
     170           1 :         sTypeName = "writer_eReader_eBook";
     171          29 :     else if (libebook::TDDocument::isSupported(&input))
     172           0 :         sTypeName = "writer_TealDoc";
     173          29 :     else if (libebook::ZTXTDocument::isSupported(&input))
     174           0 :         sTypeName = "writer_zTXT";
     175             : 
     176          33 :     if (!sTypeName.isEmpty())
     177             :     {
     178           4 :         if ( location == nLength )
     179             :         {
     180           4 :             Descriptor.realloc(nLength+1);
     181           4 :             Descriptor[location].Name = "TypeName";
     182             :         }
     183             : 
     184           4 :         Descriptor[location].Value <<=sTypeName;
     185             :     }
     186             : 
     187          66 :     return sTypeName;
     188             : }
     189             : 
     190             : 
     191             : // XInitialization
     192           0 : void SAL_CALL EBookImportFilter::initialize( const Sequence< Any >& aArguments )
     193             : throw (Exception, RuntimeException, std::exception)
     194             : {
     195             :     SAL_INFO("writerperfect", "EBookImportFilter::initialize");
     196           0 :     Sequence < PropertyValue > aAnySeq;
     197           0 :     sal_Int32 nLength = aArguments.getLength();
     198           0 :     if ( nLength && ( aArguments[0] >>= aAnySeq ) )
     199             :     {
     200           0 :         const PropertyValue *pValue = aAnySeq.getConstArray();
     201           0 :         nLength = aAnySeq.getLength();
     202           0 :         for ( sal_Int32 i = 0 ; i < nLength; i++)
     203             :         {
     204           0 :             if ( pValue[i].Name == "Type" )
     205             :             {
     206           0 :                 pValue[i].Value >>= msFilterName;
     207           0 :                 break;
     208             :             }
     209             :         }
     210           0 :     }
     211           0 : }
     212          11 : OUString EBookImportFilter_getImplementationName ()
     213             : throw (RuntimeException)
     214             : {
     215          11 :     return OUString (  "org.libreoffice.comp.Writer.EBookImportFilter"  );
     216             : }
     217             : 
     218           3 : Sequence< OUString > SAL_CALL EBookImportFilter_getSupportedServiceNames(  )
     219             : throw (RuntimeException)
     220             : {
     221           3 :     Sequence < OUString > aRet(2);
     222           3 :     OUString *pArray = aRet.getArray();
     223           3 :     pArray[0] =  "com.sun.star.document.ImportFilter";
     224           3 :     pArray[1] =  "com.sun.star.document.ExtendedTypeDetection";
     225           3 :     return aRet;
     226             : }
     227             : #undef SERVICE_NAME2
     228             : #undef SERVICE_NAME1
     229             : 
     230          29 : Reference< XInterface > SAL_CALL EBookImportFilter_createInstance( const Reference< XComponentContext > & rContext)
     231             : throw( Exception )
     232             : {
     233          29 :     return (cppu::OWeakObject *) new EBookImportFilter( rContext );
     234             : }
     235             : 
     236             : // XServiceInfo
     237           0 : OUString SAL_CALL EBookImportFilter::getImplementationName(  )
     238             : throw (RuntimeException, std::exception)
     239             : {
     240           0 :     return EBookImportFilter_getImplementationName();
     241             : }
     242           0 : sal_Bool SAL_CALL EBookImportFilter::supportsService( const OUString &rServiceName )
     243             : throw (RuntimeException, std::exception)
     244             : {
     245           0 :     return cppu::supportsService( this, rServiceName );
     246             : }
     247           0 : Sequence< OUString > SAL_CALL EBookImportFilter::getSupportedServiceNames(  )
     248             : throw (RuntimeException, std::exception)
     249             : {
     250           0 :     return EBookImportFilter_getSupportedServiceNames();
     251           9 : }
     252             : 
     253             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10