Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / starmath / source / mathml / import.cxx
blob8a04632832863a06f05a5922bf53b56c79a02068
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 // Our mathml
11 #include <mathml/import.hxx>
13 // LO tools to use
14 #include <com/sun/star/beans/PropertyAttribute.hpp>
15 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
16 #include <com/sun/star/embed/ElementModes.hpp>
17 #include <com/sun/star/packages/WrongPasswordException.hpp>
18 #include <com/sun/star/packages/zip/ZipIOException.hpp>
19 #include <com/sun/star/task/XStatusIndicator.hpp>
20 #include <com/sun/star/xml/sax/FastParser.hpp>
21 #include <com/sun/star/xml/sax/InputSource.hpp>
22 #include <com/sun/star/xml/sax/Parser.hpp>
23 #include <com/sun/star/xml/sax/SAXParseException.hpp>
25 // Extra LO tools
26 #include <comphelper/fileformat.h>
27 #include <comphelper/genericpropertyset.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <comphelper/propertysetinfo.hxx>
30 #include <comphelper/servicehelper.hxx>
31 #include <rtl/character.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <sfx2/frame.hxx>
34 #include <sfx2/sfxsids.hrc>
35 #include <sot/storage.hxx>
36 #include <svtools/sfxecode.hxx>
37 #include <svl/itemset.hxx>
38 #include <svl/stritem.hxx>
39 #include <svx/dialmgr.hxx>
40 #include <svx/strings.hrc>
41 #include <unotools/streamwrap.hxx>
42 #include <xmloff/DocumentSettingsContext.hxx>
43 #include <xmloff/xmlmetai.hxx>
44 #include <xmloff/xmlnamespace.hxx>
45 #include <xmloff/xmltoken.hxx>
46 #include <o3tl/string_view.hxx>
48 // Our starmath tools
49 #include <cfgitem.hxx>
50 #include <document.hxx>
51 #include <xparsmlbase.hxx>
52 #include <smmod.hxx>
53 #include <starmathdatabase.hxx>
54 #include <unomodel.hxx>
56 // Old parser
57 #include <mathmlimport.hxx>
59 using namespace ::com::sun::star;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::container;
62 using namespace ::com::sun::star::document;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::uno;
65 using namespace com::sun::star::xml::sax;
66 using namespace ::xmloff::token;
68 // SmMLImportContext
69 /*************************************************************************************************/
71 SmMlElement* SmMLImportWrapper::getElementTree()
73 return m_pMlImport == nullptr ? nullptr : m_pMlImport->getElementTree();
76 ErrCode SmMLImportWrapper::Import(SfxMedium& rMedium)
78 // Fetch context
79 uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
80 if (!xContext.is())
82 SAL_WARN("starmath", "Failed to fetch model while file input");
83 return ERRCODE_SFX_DOLOADFAILED;
86 // Check model
87 if (!m_xModel.is())
89 SAL_WARN("starmath", "Failed to fetch model while file input");
90 return ERRCODE_SFX_DOLOADFAILED;
93 // Try to get an XStatusIndicator from the Medium
94 uno::Reference<task::XStatusIndicator> xStatusIndicator;
96 // Get model via uno
97 SmModel* pModel = m_xModel.get();
98 if (pModel == nullptr)
100 SAL_WARN("starmath", "Failed to fetch sm model while file input");
101 return ERRCODE_SFX_DOLOADFAILED;
104 // Get doc shell
105 m_pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
106 if (m_pDocShell == nullptr)
108 SAL_WARN("starmath", "Failed to fetch smdoc shell while file input");
109 return ERRCODE_SFX_DOLOADFAILED;
112 // Check if it is an embed object
113 bool bEmbedded = m_pDocShell->GetCreateMode() == SfxObjectCreateMode::EMBEDDED;
115 if (!bEmbedded)
117 // Extra check to ensure everything is fine
118 if (m_pDocShell->GetMedium() != &rMedium)
120 SAL_WARN("starmath", "Given medium and doc shell medium differ while file input");
121 return ERRCODE_SFX_DOLOADFAILED;
124 // Fetch the item set
125 SfxItemSet* pSet = rMedium.GetItemSet();
126 if (pSet)
128 const SfxUnoAnyItem* pItem = pSet->GetItem(SID_PROGRESS_STATUSBAR_CONTROL);
129 if (pItem != nullptr)
130 pItem->GetValue() >>= xStatusIndicator;
134 // Create property list
135 static const comphelper::PropertyMapEntry aInfoMap[]
136 = { { u"PrivateData", 0, cppu::UnoType<XInterface>::get(),
137 beans::PropertyAttribute::MAYBEVOID, 0 },
138 { u"BaseURI", 0, ::cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID,
139 0 },
140 { u"StreamRelPath", 0, ::cppu::UnoType<OUString>::get(),
141 beans::PropertyAttribute::MAYBEVOID, 0 },
142 { u"StreamName", 0, ::cppu::UnoType<OUString>::get(),
143 beans::PropertyAttribute::MAYBEVOID, 0 } };
144 uno::Reference<beans::XPropertySet> xInfoSet(
145 comphelper::GenericPropertySet_CreateInstance(new comphelper::PropertySetInfo(aInfoMap)));
147 // Set base URI
148 // needed for relative URLs; but it's OK to import e.g. MathML from the clipboard without one
149 SAL_INFO_IF(rMedium.GetBaseURL().isEmpty(), "starmath", "SmMLImportWrapper: no base URL");
150 xInfoSet->setPropertyValue("BaseURI", Any(rMedium.GetBaseURL()));
152 // Fetch progress range
153 sal_Int32 nProgressRange(rMedium.IsStorage() ? 3 : 1);
154 if (xStatusIndicator.is())
156 xStatusIndicator->start(SvxResId(RID_SVXSTR_DOC_LOAD), nProgressRange);
157 xStatusIndicator->setValue(0);
160 // Get storage
161 if (rMedium.IsStorage())
163 // TODO/LATER: handle the case of embedded links gracefully
164 if (bEmbedded) // && !rMedium.GetStorage()->IsRoot() )
166 OUString aName(u"dummyObjName");
167 if (rMedium.GetItemSet())
169 const SfxStringItem* pDocHierarchItem
170 = rMedium.GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME);
171 if (pDocHierarchItem != nullptr)
172 aName = pDocHierarchItem->GetValue();
175 if (!aName.isEmpty())
176 xInfoSet->setPropertyValue("StreamRelPath", Any(aName));
179 // Check if use OASIS ( new document format )
180 bool bOASIS = SotStorage::GetVersion(rMedium.GetStorage()) > SOFFICE_FILEFORMAT_60;
181 if (xStatusIndicator.is())
182 xStatusIndicator->setValue(1);
184 // Error code in case of needed
185 ErrCode nWarn = ERRCODE_NONE;
187 // Read metadata
188 // read a component from storage
189 if (!bEmbedded)
191 if (bOASIS)
192 nWarn = ReadThroughComponentS(rMedium.GetStorage(), m_xModel, u"meta.xml", xContext,
193 xInfoSet,
194 u"com.sun.star.comp.Math.MLOasisMetaImporter", 6);
195 else
196 nWarn
197 = ReadThroughComponentS(rMedium.GetStorage(), m_xModel, u"meta.xml", xContext,
198 xInfoSet, u"com.sun.star.comp.Math.XMLMetaImporter", 5);
201 // Check if successful
202 if (nWarn != ERRCODE_NONE)
204 if (xStatusIndicator.is())
205 xStatusIndicator->end();
206 SAL_WARN("starmath", "Failed to read file");
207 return nWarn;
210 // Increase success indicator
211 if (xStatusIndicator.is())
212 xStatusIndicator->setValue(2);
214 // Read settings
215 // read a component from storage
216 if (bOASIS)
217 nWarn = ReadThroughComponentS(rMedium.GetStorage(), m_xModel, u"settings.xml", xContext,
218 xInfoSet,
219 u"com.sun.star.comp.Math.MLOasisSettingsImporter", 6);
220 else
221 nWarn
222 = ReadThroughComponentS(rMedium.GetStorage(), m_xModel, u"settings.xml", xContext,
223 xInfoSet, u"com.sun.star.comp.Math.XMLSettingsImporter", 5);
225 // Check if successful
226 if (nWarn != ERRCODE_NONE)
228 if (xStatusIndicator.is())
229 xStatusIndicator->end();
230 SAL_WARN("starmath", "Failed to read file");
231 return nWarn;
234 // Increase success indicator
235 if (xStatusIndicator.is())
236 xStatusIndicator->setValue(3);
238 // Read document
239 // read a component from storage
240 if (m_pDocShell->GetSmSyntaxVersion() == 5)
241 nWarn = ReadThroughComponentS(rMedium.GetStorage(), m_xModel, u"content.xml", xContext,
242 xInfoSet, u"com.sun.star.comp.Math.XMLImporter", 5);
243 else
244 nWarn = ReadThroughComponentS(rMedium.GetStorage(), m_xModel, u"content.xml", xContext,
245 xInfoSet, u"com.sun.star.comp.Math.MLImporter", 6);
246 // Check if successful
247 if (nWarn != ERRCODE_NONE)
249 if (xStatusIndicator.is())
250 xStatusIndicator->end();
251 SAL_WARN("starmath", "Failed to read file");
252 return nWarn;
255 // Finish
256 if (xStatusIndicator.is())
257 xStatusIndicator->end();
258 return ERRCODE_NONE;
260 else
262 // Create input stream
263 Reference<io::XInputStream> xInputStream
264 = new utl::OInputStreamWrapper(rMedium.GetInStream());
266 // Increase success indicator
267 if (xStatusIndicator.is())
268 xStatusIndicator->setValue(1);
270 // Read data
271 // read a component from input stream
272 ErrCode nError = ERRCODE_NONE;
273 if (m_pDocShell->GetSmSyntaxVersion() == 5)
274 nError = ReadThroughComponentIS(xInputStream, m_xModel, xContext, xInfoSet,
275 u"com.sun.star.comp.Math.XMLImporter", false, 5);
276 else
277 nError = ReadThroughComponentIS(xInputStream, m_xModel, xContext, xInfoSet,
278 u"com.sun.star.comp.Math.MLImporter", false, 6);
280 // Finish
281 if (xStatusIndicator.is())
282 xStatusIndicator->end();
284 // Declare any error
285 if (nError != ERRCODE_NONE)
286 SAL_WARN("starmath", "Failed to read file");
288 return nError;
292 ErrCode SmMLImportWrapper::Import(std::u16string_view aSource)
294 // Fetch context
295 uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
296 if (!xContext.is())
298 SAL_WARN("starmath", "Failed to fetch model while file input");
299 return ERRCODE_SFX_DOLOADFAILED;
302 // Check model
303 if (!m_xModel.is())
305 SAL_WARN("starmath", "Failed to fetch model while file input");
306 return ERRCODE_SFX_DOLOADFAILED;
309 // Make a model component from our SmModel
310 uno::Reference<lang::XComponent> xModelComp = m_xModel;
311 if (!xModelComp.is())
313 SAL_WARN("starmath", "Failed to make model while file input");
314 return ERRCODE_SFX_DOLOADFAILED;
317 // Get model via uno
318 SmModel* pModel = m_xModel.get();
319 if (pModel == nullptr)
321 SAL_WARN("starmath", "Failed to fetch sm model while file input");
322 return ERRCODE_SFX_DOLOADFAILED;
325 // Get doc shell
326 m_pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
327 if (m_pDocShell == nullptr)
329 SAL_WARN("starmath", "Failed to fetch smdoc shell while file input");
330 return ERRCODE_SFX_DOLOADFAILED;
333 // Create property list
334 static const comphelper::PropertyMapEntry aInfoMap[]
335 = { { u"PrivateData", 0, cppu::UnoType<XInterface>::get(),
336 beans::PropertyAttribute::MAYBEVOID, 0 },
337 { u"BaseURI", 0, ::cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID,
338 0 },
339 { u"StreamRelPath", 0, ::cppu::UnoType<OUString>::get(),
340 beans::PropertyAttribute::MAYBEVOID, 0 },
341 { u"StreamName", 0, ::cppu::UnoType<OUString>::get(),
342 beans::PropertyAttribute::MAYBEVOID, 0 } };
343 uno::Reference<beans::XPropertySet> xInfoSet(
344 comphelper::GenericPropertySet_CreateInstance(new comphelper::PropertySetInfo(aInfoMap)));
346 // Read data
347 // read a component from text
348 ErrCode nError = ReadThroughComponentMS(aSource, xModelComp, xContext, xInfoSet);
350 // Declare any error
351 if (nError != ERRCODE_NONE)
353 SAL_WARN("starmath", "Failed to read file");
354 return nError;
357 return ERRCODE_NONE;
360 // read a component from input stream
361 ErrCode SmMLImportWrapper::ReadThroughComponentIS(
362 const Reference<io::XInputStream>& xInputStream, const Reference<XComponent>& xModelComponent,
363 Reference<uno::XComponentContext> const& rxContext,
364 Reference<beans::XPropertySet> const& rPropSet, const char16_t* pFilterName, bool bEncrypted,
365 int_fast16_t nSyntaxVersion)
367 // Needs an input stream but checked by caller
368 // Needs a context but checked by caller
369 // Needs property set but checked by caller
370 // Needs a filter name but checked by caller
372 // Prepare ParserInputSource
373 xml::sax::InputSource aParserInput;
374 aParserInput.aInputStream = xInputStream;
376 // Prepare property list
377 Sequence<Any> aArgs{ Any(rPropSet) };
379 // Get filter
380 Reference<XInterface> xFilter
381 = rxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
382 OUString(pFilterName), aArgs, rxContext);
383 if (!xFilter.is())
385 SAL_WARN("starmath", "Can't instantiate filter component " << OUString(pFilterName));
386 return ERRCODE_SFX_DOLOADFAILED;
389 // Connect model and filter
390 Reference<XImporter> xImporter(xFilter, UNO_QUERY);
391 xImporter->setTargetDocument(xModelComponent);
393 // Finally, parser the stream
396 Reference<css::xml::sax::XFastParser> xFastParser(xFilter, UNO_QUERY);
397 Reference<css::xml::sax::XFastDocumentHandler> xFastDocHandler(xFilter, UNO_QUERY);
398 if (xFastParser)
400 xFastParser->setCustomEntityNames(starmathdatabase::icustomMathmlHtmlEntities);
401 xFastParser->parseStream(aParserInput);
403 else if (xFastDocHandler)
405 Reference<css::xml::sax::XFastParser> xParser
406 = css::xml::sax::FastParser::create(rxContext);
407 xParser->setCustomEntityNames(starmathdatabase::icustomMathmlHtmlEntities);
408 xParser->setFastDocumentHandler(xFastDocHandler);
409 xParser->parseStream(aParserInput);
411 else
413 Reference<css::xml::sax::XDocumentHandler> xDocHandler(xFilter, UNO_QUERY);
414 assert(xDocHandler);
415 Reference<css::xml::sax::XParser> xParser = css::xml::sax::Parser::create(rxContext);
416 xParser->setDocumentHandler(xDocHandler);
417 xParser->parseStream(aParserInput);
420 if (nSyntaxVersion == 5)
422 SmXMLImport* pXMlImport = dynamic_cast<SmXMLImport*>(xFilter.get());
423 if (pXMlImport != nullptr && pXMlImport->GetSuccess())
424 return ERRCODE_NONE;
425 else
427 SAL_WARN("starmath", "Filter failed on file input");
428 // However this can not be included since it's not public
429 if (pXMlImport == nullptr)
430 return ERRCODE_NONE;
431 return ERRCODE_SFX_DOLOADFAILED;
435 m_pMlImport = dynamic_cast<SmMLImport*>(xFilter.get());
436 if (m_pMlImport != nullptr && m_pMlImport->getSuccess())
437 return ERRCODE_NONE;
438 else
440 SAL_WARN("starmath", "Filter failed on file input");
441 return ERRCODE_SFX_DOLOADFAILED;
444 catch (const xml::sax::SAXParseException& r)
446 // Sax parser sends wrapped exceptions, try to find the original one
447 xml::sax::SAXException aTmp;
448 xml::sax::SAXException aSaxEx = *static_cast<const xml::sax::SAXException*>(&r);
449 while (aSaxEx.WrappedException >>= aTmp)
450 aSaxEx = aTmp;
452 packages::zip::ZipIOException aBrokenPackage;
453 if (aSaxEx.WrappedException >>= aBrokenPackage)
455 SAL_WARN("starmath", "Failed to read file SAXParseException");
456 return ERRCODE_IO_BROKENPACKAGE;
459 if (bEncrypted)
461 SAL_WARN("starmath", "Wrong file password SAXParseException");
462 return ERRCODE_SFX_WRONGPASSWORD;
465 catch (const xml::sax::SAXException& r)
467 packages::zip::ZipIOException aBrokenPackage;
468 if (r.WrappedException >>= aBrokenPackage)
470 SAL_WARN("starmath", "Failed to read file SAXException");
471 return ERRCODE_IO_BROKENPACKAGE;
474 if (bEncrypted)
476 SAL_WARN("starmath", "Wrong file password SAXException");
477 return ERRCODE_SFX_WRONGPASSWORD;
480 catch (const packages::zip::ZipIOException&)
482 SAL_WARN("starmath", "Failed to unzip file ZipIOException");
483 return ERRCODE_IO_BROKENPACKAGE;
485 catch (const io::IOException&)
487 SAL_WARN("starmath", "Failed to read file ZipIOException");
488 return ERRCODE_IO_UNKNOWN;
490 catch (const std::range_error&)
492 SAL_WARN("starmath", "Failed to read file");
493 return ERRCODE_ABORT;
496 return ERRCODE_ABORT;
499 // read a component from storage
500 ErrCode SmMLImportWrapper::ReadThroughComponentS(const uno::Reference<embed::XStorage>& xStorage,
501 const Reference<XComponent>& xModelComponent,
502 const char16_t* pStreamName,
503 Reference<uno::XComponentContext> const& rxContext,
504 Reference<beans::XPropertySet> const& rPropSet,
505 const char16_t* pFilterName,
506 int_fast16_t nSyntaxVersion)
508 // Needs a storage but checked by caller
509 // Needs a model but checked by caller
510 // Needs a stream name but checked by caller
511 // Needs a context but checked by caller
512 // Needs a property set but checked by caller
513 // Needs a filter name but checked by caller
515 // Get the input stream
518 // Create the stream for the event read
519 uno::Reference<io::XStream> xEventsStream
520 = xStorage->openStreamElement(OUString(pStreamName), embed::ElementModes::READ);
522 // Determine if stream is encrypted or not
523 uno::Reference<beans::XPropertySet> xProps(xEventsStream, uno::UNO_QUERY);
524 Any aAny = xProps->getPropertyValue("Encrypted");
525 bool bEncrypted = false;
526 aAny >>= bEncrypted;
528 // Set base URL and open stream
529 rPropSet->setPropertyValue("StreamName", Any(OUString(pStreamName)));
530 Reference<io::XInputStream> xStream = xEventsStream->getInputStream();
532 // Execute read
533 return ReadThroughComponentIS(xStream, xModelComponent, rxContext, rPropSet, pFilterName,
534 bEncrypted, nSyntaxVersion);
536 catch (packages::WrongPasswordException&)
538 SAL_WARN("starmath", "Wrong file password");
539 return ERRCODE_SFX_WRONGPASSWORD;
541 catch (packages::zip::ZipIOException&)
543 SAL_WARN("starmath", "Failed to unzip file");
544 return ERRCODE_IO_BROKENPACKAGE;
546 catch (uno::Exception&)
550 return ERRCODE_SFX_DOLOADFAILED;
553 // read a component from text
554 ErrCode SmMLImportWrapper::ReadThroughComponentMS(
555 std::u16string_view aText, const css::uno::Reference<css::lang::XComponent>& xModelComponent,
556 css::uno::Reference<css::uno::XComponentContext> const& rxContext,
557 css::uno::Reference<css::beans::XPropertySet> const& rPropSet)
559 // Needs a storage but checked by caller
560 // Needs a model but checked by caller
561 // Needs a stream name but checked by caller
562 // Needs a context but checked by caller
563 // Needs a property set but checked by caller
564 // Needs a filter name but checked by caller
566 // Get the input stream
569 // Generate input memory stream
570 SvMemoryStream aMemoryStream;
571 aMemoryStream.WriteOString(OUStringToOString(aText, RTL_TEXTENCODING_UTF8));
572 uno::Reference<io::XInputStream> xStream(new utl::OInputStreamWrapper(aMemoryStream));
574 // Execute read
575 return ReadThroughComponentIS(xStream, xModelComponent, rxContext, rPropSet,
576 u"com.sun.star.comp.Math.MLImporter", false, 6);
578 catch (packages::WrongPasswordException&)
580 SAL_WARN("starmath", "Wrong file password");
581 return ERRCODE_SFX_WRONGPASSWORD;
583 catch (packages::zip::ZipIOException&)
585 SAL_WARN("starmath", "Failed to unzip file");
586 return ERRCODE_IO_BROKENPACKAGE;
588 catch (uno::Exception&)
592 return ERRCODE_SFX_DOLOADFAILED;
595 // SmMLImport technical
596 /*************************************************************************************************/
598 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
599 Math_MLImporter_get_implementation(uno::XComponentContext* pCtx,
600 uno::Sequence<uno::Any> const& /*rSeq*/)
602 return cppu::acquire(
603 new SmMLImport(pCtx, "com.sun.star.comp.Math.XMLImporter", SvXMLImportFlags::ALL));
606 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
607 Math_MLOasisMetaImporter_get_implementation(uno::XComponentContext* pCtx,
608 uno::Sequence<uno::Any> const& /*rSeq*/)
610 return cppu::acquire(new SmMLImport(pCtx, "com.sun.star.comp.Math.XMLOasisMetaImporter",
611 SvXMLImportFlags::META));
614 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
615 Math_MLOasisSettingsImporter_get_implementation(uno::XComponentContext* pCtx,
616 uno::Sequence<uno::Any> const& /*rSeq*/)
618 return cppu::acquire(new SmMLImport(pCtx, "com.sun.star.comp.Math.XMLOasisSettingsImporter",
619 SvXMLImportFlags::SETTINGS));
622 // SmMLImportContext
623 /*************************************************************************************************/
625 namespace
627 class SmMLImportContext : public SvXMLImportContext
629 private:
630 SmMlElement** m_pParent;
631 SmMlElement* m_pElement;
632 SmMlElement* m_pStyle;
634 public:
635 SmMLImportContext(SmMLImport& rImport, SmMlElement** pParent)
636 : SvXMLImportContext(rImport)
637 , m_pParent(pParent)
638 , m_pElement(nullptr)
639 , m_pStyle(nullptr)
643 private:
644 void declareMlError();
646 public:
647 /** Handles characters (text)
649 virtual void SAL_CALL characters(const OUString& aChars) override;
651 /** Starts the mathml element
653 virtual void SAL_CALL startFastElement(
654 sal_Int32 nElement, const Reference<XFastAttributeList>& aAttributeList) override;
656 /** Ends the mathml element
658 virtual void SAL_CALL endFastElement(sal_Int32 Element) override;
660 /** Creates child element
662 virtual uno::Reference<XFastContextHandler>
663 SAL_CALL createFastChildContext(sal_Int32 nElement,
664 const uno::Reference<XFastAttributeList>& Attribs) override;
666 /** Inherits the style from it's parents
668 void inheritStyle();
670 /** Inherits the style from it's parents on end
672 void inheritStyleEnd();
674 /** Handle mathml attributes
676 void handleAttributes(const Reference<XFastAttributeList>& aAttributeList);
678 /** Handle mathml length attributes
680 SmLengthValue handleLengthAttribute(const OUString& aAttribute);
683 uno::Reference<XFastContextHandler> SAL_CALL
684 SmMLImportContext::createFastChildContext(sal_Int32, const uno::Reference<XFastAttributeList>&)
686 uno::Reference<xml::sax::XFastContextHandler> xContext;
687 xContext = new SmMLImportContext(static_cast<SmMLImport&>(GetImport()), &m_pElement);
688 return xContext;
691 void SmMLImportContext::declareMlError()
693 SmMLImport& aSmMLImport = static_cast<SmMLImport&>(GetImport());
694 aSmMLImport.declareMlError();
697 void SmMLImportContext::inheritStyle()
699 while ((m_pStyle = m_pStyle->getParentElement()) != nullptr)
701 if (m_pStyle->getParentElement()->getMlElementType() == SmMlElementType::MlMstyle
702 || m_pStyle->getParentElement()->getMlElementType() == SmMlElementType::MlMath)
703 break;
706 // Parent inheritation
707 // Mathcolor, mathsize, dir and displaystyle are inherited from parent
708 SmMlElement* pParent = *m_pParent;
709 m_pElement->setAttribute(pParent->getAttribute(SmMlAttributeValueType::MlMathcolor));
710 m_pElement->setAttribute(pParent->getAttribute(SmMlAttributeValueType::MlMathsize));
711 m_pElement->setAttribute(pParent->getAttribute(SmMlAttributeValueType::MlDir));
712 m_pElement->setAttribute(pParent->getAttribute(SmMlAttributeValueType::MlDisplaystyle));
714 // Inherit operator dictionary overwrites
715 if (m_pStyle != nullptr
716 && (m_pElement->getMlElementType() == SmMlElementType::MlMo
717 || m_pElement->getMlElementType() == SmMlElementType::MlMstyle
718 || m_pElement->getMlElementType() == SmMlElementType::MlMath))
720 // TODO fetch operator dictionary first and then overwrite
721 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlAccent))
722 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlAccent));
723 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlFence))
724 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlFence));
725 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlLspace))
726 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlLspace));
727 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlMaxsize))
728 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlMaxsize));
729 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlMinsize))
730 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlMinsize));
731 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlMovablelimits))
732 m_pElement->setAttribute(
733 m_pStyle->getAttribute(SmMlAttributeValueType::MlMovablelimits));
734 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlRspace))
735 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlRspace));
736 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlSeparator))
737 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlSeparator));
738 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlStretchy))
739 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlStretchy));
740 if (m_pStyle->isAttributeSet(SmMlAttributeValueType::MlSymmetric))
741 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlSymmetric));
743 if (m_pElement->getMlElementType() == SmMlElementType::MlMo)
745 // Set form based in position
746 SmMlAttribute aAttribute(SmMlAttributeValueType::MlForm);
747 SmMlForm aForm;
748 if (m_pElement->getSubElementId() == 0)
749 aForm = { SmMlAttributeValueForm::MlPrefix };
750 else
751 aForm = { SmMlAttributeValueForm::MlInfix };
752 aAttribute.setMlForm(&aForm);
753 m_pElement->setAttribute(aAttribute);
757 // Inherit mathvariant
758 if (m_pStyle && m_pStyle->isAttributeSet(SmMlAttributeValueType::MlMathvariant))
759 m_pElement->setAttribute(m_pStyle->getAttribute(SmMlAttributeValueType::MlMathvariant));
762 void SmMLImportContext::inheritStyleEnd()
764 // Mo: check it is the end: postfix
765 if (m_pElement->getMlElementType() == SmMlElementType::MlMo)
767 if ((*m_pParent)->getSubElementsCount() == m_pElement->getSubElementId())
769 // Set form based in position
770 SmMlAttribute aAttribute(SmMlAttributeValueType::MlForm);
771 SmMlForm aForm = { SmMlAttributeValueForm::MlPosfix };
772 aAttribute.setMlForm(&aForm);
773 m_pElement->setAttribute(aAttribute);
777 // Mi: 1 char -> italic
778 if (m_pElement->getMlElementType() != SmMlElementType::MlMi)
779 return;
781 // Inherit mathvariant
782 if (!m_pStyle->isAttributeSet(SmMlAttributeValueType::MlMathvariant))
784 sal_Int32 nIndexUtf16 = 0;
785 // Check if there is only one code point
786 m_pElement->getText().iterateCodePoints(&nIndexUtf16, 1);
787 // Mathml says that 1 code point -> italic
788 if (nIndexUtf16 == m_pElement->getText().getLength())
790 SmMlAttribute aAttribute(SmMlAttributeValueType::MlMathvariant);
791 SmMlMathvariant aMathvariant = { SmMlAttributeValueMathvariant::italic };
792 aAttribute.setMlMathvariant(&aMathvariant);
793 aAttribute.setSet(false);
794 m_pElement->setAttribute(aAttribute);
799 SmLengthValue SmMLImportContext::handleLengthAttribute(const OUString& aAttribute)
801 // Locate unit indication
802 int32_t nUnitPos;
803 for (nUnitPos = 0;
804 nUnitPos < aAttribute.getLength()
805 && (rtl::isAsciiHexDigit(aAttribute[nUnitPos]) || aAttribute[nUnitPos] == '.');
806 ++nUnitPos)
809 // Find unit
810 SmLengthUnit nUnit = SmLengthUnit::MlM;
811 if (nUnitPos != aAttribute.getLength())
813 OUString aUnit = aAttribute.copy(nUnitPos);
814 if (aUnit.compareToIgnoreAsciiCaseAscii("ex"))
815 nUnit = SmLengthUnit::MlEx;
816 if (aUnit.compareToIgnoreAsciiCaseAscii("px"))
817 nUnit = SmLengthUnit::MlPx;
818 if (aUnit.compareToIgnoreAsciiCaseAscii("in"))
819 nUnit = SmLengthUnit::MlIn;
820 if (aUnit.compareToIgnoreAsciiCaseAscii("cm"))
821 nUnit = SmLengthUnit::MlCm;
822 if (aUnit.compareToIgnoreAsciiCaseAscii("mm"))
823 nUnit = SmLengthUnit::MlMm;
824 if (aUnit.compareToIgnoreAsciiCaseAscii("pt"))
825 nUnit = SmLengthUnit::MlPt;
826 if (aUnit.compareToIgnoreAsciiCaseAscii("pc"))
827 nUnit = SmLengthUnit::MlPc;
828 if (aUnit.compareToIgnoreAsciiCaseAscii("%"))
829 nUnit = SmLengthUnit::MlP;
830 else
831 declareMlError();
834 // Get value
835 std::u16string_view aValue = aAttribute.subView(0, nUnitPos);
836 double nValue = o3tl::toDouble(aValue);
837 if (nValue == 0)
839 nUnit = SmLengthUnit::MlM;
840 nValue = 1.0;
841 declareMlError();
844 // Return
845 SmLengthValue aLengthValue = { nUnit, nValue, new OUString(aAttribute) };
846 return aLengthValue;
849 void SmMLImportContext::handleAttributes(const Reference<XFastAttributeList>& aAttributeList)
851 for (auto& aIter : sax_fastparser::castToFastAttributeList(aAttributeList))
853 SmMlAttribute aAttribute(SmMlAttributeValueType::NMlEmpty);
854 switch (aIter.getToken() & TOKEN_MASK)
856 case XML_ACCENT:
858 if (IsXMLToken(aIter, XML_TRUE))
860 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlAccent);
861 SmMlAccent aAccent = { SmMlAttributeValueAccent::MlTrue };
862 aAttribute.setMlAccent(&aAccent);
864 else if (IsXMLToken(aIter, XML_FALSE))
866 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlAccent);
867 SmMlAccent aAccent = { SmMlAttributeValueAccent::MlFalse };
868 aAttribute.setMlAccent(&aAccent);
870 else
872 declareMlError();
874 break;
876 case XML_DIR:
878 if (IsXMLToken(aIter, XML_RTL))
880 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlDir);
881 SmMlDir aDir = { SmMlAttributeValueDir::MlRtl };
882 aAttribute.setMlDir(&aDir);
884 else if (IsXMLToken(aIter, XML_LTR))
886 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlDir);
887 SmMlDir aDir = { SmMlAttributeValueDir::MlLtr };
888 aAttribute.setMlDir(&aDir);
890 else
892 declareMlError();
894 break;
896 case XML_DISPLAYSTYLE:
897 if (IsXMLToken(aIter, XML_TRUE))
899 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlDisplaystyle);
900 SmMlDisplaystyle aDisplaystyle = { SmMlAttributeValueDisplaystyle::MlTrue };
901 aAttribute.setMlDisplaystyle(&aDisplaystyle);
903 else if (IsXMLToken(aIter, XML_FALSE))
905 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlDisplaystyle);
906 SmMlDisplaystyle aDisplaystyle = { SmMlAttributeValueDisplaystyle::MlFalse };
907 aAttribute.setMlDisplaystyle(&aDisplaystyle);
909 else
911 declareMlError();
913 break;
914 case XML_FENCE:
915 if (IsXMLToken(aIter, XML_TRUE))
917 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlFence);
918 SmMlFence aFence = { SmMlAttributeValueFence::MlTrue };
919 aAttribute.setMlFence(&aFence);
921 else if (IsXMLToken(aIter, XML_FALSE))
923 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlFence);
924 SmMlFence aFence = { SmMlAttributeValueFence::MlFalse };
925 aAttribute.setMlFence(&aFence);
927 else
929 declareMlError();
931 break;
932 case XML_HREF:
934 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlHref);
935 OUString* aRef = new OUString(aIter.toString());
936 SmMlHref aHref = { SmMlAttributeValueHref::NMlValid, aRef };
937 aAttribute.setMlHref(&aHref);
938 break;
940 case XML_LSPACE:
942 SmMlLspace aLspace;
943 aLspace.m_aLengthValue = handleLengthAttribute(aIter.toString());
944 aAttribute.setMlLspace(&aLspace);
945 break;
947 case XML_MATHBACKGROUND:
949 if (IsXMLToken(aIter, XML_TRANSPARENT))
951 SmMlMathbackground aMathbackground
952 = { SmMlAttributeValueMathbackground::MlTransparent, COL_TRANSPARENT };
953 aAttribute.setMlMathbackground(&aMathbackground);
955 else
957 Color aColor
958 = starmathdatabase::Identify_ColorName_HTML(aIter.toString()).cColor;
959 SmMlMathbackground aMathbackground
960 = { SmMlAttributeValueMathbackground::MlRgb, aColor };
961 aAttribute.setMlMathbackground(&aMathbackground);
963 break;
965 case XML_MATHCOLOR:
967 if (IsXMLToken(aIter, XML_DEFAULT))
969 SmMlMathcolor aMathcolor
970 = { SmMlAttributeValueMathcolor::MlDefault, COL_BLACK };
971 aAttribute.setMlMathcolor(&aMathcolor);
973 else
975 Color aColor
976 = starmathdatabase::Identify_ColorName_HTML(aIter.toString()).cColor;
977 SmMlMathcolor aMathcolor = { SmMlAttributeValueMathcolor::MlRgb, aColor };
978 aAttribute.setMlMathcolor(&aMathcolor);
980 break;
982 case XML_MATHSIZE:
984 SmMlMathsize aMathsize;
985 aMathsize.m_aLengthValue = handleLengthAttribute(aIter.toString());
986 aAttribute.setMlMathsize(&aMathsize);
987 break;
989 case XML_MATHVARIANT:
991 OUString aVariant = aIter.toString();
992 SmMlAttributeValueMathvariant nVariant = SmMlAttributeValueMathvariant::normal;
993 if (aVariant.compareTo(u"normal"))
994 nVariant = SmMlAttributeValueMathvariant::normal;
995 else if (aVariant.compareTo(u"bold"))
996 nVariant = SmMlAttributeValueMathvariant::bold;
997 else if (aVariant.compareTo(u"italic"))
998 nVariant = SmMlAttributeValueMathvariant::italic;
999 else if (aVariant.compareTo(u"double-struck"))
1000 nVariant = SmMlAttributeValueMathvariant::double_struck;
1001 else if (aVariant.compareTo(u"script"))
1002 nVariant = SmMlAttributeValueMathvariant::script;
1003 else if (aVariant.compareTo(u"fraktur"))
1004 nVariant = SmMlAttributeValueMathvariant::fraktur;
1005 else if (aVariant.compareTo(u"sans-serif"))
1006 nVariant = SmMlAttributeValueMathvariant::sans_serif;
1007 else if (aVariant.compareTo(u"monospace"))
1008 nVariant = SmMlAttributeValueMathvariant::monospace;
1009 else if (aVariant.compareTo(u"bold-italic"))
1010 nVariant = SmMlAttributeValueMathvariant::bold_italic;
1011 else if (aVariant.compareTo(u"bold-fracktur"))
1012 nVariant = SmMlAttributeValueMathvariant::bold_fraktur;
1013 else if (aVariant.compareTo(u"bold-script"))
1014 nVariant = SmMlAttributeValueMathvariant::bold_script;
1015 else if (aVariant.compareTo(u"bold-sans-serif"))
1016 nVariant = SmMlAttributeValueMathvariant::bold_sans_serif;
1017 else if (aVariant.compareTo(u"sans-serif-italic"))
1018 nVariant = SmMlAttributeValueMathvariant::sans_serif_italic;
1019 else if (aVariant.compareTo(u"sans-serif-bold-italic"))
1020 nVariant = SmMlAttributeValueMathvariant::sans_serif_bold_italic;
1021 else if (aVariant.compareTo(u"initial"))
1022 nVariant = SmMlAttributeValueMathvariant::initial;
1023 else if (aVariant.compareTo(u"tailed"))
1024 nVariant = SmMlAttributeValueMathvariant::tailed;
1025 else if (aVariant.compareTo(u"looped"))
1026 nVariant = SmMlAttributeValueMathvariant::looped;
1027 else if (aVariant.compareTo(u"stretched"))
1028 nVariant = SmMlAttributeValueMathvariant::stretched;
1029 else
1030 declareMlError();
1031 SmMlMathvariant aMathvariant = { nVariant };
1032 aAttribute.setMlMathvariant(&aMathvariant);
1033 break;
1035 case XML_MAXSIZE:
1037 SmMlMaxsize aMaxsize;
1038 if (IsXMLToken(aIter, XML_INFINITY))
1040 aMaxsize.m_aMaxsize = SmMlAttributeValueMaxsize::MlInfinity;
1041 aMaxsize.m_aLengthValue = { SmLengthUnit::MlP, 10000, new OUString(u"10000%") };
1043 else
1045 aMaxsize.m_aMaxsize = SmMlAttributeValueMaxsize::MlFinite;
1046 aMaxsize.m_aLengthValue = handleLengthAttribute(aIter.toString());
1048 aAttribute.setMlMaxsize(&aMaxsize);
1049 break;
1051 case XML_MINSIZE:
1053 SmMlMinsize aMinsize;
1054 aMinsize.m_aLengthValue = handleLengthAttribute(aIter.toString());
1055 aAttribute.setMlMinsize(&aMinsize);
1056 break;
1058 case XML_MOVABLELIMITS:
1059 if (IsXMLToken(aIter, XML_TRUE))
1061 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlMovablelimits);
1062 SmMlMovablelimits aMovablelimits = { SmMlAttributeValueMovablelimits::MlTrue };
1063 aAttribute.setMlMovablelimits(&aMovablelimits);
1065 else if (IsXMLToken(aIter, XML_FALSE))
1067 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlMovablelimits);
1068 SmMlMovablelimits aMovablelimits = { SmMlAttributeValueMovablelimits::MlFalse };
1069 aAttribute.setMlMovablelimits(&aMovablelimits);
1071 else
1073 declareMlError();
1075 break;
1076 case XML_RSPACE:
1078 SmMlRspace aRspace;
1079 aRspace.m_aLengthValue = handleLengthAttribute(aIter.toString());
1080 aAttribute.setMlRspace(&aRspace);
1081 break;
1083 case XML_SEPARATOR:
1084 if (IsXMLToken(aIter, XML_TRUE))
1086 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlSeparator);
1087 SmMlSeparator aSeparator = { SmMlAttributeValueSeparator::MlTrue };
1088 aAttribute.setMlSeparator(&aSeparator);
1090 else if (IsXMLToken(aIter, XML_FALSE))
1092 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlSeparator);
1093 SmMlSeparator aSeparator = { SmMlAttributeValueSeparator::MlFalse };
1094 aAttribute.setMlSeparator(&aSeparator);
1096 else
1098 declareMlError();
1100 break;
1101 case XML_STRETCHY:
1102 if (IsXMLToken(aIter, XML_TRUE))
1104 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlStretchy);
1105 SmMlStretchy aStretchy = { SmMlAttributeValueStretchy::MlTrue };
1106 aAttribute.setMlStretchy(&aStretchy);
1108 else if (IsXMLToken(aIter, XML_FALSE))
1110 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlStretchy);
1111 SmMlStretchy aStretchy = { SmMlAttributeValueStretchy::MlFalse };
1112 aAttribute.setMlStretchy(&aStretchy);
1114 else
1116 declareMlError();
1118 break;
1119 case XML_SYMMETRIC:
1120 if (IsXMLToken(aIter, XML_TRUE))
1122 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlSymmetric);
1123 SmMlSymmetric aSymmetric = { SmMlAttributeValueSymmetric::MlTrue };
1124 aAttribute.setMlSymmetric(&aSymmetric);
1126 else if (IsXMLToken(aIter, XML_FALSE))
1128 aAttribute.setMlAttributeValueType(SmMlAttributeValueType::MlSymmetric);
1129 SmMlSymmetric aSymmetric = { SmMlAttributeValueSymmetric::MlFalse };
1130 aAttribute.setMlSymmetric(&aSymmetric);
1132 else
1134 declareMlError();
1136 break;
1137 default:
1138 declareMlError();
1139 break;
1141 if (aAttribute.isNullAttribute())
1142 declareMlError();
1143 else
1144 m_pElement->setAttribute(aAttribute);
1148 void SmMLImportContext::characters(const OUString& aChars) { m_pElement->setText(aChars); }
1150 void SmMLImportContext::startFastElement(sal_Int32 nElement,
1151 const Reference<XFastAttributeList>& aAttributeList)
1153 switch (nElement)
1155 case XML_ELEMENT(MATH, XML_MATH):
1156 m_pElement = new SmMlElement(SmMlElementType::MlMath);
1157 break;
1158 case XML_ELEMENT(MATH, XML_MI):
1159 m_pElement = new SmMlElement(SmMlElementType::MlMi);
1160 break;
1161 case XML_ELEMENT(MATH, XML_MERROR):
1162 m_pElement = new SmMlElement(SmMlElementType::MlMerror);
1163 break;
1164 case XML_ELEMENT(MATH, XML_MN):
1165 m_pElement = new SmMlElement(SmMlElementType::MlMn);
1166 break;
1167 case XML_ELEMENT(MATH, XML_MO):
1168 m_pElement = new SmMlElement(SmMlElementType::MlMo);
1169 break;
1170 case XML_ELEMENT(MATH, XML_MROW):
1171 m_pElement = new SmMlElement(SmMlElementType::MlMrow);
1172 break;
1173 case XML_ELEMENT(MATH, XML_MTEXT):
1174 m_pElement = new SmMlElement(SmMlElementType::MlMtext);
1175 break;
1176 case XML_ELEMENT(MATH, XML_MSTYLE):
1177 m_pElement = new SmMlElement(SmMlElementType::MlMstyle);
1178 break;
1179 default:
1180 m_pElement = new SmMlElement(SmMlElementType::NMlEmpty);
1181 declareMlError();
1182 break;
1184 SmMlElement* pParent = *m_pParent;
1185 pParent->setSubElement(pParent->getSubElementsCount(), m_pElement);
1186 inheritStyle();
1187 handleAttributes(aAttributeList);
1190 void SmMLImportContext::endFastElement(sal_Int32) { inheritStyleEnd(); }
1193 // SmMLImport
1194 /*************************************************************************************************/
1196 SvXMLImportContext*
1197 SmMLImport::CreateFastContext(sal_Int32 nElement,
1198 const uno::Reference<xml::sax::XFastAttributeList>& /*xAttrList*/)
1200 SvXMLImportContext* pContext = nullptr;
1202 switch (nElement)
1204 case XML_ELEMENT(OFFICE, XML_DOCUMENT):
1206 if (m_pElementTree == nullptr)
1207 m_pElementTree = new SmMlElement(SmMlElementType::NMlEmpty);
1208 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(GetModel(),
1209 uno::UNO_QUERY_THROW);
1210 pContext = new SmMLImportContext(*this, &m_pElementTree);
1211 break;
1213 case XML_ELEMENT(OFFICE, XML_DOCUMENT_META):
1215 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(GetModel(),
1216 uno::UNO_QUERY_THROW);
1217 pContext = new SvXMLMetaDocumentContext(*this, xDPS->getDocumentProperties());
1218 break;
1220 case XML_ELEMENT(OFFICE, XML_DOCUMENT_SETTINGS):
1222 uno::Reference<document::XDocumentPropertiesSupplier> xDPS(GetModel(),
1223 uno::UNO_QUERY_THROW);
1224 pContext = new XMLDocumentSettingsContext(*this);
1225 break;
1227 default:
1228 declareMlError();
1229 break;
1231 return pContext;
1234 void SmMLImport::endDocument()
1236 uno::Reference<frame::XModel> xModel = GetModel();
1237 if (!xModel.is())
1239 SAL_WARN("starmath", "Failed to set view settings because missing model");
1240 SvXMLImport::endDocument();
1241 return;
1244 SmModel* pModel = comphelper::getFromUnoTunnel<SmModel>(xModel);
1245 if (!pModel)
1247 SAL_WARN("starmath", "Failed to set view settings because missing sm model");
1248 SvXMLImport::endDocument();
1249 return;
1252 SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
1253 if (!pDocShell)
1255 SAL_WARN("starmath", "Failed to set view settings because missing sm doc shell");
1256 SvXMLImport::endDocument();
1257 return;
1260 // Check if there is element tree
1261 if (m_pElementTree == nullptr)
1263 m_bSuccess = true;
1264 SvXMLImport::endDocument();
1265 return;
1268 // Get element tree and setup
1270 if (m_pElementTree->getSubElementsCount() == 0)
1272 delete m_pElementTree;
1273 m_pElementTree = nullptr;
1275 else
1277 SmMlElement* pTmpElememt = m_pElementTree->getSubElement(0);
1278 delete m_pElementTree;
1279 m_pElementTree = pTmpElememt;
1281 pDocShell->SetMlElementTree(m_pElementTree);
1283 m_bSuccess = true;
1284 SvXMLImport::endDocument();
1287 void SmMLImport::SetViewSettings(const Sequence<PropertyValue>& aViewProps)
1289 uno::Reference<frame::XModel> xModel = GetModel();
1290 if (!xModel.is())
1292 SAL_WARN("starmath", "Failed to set view settings because missing model");
1293 return;
1296 SmModel* pModel = comphelper::getFromUnoTunnel<SmModel>(xModel);
1297 if (!pModel)
1299 SAL_WARN("starmath", "Failed to set view settings because missing sm model");
1300 return;
1303 SmDocShell* pDocShell = static_cast<SmDocShell*>(pModel->GetObjectShell());
1304 if (!pDocShell)
1306 SAL_WARN("starmath", "Failed to set view settings because missing sm doc shell");
1307 return;
1310 tools::Rectangle aRect(pDocShell->GetVisArea());
1312 tools::Long nTmp = 0;
1314 for (const PropertyValue& rValue : aViewProps)
1316 if (rValue.Name == "ViewAreaTop")
1318 rValue.Value >>= nTmp;
1319 aRect.SaturatingSetPosY(nTmp);
1321 else if (rValue.Name == "ViewAreaLeft")
1323 rValue.Value >>= nTmp;
1324 aRect.SaturatingSetPosX(nTmp);
1326 else if (rValue.Name == "ViewAreaWidth")
1328 rValue.Value >>= nTmp;
1329 Size aSize(aRect.GetSize());
1330 aSize.setWidth(nTmp);
1331 aRect.SaturatingSetSize(aSize);
1333 else if (rValue.Name == "ViewAreaHeight")
1335 rValue.Value >>= nTmp;
1336 Size aSize(aRect.GetSize());
1337 aSize.setHeight(nTmp);
1338 aRect.SaturatingSetSize(aSize);
1342 pDocShell->SetVisArea(aRect);
1345 void SmMLImport::SetConfigurationSettings(const Sequence<PropertyValue>& aConfProps)
1347 uno::Reference<frame::XModel> xModel = GetModel();
1348 if (!xModel.is())
1350 SAL_WARN("starmath", "Failed to set view settings because missing model");
1351 return;
1354 uno::Reference<XPropertySet> xProps(xModel, UNO_QUERY);
1355 if (!xProps.is())
1357 SAL_WARN("starmath", "Failed to set view settings because missing model properties");
1358 return;
1361 Reference<XPropertySetInfo> xInfo(xProps->getPropertySetInfo());
1362 if (!xInfo.is())
1364 SAL_WARN("starmath",
1365 "Failed to set view settings because missing model properties information");
1366 return;
1369 static const OUStringLiteral sFormula(u"Formula");
1370 static const OUStringLiteral sBasicLibraries(u"BasicLibraries");
1371 static const OUStringLiteral sDialogLibraries(u"DialogLibraries");
1372 for (const PropertyValue& rValue : aConfProps)
1374 if (rValue.Name != sFormula && rValue.Name != sBasicLibraries
1375 && rValue.Name != sDialogLibraries)
1379 if (xInfo->hasPropertyByName(rValue.Name))
1380 xProps->setPropertyValue(rValue.Name, rValue.Value);
1382 catch (const beans::PropertyVetoException&)
1384 // dealing with read-only properties here. Nothing to do...
1386 catch (const Exception&)
1388 SAL_WARN("starmath", "Unexpected issue while loading document properties");
1394 SmMLImport::SmMLImport(const css::uno::Reference<css::uno::XComponentContext>& rContext,
1395 OUString const& implementationName, SvXMLImportFlags nImportFlags)
1396 : SvXMLImport(rContext, implementationName, nImportFlags)
1397 , m_pElementTree(nullptr)
1398 , m_bSuccess(false)
1399 , m_nSmSyntaxVersion(SM_MOD()->GetConfig()->GetDefaultSmSyntaxVersion())
1403 /** Handles an error on the mathml structure
1405 void SmMLImport::declareMlError()
1407 m_bSuccess = false;
1408 SAL_WARN("starmath", "MathML error");
1411 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */