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

Generated by: LCOV version 1.10