1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the qt3to4 porting application of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qtsimplexml.h"
|
---|
43 | #include <QDomDocument>
|
---|
44 |
|
---|
45 | QT_BEGIN_NAMESPACE
|
---|
46 |
|
---|
47 | QtSimpleXml::QtSimpleXml(const QString &name)
|
---|
48 | {
|
---|
49 | valid = false;
|
---|
50 | n = name;
|
---|
51 | parent = 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | QtSimpleXml &QtSimpleXml::operator [](int index)
|
---|
55 | {
|
---|
56 | if (index < 0)
|
---|
57 | return *this;
|
---|
58 |
|
---|
59 | if (index > children.size()) {
|
---|
60 | static QtSimpleXml NIL;
|
---|
61 | qWarning("QtSimpleXml::operator[], Out of range access: size is %i, index is %i",
|
---|
62 | children.size(), index);
|
---|
63 | return NIL;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (index == children.size()) {
|
---|
67 | QtSimpleXml *item = new QtSimpleXml(QLatin1String("item"));
|
---|
68 | item->parent = this;
|
---|
69 | children.insert(item->name(), item);
|
---|
70 | return *item;
|
---|
71 | }
|
---|
72 |
|
---|
73 | QMultiMap<QString, QtSimpleXml *>::Iterator it = children.begin();
|
---|
74 | while (index--) ++it;
|
---|
75 | return *it.value();
|
---|
76 | }
|
---|
77 |
|
---|
78 | QtSimpleXml &QtSimpleXml::operator [](const QString &key)
|
---|
79 | {
|
---|
80 | if (!children.contains(key)) {
|
---|
81 | QtSimpleXml *item = new QtSimpleXml(key);
|
---|
82 | item->parent = this;
|
---|
83 | children.insert(item->name(), item);
|
---|
84 | return *item;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return *children.find(key).value();
|
---|
88 | }
|
---|
89 |
|
---|
90 | QtSimpleXml &QtSimpleXml::operator =(const QString &text)
|
---|
91 | {
|
---|
92 | valid = true;
|
---|
93 | QtSimpleXml *p = parent;
|
---|
94 | while (p && !p->valid) {
|
---|
95 | p->valid = true;
|
---|
96 | p = p->parent;
|
---|
97 | }
|
---|
98 |
|
---|
99 | s = text;
|
---|
100 | return *this;
|
---|
101 | }
|
---|
102 |
|
---|
103 | QDomDocument QtSimpleXml::toDomDocument() const
|
---|
104 | {
|
---|
105 | QDomDocument doc;
|
---|
106 | QString data = QLatin1String("version=\"1.0\" encoding=\"UTF-8\"");
|
---|
107 | doc.appendChild(doc.createProcessingInstruction(QLatin1String("xml"), data));
|
---|
108 |
|
---|
109 | if (!valid)
|
---|
110 | return doc;
|
---|
111 |
|
---|
112 | if(!s.isEmpty())
|
---|
113 | doc.appendChild(doc.createTextNode(s));
|
---|
114 |
|
---|
115 | {
|
---|
116 | QMultiMap<QString, QtSimpleXml *>::ConstIterator it = children.constBegin();
|
---|
117 | for (; it != children.end(); ++it) {
|
---|
118 | QtSimpleXml *item = it.value();
|
---|
119 | if (item->valid) {
|
---|
120 | QDomNode node = item->toDomElement(&doc);
|
---|
121 | doc.appendChild(node);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | return doc;
|
---|
127 | }
|
---|
128 |
|
---|
129 | QDomElement QtSimpleXml::toDomElement(QDomDocument *doc) const
|
---|
130 | {
|
---|
131 | QDomElement elem = doc->createElement(n);
|
---|
132 | QMap<QString, QString>::ConstIterator ita = attr.constBegin();
|
---|
133 | for (; ita != attr.constEnd(); ++ita)
|
---|
134 | elem.setAttribute(ita.key(), ita.value());
|
---|
135 |
|
---|
136 | if(!s.isEmpty())
|
---|
137 | elem.appendChild(doc->createTextNode(s));
|
---|
138 |
|
---|
139 | {
|
---|
140 | QMultiMap<QString, QtSimpleXml *>::ConstIterator it = children.constBegin();
|
---|
141 | for (; it != children.constEnd(); ++it) {
|
---|
142 | QtSimpleXml *item = it.value();
|
---|
143 | if (item->valid) {
|
---|
144 | QDomNode node = item->toDomElement(doc);
|
---|
145 | elem.appendChild(node);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | return elem;
|
---|
151 | }
|
---|
152 |
|
---|
153 | QString QtSimpleXml::name() const
|
---|
154 | {
|
---|
155 | return n;
|
---|
156 | }
|
---|
157 |
|
---|
158 | QString QtSimpleXml::text() const
|
---|
159 | {
|
---|
160 | return s;
|
---|
161 | }
|
---|
162 |
|
---|
163 | int QtSimpleXml::numChildren() const
|
---|
164 | {
|
---|
165 | return children.count();
|
---|
166 | }
|
---|
167 |
|
---|
168 | bool QtSimpleXml::isValid() const
|
---|
169 | {
|
---|
170 | return valid;
|
---|
171 | }
|
---|
172 |
|
---|
173 | void QtSimpleXml::setAttribute(const QString &key, const QString &value)
|
---|
174 | {
|
---|
175 | attr.insert(key, QString(value));
|
---|
176 | }
|
---|
177 |
|
---|
178 | QString QtSimpleXml::attribute(const QString &key)
|
---|
179 | {
|
---|
180 | return attr[key];
|
---|
181 | }
|
---|
182 |
|
---|
183 | bool QtSimpleXml::setContent(const QString &content)
|
---|
184 | {
|
---|
185 | QDomDocument doc;
|
---|
186 | QString errorMsg;
|
---|
187 | int errorLine;
|
---|
188 | int errorColumn;
|
---|
189 |
|
---|
190 | if (!doc.setContent(content, false, &errorMsg, &errorLine, &errorColumn)) {
|
---|
191 | errorStr = errorMsg;
|
---|
192 | errorStr += QLatin1String(" at ") + QString::number(errorLine) + QLatin1String(":") + QString::number(errorColumn);
|
---|
193 | return false;
|
---|
194 | }
|
---|
195 |
|
---|
196 | parse(doc);
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool QtSimpleXml::setContent(QIODevice *device)
|
---|
201 | {
|
---|
202 | QDomDocument doc;
|
---|
203 | QString errorMsg;
|
---|
204 | int errorLine;
|
---|
205 | int errorColumn;
|
---|
206 | if (!doc.setContent(device, false, &errorMsg, &errorLine, &errorColumn)) {
|
---|
207 | errorStr = errorMsg;
|
---|
208 | errorStr += QLatin1String(" at ") + QString::number(errorLine) + QLatin1String(":") + QString::number(errorColumn);
|
---|
209 | return false;
|
---|
210 | }
|
---|
211 |
|
---|
212 | QDomNode child = doc.firstChild();
|
---|
213 | while (!child.isNull() && !child.isElement())
|
---|
214 | child = child.nextSibling();
|
---|
215 |
|
---|
216 | while (!child.isNull()) {
|
---|
217 | QtSimpleXml *xmlNode = new QtSimpleXml;
|
---|
218 | xmlNode->parse(child);
|
---|
219 | xmlNode->parent=this;
|
---|
220 | children.insert(xmlNode->name(), xmlNode);
|
---|
221 | do {
|
---|
222 | child = child.nextSibling();
|
---|
223 | } while (!child.isNull() && !child.isElement());
|
---|
224 | }
|
---|
225 |
|
---|
226 | return true;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | void QtSimpleXml::parse(QDomNode node)
|
---|
231 | {
|
---|
232 | // puts("parse");
|
---|
233 | if (node.isNull())
|
---|
234 | return;
|
---|
235 |
|
---|
236 | valid = true;
|
---|
237 | n = node.nodeName();
|
---|
238 | QDomElement element = node.toElement();
|
---|
239 |
|
---|
240 | QDomNamedNodeMap attrs = element.attributes();
|
---|
241 | for (int i = 0; i < (int) attrs.count(); ++i) {
|
---|
242 | QDomAttr attribute = attrs.item(i).toAttr();
|
---|
243 | attr.insert(attribute.name(), attribute.value());
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (element.firstChild().isText()) {
|
---|
247 | // printf("Got text %s\n", element.text().stripWhiteSpace().latin1());
|
---|
248 | s = element.text().trimmed();
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (node.hasChildNodes()) {
|
---|
253 |
|
---|
254 | // Skip to first element child
|
---|
255 | QDomNode child = node.firstChild();
|
---|
256 | while (!child.isNull() && !child.isElement())
|
---|
257 | child = child.nextSibling();
|
---|
258 |
|
---|
259 | while (!child.isNull()) {
|
---|
260 | QtSimpleXml *xmlNode = new QtSimpleXml;
|
---|
261 | xmlNode->parse(child);
|
---|
262 | children.insert(xmlNode->name(), xmlNode);
|
---|
263 |
|
---|
264 | node = node.nextSibling();
|
---|
265 |
|
---|
266 | do {
|
---|
267 | child = child.nextSibling();
|
---|
268 | } while (!child.isNull() && !child.isElement());
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | QString QtSimpleXml::errorString() const
|
---|
274 | {
|
---|
275 | return errorStr;
|
---|
276 | }
|
---|
277 |
|
---|
278 | QT_END_NAMESPACE
|
---|