LCOV - code coverage report
Current view: top level - writerperfect/source/writer - AbiWordImportFilter.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 56 78 71.8 %
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             : /* AbiWordImportFilter: 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 <libabw/libabw.h>
      28             : 
      29             : #include <libodfgen/libodfgen.hxx>
      30             : 
      31             : #include "common/DocumentHandler.hxx"
      32             : #include "common/WPXSvStream.hxx"
      33             : #include "AbiWordImportFilter.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           2 : sal_Bool SAL_CALL AbiWordImportFilter::importImpl( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
      59             : throw (RuntimeException)
      60             : {
      61             :     SAL_INFO("writerperfect", "AbiWordImportFilter::importImpl");
      62             : 
      63           2 :     sal_Int32 nLength = aDescriptor.getLength();
      64           2 :     const PropertyValue *pValue = aDescriptor.getConstArray();
      65           2 :     Reference < XInputStream > xInputStream;
      66          10 :     for ( sal_Int32 i = 0 ; i < nLength; i++)
      67             :     {
      68           8 :         if ( pValue[i].Name == "InputStream" )
      69           2 :             pValue[i].Value >>= xInputStream;
      70             :     }
      71           2 :     if ( !xInputStream.is() )
      72             :     {
      73             :         OSL_ASSERT( false );
      74           0 :         return sal_False;
      75             :     }
      76             : 
      77             :     // An XML import service: what we push sax messages to..
      78             :     Reference < XDocumentHandler > xInternalHandler(
      79           4 :         mxContext->getServiceManager()->createInstanceWithContext(
      80           2 :             "com.sun.star.comp.Writer.XMLOasisImporter", mxContext),
      81           4 :         css::uno::UNO_QUERY_THROW);
      82             : 
      83             :     // The XImporter sets up an empty target document for XDocumentHandler to write to..
      84           4 :     Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
      85           2 :     xImporter->setTargetDocument(mxDoc);
      86             : 
      87             :     // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
      88             :     // writes to in-memory target doc
      89           4 :     DocumentHandler xHandler(xInternalHandler);
      90             : 
      91           4 :     WPXSvInputStream input( xInputStream );
      92             : 
      93           4 :     OdtGenerator collector(&xHandler, ODF_FLAT_XML);
      94           2 :     if (libabw::AbiDocument::parse(&input, &collector))
      95           2 :         return sal_True;
      96           2 :     return sal_False;
      97             : }
      98             : 
      99           2 : sal_Bool SAL_CALL AbiWordImportFilter::filter( const Sequence< ::com::sun::star::beans::PropertyValue >& aDescriptor )
     100             : throw (RuntimeException, std::exception)
     101             : {
     102             :     SAL_INFO("writerperfect", "AbiWordImportFilter::filter");
     103           2 :     return importImpl ( aDescriptor );
     104             : }
     105           0 : void SAL_CALL AbiWordImportFilter::cancel(  )
     106             : throw (RuntimeException, std::exception)
     107             : {
     108             :     SAL_INFO("writerperfect", "AbiWordImportFilter::cancel");
     109           0 : }
     110             : 
     111             : // XImporter
     112           2 : void SAL_CALL AbiWordImportFilter::setTargetDocument( const Reference< ::com::sun::star::lang::XComponent >& xDoc )
     113             : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
     114             : {
     115             :     SAL_INFO("writerperfect", "AbiWordImportFilter::getTargetDocument");
     116           2 :     mxDoc = xDoc;
     117           2 : }
     118             : 
     119             : // XExtendedFilterDetection
     120           6 : OUString SAL_CALL AbiWordImportFilter::detect( com::sun::star::uno::Sequence< PropertyValue >& Descriptor )
     121             : throw( com::sun::star::uno::RuntimeException, std::exception )
     122             : {
     123             :     SAL_INFO("writerperfect", "AbiWordImportFilter::detect");
     124             : 
     125           6 :     OUString sTypeName;
     126           6 :     sal_Int32 nLength = Descriptor.getLength();
     127           6 :     sal_Int32 location = nLength;
     128           6 :     const PropertyValue *pValue = Descriptor.getConstArray();
     129          12 :     Reference < XInputStream > xInputStream;
     130          42 :     for ( sal_Int32 i = 0 ; i < nLength; i++)
     131             :     {
     132          36 :         if ( pValue[i].Name == "TypeName" )
     133           4 :             location=i;
     134          32 :         else if ( pValue[i].Name == "InputStream" )
     135           6 :             pValue[i].Value >>= xInputStream;
     136             :     }
     137             : 
     138           6 :     if (!xInputStream.is())
     139           0 :         return OUString();
     140             : 
     141          12 :     WPXSvInputStream input( xInputStream );
     142             : 
     143           6 :     if (libabw::AbiDocument::isFileFormatSupported(&input))
     144           2 :         sTypeName = "writer_AbiWord_Document";
     145             : 
     146           6 :     if (!sTypeName.isEmpty())
     147             :     {
     148           2 :         if ( location == nLength )
     149             :         {
     150           2 :             Descriptor.realloc(nLength+1);
     151           2 :             Descriptor[location].Name = "TypeName";
     152             :         }
     153             : 
     154           2 :         Descriptor[location].Value <<=sTypeName;
     155             :     }
     156             : 
     157          12 :     return sTypeName;
     158             : }
     159             : 
     160             : 
     161             : // XInitialization
     162           0 : void SAL_CALL AbiWordImportFilter::initialize( const Sequence< Any >& aArguments )
     163             : throw (Exception, RuntimeException, std::exception)
     164             : {
     165             :     SAL_INFO("writerperfect", "AbiWordImportFilter::initialize");
     166           0 :     Sequence < PropertyValue > aAnySeq;
     167           0 :     sal_Int32 nLength = aArguments.getLength();
     168           0 :     if ( nLength && ( aArguments[0] >>= aAnySeq ) )
     169             :     {
     170           0 :         const PropertyValue *pValue = aAnySeq.getConstArray();
     171           0 :         nLength = aAnySeq.getLength();
     172           0 :         for ( sal_Int32 i = 0 ; i < nLength; i++)
     173             :         {
     174           0 :             if ( pValue[i].Name == "Type" )
     175             :             {
     176           0 :                 pValue[i].Value >>= msFilterName;
     177           0 :                 break;
     178             :             }
     179             :         }
     180           0 :     }
     181           0 : }
     182          11 : OUString AbiWordImportFilter_getImplementationName ()
     183             : throw (RuntimeException)
     184             : {
     185          11 :     return OUString (  "com.sun.star.comp.Writer.AbiWordImportFilter"  );
     186             : }
     187             : 
     188           3 : Sequence< OUString > SAL_CALL AbiWordImportFilter_getSupportedServiceNames(  )
     189             : throw (RuntimeException)
     190             : {
     191           3 :     Sequence < OUString > aRet(2);
     192           3 :     OUString *pArray = aRet.getArray();
     193           3 :     pArray[0] =  "com.sun.star.document.ImportFilter";
     194           3 :     pArray[1] =  "com.sun.star.document.ExtendedTypeDetection";
     195           3 :     return aRet;
     196             : }
     197             : #undef SERVICE_NAME2
     198             : #undef SERVICE_NAME1
     199             : 
     200           5 : Reference< XInterface > SAL_CALL AbiWordImportFilter_createInstance( const Reference< XComponentContext > & rContext)
     201             : throw( Exception )
     202             : {
     203           5 :     return (cppu::OWeakObject *) new AbiWordImportFilter( rContext );
     204             : }
     205             : 
     206             : // XServiceInfo
     207           0 : OUString SAL_CALL AbiWordImportFilter::getImplementationName(  )
     208             : throw (RuntimeException, std::exception)
     209             : {
     210           0 :     return AbiWordImportFilter_getImplementationName();
     211             : }
     212           0 : sal_Bool SAL_CALL AbiWordImportFilter::supportsService( const OUString &rServiceName )
     213             : throw (RuntimeException, std::exception)
     214             : {
     215           0 :     return cppu::supportsService( this, rServiceName );
     216             : }
     217           0 : Sequence< OUString > SAL_CALL AbiWordImportFilter::getSupportedServiceNames(  )
     218             : throw (RuntimeException, std::exception)
     219             : {
     220           0 :     return AbiWordImportFilter_getSupportedServiceNames();
     221           9 : }
     222             : 
     223             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10