"Fossies" - the Fresh Open Source Software Archive

Member "labplot-2.8.2/src/backend/lib/XmlStreamReader.cpp" (24 Feb 2021, 6082 Bytes) of package /linux/privat/labplot-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "XmlStreamReader.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8.1_vs_2.8.2.

    1 /***************************************************************************
    2     File                 : XmlStreamReader.cpp
    3     Project              : LabPlot
    4     Description          : XML stream parser that supports errors and warnings
    5     --------------------------------------------------------------------
    6     Copyright            : (C) 2009 Tilman Benkert (thzs@gmx.net)
    7     Copyright            : (C) 2015-2016 Alexander Semke (alexander.semke@web.de)
    8  ***************************************************************************/
    9 
   10 /***************************************************************************
   11  *                                                                         *
   12  *  This program is free software; you can redistribute it and/or modify   *
   13  *  it under the terms of the GNU General Public License as published by   *
   14  *  the Free Software Foundation; either version 2 of the License, or      *
   15  *  (at your option) any later version.                                    *
   16  *                                                                         *
   17  *  This program is distributed in the hope that it will be useful,        *
   18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
   19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
   20  *  GNU General Public License for more details.                           *
   21  *                                                                         *
   22  *   You should have received a copy of the GNU General Public License     *
   23  *   along with this program; if not, write to the Free Software           *
   24  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
   25  *   Boston, MA  02110-1301  USA                                           *
   26  *                                                                         *
   27  ***************************************************************************/
   28 
   29 #include "backend/lib/XmlStreamReader.h"
   30 #include <KLocalizedString>
   31 
   32 /**
   33  * \class XmlStreamReader
   34  * \brief XML stream parser that supports errors as well as warnings.
   35  * This class also adds line and column numbers to the error message.
   36  */
   37 XmlStreamReader::XmlStreamReader() = default;
   38 
   39 XmlStreamReader::XmlStreamReader(QIODevice* device) : QXmlStreamReader(device) {
   40 }
   41 
   42 XmlStreamReader::XmlStreamReader(const QByteArray& data) : QXmlStreamReader(data) {
   43 }
   44 
   45 XmlStreamReader::XmlStreamReader(const QString& data) : QXmlStreamReader(data) {
   46 }
   47 
   48 XmlStreamReader::XmlStreamReader(const char* data) : QXmlStreamReader(data) {
   49 }
   50 
   51 const QStringList& XmlStreamReader::warningStrings() const {
   52     return m_warnings;
   53 }
   54 
   55 /*
   56  * returns the human readable string for the missing CAS plugins in case
   57  * the project has some CAS content but the application was either not compiled with
   58  * CAS/Cantor support or the correspongind plugins for the required backends are missing.
   59  *
   60  * The returned text is in the form "Octave" or "Octave and Maxima" or "Octave, Maxima and Python", etc.
   61  */
   62 QString XmlStreamReader::missingCASWarning() const {
   63     const int count = m_missingCASPlugins.count();
   64     if (count == 1)
   65         return m_missingCASPlugins.constFirst();
   66     else {
   67         QString msg;
   68         for (int i = 0; i < count; ++ i) {
   69             if (!msg.isEmpty()) {
   70                 if (i == count - 1)
   71                     msg += QLatin1Char(' ') + i18n("and") + QLatin1Char(' ');
   72                 else
   73                     msg += QLatin1String(", ");
   74             }
   75             msg += m_missingCASPlugins.at(i);
   76         }
   77         return msg;
   78     }
   79 }
   80 
   81 bool XmlStreamReader::hasWarnings() const {
   82     return !m_warnings.isEmpty();
   83 }
   84 
   85 bool XmlStreamReader::hasMissingCASWarnings() const {
   86     return !m_missingCASPlugins.isEmpty();
   87 }
   88 
   89 void XmlStreamReader::setFailedCASMissing(bool value) {
   90     m_failedCASMissing = value;
   91 }
   92 
   93 /*!
   94  * returns \c true if the loading of an project object failed because
   95  * of the missing CAS functionality (no CAS support or missing CAS plugin).
   96  * returns \c false if the loadign failed because of other reasons
   97  * like broken XML or missing important and required attributes.
   98  */
   99 bool XmlStreamReader::failedCASMissing() const {
  100     return m_failedCASMissing;
  101 }
  102 
  103 void XmlStreamReader::raiseError(const QString & message) {
  104     QXmlStreamReader::raiseError(i18n("line %1, column %2: %3", lineNumber(), columnNumber(), message));
  105 }
  106 
  107 void XmlStreamReader::raiseWarning(const QString & message) {
  108     m_warnings.append(i18n("line %1, column %2: %3", lineNumber(), columnNumber(), message));
  109 }
  110 
  111 void XmlStreamReader::raiseMissingCASWarning(const QString& name) {
  112     m_missingCASPlugins.append(name);
  113 }
  114 
  115 /*!
  116   * Go to the next start or end element tag
  117   * If the end of the document is reached, an error is raised.
  118   * \return false if end of document reached, otherwise true
  119   */
  120 bool XmlStreamReader::skipToNextTag() {
  121     if (atEnd()) {
  122         raiseError(i18n("unexpected end of document"));
  123         return false;
  124     }
  125 
  126     do {
  127         readNext();
  128     } while (!(isStartElement() || isEndElement() || atEnd()));
  129 
  130     if (atEnd()) {
  131         raiseError(i18n("unexpected end of document"));
  132         return false;
  133     }
  134 
  135     return true;
  136 }
  137 
  138 /*!
  139   * Go to the end element tag of the current element
  140   * If the end of the document is reached, an error is raised.
  141   * \return false if end of document reached, otherwise true
  142   */
  143 bool XmlStreamReader::skipToEndElement() {
  144     int depth = 1;
  145     if (atEnd()) {
  146         raiseError(i18n("unexpected end of document"));
  147         return false;
  148     }
  149 
  150     do {
  151         readNext();
  152         if (isEndElement()) depth--;
  153         if (isStartElement()) depth++;
  154     } while (!((isEndElement() && depth == 0) || atEnd()));
  155 
  156     if (atEnd()) {
  157         raiseError(i18n("unexpected end of document"));
  158         return false;
  159     }
  160 
  161     return true;
  162 }
  163 
  164 /*!
  165  * Read an XML attribute and convert it to int
  166   * \param name attribute name
  167   * \param ok pointer to report back whether the attribute value could be determined (may be NULL)
  168   * \return the attribute value if found and converted, otherwise zero (in this case *ok is false)
  169   */
  170 int XmlStreamReader::readAttributeInt(const QString& name, bool* ok) {
  171     QString str = attributes().value(namespaceUri().toString(), name).toString();
  172     if (str.isEmpty()) {
  173         if (ok) *ok = false;
  174         return 0;
  175     }
  176 
  177     return str.toInt(ok);
  178 }