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

Generated by: LCOV version 1.10