LCOV - code coverage report
Current view: top level - writerfilter/source/filter - ImportFilter.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 70 95 73.7 %
Date: 2012-08-25 Functions: 9 14 64.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 90 236 38.1 %

           Branch data     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 <osl/diagnose.h>
      21                 :            : #include <com/sun/star/uno/XComponentContext.hpp>
      22                 :            : #include <com/sun/star/beans/XPropertySet.hpp>
      23                 :            : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
      24                 :            : #include <com/sun/star/io/XInputStream.hpp>
      25                 :            : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      26                 :            : #include <comphelper/mediadescriptor.hxx>
      27                 :            : #include <oox/core/filterdetect.hxx>
      28                 :            : #include <dmapper/DomainMapper.hxx>
      29                 :            : #include <WriterFilter.hxx>
      30                 :            : #include <doctok/WW8Document.hxx>
      31                 :            : #include <ooxml/OOXMLDocument.hxx>
      32                 :            : #ifdef DEBUG_IMPORT
      33                 :            : #include <iostream>
      34                 :            : #include <osl/process.h>
      35                 :            : #endif
      36                 :            : 
      37                 :            : #include <resourcemodel/TagLogger.hxx>
      38                 :            : #include <oox/ole/olestorage.hxx>
      39                 :            : #include <oox/ole/vbaproject.hxx>
      40                 :            : #include <oox/helper/graphichelper.hxx>
      41                 :            : using namespace ::rtl;
      42                 :            : using namespace ::com::sun::star;
      43                 :            : using ::comphelper::MediaDescriptor;
      44                 :            : 
      45                 :            : 
      46                 :            : 
      47                 :        248 : sal_Bool WriterFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
      48                 :            :    throw (uno::RuntimeException)
      49                 :            : {
      50         [ +  + ]:        248 :     if( m_xSrcDoc.is() )
      51                 :            :     {
      52 [ +  - ][ +  - ]:         57 :         uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
                 [ +  - ]
      53 [ +  - ][ +  - ]:         57 :         uno::Reference< uno::XInterface > xIfc( xMSF->createInstance("com.sun.star.comp.Writer.DocxExport"), uno::UNO_QUERY_THROW);
                 [ +  - ]
      54         [ -  + ]:         57 :         if (!xIfc.is())
      55                 :          0 :             return sal_False;
      56         [ +  - ]:         57 :         uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
      57         [ +  - ]:         57 :         uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
      58 [ +  - ][ -  + ]:         57 :         if (!xExprtr.is() || !xFltr.is())
                 [ -  + ]
      59                 :          0 :             return sal_False;
      60 [ +  - ][ +  - ]:         57 :         xExprtr->setSourceDocument(m_xSrcDoc);
      61 [ +  - ][ +  - ]:         57 :         return xFltr->filter(aDescriptor);
      62                 :            :     }
      63         [ +  - ]:        191 :     else if (m_xDstDoc.is())
      64                 :            :     {
      65         [ +  - ]:        191 :         MediaDescriptor aMediaDesc( aDescriptor );
      66 [ +  - ][ +  - ]:        191 :         OUString sFilterName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_FILTERNAME(), OUString() );
      67                 :            : 
      68                 :        191 :         uno::Reference< io::XInputStream > xInputStream;
      69                 :            :         try
      70                 :            :         {
      71                 :            :             // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
      72         [ +  - ]:        191 :             ::oox::core::FilterDetect aDetector( m_xContext );
      73 [ +  - ][ +  - ]:        191 :             xInputStream = aDetector.extractUnencryptedPackage( aMediaDesc );
         [ +  - ][ #  # ]
      74                 :            :         }
      75         [ #  # ]:          0 :         catch( uno::Exception& )
      76                 :            :         {
      77                 :            :         }
      78                 :            : 
      79         [ -  + ]:        191 :         if ( !xInputStream.is() )
      80                 :            :         {
      81                 :          0 :             return sal_False;
      82                 :            :         }
      83                 :            : 
      84                 :            : #ifdef DEBUG_IMPORT
      85                 :            :         OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
      86                 :            :         ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
      87                 :            : 
      88                 :            :         writerfilter::TagLogger::Pointer_t debugLogger
      89                 :            :         (writerfilter::TagLogger::getInstance("DEBUG"));
      90                 :            :         debugLogger->setFileName(sURLc);
      91                 :            :         debugLogger->startDocument();
      92                 :            : 
      93                 :            :         writerfilter::TagLogger::Pointer_t dmapperLogger
      94                 :            :         (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
      95                 :            :         dmapperLogger->setFileName(sURLc);
      96                 :            :         dmapperLogger->startDocument();
      97                 :            : #endif
      98                 :            : 
      99                 :            :     writerfilter::dmapper::SourceDocumentType eType =
     100                 :        191 :         (m_sFilterName == "writer_MS_Word_2007" || m_sFilterName == "writer_MS_Word_2007_Template" ||
     101                 :          0 :          m_sFilterName == "writer_OOXML" || m_sFilterName == "writer_OOXML_Text_Template" ) ?
     102   [ -  +  #  #  :        191 :             writerfilter::dmapper::DOCUMENT_OOXML : writerfilter::dmapper::DOCUMENT_DOC;
             #  #  #  # ]
     103                 :            : 
     104 [ +  - ][ +  - ]:        191 :     writerfilter::Stream::Pointer_t pStream(new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, eType));
                 [ +  - ]
     105                 :            :     //create the tokenizer and domain mapper
     106         [ +  - ]:        191 :     if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
     107                 :            :     {
     108         [ +  - ]:        191 :         writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream);
     109 [ +  - ][ +  - ]:        191 :         writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream));
         [ +  - ][ +  - ]
     110                 :            : 
     111         [ +  - ]:        191 :         uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
     112         [ +  - ]:        191 :         pDocument->setModel(xModel);
     113                 :            : 
     114                 :            :         uno::Reference<drawing::XDrawPageSupplier> xDrawings
     115         [ +  - ]:        191 :             (m_xDstDoc, uno::UNO_QUERY_THROW);
     116                 :            :         uno::Reference<drawing::XDrawPage> xDrawPage
     117 [ +  - ][ +  - ]:        191 :             (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
                 [ +  - ]
     118         [ +  - ]:        191 :         pDocument->setDrawPage(xDrawPage);
     119                 :            : 
     120         [ +  - ]:        191 :         pDocument->resolve(*pStream);
     121 [ +  - ][ +  - ]:        191 :         writerfilter::ooxml::OOXMLStream::Pointer_t  pVBAProjectStream(writerfilter::ooxml::OOXMLDocumentFactory::createStream( pDocStream, writerfilter::ooxml::OOXMLStream::VBAPROJECT ));
                 [ +  - ]
     122 [ +  - ][ +  - ]:        191 :         oox::StorageRef xVbaPrjStrg( new ::oox::ole::OleStorage( m_xContext, pVBAProjectStream->getDocumentStream(), false ) );
         [ +  - ][ +  - ]
     123 [ +  - ][ -  + ]:        191 :         if( xVbaPrjStrg.get() && xVbaPrjStrg->isStorage() )
         [ -  + ][ +  - ]
     124                 :            :         {
     125         [ #  # ]:          0 :             ::oox::ole::VbaProject aVbaProject( m_xContext, xModel, "Writer" );
     126 [ #  # ][ #  # ]:          0 :             uno::Reference< frame::XFrame > xFrame = aMediaDesc.getUnpackedValueOrDefault(  MediaDescriptor::PROP_FRAME(), uno::Reference< frame::XFrame > () );
     127                 :            : 
     128                 :            :             // if no XFrame try fallback to what we can glean from the Model
     129         [ #  # ]:          0 :             if ( !xFrame.is() )
     130                 :            :             {
     131 [ #  # ][ #  # ]:          0 :                 uno::Reference< frame::XController > xController =  xModel->getCurrentController();
     132 [ #  # ][ #  # ]:          0 :                 xFrame =  xController.is() ? xController->getFrame() : NULL;
         [ #  # ][ #  # ]
                 [ #  # ]
     133                 :            :             }
     134                 :            : 
     135         [ #  # ]:          0 :             oox::GraphicHelper gHelper( m_xContext, xFrame, xVbaPrjStrg );
     136 [ #  # ][ #  # ]:          0 :             aVbaProject.importVbaProject( *xVbaPrjStrg, gHelper );
                 [ #  # ]
     137 [ +  - ][ +  - ]:        191 :         }
         [ +  - ][ +  - ]
     138                 :            :     }
     139                 :            :     else
     140                 :            :     {
     141         [ #  # ]:          0 :         writerfilter::doctok::WW8Stream::Pointer_t pDocStream = writerfilter::doctok::WW8DocumentFactory::createStream(m_xContext, xInputStream);
     142 [ #  # ][ #  # ]:          0 :         writerfilter::doctok::WW8Document::Pointer_t pDocument(writerfilter::doctok::WW8DocumentFactory::createDocument(pDocStream));
         [ #  # ][ #  # ]
     143                 :            : 
     144 [ #  # ][ #  # ]:          0 :         pDocument->resolve(*pStream);
                 [ #  # ]
     145                 :            :     }
     146                 :            : 
     147         [ +  - ]:        191 :     pStream.reset();
     148                 :            : #ifdef DEBUG_IMPORT
     149                 :            : 
     150                 :            :     dmapperLogger->endDocument();
     151                 :            :     debugLogger->endDocument();
     152                 :            : #endif
     153                 :            : 
     154 [ +  - ][ +  - ]:        191 :     return sal_True;
     155                 :            :     }
     156                 :        248 :     return sal_False;
     157                 :            : }
     158                 :            : 
     159                 :            : 
     160                 :          0 : void WriterFilter::cancel(  ) throw (uno::RuntimeException)
     161                 :            : {
     162                 :          0 : }
     163                 :            : 
     164                 :            : 
     165                 :            : 
     166                 :        191 : void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
     167                 :            :    throw (lang::IllegalArgumentException, uno::RuntimeException)
     168                 :            : {
     169         [ +  - ]:        191 :    m_xDstDoc = xDoc;
     170                 :            : 
     171                 :            :    // Set some compatibility options that are valid for all the formats
     172         [ +  - ]:        191 :    uno::Reference< lang::XMultiServiceFactory > xFactory( xDoc, uno::UNO_QUERY );
     173 [ +  - ][ +  - ]:        191 :    uno::Reference< beans::XPropertySet > xSettings( xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY );
                 [ +  - ]
     174                 :            : 
     175 [ +  - ][ +  - ]:        191 :    xSettings->setPropertyValue( "UnbreakableNumberings", uno::makeAny( sal_True ) );
                 [ +  - ]
     176                 :            : 
     177                 :            :    // Don't load the default style definitions to avoid weird mix
     178 [ +  - ][ +  - ]:        191 :    xSettings->setPropertyValue( "StylesNoDefault", uno::makeAny( sal_True ) );
                 [ +  - ]
     179                 :            : 
     180 [ +  - ][ +  - ]:        191 :    xSettings->setPropertyValue("FloattableNomargins", uno::makeAny( sal_True ));
                 [ +  - ]
     181                 :        191 : }
     182                 :            : 
     183                 :         57 : void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
     184                 :            :    throw (lang::IllegalArgumentException, uno::RuntimeException)
     185                 :            : {
     186                 :         57 :    m_xSrcDoc = xDoc;
     187                 :         57 : }
     188                 :            : 
     189                 :            : 
     190                 :            : 
     191                 :        248 : void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
     192                 :            : {
     193         [ +  - ]:        248 :    uno::Sequence < beans::PropertyValue > aAnySeq;
     194                 :        248 :    sal_Int32 nLength = aArguments.getLength();
     195 [ +  - ][ +  - ]:        248 :    if ( nLength && ( aArguments[0] >>= aAnySeq ) )
         [ +  - ][ +  - ]
     196                 :            :    {
     197                 :        248 :        const beans::PropertyValue * pValue = aAnySeq.getConstArray();
     198                 :        248 :        nLength = aAnySeq.getLength();
     199         [ +  - ]:       3224 :        for ( sal_Int32 i = 0 ; i < nLength; i++)
     200                 :            :        {
     201         [ +  + ]:       2976 :            if ( pValue[i].Name == "Type" )
     202                 :            :            {
     203                 :        248 :                pValue[i].Value >>= m_sFilterName;
     204                 :        248 :                break;
     205                 :            :            }
     206                 :            :        }
     207         [ +  - ]:        248 :    }
     208                 :        248 : }
     209                 :            : 
     210                 :            : 
     211                 :         17 : OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
     212                 :            : {
     213                 :         17 :    return OUString ( "com.sun.star.comp.Writer.WriterFilter" );
     214                 :            : }
     215                 :            : 
     216                 :            : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
     217                 :            : #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
     218                 :            : 
     219                 :            : 
     220                 :          0 : sal_Bool WriterFilter_supportsService( const OUString& ServiceName ) throw (uno::RuntimeException)
     221                 :            : {
     222 [ #  # ][ #  # ]:          0 :    return (ServiceName == SERVICE_NAME1 || ServiceName == SERVICE_NAME2 );
     223                 :            : }
     224                 :            : 
     225                 :            : 
     226                 :          8 : uno::Sequence< OUString > WriterFilter_getSupportedServiceNames(  ) throw (uno::RuntimeException)
     227                 :            : {
     228                 :          8 :    uno::Sequence < OUString > aRet(2);
     229         [ +  - ]:          8 :    OUString* pArray = aRet.getArray();
     230                 :          8 :    pArray[0] =  SERVICE_NAME1;
     231                 :          8 :    pArray[1] =  SERVICE_NAME2;
     232                 :          8 :    return aRet;
     233                 :            : }
     234                 :            : #undef SERVICE_NAME1
     235                 :            : #undef SERVICE_NAME2
     236                 :            : 
     237                 :            : 
     238                 :            : 
     239                 :        248 : uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
     240                 :            :                 throw( uno::Exception )
     241                 :            : {
     242         [ +  - ]:        248 :    return (cppu::OWeakObject*) new WriterFilter( xContext );
     243                 :            : }
     244                 :            : 
     245                 :            : 
     246                 :            : 
     247                 :          0 : OUString WriterFilter::getImplementationName(  ) throw (uno::RuntimeException)
     248                 :            : {
     249                 :          0 :    return WriterFilter_getImplementationName();
     250                 :            : }
     251                 :            : 
     252                 :            : 
     253                 :          0 : sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
     254                 :            : {
     255                 :          0 :     return WriterFilter_supportsService( rServiceName );
     256                 :            : }
     257                 :            : 
     258                 :            : 
     259                 :          0 : uno::Sequence< OUString > WriterFilter::getSupportedServiceNames(  ) throw (uno::RuntimeException)
     260                 :            : {
     261                 :          0 :     return WriterFilter_getSupportedServiceNames();
     262 [ +  - ][ +  - ]:         60 : }
     263                 :            : 
     264                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10