LCOV - code coverage report
Current view: top level - libreoffice/writerfilter/source/filter - RtfFilter.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 60 70 85.7 %
Date: 2012-12-27 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             : #include <rtl/ustring.hxx>
      21             : #include <cppuhelper/implementationentry.hxx>
      22             : #include <osl/module.hxx>
      23             : #include <tools/solar.h>
      24             : #include <RtfFilter.hxx>
      25             : #include <comphelper/mediadescriptor.hxx>
      26             : #include <dmapper/DomainMapper.hxx>
      27             : #include <rtftok/RTFDocument.hxx>
      28             : #include <com/sun/star/frame/XFrame.hpp>
      29             : #include <com/sun/star/task/XStatusIndicator.hpp>
      30             : #include <com/sun/star/io/WrongFormatException.hpp>
      31             : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
      32             : #include <com/sun/star/text/XTextRange.hpp>
      33             : #ifdef DBG_COPYPASTE
      34             : #include <unotools/localfilehelper.hxx>
      35             : #include <tools/stream.hxx>
      36             : #include <unotools/ucbstreamhelper.hxx>
      37             : #endif
      38             : 
      39             : using namespace ::rtl;
      40             : using namespace ::cppu;
      41             : using namespace ::com::sun::star;
      42             : using ::comphelper::MediaDescriptor;
      43             : 
      44         149 : RtfFilter::RtfFilter( const uno::Reference< uno::XComponentContext >& rxContext)  :
      45         149 :     m_xContext( rxContext )
      46             : {
      47         149 : }
      48             : 
      49         298 : RtfFilter::~RtfFilter()
      50             : {
      51         298 : }
      52             : 
      53         159 : sal_Bool RtfFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
      54             :    throw (uno::RuntimeException)
      55             : {
      56         159 :     sal_uInt32 nStartTime = osl_getGlobalTimer();
      57         159 :     if( m_xSrcDoc.is() )
      58             :     {
      59          29 :         uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
      60          29 :         uno::Reference< uno::XInterface > xIfc( xMSF->createInstance("com.sun.star.comp.Writer.RtfExport"), uno::UNO_QUERY_THROW);
      61          29 :         if (!xIfc.is())
      62           0 :             return sal_False;
      63          29 :         uno::Reference< document::XExporter > xExprtr(xIfc, uno::UNO_QUERY_THROW);
      64          29 :         uno::Reference< document::XFilter > xFltr(xIfc, uno::UNO_QUERY_THROW);
      65          29 :         if (!xExprtr.is() || !xFltr.is())
      66           0 :             return sal_False;
      67          29 :         xExprtr->setSourceDocument(m_xSrcDoc);
      68          29 :         return xFltr->filter(aDescriptor);
      69             :     }
      70             : 
      71         130 :     sal_Bool bResult(sal_False);
      72         130 :     uno::Reference<task::XStatusIndicator> xStatusIndicator;
      73             : 
      74             :     try
      75             :     {
      76         130 :         MediaDescriptor aMediaDesc( aDescriptor );
      77         130 :         bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault( "RepairPackage", false );
      78         130 :         bool bIsNewDoc = aMediaDesc.getUnpackedValueOrDefault( "IsNewDoc", true );
      79         130 :         uno::Reference<text::XTextRange> xInsertTextRange = aMediaDesc.getUnpackedValueOrDefault( "TextInsertModeRange", uno::Reference<text::XTextRange>());
      80             : #ifdef DEBUG_IMPORT
      81             :         OUString sURL = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() );
      82             :         ::std::string sURLc = OUStringToOString(sURL, RTL_TEXTENCODING_ASCII_US).getStr();
      83             : 
      84             :         writerfilter::TagLogger::Pointer_t dmapperLogger
      85             :             (writerfilter::TagLogger::getInstance("DOMAINMAPPER"));
      86             :         dmapperLogger->setFileName(sURLc);
      87             :         dmapperLogger->startDocument();
      88             : #endif
      89         130 :         uno::Reference< io::XInputStream > xInputStream;
      90             : 
      91         130 :         aMediaDesc.addInputStream();
      92         130 :         aMediaDesc[ MediaDescriptor::PROP_INPUTSTREAM() ] >>= xInputStream;
      93             : 
      94             : #ifdef DBG_COPYPASTE
      95             :         OUString aOutStr;
      96             :         if (utl::LocalFileHelper::ConvertPhysicalNameToURL("/tmp/stream.rtf", aOutStr))
      97             :         {
      98             :             SvStream* pOut = utl::UcbStreamHelper::CreateStream(aOutStr, STREAM_WRITE);
      99             :             SvStream* pIn = utl::UcbStreamHelper::CreateStream(xInputStream);
     100             :             *pOut << *pIn;
     101             :             delete pOut;
     102             :         }
     103             : #endif
     104             : 
     105         130 :         uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_FRAME(),
     106         260 :                 uno::Reference<frame::XFrame>());
     107             : 
     108         130 :         xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_STATUSINDICATOR(),
     109         260 :                 uno::Reference<task::XStatusIndicator>());
     110             : 
     111             :         writerfilter::Stream::Pointer_t pStream(
     112         131 :                 new writerfilter::dmapper::DomainMapper(m_xContext, xInputStream, m_xDstDoc, bRepairStorage, writerfilter::dmapper::DOCUMENT_RTF, xInsertTextRange, bIsNewDoc));
     113             :         writerfilter::rtftok::RTFDocument::Pointer_t const pDocument(
     114         129 :                 writerfilter::rtftok::RTFDocumentFactory::createDocument(m_xContext, xInputStream, m_xDstDoc, xFrame, xStatusIndicator));
     115         129 :         pDocument->resolve(*pStream);
     116         126 :         bResult = sal_True;
     117             : #ifdef DEBUG_IMPORT
     118             :         dmapperLogger->endDocument();
     119             : #endif
     120         126 :         sal_uInt32 nEndTime = osl_getGlobalTimer();
     121         129 :         SAL_INFO("writerfilter.profile", OSL_THIS_FUNC << " finished in " << nEndTime - nStartTime << " ms");
     122             :     }
     123           2 :     catch (const io::WrongFormatException& e)
     124             :     {
     125             :         // cannot throw WrongFormatException directly :(
     126             :         throw lang::WrappedTargetRuntimeException("",
     127           2 :                 static_cast<OWeakObject*>(this), uno::makeAny(e));
     128             :     }
     129           2 :     catch (const uno::Exception& e)
     130             :     {
     131             :         SAL_INFO("writerfilter", "Exception caught: " << e.Message);
     132             :     }
     133             : 
     134         128 :     if (xStatusIndicator.is())
     135         118 :         xStatusIndicator->end();
     136         130 :     return bResult;
     137             : }
     138             : 
     139           0 : void RtfFilter::cancel(  ) throw (uno::RuntimeException)
     140             : {
     141           0 : }
     142             : 
     143          29 : void RtfFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
     144             :    throw (lang::IllegalArgumentException, uno::RuntimeException)
     145             : {
     146          29 :    m_xSrcDoc = xDoc;
     147          29 : }
     148             : 
     149         119 : void RtfFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
     150             :    throw (lang::IllegalArgumentException, uno::RuntimeException)
     151             : {
     152         119 :    m_xDstDoc = xDoc;
     153         119 : }
     154             : 
     155         147 : void RtfFilter::initialize( const uno::Sequence< uno::Any >& /*aArguments*/ ) throw (uno::Exception, uno::RuntimeException)
     156             : {
     157             :     // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
     158             :     // 'Word Template' but we don't need it for RTF.
     159         147 : }
     160             : 
     161           0 : OUString RtfFilter::getImplementationName(  ) throw (uno::RuntimeException)
     162             : {
     163           0 :    return RtfFilter_getImplementationName();
     164             : }
     165             : 
     166             : #define SERVICE_NAME1 "com.sun.star.document.ImportFilter"
     167             : #define SERVICE_NAME2 "com.sun.star.document.ExportFilter"
     168           0 : sal_Bool RtfFilter::supportsService( const OUString& rServiceName ) throw (uno::RuntimeException)
     169             : {
     170           0 :     return ( rServiceName == SERVICE_NAME1 || rServiceName == SERVICE_NAME2 );
     171             : }
     172             : 
     173           0 : uno::Sequence< OUString > RtfFilter::getSupportedServiceNames(  ) throw (uno::RuntimeException)
     174             : {
     175           0 :     return RtfFilter_getSupportedServiceNames();
     176             : }
     177             : 
     178             : /* Helpers, used by shared lib exports. */
     179             : 
     180           5 : OUString RtfFilter_getImplementationName () throw (uno::RuntimeException)
     181             : {
     182           5 :    return OUString ( "com.sun.star.comp.Writer.RtfFilter" );
     183             : }
     184             : 
     185           3 : uno::Sequence< OUString > RtfFilter_getSupportedServiceNames(  ) throw (uno::RuntimeException)
     186             : {
     187           3 :    uno::Sequence < OUString > aRet(2);
     188           3 :    OUString* pArray = aRet.getArray();
     189           3 :    pArray[0] =  SERVICE_NAME1;
     190           3 :    pArray[1] =  SERVICE_NAME2;
     191           3 :    return aRet;
     192             : }
     193             : #undef SERVICE_NAME1
     194             : #undef SERVICE_NAME2
     195             : 
     196         149 : uno::Reference< uno::XInterface > RtfFilter_createInstance( const uno::Reference< uno::XComponentContext >& xContext)
     197             :                 throw( uno::Exception )
     198             : {
     199         149 :    return (cppu::OWeakObject*) new RtfFilter( xContext );
     200          15 : }
     201             : 
     202             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10