1 | /*
|
---|
2 | * xmlcommon.cpp - helper functions for dealing with XML
|
---|
3 | * Copyright (C) 2001, 2002 Justin Karneges
|
---|
4 | *
|
---|
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.
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
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
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include"xmpp_xmlcommon.h"
|
---|
22 |
|
---|
23 | #include <qstring.h>
|
---|
24 | #include <qdom.h>
|
---|
25 | #include <qdatetime.h>
|
---|
26 | #include <qsize.h>
|
---|
27 | #include <qrect.h>
|
---|
28 | #include <qstringlist.h>
|
---|
29 | #include <qcolor.h>
|
---|
30 |
|
---|
31 | #include"im.h"
|
---|
32 |
|
---|
33 | bool stamp2TS(const QString &ts, QDateTime *d)
|
---|
34 | {
|
---|
35 | if(ts.length() != 17)
|
---|
36 | return false;
|
---|
37 |
|
---|
38 | int year = ts.mid(0,4).toInt();
|
---|
39 | int month = ts.mid(4,2).toInt();
|
---|
40 | int day = ts.mid(6,2).toInt();
|
---|
41 |
|
---|
42 | int hour = ts.mid(9,2).toInt();
|
---|
43 | int min = ts.mid(12,2).toInt();
|
---|
44 | int sec = ts.mid(15,2).toInt();
|
---|
45 |
|
---|
46 | QDate xd;
|
---|
47 | xd.setYMD(year, month, day);
|
---|
48 | if(!xd.isValid())
|
---|
49 | return false;
|
---|
50 |
|
---|
51 | QTime xt;
|
---|
52 | xt.setHMS(hour, min, sec);
|
---|
53 | if(!xt.isValid())
|
---|
54 | return false;
|
---|
55 |
|
---|
56 | d->setDate(xd);
|
---|
57 | d->setTime(xt);
|
---|
58 |
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | QString TS2stamp(const QDateTime &d)
|
---|
63 | {
|
---|
64 | QString str;
|
---|
65 |
|
---|
66 | str.sprintf("%04d%02d%02dT%02d:%02d:%02d",
|
---|
67 | d.date().year(),
|
---|
68 | d.date().month(),
|
---|
69 | d.date().day(),
|
---|
70 | d.time().hour(),
|
---|
71 | d.time().minute(),
|
---|
72 | d.time().second());
|
---|
73 |
|
---|
74 | return str;
|
---|
75 | }
|
---|
76 |
|
---|
77 | QDomElement textTag(QDomDocument *doc, const QString &name, const QString &content)
|
---|
78 | {
|
---|
79 | QDomElement tag = doc->createElement(name);
|
---|
80 | QDomText text = doc->createTextNode(content);
|
---|
81 | tag.appendChild(text);
|
---|
82 |
|
---|
83 | return tag;
|
---|
84 | }
|
---|
85 |
|
---|
86 | QString tagContent(const QDomElement &e)
|
---|
87 | {
|
---|
88 | // look for some tag content
|
---|
89 | for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
---|
90 | QDomText i = n.toText();
|
---|
91 | if(i.isNull())
|
---|
92 | continue;
|
---|
93 | return i.data();
|
---|
94 | }
|
---|
95 |
|
---|
96 | return "";
|
---|
97 | }
|
---|
98 |
|
---|
99 | QDomElement findSubTag(const QDomElement &e, const QString &name, bool *found)
|
---|
100 | {
|
---|
101 | if(found)
|
---|
102 | *found = false;
|
---|
103 |
|
---|
104 | for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
---|
105 | QDomElement i = n.toElement();
|
---|
106 | if(i.isNull())
|
---|
107 | continue;
|
---|
108 | if(i.tagName() == name) {
|
---|
109 | if(found)
|
---|
110 | *found = true;
|
---|
111 | return i;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | QDomElement tmp;
|
---|
116 | return tmp;
|
---|
117 | }
|
---|
118 |
|
---|
119 | QDomElement createIQ(QDomDocument *doc, const QString &type, const QString &to, const QString &id)
|
---|
120 | {
|
---|
121 | QDomElement iq = doc->createElement("iq");
|
---|
122 | if(!type.isEmpty())
|
---|
123 | iq.setAttribute("type", type);
|
---|
124 | if(!to.isEmpty())
|
---|
125 | iq.setAttribute("to", to);
|
---|
126 | if(!id.isEmpty())
|
---|
127 | iq.setAttribute("id", id);
|
---|
128 |
|
---|
129 | return iq;
|
---|
130 | }
|
---|
131 |
|
---|
132 | QDomElement queryTag(const QDomElement &e)
|
---|
133 | {
|
---|
134 | bool found;
|
---|
135 | QDomElement q = findSubTag(e, "query", &found);
|
---|
136 | return q;
|
---|
137 | }
|
---|
138 |
|
---|
139 | QString queryNS(const QDomElement &e)
|
---|
140 | {
|
---|
141 | bool found;
|
---|
142 | QDomElement q = findSubTag(e, "query", &found);
|
---|
143 | if(found)
|
---|
144 | return q.attribute("xmlns");
|
---|
145 |
|
---|
146 | return "";
|
---|
147 | }
|
---|
148 |
|
---|
149 | void getErrorFromElement(const QDomElement &e, int *code, QString *str)
|
---|
150 | {
|
---|
151 | bool found;
|
---|
152 | QDomElement tag = findSubTag(e, "error", &found);
|
---|
153 | if(!found)
|
---|
154 | return;
|
---|
155 |
|
---|
156 | if(code)
|
---|
157 | *code = tag.attribute("code").toInt();
|
---|
158 | if(str)
|
---|
159 | *str = tagContent(tag);
|
---|
160 | }
|
---|
161 |
|
---|
162 | //----------------------------------------------------------------------------
|
---|
163 | // XMLHelper
|
---|
164 | //----------------------------------------------------------------------------
|
---|
165 |
|
---|
166 | namespace XMLHelper {
|
---|
167 |
|
---|
168 | QDomElement emptyTag(QDomDocument *doc, const QString &name)
|
---|
169 | {
|
---|
170 | QDomElement tag = doc->createElement(name);
|
---|
171 |
|
---|
172 | return tag;
|
---|
173 | }
|
---|
174 |
|
---|
175 | bool hasSubTag(const QDomElement &e, const QString &name)
|
---|
176 | {
|
---|
177 | bool found;
|
---|
178 | findSubTag(e, name, &found);
|
---|
179 | return found;
|
---|
180 | }
|
---|
181 |
|
---|
182 | QString subTagText(const QDomElement &e, const QString &name)
|
---|
183 | {
|
---|
184 | bool found;
|
---|
185 | QDomElement i = findSubTag(e, name, &found);
|
---|
186 | if ( found )
|
---|
187 | return i.text();
|
---|
188 | return QString::null;
|
---|
189 | }
|
---|
190 |
|
---|
191 | QDomElement textTag(QDomDocument &doc, const QString &name, const QString &content)
|
---|
192 | {
|
---|
193 | QDomElement tag = doc.createElement(name);
|
---|
194 | QDomText text = doc.createTextNode(content);
|
---|
195 | tag.appendChild(text);
|
---|
196 |
|
---|
197 | return tag;
|
---|
198 | }
|
---|
199 |
|
---|
200 | QDomElement textTag(QDomDocument &doc, const QString &name, int content)
|
---|
201 | {
|
---|
202 | QDomElement tag = doc.createElement(name);
|
---|
203 | QDomText text = doc.createTextNode(QString::number(content));
|
---|
204 | tag.appendChild(text);
|
---|
205 |
|
---|
206 | return tag;
|
---|
207 | }
|
---|
208 |
|
---|
209 | QDomElement textTag(QDomDocument &doc, const QString &name, bool content)
|
---|
210 | {
|
---|
211 | QDomElement tag = doc.createElement(name);
|
---|
212 | QDomText text = doc.createTextNode(content ? "true" : "false");
|
---|
213 | tag.appendChild(text);
|
---|
214 |
|
---|
215 | return tag;
|
---|
216 | }
|
---|
217 |
|
---|
218 | QDomElement textTag(QDomDocument &doc, const QString &name, QSize &s)
|
---|
219 | {
|
---|
220 | QString str;
|
---|
221 | str.sprintf("%d,%d", s.width(), s.height());
|
---|
222 |
|
---|
223 | QDomElement tag = doc.createElement(name);
|
---|
224 | QDomText text = doc.createTextNode(str);
|
---|
225 | tag.appendChild(text);
|
---|
226 |
|
---|
227 | return tag;
|
---|
228 | }
|
---|
229 |
|
---|
230 | QDomElement textTag(QDomDocument &doc, const QString &name, QRect &r)
|
---|
231 | {
|
---|
232 | QString str;
|
---|
233 | str.sprintf("%d,%d,%d,%d", r.x(), r.y(), r.width(), r.height());
|
---|
234 |
|
---|
235 | QDomElement tag = doc.createElement(name);
|
---|
236 | QDomText text = doc.createTextNode(str);
|
---|
237 | tag.appendChild(text);
|
---|
238 |
|
---|
239 | return tag;
|
---|
240 | }
|
---|
241 |
|
---|
242 | QDomElement stringListToXml(QDomDocument &doc, const QString &name, const QStringList &l)
|
---|
243 | {
|
---|
244 | QDomElement tag = doc.createElement(name);
|
---|
245 | for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it)
|
---|
246 | tag.appendChild(textTag(doc, "item", *it));
|
---|
247 |
|
---|
248 | return tag;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /*QString tagContent(const QDomElement &e)
|
---|
252 | {
|
---|
253 | // look for some tag content
|
---|
254 | for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
---|
255 | QDomText i = n.toText();
|
---|
256 | if(i.isNull())
|
---|
257 | continue;
|
---|
258 | return i.data();
|
---|
259 | }
|
---|
260 |
|
---|
261 | return "";
|
---|
262 | }*/
|
---|
263 |
|
---|
264 | /*QDomElement findSubTag(const QDomElement &e, const QString &name, bool *found)
|
---|
265 | {
|
---|
266 | if(found)
|
---|
267 | *found = FALSE;
|
---|
268 |
|
---|
269 | for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
---|
270 | QDomElement i = n.toElement();
|
---|
271 | if(i.isNull())
|
---|
272 | continue;
|
---|
273 | if(i.tagName() == name) {
|
---|
274 | if(found)
|
---|
275 | *found = TRUE;
|
---|
276 | return i;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | QDomElement tmp;
|
---|
281 | return tmp;
|
---|
282 | }*/
|
---|
283 |
|
---|
284 | void readEntry(const QDomElement &e, const QString &name, QString *v)
|
---|
285 | {
|
---|
286 | bool found = FALSE;
|
---|
287 | QDomElement tag = findSubTag(e, name, &found);
|
---|
288 | if(!found)
|
---|
289 | return;
|
---|
290 | *v = tagContent(tag);
|
---|
291 | }
|
---|
292 |
|
---|
293 | void readNumEntry(const QDomElement &e, const QString &name, int *v)
|
---|
294 | {
|
---|
295 | bool found = FALSE;
|
---|
296 | QDomElement tag = findSubTag(e, name, &found);
|
---|
297 | if(!found)
|
---|
298 | return;
|
---|
299 | *v = tagContent(tag).toInt();
|
---|
300 | }
|
---|
301 |
|
---|
302 | void readBoolEntry(const QDomElement &e, const QString &name, bool *v)
|
---|
303 | {
|
---|
304 | bool found = FALSE;
|
---|
305 | QDomElement tag = findSubTag(e, name, &found);
|
---|
306 | if(!found)
|
---|
307 | return;
|
---|
308 | *v = (tagContent(tag) == "true") ? TRUE: FALSE;
|
---|
309 | }
|
---|
310 |
|
---|
311 | void readSizeEntry(const QDomElement &e, const QString &name, QSize *v)
|
---|
312 | {
|
---|
313 | bool found = FALSE;
|
---|
314 | QDomElement tag = findSubTag(e, name, &found);
|
---|
315 | if(!found)
|
---|
316 | return;
|
---|
317 | QStringList list = QStringList::split(',', tagContent(tag));
|
---|
318 | if(list.count() != 2)
|
---|
319 | return;
|
---|
320 | QSize s;
|
---|
321 | s.setWidth(list[0].toInt());
|
---|
322 | s.setHeight(list[1].toInt());
|
---|
323 | *v = s;
|
---|
324 | }
|
---|
325 |
|
---|
326 | void readRectEntry(const QDomElement &e, const QString &name, QRect *v)
|
---|
327 | {
|
---|
328 | bool found = FALSE;
|
---|
329 | QDomElement tag = findSubTag(e, name, &found);
|
---|
330 | if(!found)
|
---|
331 | return;
|
---|
332 | QStringList list = QStringList::split(',', tagContent(tag));
|
---|
333 | if(list.count() != 4)
|
---|
334 | return;
|
---|
335 | QRect r;
|
---|
336 | r.setX(list[0].toInt());
|
---|
337 | r.setY(list[1].toInt());
|
---|
338 | r.setWidth(list[2].toInt());
|
---|
339 | r.setHeight(list[3].toInt());
|
---|
340 | *v = r;
|
---|
341 | }
|
---|
342 |
|
---|
343 | void readColorEntry(const QDomElement &e, const QString &name, QColor *v)
|
---|
344 | {
|
---|
345 | bool found = FALSE;
|
---|
346 | QDomElement tag = findSubTag(e, name, &found);
|
---|
347 | if(!found)
|
---|
348 | return;
|
---|
349 | QColor c;
|
---|
350 | c.setNamedColor(tagContent(tag));
|
---|
351 | if(c.isValid())
|
---|
352 | *v = c;
|
---|
353 | }
|
---|
354 |
|
---|
355 | void xmlToStringList(const QDomElement &e, const QString &name, QStringList *v)
|
---|
356 | {
|
---|
357 | bool found = false;
|
---|
358 | QDomElement tag = findSubTag(e, name, &found);
|
---|
359 | if(!found)
|
---|
360 | return;
|
---|
361 | QStringList list;
|
---|
362 | for(QDomNode n = tag.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
---|
363 | QDomElement i = n.toElement();
|
---|
364 | if(i.isNull())
|
---|
365 | continue;
|
---|
366 | if(i.tagName() == "item")
|
---|
367 | list += tagContent(i);
|
---|
368 | }
|
---|
369 | *v = list;
|
---|
370 | }
|
---|
371 |
|
---|
372 | void setBoolAttribute(QDomElement e, const QString &name, bool b)
|
---|
373 | {
|
---|
374 | e.setAttribute(name, b ? "true" : "false");
|
---|
375 | }
|
---|
376 |
|
---|
377 | void readBoolAttribute(QDomElement e, const QString &name, bool *v)
|
---|
378 | {
|
---|
379 | if(e.hasAttribute(name)) {
|
---|
380 | QString s = e.attribute(name);
|
---|
381 | *v = (s == "true") ? TRUE: FALSE;
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | };
|
---|
386 |
|
---|