labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

XmlStreamReader.cpp
Go to the documentation of this file.
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 
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  */
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  */
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 
82  return !m_warnings.isEmpty();
83 }
84 
86  return !m_missingCASPlugins.isEmpty();
87 }
88 
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  */
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  */
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  */
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 }
void setFailedCASMissing(bool)
const QStringList & warningStrings() const
bool hasWarnings() const
bool hasMissingCASWarnings() const
void raiseWarning(const QString &)
QStringList m_warnings
int readAttributeInt(const QString &name, bool *ok)
QStringList m_missingCASPlugins
void raiseError(const QString &)
QString missingCASWarning() const
void raiseMissingCASWarning(const QString &)
bool failedCASMissing() const
#define i18n(m)
Definition: nsl_common.h:38