-
Notifications
You must be signed in to change notification settings - Fork 6
/
capixmlparser.cpp
194 lines (174 loc) · 5.5 KB
/
capixmlparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*------------------------------ Information ---------------------------*//**
*
* $HeadURL$
*
* @file capixmlparser.cpp
*
* @author Jo2003
*
* @date 18.04.2013
*
* $Id$
*
*///------------------------- (c) 2013 by Jo2003 --------------------------
#include "capixmlparser.h"
#include "externals_inc.h"
//---------------------------------------------------------------------------
//
//! \brief construct api xml parser
//
//! \author Jo2003
//! \date 18.04.2013
//
//! \param parent (QObject *) pointer to parent object
//
//! \return --
//---------------------------------------------------------------------------
CApiXmlParser::CApiXmlParser(QObject * parent) : CApiParser(parent)
{
// nothing to do here ...
}
//---------------------------------------------------------------------------
//
//! \brief get string value from unique xml element
//
//! \author Jo2003
//! \date 18.04.2013
//
//! \param sElement (const QString &) ref. to XML string to parse
//! \param sName (const QString &) ref. to element name
//
//! \return element value as string (if found)
//---------------------------------------------------------------------------
QString CApiXmlParser::xmlElementToValue(const QString &sElement, const QString &sName)
{
QString sValue;
QString sRegEx = QString("<%1>([^<]+)</%1>").arg(sName);
QRegExp rx(sRegEx);
if (rx.indexIn(sElement) > -1)
{
sValue = rx.cap(1);
sValue = sValue.simplified();
}
return sValue;
}
//---------------------------------------------------------------------------
//
//! \brief parse one level of XML element
//
//! \author Jo2003
//! \date 18.04.2013
//
//! \param xml (QXmlStreamReader &) ref. to XML stream reader
//! \param sEndElement (const QString &) parse until this element
//! \param slNeeded (const QStringList &) elements to look for
//! \param mResults QMap<QString, QString> &) ref. to results map
//
//! \return 0 --> ok; -1 --> any error
//---------------------------------------------------------------------------
int CApiXmlParser::oneLevelParser(QXmlStreamReader &xml, const QString &sEndElement,
const QStringList &slNeeded, QMap<QString, QString> &mResults)
{
QString sUnknown, sKey, sVal;
bool bEndMain = false;
mResults.clear();
while(!xml.atEnd() && !xml.hasError() && !bEndMain)
{
switch (xml.readNext())
{
// start element ...
case QXmlStreamReader::StartElement:
// needed element ... ?
if (slNeeded.contains(xml.name().toString()))
{
// store key / value in map ...
// make sure we add an empty string if there is no text
// inside this element.
sKey = xml.name().toString();
sVal = "";
if (xml.readNext() == QXmlStreamReader::Characters)
{
sVal = xml.text().toString();
}
mResults.insert(sKey, sVal);
}
else if (xml.name().toString() == sEndElement)
{
// maybe end element isn't searched ...
// to get the end element we should NOT count it
// as unknown ...
}
else
{
// starttag unknown element ...
sUnknown = xml.name().toString();
#ifndef QT_NO_DEBUG
mInfo(tr("Found unused element %1 ...").arg(sUnknown));
#endif
// search for endtag of unknown element ...
ignoreUntil(xml, sUnknown);
}
break;
case QXmlStreamReader::EndElement:
if (xml.name().toString() == sEndElement)
{
bEndMain = true;
}
break;
default:
break;
}
}
return 0;
}
//---------------------------------------------------------------------------
//
//! \brief parse one level of XML element
//
//! \author Jo2003
//! \date 18.04.2013
//
//! \param xml (QXmlStreamReader &) ref. to XML stream reader
//! \param sEndElement (const QString &) read forward until this element
//
//! \return 0 --> ok; -1 --> any error
//---------------------------------------------------------------------------
int CApiXmlParser::ignoreUntil(QXmlStreamReader &xml, const QString &sEndElement)
{
while(!xml.atEnd() && !xml.hasError())
{
if ((xml.readNext() == QXmlStreamReader::EndElement)
&& (xml.name().toString() == sEndElement))
{
// found end tag of searched element ...
break;
}
}
return (xml.atEnd() || xml.hasError()) ? -1 : 0;
}
//---------------------------------------------------------------------------
//
//! \brief check response for errors
//
//! \author Jo2003
//! \date 18.04.2013
//
//! \param sResp (const QString&) ref. to XML response
//! \param sMsg (QString&) store error message there
//! \param eCode (int&) store error coe there
//
//! \return 0 --> ok; -1 --> pattern doesn't match
//---------------------------------------------------------------------------
int CApiXmlParser::parseError (const QString& sResp, QString& sMsg, int& eCode)
{
int iRet = 0;
QRegExp rx("<message>(.*)</message>[ \t\n\r]*"
"<code>(.*)</code>");
// quick'n'dirty error parser ...
if ((iRet = rx.indexIn(sResp)) > -1)
{
sMsg = rx.cap(1);
eCode = rx.cap(2).toInt();
}
return iRet;
}