Torc  0.1
torcxmlreader.cpp
Go to the documentation of this file.
1 /* Class TorcXMLReader
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2015-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QFile>
25 #include <QBuffer>
26 
27 // Torc
28 #include "torclogging.h"
29 #include "torcxmlreader.h"
30 
43 TorcXMLReader::TorcXMLReader(const QString &File)
44  : m_reader(),
45  m_map(),
46  m_valid(false),
47  m_message()
48 {
49  if (!QFile::exists(File))
50  {
51  m_message = QStringLiteral("File '%1' does not exist").arg(File);
52  return;
53  }
54 
55  QFile file(File);
56  if (!file.open(QIODevice::ReadOnly))
57  {
58  m_message = QStringLiteral("Cannot open file '%1' for reading").arg(File);
59  return;
60  }
61 
62  m_reader.setDevice(&file);
63  m_valid = ReadXML();
64 
65  file.close();
66 }
67 
69  : m_reader(),
70  m_map(),
71  m_valid(false),
72  m_message()
73 {
74  QBuffer buffer(&Data);
75  buffer.open(QBuffer::ReadOnly);
76  m_reader.setDevice(&buffer);
77  m_valid = ReadXML();
78 }
79 
80 bool TorcXMLReader::IsValid(QString &Message) const
81 {
82  Message = m_message;
83  return m_valid;
84 }
85 
86 QVariantMap TorcXMLReader::GetResult(void) const
87 {
88  return m_map;
89 }
90 
91 bool TorcXMLReader::ReadXML(void)
92 {
93  if (!m_reader.readNextStartElement())
94  return false;
95 
96  QString root = m_reader.name().toString();
97  QVariantMap objects;
98  while (m_reader.readNextStartElement())
99  if (!ReadElement(objects))
100  return false;
101  m_map.insert(root, objects);
102  return true;
103 }
104 
108 bool TorcXMLReader::ReadElement(QVariantMap &Map)
109 {
110  QString name = m_reader.name().toString();
111  QVariantMap element;
112 
113  bool done = false;
114  while (!done)
115  {
116  switch (m_reader.readNext())
117  {
118  case QXmlStreamReader::Invalid:
119  m_message = QStringLiteral("XML error: '%1' at line %2:%3").arg(m_reader.errorString()).arg(m_reader.lineNumber()).arg(m_reader.columnNumber());
120  return false;
121  case QXmlStreamReader::EntityReference:
122  case QXmlStreamReader::Characters:
123  if (!m_reader.isWhitespace())
124  Map.insertMulti(name, m_reader.text().toString());
125  break;
126  case QXmlStreamReader::StartElement:
127  if (!ReadElement(element))
128  return false;
129  break;
130  case QXmlStreamReader::EndDocument:
131  case QXmlStreamReader::EndElement:
132  done = true;
133  case QXmlStreamReader::NoToken:
134  case QXmlStreamReader::StartDocument:
135  case QXmlStreamReader::ProcessingInstruction:
136  case QXmlStreamReader::Comment:
137  case QXmlStreamReader::DTD:
138  break;
139  }
140  }
141 
142  if (!element.isEmpty())
143  Map.insertMulti(name, element);
144 
145  return true;
146 }
TorcXMLReader(const QString &File)
QVariantMap GetResult(void) const
bool IsValid(QString &Message) const