author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <qxml.h> |
|
44 |
#include <qregexp.h> |
|
45 |
||
46 |
#include "parser.h" |
|
47 |
||
48 |
class ContentHandler : public QXmlDefaultHandler |
|
49 |
{ |
|
50 |
public: |
|
51 |
ContentHandler(); |
|
52 |
||
53 |
// QXmlContentHandler methods |
|
54 |
bool startDocument(); |
|
55 |
bool endDocument(); |
|
56 |
bool startElement(const QString &namespaceURI, |
|
57 |
const QString &localName, |
|
58 |
const QString &qName, |
|
59 |
const QXmlAttributes & atts); |
|
60 |
bool endElement(const QString &namespaceURI, |
|
61 |
const QString &localName, |
|
62 |
const QString &qName); |
|
63 |
bool characters(const QString &ch); |
|
64 |
void setDocumentLocator(QXmlLocator *locator); |
|
65 |
bool startPrefixMapping (const QString &prefix, const QString & uri); |
|
66 |
bool endPrefixMapping(const QString &prefix); |
|
67 |
bool ignorableWhitespace (const QString & ch); |
|
68 |
bool processingInstruction(const QString &target, const QString &data); |
|
69 |
bool skippedEntity (const QString & name); |
|
70 |
||
71 |
// QXmlErrorHandler methods |
|
72 |
bool warning (const QXmlParseException & exception); |
|
73 |
bool error (const QXmlParseException & exception); |
|
74 |
bool fatalError (const QXmlParseException & exception); |
|
75 |
||
76 |
// QXmlDTDHandler methods |
|
77 |
bool notationDecl ( const QString & name, const QString & publicId, |
|
78 |
const QString & systemId ); |
|
79 |
bool unparsedEntityDecl ( const QString & name, |
|
80 |
const QString & publicId, |
|
81 |
const QString & systemId, |
|
82 |
const QString & notationName ); |
|
83 |
||
84 |
// QXmlEntityResolver methods |
|
85 |
bool resolveEntity ( const QString & publicId, |
|
86 |
const QString & systemId, |
|
87 |
QXmlInputSource *&); |
|
88 |
||
89 |
// QXmlLexicalHandler methods |
|
90 |
bool startDTD ( const QString & name, const QString & publicId, const QString & systemId ); |
|
91 |
bool endDTD (); |
|
92 |
bool startEntity ( const QString & name ); |
|
93 |
bool endEntity ( const QString & name ); |
|
94 |
bool startCDATA (); |
|
95 |
bool endCDATA (); |
|
96 |
bool comment ( const QString & ch ); |
|
97 |
||
98 |
// QXmlDeclHandler methods |
|
99 |
bool attributeDecl ( const QString & eName, const QString & aName, const QString & type, const QString & valueDefault, const QString & value ); |
|
100 |
bool internalEntityDecl ( const QString & name, const QString & value ); |
|
101 |
bool externalEntityDecl ( const QString & name, const QString & publicId, const QString & systemId ); |
|
102 |
||
103 |
||
104 |
const QString &result() const { return m_result; } |
|
105 |
const QString &errorMsg() const { return m_error_msg; } |
|
106 |
||
107 |
private: |
|
108 |
QString nestPrefix() const { return QString().fill(' ', 3*m_nest); } |
|
109 |
QString formatAttributes(const QXmlAttributes & atts); |
|
110 |
QString escapeStr(const QString &s); |
|
111 |
||
112 |
unsigned m_nest; |
|
113 |
QString m_result, m_error_msg; |
|
114 |
}; |
|
115 |
||
116 |
ContentHandler::ContentHandler() |
|
117 |
{ |
|
118 |
m_nest = 0; |
|
119 |
} |
|
120 |
||
121 |
||
122 |
bool ContentHandler::startDocument() |
|
123 |
{ |
|
124 |
m_result += nestPrefix(); |
|
125 |
m_result += "startDocument()\n"; |
|
126 |
++m_nest; |
|
127 |
return TRUE; |
|
128 |
} |
|
129 |
||
130 |
bool ContentHandler::endDocument() |
|
131 |
{ |
|
132 |
--m_nest; |
|
133 |
m_result += nestPrefix(); |
|
134 |
m_result += "endDocument()\n"; |
|
135 |
return TRUE; |
|
136 |
} |
|
137 |
||
138 |
bool ContentHandler::startElement(const QString &namespaceURI, |
|
139 |
const QString &localName, |
|
140 |
const QString &qName, |
|
141 |
const QXmlAttributes & atts) |
|
142 |
{ |
|
143 |
m_result += nestPrefix(); |
|
144 |
m_result += "startElement(namespaceURI=\"" + escapeStr(namespaceURI) |
|
145 |
+ "\", localName=\"" + escapeStr(localName) |
|
146 |
+ "\", qName=\"" + escapeStr(qName) |
|
147 |
+ "\", atts=[" + formatAttributes(atts) + "])\n"; |
|
148 |
++m_nest; |
|
149 |
return TRUE; |
|
150 |
} |
|
151 |
||
152 |
QString ContentHandler::escapeStr(const QString &s) |
|
153 |
{ |
|
154 |
QString result = s; |
|
155 |
result.replace(QRegExp("\""), "\\\""); |
|
156 |
result.replace(QRegExp("\\"), "\\\\"); |
|
157 |
result.replace(QRegExp("\n"), "\\n"); |
|
158 |
result.replace(QRegExp("\r"), "\\r"); |
|
159 |
result.replace(QRegExp("\t"), "\\t"); |
|
160 |
return result; |
|
161 |
} |
|
162 |
||
163 |
QString ContentHandler::formatAttributes(const QXmlAttributes &atts) |
|
164 |
{ |
|
165 |
QString result; |
|
166 |
for (int i = 0, cnt = atts.count(); i < cnt; ++i) { |
|
167 |
if (i != 0) result += ", "; |
|
168 |
result += "{localName=\"" + escapeStr(atts.localName(i)) |
|
169 |
+ "\", qName=\"" + escapeStr(atts.qName(i)) |
|
170 |
+ "\", uri=\"" + escapeStr(atts.uri(i)) |
|
171 |
+ "\", type=\"" + escapeStr(atts.type(i)) |
|
172 |
+ "\", value=\"" + escapeStr(atts.value(i)) + "\"}"; |
|
173 |
} |
|
174 |
return result; |
|
175 |
} |
|
176 |
||
177 |
bool ContentHandler::endElement(const QString &namespaceURI, |
|
178 |
const QString &localName, |
|
179 |
const QString &qName) |
|
180 |
{ |
|
181 |
--m_nest; |
|
182 |
m_result += nestPrefix(); |
|
183 |
m_result += "endElement(namespaceURI=\"" + escapeStr(namespaceURI) |
|
184 |
+ "\", localName=\"" + escapeStr(localName) |
|
185 |
+ "\", qName=\"" + escapeStr(qName) + "\")\n"; |
|
186 |
return TRUE; |
|
187 |
} |
|
188 |
||
189 |
bool ContentHandler::characters(const QString &ch) |
|
190 |
{ |
|
191 |
m_result += nestPrefix(); |
|
192 |
m_result += "characters(ch=\"" + escapeStr(ch) + "\")\n"; |
|
193 |
return TRUE; |
|
194 |
} |
|
195 |
||
196 |
void ContentHandler::setDocumentLocator(QXmlLocator *locator) |
|
197 |
{ |
|
198 |
m_result += nestPrefix(); |
|
199 |
m_result += "setDocumentLocator(locator={columnNumber=" |
|
200 |
+ QString::number(locator->columnNumber()) |
|
201 |
+ ", lineNumber=" + QString::number(locator->lineNumber()) |
|
202 |
+ "})\n"; |
|
203 |
} |
|
204 |
||
205 |
bool ContentHandler::startPrefixMapping (const QString &prefix, const QString & uri) |
|
206 |
{ |
|
207 |
m_result += nestPrefix(); |
|
208 |
m_result += "startPrefixMapping(prefix=\"" + escapeStr(prefix) |
|
209 |
+ "\", uri=\"" + escapeStr(uri) + "\")\n"; |
|
210 |
++m_nest; |
|
211 |
return TRUE; |
|
212 |
} |
|
213 |
||
214 |
bool ContentHandler::endPrefixMapping(const QString &prefix) |
|
215 |
{ |
|
216 |
--m_nest; |
|
217 |
m_result += nestPrefix(); |
|
218 |
m_result += "endPrefixMapping(prefix=\"" + escapeStr(prefix) + "\")\n"; |
|
219 |
return TRUE; |
|
220 |
} |
|
221 |
||
222 |
bool ContentHandler::ignorableWhitespace(const QString & ch) |
|
223 |
{ |
|
224 |
m_result += nestPrefix(); |
|
225 |
m_result += "ignorableWhitespace(ch=\"" + escapeStr(ch) + "\")\n"; |
|
226 |
return TRUE; |
|
227 |
} |
|
228 |
||
229 |
bool ContentHandler::processingInstruction(const QString &target, const QString &data) |
|
230 |
{ |
|
231 |
m_result += nestPrefix(); |
|
232 |
m_result += "processingInstruction(target=\"" + escapeStr(target) |
|
233 |
+ "\", data=\"" + escapeStr(data) + "\")\n"; |
|
234 |
return TRUE; |
|
235 |
} |
|
236 |
||
237 |
bool ContentHandler::skippedEntity (const QString & name) |
|
238 |
{ |
|
239 |
m_result += nestPrefix(); |
|
240 |
m_result += "skippedEntity(name=\"" + escapeStr(name) + "\")\n"; |
|
241 |
return TRUE; |
|
242 |
} |
|
243 |
||
244 |
bool ContentHandler::warning(const QXmlParseException & exception) |
|
245 |
{ |
|
246 |
m_error_msg = QString("Warning %1:%2: %3") |
|
247 |
.arg(exception.columnNumber()) |
|
248 |
.arg(exception.lineNumber()) |
|
249 |
.arg(exception.message()); |
|
250 |
m_result += nestPrefix(); |
|
251 |
m_result += "warning(exception={columnNumber=" |
|
252 |
+ QString::number(exception.columnNumber()) |
|
253 |
+ ", lineNumber=" |
|
254 |
+ QString::number(exception.lineNumber()) |
|
255 |
+ ", publicId=\"" + escapeStr(exception.publicId()) |
|
256 |
+ "\", systemId=\"" + escapeStr(exception.systemId()) |
|
257 |
+ "\", message=\"" + escapeStr(exception.message()) |
|
258 |
+ "\"})\n"; |
|
259 |
return TRUE; |
|
260 |
} |
|
261 |
||
262 |
bool ContentHandler::error(const QXmlParseException & exception) |
|
263 |
{ |
|
264 |
m_error_msg = QString("Error %1:%2: %3") |
|
265 |
.arg(exception.columnNumber()) |
|
266 |
.arg(exception.lineNumber()) |
|
267 |
.arg(exception.message()); |
|
268 |
m_result += nestPrefix(); |
|
269 |
m_result += "error(exception={columnNumber=" |
|
270 |
+ QString::number(exception.columnNumber()) |
|
271 |
+ ", lineNumber=" |
|
272 |
+ QString::number(exception.lineNumber()) |
|
273 |
+ ", publicId=\"" + escapeStr(exception.publicId()) |
|
274 |
+ "\", systemId=\"" + escapeStr(exception.systemId()) |
|
275 |
+ "\", message=\"" + escapeStr(exception.message()) |
|
276 |
+ "\"})\n"; |
|
277 |
return TRUE; |
|
278 |
} |
|
279 |
||
280 |
bool ContentHandler::fatalError(const QXmlParseException & exception) |
|
281 |
{ |
|
282 |
m_error_msg = QString("Fatal error %1:%2: %3") |
|
283 |
.arg(exception.columnNumber()) |
|
284 |
.arg(exception.lineNumber()) |
|
285 |
.arg(exception.message()); |
|
286 |
m_result += nestPrefix(); |
|
287 |
m_result += "fatalError(exception={columnNumber=" |
|
288 |
+ QString::number(exception.columnNumber()) |
|
289 |
+ ", lineNumber=" |
|
290 |
+ QString::number(exception.lineNumber()) |
|
291 |
+ ", publicId=\"" + escapeStr(exception.publicId()) |
|
292 |
+ "\", systemId=\"" + escapeStr(exception.systemId()) |
|
293 |
+ "\", message=\"" + escapeStr(exception.message()) |
|
294 |
+ "\"})\n"; |
|
295 |
return TRUE; |
|
296 |
} |
|
297 |
||
298 |
bool ContentHandler::notationDecl ( const QString & name, |
|
299 |
const QString & publicId, |
|
300 |
const QString & systemId ) |
|
301 |
{ |
|
302 |
m_result += nestPrefix(); |
|
303 |
m_result += "notationDecl(name=\"" + escapeStr(name) + "\", publicId=\"" |
|
304 |
+ escapeStr(publicId) + "\", systemId=\"" |
|
305 |
+ escapeStr(systemId) + "\")\n"; |
|
306 |
return TRUE; |
|
307 |
} |
|
308 |
||
309 |
bool ContentHandler::unparsedEntityDecl ( const QString & name, |
|
310 |
const QString & publicId, |
|
311 |
const QString & systemId, |
|
312 |
const QString & notationName ) |
|
313 |
{ |
|
314 |
m_result += nestPrefix(); |
|
315 |
m_result += "unparsedEntityDecl(name=\"" + escapeStr(name) |
|
316 |
+ "\", publicId=\"" + escapeStr(publicId) |
|
317 |
+ "\", systemId=\"" + escapeStr(systemId) |
|
318 |
+ "\", notationName=\"" + escapeStr(notationName) |
|
319 |
+ "\")\n"; |
|
320 |
return TRUE; |
|
321 |
} |
|
322 |
||
323 |
bool ContentHandler::resolveEntity(const QString & publicId, |
|
324 |
const QString & systemId, |
|
325 |
QXmlInputSource *&) |
|
326 |
{ |
|
327 |
m_result += nestPrefix(); |
|
328 |
m_result += "resolveEntity(publicId=\"" + escapeStr(publicId) |
|
329 |
+ "\", systemId=\"" + escapeStr(systemId) |
|
330 |
+ "\", ret={})\n"; |
|
331 |
return TRUE; |
|
332 |
} |
|
333 |
||
334 |
bool ContentHandler::startDTD ( const QString & name, const QString & publicId, const QString & systemId ) |
|
335 |
{ |
|
336 |
m_result += nestPrefix(); |
|
337 |
m_result += "startDTD(name=\"" + escapeStr(name) |
|
338 |
+ "\", publicId=\"" + escapeStr(publicId) |
|
339 |
+ "\", systemId=\"" + escapeStr(systemId) + "\")\n"; |
|
340 |
++m_nest; |
|
341 |
return TRUE; |
|
342 |
} |
|
343 |
||
344 |
bool ContentHandler::endDTD () |
|
345 |
{ |
|
346 |
--m_nest; |
|
347 |
m_result += nestPrefix(); |
|
348 |
m_result += "endDTD()\n"; |
|
349 |
return TRUE; |
|
350 |
} |
|
351 |
||
352 |
bool ContentHandler::startEntity ( const QString & name ) |
|
353 |
{ |
|
354 |
m_result += nestPrefix(); |
|
355 |
m_result += "startEntity(name=\"" + escapeStr(name) + "\")\n"; |
|
356 |
++m_nest; |
|
357 |
return TRUE; |
|
358 |
} |
|
359 |
||
360 |
bool ContentHandler::endEntity ( const QString & name ) |
|
361 |
{ |
|
362 |
--m_nest; |
|
363 |
m_result += nestPrefix(); |
|
364 |
m_result += "endEntity(name=\"" + escapeStr(name) + "\")\n"; |
|
365 |
return TRUE; |
|
366 |
} |
|
367 |
||
368 |
bool ContentHandler::startCDATA () |
|
369 |
{ |
|
370 |
m_result += nestPrefix(); |
|
371 |
m_result += "startCDATA()\n"; |
|
372 |
++m_nest; |
|
373 |
return TRUE; |
|
374 |
} |
|
375 |
||
376 |
bool ContentHandler::endCDATA () |
|
377 |
{ |
|
378 |
--m_nest; |
|
379 |
m_result += nestPrefix(); |
|
380 |
m_result += "endCDATA()\n"; |
|
381 |
return TRUE; |
|
382 |
} |
|
383 |
||
384 |
bool ContentHandler::comment ( const QString & ch ) |
|
385 |
{ |
|
386 |
m_result += nestPrefix(); |
|
387 |
m_result += "comment(ch=\"" + escapeStr(ch) + "\")\n"; |
|
388 |
return TRUE; |
|
389 |
} |
|
390 |
||
391 |
bool ContentHandler::attributeDecl ( const QString & eName, |
|
392 |
const QString & aName, |
|
393 |
const QString & type, |
|
394 |
const QString & valueDefault, |
|
395 |
const QString & value ) |
|
396 |
{ |
|
397 |
m_result += nestPrefix(); |
|
398 |
m_result += "attributeDecl(eName=\"" + escapeStr(eName) + "\", aName=\"" |
|
399 |
+ escapeStr(aName) + "\", type=\"" + escapeStr(type) |
|
400 |
+ "\", valueDefault=\"" + escapeStr(valueDefault) |
|
401 |
+ "\", value=\"" + escapeStr(value) + "\")\n"; |
|
402 |
return TRUE; |
|
403 |
} |
|
404 |
||
405 |
bool ContentHandler::internalEntityDecl ( const QString & name, |
|
406 |
const QString & value ) |
|
407 |
{ |
|
408 |
m_result += nestPrefix(); |
|
409 |
m_result += "internatlEntityDecl(name=\"" + escapeStr(name) |
|
410 |
+ "\", value=\"" + escapeStr(value) + "\")\n"; |
|
411 |
return TRUE; |
|
412 |
} |
|
413 |
||
414 |
bool ContentHandler::externalEntityDecl ( const QString & name, |
|
415 |
const QString & publicId, |
|
416 |
const QString & systemId ) |
|
417 |
{ |
|
418 |
m_result += nestPrefix(); |
|
419 |
m_result += "externalEntityDecl(name=\"" + escapeStr(name) |
|
420 |
+ "\", publicId=\"" + escapeStr(publicId) |
|
421 |
+ "\", systemId=\"" + escapeStr(systemId) + "\")\n"; |
|
422 |
return TRUE; |
|
423 |
} |
|
424 |
||
425 |
Parser::Parser() |
|
426 |
{ |
|
427 |
handler = new ContentHandler; |
|
428 |
setContentHandler(handler); |
|
429 |
setDTDHandler(handler); |
|
430 |
setDeclHandler(handler); |
|
431 |
setEntityResolver(handler); |
|
432 |
setErrorHandler(handler); |
|
433 |
setLexicalHandler(handler); |
|
434 |
} |
|
435 |
||
436 |
Parser::~Parser() |
|
437 |
{ |
|
438 |
delete handler; |
|
439 |
} |
|
440 |
||
441 |
bool Parser::parseFile(QFile *file) |
|
442 |
{ |
|
443 |
QXmlInputSource source(file); |
|
444 |
return parse(source); |
|
445 |
} |
|
446 |
||
447 |
QString Parser::result() const |
|
448 |
{ |
|
449 |
return handler->result(); |
|
450 |
} |
|
451 |
||
452 |
QString Parser::errorMsg() const |
|
453 |
{ |
|
454 |
return handler->errorMsg(); |
|
455 |
} |