From 3e33591093801244ae88ea0c46a62b84cbaf7bc1 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 8 Oct 2023 19:17:53 +0200 Subject: [PATCH] Core: add method to XMLReader to access next element of an XML file If the next element could be read-in successfully true is returned, and false otherwise. --- src/Base/Reader.cpp | 28 ++++++++++++++++++++++++++++ src/Base/Reader.h | 10 ++++++++++ src/Gui/Document.cpp | 15 ++++++--------- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Base/Reader.cpp b/src/Base/Reader.cpp index 189a1c34d3..d7b355e42c 100644 --- a/src/Base/Reader.cpp +++ b/src/Base/Reader.cpp @@ -222,10 +222,38 @@ void Base::XMLReader::readElement(const char* ElementName) (ElementName && LocalName != ElementName)); } +bool Base::XMLReader::readNextElement() +{ + bool ok{}; + while (true) { + ok = read(); + if (!ok) + break; + if (ReadType == StartEndElement) + break; + if (ReadType == EndElement) + break; + if (ReadType == EndDocument) + break; + }; + + return (ReadType == StartEndElement); +} + int Base::XMLReader::level() const { return Level; } +bool Base::XMLReader::isEndOfElement() const +{ + return (ReadType == EndElement); +} + +bool Base::XMLReader::isEndOfDocument() const +{ + return (ReadType == EndDocument); +} + void Base::XMLReader::readEndElement(const char* ElementName, int level) { // if we are already at the end of the current element diff --git a/src/Base/Reader.h b/src/Base/Reader.h index 64149820a0..9f3831548d 100644 --- a/src/Base/Reader.h +++ b/src/Base/Reader.h @@ -146,9 +146,19 @@ public: const char* localName() const; /// get the current element level int level() const; + + /// return true if the end of an element is reached, false otherwise + bool isEndOfElement() const; + + /// return true if the end of the document is reached, false otherwise + bool isEndOfDocument() const; + /// read until a start element is found (\) or start-end element (\) (with special name if given) void readElement (const char* ElementName=nullptr); + /// Read in the next element. Return true if it succeeded and false otherwise + bool readNextElement(); + /** read until an end element is found * * @param ElementName: optional end element name to look for. If given, then diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index a0165edf06..ce1ceed12f 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -1486,15 +1486,12 @@ void Document::RestoreDocFile(Base::Reader &reader) } } - //TODO: Implement a method to start the next element and returns its name - try { - localreader->readElement("ProjectUnitSystem"); - d->projectUnitSystem = localreader->getAttributeAsInteger("US"); - d->projectUnitSystemIgnore = localreader->getAttributeAsInteger("ignore"); - localreader->readEndElement("Document"); - } - catch (const Base::XMLParseException) { - // fails for older project files + if (localreader->readNextElement()) { + if (strcmp(localreader->localName(), "ProjectUnitSystem") == 0) { + d->projectUnitSystem = localreader->getAttributeAsInteger("US"); + d->projectUnitSystemIgnore = localreader->getAttributeAsInteger("ignore"); + localreader->readEndElement("Document"); + } } }