-rw-r--r-- | library/xmlreader.cpp | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/library/xmlreader.cpp b/library/xmlreader.cpp new file mode 100644 index 0000000..1ae4485 --- a/dev/null +++ b/library/xmlreader.cpp | |||
@@ -0,0 +1,137 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "xmlreader.h" | ||
21 | |||
22 | Node::Node() | ||
23 | : parent( 0 ), prev( 0 ), | ||
24 | next( 0 ), first( 0 ), last( 0 ) | ||
25 | { | ||
26 | } | ||
27 | |||
28 | |||
29 | Node::~Node() | ||
30 | { | ||
31 | Node *n = first, *m; | ||
32 | |||
33 | while ( n ) { | ||
34 | m = n->next; | ||
35 | delete n; | ||
36 | n = m; | ||
37 | } | ||
38 | } | ||
39 | |||
40 | |||
41 | void Node::addChild( Node *child ) | ||
42 | { | ||
43 | child->parent = this; | ||
44 | |||
45 | if ( last ) | ||
46 | last->next = child; | ||
47 | child->prev = last; | ||
48 | |||
49 | if ( !first ) | ||
50 | first = child; | ||
51 | last = child; | ||
52 | } | ||
53 | |||
54 | QString Node::attribute( const QString& name ) | ||
55 | { | ||
56 | return attributes[name]; | ||
57 | } | ||
58 | |||
59 | void Node::setAttributes( const QXmlAttributes &a ) | ||
60 | { | ||
61 | for ( int i = 0; i < a.length(); i++ ) | ||
62 | attributes[ a.qName( i ) ] = a.value( i ); | ||
63 | } | ||
64 | |||
65 | QMap<QString, QString> Node::attributeMap() | ||
66 | { | ||
67 | return attributes; | ||
68 | } | ||
69 | |||
70 | QString Node::subData(const QString& tag) const | ||
71 | { | ||
72 | Node* c = firstChild(); | ||
73 | while ( c ) { | ||
74 | if ( c->tagName() == tag ) | ||
75 | return c->data(); | ||
76 | c = c->nextNode(); | ||
77 | } | ||
78 | return QString::null; | ||
79 | } | ||
80 | |||
81 | XmlHandler::XmlHandler() | ||
82 | : node( 0 ), tree( 0 ) | ||
83 | { | ||
84 | } | ||
85 | |||
86 | XmlHandler::~XmlHandler() | ||
87 | { | ||
88 | } | ||
89 | |||
90 | |||
91 | bool XmlHandler::startDocument() | ||
92 | { | ||
93 | tree = node = new Node; | ||
94 | node->setTagName( "DOCUMENT" ); | ||
95 | |||
96 | return TRUE; | ||
97 | } | ||
98 | |||
99 | |||
100 | bool XmlHandler::endDocument() | ||
101 | { | ||
102 | if ( node != tree ) | ||
103 | return FALSE; | ||
104 | |||
105 | return TRUE; | ||
106 | } | ||
107 | |||
108 | bool XmlHandler::startElement( const QString &, const QString &, | ||
109 | const QString &qName, const QXmlAttributes &attr ) | ||
110 | { | ||
111 | Node *nnode = new Node; | ||
112 | nnode->setAttributes( attr ); | ||
113 | nnode->setTagName( qName ); | ||
114 | |||
115 | node->addChild( nnode ); | ||
116 | node = nnode; | ||
117 | |||
118 | return TRUE; | ||
119 | } | ||
120 | |||
121 | |||
122 | bool XmlHandler::endElement( const QString &, const QString &, const QString & ) | ||
123 | { | ||
124 | if ( node == tree ) | ||
125 | return FALSE; | ||
126 | |||
127 | node = node->parentNode(); | ||
128 | return TRUE; | ||
129 | } | ||
130 | |||
131 | |||
132 | bool XmlHandler::characters( const QString &ch ) | ||
133 | { | ||
134 | node->appendData( ch ); | ||
135 | |||
136 | return TRUE; | ||
137 | } | ||