Attica

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

KDE's Doxygen guidelines are available online.