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