LibreOffice Module writerfilter (master) 1
WriterFilter.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#ifdef DBG_UTIL
21#include <iostream>
22#endif
23
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/document/XExporter.hpp>
26#include <com/sun/star/document/XFilter.hpp>
27#include <com/sun/star/document/XImporter.hpp>
28#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
29#include <com/sun/star/io/WrongFormatException.hpp>
30#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
31#include <com/sun/star/lang/XInitialization.hpp>
32#include <com/sun/star/lang/XMultiServiceFactory.hpp>
33#include <com/sun/star/xml/sax/SAXParseException.hpp>
44#include <rtl/ref.hxx>
45#include <sal/log.hxx>
48
49using namespace ::com::sun::star;
50
51static OUString lcl_GetExceptionMessageRec(xml::sax::SAXException const& e);
52
53static OUString lcl_GetExceptionMessage(xml::sax::SAXException const& e)
54{
55 OUString const thisMessage("SAXParseException: \"" + e.Message + "\"");
56 OUString const restMessage(lcl_GetExceptionMessageRec(e));
57 return restMessage + "\n" + thisMessage;
58}
59static OUString lcl_GetExceptionMessage(xml::sax::SAXParseException const& e)
60{
61 OUString const thisMessage("SAXParseException: '" + e.Message + "', Stream '" + e.SystemId
62 + "', Line " + OUString::number(e.LineNumber) + ", Column "
63 + OUString::number(e.ColumnNumber));
64 OUString const restMessage(lcl_GetExceptionMessageRec(e));
65 return restMessage + "\n" + thisMessage;
66}
67
68static OUString lcl_GetExceptionMessageRec(xml::sax::SAXException const& e)
69{
70 xml::sax::SAXParseException saxpe;
71 if (e.WrappedException >>= saxpe)
72 {
73 return lcl_GetExceptionMessage(saxpe);
74 }
75 xml::sax::SAXException saxe;
76 if (e.WrappedException >>= saxe)
77 {
78 return lcl_GetExceptionMessage(saxe);
79 }
80 uno::Exception ue;
81 if (e.WrappedException >>= ue)
82 {
83 return ue.Message;
84 }
85 return {};
86}
87
88namespace
89{
91class WriterFilter
92 : public cppu::WeakImplHelper<document::XFilter, document::XImporter, document::XExporter,
93 lang::XInitialization, lang::XServiceInfo>
94{
95 uno::Reference<uno::XComponentContext> m_xContext;
96 uno::Reference<lang::XComponent> m_xSrcDoc, m_xDstDoc;
97 uno::Sequence<uno::Any> m_xInitializationArguments;
98
99public:
100 explicit WriterFilter(uno::Reference<uno::XComponentContext> xContext)
101 : m_xContext(std::move(xContext))
102 {
103 }
104
105 // XFilter
106 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
107 void SAL_CALL cancel() override;
108
109 // XImporter
110 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
111
112 // XExporter
113 void SAL_CALL setSourceDocument(const uno::Reference<lang::XComponent>& xDoc) override;
114
115 // XInitialization
116 void SAL_CALL initialize(const uno::Sequence<uno::Any>& rArguments) override;
117
118 // XServiceInfo
119 OUString SAL_CALL getImplementationName() override;
120 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
121 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
122};
123}
124
125sal_Bool WriterFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
126{
127 if (m_xSrcDoc.is())
128 {
129 uno::Reference<lang::XMultiServiceFactory> xMSF(m_xContext->getServiceManager(),
130 uno::UNO_QUERY_THROW);
131 uno::Reference<uno::XInterface> xIfc;
132 try
133 {
134 xIfc.set(xMSF->createInstance("com.sun.star.comp.Writer.DocxExport"),
135 uno::UNO_SET_THROW);
136 }
137 catch (uno::RuntimeException&)
138 {
139 throw;
140 }
141 catch (uno::Exception& e)
142 {
144 throw lang::WrappedTargetRuntimeException("wrapped " + a.getValueTypeName() + ": "
145 + e.Message,
146 uno::Reference<uno::XInterface>(), a);
147 }
148
149 uno::Reference<lang::XInitialization> xInit(xIfc, uno::UNO_QUERY_THROW);
150 xInit->initialize(m_xInitializationArguments);
151
152 uno::Reference<document::XExporter> xExprtr(xIfc, uno::UNO_QUERY_THROW);
153 uno::Reference<document::XFilter> xFltr(xIfc, uno::UNO_QUERY_THROW);
154 xExprtr->setSourceDocument(m_xSrcDoc);
155 return xFltr->filter(rDescriptor);
156 }
157 if (m_xDstDoc.is())
158 {
159 uno::Reference<beans::XPropertySet> const xDocProps(m_xDstDoc, uno::UNO_QUERY);
160 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::Any(true));
161 comphelper::ScopeGuard g([xDocProps] {
162 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::Any(false));
163 });
164 utl::MediaDescriptor aMediaDesc(rDescriptor);
165 bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault("RepairPackage", false);
166 bool bSkipImages
167 = aMediaDesc.getUnpackedValueOrDefault("FilterOptions", OUString()) == "SkipImages";
168
169 uno::Reference<io::XInputStream> xInputStream;
170 try
171 {
172 // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
174 new ::oox::core::FilterDetect(m_xContext));
175 xInputStream = xDetector->extractUnencryptedPackage(aMediaDesc);
176 }
177 catch (uno::Exception&)
178 {
179 }
180
181 if (!xInputStream.is())
182 return false;
183
186 m_xContext, xInputStream, m_xDstDoc, bRepairStorage,
188 //create the tokenizer and domain mapper
191 bRepairStorage);
192 uno::Reference<task::XStatusIndicator> xStatusIndicator
193 = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_STATUSINDICATOR,
194 uno::Reference<task::XStatusIndicator>());
197 bSkipImages, rDescriptor));
198
199 uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
200 pDocument->setModel(xModel);
201
202 uno::Reference<drawing::XDrawPageSupplier> xDrawings(m_xDstDoc, uno::UNO_QUERY_THROW);
203 uno::Reference<drawing::XDrawPage> xDrawPage(xDrawings->getDrawPage(), uno::UNO_SET_THROW);
204 pDocument->setDrawPage(xDrawPage);
205
206 try
207 {
208 pDocument->resolve(*pStream);
209 }
210 catch (xml::sax::SAXParseException const& e)
211 {
212 // note: SfxObjectShell checks for WrongFormatException
213 io::WrongFormatException wfe(lcl_GetExceptionMessage(e));
214 throw lang::WrappedTargetRuntimeException("", getXWeak(), uno::Any(wfe));
215 }
216 catch (xml::sax::SAXException const& e)
217 {
218 // note: SfxObjectShell checks for WrongFormatException
219 io::WrongFormatException wfe(lcl_GetExceptionMessage(e));
220 throw lang::WrappedTargetRuntimeException("", getXWeak(), uno::Any(wfe));
221 }
222 catch (uno::RuntimeException const&)
223 {
224 throw;
225 }
226 catch (uno::Exception const&)
227 {
228 css::uno::Any anyEx = cppu::getCaughtException();
229 SAL_WARN("writerfilter",
230 "WriterFilter::filter(): failed with " << exceptionToString(anyEx));
231 throw lang::WrappedTargetRuntimeException("", getXWeak(), anyEx);
232 }
233
234 // Adding some properties to the document's grab bag for interoperability purposes:
235 comphelper::SequenceAsHashMap aGrabBagProperties;
236
237 // Adding the saved Theme DOM
238 aGrabBagProperties["OOXTheme"] <<= pDocument->getThemeDom();
239
240 // Adding the saved custom xml DOM
241 aGrabBagProperties["OOXCustomXml"] <<= pDocument->getCustomXmlDomList();
242 aGrabBagProperties["OOXCustomXmlProps"] <<= pDocument->getCustomXmlDomPropsList();
243
244 // Adding the saved Glossary Document DOM to the document's grab bag
245 aGrabBagProperties["OOXGlossary"] <<= pDocument->getGlossaryDocDom();
246 aGrabBagProperties["OOXGlossaryDom"] <<= pDocument->getGlossaryDomList();
247
248 // Adding the saved embedding document to document's grab bag
249 aGrabBagProperties["OOXEmbeddings"] <<= pDocument->getEmbeddingsList();
250
252
256 oox::StorageRef xVbaPrjStrg = std::make_shared<::oox::ole::OleStorage>(
257 m_xContext, pVBAProjectStream->getDocumentStream(), false);
258 if (xVbaPrjStrg && xVbaPrjStrg->isStorage())
259 {
260 ::oox::ole::VbaProject aVbaProject(m_xContext, xModel, u"Writer");
261 uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(
262 utl::MediaDescriptor::PROP_FRAME, uno::Reference<frame::XFrame>());
263
264 // if no XFrame try fallback to what we can glean from the Model
265 if (!xFrame.is())
266 {
267 uno::Reference<frame::XController> xController = xModel->getCurrentController();
268 xFrame = xController.is() ? xController->getFrame() : nullptr;
269 }
270
271 oox::GraphicHelper gHelper(m_xContext, xFrame, xVbaPrjStrg);
272 aVbaProject.importVbaProject(*xVbaPrjStrg, gHelper);
273
277 if (pVBADataStream)
278 {
279 uno::Reference<io::XInputStream> xDataStream = pVBADataStream->getDocumentStream();
280 if (xDataStream.is())
281 aVbaProject.importVbaData(xDataStream);
282 }
283 }
284
285 pStream.clear();
286
287 // note: pStream.clear calls RemoveLastParagraph()
288
289 return true;
290 }
291 return false;
292}
293
294void WriterFilter::cancel() {}
295
296void WriterFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
297{
298 m_xDstDoc = xDoc;
299
300 // Set some compatibility options that are valid for the DOCX format
301 uno::Reference<lang::XMultiServiceFactory> xFactory(xDoc, uno::UNO_QUERY);
302 uno::Reference<beans::XPropertySet> xSettings(
303 xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY);
304
305 xSettings->setPropertyValue("AddVerticalFrameOffsets", uno::Any(true));
306 xSettings->setPropertyValue("UseOldNumbering", uno::Any(false));
307 xSettings->setPropertyValue("IgnoreFirstLineIndentInNumbering", uno::Any(false));
308 xSettings->setPropertyValue("DoNotResetParaAttrsForNumFont", uno::Any(false));
309 xSettings->setPropertyValue("UseFormerLineSpacing", uno::Any(false));
310 xSettings->setPropertyValue("AddParaSpacingToTableCells", uno::Any(true));
311 xSettings->setPropertyValue("AddParaLineSpacingToTableCells", uno::Any(true));
312 xSettings->setPropertyValue("UseFormerObjectPositioning", uno::Any(false));
313 xSettings->setPropertyValue("ConsiderTextWrapOnObjPos", uno::Any(true));
314 xSettings->setPropertyValue("UseFormerTextWrapping", uno::Any(false));
315 xSettings->setPropertyValue("IgnoreTabsAndBlanksForLineCalculation", uno::Any(true));
316 xSettings->setPropertyValue("InvertBorderSpacing", uno::Any(true));
317 xSettings->setPropertyValue("CollapseEmptyCellPara", uno::Any(true));
318 // tdf#142404 TabOverSpacing (new for compatibilityMode15/Word2013+) is a subset of TabOverMargin
319 // (which applied to DOCX <= compatibilityMode14).
320 // TabOverMargin looks at tabs beyond the normal text area,
321 // while TabOverSpacing only refers to a tab beyond the paragraph margin.
322 xSettings->setPropertyValue("TabOverSpacing", uno::Any(true));
323 xSettings->setPropertyValue("UnbreakableNumberings", uno::Any(true));
324
325 xSettings->setPropertyValue("FloattableNomargins", uno::Any(true));
326 xSettings->setPropertyValue("ClippedPictures", uno::Any(true));
327 xSettings->setPropertyValue("BackgroundParaOverDrawings", uno::Any(true));
328 xSettings->setPropertyValue("TreatSingleColumnBreakAsPageBreak", uno::Any(true));
329 xSettings->setPropertyValue("PropLineSpacingShrinksFirstLine", uno::Any(true));
330 xSettings->setPropertyValue("DoNotCaptureDrawObjsOnPage", uno::Any(true));
331 xSettings->setPropertyValue("DisableOffPagePositioning", uno::Any(true));
332 xSettings->setPropertyValue("DropCapPunctuation", uno::Any(true));
333 // rely on default for HyphenateURLs=false
334}
335
336void WriterFilter::setSourceDocument(const uno::Reference<lang::XComponent>& xDoc)
337{
338 m_xSrcDoc = xDoc;
339}
340
341void WriterFilter::initialize(const uno::Sequence<uno::Any>& rArguments)
342{
343 m_xInitializationArguments = rArguments;
344}
345
346OUString WriterFilter::getImplementationName() { return "com.sun.star.comp.Writer.WriterFilter"; }
347
348sal_Bool WriterFilter::supportsService(const OUString& rServiceName)
349{
350 return cppu::supportsService(this, rServiceName);
351}
352
353uno::Sequence<OUString> WriterFilter::getSupportedServiceNames()
354{
355 uno::Sequence<OUString> aRet = { OUString("com.sun.star.document.ImportFilter"),
356 OUString("com.sun.star.document.ExportFilter") };
357 return aRet;
358}
359
360extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
362 uno::XComponentContext* component, uno::Sequence<uno::Any> const& /*rSequence*/)
363{
364 return cppu::acquire(new WriterFilter(component));
365}
366
367/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Writer_WriterFilter_get_implementation(uno::XComponentContext *component, uno::Sequence< uno::Any > const &)
static OUString lcl_GetExceptionMessageRec(xml::sax::SAXException const &e)
static OUString lcl_GetExceptionMessage(xml::sax::SAXException const &e)
static void putPropertiesToDocumentGrabBag(const css::uno::Reference< css::lang::XComponent > &xDstDoc, const comphelper::SequenceAsHashMap &rProperties)
static constexpr OUStringLiteral PROP_STATUSINDICATOR
static constexpr OUStringLiteral PROP_FRAME
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 OOXMLStream::Pointer_t createStream(const css::uno::Reference< css::uno::XComponentContext > &rContext, const css::uno::Reference< css::io::XInputStream > &rStream, bool bRepairStorage)
static OOXMLDocument * createDocument(const OOXMLStream::Pointer_t &pStream, const css::uno::Reference< css::task::XStatusIndicator > &xStatusIndicator, bool bSkipImage, const css::uno::Sequence< css::beans::PropertyValue > &rDescriptor)
OString exceptionToString(const css::uno::Any &caught)
Reference< XSingleServiceFactory > xFactory
uno_Any a
#define SAL_WARN(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()
std::shared_ptr< StorageBase > StorageRef
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel
unsigned char sal_Bool