LibreOffice Module writerfilter (master) 1
RtfFilter.cxx
Go to the documentation of this file.
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 <memory>
21
22#include <com/sun/star/beans/XPropertySet.hpp>
23#include <com/sun/star/document/XExporter.hpp>
24#include <com/sun/star/document/XFilter.hpp>
25#include <com/sun/star/document/XImporter.hpp>
26#include <com/sun/star/io/WrongFormatException.hpp>
27#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
28#include <com/sun/star/lang/XInitialization.hpp>
29#include <com/sun/star/lang/XServiceInfo.hpp>
30#include <com/sun/star/lang/XMultiServiceFactory.hpp>
33#include <osl/file.hxx>
39
42
43using namespace ::com::sun::star;
44
45namespace
46{
48class RtfFilter
49 : public cppu::WeakImplHelper<document::XFilter, document::XImporter, document::XExporter,
50 lang::XInitialization, lang::XServiceInfo>
51{
52 uno::Reference<uno::XComponentContext> m_xContext;
53 uno::Reference<lang::XComponent> m_xSrcDoc, m_xDstDoc;
54
55public:
56 explicit RtfFilter(uno::Reference<uno::XComponentContext> xContext);
57
58 // XFilter
59 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
60 void SAL_CALL cancel() override;
61
62 // XImporter
63 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
64
65 // XExporter
66 void SAL_CALL setSourceDocument(const uno::Reference<lang::XComponent>& xDoc) override;
67
68 // XInitialization
69 void SAL_CALL initialize(const uno::Sequence<uno::Any>& rArguments) override;
70
71 // XServiceInfo
72 OUString SAL_CALL getImplementationName() override;
73 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
74 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
75};
76}
77
78RtfFilter::RtfFilter(uno::Reference<uno::XComponentContext> xContext)
79 : m_xContext(std::move(xContext))
80{
81}
82
83sal_Bool RtfFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
84{
85 if (m_xSrcDoc.is())
86 {
87 uno::Reference<lang::XMultiServiceFactory> xMSF(m_xContext->getServiceManager(),
88 uno::UNO_QUERY_THROW);
89 uno::Reference<uno::XInterface> xIfc(
90 xMSF->createInstance("com.sun.star.comp.Writer.RtfExport"), uno::UNO_SET_THROW);
91 uno::Reference<document::XExporter> xExporter(xIfc, uno::UNO_QUERY_THROW);
92 uno::Reference<document::XFilter> xFilter(xIfc, uno::UNO_QUERY_THROW);
93 xExporter->setSourceDocument(m_xSrcDoc);
94 return xFilter->filter(rDescriptor);
95 }
96
97 bool bResult(false);
98 uno::Reference<task::XStatusIndicator> xStatusIndicator;
99
100 uno::Reference<beans::XPropertySet> xDocProps;
101 if (m_xDstDoc.is()) // not in cppunittest?
102 {
103 xDocProps.set(m_xDstDoc, uno::UNO_QUERY);
104 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::Any(true));
105 }
106 comphelper::ScopeGuard g([xDocProps] {
107 if (xDocProps.is()) // not in cppunittest?
108 {
109 // note: pStream.clear calls RemoveLastParagraph()
110 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::Any(false));
111 }
112 });
113
114 try
115 {
116 utl::MediaDescriptor aMediaDesc(rDescriptor);
117 bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault("RepairPackage", false);
118 bool bIsNewDoc = !aMediaDesc.getUnpackedValueOrDefault("InsertMode", false);
119 uno::Reference<io::XInputStream> xInputStream;
120
121 aMediaDesc.addInputStream();
122 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM] >>= xInputStream;
123
124 // If this is set, write to this file, instead of the real document during paste.
125 char* pEnv = getenv("SW_DEBUG_RTF_PASTE_TO");
126 OUString aOutStr;
127 if (!bIsNewDoc && pEnv
128 && osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pEnv), aOutStr)
129 == osl::FileBase::E_None)
130 {
131 std::unique_ptr<SvStream> pOut(
132 utl::UcbStreamHelper::CreateStream(aOutStr, StreamMode::WRITE));
133 std::unique_ptr<SvStream> pIn(utl::UcbStreamHelper::CreateStream(xInputStream));
134 pOut->WriteStream(*pIn);
135 return true;
136 }
137
138 // If this is set, read from this file, instead of the real clipboard during paste.
139 pEnv = getenv("SW_DEBUG_RTF_PASTE_FROM");
140 if (!bIsNewDoc && pEnv)
141 {
142 OUString aInStr;
143 osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pEnv), aInStr);
144 std::unique_ptr<SvStream> pStream
145 = utl::UcbStreamHelper::CreateStream(aInStr, StreamMode::READ);
146 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(std::move(pStream)));
147 xInputStream.set(xStream, uno::UNO_QUERY);
148 }
149
150 uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(
151 utl::MediaDescriptor::PROP_FRAME, uno::Reference<frame::XFrame>());
152
153 xStatusIndicator = aMediaDesc.getUnpackedValueOrDefault(
154 utl::MediaDescriptor::PROP_STATUSINDICATOR, uno::Reference<task::XStatusIndicator>());
155
158 m_xContext, xInputStream, m_xDstDoc, bRepairStorage,
162 m_xContext, xInputStream, m_xDstDoc, xFrame, xStatusIndicator, aMediaDesc));
163 pDocument->resolve(*pStream);
164 bResult = true;
165 }
166 catch (const io::WrongFormatException&)
167 {
168 css::uno::Any anyEx = cppu::getCaughtException();
169 // cannot throw WrongFormatException directly :(
170 throw lang::WrappedTargetRuntimeException("", getXWeak(), anyEx);
171 }
172 catch (const uno::Exception&)
173 {
174 TOOLS_INFO_EXCEPTION("writerfilter", "Exception caught");
175 }
176
177 if (xStatusIndicator.is())
178 xStatusIndicator->end();
179 return bResult;
180}
181
182void RtfFilter::cancel() {}
183
184void RtfFilter::setSourceDocument(const uno::Reference<lang::XComponent>& xDoc)
185{
186 m_xSrcDoc = xDoc;
187}
188
189void RtfFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
190{
191 m_xDstDoc = xDoc;
192}
193
194void RtfFilter::initialize(const uno::Sequence<uno::Any>& /*aArguments*/)
195{
196 // The DOCX exporter here extracts 'type' of the filter, ie 'Word' or
197 // 'Word Template' but we don't need it for RTF.
198}
199
200OUString RtfFilter::getImplementationName() { return "com.sun.star.comp.Writer.RtfFilter"; }
201
202sal_Bool RtfFilter::supportsService(const OUString& rServiceName)
203{
204 return cppu::supportsService(this, rServiceName);
205}
206
207uno::Sequence<OUString> RtfFilter::getSupportedServiceNames()
208{
209 uno::Sequence<OUString> aRet = { OUString("com.sun.star.document.ImportFilter"),
210 OUString("com.sun.star.document.ExportFilter") };
211 return aRet;
212}
213
214extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
216 uno::Sequence<uno::Any> const& /*rSequence*/)
217{
218 return cppu::acquire(new RtfFilter(pComponent));
219}
220/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Writer_RtfFilter_get_implementation(uno::XComponentContext *pComponent, uno::Sequence< uno::Any > const &)
Definition: RtfFilter.cxx:215
Reference< XInputStream > xStream
static constexpr OUStringLiteral PROP_INPUTSTREAM
static constexpr OUStringLiteral PROP_STATUSINDICATOR
static constexpr OUStringLiteral PROP_FRAME
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
static Stream::Pointer_t createMapper(css::uno::Reference< css::uno::XComponentContext > const &xContext, css::uno::Reference< css::io::XInputStream > const &xInputStream, css::uno::Reference< css::lang::XComponent > const &xModel, bool bRepairStorage, SourceDocumentType eDocumentType, utl::MediaDescriptor const &rMediaDesc)
static RTFDocument::Pointer_t createDocument(css::uno::Reference< css::uno::XComponentContext > const &xContext, css::uno::Reference< css::io::XInputStream > const &xInputStream, css::uno::Reference< css::lang::XComponent > const &xDstDoc, css::uno::Reference< css::frame::XFrame > const &xFrame, css::uno::Reference< css::task::XStatusIndicator > const &xStatusIndicator, const utl::MediaDescriptor &rMediaDescriptor)
#define TOOLS_INFO_EXCEPTION(area, stream)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
Reference< XFrame > xFrame
unsigned char sal_Bool
oslFileHandle & pOut