Attica

parser.cpp
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 2009 Eckhart W├Ârner <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "parser.h"
10 #include <QDebug>
11 
12 using namespace Attica;
13 
14 static bool stringList_contains_stringRef(const QStringList &stringList, const QStringRef &str)
15 {
16  for (const auto &string : stringList) {
17  if (str == string)
18  return true;
19  }
20  return false;
21 }
22 
23 template <class T>
24 Parser<T>::~Parser()
25 {
26 }
27 
28 template <class T>
29 T Parser<T>::parse(const QString &xmlString)
30 {
31  QStringList elements = xmlElement();
32  T item;
33 
34  QXmlStreamReader xml(xmlString);
35 
36  while (!xml.atEnd()) {
37  xml.readNext();
38 
39  if (xml.isStartElement()) {
40  if (xml.name() == QLatin1String("meta")) {
41  parseMetadataXml(xml);
42  } else if (stringList_contains_stringRef(elements, xml.name())) {
43  item = parseXml(xml);
44  }
45  }
46  }
47  if (xml.hasError()) {
48  // TODO: error handling in metadata?
49  qWarning() << "parse():: XML Error: " << xml.errorString() << "\nIn XML:\n" << xmlString;
50  }
51 
52  return item;
53 }
54 
55 template <class T>
56 typename T::List Parser<T>::parseList(const QString &xmlString)
57 {
58  /*
59  QString testxml = QString("<?xml version=\"1.0\"?>\
60  <ocs>\
61  <meta>\
62  <status>ok</status>\
63  <statuscode>100</statuscode>\
64  <message></message>\
65  </meta>\
66  <data>\
67  <buildservice>\
68  <id>obs</id>\
69  <name>openSUSE Build Service</name>\
70  <registrationurl>foobar.com</registrationurl>\
71  <supportedtargets>\
72  <target>openSUSE 11.2 32bit Intel</target>\
73  <target>openSUSE 11.3 64bit Intel</target>\
74  <target>openSUSE 11.3 32bit Intel</target>\
75  <target>openSUSE 11.3 64bit Intel</target>\
76  </supportedtargets>\
77  </buildservice>\
78  <buildservice>\
79  <id>mbs</id>\
80  <name>MeeGo Build Service</name>\
81  <registrationurl>foobar42.com</registrationurl>\
82  <supportedtargets>\
83  <target>MeeGo 1.0 Intel</target>\
84  <target>MeeGo 1.0 ARM</target>\
85  <target>MeeGo 1.1 Intel</target>\
86  <target>MeeGo 1.1 ARM</target>\
87  </supportedtargets>\
88  </buildservice>\
89  <buildservice>\
90  <id>sbs</id>\
91  <name>Sebas' Build Service</name>\
92  <registrationurl>foobar42.com</registrationurl>\
93  <supportedtargets>\
94  <target>sebasix 1.3 33bit</target>\
95  <target>sebasis 4.4 14bit</target>\
96  <target>sebasix 1.3 65bit</target>\
97  <target>sebasis 4.4 37bit</target>\
98  </supportedtargets>\
99  </buildservice>\
100  </data>\
101  </ocs>\
102  ");
103 
104  qCDebug(ATTICA) << "parsing list:" << xmlString;
105  */
106  QStringList elements = xmlElement();
107  typename T::List items;
108 
109  //QXmlStreamReader xml( xmlString );
110  QXmlStreamReader xml(xmlString);
111 
112  while (!xml.atEnd()) {
113  xml.readNext();
114  //qCDebug(ATTICA) << "parseList():: Looking for:" << xml.name().toString();
115  if (xml.isStartElement()) {
116  if (xml.name() == QLatin1String("data")) {
117  while (!xml.atEnd()) {
118  xml.readNext();
119 
120  if (xml.isEndElement() && xml.name() == QLatin1String("data")) {
121  break;
122  }
123 
124  if (xml.isStartElement() && stringList_contains_stringRef(elements, xml.name())) {
125  //qCDebug(ATTICA) << "xxxxxxxxx New Item!" << xml.name().toString();
126  items.append(parseXml(xml));
127  }
128  }
129  } else if (xml.name() == QLatin1String("meta")) {
130  parseMetadataXml(xml);
131  }
132  }
133  }
134  if (xml.hasError()) {
135  // TODO: error handling in metadata?
136  qWarning() << "parseList():: XML Error: " << xml.errorString() << "\nIn xml name" << xml.name() << "with text" << xml.text() << "at offset:\n" << xml.characterOffset() << "\nIn XML:\n" << xmlString;
137  }
138 
139  return items;
140 }
141 
142 template <class T>
143 void Parser<T>::parseMetadataXml(QXmlStreamReader &xml)
144 {
145  while (!xml.atEnd()) {
146  xml.readNext();
147  if (xml.isEndElement() && xml.name() == QLatin1String("meta")) {
148  break;
149  } else if (xml.isStartElement()) {
150  if (xml.name() == QLatin1String("status")) {
151  m_metadata.setStatusString(xml.readElementText());
152  } else if (xml.name() == QLatin1String("statuscode")) {
153  m_metadata.setStatusCode(xml.readElementText().toInt());
154  } else if (xml.name() == QLatin1String("message")) {
155  m_metadata.setMessage(xml.readElementText());
156  } else if (xml.name() == QLatin1String("totalitems")) {
157  m_metadata.setTotalItems(xml.readElementText().toInt());
158  } else if (xml.name() == QLatin1String("itemsperpage")) {
159  m_metadata.setItemsPerPage(xml.readElementText().toInt());
160  }
161  }
162  }
163  if (xml.hasError()) {
164  // TODO: error handling in metadata?
165  qWarning() << "XML Error: " << xml.errorString();
166  }
167 
168 }
169 
170 template <class T>
171 Metadata Parser<T>::metadata() const
172 {
173  return m_metadata;
174 }
bool atEnd() const const
QString errorString() const const
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
int toInt(bool *ok, int base) const const
Status messages from the server.
Definition: metadata.h:25
QXmlStreamReader::TokenType readNext()
bool isStartElement() const const
bool hasError() const const
QStringRef name() const const
bool isEndElement() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 23:06:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.