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