LCOV - code coverage report
Current view: top level - hwpfilter/source - hwpreader.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 62 75 82.7 %
Date: 2015-06-13 12:38:46 Functions: 12 16 75.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             : #ifndef INCLUDED_HWPFILTER_SOURCE_HWPREADER_HXX
      21             : #define INCLUDED_HWPFILTER_SOURCE_HWPREADER_HXX
      22             : #include <errno.h>
      23             : #include <stdio.h>
      24             : #include <string.h>
      25             : #include <sal/alloca.h>
      26             : 
      27             : #include <com/sun/star/lang/XServiceInfo.hpp>
      28             : #include <com/sun/star/lang/XComponent.hpp>
      29             : #include <com/sun/star/io/XInputStream.hpp>
      30             : #include <com/sun/star/document/XFilter.hpp>
      31             : #include <com/sun/star/document/XImporter.hpp>
      32             : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
      33             : 
      34             : #include <com/sun/star/io/XActiveDataSink.hpp>
      35             : #include <com/sun/star/io/XActiveDataControl.hpp>
      36             : #include <com/sun/star/io/XStreamListener.hpp>
      37             : #include <com/sun/star/document/XExtendedFilterDetection.hpp>
      38             : 
      39             : #include <cppuhelper/factory.hxx>
      40             : #include <cppuhelper/implbase1.hxx>
      41             : #include <cppuhelper/implbase2.hxx>
      42             : #include <cppuhelper/implbase4.hxx>
      43             : #include <cppuhelper/supportsservice.hxx>
      44             : #include <cppuhelper/weak.hxx>
      45             : 
      46             : using namespace ::cppu;
      47             : using namespace ::com::sun::star::lang;
      48             : using namespace ::com::sun::star::uno;
      49             : using namespace ::com::sun::star::io;
      50             : using namespace ::com::sun::star::registry;
      51             : using namespace ::com::sun::star::document;
      52             : using namespace ::com::sun::star::beans;
      53             : using namespace ::com::sun::star::xml::sax;
      54             : 
      55             : #include <assert.h>
      56             : 
      57             : #include <unotools/mediadescriptor.hxx>
      58             : 
      59             : #include "hwpfile.h"
      60             : #include "hcode.h"
      61             : #include "hbox.h"
      62             : #include "htags.h"
      63             : #include "hstream.hxx"
      64             : #include "drawdef.h"
      65             : #include "attributes.hxx"
      66             : 
      67             : #define IMPLEMENTATION_NAME "com.sun.comp.hwpimport.HwpImportFilter"
      68             : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
      69             : #define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection"
      70             : #define WRITER_IMPORTER_NAME "com.sun.star.comp.Writer.XMLImporter"
      71             : 
      72             : struct HwpReaderPrivate;
      73             : /**
      74             :  * This class implements the external Parser interface
      75             :  */
      76             : class HwpReader : public WeakImplHelper1<XFilter>
      77             : {
      78             : 
      79             : public:
      80             :     HwpReader();
      81             :     virtual ~HwpReader();
      82             : 
      83             : public:
      84             :     /**
      85             :      * parseStream does Parser-startup initializations
      86             :      */
      87             :     virtual sal_Bool SAL_CALL filter(const Sequence< PropertyValue >& aDescriptor) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      88           0 :     virtual void SAL_CALL cancel() throw(RuntimeException, std::exception) SAL_OVERRIDE {}
      89          86 :     void SAL_CALL setDocumentHandler(Reference< XDocumentHandler > xHandler)
      90             :     {
      91          86 :         m_rxDocumentHandler = xHandler;
      92          86 :     }
      93             : private:
      94             :     Reference< XDocumentHandler > m_rxDocumentHandler;
      95             :     Reference< XAttributeList > rList;
      96             :     AttributeListImpl *pList;
      97             :     HWPFile hwpfile;
      98             :     HwpReaderPrivate *d;
      99             : private:
     100             :     /* -------- Document Parsing --------- */
     101             :     void makeMeta();
     102             :     void makeStyles();
     103             :     void makeDrawMiscStyle(HWPDrawingObject *);
     104             :     void makeAutoStyles();
     105             :     void makeMasterStyles();
     106             :     void makeBody();
     107             : 
     108             :     void makeTextDecls();
     109             : 
     110             :     /* -------- Paragraph Parsing --------- */
     111             :     void parsePara(HWPPara *para, bool bParaStart = false);
     112             :     void make_text_p0(HWPPara *para, bool bParaStart = false);
     113             :     void make_text_p1(HWPPara *para, bool bParaStart = false);
     114             :     void make_text_p3(HWPPara *para, bool bParaStart = false);
     115             : 
     116             :     /* -------- rDocument->characters(x) --------- */
     117             :     void makeChars(hchar_string & rStr);
     118             : 
     119             :     /* -------- Special Char Parsing --------- */
     120             :     void makeFieldCode(hchar_string & rStr, FieldCode *hbox); //6
     121             :     void makeBookmark(Bookmark *hbox);      //6
     122             :     void makeDateFormat(DateCode *hbox);    //7
     123             :     void makeDateCode(DateCode *hbox);      //8
     124             :     void makeTab(Tab *hbox);            //9
     125             :     void makeTable(TxtBox *hbox);
     126             :     void makeTextBox(TxtBox *hbox);
     127             :     void makeFormula(TxtBox *hbox);
     128             :     void makeHyperText(TxtBox *hbox);
     129             :     void makePicture(Picture *hbox);
     130             :     void makePictureDRAW(HWPDrawingObject *drawobj, Picture *hbox);
     131             :     void makeLine(Line *hbox);
     132             :     void makeHidden(Hidden *hbox);
     133             :     void makeFootnote(Footnote *hbox);
     134             :     void makeAutoNum(AutoNum *hbox);
     135             :     void makeShowPageNum();
     136             :     void makeMailMerge(MailMerge *hbox);
     137             :     void makeOutline(Outline *hbox);
     138             : 
     139             :     /* --------- Styles Parsing ------------ */
     140             :     void makePageStyle();
     141             :     void makeColumns(ColumnDef *);
     142             :     void makeTStyle(CharShape *);
     143             :     void makePStyle(ParaShape *);
     144             :     void makeFStyle(FBoxStyle *);
     145             :     void makeCaptionStyle(FBoxStyle *);
     146             :     void makeDrawStyle(HWPDrawingObject *,FBoxStyle *);
     147             :     void makeTableStyle(Table *);
     148             :     void parseCharShape(CharShape *);
     149             :     void parseParaShape(ParaShape *);
     150             :     static char* getTStyleName(int, char *);
     151             :     static char* getPStyleName(int, char *);
     152             : };
     153             : 
     154             : class HwpImportFilter : public WeakImplHelper4< XFilter, XImporter, XServiceInfo, XExtendedFilterDetection >
     155             : {
     156             : public:
     157             :     HwpImportFilter(const Reference< XMultiServiceFactory >& rFact);
     158             :     virtual ~HwpImportFilter();
     159             : 
     160             : public:
     161             :     static Sequence< OUString > getSupportedServiceNames_Static() throw();
     162             :     static OUString getImplementationName_Static() throw();
     163             : 
     164             : public:
     165             :     // XFilter
     166             :     virtual sal_Bool SAL_CALL filter( const Sequence< PropertyValue >& aDescriptor )
     167             :         throw( RuntimeException, std::exception ) SAL_OVERRIDE;
     168             :     virtual void SAL_CALL cancel() throw(RuntimeException, std::exception) SAL_OVERRIDE;
     169             : 
     170             :     // XImporter
     171             :     virtual void SAL_CALL setTargetDocument( const Reference< XComponent >& xDoc)
     172             :         throw( IllegalArgumentException, RuntimeException, std::exception ) SAL_OVERRIDE;
     173             : 
     174             :     // XServiceInfo
     175             :     OUString SAL_CALL getImplementationName() throw (RuntimeException, std::exception) SAL_OVERRIDE;
     176             :     Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     177             :     sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     178             : 
     179             :     //XExtendedFilterDetection
     180             :     virtual OUString SAL_CALL detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rDescriptor ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     181             : 
     182             : public:
     183             :     Reference< XFilter > rFilter;
     184             :     Reference< XImporter > rImporter;
     185             : };
     186             : 
     187          86 : Reference< XInterface > HwpImportFilter_CreateInstance(
     188             :     const Reference< XMultiServiceFactory >& rSMgr ) throw( Exception )
     189             : {
     190          86 :     HwpImportFilter *p = new HwpImportFilter( rSMgr );
     191             : 
     192          86 :     return Reference< XInterface > ( static_cast<OWeakObject*>(p) );
     193             : }
     194             : 
     195           4 : Sequence< OUString > HwpImportFilter::getSupportedServiceNames_Static() throw ()
     196             : {
     197           4 :     Sequence< OUString > aRet(1);
     198           4 :     aRet.getArray()[0] = HwpImportFilter::getImplementationName_Static();
     199           4 :     return aRet;
     200             : }
     201             : 
     202          86 : HwpImportFilter::HwpImportFilter(const Reference< XMultiServiceFactory >& rFact)
     203             : {
     204          86 :     OUString sService( WRITER_IMPORTER_NAME );
     205             :     try {
     206             :         Reference< XDocumentHandler >
     207          86 :             xHandler( rFact->createInstance( sService ), UNO_QUERY );
     208             : 
     209          86 :         HwpReader *p = new HwpReader;
     210          86 :         p->setDocumentHandler( xHandler );
     211             : 
     212         172 :         Reference< XImporter > xImporter = Reference< XImporter >( xHandler, UNO_QUERY );
     213          86 :         rImporter = xImporter;
     214         172 :         Reference< XFilter > xFilter = Reference< XFilter >( p );
     215         172 :         rFilter = xFilter;
     216             :     }
     217           0 :     catch( Exception & )
     218             :     {
     219           0 :         printf(" fail to instanciate %s\n", WRITER_IMPORTER_NAME );
     220           0 :         exit( 1 );
     221          86 :     }
     222          86 : }
     223             : 
     224         172 : HwpImportFilter::~HwpImportFilter()
     225             : {
     226         172 : }
     227             : 
     228           2 : sal_Bool HwpImportFilter::filter( const Sequence< PropertyValue >& aDescriptor )
     229             :     throw( RuntimeException, std::exception )
     230             : {
     231             :     // delegate to IchitaroImpoter
     232           2 :     return rFilter->filter( aDescriptor );
     233             : }
     234             : 
     235           0 : void HwpImportFilter::cancel() throw(::com::sun::star::uno::RuntimeException, std::exception)
     236             : {
     237           0 :     rFilter->cancel();
     238           0 : }
     239             : 
     240           0 : void HwpImportFilter::setTargetDocument( const Reference< XComponent >& xDoc )
     241             :     throw( IllegalArgumentException, RuntimeException, std::exception )
     242             : {
     243             :         // delegate
     244           0 :     rImporter->setTargetDocument( xDoc );
     245           0 : }
     246             : 
     247           4 : OUString HwpImportFilter::getImplementationName_Static() throw()
     248             : {
     249           4 :     return OUString( IMPLEMENTATION_NAME );
     250             : }
     251             : 
     252           1 : OUString HwpImportFilter::getImplementationName() throw(::com::sun::star::uno::RuntimeException, std::exception)
     253             : {
     254           1 :     return OUString( IMPLEMENTATION_NAME );
     255             : }
     256             : 
     257           0 : sal_Bool HwpImportFilter::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
     258             : {
     259           0 :     return cppu::supportsService(this, ServiceName);
     260             : }
     261             : 
     262             : //XExtendedFilterDetection
     263          84 : OUString HwpImportFilter::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rDescriptor ) throw (::com::sun::star::uno::RuntimeException, std::exception)
     264             : {
     265          84 :     OUString sTypeName;
     266             : 
     267         168 :     utl::MediaDescriptor aDescriptor(rDescriptor);
     268          84 :     aDescriptor.addInputStream();
     269             : 
     270             :     Reference< XInputStream > xInputStream(
     271         168 :         aDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()], UNO_QUERY);
     272             : 
     273          84 :     if (xInputStream.is())
     274             :     {
     275          84 :         Sequence< sal_Int8 > aData;
     276          84 :         sal_Int32 nLen = HWPIDLen;
     277          84 :         if (
     278         168 :              nLen == xInputStream->readBytes(aData, nLen) &&
     279          84 :              detect_hwp_version(reinterpret_cast<const char*>(aData.getConstArray()))
     280             :            )
     281             :         {
     282           0 :             sTypeName = "writer_MIZI_Hwp_97";
     283          84 :         }
     284             :     }
     285             : 
     286         168 :     return sTypeName;
     287             : }
     288             : 
     289           1 : Sequence< OUString> HwpImportFilter::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException, std::exception)
     290             : {
     291           1 :     Sequence < OUString > aRet(2);
     292           1 :     OUString* pArray = aRet.getArray();
     293           1 :     pArray[0] = SERVICE_NAME1;
     294           1 :     pArray[1] = SERVICE_NAME2;
     295           1 :     return aRet;
     296             : }
     297             : 
     298             : extern "C"
     299             : {
     300           4 :     SAL_DLLPUBLIC_EXPORT void * SAL_CALL hwp_component_getFactory( const sal_Char * pImplName, void * pServiceManager, void *  )
     301             :     {
     302           4 :         void * pRet = 0;
     303             : 
     304           4 :         if (pServiceManager )
     305             :         {
     306           4 :             Reference< XSingleServiceFactory > xRet;
     307           8 :             Reference< XMultiServiceFactory > xSMgr = static_cast< XMultiServiceFactory * > ( pServiceManager );
     308             : 
     309           8 :             OUString aImplementationName = OUString::createFromAscii( pImplName );
     310             : 
     311           4 :             if (aImplementationName == IMPLEMENTATION_NAME )
     312             :             {
     313           8 :                 xRet = createSingleFactory( xSMgr, aImplementationName,
     314             :                                             HwpImportFilter_CreateInstance,
     315           4 :                                             HwpImportFilter::getSupportedServiceNames_Static() );
     316             :             }
     317           4 :             if (xRet.is())
     318             :             {
     319           4 :                 xRet->acquire();
     320           4 :                 pRet = xRet.get();
     321           4 :             }
     322             :         }
     323             : 
     324           4 :         return pRet;
     325             :     }
     326             : }
     327             : 
     328             : #endif
     329             : 
     330             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11