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