author | harlekin <harlekin> | 2002-03-19 00:18:04 (UTC) |
---|---|---|
committer | harlekin <harlekin> | 2002-03-19 00:18:04 (UTC) |
commit | 342ac04976065864b351c6b36087a483a8932da7 (patch) (unidiff) | |
tree | 646a89071882378786532ff587ac0e6e8fd0df03 | |
parent | 02bd3d07a8949bdfe0fe708ff8f3c01bc84fab08 (diff) | |
download | opie-342ac04976065864b351c6b36087a483a8932da7.zip opie-342ac04976065864b351c6b36087a483a8932da7.tar.gz opie-342ac04976065864b351c6b36087a483a8932da7.tar.bz2 |
along with tododb
-rw-r--r-- | include/opie/xmltree.h | 2 | ||||
-rw-r--r-- | libopie/xmltree.cc | 42 | ||||
-rw-r--r-- | libopie/xmltree.h | 2 | ||||
-rw-r--r-- | libopie2/opiecore/xmltree.cc | 42 | ||||
-rw-r--r-- | libopie2/opiecore/xmltree.h | 2 |
5 files changed, 40 insertions, 50 deletions
diff --git a/include/opie/xmltree.h b/include/opie/xmltree.h index 7f1b7b7..7f50365 100644 --- a/include/opie/xmltree.h +++ b/include/opie/xmltree.h | |||
@@ -23,88 +23,90 @@ | |||
23 | 23 | ||
24 | #include <qstring.h> | 24 | #include <qstring.h> |
25 | #include <qmap.h> | 25 | #include <qmap.h> |
26 | #include <qtextstream.h> | 26 | #include <qtextstream.h> |
27 | 27 | ||
28 | /** | 28 | /** |
29 | * A small xml lib written by Simon Hausmann. | 29 | * A small xml lib written by Simon Hausmann. |
30 | */ | 30 | */ |
31 | class XMLElement | 31 | class XMLElement |
32 | { | 32 | { |
33 | public: | 33 | public: |
34 | typedef QMap<QString, QString> AttributeMap; | 34 | typedef QMap<QString, QString> AttributeMap; |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * The constructor of XMLElement | 37 | * The constructor of XMLElement |
38 | */ | 38 | */ |
39 | XMLElement(); | 39 | XMLElement(); |
40 | ~XMLElement(); | 40 | ~XMLElement(); |
41 | 41 | ||
42 | /** appendChild appends a child to the XMLElement behind the last element. | 42 | /** appendChild appends a child to the XMLElement behind the last element. |
43 | * The ownership of the child get's transfered to the | 43 | * The ownership of the child get's transfered to the |
44 | * this XMLElement. | 44 | * this XMLElement. |
45 | * If child is already the child of another parent | 45 | * If child is already the child of another parent |
46 | * it's get removed from the other parent first. | 46 | * it's get removed from the other parent first. |
47 | */ | 47 | */ |
48 | void appendChild( XMLElement *child ); | 48 | void appendChild( XMLElement *child ); |
49 | 49 | ||
50 | /** inserts newChild after refChild. If newChild is the child | 50 | /** inserts newChild after refChild. If newChild is the child |
51 | * of another parent the child will get removed. | 51 | * of another parent the child will get removed. |
52 | * The ownership of child gets transfered. | 52 | * The ownership of child gets transfered. |
53 | * | 53 | * |
54 | */ | 54 | */ |
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | 55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); |
56 | 56 | ||
57 | /** same as insertAfter but the element get's inserted before refChild. | 57 | /** same as insertAfter but the element get's inserted before refChild. |
58 | * | 58 | * |
59 | */ | 59 | */ |
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | 60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); |
61 | 61 | ||
62 | /** removeChild removes the child from the XMLElement. | 62 | /** removeChild removes the child from the XMLElement. |
63 | * The ownership gets dropped. You need to delete the | 63 | * The ownership gets dropped. You need to delete the |
64 | * child yourself. | 64 | * child yourself. |
65 | */ | 65 | */ |
66 | void removeChild( XMLElement *child ); | 66 | void removeChild( XMLElement *child ); |
67 | 67 | ||
68 | /** parent() returns the parent of this XMLElement | 68 | /** parent() returns the parent of this XMLElement |
69 | * If there is no parent 0l gets returned | 69 | * If there is no parent 0l gets returned |
70 | */ | 70 | */ |
71 | XMLElement *parent() const { return m_parent; } | 71 | XMLElement *parent() const { return m_parent; } |
72 | XMLElement *firstChild() const { return m_first; } | 72 | XMLElement *firstChild() const { return m_first; } |
73 | XMLElement *nextChild() const { return m_next; } | 73 | XMLElement *nextChild() const { return m_next; } |
74 | XMLElement *prevChild() const { return m_prev; } | 74 | XMLElement *prevChild() const { return m_prev; } |
75 | XMLElement *lastChild() const { return m_last; } | 75 | XMLElement *lastChild() const { return m_last; } |
76 | 76 | ||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | 77 | void setTagName( const QString &tag ) { m_tag = tag; } |
78 | QString tagName() const { return m_tag; } | 78 | QString tagName() const { return m_tag; } |
79 | 79 | ||
80 | void setValue( const QString &val ) { m_value = val; } | 80 | void setValue( const QString &val ) { m_value = val; } |
81 | QString value() const { return m_value; } | 81 | QString value() const { return m_value; } |
82 | 82 | ||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | 83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } |
84 | AttributeMap attributes() const { return m_attributes; } | 84 | AttributeMap attributes() const { return m_attributes; } |
85 | AttributeMap &attributes() { return m_attributes; } | 85 | AttributeMap &attributes() { return m_attributes; } |
86 | 86 | ||
87 | QString attribute(const QString &)const; | ||
88 | void setAttribute( const QString &attr, const QString &value ); | ||
87 | void save( QTextStream &stream, uint indent = 0 ); | 89 | void save( QTextStream &stream, uint indent = 0 ); |
88 | 90 | ||
89 | XMLElement *namedItem( const QString &name ); | 91 | XMLElement *namedItem( const QString &name ); |
90 | 92 | ||
91 | XMLElement *clone() const; | 93 | XMLElement *clone() const; |
92 | 94 | ||
93 | static XMLElement *load( const QString &fileName ); | 95 | static XMLElement *load( const QString &fileName ); |
94 | 96 | ||
95 | private: | 97 | private: |
96 | QString m_tag; | 98 | QString m_tag; |
97 | QString m_value; | 99 | QString m_value; |
98 | AttributeMap m_attributes; | 100 | AttributeMap m_attributes; |
99 | 101 | ||
100 | XMLElement *m_parent; | 102 | XMLElement *m_parent; |
101 | XMLElement *m_next; | 103 | XMLElement *m_next; |
102 | XMLElement *m_prev; | 104 | XMLElement *m_prev; |
103 | XMLElement *m_first; | 105 | XMLElement *m_first; |
104 | XMLElement *m_last; | 106 | XMLElement *m_last; |
105 | 107 | ||
106 | XMLElement( const XMLElement &rhs ); | 108 | XMLElement( const XMLElement &rhs ); |
107 | XMLElement &operator=( const XMLElement &rhs ); | 109 | XMLElement &operator=( const XMLElement &rhs ); |
108 | }; | 110 | }; |
109 | 111 | ||
110 | #endif | 112 | #endif |
diff --git a/libopie/xmltree.cc b/libopie/xmltree.cc index d5ce74a..3d03cc6 100644 --- a/libopie/xmltree.cc +++ b/libopie/xmltree.cc | |||
@@ -1,246 +1,236 @@ | |||
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 | 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 | namespace | ||
28 | { | ||
29 | |||
30 | /** | ||
31 | Encode an attribute value upon saving. | ||
32 | replaces '"' with """ | ||
33 | replaces '<' with "<" | ||
34 | replaces '&' with "&" | ||
35 | replaces '>' with ">" | ||
36 | */ | ||
37 | QString encodeAttr( const QString& str ) | ||
38 | { | ||
39 | QString encAttr( str ); // cause of the const parameter | ||
40 | encAttr = encAttr.replace( QRegExp( "[<]" ), "<" ); | ||
41 | encAttr = encAttr.replace( QRegExp( "[>]" ), ">" ); | ||
42 | encAttr = encAttr.replace( QRegExp( "[\"]" ), """ ); | ||
43 | encAttr = encAttr.replace( QRegExp( "[&]" ), "&" ); | ||
44 | return encAttr; | ||
45 | } | ||
46 | |||
47 | } | ||
48 | 27 | ||
49 | XMLElement::XMLElement() | 28 | XMLElement::XMLElement() |
50 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) | 29 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) |
51 | { | 30 | { |
52 | } | 31 | } |
53 | 32 | ||
54 | XMLElement::~XMLElement() | 33 | XMLElement::~XMLElement() |
55 | { | 34 | { |
56 | XMLElement *n = m_first; | 35 | XMLElement *n = m_first; |
57 | 36 | ||
58 | while ( n ) | 37 | while ( n ) |
59 | { | 38 | { |
60 | XMLElement *tmp = n; | 39 | XMLElement *tmp = n; |
61 | n = n->m_next; | 40 | n = n->m_next; |
62 | delete tmp; | 41 | delete tmp; |
63 | } | 42 | } |
64 | } | 43 | } |
65 | 44 | ||
66 | void XMLElement::appendChild( XMLElement *child ) | 45 | void XMLElement::appendChild( XMLElement *child ) |
67 | { | 46 | { |
68 | if ( child->m_parent ) | 47 | if ( child->m_parent ) |
69 | child->m_parent->removeChild( child ); | 48 | child->m_parent->removeChild( child ); |
70 | 49 | ||
71 | child->m_parent = this; | 50 | child->m_parent = this; |
72 | 51 | ||
73 | if ( m_last ) | 52 | if ( m_last ) |
74 | m_last->m_next = child; | 53 | m_last->m_next = child; |
75 | 54 | ||
76 | child->m_prev = m_last; | 55 | child->m_prev = m_last; |
77 | 56 | ||
78 | if ( !m_first ) | 57 | if ( !m_first ) |
79 | m_first = child; | 58 | m_first = child; |
80 | 59 | ||
81 | m_last = child; | 60 | m_last = child; |
82 | } | 61 | } |
83 | 62 | ||
84 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | 63 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) |
85 | { | 64 | { |
86 | assert( newChild != refChild ); | 65 | assert( newChild != refChild ); |
87 | 66 | ||
88 | if ( refChild == m_last ) | 67 | if ( refChild == m_last ) |
89 | { | 68 | { |
90 | appendChild( newChild ); | 69 | appendChild( newChild ); |
91 | return; | 70 | return; |
92 | } | 71 | } |
93 | 72 | ||
94 | assert( refChild ); | 73 | assert( refChild ); |
95 | assert( refChild->m_parent ); | 74 | assert( refChild->m_parent ); |
96 | assert( refChild->m_parent == this ); | 75 | assert( refChild->m_parent == this ); |
97 | 76 | ||
98 | if ( newChild->m_parent && newChild != refChild ) | 77 | if ( newChild->m_parent && newChild != refChild ) |
99 | newChild->m_parent->removeChild( newChild ); | 78 | newChild->m_parent->removeChild( newChild ); |
100 | 79 | ||
101 | newChild->m_parent = this; | 80 | newChild->m_parent = this; |
102 | 81 | ||
103 | XMLElement *next = refChild->m_next; | 82 | XMLElement *next = refChild->m_next; |
104 | 83 | ||
105 | refChild->m_next = newChild; | 84 | refChild->m_next = newChild; |
106 | 85 | ||
107 | newChild->m_prev = refChild; | 86 | newChild->m_prev = refChild; |
108 | newChild->m_next = next; | 87 | newChild->m_next = next; |
109 | 88 | ||
110 | if ( next ) | 89 | if ( next ) |
111 | next->m_prev = newChild; | 90 | next->m_prev = newChild; |
112 | } | 91 | } |
113 | 92 | QString XMLElement::attribute(const QString &attr )const | |
93 | { | ||
94 | if ( !m_attributes.contains( attr ) ) | ||
95 | return QString::null; | ||
96 | AttributeMap::ConstIterator it = m_attributes.find( attr ); | ||
97 | return it.data(); | ||
98 | } | ||
99 | void XMLElement::setAttribute(const QString &attr, const QString &value ) | ||
100 | { | ||
101 | m_attributes.remove( attr ); | ||
102 | m_attributes.insert( attr, value ); | ||
103 | } | ||
114 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | 104 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) |
115 | { | 105 | { |
116 | assert( refChild ); | 106 | assert( refChild ); |
117 | assert( refChild->m_parent ); | 107 | assert( refChild->m_parent ); |
118 | assert( refChild->m_parent == this ); | 108 | assert( refChild->m_parent == this ); |
119 | assert( newChild != refChild ); | 109 | assert( newChild != refChild ); |
120 | 110 | ||
121 | if ( newChild->m_parent && newChild != refChild ) | 111 | if ( newChild->m_parent && newChild != refChild ) |
122 | newChild->m_parent->removeChild( newChild ); | 112 | newChild->m_parent->removeChild( newChild ); |
123 | 113 | ||
124 | newChild->m_parent = this; | 114 | newChild->m_parent = this; |
125 | 115 | ||
126 | XMLElement *prev = refChild->m_prev; | 116 | XMLElement *prev = refChild->m_prev; |
127 | 117 | ||
128 | refChild->m_prev = newChild; | 118 | refChild->m_prev = newChild; |
129 | 119 | ||
130 | newChild->m_prev = prev; | 120 | newChild->m_prev = prev; |
131 | newChild->m_next = refChild; | 121 | newChild->m_next = refChild; |
132 | 122 | ||
133 | if ( prev ) | 123 | if ( prev ) |
134 | prev->m_next = newChild; | 124 | prev->m_next = newChild; |
135 | 125 | ||
136 | if ( refChild == m_first ) | 126 | if ( refChild == m_first ) |
137 | m_first = newChild; | 127 | m_first = newChild; |
138 | } | 128 | } |
139 | 129 | ||
140 | void XMLElement::removeChild( XMLElement *child ) | 130 | void XMLElement::removeChild( XMLElement *child ) |
141 | { | 131 | { |
142 | if ( child->m_parent != this ) | 132 | if ( child->m_parent != this ) |
143 | return; | 133 | return; |
144 | 134 | ||
145 | if ( m_first == child ) | 135 | if ( m_first == child ) |
146 | m_first = child->m_next; | 136 | m_first = child->m_next; |
147 | 137 | ||
148 | if ( m_last == child ) | 138 | if ( m_last == child ) |
149 | m_last = child->m_prev; | 139 | m_last = child->m_prev; |
150 | 140 | ||
151 | if ( child->m_prev ) | 141 | if ( child->m_prev ) |
152 | child->m_prev->m_next = child->m_next; | 142 | child->m_prev->m_next = child->m_next; |
153 | 143 | ||
154 | if ( child->m_next ) | 144 | if ( child->m_next ) |
155 | child->m_next->m_prev = child->m_prev; | 145 | child->m_next->m_prev = child->m_prev; |
156 | 146 | ||
157 | child->m_parent = 0; | 147 | child->m_parent = 0; |
158 | child->m_prev = 0; | 148 | child->m_prev = 0; |
159 | child->m_next = 0; | 149 | child->m_next = 0; |
160 | } | 150 | } |
161 | 151 | ||
162 | void XMLElement::save( QTextStream &s, uint indent ) | 152 | void XMLElement::save( QTextStream &s, uint indent ) |
163 | { | 153 | { |
164 | if ( !m_value.isEmpty() ) | 154 | if ( !m_value.isEmpty() ) |
165 | { | 155 | { |
166 | s << encodeAttr( m_value ); | 156 | s << Qtopia::escapeString( m_value ); |
167 | return; | 157 | return; |
168 | } | 158 | } |
169 | 159 | ||
170 | for ( uint i = 0; i < indent; ++i ) | 160 | for ( uint i = 0; i < indent; ++i ) |
171 | s << " "; | 161 | s << " "; |
172 | 162 | ||
173 | s << "<" << m_tag; | 163 | s << "<" << m_tag; |
174 | 164 | ||
175 | if ( !m_attributes.isEmpty() ) | 165 | if ( !m_attributes.isEmpty() ) |
176 | { | 166 | { |
177 | s << " "; | 167 | s << " "; |
178 | AttributeMap::ConstIterator it = m_attributes.begin(); | 168 | AttributeMap::ConstIterator it = m_attributes.begin(); |
179 | AttributeMap::ConstIterator end = m_attributes.end(); | 169 | AttributeMap::ConstIterator end = m_attributes.end(); |
180 | for (; it != end; ++it ) | 170 | for (; it != end; ++it ) |
181 | { | 171 | { |
182 | s << it.key() << "=\"" << encodeAttr( it.data() ) << "\""; | 172 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; |
183 | s << " "; | 173 | s << " "; |
184 | } | 174 | } |
185 | } | 175 | } |
186 | 176 | ||
187 | if ( m_last ) | 177 | if ( m_last ) |
188 | { | 178 | { |
189 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | 179 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) |
190 | s << ">"; | 180 | s << ">"; |
191 | else | 181 | else |
192 | s << ">" << endl; | 182 | s << ">" << endl; |
193 | 183 | ||
194 | int newIndent = indent; | 184 | int newIndent = indent; |
195 | if ( m_parent ) | 185 | if ( m_parent ) |
196 | newIndent++; | 186 | newIndent++; |
197 | 187 | ||
198 | XMLElement *n = m_first; | 188 | XMLElement *n = m_first; |
199 | while ( n ) | 189 | while ( n ) |
200 | { | 190 | { |
201 | n->save( s, newIndent ); | 191 | n->save( s, newIndent ); |
202 | n = n->nextChild(); | 192 | n = n->nextChild(); |
203 | } | 193 | } |
204 | 194 | ||
205 | if ( m_last && m_last->value().isEmpty() && m_parent ) | 195 | if ( m_last && m_last->value().isEmpty() && m_parent ) |
206 | for ( uint i = 0; i < indent; ++i ) | 196 | for ( uint i = 0; i < indent; ++i ) |
207 | s << " "; | 197 | s << " "; |
208 | 198 | ||
209 | if ( m_parent ) | 199 | if ( m_parent ) |
210 | s << "</" << m_tag << ">" << endl; | 200 | s << "</" << m_tag << ">" << endl; |
211 | } | 201 | } |
212 | else | 202 | else |
213 | s << "/>" << endl; | 203 | s << "/>" << endl; |
214 | } | 204 | } |
215 | 205 | ||
216 | class Handler : public QXmlDefaultHandler | 206 | class Handler : public QXmlDefaultHandler |
217 | { | 207 | { |
218 | public: | 208 | public: |
219 | Handler() : m_node( 0 ), m_root( 0 ) {} | 209 | Handler() : m_node( 0 ), m_root( 0 ) {} |
220 | 210 | ||
221 | XMLElement *root() const { return m_root; } | 211 | XMLElement *root() const { return m_root; } |
222 | 212 | ||
223 | virtual bool startDocument(); | 213 | virtual bool startDocument(); |
224 | virtual bool endDocument(); | 214 | virtual bool endDocument(); |
225 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, | 215 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, |
226 | const QXmlAttributes &attr ); | 216 | const QXmlAttributes &attr ); |
227 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); | 217 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); |
228 | virtual bool characters( const QString &ch ); | 218 | virtual bool characters( const QString &ch ); |
229 | 219 | ||
230 | private: | 220 | private: |
231 | XMLElement *m_node; | 221 | XMLElement *m_node; |
232 | XMLElement *m_root; | 222 | XMLElement *m_root; |
233 | }; | 223 | }; |
234 | 224 | ||
235 | bool Handler::startDocument() | 225 | bool Handler::startDocument() |
236 | { | 226 | { |
237 | m_root = m_node = new XMLElement; | 227 | m_root = m_node = new XMLElement; |
238 | 228 | ||
239 | return true; | 229 | return true; |
240 | } | 230 | } |
241 | 231 | ||
242 | bool Handler::endDocument() | 232 | bool Handler::endDocument() |
243 | { | 233 | { |
244 | return m_root == m_node; | 234 | return m_root == m_node; |
245 | } | 235 | } |
246 | 236 | ||
@@ -263,64 +253,66 @@ bool Handler::startElement( const QString &, const QString &, const QString &qNa | |||
263 | return true; | 253 | return true; |
264 | } | 254 | } |
265 | 255 | ||
266 | bool Handler::endElement( const QString &, const QString &, const QString & ) | 256 | bool Handler::endElement( const QString &, const QString &, const QString & ) |
267 | { | 257 | { |
268 | if ( m_node == m_root ) | 258 | if ( m_node == m_root ) |
269 | return false; | 259 | return false; |
270 | 260 | ||
271 | m_node = m_node->parent(); | 261 | m_node = m_node->parent(); |
272 | return true; | 262 | return true; |
273 | } | 263 | } |
274 | 264 | ||
275 | bool Handler::characters( const QString &ch ) | 265 | bool Handler::characters( const QString &ch ) |
276 | { | 266 | { |
277 | XMLElement *textNode = new XMLElement; | 267 | XMLElement *textNode = new XMLElement; |
278 | textNode->setValue( ch ); | 268 | textNode->setValue( ch ); |
279 | m_node->appendChild( textNode ); | 269 | m_node->appendChild( textNode ); |
280 | return true; | 270 | return true; |
281 | } | 271 | } |
282 | 272 | ||
283 | XMLElement *XMLElement::namedItem( const QString &name ) | 273 | XMLElement *XMLElement::namedItem( const QString &name ) |
284 | { | 274 | { |
285 | XMLElement *e = m_first; | 275 | XMLElement *e = m_first; |
286 | 276 | ||
287 | for (; e; e = e->nextChild() ) | 277 | for (; e; e = e->nextChild() ) |
288 | if ( e->tagName() == name ) | 278 | if ( e->tagName() == name ) |
289 | return e; | 279 | return e; |
290 | 280 | ||
291 | return 0; | 281 | return 0; |
292 | } | 282 | } |
293 | 283 | ||
294 | XMLElement *XMLElement::clone() const | 284 | XMLElement *XMLElement::clone() const |
295 | { | 285 | { |
296 | XMLElement *res = new XMLElement; | 286 | XMLElement *res = new XMLElement; |
297 | 287 | ||
298 | res->setTagName( m_tag ); | 288 | res->setTagName( m_tag ); |
299 | res->setValue( m_value ); | 289 | res->setValue( m_value ); |
300 | res->setAttributes( m_attributes ); | 290 | res->setAttributes( m_attributes ); |
301 | 291 | ||
302 | XMLElement *e = m_first; | 292 | XMLElement *e = m_first; |
303 | for (; e; e = e->m_next ) | 293 | for (; e; e = e->m_next ) |
304 | res->appendChild( e->clone() ); | 294 | res->appendChild( e->clone() ); |
305 | 295 | ||
306 | return res; | 296 | return res; |
307 | } | 297 | } |
308 | 298 | ||
309 | XMLElement *XMLElement::load( const QString &fileName ) | 299 | XMLElement *XMLElement::load( const QString &fileName ) |
310 | { | 300 | { |
311 | QFile f( fileName ); | 301 | QFile f( fileName ); |
312 | if ( !f.open( IO_ReadOnly ) ) | 302 | if ( !f.open( IO_ReadOnly ) ) |
313 | return 0; | 303 | return 0; |
314 | 304 | ||
315 | QTextStream stream( &f ); | 305 | QTextStream stream( &f ); |
316 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 306 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
317 | QXmlInputSource src( stream ); | 307 | QXmlInputSource src( stream ); |
318 | QXmlSimpleReader reader; | 308 | QXmlSimpleReader reader; |
319 | Handler handler; | 309 | Handler handler; |
320 | 310 | ||
321 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); | 311 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); |
322 | reader.setContentHandler( &handler ); | 312 | reader.setContentHandler( &handler ); |
323 | reader.parse( src ); | 313 | reader.parse( src ); |
324 | 314 | ||
325 | return handler.root();; | 315 | return handler.root();; |
326 | } | 316 | } |
317 | |||
318 | |||
diff --git a/libopie/xmltree.h b/libopie/xmltree.h index 7f1b7b7..7f50365 100644 --- a/libopie/xmltree.h +++ b/libopie/xmltree.h | |||
@@ -23,88 +23,90 @@ | |||
23 | 23 | ||
24 | #include <qstring.h> | 24 | #include <qstring.h> |
25 | #include <qmap.h> | 25 | #include <qmap.h> |
26 | #include <qtextstream.h> | 26 | #include <qtextstream.h> |
27 | 27 | ||
28 | /** | 28 | /** |
29 | * A small xml lib written by Simon Hausmann. | 29 | * A small xml lib written by Simon Hausmann. |
30 | */ | 30 | */ |
31 | class XMLElement | 31 | class XMLElement |
32 | { | 32 | { |
33 | public: | 33 | public: |
34 | typedef QMap<QString, QString> AttributeMap; | 34 | typedef QMap<QString, QString> AttributeMap; |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * The constructor of XMLElement | 37 | * The constructor of XMLElement |
38 | */ | 38 | */ |
39 | XMLElement(); | 39 | XMLElement(); |
40 | ~XMLElement(); | 40 | ~XMLElement(); |
41 | 41 | ||
42 | /** appendChild appends a child to the XMLElement behind the last element. | 42 | /** appendChild appends a child to the XMLElement behind the last element. |
43 | * The ownership of the child get's transfered to the | 43 | * The ownership of the child get's transfered to the |
44 | * this XMLElement. | 44 | * this XMLElement. |
45 | * If child is already the child of another parent | 45 | * If child is already the child of another parent |
46 | * it's get removed from the other parent first. | 46 | * it's get removed from the other parent first. |
47 | */ | 47 | */ |
48 | void appendChild( XMLElement *child ); | 48 | void appendChild( XMLElement *child ); |
49 | 49 | ||
50 | /** inserts newChild after refChild. If newChild is the child | 50 | /** inserts newChild after refChild. If newChild is the child |
51 | * of another parent the child will get removed. | 51 | * of another parent the child will get removed. |
52 | * The ownership of child gets transfered. | 52 | * The ownership of child gets transfered. |
53 | * | 53 | * |
54 | */ | 54 | */ |
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | 55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); |
56 | 56 | ||
57 | /** same as insertAfter but the element get's inserted before refChild. | 57 | /** same as insertAfter but the element get's inserted before refChild. |
58 | * | 58 | * |
59 | */ | 59 | */ |
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | 60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); |
61 | 61 | ||
62 | /** removeChild removes the child from the XMLElement. | 62 | /** removeChild removes the child from the XMLElement. |
63 | * The ownership gets dropped. You need to delete the | 63 | * The ownership gets dropped. You need to delete the |
64 | * child yourself. | 64 | * child yourself. |
65 | */ | 65 | */ |
66 | void removeChild( XMLElement *child ); | 66 | void removeChild( XMLElement *child ); |
67 | 67 | ||
68 | /** parent() returns the parent of this XMLElement | 68 | /** parent() returns the parent of this XMLElement |
69 | * If there is no parent 0l gets returned | 69 | * If there is no parent 0l gets returned |
70 | */ | 70 | */ |
71 | XMLElement *parent() const { return m_parent; } | 71 | XMLElement *parent() const { return m_parent; } |
72 | XMLElement *firstChild() const { return m_first; } | 72 | XMLElement *firstChild() const { return m_first; } |
73 | XMLElement *nextChild() const { return m_next; } | 73 | XMLElement *nextChild() const { return m_next; } |
74 | XMLElement *prevChild() const { return m_prev; } | 74 | XMLElement *prevChild() const { return m_prev; } |
75 | XMLElement *lastChild() const { return m_last; } | 75 | XMLElement *lastChild() const { return m_last; } |
76 | 76 | ||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | 77 | void setTagName( const QString &tag ) { m_tag = tag; } |
78 | QString tagName() const { return m_tag; } | 78 | QString tagName() const { return m_tag; } |
79 | 79 | ||
80 | void setValue( const QString &val ) { m_value = val; } | 80 | void setValue( const QString &val ) { m_value = val; } |
81 | QString value() const { return m_value; } | 81 | QString value() const { return m_value; } |
82 | 82 | ||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | 83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } |
84 | AttributeMap attributes() const { return m_attributes; } | 84 | AttributeMap attributes() const { return m_attributes; } |
85 | AttributeMap &attributes() { return m_attributes; } | 85 | AttributeMap &attributes() { return m_attributes; } |
86 | 86 | ||
87 | QString attribute(const QString &)const; | ||
88 | void setAttribute( const QString &attr, const QString &value ); | ||
87 | void save( QTextStream &stream, uint indent = 0 ); | 89 | void save( QTextStream &stream, uint indent = 0 ); |
88 | 90 | ||
89 | XMLElement *namedItem( const QString &name ); | 91 | XMLElement *namedItem( const QString &name ); |
90 | 92 | ||
91 | XMLElement *clone() const; | 93 | XMLElement *clone() const; |
92 | 94 | ||
93 | static XMLElement *load( const QString &fileName ); | 95 | static XMLElement *load( const QString &fileName ); |
94 | 96 | ||
95 | private: | 97 | private: |
96 | QString m_tag; | 98 | QString m_tag; |
97 | QString m_value; | 99 | QString m_value; |
98 | AttributeMap m_attributes; | 100 | AttributeMap m_attributes; |
99 | 101 | ||
100 | XMLElement *m_parent; | 102 | XMLElement *m_parent; |
101 | XMLElement *m_next; | 103 | XMLElement *m_next; |
102 | XMLElement *m_prev; | 104 | XMLElement *m_prev; |
103 | XMLElement *m_first; | 105 | XMLElement *m_first; |
104 | XMLElement *m_last; | 106 | XMLElement *m_last; |
105 | 107 | ||
106 | XMLElement( const XMLElement &rhs ); | 108 | XMLElement( const XMLElement &rhs ); |
107 | XMLElement &operator=( const XMLElement &rhs ); | 109 | XMLElement &operator=( const XMLElement &rhs ); |
108 | }; | 110 | }; |
109 | 111 | ||
110 | #endif | 112 | #endif |
diff --git a/libopie2/opiecore/xmltree.cc b/libopie2/opiecore/xmltree.cc index d5ce74a..3d03cc6 100644 --- a/libopie2/opiecore/xmltree.cc +++ b/libopie2/opiecore/xmltree.cc | |||
@@ -1,246 +1,236 @@ | |||
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 | 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 | namespace | ||
28 | { | ||
29 | |||
30 | /** | ||
31 | Encode an attribute value upon saving. | ||
32 | replaces '"' with """ | ||
33 | replaces '<' with "<" | ||
34 | replaces '&' with "&" | ||
35 | replaces '>' with ">" | ||
36 | */ | ||
37 | QString encodeAttr( const QString& str ) | ||
38 | { | ||
39 | QString encAttr( str ); // cause of the const parameter | ||
40 | encAttr = encAttr.replace( QRegExp( "[<]" ), "<" ); | ||
41 | encAttr = encAttr.replace( QRegExp( "[>]" ), ">" ); | ||
42 | encAttr = encAttr.replace( QRegExp( "[\"]" ), """ ); | ||
43 | encAttr = encAttr.replace( QRegExp( "[&]" ), "&" ); | ||
44 | return encAttr; | ||
45 | } | ||
46 | |||
47 | } | ||
48 | 27 | ||
49 | XMLElement::XMLElement() | 28 | XMLElement::XMLElement() |
50 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) | 29 | : m_parent( 0 ), m_next( 0 ), m_prev( 0 ), m_first( 0 ), m_last( 0 ) |
51 | { | 30 | { |
52 | } | 31 | } |
53 | 32 | ||
54 | XMLElement::~XMLElement() | 33 | XMLElement::~XMLElement() |
55 | { | 34 | { |
56 | XMLElement *n = m_first; | 35 | XMLElement *n = m_first; |
57 | 36 | ||
58 | while ( n ) | 37 | while ( n ) |
59 | { | 38 | { |
60 | XMLElement *tmp = n; | 39 | XMLElement *tmp = n; |
61 | n = n->m_next; | 40 | n = n->m_next; |
62 | delete tmp; | 41 | delete tmp; |
63 | } | 42 | } |
64 | } | 43 | } |
65 | 44 | ||
66 | void XMLElement::appendChild( XMLElement *child ) | 45 | void XMLElement::appendChild( XMLElement *child ) |
67 | { | 46 | { |
68 | if ( child->m_parent ) | 47 | if ( child->m_parent ) |
69 | child->m_parent->removeChild( child ); | 48 | child->m_parent->removeChild( child ); |
70 | 49 | ||
71 | child->m_parent = this; | 50 | child->m_parent = this; |
72 | 51 | ||
73 | if ( m_last ) | 52 | if ( m_last ) |
74 | m_last->m_next = child; | 53 | m_last->m_next = child; |
75 | 54 | ||
76 | child->m_prev = m_last; | 55 | child->m_prev = m_last; |
77 | 56 | ||
78 | if ( !m_first ) | 57 | if ( !m_first ) |
79 | m_first = child; | 58 | m_first = child; |
80 | 59 | ||
81 | m_last = child; | 60 | m_last = child; |
82 | } | 61 | } |
83 | 62 | ||
84 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | 63 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) |
85 | { | 64 | { |
86 | assert( newChild != refChild ); | 65 | assert( newChild != refChild ); |
87 | 66 | ||
88 | if ( refChild == m_last ) | 67 | if ( refChild == m_last ) |
89 | { | 68 | { |
90 | appendChild( newChild ); | 69 | appendChild( newChild ); |
91 | return; | 70 | return; |
92 | } | 71 | } |
93 | 72 | ||
94 | assert( refChild ); | 73 | assert( refChild ); |
95 | assert( refChild->m_parent ); | 74 | assert( refChild->m_parent ); |
96 | assert( refChild->m_parent == this ); | 75 | assert( refChild->m_parent == this ); |
97 | 76 | ||
98 | if ( newChild->m_parent && newChild != refChild ) | 77 | if ( newChild->m_parent && newChild != refChild ) |
99 | newChild->m_parent->removeChild( newChild ); | 78 | newChild->m_parent->removeChild( newChild ); |
100 | 79 | ||
101 | newChild->m_parent = this; | 80 | newChild->m_parent = this; |
102 | 81 | ||
103 | XMLElement *next = refChild->m_next; | 82 | XMLElement *next = refChild->m_next; |
104 | 83 | ||
105 | refChild->m_next = newChild; | 84 | refChild->m_next = newChild; |
106 | 85 | ||
107 | newChild->m_prev = refChild; | 86 | newChild->m_prev = refChild; |
108 | newChild->m_next = next; | 87 | newChild->m_next = next; |
109 | 88 | ||
110 | if ( next ) | 89 | if ( next ) |
111 | next->m_prev = newChild; | 90 | next->m_prev = newChild; |
112 | } | 91 | } |
113 | 92 | QString XMLElement::attribute(const QString &attr )const | |
93 | { | ||
94 | if ( !m_attributes.contains( attr ) ) | ||
95 | return QString::null; | ||
96 | AttributeMap::ConstIterator it = m_attributes.find( attr ); | ||
97 | return it.data(); | ||
98 | } | ||
99 | void XMLElement::setAttribute(const QString &attr, const QString &value ) | ||
100 | { | ||
101 | m_attributes.remove( attr ); | ||
102 | m_attributes.insert( attr, value ); | ||
103 | } | ||
114 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | 104 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) |
115 | { | 105 | { |
116 | assert( refChild ); | 106 | assert( refChild ); |
117 | assert( refChild->m_parent ); | 107 | assert( refChild->m_parent ); |
118 | assert( refChild->m_parent == this ); | 108 | assert( refChild->m_parent == this ); |
119 | assert( newChild != refChild ); | 109 | assert( newChild != refChild ); |
120 | 110 | ||
121 | if ( newChild->m_parent && newChild != refChild ) | 111 | if ( newChild->m_parent && newChild != refChild ) |
122 | newChild->m_parent->removeChild( newChild ); | 112 | newChild->m_parent->removeChild( newChild ); |
123 | 113 | ||
124 | newChild->m_parent = this; | 114 | newChild->m_parent = this; |
125 | 115 | ||
126 | XMLElement *prev = refChild->m_prev; | 116 | XMLElement *prev = refChild->m_prev; |
127 | 117 | ||
128 | refChild->m_prev = newChild; | 118 | refChild->m_prev = newChild; |
129 | 119 | ||
130 | newChild->m_prev = prev; | 120 | newChild->m_prev = prev; |
131 | newChild->m_next = refChild; | 121 | newChild->m_next = refChild; |
132 | 122 | ||
133 | if ( prev ) | 123 | if ( prev ) |
134 | prev->m_next = newChild; | 124 | prev->m_next = newChild; |
135 | 125 | ||
136 | if ( refChild == m_first ) | 126 | if ( refChild == m_first ) |
137 | m_first = newChild; | 127 | m_first = newChild; |
138 | } | 128 | } |
139 | 129 | ||
140 | void XMLElement::removeChild( XMLElement *child ) | 130 | void XMLElement::removeChild( XMLElement *child ) |
141 | { | 131 | { |
142 | if ( child->m_parent != this ) | 132 | if ( child->m_parent != this ) |
143 | return; | 133 | return; |
144 | 134 | ||
145 | if ( m_first == child ) | 135 | if ( m_first == child ) |
146 | m_first = child->m_next; | 136 | m_first = child->m_next; |
147 | 137 | ||
148 | if ( m_last == child ) | 138 | if ( m_last == child ) |
149 | m_last = child->m_prev; | 139 | m_last = child->m_prev; |
150 | 140 | ||
151 | if ( child->m_prev ) | 141 | if ( child->m_prev ) |
152 | child->m_prev->m_next = child->m_next; | 142 | child->m_prev->m_next = child->m_next; |
153 | 143 | ||
154 | if ( child->m_next ) | 144 | if ( child->m_next ) |
155 | child->m_next->m_prev = child->m_prev; | 145 | child->m_next->m_prev = child->m_prev; |
156 | 146 | ||
157 | child->m_parent = 0; | 147 | child->m_parent = 0; |
158 | child->m_prev = 0; | 148 | child->m_prev = 0; |
159 | child->m_next = 0; | 149 | child->m_next = 0; |
160 | } | 150 | } |
161 | 151 | ||
162 | void XMLElement::save( QTextStream &s, uint indent ) | 152 | void XMLElement::save( QTextStream &s, uint indent ) |
163 | { | 153 | { |
164 | if ( !m_value.isEmpty() ) | 154 | if ( !m_value.isEmpty() ) |
165 | { | 155 | { |
166 | s << encodeAttr( m_value ); | 156 | s << Qtopia::escapeString( m_value ); |
167 | return; | 157 | return; |
168 | } | 158 | } |
169 | 159 | ||
170 | for ( uint i = 0; i < indent; ++i ) | 160 | for ( uint i = 0; i < indent; ++i ) |
171 | s << " "; | 161 | s << " "; |
172 | 162 | ||
173 | s << "<" << m_tag; | 163 | s << "<" << m_tag; |
174 | 164 | ||
175 | if ( !m_attributes.isEmpty() ) | 165 | if ( !m_attributes.isEmpty() ) |
176 | { | 166 | { |
177 | s << " "; | 167 | s << " "; |
178 | AttributeMap::ConstIterator it = m_attributes.begin(); | 168 | AttributeMap::ConstIterator it = m_attributes.begin(); |
179 | AttributeMap::ConstIterator end = m_attributes.end(); | 169 | AttributeMap::ConstIterator end = m_attributes.end(); |
180 | for (; it != end; ++it ) | 170 | for (; it != end; ++it ) |
181 | { | 171 | { |
182 | s << it.key() << "=\"" << encodeAttr( it.data() ) << "\""; | 172 | s << it.key() << "=\"" << Qtopia::escapeString( it.data() ) << "\""; |
183 | s << " "; | 173 | s << " "; |
184 | } | 174 | } |
185 | } | 175 | } |
186 | 176 | ||
187 | if ( m_last ) | 177 | if ( m_last ) |
188 | { | 178 | { |
189 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | 179 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) |
190 | s << ">"; | 180 | s << ">"; |
191 | else | 181 | else |
192 | s << ">" << endl; | 182 | s << ">" << endl; |
193 | 183 | ||
194 | int newIndent = indent; | 184 | int newIndent = indent; |
195 | if ( m_parent ) | 185 | if ( m_parent ) |
196 | newIndent++; | 186 | newIndent++; |
197 | 187 | ||
198 | XMLElement *n = m_first; | 188 | XMLElement *n = m_first; |
199 | while ( n ) | 189 | while ( n ) |
200 | { | 190 | { |
201 | n->save( s, newIndent ); | 191 | n->save( s, newIndent ); |
202 | n = n->nextChild(); | 192 | n = n->nextChild(); |
203 | } | 193 | } |
204 | 194 | ||
205 | if ( m_last && m_last->value().isEmpty() && m_parent ) | 195 | if ( m_last && m_last->value().isEmpty() && m_parent ) |
206 | for ( uint i = 0; i < indent; ++i ) | 196 | for ( uint i = 0; i < indent; ++i ) |
207 | s << " "; | 197 | s << " "; |
208 | 198 | ||
209 | if ( m_parent ) | 199 | if ( m_parent ) |
210 | s << "</" << m_tag << ">" << endl; | 200 | s << "</" << m_tag << ">" << endl; |
211 | } | 201 | } |
212 | else | 202 | else |
213 | s << "/>" << endl; | 203 | s << "/>" << endl; |
214 | } | 204 | } |
215 | 205 | ||
216 | class Handler : public QXmlDefaultHandler | 206 | class Handler : public QXmlDefaultHandler |
217 | { | 207 | { |
218 | public: | 208 | public: |
219 | Handler() : m_node( 0 ), m_root( 0 ) {} | 209 | Handler() : m_node( 0 ), m_root( 0 ) {} |
220 | 210 | ||
221 | XMLElement *root() const { return m_root; } | 211 | XMLElement *root() const { return m_root; } |
222 | 212 | ||
223 | virtual bool startDocument(); | 213 | virtual bool startDocument(); |
224 | virtual bool endDocument(); | 214 | virtual bool endDocument(); |
225 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, | 215 | virtual bool startElement( const QString &ns, const QString &ln, const QString &qName, |
226 | const QXmlAttributes &attr ); | 216 | const QXmlAttributes &attr ); |
227 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); | 217 | virtual bool endElement( const QString &ns, const QString &ln, const QString &qName ); |
228 | virtual bool characters( const QString &ch ); | 218 | virtual bool characters( const QString &ch ); |
229 | 219 | ||
230 | private: | 220 | private: |
231 | XMLElement *m_node; | 221 | XMLElement *m_node; |
232 | XMLElement *m_root; | 222 | XMLElement *m_root; |
233 | }; | 223 | }; |
234 | 224 | ||
235 | bool Handler::startDocument() | 225 | bool Handler::startDocument() |
236 | { | 226 | { |
237 | m_root = m_node = new XMLElement; | 227 | m_root = m_node = new XMLElement; |
238 | 228 | ||
239 | return true; | 229 | return true; |
240 | } | 230 | } |
241 | 231 | ||
242 | bool Handler::endDocument() | 232 | bool Handler::endDocument() |
243 | { | 233 | { |
244 | return m_root == m_node; | 234 | return m_root == m_node; |
245 | } | 235 | } |
246 | 236 | ||
@@ -263,64 +253,66 @@ bool Handler::startElement( const QString &, const QString &, const QString &qNa | |||
263 | return true; | 253 | return true; |
264 | } | 254 | } |
265 | 255 | ||
266 | bool Handler::endElement( const QString &, const QString &, const QString & ) | 256 | bool Handler::endElement( const QString &, const QString &, const QString & ) |
267 | { | 257 | { |
268 | if ( m_node == m_root ) | 258 | if ( m_node == m_root ) |
269 | return false; | 259 | return false; |
270 | 260 | ||
271 | m_node = m_node->parent(); | 261 | m_node = m_node->parent(); |
272 | return true; | 262 | return true; |
273 | } | 263 | } |
274 | 264 | ||
275 | bool Handler::characters( const QString &ch ) | 265 | bool Handler::characters( const QString &ch ) |
276 | { | 266 | { |
277 | XMLElement *textNode = new XMLElement; | 267 | XMLElement *textNode = new XMLElement; |
278 | textNode->setValue( ch ); | 268 | textNode->setValue( ch ); |
279 | m_node->appendChild( textNode ); | 269 | m_node->appendChild( textNode ); |
280 | return true; | 270 | return true; |
281 | } | 271 | } |
282 | 272 | ||
283 | XMLElement *XMLElement::namedItem( const QString &name ) | 273 | XMLElement *XMLElement::namedItem( const QString &name ) |
284 | { | 274 | { |
285 | XMLElement *e = m_first; | 275 | XMLElement *e = m_first; |
286 | 276 | ||
287 | for (; e; e = e->nextChild() ) | 277 | for (; e; e = e->nextChild() ) |
288 | if ( e->tagName() == name ) | 278 | if ( e->tagName() == name ) |
289 | return e; | 279 | return e; |
290 | 280 | ||
291 | return 0; | 281 | return 0; |
292 | } | 282 | } |
293 | 283 | ||
294 | XMLElement *XMLElement::clone() const | 284 | XMLElement *XMLElement::clone() const |
295 | { | 285 | { |
296 | XMLElement *res = new XMLElement; | 286 | XMLElement *res = new XMLElement; |
297 | 287 | ||
298 | res->setTagName( m_tag ); | 288 | res->setTagName( m_tag ); |
299 | res->setValue( m_value ); | 289 | res->setValue( m_value ); |
300 | res->setAttributes( m_attributes ); | 290 | res->setAttributes( m_attributes ); |
301 | 291 | ||
302 | XMLElement *e = m_first; | 292 | XMLElement *e = m_first; |
303 | for (; e; e = e->m_next ) | 293 | for (; e; e = e->m_next ) |
304 | res->appendChild( e->clone() ); | 294 | res->appendChild( e->clone() ); |
305 | 295 | ||
306 | return res; | 296 | return res; |
307 | } | 297 | } |
308 | 298 | ||
309 | XMLElement *XMLElement::load( const QString &fileName ) | 299 | XMLElement *XMLElement::load( const QString &fileName ) |
310 | { | 300 | { |
311 | QFile f( fileName ); | 301 | QFile f( fileName ); |
312 | if ( !f.open( IO_ReadOnly ) ) | 302 | if ( !f.open( IO_ReadOnly ) ) |
313 | return 0; | 303 | return 0; |
314 | 304 | ||
315 | QTextStream stream( &f ); | 305 | QTextStream stream( &f ); |
316 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 306 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
317 | QXmlInputSource src( stream ); | 307 | QXmlInputSource src( stream ); |
318 | QXmlSimpleReader reader; | 308 | QXmlSimpleReader reader; |
319 | Handler handler; | 309 | Handler handler; |
320 | 310 | ||
321 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); | 311 | reader.setFeature( "http://trolltech.com/xml/features/report-whitespace-only-CharData", false ); |
322 | reader.setContentHandler( &handler ); | 312 | reader.setContentHandler( &handler ); |
323 | reader.parse( src ); | 313 | reader.parse( src ); |
324 | 314 | ||
325 | return handler.root();; | 315 | return handler.root();; |
326 | } | 316 | } |
317 | |||
318 | |||
diff --git a/libopie2/opiecore/xmltree.h b/libopie2/opiecore/xmltree.h index 7f1b7b7..7f50365 100644 --- a/libopie2/opiecore/xmltree.h +++ b/libopie2/opiecore/xmltree.h | |||
@@ -23,88 +23,90 @@ | |||
23 | 23 | ||
24 | #include <qstring.h> | 24 | #include <qstring.h> |
25 | #include <qmap.h> | 25 | #include <qmap.h> |
26 | #include <qtextstream.h> | 26 | #include <qtextstream.h> |
27 | 27 | ||
28 | /** | 28 | /** |
29 | * A small xml lib written by Simon Hausmann. | 29 | * A small xml lib written by Simon Hausmann. |
30 | */ | 30 | */ |
31 | class XMLElement | 31 | class XMLElement |
32 | { | 32 | { |
33 | public: | 33 | public: |
34 | typedef QMap<QString, QString> AttributeMap; | 34 | typedef QMap<QString, QString> AttributeMap; |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * The constructor of XMLElement | 37 | * The constructor of XMLElement |
38 | */ | 38 | */ |
39 | XMLElement(); | 39 | XMLElement(); |
40 | ~XMLElement(); | 40 | ~XMLElement(); |
41 | 41 | ||
42 | /** appendChild appends a child to the XMLElement behind the last element. | 42 | /** appendChild appends a child to the XMLElement behind the last element. |
43 | * The ownership of the child get's transfered to the | 43 | * The ownership of the child get's transfered to the |
44 | * this XMLElement. | 44 | * this XMLElement. |
45 | * If child is already the child of another parent | 45 | * If child is already the child of another parent |
46 | * it's get removed from the other parent first. | 46 | * it's get removed from the other parent first. |
47 | */ | 47 | */ |
48 | void appendChild( XMLElement *child ); | 48 | void appendChild( XMLElement *child ); |
49 | 49 | ||
50 | /** inserts newChild after refChild. If newChild is the child | 50 | /** inserts newChild after refChild. If newChild is the child |
51 | * of another parent the child will get removed. | 51 | * of another parent the child will get removed. |
52 | * The ownership of child gets transfered. | 52 | * The ownership of child gets transfered. |
53 | * | 53 | * |
54 | */ | 54 | */ |
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | 55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); |
56 | 56 | ||
57 | /** same as insertAfter but the element get's inserted before refChild. | 57 | /** same as insertAfter but the element get's inserted before refChild. |
58 | * | 58 | * |
59 | */ | 59 | */ |
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | 60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); |
61 | 61 | ||
62 | /** removeChild removes the child from the XMLElement. | 62 | /** removeChild removes the child from the XMLElement. |
63 | * The ownership gets dropped. You need to delete the | 63 | * The ownership gets dropped. You need to delete the |
64 | * child yourself. | 64 | * child yourself. |
65 | */ | 65 | */ |
66 | void removeChild( XMLElement *child ); | 66 | void removeChild( XMLElement *child ); |
67 | 67 | ||
68 | /** parent() returns the parent of this XMLElement | 68 | /** parent() returns the parent of this XMLElement |
69 | * If there is no parent 0l gets returned | 69 | * If there is no parent 0l gets returned |
70 | */ | 70 | */ |
71 | XMLElement *parent() const { return m_parent; } | 71 | XMLElement *parent() const { return m_parent; } |
72 | XMLElement *firstChild() const { return m_first; } | 72 | XMLElement *firstChild() const { return m_first; } |
73 | XMLElement *nextChild() const { return m_next; } | 73 | XMLElement *nextChild() const { return m_next; } |
74 | XMLElement *prevChild() const { return m_prev; } | 74 | XMLElement *prevChild() const { return m_prev; } |
75 | XMLElement *lastChild() const { return m_last; } | 75 | XMLElement *lastChild() const { return m_last; } |
76 | 76 | ||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | 77 | void setTagName( const QString &tag ) { m_tag = tag; } |
78 | QString tagName() const { return m_tag; } | 78 | QString tagName() const { return m_tag; } |
79 | 79 | ||
80 | void setValue( const QString &val ) { m_value = val; } | 80 | void setValue( const QString &val ) { m_value = val; } |
81 | QString value() const { return m_value; } | 81 | QString value() const { return m_value; } |
82 | 82 | ||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | 83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } |
84 | AttributeMap attributes() const { return m_attributes; } | 84 | AttributeMap attributes() const { return m_attributes; } |
85 | AttributeMap &attributes() { return m_attributes; } | 85 | AttributeMap &attributes() { return m_attributes; } |
86 | 86 | ||
87 | QString attribute(const QString &)const; | ||
88 | void setAttribute( const QString &attr, const QString &value ); | ||
87 | void save( QTextStream &stream, uint indent = 0 ); | 89 | void save( QTextStream &stream, uint indent = 0 ); |
88 | 90 | ||
89 | XMLElement *namedItem( const QString &name ); | 91 | XMLElement *namedItem( const QString &name ); |
90 | 92 | ||
91 | XMLElement *clone() const; | 93 | XMLElement *clone() const; |
92 | 94 | ||
93 | static XMLElement *load( const QString &fileName ); | 95 | static XMLElement *load( const QString &fileName ); |
94 | 96 | ||
95 | private: | 97 | private: |
96 | QString m_tag; | 98 | QString m_tag; |
97 | QString m_value; | 99 | QString m_value; |
98 | AttributeMap m_attributes; | 100 | AttributeMap m_attributes; |
99 | 101 | ||
100 | XMLElement *m_parent; | 102 | XMLElement *m_parent; |
101 | XMLElement *m_next; | 103 | XMLElement *m_next; |
102 | XMLElement *m_prev; | 104 | XMLElement *m_prev; |
103 | XMLElement *m_first; | 105 | XMLElement *m_first; |
104 | XMLElement *m_last; | 106 | XMLElement *m_last; |
105 | 107 | ||
106 | XMLElement( const XMLElement &rhs ); | 108 | XMLElement( const XMLElement &rhs ); |
107 | XMLElement &operator=( const XMLElement &rhs ); | 109 | XMLElement &operator=( const XMLElement &rhs ); |
108 | }; | 110 | }; |
109 | 111 | ||
110 | #endif | 112 | #endif |