summaryrefslogtreecommitdiff
path: root/libopie/xmltree.cc
authorsimon <simon>2002-04-30 14:26:53 (UTC)
committer simon <simon>2002-04-30 14:26:53 (UTC)
commit5dae945f8ec25a811efb10eb71d606ce8da4429f (patch) (unidiff)
treec12689435fbfb2abd6aedfcbd88208c1cb69407f /libopie/xmltree.cc
parent3c1b87ffa621f872c72228733a078511e56d2daf (diff)
downloadopie-5dae945f8ec25a811efb10eb71d606ce8da4429f.zip
opie-5dae945f8ec25a811efb10eb71d606ce8da4429f.tar.gz
opie-5dae945f8ec25a811efb10eb71d606ce8da4429f.tar.bz2
- put the xml tree classes into the Opie namespace
Diffstat (limited to 'libopie/xmltree.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/xmltree.cc1
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,218 +1,219 @@
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
27using namespace Opie;
27 28
28XMLElement::XMLElement() 29XMLElement::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
33XMLElement::~XMLElement() 34XMLElement::~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
45void XMLElement::appendChild( XMLElement *child ) 46void 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
63void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) 64void 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
93QString XMLElement::attribute( const QString &attr ) const 94QString 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
101void XMLElement::setAttribute( const QString &attr, const QString &value ) 102void 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
106void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) 107void 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
132void XMLElement::removeChild( XMLElement *child ) 133void 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
154void XMLElement::save( QTextStream &s, uint indent ) 155void 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
208class Handler : public QXmlDefaultHandler 209class Handler : public QXmlDefaultHandler
209{ 210{
210public: 211public:
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 );