-rw-r--r-- | libopie/xmltree.cc | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/libopie/xmltree.cc b/libopie/xmltree.cc index 408e3c6..27db5b3 100644 --- a/libopie/xmltree.cc +++ b/libopie/xmltree.cc | |||
@@ -1,321 +1,322 @@ | |||
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project |
2 | Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> | 2 | Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> |
3 | 3 | ||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public | 5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either | 6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. | 7 | version 2 of the License, or (at your option) any later version. |
8 | 8 | ||
9 | This library is distributed in the hope that it will be useful, | 9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. | 12 | Library General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to | 15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. | 17 | Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | #include <qpe/stringutil.h> | 20 | #include <qpe/stringutil.h> |
21 | #include <opie/xmltree.h> | 21 | #include <opie/xmltree.h> |
22 | 22 | ||
23 | #include <qxml.h> | 23 | #include <qxml.h> |
24 | 24 | ||
25 | #include <assert.h> | 25 | #include <assert.h> |
26 | 26 | ||
27 | using namespace Opie; | ||
27 | 28 | ||
28 | XMLElement::XMLElement() | 29 | XMLElement::XMLElement() |
29 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) | 30 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) |
30 | { | 31 | { |
31 | } | 32 | } |
32 | 33 | ||
33 | XMLElement::~XMLElement() | 34 | XMLElement::~XMLElement() |
34 | { | 35 | { |
35 | XMLElement *n = m_first; | 36 | XMLElement *n = m_first; |
36 | 37 | ||
37 | while ( n ) | 38 | while ( n ) |
38 | { | 39 | { |
39 | XMLElement *tmp = n; | 40 | XMLElement *tmp = n; |
40 | n = n->m_next; | 41 | n = n->m_next; |
41 | delete tmp; | 42 | delete tmp; |
42 | } | 43 | } |
43 | } | 44 | } |
44 | 45 | ||
45 | void XMLElement::appendChild( XMLElement *child ) | 46 | void XMLElement::appendChild( XMLElement *child ) |
46 | { | 47 | { |
47 | if ( child->m_parent ) | 48 | if ( child->m_parent ) |
48 | child->m_parent->removeChild( child ); | 49 | child->m_parent->removeChild( child ); |
49 | 50 | ||
50 | child->m_parent = this; | 51 | child->m_parent = this; |
51 | 52 | ||
52 | if ( m_last ) | 53 | if ( m_last ) |
53 | m_last->m_next = child; | 54 | m_last->m_next = child; |
54 | 55 | ||
55 | child->m_prev = m_last; | 56 | child->m_prev = m_last; |
56 | 57 | ||
57 | if ( !m_first ) | 58 | if ( !m_first ) |
58 | m_first = child; | 59 | m_first = child; |
59 | 60 | ||
60 | m_last = child; | 61 | m_last = child; |
61 | } | 62 | } |
62 | 63 | ||
63 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | 64 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) |
64 | { | 65 | { |
65 | assert( newChild != refChild ); | 66 | assert( newChild != refChild ); |
66 | 67 | ||
67 | if ( refChild == m_last ) | 68 | if ( refChild == m_last ) |
68 | { | 69 | { |
69 | appendChild( newChild ); | 70 | appendChild( newChild ); |
70 | return; | 71 | return; |
71 | } | 72 | } |
72 | 73 | ||
73 | assert( refChild ); | 74 | assert( refChild ); |
74 | assert( refChild->m_parent ); | 75 | assert( refChild->m_parent ); |
75 | assert( refChild->m_parent == this ); | 76 | assert( refChild->m_parent == this ); |
76 | 77 | ||
77 | if ( newChild->m_parent && newChild != refChild ) | 78 | if ( newChild->m_parent && newChild != refChild ) |
78 | newChild->m_parent->removeChild( newChild ); | 79 | newChild->m_parent->removeChild( newChild ); |
79 | 80 | ||
80 | newChild->m_parent = this; | 81 | newChild->m_parent = this; |
81 | 82 | ||
82 | XMLElement *next = refChild->m_next; | 83 | XMLElement *next = refChild->m_next; |
83 | 84 | ||
84 | refChild->m_next = newChild; | 85 | refChild->m_next = newChild; |
85 | 86 | ||
86 | newChild->m_prev = refChild; | 87 | newChild->m_prev = refChild; |
87 | newChild->m_next = next; | 88 | newChild->m_next = next; |
88 | 89 | ||
89 | if ( next ) | 90 | if ( next ) |
90 | next->m_prev = newChild; | 91 | next->m_prev = newChild; |
91 | } | 92 | } |
92 | 93 | ||
93 | QString XMLElement::attribute( const QString &attr ) const | 94 | QString XMLElement::attribute( const QString &attr ) const |
94 | { | 95 | { |
95 | AttributeMap::ConstIterator it = m_attributes.find( attr ); | 96 | AttributeMap::ConstIterator it = m_attributes.find( attr ); |
96 | if ( it == m_attributes.end() ) | 97 | if ( it == m_attributes.end() ) |
97 | return QString::null; | 98 | return QString::null; |
98 | return it.data(); | 99 | return it.data(); |
99 | } | 100 | } |
100 | 101 | ||
101 | void XMLElement::setAttribute( const QString &attr, const QString &value ) | 102 | void XMLElement::setAttribute( const QString &attr, const QString &value ) |
102 | { | 103 | { |
103 | m_attributes.replace( attr, value ); | 104 | m_attributes.replace( attr, value ); |
104 | } | 105 | } |
105 | 106 | ||
106 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | 107 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) |
107 | { | 108 | { |
108 | assert( refChild ); | 109 | assert( refChild ); |
109 | assert( refChild->m_parent ); | 110 | assert( refChild->m_parent ); |
110 | assert( refChild->m_parent == this ); | 111 | assert( refChild->m_parent == this ); |
111 | assert( newChild != refChild ); | 112 | assert( newChild != refChild ); |
112 | 113 | ||
113 | if ( newChild->m_parent && newChild != refChild ) | 114 | if ( newChild->m_parent && newChild != refChild ) |
114 | newChild->m_parent->removeChild( newChild ); | 115 | newChild->m_parent->removeChild( newChild ); |
115 | 116 | ||
116 | newChild->m_parent = this; | 117 | newChild->m_parent = this; |
117 | 118 | ||
118 | XMLElement *prev = refChild->m_prev; | 119 | XMLElement *prev = refChild->m_prev; |
119 | 120 | ||
120 | refChild->m_prev = newChild; | 121 | refChild->m_prev = newChild; |
121 | 122 | ||
122 | newChild->m_prev = prev; | 123 | newChild->m_prev = prev; |
123 | newChild->m_next = refChild; | 124 | newChild->m_next = refChild; |
124 | 125 | ||
125 | if ( prev ) | 126 | if ( prev ) |
126 | prev->m_next = newChild; | 127 | prev->m_next = newChild; |
127 | 128 | ||
128 | if ( refChild == m_first ) | 129 | if ( refChild == m_first ) |
129 | m_first = newChild; | 130 | m_first = newChild; |
130 | } | 131 | } |
131 | 132 | ||
132 | void XMLElement::removeChild( XMLElement *child ) | 133 | void XMLElement::removeChild( XMLElement *child ) |
133 | { | 134 | { |
134 | if ( child->m_parent != this ) | 135 | if ( child->m_parent != this ) |
135 | return; | 136 | return; |
136 | 137 | ||
137 | if ( m_first == child ) | 138 | if ( m_first == child ) |
138 | m_first = child->m_next; | 139 | m_first = child->m_next; |
139 | 140 | ||
140 | if ( m_last == child ) | 141 | if ( m_last == child ) |
141 | m_last = child->m_prev; | 142 | m_last = child->m_prev; |
142 | 143 | ||
143 | if ( child->m_prev ) | 144 | if ( child->m_prev ) |
144 | child->m_prev->m_next = child->m_next; | 145 | child->m_prev->m_next = child->m_next; |
145 | 146 | ||
146 | if ( child->m_next ) | 147 | if ( child->m_next ) |
147 | child->m_next->m_prev = child->m_prev; | 148 | child->m_next->m_prev = child->m_prev; |
148 | 149 | ||
149 | child->m_parent = 0; | 150 | child->m_parent = 0; |
150 | child->m_prev = 0; | 151 | child->m_prev = 0; |
151 | child->m_next = 0; | 152 | child->m_next = 0; |
152 | } | 153 | } |
153 | 154 | ||
154 | void XMLElement::save( QTextStream &s, uint indent ) | 155 | void XMLElement::save( QTextStream &s, uint indent ) |
155 | { | 156 | { |
156 | if ( !m_value.isEmpty() ) | 157 | if ( !m_value.isEmpty() ) |
157 | { | 158 | { |
158 | s << Qtopia::escapeString( m_value ); | 159 | s << Qtopia::escapeString( m_value ); |
159 | return; | 160 | return; |
160 | } | 161 | } |
161 | 162 | ||
162 | for ( uint i = 0; i < indent; ++i ) | 163 | for ( uint i = 0; i < indent; ++i ) |
163 | s << " "; | 164 | s << " "; |
164 | 165 | ||
165 | s << "<" << m_tag; | 166 | s << "<" << m_tag; |
166 | 167 | ||
167 | if ( !m_attributes.isEmpty() ) | 168 | if ( !m_attributes.isEmpty() ) |
168 | { | 169 | { |
169 | s << " "; | 170 | s << " "; |
170 | AttributeMap::ConstIterator it = m_attributes.begin(); | 171 | AttributeMap::ConstIterator it = m_attributes.begin(); |
171 | AttributeMap::ConstIterator end = m_attributes.end(); | 172 | AttributeMap::ConstIterator end = m_attributes.end(); |
172 | for (; it != end; ++it ) | 173 | for (; it != end; ++it ) |
173 | { | 174 | { |
174 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; | 175 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; |
175 | s << " "; | 176 | s << " "; |
176 | } | 177 | } |
177 | } | 178 | } |
178 | 179 | ||
179 | if ( m_last ) | 180 | if ( m_last ) |
180 | { | 181 | { |
181 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | 182 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) |
182 | s << ">"; | 183 | s << ">"; |
183 | else | 184 | else |
184 | s << ">" << endl; | 185 | s << ">" << endl; |
185 | 186 | ||
186 | int newIndent = indent; | 187 | int newIndent = indent; |
187 | if ( m_parent ) | 188 | if ( m_parent ) |
188 | newIndent++; | 189 | newIndent++; |
189 | 190 | ||
190 | XMLElement *n = m_first; | 191 | XMLElement *n = m_first; |
191 | while ( n ) | 192 | while ( n ) |
192 | { | 193 | { |
193 | n->save( s, newIndent ); | 194 | n->save( s, newIndent ); |
194 | n = n->nextChild(); | 195 | n = n->nextChild(); |
195 | } | 196 | } |
196 | 197 | ||
197 | if ( m_last && m_last->value().isEmpty() && m_parent ) | 198 | if ( m_last && m_last->value().isEmpty() && m_parent ) |
198 | for ( uint i = 0; i < indent; ++i ) | 199 | for ( uint i = 0; i < indent; ++i ) |
199 | s << " "; | 200 | s << " "; |
200 | 201 | ||
201 | if ( m_parent ) | 202 | if ( m_parent ) |
202 | s << "</" << m_tag << ">" << endl; | 203 | s << "</" << m_tag << ">" << endl; |
203 | } | 204 | } |
204 | else | 205 | else |
205 | s << "/>" << endl; | 206 | s << "/>" << endl; |
206 | } | 207 | } |
207 | 208 | ||
208 | class Handler : public QXmlDefaultHandler | 209 | class Handler : public QXmlDefaultHandler |
209 | { | 210 | { |
210 | public: | 211 | public: |
211 | Handler() : m_node( 0 ), m_root( 0 ) {} | 212 | Handler() : m_node( 0 ), m_root( 0 ) {} |
212 | 213 | ||
213 | XMLElement *root() const { return m_root; } | 214 | XMLElement *root() const { return m_root; } |
214 | 215 | ||
215 | virtual bool startDocument(); | 216 | virtual bool startDocument(); |
216 | virtual bool endDocument(); | 217 | virtual bool endDocument(); |
217 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, | 218 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, |
218 | const QXmlAttributes &attr ); | 219 | const QXmlAttributes &attr ); |
219 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); | 220 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); |
220 | virtual bool characters( const QString &ch ); | 221 | virtual bool characters( const QString &ch ); |
221 | 222 | ||
222 | private: | 223 | private: |
223 | XMLElement *m_node; | 224 | XMLElement *m_node; |
224 | XMLElement *m_root; | 225 | XMLElement *m_root; |
225 | }; | 226 | }; |
226 | 227 | ||
227 | bool Handler::startDocument() | 228 | bool Handler::startDocument() |
228 | { | 229 | { |
229 | m_root = m_node = new XMLElement; | 230 | m_root = m_node = new XMLElement; |
230 | 231 | ||
231 | return true; | 232 | return true; |
232 | } | 233 | } |
233 | 234 | ||
234 | bool Handler::endDocument() | 235 | bool Handler::endDocument() |
235 | { | 236 | { |
236 | return m_root == m_node; | 237 | return m_root == m_node; |
237 | } | 238 | } |
238 | 239 | ||
239 | bool Handler::startElement( const QString &, const QString &, const QString &qName, | 240 | bool Handler::startElement( const QString &, const QString &, const QString &qName, |
240 | const QXmlAttributes &attr ) | 241 | const QXmlAttributes &attr ) |
241 | { | 242 | { |
242 | XMLElement *bm = new XMLElement; | 243 | XMLElement *bm = new XMLElement; |
243 | 244 | ||
244 | XMLElement::AttributeMap attributes; | 245 | XMLElement::AttributeMap attributes; |
245 | for ( int i = 0; i < attr.length(); ++i ) | 246 | for ( int i = 0; i < attr.length(); ++i ) |
246 | attributes[ attr.qName( i ) ] = attr.value( i ); | 247 | attributes[ attr.qName( i ) ] = attr.value( i ); |
247 | 248 | ||
248 | bm->setAttributes( attributes ); | 249 | bm->setAttributes( attributes ); |
249 | 250 | ||
250 | bm->setTagName( qName ); | 251 | bm->setTagName( qName ); |
251 | 252 | ||
252 | m_node->appendChild( bm ); | 253 | m_node->appendChild( bm ); |
253 | m_node = bm; | 254 | m_node = bm; |
254 | 255 | ||
255 | return true; | 256 | return true; |
256 | } | 257 | } |
257 | 258 | ||
258 | bool Handler::endElement( const QString &, const QString &, const QString & ) | 259 | bool Handler::endElement( const QString &, const QString &, const QString & ) |
259 | { | 260 | { |
260 | if ( m_node == m_root ) | 261 | if ( m_node == m_root ) |
261 | return false; | 262 | return false; |
262 | 263 | ||
263 | m_node = m_node->parent(); | 264 | m_node = m_node->parent(); |
264 | return true; | 265 | return true; |
265 | } | 266 | } |
266 | 267 | ||
267 | bool Handler::characters( const QString &ch ) | 268 | bool Handler::characters( const QString &ch ) |
268 | { | 269 | { |
269 | XMLElement *textNode = new XMLElement; | 270 | XMLElement *textNode = new XMLElement; |
270 | textNode->setValue( ch ); | 271 | textNode->setValue( ch ); |
271 | m_node->appendChild( textNode ); | 272 | m_node->appendChild( textNode ); |
272 | return true; | 273 | return true; |
273 | } | 274 | } |
274 | 275 | ||
275 | XMLElement *XMLElement::namedItem( const QString &name ) | 276 | XMLElement *XMLElement::namedItem( const QString &name ) |
276 | { | 277 | { |
277 | XMLElement *e = m_first; | 278 | XMLElement *e = m_first; |
278 | 279 | ||
279 | for (; e; e = e->nextChild() ) | 280 | for (; e; e = e->nextChild() ) |
280 | if ( e->tagName() == name ) | 281 | if ( e->tagName() == name ) |
281 | return e; | 282 | return e; |
282 | 283 | ||
283 | return 0; | 284 | return 0; |
284 | } | 285 | } |
285 | 286 | ||
286 | XMLElement *XMLElement::clone() const | 287 | XMLElement *XMLElement::clone() const |
287 | { | 288 | { |
288 | XMLElement *res = new XMLElement; | 289 | XMLElement *res = new XMLElement; |
289 | 290 | ||
290 | res->setTagName( m_tag ); | 291 | res->setTagName( m_tag ); |
291 | res->setValue( m_value ); | 292 | res->setValue( m_value ); |
292 | res->setAttributes( m_attributes ); | 293 | res->setAttributes( m_attributes ); |
293 | 294 | ||
294 | XMLElement *e = m_first; | 295 | XMLElement *e = m_first; |
295 | for (; e; e = e->m_next ) | 296 | for (; e; e = e->m_next ) |
296 | res->appendChild( e->clone() ); | 297 | res->appendChild( e->clone() ); |
297 | 298 | ||
298 | return res; | 299 | return res; |
299 | } | 300 | } |
300 | 301 | ||
301 | XMLElement *XMLElement::load( const QString &fileName ) | 302 | XMLElement *XMLElement::load( const QString &fileName ) |
302 | { | 303 | { |
303 | QFile f( fileName ); | 304 | QFile f( fileName ); |
304 | if ( !f.open( IO_ReadOnly ) ) | 305 | if ( !f.open( IO_ReadOnly ) ) |
305 | return 0; | 306 | return 0; |
306 | 307 | ||
307 | QTextStream stream( &f ); | 308 | QTextStream stream( &f ); |
308 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 309 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
309 | QXmlInputSource src( stream ); | 310 | QXmlInputSource src( stream ); |
310 | QXmlSimpleReader reader; | 311 | QXmlSimpleReader reader; |
311 | Handler handler; | 312 | Handler handler; |
312 | 313 | ||
313 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); | 314 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); |
314 | reader.setContentHandler( &handler ); | 315 | reader.setContentHandler( &handler ); |
315 | reader.parse( src ); | 316 | reader.parse( src ); |
316 | 317 | ||
317 | return handler.root();; | 318 | return handler.root();; |
318 | } | 319 | } |
319 | 320 | ||
320 | /* vim: et sw=4 | 321 | /* vim: et sw=4 |
321 | */ | 322 | */ |