2
* xmlcommon.cpp - helper functions for dealing with XML
3
* Copyright (C) 2001, 2002 Justin Karneges
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include"xmpp_xmlcommon.h"
25
#include <qdatetime.h>
28
#include <qstringlist.h>
33
bool stamp2TS(const QString &ts, QDateTime *d)
38
int year = ts.mid(0,4).toInt();
39
int month = ts.mid(4,2).toInt();
40
int day = ts.mid(6,2).toInt();
42
int hour = ts.mid(9,2).toInt();
43
int min = ts.mid(12,2).toInt();
44
int sec = ts.mid(15,2).toInt();
47
xd.setYMD(year, month, day);
52
xt.setHMS(hour, min, sec);
62
QString TS2stamp(const QDateTime &d)
66
str.sprintf("%04d%02d%02dT%02d:%02d:%02d",
77
QDomElement textTag(QDomDocument *doc, const QString &name, const QString &content)
79
QDomElement tag = doc->createElement(name);
80
QDomText text = doc->createTextNode(content);
81
tag.appendChild(text);
86
QString tagContent(const QDomElement &e)
88
// look for some tag content
89
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
90
QDomText i = n.toText();
99
QDomElement findSubTag(const QDomElement &e, const QString &name, bool *found)
104
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
105
QDomElement i = n.toElement();
108
if(i.tagName() == name) {
119
QDomElement createIQ(QDomDocument *doc, const QString &type, const QString &to, const QString &id)
121
QDomElement iq = doc->createElement("iq");
123
iq.setAttribute("type", type);
125
iq.setAttribute("to", to);
127
iq.setAttribute("id", id);
132
QDomElement queryTag(const QDomElement &e)
135
QDomElement q = findSubTag(e, "query", &found);
139
QString queryNS(const QDomElement &e)
142
QDomElement q = findSubTag(e, "query", &found);
144
return q.attribute("xmlns");
149
void getErrorFromElement(const QDomElement &e, int *code, QString *str)
152
QDomElement tag = findSubTag(e, "error", &found);
157
*code = tag.attribute("code").toInt();
159
*str = tagContent(tag);
162
//----------------------------------------------------------------------------
164
//----------------------------------------------------------------------------
166
namespace XMLHelper {
168
QDomElement emptyTag(QDomDocument *doc, const QString &name)
170
QDomElement tag = doc->createElement(name);
175
bool hasSubTag(const QDomElement &e, const QString &name)
178
findSubTag(e, name, &found);
182
QString subTagText(const QDomElement &e, const QString &name)
185
QDomElement i = findSubTag(e, name, &found);
188
return QString::null;
191
QDomElement textTag(QDomDocument &doc, const QString &name, const QString &content)
193
QDomElement tag = doc.createElement(name);
194
QDomText text = doc.createTextNode(content);
195
tag.appendChild(text);
200
QDomElement textTag(QDomDocument &doc, const QString &name, int content)
202
QDomElement tag = doc.createElement(name);
203
QDomText text = doc.createTextNode(QString::number(content));
204
tag.appendChild(text);
209
QDomElement textTag(QDomDocument &doc, const QString &name, bool content)
211
QDomElement tag = doc.createElement(name);
212
QDomText text = doc.createTextNode(content ? "true" : "false");
213
tag.appendChild(text);
218
QDomElement textTag(QDomDocument &doc, const QString &name, QSize &s)
221
str.sprintf("%d,%d", s.width(), s.height());
223
QDomElement tag = doc.createElement(name);
224
QDomText text = doc.createTextNode(str);
225
tag.appendChild(text);
230
QDomElement textTag(QDomDocument &doc, const QString &name, QRect &r)
233
str.sprintf("%d,%d,%d,%d", r.x(), r.y(), r.width(), r.height());
235
QDomElement tag = doc.createElement(name);
236
QDomText text = doc.createTextNode(str);
237
tag.appendChild(text);
242
QDomElement stringListToXml(QDomDocument &doc, const QString &name, const QStringList &l)
244
QDomElement tag = doc.createElement(name);
245
for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it)
246
tag.appendChild(textTag(doc, "item", *it));
251
/*QString tagContent(const QDomElement &e)
253
// look for some tag content
254
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
255
QDomText i = n.toText();
264
/*QDomElement findSubTag(const QDomElement &e, const QString &name, bool *found)
269
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
270
QDomElement i = n.toElement();
273
if(i.tagName() == name) {
284
void readEntry(const QDomElement &e, const QString &name, QString *v)
287
QDomElement tag = findSubTag(e, name, &found);
290
*v = tagContent(tag);
293
void readNumEntry(const QDomElement &e, const QString &name, int *v)
296
QDomElement tag = findSubTag(e, name, &found);
299
*v = tagContent(tag).toInt();
302
void readBoolEntry(const QDomElement &e, const QString &name, bool *v)
305
QDomElement tag = findSubTag(e, name, &found);
308
*v = (tagContent(tag) == "true") ? TRUE: FALSE;
311
void readSizeEntry(const QDomElement &e, const QString &name, QSize *v)
314
QDomElement tag = findSubTag(e, name, &found);
317
QStringList list = QStringList::split(',', tagContent(tag));
318
if(list.count() != 2)
321
s.setWidth(list[0].toInt());
322
s.setHeight(list[1].toInt());
326
void readRectEntry(const QDomElement &e, const QString &name, QRect *v)
329
QDomElement tag = findSubTag(e, name, &found);
332
QStringList list = QStringList::split(',', tagContent(tag));
333
if(list.count() != 4)
336
r.setX(list[0].toInt());
337
r.setY(list[1].toInt());
338
r.setWidth(list[2].toInt());
339
r.setHeight(list[3].toInt());
343
void readColorEntry(const QDomElement &e, const QString &name, QColor *v)
346
QDomElement tag = findSubTag(e, name, &found);
350
c.setNamedColor(tagContent(tag));
355
void xmlToStringList(const QDomElement &e, const QString &name, QStringList *v)
358
QDomElement tag = findSubTag(e, name, &found);
362
for(QDomNode n = tag.firstChild(); !n.isNull(); n = n.nextSibling()) {
363
QDomElement i = n.toElement();
366
if(i.tagName() == "item")
367
list += tagContent(i);
372
void setBoolAttribute(QDomElement e, const QString &name, bool b)
374
e.setAttribute(name, b ? "true" : "false");
377
void readBoolAttribute(QDomElement e, const QString &name, bool *v)
379
if(e.hasAttribute(name)) {
380
QString s = e.attribute(name);
381
*v = (s == "true") ? TRUE: FALSE;