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