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 328 : RtfFilter::RtfFilter(const uno::Reference< uno::XComponentContext >& rxContext)
41 328 : : m_xContext(rxContext)
42 : {
43 328 : }
44 :
45 656 : RtfFilter::~RtfFilter()
46 : {
47 656 : }
48 :
49 343 : sal_Bool RtfFilter::filter(const uno::Sequence< beans::PropertyValue >& aDescriptor) throw(uno::RuntimeException, std::exception)
50 : {
51 343 : sal_uInt32 nStartTime = osl_getGlobalTimer();
52 343 : if (m_xSrcDoc.is())
53 : {
54 55 : uno::Reference< lang::XMultiServiceFactory > xMSF(m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
55 110 : uno::Reference< uno::XInterface > xIfc(xMSF->createInstance("com.sun.star.comp.Writer.RtfExport"), uno::UNO_QUERY_THROW);
56 55 : if (!xIfc.is())
57 0 : return sal_False;
58 110 : uno::Reference< document::XExporter > xExporter(xIfc, uno::UNO_QUERY_THROW);
59 110 : uno::Reference< document::XFilter > xFilter(xIfc, uno::UNO_QUERY_THROW);
60 55 : if (!xExporter.is() || !xFilter.is())
61 0 : return sal_False;
62 55 : xExporter->setSourceDocument(m_xSrcDoc);
63 110 : return xFilter->filter(aDescriptor);
64 : }
65 :
66 288 : sal_Bool bResult(sal_False);
67 288 : uno::Reference<task::XStatusIndicator> xStatusIndicator;
68 :
69 : try
70 : {
71 288 : utl::MediaDescriptor aMediaDesc(aDescriptor);
72 288 : bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault("RepairPackage", false);
73 288 : bool bIsNewDoc = !aMediaDesc.getUnpackedValueOrDefault("InsertMode", false);
74 576 : 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 576 : uno::Reference< io::XInputStream > xInputStream;
85 :
86 288 : aMediaDesc.addInputStream();
87 288 : aMediaDesc[ utl::MediaDescriptor::PROP_INPUTSTREAM() ] >>= xInputStream;
88 :
89 : // If this is set, write to this file, instead of the real document during paste.
90 288 : char* pEnv = getenv("SW_DEBUG_RTF_PASTE_TO");
91 576 : OUString aOutStr;
92 288 : 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 288 : pEnv = getenv("SW_DEBUG_RTF_PASTE_FROM");
103 288 : 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 288 : uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_FRAME(),
113 864 : uno::Reference<frame::XFrame>());
114 :
115 576 : xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_STATUSINDICATOR(),
116 288 : uno::Reference<task::XStatusIndicator>());
117 :
118 : writerfilter::Stream::Pointer_t pStream(
119 576 : 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 574 : writerfilter::rtftok::RTFDocumentFactory::createDocument(m_xContext, xInputStream, m_xDstDoc, xFrame, xStatusIndicator));
122 287 : pDocument->resolve(*pStream);
123 284 : bResult = sal_True;
124 : #ifdef DEBUG_IMPORT
125 : dmapperLogger->endDocument();
126 : #endif
127 284 : sal_uInt32 nEndTime = osl_getGlobalTimer();
128 288 : SAL_INFO("writerfilter.profile", OSL_THIS_FUNC << " finished in " << nEndTime - nStartTime << " ms");
129 : }
130 2 : catch (const io::WrongFormatException& e)
131 : {
132 : // cannot throw WrongFormatException directly :(
133 : throw lang::WrappedTargetRuntimeException("",
134 2 : static_cast<OWeakObject*>(this), uno::makeAny(e));
135 : }
136 2 : catch (const uno::Exception& e)
137 : {
138 : SAL_INFO("writerfilter", "Exception caught: " << e.Message);
139 : }
140 :
141 286 : if (xStatusIndicator.is())
142 258 : xStatusIndicator->end();
143 288 : return bResult;
144 : }
145 :
146 0 : void RtfFilter::cancel() throw(uno::RuntimeException, std::exception)
147 : {
148 0 : }
149 :
150 55 : void RtfFilter::setSourceDocument(const uno::Reference< lang::XComponent >& xDoc) throw(lang::IllegalArgumentException, uno::RuntimeException, std::exception)
151 : {
152 55 : m_xSrcDoc = xDoc;
153 55 : }
154 :
155 272 : void RtfFilter::setTargetDocument(const uno::Reference< lang::XComponent >& xDoc) throw(lang::IllegalArgumentException, uno::RuntimeException, std::exception)
156 : {
157 272 : m_xDstDoc = xDoc;
158 272 : }
159 :
160 319 : 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 319 : }
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 14 : OUString RtfFilter_getImplementationName() throw(uno::RuntimeException)
183 : {
184 14 : return OUString("com.sun.star.comp.Writer.RtfFilter");
185 : }
186 :
187 5 : uno::Sequence< OUString > RtfFilter_getSupportedServiceNames() throw(uno::RuntimeException)
188 : {
189 5 : uno::Sequence < OUString > aRet(2);
190 5 : OUString* pArray = aRet.getArray();
191 5 : pArray[0] = "com.sun.star.document.ImportFilter";
192 5 : pArray[1] = "com.sun.star.document.ExportFilter";
193 5 : return aRet;
194 : }
195 :
196 328 : uno::Reference< uno::XInterface > RtfFilter_createInstance(const uno::Reference< uno::XComponentContext >& xContext) throw(uno::Exception)
197 : {
198 328 : return (cppu::OWeakObject*) new RtfFilter(xContext);
199 : }
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|