LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / writerfilter / source / filter / WriterFilter.cxx
blob945a00f952582f94e45a762d2e1506c0d621ae5c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifdef DBG_UTIL
21 #include <iostream>
22 #endif
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>
34 #include <cppuhelper/exc_hlp.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <dmapper/DomainMapperFactory.hxx>
37 #include <oox/core/filterdetect.hxx>
38 #include <oox/core/xmlfilterbase.hxx>
39 #include <oox/helper/graphichelper.hxx>
40 #include <oox/ole/olestorage.hxx>
41 #include <oox/ole/vbaproject.hxx>
42 #include <ooxml/OOXMLDocument.hxx>
43 #include <unotools/mediadescriptor.hxx>
44 #include <rtl/ref.hxx>
45 #include <sal/log.hxx>
46 #include <tools/diagnose_ex.h>
47 #include <comphelper/scopeguard.hxx>
49 using namespace ::com::sun::star;
51 static OUString lcl_GetExceptionMessageRec(xml::sax::SAXException const& e);
53 static OUString lcl_GetExceptionMessage(xml::sax::SAXException const& e)
55 OUString const thisMessage("SAXParseException: \"" + e.Message + "\"");
56 OUString const restMessage(lcl_GetExceptionMessageRec(e));
57 return restMessage + "\n" + thisMessage;
59 static OUString lcl_GetExceptionMessage(xml::sax::SAXParseException const& e)
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;
68 static OUString lcl_GetExceptionMessageRec(xml::sax::SAXException const& e)
70 xml::sax::SAXParseException saxpe;
71 if (e.WrappedException >>= saxpe)
73 return lcl_GetExceptionMessage(saxpe);
75 xml::sax::SAXException saxe;
76 if (e.WrappedException >>= saxe)
78 return lcl_GetExceptionMessage(saxe);
80 uno::Exception ue;
81 if (e.WrappedException >>= ue)
83 return ue.Message;
85 return {};
88 namespace
90 /// Common DOCX filter, calls DocxExportFilter via UNO or does the DOCX import.
91 class WriterFilter
92 : public cppu::WeakImplHelper<document::XFilter, document::XImporter, document::XExporter,
93 lang::XInitialization, lang::XServiceInfo>
95 uno::Reference<uno::XComponentContext> m_xContext;
96 uno::Reference<lang::XComponent> m_xSrcDoc, m_xDstDoc;
97 uno::Sequence<uno::Any> m_xInitializationArguments;
99 public:
100 explicit WriterFilter(uno::Reference<uno::XComponentContext> xContext)
101 : m_xContext(std::move(xContext))
105 // XFilter
106 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
107 void SAL_CALL cancel() override;
109 // XImporter
110 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
112 // XExporter
113 void SAL_CALL setSourceDocument(const uno::Reference<lang::XComponent>& xDoc) override;
115 // XInitialization
116 void SAL_CALL initialize(const uno::Sequence<uno::Any>& rArguments) override;
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;
125 sal_Bool WriterFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
127 if (m_xSrcDoc.is())
129 uno::Reference<lang::XMultiServiceFactory> xMSF(m_xContext->getServiceManager(),
130 uno::UNO_QUERY_THROW);
131 uno::Reference<uno::XInterface> xIfc;
134 xIfc.set(xMSF->createInstance("com.sun.star.comp.Writer.DocxExport"),
135 uno::UNO_SET_THROW);
137 catch (uno::RuntimeException&)
139 throw;
141 catch (uno::Exception& e)
143 uno::Any a(cppu::getCaughtException());
144 throw lang::WrappedTargetRuntimeException("wrapped " + a.getValueTypeName() + ": "
145 + e.Message,
146 uno::Reference<uno::XInterface>(), a);
149 uno::Reference<lang::XInitialization> xInit(xIfc, uno::UNO_QUERY_THROW);
150 xInit->initialize(m_xInitializationArguments);
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);
157 if (m_xDstDoc.is())
159 uno::Reference<beans::XPropertySet> const xDocProps(m_xDstDoc, uno::UNO_QUERY);
160 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::makeAny(true));
161 comphelper::ScopeGuard g([xDocProps] {
162 xDocProps->setPropertyValue("UndocumentedWriterfilterHack", uno::makeAny(false));
164 utl::MediaDescriptor aMediaDesc(rDescriptor);
165 bool bRepairStorage = aMediaDesc.getUnpackedValueOrDefault("RepairPackage", false);
166 bool bSkipImages
167 = aMediaDesc.getUnpackedValueOrDefault("FilterOptions", OUString()) == "SkipImages";
169 uno::Reference<io::XInputStream> xInputStream;
172 // use the oox.core.FilterDetect implementation to extract the decrypted ZIP package
173 rtl::Reference<::oox::core::FilterDetect> xDetector(
174 new ::oox::core::FilterDetect(m_xContext));
175 xInputStream = xDetector->extractUnencryptedPackage(aMediaDesc);
177 catch (uno::Exception&)
181 if (!xInputStream.is())
182 return false;
184 writerfilter::Stream::Pointer_t pStream(
185 writerfilter::dmapper::DomainMapperFactory::createMapper(
186 m_xContext, xInputStream, m_xDstDoc, bRepairStorage,
187 writerfilter::dmapper::SourceDocumentType::OOXML, aMediaDesc));
188 //create the tokenizer and domain mapper
189 writerfilter::ooxml::OOXMLStream::Pointer_t pDocStream
190 = writerfilter::ooxml::OOXMLDocumentFactory::createStream(m_xContext, xInputStream,
191 bRepairStorage);
192 uno::Reference<task::XStatusIndicator> xStatusIndicator
193 = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_STATUSINDICATOR,
194 uno::Reference<task::XStatusIndicator>());
195 writerfilter::ooxml::OOXMLDocument::Pointer_t pDocument(
196 writerfilter::ooxml::OOXMLDocumentFactory::createDocument(pDocStream, xStatusIndicator,
197 bSkipImages, rDescriptor));
199 uno::Reference<frame::XModel> xModel(m_xDstDoc, uno::UNO_QUERY_THROW);
200 pDocument->setModel(xModel);
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);
208 pDocument->resolve(*pStream);
210 catch (xml::sax::SAXParseException const& e)
212 // note: SfxObjectShell checks for WrongFormatException
213 io::WrongFormatException wfe(lcl_GetExceptionMessage(e));
214 throw lang::WrappedTargetRuntimeException("", static_cast<OWeakObject*>(this),
215 uno::makeAny(wfe));
217 catch (xml::sax::SAXException const& e)
219 // note: SfxObjectShell checks for WrongFormatException
220 io::WrongFormatException wfe(lcl_GetExceptionMessage(e));
221 throw lang::WrappedTargetRuntimeException("", static_cast<OWeakObject*>(this),
222 uno::makeAny(wfe));
224 catch (uno::RuntimeException const&)
226 throw;
228 catch (uno::Exception const&)
230 css::uno::Any anyEx = cppu::getCaughtException();
231 SAL_WARN("writerfilter",
232 "WriterFilter::filter(): failed with " << exceptionToString(anyEx));
233 throw lang::WrappedTargetRuntimeException("", static_cast<OWeakObject*>(this), anyEx);
236 // Adding some properties to the document's grab bag for interoperability purposes:
237 comphelper::SequenceAsHashMap aGrabBagProperties;
239 // Adding the saved Theme DOM
240 aGrabBagProperties["OOXTheme"] <<= pDocument->getThemeDom();
242 // Adding the saved custom xml DOM
243 aGrabBagProperties["OOXCustomXml"] <<= pDocument->getCustomXmlDomList();
244 aGrabBagProperties["OOXCustomXmlProps"] <<= pDocument->getCustomXmlDomPropsList();
246 // Adding the saved Glossary Document DOM to the document's grab bag
247 aGrabBagProperties["OOXGlossary"] <<= pDocument->getGlossaryDocDom();
248 aGrabBagProperties["OOXGlossaryDom"] <<= pDocument->getGlossaryDomList();
250 // Adding the saved embedding document to document's grab bag
251 aGrabBagProperties["OOXEmbeddings"] <<= pDocument->getEmbeddingsList();
253 oox::core::XmlFilterBase::putPropertiesToDocumentGrabBag(m_xDstDoc, aGrabBagProperties);
255 writerfilter::ooxml::OOXMLStream::Pointer_t pVBAProjectStream(
256 writerfilter::ooxml::OOXMLDocumentFactory::createStream(
257 pDocStream, writerfilter::ooxml::OOXMLStream::VBAPROJECT));
258 oox::StorageRef xVbaPrjStrg = std::make_shared<::oox::ole::OleStorage>(
259 m_xContext, pVBAProjectStream->getDocumentStream(), false);
260 if (xVbaPrjStrg && xVbaPrjStrg->isStorage())
262 ::oox::ole::VbaProject aVbaProject(m_xContext, xModel, u"Writer");
263 uno::Reference<frame::XFrame> xFrame = aMediaDesc.getUnpackedValueOrDefault(
264 utl::MediaDescriptor::PROP_FRAME, uno::Reference<frame::XFrame>());
266 // if no XFrame try fallback to what we can glean from the Model
267 if (!xFrame.is())
269 uno::Reference<frame::XController> xController = xModel->getCurrentController();
270 xFrame = xController.is() ? xController->getFrame() : nullptr;
273 oox::GraphicHelper gHelper(m_xContext, xFrame, xVbaPrjStrg);
274 aVbaProject.importVbaProject(*xVbaPrjStrg, gHelper);
276 writerfilter::ooxml::OOXMLStream::Pointer_t pVBADataStream(
277 writerfilter::ooxml::OOXMLDocumentFactory::createStream(
278 pDocStream, writerfilter::ooxml::OOXMLStream::VBADATA));
279 if (pVBADataStream)
281 uno::Reference<io::XInputStream> xDataStream = pVBADataStream->getDocumentStream();
282 if (xDataStream.is())
283 aVbaProject.importVbaData(xDataStream);
287 pStream.clear();
289 // note: pStream.clear calls RemoveLastParagraph()
291 return true;
293 return false;
296 void WriterFilter::cancel() {}
298 void WriterFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
300 m_xDstDoc = xDoc;
302 // Set some compatibility options that are valid for the DOCX format
303 uno::Reference<lang::XMultiServiceFactory> xFactory(xDoc, uno::UNO_QUERY);
304 uno::Reference<beans::XPropertySet> xSettings(
305 xFactory->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY);
307 xSettings->setPropertyValue("AddVerticalFrameOffsets", uno::makeAny(true));
308 xSettings->setPropertyValue("UseOldNumbering", uno::makeAny(false));
309 xSettings->setPropertyValue("IgnoreFirstLineIndentInNumbering", uno::makeAny(false));
310 xSettings->setPropertyValue("DoNotResetParaAttrsForNumFont", uno::makeAny(false));
311 xSettings->setPropertyValue("UseFormerLineSpacing", uno::makeAny(false));
312 xSettings->setPropertyValue("AddParaSpacingToTableCells", uno::makeAny(true));
313 xSettings->setPropertyValue("AddParaLineSpacingToTableCells", uno::makeAny(true));
314 xSettings->setPropertyValue("UseFormerObjectPositioning", uno::makeAny(false));
315 xSettings->setPropertyValue("ConsiderTextWrapOnObjPos", uno::makeAny(true));
316 xSettings->setPropertyValue("UseFormerTextWrapping", uno::makeAny(false));
317 xSettings->setPropertyValue("IgnoreTabsAndBlanksForLineCalculation", uno::makeAny(true));
318 xSettings->setPropertyValue("InvertBorderSpacing", uno::makeAny(true));
319 xSettings->setPropertyValue("CollapseEmptyCellPara", uno::makeAny(true));
320 // tdf#142404 TabOverSpacing (new for compatibilityMode15/Word2013+) is a subset of TabOverMargin
321 // (which applied to DOCX <= compatibilityMode14).
322 // TabOverMargin looks at tabs beyond the normal text area,
323 // while TabOverSpacing only refers to a tab beyond the paragraph margin.
324 xSettings->setPropertyValue("TabOverSpacing", uno::makeAny(true));
325 xSettings->setPropertyValue("UnbreakableNumberings", uno::makeAny(true));
327 xSettings->setPropertyValue("FloattableNomargins", uno::makeAny(true));
328 xSettings->setPropertyValue("ClippedPictures", uno::makeAny(true));
329 xSettings->setPropertyValue("BackgroundParaOverDrawings", uno::makeAny(true));
330 xSettings->setPropertyValue("TreatSingleColumnBreakAsPageBreak", uno::makeAny(true));
331 xSettings->setPropertyValue("PropLineSpacingShrinksFirstLine", uno::makeAny(true));
332 xSettings->setPropertyValue("DoNotCaptureDrawObjsOnPage", uno::makeAny(true));
333 xSettings->setPropertyValue("DisableOffPagePositioning", uno::makeAny(true));
336 void WriterFilter::setSourceDocument(const uno::Reference<lang::XComponent>& xDoc)
338 m_xSrcDoc = xDoc;
341 void WriterFilter::initialize(const uno::Sequence<uno::Any>& rArguments)
343 m_xInitializationArguments = rArguments;
346 OUString WriterFilter::getImplementationName() { return "com.sun.star.comp.Writer.WriterFilter"; }
348 sal_Bool WriterFilter::supportsService(const OUString& rServiceName)
350 return cppu::supportsService(this, rServiceName);
353 uno::Sequence<OUString> WriterFilter::getSupportedServiceNames()
355 uno::Sequence<OUString> aRet = { OUString("com.sun.star.document.ImportFilter"),
356 OUString("com.sun.star.document.ExportFilter") };
357 return aRet;
360 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
361 com_sun_star_comp_Writer_WriterFilter_get_implementation(
362 uno::XComponentContext* component, uno::Sequence<uno::Any> const& /*rSequence*/)
364 return cppu::acquire(new WriterFilter(component));
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */