LCOV - code coverage report
Current view: top level - writerperfect/inc - ImportFilter.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 48 64 75.0 %
Date: 2015-06-13 12:38:46 Functions: 31 48 64.6 %
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 Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       5             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       6             :  */
       7             : 
       8             : #ifndef INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
       9             : #define INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
      10             : 
      11             : #include <libodfgen/libodfgen.hxx>
      12             : 
      13             : #include <librevenge/librevenge.h>
      14             : 
      15             : #include <librevenge-stream/librevenge-stream.h>
      16             : 
      17             : #include <com/sun/star/beans/PropertyValue.hpp>
      18             : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
      19             : #include <com/sun/star/document/XFilter.hpp>
      20             : #include <com/sun/star/document/XImporter.hpp>
      21             : #include <com/sun/star/io/XInputStream.hpp>
      22             : #include <com/sun/star/io/XSeekable.hpp>
      23             : #include <com/sun/star/lang/XInitialization.hpp>
      24             : #include <com/sun/star/lang/XServiceInfo.hpp>
      25             : #include <com/sun/star/uno/Reference.h>
      26             : #include <com/sun/star/uno/XComponentContext.hpp>
      27             : #include <com/sun/star/xml/sax/InputSource.hpp>
      28             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      29             : 
      30             : #include <osl/diagnose.h>
      31             : #include <cppuhelper/implbase1.hxx>
      32             : #include <cppuhelper/implbase4.hxx>
      33             : 
      34             : #include <unotools/mediadescriptor.hxx>
      35             : 
      36             : #include <DocumentHandler.hxx>
      37             : #include <WPXSvInputStream.hxx>
      38             : 
      39             : #include <xmloff/attrlist.hxx>
      40             : 
      41             : #include "DocumentHandlerFor.hxx"
      42             : 
      43             : namespace writerperfect
      44             : {
      45             : 
      46             : namespace detail
      47             : {
      48             : 
      49             : template<class Generator>
      50             : class ImportFilterImpl : public cppu::WeakImplHelper4
      51             :     <
      52             :     com::sun::star::document::XFilter,
      53             :     com::sun::star::document::XImporter,
      54             :     com::sun::star::document::XExtendedFilterDetection,
      55             :     com::sun::star::lang::XInitialization
      56             :     >
      57             : {
      58             : public:
      59        5585 :     ImportFilterImpl(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > &rxContext)
      60        5585 :         : mxContext(rxContext)
      61             :     {
      62        5585 :     }
      63             : 
      64        5585 :     virtual ~ImportFilterImpl()
      65             :     {
      66        5585 :     }
      67             : 
      68             :     // XFilter
      69         114 :     virtual sal_Bool SAL_CALL filter(const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > &rDescriptor)
      70             :     throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
      71             :     {
      72         114 :         utl::MediaDescriptor aDescriptor(rDescriptor);
      73         228 :         css::uno::Reference < css::io::XInputStream > xInputStream;
      74         114 :         aDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
      75         114 :         if (!xInputStream.is())
      76             :         {
      77             :             OSL_ASSERT(false);
      78           0 :             return sal_False;
      79             :         }
      80             : 
      81             :         // An XML import service: what we push sax messages to..
      82             :         css::uno::Reference < css::xml::sax::XDocumentHandler > xInternalHandler(
      83         114 :             mxContext->getServiceManager()->createInstanceWithContext(
      84             :                 DocumentHandlerFor<Generator>::name(), mxContext),
      85         228 :             css::uno::UNO_QUERY_THROW);
      86             : 
      87             :         // The XImporter sets up an empty target document for XDocumentHandler to write to..
      88         228 :         css::uno::Reference < css::document::XImporter > xImporter(xInternalHandler, css::uno::UNO_QUERY);
      89         114 :         xImporter->setTargetDocument(mxDoc);
      90             : 
      91             :         // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
      92             :         // writes to in-memory target doc
      93         228 :         DocumentHandler xHandler(xInternalHandler);
      94             : 
      95         228 :         WPXSvInputStream input(xInputStream);
      96             : 
      97         228 :         Generator exporter;
      98         114 :         exporter.addDocumentHandler(&xHandler, ODF_FLAT_XML);
      99             : 
     100         114 :         this->doRegisterHandlers(exporter);
     101             : 
     102         228 :         return this->doImportDocument(input, exporter, aDescriptor);
     103             :     }
     104             : 
     105           0 :     virtual void SAL_CALL cancel()
     106             :     throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     107             :     {
     108           0 :     }
     109             : 
     110             :     // XImporter
     111         116 :     virtual void SAL_CALL setTargetDocument(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &xDoc)
     112             :     throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     113             :     {
     114         116 :         mxDoc = xDoc;
     115         116 :     }
     116             : 
     117             :     //XExtendedFilterDetection
     118        5574 :     virtual OUString SAL_CALL detect(com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue > &Descriptor)
     119             :     throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     120             :     {
     121        5574 :         OUString sTypeName;
     122        5574 :         sal_Int32 nLength = Descriptor.getLength();
     123        5574 :         sal_Int32 location = nLength;
     124        5574 :         const css::beans::PropertyValue *pValue = Descriptor.getConstArray();
     125       11148 :         css::uno::Reference < css::io::XInputStream > xInputStream;
     126       49389 :         for (sal_Int32 i = 0 ; i < nLength; i++)
     127             :         {
     128       43815 :             if (pValue[i].Name == "TypeName")
     129        5463 :                 location=i;
     130       38352 :             else if (pValue[i].Name == "InputStream")
     131        5574 :                 pValue[i].Value >>= xInputStream;
     132             :         }
     133             : 
     134        5574 :         if (!xInputStream.is())
     135           0 :             return OUString();
     136             : 
     137       11148 :         WPXSvInputStream input(xInputStream);
     138             : 
     139        5574 :         if (this->doDetectFormat(input, sTypeName))
     140             :         {
     141             :             assert(!sTypeName.isEmpty());
     142             : 
     143         110 :             if (location == nLength)
     144             :             {
     145         110 :                 Descriptor.realloc(nLength+1);
     146         110 :                 Descriptor[location].Name = "TypeName";
     147             :             }
     148             : 
     149         110 :             Descriptor[location].Value <<=sTypeName;
     150             :         }
     151             : 
     152       11148 :         return sTypeName;
     153             :     }
     154             : 
     155             :     // XInitialization
     156           0 :     virtual void SAL_CALL initialize(const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > &aArguments)
     157             :     throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     158             :     {
     159           0 :         css::uno::Sequence < css::beans::PropertyValue > aAnySeq;
     160           0 :         sal_Int32 nLength = aArguments.getLength();
     161           0 :         if (nLength && (aArguments[0] >>= aAnySeq))
     162             :         {
     163           0 :             const css::beans::PropertyValue *pValue = aAnySeq.getConstArray();
     164           0 :             nLength = aAnySeq.getLength();
     165           0 :             for (sal_Int32 i = 0 ; i < nLength; i++)
     166             :             {
     167           0 :                 if (pValue[i].Name == "Type")
     168             :                 {
     169           0 :                     pValue[i].Value >>= msFilterName;
     170           0 :                     break;
     171             :                 }
     172             :             }
     173           0 :         }
     174           0 :     }
     175             : 
     176             : private:
     177             :     virtual bool doDetectFormat(librevenge::RVNGInputStream &rInput, OUString &rTypeName) = 0;
     178             :     virtual bool doImportDocument(librevenge::RVNGInputStream &rInput, Generator &rGenerator, utl::MediaDescriptor &rDescriptor) = 0;
     179          23 :     virtual void doRegisterHandlers(Generator &)
     180             :     {
     181          23 :     }
     182             : 
     183             : private:
     184             :     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > mxContext;
     185             :     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > mxDoc;
     186             :     OUString msFilterName;
     187             :     ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler > mxHandler;
     188             : };
     189             : 
     190             : }
     191             : 
     192             : /** A base class for import filters.
     193             :  */
     194             : template<class Generator>
     195        5585 : struct ImportFilter : public cppu::ImplInheritanceHelper1<detail::ImportFilterImpl<Generator>, com::sun::star::lang::XServiceInfo>
     196             : {
     197        5585 :     ImportFilter(const css::uno::Reference<css::uno::XComponentContext> &rxContext)
     198        5585 :         : cppu::ImplInheritanceHelper1<detail::ImportFilterImpl<Generator>, com::sun::star::lang::XServiceInfo>(rxContext)
     199             :     {
     200        5585 :     }
     201             : };
     202             : 
     203             : }
     204             : 
     205             : #endif // INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
     206             : 
     207             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11