LCOV - code coverage report
Current view: top level - writerfilter/source/filter - ImportFilter.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 148 0.0 %
Date: 2014-04-14 Functions: 0 14 0.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             : #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 <unotools/mediadescriptor.hxx>
      27             : #include <cppuhelper/supportsservice.hxx>
      28             : #include <oox/core/filterdetect.hxx>
      29             : #include <dmapper/DomainMapper.hxx>
      30             : #include <WriterFilter.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 utl::MediaDescriptor;
      44             : 
      45             : 
      46             : 
      47           0 : sal_Bool WriterFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
      48             :    throw (uno::RuntimeException, std::exception)
      49             : {
      50           0 :     if( m_xSrcDoc.is() )
      51             :     {
      52           0 :         uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
      53           0 :         uno::Reference< uno::XInterface > xIfc( xMSF->createInstance("com.sun.star.comp.Writer.DocxExport"), uno::UNO_QUERY_THROW);
      54           0 :         if (!xIfc.is())
      55           0 :             return sal_False;
      56           0 :         uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
      57           0 :         uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
      58           0 :         if (!xExprtr.is() || !xFltr.is())
      59           0 :             return sal_False;
      60           0 :         xExprtr->setSourceDocument(m_xSrcDoc);
      61           0 :         return xFltr->filter(aDescriptor);
      62             :     }
      63           0 :     else if (m_xDstDoc.is())
      64             :     {
      65           0 :         MediaDescriptor aMediaDesc( aDescriptor );
      66           0 :         bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
      67             : 
      68           0 :         uno::Reference< io::XInputStream > xInputStream;
      69             :         try
      70             :         {
      71             :             // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
      72           0 :             ::oox::core::FilterDetect aDetector( m_xContext );
      73           0 :             xInputStream = aDetector.extractUnencryptedPackage( aMediaDesc );
      74             :         }
      75           0 :         catch( uno::Exception& )
      76             :         {
      77             :         }
      78             : 
      79           0 :         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           0 :         (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           0 :             writerfilter::dmapper::DOCUMENT_OOXML : writerfilter::dmapper::DOCUMENT_DOC;
     101             : 
     102           0 :     writerfilter::dmapper::DomainMapper* aDomainMapper = new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, bRepairStorage, eType, uno::Reference<text::XTextRange>());
     103           0 :     writerfilter::Stream::Pointer_t pStream(aDomainMapper);
     104             :     //create the tokenizer and domain mapper
     105           0 :     if( eType == writerfilter::dmapper::DOCUMENT_OOXML )
     106             :     {
     107           0 :         writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream, bRepairStorage);
     108           0 :         uno::Reference<task::XStatusIndicator> xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_STATUSINDICATOR(), uno::Reference<task::XStatusIndicator>());
     109           0 :         writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream, xStatusIndicator));
     110             : 
     111           0 :         uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
     112           0 :         pDocument->setModel(xModel);
     113             : 
     114             :         uno::Reference<drawing::XDrawPageSupplier> xDrawings
     115           0 :             (m_xDstDoc, uno::UNO_QUERY_THROW);
     116             :         uno::Reference<drawing::XDrawPage> xDrawPage
     117           0 :             (xDrawings->getDrawPage(), uno::UNO_SET_THROW);
     118           0 :         pDocument->setDrawPage(xDrawPage);
     119             : 
     120           0 :         pDocument->resolve(*pStream);
     121             : 
     122             :         // Adding some properties to the document's grab bag for interoperability purposes:
     123           0 :         uno::Sequence<beans::PropertyValue> aGrabBagProperties(10);
     124             : 
     125             :         // Adding the saved Theme DOM
     126           0 :         aGrabBagProperties[0].Name = "OOXTheme";
     127           0 :         aGrabBagProperties[0].Value = uno::makeAny( pDocument->getThemeDom() );
     128             : 
     129             :         // Adding the saved custom xml DOM
     130           0 :         aGrabBagProperties[1].Name = "OOXCustomXml";
     131           0 :         aGrabBagProperties[1].Value = uno::makeAny( pDocument->getCustomXmlDomList() );
     132           0 :         aGrabBagProperties[2].Name = "OOXCustomXmlProps";
     133           0 :         aGrabBagProperties[2].Value = uno::makeAny( pDocument->getCustomXmlDomPropsList() );
     134             : 
     135             :         // Adding the saved ActiveX DOM
     136           0 :         aGrabBagProperties[3].Name = "OOXActiveX";
     137           0 :         aGrabBagProperties[3].Value = uno::makeAny( pDocument->getActiveXDomList() );
     138           0 :         aGrabBagProperties[4].Name = "OOXActiveXBin";
     139           0 :         aGrabBagProperties[4].Value = uno::makeAny( pDocument->getActiveXBinList() );
     140             : 
     141             :         // Adding the saved w:themeFontLang setting
     142           0 :         aGrabBagProperties[5].Name = "ThemeFontLangProps";
     143           0 :         aGrabBagProperties[5].Value = uno::makeAny( aDomainMapper->GetThemeFontLangProperties() );
     144             : 
     145             :         // Adding the saved Glossary Documnet DOM to the document's grab bag
     146           0 :         aGrabBagProperties[6].Name = "OOXGlossary";
     147           0 :         aGrabBagProperties[6].Value = uno::makeAny( pDocument->getGlossaryDocDom() );
     148           0 :         aGrabBagProperties[7].Name = "OOXGlossaryDom";
     149           0 :         aGrabBagProperties[7].Value = uno::makeAny( pDocument->getGlossaryDomList() );
     150             : 
     151             :         // Adding the saved embedding document to document's grab bag
     152           0 :         aGrabBagProperties[8].Name = "OOXEmbeddings";
     153           0 :         aGrabBagProperties[8].Value = uno::makeAny( pDocument->getEmbeddingsList() );
     154             : 
     155             :         // Adding the saved compat settings
     156           0 :         aGrabBagProperties[9].Name = "CompatSettings";
     157           0 :         aGrabBagProperties[9].Value = uno::makeAny( aDomainMapper->GetCompatSettings() );
     158             : 
     159           0 :         putPropertiesToDocumentGrabBag( aGrabBagProperties );
     160             : 
     161           0 :         writerfilter::ooxml::OOXMLStream::Pointer_t  pVBAProjectStream(writerfilter::ooxml::OOXMLDocumentFactory::createStream( pDocStream, writerfilter::ooxml::OOXMLStream::VBAPROJECT ));
     162           0 :         oox::StorageRef xVbaPrjStrg( new ::oox::ole::OleStorage( m_xContext, pVBAProjectStream->getDocumentStream(), false ) );
     163           0 :         if( xVbaPrjStrg.get() && xVbaPrjStrg->isStorage() )
     164             :         {
     165           0 :             ::oox::ole::VbaProject aVbaProject( m_xContext, xModel, "Writer" );
     166           0 :             uno::Reference< frame::XFrame > xFrame = aMediaDesc.getUnpackedValueOrDefault(  MediaDescriptor::PROP_FRAME(), uno::Reference< frame::XFrame > () );
     167             : 
     168             :             // if no XFrame try fallback to what we can glean from the Model
     169           0 :             if ( !xFrame.is() )
     170             :             {
     171           0 :                 uno::Reference< frame::XController > xController =  xModel->getCurrentController();
     172           0 :                 xFrame =  xController.is() ? xController->getFrame() : NULL;
     173             :             }
     174             : 
     175           0 :             oox::GraphicHelper gHelper( m_xContext, xFrame, xVbaPrjStrg );
     176           0 :             aVbaProject.importVbaProject( *xVbaPrjStrg, gHelper );
     177           0 :         }
     178             :     }
     179             : 
     180           0 :     pStream.reset();
     181             : #ifdef DEBUG_IMPORT
     182             : 
     183             :     dmapperLogger->endDocument();
     184             :     debugLogger->endDocument();
     185             : #endif
     186             : 
     187           0 :     return sal_True;
     188             :     }
     189           0 :     return sal_False;
     190             : }
     191             : 
     192             : 
     193           0 : void WriterFilter::cancel(  ) throw (uno::RuntimeException, std::exception)
     194             : {
     195           0 : }
     196             : 
     197             : 
     198             : 
     199           0 : void WriterFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
     200             :    throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
     201             : {
     202           0 :    m_xDstDoc = xDoc;
     203             : 
     204             :    // Set some compatibility options that are valid for all the formats
     205           0 :    uno::Reference< lang::XMultiServiceFactory > xFactory( xDoc, uno::UNO_QUERY );
     206           0 :    uno::Reference< beans::XPropertySet > xSettings( xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY );
     207             : 
     208           0 :    xSettings->setPropertyValue( "AddFrameOffsets", uno::makeAny( sal_True ) );
     209           0 :    xSettings->setPropertyValue( "UseOldNumbering", uno::makeAny( sal_False ) );
     210           0 :    xSettings->setPropertyValue( "IgnoreFirstLineIndentInNumbering", uno::makeAny( sal_False ) );
     211           0 :    xSettings->setPropertyValue( "DoNotResetParaAttrsForNumFont", uno::makeAny( sal_False ) );
     212           0 :    xSettings->setPropertyValue( "UseFormerLineSpacing", uno::makeAny( sal_False ) );
     213           0 :    xSettings->setPropertyValue( "AddParaSpacingToTableCells", uno::makeAny( sal_True ) );
     214           0 :    xSettings->setPropertyValue( "UseFormerObjectPositioning", uno::makeAny( sal_False ) );
     215           0 :    xSettings->setPropertyValue( "ConsiderTextWrapOnObjPos", uno::makeAny( sal_True ) );
     216           0 :    xSettings->setPropertyValue( "UseFormerTextWrapping", uno::makeAny( sal_False ) );
     217           0 :    xSettings->setPropertyValue( "TableRowKeep", uno::makeAny( sal_True ) );
     218           0 :    xSettings->setPropertyValue( "IgnoreTabsAndBlanksForLineCalculation", uno::makeAny( sal_True ) );
     219           0 :    xSettings->setPropertyValue( "InvertBorderSpacing", uno::makeAny( sal_True ) );
     220           0 :    xSettings->setPropertyValue( "CollapseEmptyCellPara", uno::makeAny( sal_True ) );
     221           0 :    xSettings->setPropertyValue( "TabOverflow", uno::makeAny( sal_True ) );
     222           0 :    xSettings->setPropertyValue( "UnbreakableNumberings", uno::makeAny( sal_True ) );
     223             : 
     224             :    // Don't load the default style definitions to avoid weird mix
     225           0 :    xSettings->setPropertyValue( "StylesNoDefault", uno::makeAny( sal_True ) );
     226             : 
     227           0 :    xSettings->setPropertyValue("FloattableNomargins", uno::makeAny( sal_True ));
     228           0 :    xSettings->setPropertyValue( "ClippedPictures", uno::makeAny( sal_True ) );
     229           0 :    xSettings->setPropertyValue( "BackgroundParaOverDrawings", uno::makeAny( sal_True ) );
     230           0 :    xSettings->setPropertyValue( "TabOverMargin", uno::makeAny( sal_True ) );
     231           0 : }
     232             : 
     233           0 : void WriterFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
     234             :    throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
     235             : {
     236           0 :    m_xSrcDoc = xDoc;
     237           0 : }
     238             : 
     239             : 
     240             : 
     241           0 : void WriterFilter::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException, std::exception)
     242             : {
     243           0 :    uno::Sequence < beans::PropertyValue > aAnySeq;
     244           0 :    sal_Int32 nLength = aArguments.getLength();
     245           0 :    if ( nLength && ( aArguments[0] >>= aAnySeq ) )
     246             :    {
     247           0 :        const beans::PropertyValue * pValue = aAnySeq.getConstArray();
     248           0 :        nLength = aAnySeq.getLength();
     249           0 :        for ( sal_Int32 i = 0 ; i < nLength; i++)
     250             :        {
     251           0 :            if ( pValue[i].Name == "Type" )
     252             :            {
     253           0 :                pValue[i].Value >>= m_sFilterName;
     254           0 :                break;
     255             :            }
     256             :        }
     257           0 :    }
     258           0 : }
     259             : 
     260             : 
     261           0 : OUString WriterFilter_getImplementationName () throw (uno::RuntimeException)
     262             : {
     263           0 :    return OUString ( "com.sun.star.comp.Writer.WriterFilter" );
     264             : }
     265             : 
     266           0 : uno::Sequence< OUString > WriterFilter_getSupportedServiceNames(  ) throw (uno::RuntimeException)
     267             : {
     268           0 :    uno::Sequence < OUString > aRet(2);
     269           0 :    OUString* pArray = aRet.getArray();
     270           0 :    pArray[0] = "com.sun.star.document.ImportFilter";
     271           0 :    pArray[1] = "com.sun.star.document.ExportFilter";
     272           0 :    return aRet;
     273             : }
     274             : 
     275           0 : uno::Reference< uno::XInterface > WriterFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
     276             :                 throw( uno::Exception )
     277             : {
     278           0 :    return (cppu::OWeakObject*) new WriterFilter( xContext );
     279             : }
     280             : 
     281             : 
     282             : 
     283           0 : OUString WriterFilter::getImplementationName(  ) throw (uno::RuntimeException, std::exception)
     284             : {
     285           0 :    return WriterFilter_getImplementationName();
     286             : }
     287             : 
     288             : 
     289           0 : sal_Bool WriterFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException, std::exception)
     290             : {
     291           0 :     return cppu::supportsService( this, rServiceName );
     292             : }
     293             : 
     294             : 
     295           0 : uno::Sequence< OUString > WriterFilter::getSupportedServiceNames(  ) throw (uno::RuntimeException, std::exception)
     296             : {
     297           0 :     return WriterFilter_getSupportedServiceNames();
     298             : }
     299             : 
     300           0 : void WriterFilter::putPropertiesToDocumentGrabBag( const uno::Sequence< beans::PropertyValue >& aProperties )
     301             : {
     302             :     try
     303             :     {
     304           0 :         uno::Reference<beans::XPropertySet> xDocProps(m_xDstDoc, uno::UNO_QUERY);
     305           0 :         if( xDocProps.is() )
     306             :         {
     307           0 :             uno::Reference<beans::XPropertySetInfo> xPropsInfo = xDocProps->getPropertySetInfo();
     308             : 
     309           0 :             const OUString aGrabBagPropName = "InteropGrabBag";
     310           0 :             if( xPropsInfo.is() && xPropsInfo->hasPropertyByName( aGrabBagPropName ) )
     311             :             {
     312             :                 // get existing grab bag
     313           0 :                 uno::Sequence<beans::PropertyValue> aGrabBag;
     314           0 :                 xDocProps->getPropertyValue( aGrabBagPropName ) >>= aGrabBag;
     315           0 :                 sal_Int32 length = aGrabBag.getLength();
     316             : 
     317             :                 // update grab bag size to contain the new items
     318           0 :                 aGrabBag.realloc( length + aProperties.getLength() );
     319             : 
     320             :                 // put the new items
     321           0 :                 for( sal_Int32 i=0; i < aProperties.getLength(); ++i )
     322             :                 {
     323           0 :                     aGrabBag[length + i].Name = aProperties[i].Name;
     324           0 :                     aGrabBag[length + i].Value = aProperties[i].Value;
     325             :                 }
     326             : 
     327             :                 // put it back to the document
     328           0 :                 xDocProps->setPropertyValue( aGrabBagPropName, uno::Any( aGrabBag ) );
     329           0 :             }
     330           0 :         }
     331             :     }
     332           0 :     catch(const uno::Exception&)
     333             :     {
     334             :         SAL_WARN("writerfilter","Failed to save documents grab bag");
     335             :     }
     336           0 : }
     337             : 
     338             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10