sw/qa: warning C6011: Dereferencing NULL pointer
[LibreOffice.git] / starmath / source / unofilter.cxx
blob28bc21b89cd1c5cde2aeb2c61af157dd0e79e045
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/.
8 */
10 #include <unotools/mediadescriptor.hxx>
11 #include <unotools/ucbstreamhelper.hxx>
12 #include <sot/storage.hxx>
13 #include <cppuhelper/supportsservice.hxx>
15 #include <document.hxx>
16 #include "mathtype.hxx"
17 #include <unomodel.hxx>
18 #include <comphelper/diagnose_ex.hxx>
20 using namespace ::com::sun::star;
22 namespace
24 /// Invokes the MathType importer via UNO.
25 class MathTypeFilter
26 : public cppu::WeakImplHelper<document::XFilter, document::XImporter, lang::XServiceInfo>
28 uno::Reference<lang::XComponent> m_xDstDoc;
30 public:
31 MathTypeFilter();
33 // XFilter
34 sal_Bool SAL_CALL filter(const uno::Sequence<beans::PropertyValue>& rDescriptor) override;
35 void SAL_CALL cancel() override;
37 // XImporter
38 void SAL_CALL setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) override;
40 // XServiceInfo
41 OUString SAL_CALL getImplementationName() override;
42 sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
43 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
47 MathTypeFilter::MathTypeFilter() = default;
49 sal_Bool MathTypeFilter::filter(const uno::Sequence<beans::PropertyValue>& rDescriptor)
51 bool bSuccess = false;
52 try
54 utl::MediaDescriptor aMediaDesc(rDescriptor);
55 aMediaDesc.addInputStream();
56 uno::Reference<io::XInputStream> xInputStream;
57 aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM] >>= xInputStream;
58 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream));
59 if (pStream)
61 if (SotStorage::IsStorageFile(pStream.get()))
63 rtl::Reference<SotStorage> aStorage(new SotStorage(pStream.get(), false));
64 // Is this a MathType Storage?
65 if (aStorage->IsStream(u"Equation Native"_ustr))
67 if (auto pModel = dynamic_cast<SmModel*>(m_xDstDoc.get()))
69 auto pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
70 OUStringBuffer aText(pDocShell->GetText());
71 MathType aEquation(aText);
72 bSuccess = aEquation.Parse(aStorage.get());
73 if (bSuccess)
75 pDocShell->SetText(aText.makeStringAndClear());
76 pDocShell->Parse();
83 catch (const uno::Exception&)
85 DBG_UNHANDLED_EXCEPTION("starmath");
87 return bSuccess;
90 void MathTypeFilter::cancel() {}
92 void MathTypeFilter::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
94 m_xDstDoc = xDoc;
97 OUString MathTypeFilter::getImplementationName()
99 return u"com.sun.star.comp.Math.MathTypeFilter"_ustr;
102 sal_Bool MathTypeFilter::supportsService(const OUString& rServiceName)
104 return cppu::supportsService(this, rServiceName);
107 uno::Sequence<OUString> MathTypeFilter::getSupportedServiceNames()
109 return { u"com.sun.star.document.ImportFilter"_ustr };
112 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
113 com_sun_star_comp_Math_MathTypeFilter_get_implementation(uno::XComponentContext* /*pCtx*/,
114 uno::Sequence<uno::Any> const& /*rSeq*/)
116 return cppu::acquire(new MathTypeFilter);
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */