-rw-r--r-- | include/opie/xmltree.h | 110 | ||||
-rw-r--r-- | libopie/libopie.pro | 7 | ||||
-rw-r--r-- | libopie/xmltree.cc | 2 | ||||
-rw-r--r-- | libopie/xmltree.h | 110 | ||||
-rw-r--r-- | libopie2/opiecore/xmltree.cc | 2 | ||||
-rw-r--r-- | libopie2/opiecore/xmltree.h | 110 |
6 files changed, 339 insertions, 2 deletions
diff --git a/include/opie/xmltree.h b/include/opie/xmltree.h new file mode 100644 index 0000000..7f1b7b7 --- a/dev/null +++ b/include/opie/xmltree.h | |||
@@ -0,0 +1,110 @@ | |||
1 | /* This file is part of the KDE project | ||
2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | |||
20 | |||
21 | #ifndef __bookmarks_h__ | ||
22 | #define __bookmarks_h__ | ||
23 | |||
24 | #include <qstring.h> | ||
25 | #include <qmap.h> | ||
26 | #include <qtextstream.h> | ||
27 | |||
28 | /** | ||
29 | * A small xml lib written by Simon Hausmann. | ||
30 | */ | ||
31 | class XMLElement | ||
32 | { | ||
33 | public: | ||
34 | typedef QMap<QString, QString> AttributeMap; | ||
35 | |||
36 | /** | ||
37 | * The constructor of XMLElement | ||
38 | */ | ||
39 | XMLElement(); | ||
40 | ~XMLElement(); | ||
41 | |||
42 | /** appendChild appends a child to the XMLElement behind the last element. | ||
43 | * The ownership of the child get's transfered to the | ||
44 | * this XMLElement. | ||
45 | * If child is already the child of another parent | ||
46 | * it's get removed from the other parent first. | ||
47 | */ | ||
48 | void appendChild( XMLElement *child ); | ||
49 | |||
50 | /** inserts newChild after refChild. If newChild is the child | ||
51 | * of another parent the child will get removed. | ||
52 | * The ownership of child gets transfered. | ||
53 | * | ||
54 | */ | ||
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | ||
56 | |||
57 | /** same as insertAfter but the element get's inserted before refChild. | ||
58 | * | ||
59 | */ | ||
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | ||
61 | |||
62 | /** removeChild removes the child from the XMLElement. | ||
63 | * The ownership gets dropped. You need to delete the | ||
64 | * child yourself. | ||
65 | */ | ||
66 | void removeChild( XMLElement *child ); | ||
67 | |||
68 | /** parent() returns the parent of this XMLElement | ||
69 | * If there is no parent 0l gets returned | ||
70 | */ | ||
71 | XMLElement *parent() const { return m_parent; } | ||
72 | XMLElement *firstChild() const { return m_first; } | ||
73 | XMLElement *nextChild() const { return m_next; } | ||
74 | XMLElement *prevChild() const { return m_prev; } | ||
75 | XMLElement *lastChild() const { return m_last; } | ||
76 | |||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | ||
78 | QString tagName() const { return m_tag; } | ||
79 | |||
80 | void setValue( const QString &val ) { m_value = val; } | ||
81 | QString value() const { return m_value; } | ||
82 | |||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | ||
84 | AttributeMap attributes() const { return m_attributes; } | ||
85 | AttributeMap &attributes() { return m_attributes; } | ||
86 | |||
87 | void save( QTextStream &stream, uint indent = 0 ); | ||
88 | |||
89 | XMLElement *namedItem( const QString &name ); | ||
90 | |||
91 | XMLElement *clone() const; | ||
92 | |||
93 | static XMLElement *load( const QString &fileName ); | ||
94 | |||
95 | private: | ||
96 | QString m_tag; | ||
97 | QString m_value; | ||
98 | AttributeMap m_attributes; | ||
99 | |||
100 | XMLElement *m_parent; | ||
101 | XMLElement *m_next; | ||
102 | XMLElement *m_prev; | ||
103 | XMLElement *m_first; | ||
104 | XMLElement *m_last; | ||
105 | |||
106 | XMLElement( const XMLElement &rhs ); | ||
107 | XMLElement &operator=( const XMLElement &rhs ); | ||
108 | }; | ||
109 | |||
110 | #endif | ||
diff --git a/libopie/libopie.pro b/libopie/libopie.pro new file mode 100644 index 0000000..95ec145 --- a/dev/null +++ b/libopie/libopie.pro | |||
@@ -0,0 +1,7 @@ | |||
1 | TEMPLATE = lib | ||
2 | CONFIG += qte warn_on release | ||
3 | HEADERS = $(OPIEDIR)/include/opie/xmltree.h | ||
4 | SOURCES = xmltree.cc | ||
5 | TARGET = opie | ||
6 | INCLUDEPATH += $(OPIEDIR)/include | ||
7 | DESTDIR = $(QTDIR)/lib$(PROJMAK) | ||
diff --git a/libopie/xmltree.cc b/libopie/xmltree.cc index bf93151..d5ce74a 100644 --- a/libopie/xmltree.cc +++ b/libopie/xmltree.cc | |||
@@ -1,326 +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 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 | ||
21 | #include "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 | 27 | namespace |
28 | { | 28 | { |
29 | 29 | ||
30 | /** | 30 | /** |
31 | Encode an attribute value upon saving. | 31 | Encode an attribute value upon saving. |
32 | replaces '"' with """ | 32 | replaces '"' with """ |
33 | replaces '<' with "<" | 33 | replaces '<' with "<" |
34 | replaces '&' with "&" | 34 | replaces '&' with "&" |
35 | replaces '>' with ">" | 35 | replaces '>' with ">" |
36 | */ | 36 | */ |
37 | QString encodeAttr( const QString& str ) | 37 | QString encodeAttr( const QString& str ) |
38 | { | 38 | { |
39 | QString encAttr( str ); // cause of the const parameter | 39 | QString encAttr( str ); // cause of the const parameter |
40 | encAttr = encAttr.replace( QRegExp( "[<]" ), "<" ); | 40 | encAttr = encAttr.replace( QRegExp( "[<]" ), "<" ); |
41 | encAttr = encAttr.replace( QRegExp( "[>]" ), ">" ); | 41 | encAttr = encAttr.replace( QRegExp( "[>]" ), ">" ); |
42 | encAttr = encAttr.replace( QRegExp( "[\"]" ), """ ); | 42 | encAttr = encAttr.replace( QRegExp( "[\"]" ), """ ); |
43 | encAttr = encAttr.replace( QRegExp( "[&]" ), "&" ); | 43 | encAttr = encAttr.replace( QRegExp( "[&]" ), "&" ); |
44 | return encAttr; | 44 | return encAttr; |
45 | } | 45 | } |
46 | 46 | ||
47 | } | 47 | } |
48 | 48 | ||
49 | XMLElement::XMLElement() | 49 | XMLElement::XMLElement() |
50 | : 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 ) |
51 | { | 51 | { |
52 | } | 52 | } |
53 | 53 | ||
54 | XMLElement::~XMLElement() | 54 | XMLElement::~XMLElement() |
55 | { | 55 | { |
56 | XMLElement *n = m_first; | 56 | XMLElement *n = m_first; |
57 | 57 | ||
58 | while ( n ) | 58 | while ( n ) |
59 | { | 59 | { |
60 | XMLElement *tmp = n; | 60 | XMLElement *tmp = n; |
61 | n = n->m_next; | 61 | n = n->m_next; |
62 | delete tmp; | 62 | delete tmp; |
63 | } | 63 | } |
64 | } | 64 | } |
65 | 65 | ||
66 | void XMLElement::appendChild( XMLElement *child ) | 66 | void XMLElement::appendChild( XMLElement *child ) |
67 | { | 67 | { |
68 | if ( child->m_parent ) | 68 | if ( child->m_parent ) |
69 | child->m_parent->removeChild( child ); | 69 | child->m_parent->removeChild( child ); |
70 | 70 | ||
71 | child->m_parent = this; | 71 | child->m_parent = this; |
72 | 72 | ||
73 | if ( m_last ) | 73 | if ( m_last ) |
74 | m_last->m_next = child; | 74 | m_last->m_next = child; |
75 | 75 | ||
76 | child->m_prev = m_last; | 76 | child->m_prev = m_last; |
77 | 77 | ||
78 | if ( !m_first ) | 78 | if ( !m_first ) |
79 | m_first = child; | 79 | m_first = child; |
80 | 80 | ||
81 | m_last = child; | 81 | m_last = child; |
82 | } | 82 | } |
83 | 83 | ||
84 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | 84 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) |
85 | { | 85 | { |
86 | assert( newChild != refChild ); | 86 | assert( newChild != refChild ); |
87 | 87 | ||
88 | if ( refChild == m_last ) | 88 | if ( refChild == m_last ) |
89 | { | 89 | { |
90 | appendChild( newChild ); | 90 | appendChild( newChild ); |
91 | return; | 91 | return; |
92 | } | 92 | } |
93 | 93 | ||
94 | assert( refChild ); | 94 | assert( refChild ); |
95 | assert( refChild->m_parent ); | 95 | assert( refChild->m_parent ); |
96 | assert( refChild->m_parent == this ); | 96 | assert( refChild->m_parent == this ); |
97 | 97 | ||
98 | if ( newChild->m_parent && newChild != refChild ) | 98 | if ( newChild->m_parent && newChild != refChild ) |
99 | newChild->m_parent->removeChild( newChild ); | 99 | newChild->m_parent->removeChild( newChild ); |
100 | 100 | ||
101 | newChild->m_parent = this; | 101 | newChild->m_parent = this; |
102 | 102 | ||
103 | XMLElement *next = refChild->m_next; | 103 | XMLElement *next = refChild->m_next; |
104 | 104 | ||
105 | refChild->m_next = newChild; | 105 | refChild->m_next = newChild; |
106 | 106 | ||
107 | newChild->m_prev = refChild; | 107 | newChild->m_prev = refChild; |
108 | newChild->m_next = next; | 108 | newChild->m_next = next; |
109 | 109 | ||
110 | if ( next ) | 110 | if ( next ) |
111 | next->m_prev = newChild; | 111 | next->m_prev = newChild; |
112 | } | 112 | } |
113 | 113 | ||
114 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | 114 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) |
115 | { | 115 | { |
116 | assert( refChild ); | 116 | assert( refChild ); |
117 | assert( refChild->m_parent ); | 117 | assert( refChild->m_parent ); |
118 | assert( refChild->m_parent == this ); | 118 | assert( refChild->m_parent == this ); |
119 | assert( newChild != refChild ); | 119 | assert( newChild != refChild ); |
120 | 120 | ||
121 | if ( newChild->m_parent && newChild != refChild ) | 121 | if ( newChild->m_parent && newChild != refChild ) |
122 | newChild->m_parent->removeChild( newChild ); | 122 | newChild->m_parent->removeChild( newChild ); |
123 | 123 | ||
124 | newChild->m_parent = this; | 124 | newChild->m_parent = this; |
125 | 125 | ||
126 | XMLElement *prev = refChild->m_prev; | 126 | XMLElement *prev = refChild->m_prev; |
127 | 127 | ||
128 | refChild->m_prev = newChild; | 128 | refChild->m_prev = newChild; |
129 | 129 | ||
130 | newChild->m_prev = prev; | 130 | newChild->m_prev = prev; |
131 | newChild->m_next = refChild; | 131 | newChild->m_next = refChild; |
132 | 132 | ||
133 | if ( prev ) | 133 | if ( prev ) |
134 | prev->m_next = newChild; | 134 | prev->m_next = newChild; |
135 | 135 | ||
136 | if ( refChild == m_first ) | 136 | if ( refChild == m_first ) |
137 | m_first = newChild; | 137 | m_first = newChild; |
138 | } | 138 | } |
139 | 139 | ||
140 | void XMLElement::removeChild( XMLElement *child ) | 140 | void XMLElement::removeChild( XMLElement *child ) |
141 | { | 141 | { |
142 | if ( child->m_parent != this ) | 142 | if ( child->m_parent != this ) |
143 | return; | 143 | return; |
144 | 144 | ||
145 | if ( m_first == child ) | 145 | if ( m_first == child ) |
146 | m_first = child->m_next; | 146 | m_first = child->m_next; |
147 | 147 | ||
148 | if ( m_last == child ) | 148 | if ( m_last == child ) |
149 | m_last = child->m_prev; | 149 | m_last = child->m_prev; |
150 | 150 | ||
151 | if ( child->m_prev ) | 151 | if ( child->m_prev ) |
152 | child->m_prev->m_next = child->m_next; | 152 | child->m_prev->m_next = child->m_next; |
153 | 153 | ||
154 | if ( child->m_next ) | 154 | if ( child->m_next ) |
155 | child->m_next->m_prev = child->m_prev; | 155 | child->m_next->m_prev = child->m_prev; |
156 | 156 | ||
157 | child->m_parent = 0; | 157 | child->m_parent = 0; |
158 | child->m_prev = 0; | 158 | child->m_prev = 0; |
159 | child->m_next = 0; | 159 | child->m_next = 0; |
160 | } | 160 | } |
161 | 161 | ||
162 | void XMLElement::save( QTextStream &s, uint indent ) | 162 | void XMLElement::save( QTextStream &s, uint indent ) |
163 | { | 163 | { |
164 | if ( !m_value.isEmpty() ) | 164 | if ( !m_value.isEmpty() ) |
165 | { | 165 | { |
166 | s << encodeAttr( m_value ); | 166 | s << encodeAttr( m_value ); |
167 | return; | 167 | return; |
168 | } | 168 | } |
169 | 169 | ||
170 | for ( uint i = 0; i < indent; ++i ) | 170 | for ( uint i = 0; i < indent; ++i ) |
171 | s << " "; | 171 | s << " "; |
172 | 172 | ||
173 | s << "<" << m_tag; | 173 | s << "<" << m_tag; |
174 | 174 | ||
175 | if ( !m_attributes.isEmpty() ) | 175 | if ( !m_attributes.isEmpty() ) |
176 | { | 176 | { |
177 | s << " "; | 177 | s << " "; |
178 | AttributeMap::ConstIterator it = m_attributes.begin(); | 178 | AttributeMap::ConstIterator it = m_attributes.begin(); |
179 | AttributeMap::ConstIterator end = m_attributes.end(); | 179 | AttributeMap::ConstIterator end = m_attributes.end(); |
180 | for (; it != end; ++it ) | 180 | for (; it != end; ++it ) |
181 | { | 181 | { |
182 | s << it.key() << "=\"" << encodeAttr( it.data() ) << "\""; | 182 | s << it.key() << "=\"" << encodeAttr( it.data() ) << "\""; |
183 | s << " "; | 183 | s << " "; |
184 | } | 184 | } |
185 | } | 185 | } |
186 | 186 | ||
187 | if ( m_last ) | 187 | if ( m_last ) |
188 | { | 188 | { |
189 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | 189 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) |
190 | s << ">"; | 190 | s << ">"; |
191 | else | 191 | else |
192 | s << ">" << endl; | 192 | s << ">" << endl; |
193 | 193 | ||
194 | int newIndent = indent; | 194 | int newIndent = indent; |
195 | if ( m_parent ) | 195 | if ( m_parent ) |
196 | newIndent++; | 196 | newIndent++; |
197 | 197 | ||
198 | XMLElement *n = m_first; | 198 | XMLElement *n = m_first; |
199 | while ( n ) | 199 | while ( n ) |
200 | { | 200 | { |
201 | n->save( s, newIndent ); | 201 | n->save( s, newIndent ); |
202 | n = n->nextChild(); | 202 | n = n->nextChild(); |
203 | } | 203 | } |
204 | 204 | ||
205 | if ( m_last && m_last->value().isEmpty() && m_parent ) | 205 | if ( m_last && m_last->value().isEmpty() && m_parent ) |
206 | for ( uint i = 0; i < indent; ++i ) | 206 | for ( uint i = 0; i < indent; ++i ) |
207 | s << " "; | 207 | s << " "; |
208 | 208 | ||
209 | if ( m_parent ) | 209 | if ( m_parent ) |
210 | s << "</" << m_tag << ">" << endl; | 210 | s << "</" << m_tag << ">" << endl; |
211 | } | 211 | } |
212 | else | 212 | else |
213 | s << "/>" << endl; | 213 | s << "/>" << endl; |
214 | } | 214 | } |
215 | 215 | ||
216 | class Handler : public QXmlDefaultHandler | 216 | class Handler : public QXmlDefaultHandler |
217 | { | 217 | { |
218 | public: | 218 | public: |
219 | Handler() : m_node( 0 ), m_root( 0 ) {} | 219 | Handler() : m_node( 0 ), m_root( 0 ) {} |
220 | 220 | ||
221 | XMLElement *root() const { return m_root; } | 221 | XMLElement *root() const { return m_root; } |
222 | 222 | ||
223 | virtual bool startDocument(); | 223 | virtual bool startDocument(); |
224 | virtual bool endDocument(); | 224 | virtual bool endDocument(); |
225 | 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, |
226 | const QXmlAttributes &attr ); | 226 | const QXmlAttributes &attr ); |
227 | 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 ); |
228 | virtual bool characters( const QString &ch ); | 228 | virtual bool characters( const QString &ch ); |
229 | 229 | ||
230 | private: | 230 | private: |
231 | XMLElement *m_node; | 231 | XMLElement *m_node; |
232 | XMLElement *m_root; | 232 | XMLElement *m_root; |
233 | }; | 233 | }; |
234 | 234 | ||
235 | bool Handler::startDocument() | 235 | bool Handler::startDocument() |
236 | { | 236 | { |
237 | m_root = m_node = new XMLElement; | 237 | m_root = m_node = new XMLElement; |
238 | 238 | ||
239 | return true; | 239 | return true; |
240 | } | 240 | } |
241 | 241 | ||
242 | bool Handler::endDocument() | 242 | bool Handler::endDocument() |
243 | { | 243 | { |
244 | return m_root == m_node; | 244 | return m_root == m_node; |
245 | } | 245 | } |
246 | 246 | ||
247 | bool Handler::startElement( const QString &, const QString &, const QString &qName, | 247 | bool Handler::startElement( const QString &, const QString &, const QString &qName, |
248 | const QXmlAttributes &attr ) | 248 | const QXmlAttributes &attr ) |
249 | { | 249 | { |
250 | XMLElement *bm = new XMLElement; | 250 | XMLElement *bm = new XMLElement; |
251 | 251 | ||
252 | XMLElement::AttributeMap attributes; | 252 | XMLElement::AttributeMap attributes; |
253 | for ( int i = 0; i < attr.length(); ++i ) | 253 | for ( int i = 0; i < attr.length(); ++i ) |
254 | attributes[ attr.qName( i ) ] = attr.value( i ); | 254 | attributes[ attr.qName( i ) ] = attr.value( i ); |
255 | 255 | ||
256 | bm->setAttributes( attributes ); | 256 | bm->setAttributes( attributes ); |
257 | 257 | ||
258 | bm->setTagName( qName ); | 258 | bm->setTagName( qName ); |
259 | 259 | ||
260 | m_node->appendChild( bm ); | 260 | m_node->appendChild( bm ); |
261 | m_node = bm; | 261 | m_node = bm; |
262 | 262 | ||
263 | return true; | 263 | return true; |
264 | } | 264 | } |
265 | 265 | ||
266 | bool Handler::endElement( const QString &, const QString &, const QString & ) | 266 | bool Handler::endElement( const QString &, const QString &, const QString & ) |
267 | { | 267 | { |
268 | if ( m_node == m_root ) | 268 | if ( m_node == m_root ) |
269 | return false; | 269 | return false; |
270 | 270 | ||
271 | m_node = m_node->parent(); | 271 | m_node = m_node->parent(); |
272 | return true; | 272 | return true; |
273 | } | 273 | } |
274 | 274 | ||
275 | bool Handler::characters( const QString &ch ) | 275 | bool Handler::characters( const QString &ch ) |
276 | { | 276 | { |
277 | XMLElement *textNode = new XMLElement; | 277 | XMLElement *textNode = new XMLElement; |
278 | textNode->setValue( ch ); | 278 | textNode->setValue( ch ); |
279 | m_node->appendChild( textNode ); | 279 | m_node->appendChild( textNode ); |
280 | return true; | 280 | return true; |
281 | } | 281 | } |
282 | 282 | ||
283 | XMLElement *XMLElement::namedItem( const QString &name ) | 283 | XMLElement *XMLElement::namedItem( const QString &name ) |
284 | { | 284 | { |
285 | XMLElement *e = m_first; | 285 | XMLElement *e = m_first; |
286 | 286 | ||
287 | for (; e; e = e->nextChild() ) | 287 | for (; e; e = e->nextChild() ) |
288 | if ( e->tagName() == name ) | 288 | if ( e->tagName() == name ) |
289 | return e; | 289 | return e; |
290 | 290 | ||
291 | return 0; | 291 | return 0; |
292 | } | 292 | } |
293 | 293 | ||
294 | XMLElement *XMLElement::clone() const | 294 | XMLElement *XMLElement::clone() const |
295 | { | 295 | { |
296 | XMLElement *res = new XMLElement; | 296 | XMLElement *res = new XMLElement; |
297 | 297 | ||
298 | res->setTagName( m_tag ); | 298 | res->setTagName( m_tag ); |
299 | res->setValue( m_value ); | 299 | res->setValue( m_value ); |
300 | res->setAttributes( m_attributes ); | 300 | res->setAttributes( m_attributes ); |
301 | 301 | ||
302 | XMLElement *e = m_first; | 302 | XMLElement *e = m_first; |
303 | for (; e; e = e->m_next ) | 303 | for (; e; e = e->m_next ) |
304 | res->appendChild( e->clone() ); | 304 | res->appendChild( e->clone() ); |
305 | 305 | ||
306 | return res; | 306 | return res; |
307 | } | 307 | } |
308 | 308 | ||
309 | XMLElement *XMLElement::load( const QString &fileName ) | 309 | XMLElement *XMLElement::load( const QString &fileName ) |
310 | { | 310 | { |
311 | QFile f( fileName ); | 311 | QFile f( fileName ); |
312 | if ( !f.open( IO_ReadOnly ) ) | 312 | if ( !f.open( IO_ReadOnly ) ) |
313 | return 0; | 313 | return 0; |
314 | 314 | ||
315 | QTextStream stream( &f ); | 315 | QTextStream stream( &f ); |
316 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 316 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
317 | QXmlInputSource src( stream ); | 317 | QXmlInputSource src( stream ); |
318 | QXmlSimpleReader reader; | 318 | QXmlSimpleReader reader; |
319 | Handler handler; | 319 | Handler handler; |
320 | 320 | ||
321 | 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 ); |
322 | reader.setContentHandler( &handler ); | 322 | reader.setContentHandler( &handler ); |
323 | reader.parse( src ); | 323 | reader.parse( src ); |
324 | 324 | ||
325 | return handler.root();; | 325 | return handler.root();; |
326 | } | 326 | } |
diff --git a/libopie/xmltree.h b/libopie/xmltree.h new file mode 100644 index 0000000..7f1b7b7 --- a/dev/null +++ b/libopie/xmltree.h | |||
@@ -0,0 +1,110 @@ | |||
1 | /* This file is part of the KDE project | ||
2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | |||
20 | |||
21 | #ifndef __bookmarks_h__ | ||
22 | #define __bookmarks_h__ | ||
23 | |||
24 | #include <qstring.h> | ||
25 | #include <qmap.h> | ||
26 | #include <qtextstream.h> | ||
27 | |||
28 | /** | ||
29 | * A small xml lib written by Simon Hausmann. | ||
30 | */ | ||
31 | class XMLElement | ||
32 | { | ||
33 | public: | ||
34 | typedef QMap<QString, QString> AttributeMap; | ||
35 | |||
36 | /** | ||
37 | * The constructor of XMLElement | ||
38 | */ | ||
39 | XMLElement(); | ||
40 | ~XMLElement(); | ||
41 | |||
42 | /** appendChild appends a child to the XMLElement behind the last element. | ||
43 | * The ownership of the child get's transfered to the | ||
44 | * this XMLElement. | ||
45 | * If child is already the child of another parent | ||
46 | * it's get removed from the other parent first. | ||
47 | */ | ||
48 | void appendChild( XMLElement *child ); | ||
49 | |||
50 | /** inserts newChild after refChild. If newChild is the child | ||
51 | * of another parent the child will get removed. | ||
52 | * The ownership of child gets transfered. | ||
53 | * | ||
54 | */ | ||
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | ||
56 | |||
57 | /** same as insertAfter but the element get's inserted before refChild. | ||
58 | * | ||
59 | */ | ||
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | ||
61 | |||
62 | /** removeChild removes the child from the XMLElement. | ||
63 | * The ownership gets dropped. You need to delete the | ||
64 | * child yourself. | ||
65 | */ | ||
66 | void removeChild( XMLElement *child ); | ||
67 | |||
68 | /** parent() returns the parent of this XMLElement | ||
69 | * If there is no parent 0l gets returned | ||
70 | */ | ||
71 | XMLElement *parent() const { return m_parent; } | ||
72 | XMLElement *firstChild() const { return m_first; } | ||
73 | XMLElement *nextChild() const { return m_next; } | ||
74 | XMLElement *prevChild() const { return m_prev; } | ||
75 | XMLElement *lastChild() const { return m_last; } | ||
76 | |||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | ||
78 | QString tagName() const { return m_tag; } | ||
79 | |||
80 | void setValue( const QString &val ) { m_value = val; } | ||
81 | QString value() const { return m_value; } | ||
82 | |||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | ||
84 | AttributeMap attributes() const { return m_attributes; } | ||
85 | AttributeMap &attributes() { return m_attributes; } | ||
86 | |||
87 | void save( QTextStream &stream, uint indent = 0 ); | ||
88 | |||
89 | XMLElement *namedItem( const QString &name ); | ||
90 | |||
91 | XMLElement *clone() const; | ||
92 | |||
93 | static XMLElement *load( const QString &fileName ); | ||
94 | |||
95 | private: | ||
96 | QString m_tag; | ||
97 | QString m_value; | ||
98 | AttributeMap m_attributes; | ||
99 | |||
100 | XMLElement *m_parent; | ||
101 | XMLElement *m_next; | ||
102 | XMLElement *m_prev; | ||
103 | XMLElement *m_first; | ||
104 | XMLElement *m_last; | ||
105 | |||
106 | XMLElement( const XMLElement &rhs ); | ||
107 | XMLElement &operator=( const XMLElement &rhs ); | ||
108 | }; | ||
109 | |||
110 | #endif | ||
diff --git a/libopie2/opiecore/xmltree.cc b/libopie2/opiecore/xmltree.cc index bf93151..d5ce74a 100644 --- a/libopie2/opiecore/xmltree.cc +++ b/libopie2/opiecore/xmltree.cc | |||
@@ -1,326 +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 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 | ||
21 | #include "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 | 27 | namespace |
28 | { | 28 | { |
29 | 29 | ||
30 | /** | 30 | /** |
31 | Encode an attribute value upon saving. | 31 | Encode an attribute value upon saving. |
32 | replaces '"' with """ | 32 | replaces '"' with """ |
33 | replaces '<' with "<" | 33 | replaces '<' with "<" |
34 | replaces '&' with "&" | 34 | replaces '&' with "&" |
35 | replaces '>' with ">" | 35 | replaces '>' with ">" |
36 | */ | 36 | */ |
37 | QString encodeAttr( const QString& str ) | 37 | QString encodeAttr( const QString& str ) |
38 | { | 38 | { |
39 | QString encAttr( str ); // cause of the const parameter | 39 | QString encAttr( str ); // cause of the const parameter |
40 | encAttr = encAttr.replace( QRegExp( "[<]" ), "<" ); | 40 | encAttr = encAttr.replace( QRegExp( "[<]" ), "<" ); |
41 | encAttr = encAttr.replace( QRegExp( "[>]" ), ">" ); | 41 | encAttr = encAttr.replace( QRegExp( "[>]" ), ">" ); |
42 | encAttr = encAttr.replace( QRegExp( "[\"]" ), """ ); | 42 | encAttr = encAttr.replace( QRegExp( "[\"]" ), """ ); |
43 | encAttr = encAttr.replace( QRegExp( "[&]" ), "&" ); | 43 | encAttr = encAttr.replace( QRegExp( "[&]" ), "&" ); |
44 | return encAttr; | 44 | return encAttr; |
45 | } | 45 | } |
46 | 46 | ||
47 | } | 47 | } |
48 | 48 | ||
49 | XMLElement::XMLElement() | 49 | XMLElement::XMLElement() |
50 | : 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 ) |
51 | { | 51 | { |
52 | } | 52 | } |
53 | 53 | ||
54 | XMLElement::~XMLElement() | 54 | XMLElement::~XMLElement() |
55 | { | 55 | { |
56 | XMLElement *n = m_first; | 56 | XMLElement *n = m_first; |
57 | 57 | ||
58 | while ( n ) | 58 | while ( n ) |
59 | { | 59 | { |
60 | XMLElement *tmp = n; | 60 | XMLElement *tmp = n; |
61 | n = n->m_next; | 61 | n = n->m_next; |
62 | delete tmp; | 62 | delete tmp; |
63 | } | 63 | } |
64 | } | 64 | } |
65 | 65 | ||
66 | void XMLElement::appendChild( XMLElement *child ) | 66 | void XMLElement::appendChild( XMLElement *child ) |
67 | { | 67 | { |
68 | if ( child->m_parent ) | 68 | if ( child->m_parent ) |
69 | child->m_parent->removeChild( child ); | 69 | child->m_parent->removeChild( child ); |
70 | 70 | ||
71 | child->m_parent = this; | 71 | child->m_parent = this; |
72 | 72 | ||
73 | if ( m_last ) | 73 | if ( m_last ) |
74 | m_last->m_next = child; | 74 | m_last->m_next = child; |
75 | 75 | ||
76 | child->m_prev = m_last; | 76 | child->m_prev = m_last; |
77 | 77 | ||
78 | if ( !m_first ) | 78 | if ( !m_first ) |
79 | m_first = child; | 79 | m_first = child; |
80 | 80 | ||
81 | m_last = child; | 81 | m_last = child; |
82 | } | 82 | } |
83 | 83 | ||
84 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) | 84 | void XMLElement::insertAfter( XMLElement *newChild, XMLElement *refChild ) |
85 | { | 85 | { |
86 | assert( newChild != refChild ); | 86 | assert( newChild != refChild ); |
87 | 87 | ||
88 | if ( refChild == m_last ) | 88 | if ( refChild == m_last ) |
89 | { | 89 | { |
90 | appendChild( newChild ); | 90 | appendChild( newChild ); |
91 | return; | 91 | return; |
92 | } | 92 | } |
93 | 93 | ||
94 | assert( refChild ); | 94 | assert( refChild ); |
95 | assert( refChild->m_parent ); | 95 | assert( refChild->m_parent ); |
96 | assert( refChild->m_parent == this ); | 96 | assert( refChild->m_parent == this ); |
97 | 97 | ||
98 | if ( newChild->m_parent && newChild != refChild ) | 98 | if ( newChild->m_parent && newChild != refChild ) |
99 | newChild->m_parent->removeChild( newChild ); | 99 | newChild->m_parent->removeChild( newChild ); |
100 | 100 | ||
101 | newChild->m_parent = this; | 101 | newChild->m_parent = this; |
102 | 102 | ||
103 | XMLElement *next = refChild->m_next; | 103 | XMLElement *next = refChild->m_next; |
104 | 104 | ||
105 | refChild->m_next = newChild; | 105 | refChild->m_next = newChild; |
106 | 106 | ||
107 | newChild->m_prev = refChild; | 107 | newChild->m_prev = refChild; |
108 | newChild->m_next = next; | 108 | newChild->m_next = next; |
109 | 109 | ||
110 | if ( next ) | 110 | if ( next ) |
111 | next->m_prev = newChild; | 111 | next->m_prev = newChild; |
112 | } | 112 | } |
113 | 113 | ||
114 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) | 114 | void XMLElement::insertBefore( XMLElement *newChild, XMLElement *refChild ) |
115 | { | 115 | { |
116 | assert( refChild ); | 116 | assert( refChild ); |
117 | assert( refChild->m_parent ); | 117 | assert( refChild->m_parent ); |
118 | assert( refChild->m_parent == this ); | 118 | assert( refChild->m_parent == this ); |
119 | assert( newChild != refChild ); | 119 | assert( newChild != refChild ); |
120 | 120 | ||
121 | if ( newChild->m_parent && newChild != refChild ) | 121 | if ( newChild->m_parent && newChild != refChild ) |
122 | newChild->m_parent->removeChild( newChild ); | 122 | newChild->m_parent->removeChild( newChild ); |
123 | 123 | ||
124 | newChild->m_parent = this; | 124 | newChild->m_parent = this; |
125 | 125 | ||
126 | XMLElement *prev = refChild->m_prev; | 126 | XMLElement *prev = refChild->m_prev; |
127 | 127 | ||
128 | refChild->m_prev = newChild; | 128 | refChild->m_prev = newChild; |
129 | 129 | ||
130 | newChild->m_prev = prev; | 130 | newChild->m_prev = prev; |
131 | newChild->m_next = refChild; | 131 | newChild->m_next = refChild; |
132 | 132 | ||
133 | if ( prev ) | 133 | if ( prev ) |
134 | prev->m_next = newChild; | 134 | prev->m_next = newChild; |
135 | 135 | ||
136 | if ( refChild == m_first ) | 136 | if ( refChild == m_first ) |
137 | m_first = newChild; | 137 | m_first = newChild; |
138 | } | 138 | } |
139 | 139 | ||
140 | void XMLElement::removeChild( XMLElement *child ) | 140 | void XMLElement::removeChild( XMLElement *child ) |
141 | { | 141 | { |
142 | if ( child->m_parent != this ) | 142 | if ( child->m_parent != this ) |
143 | return; | 143 | return; |
144 | 144 | ||
145 | if ( m_first == child ) | 145 | if ( m_first == child ) |
146 | m_first = child->m_next; | 146 | m_first = child->m_next; |
147 | 147 | ||
148 | if ( m_last == child ) | 148 | if ( m_last == child ) |
149 | m_last = child->m_prev; | 149 | m_last = child->m_prev; |
150 | 150 | ||
151 | if ( child->m_prev ) | 151 | if ( child->m_prev ) |
152 | child->m_prev->m_next = child->m_next; | 152 | child->m_prev->m_next = child->m_next; |
153 | 153 | ||
154 | if ( child->m_next ) | 154 | if ( child->m_next ) |
155 | child->m_next->m_prev = child->m_prev; | 155 | child->m_next->m_prev = child->m_prev; |
156 | 156 | ||
157 | child->m_parent = 0; | 157 | child->m_parent = 0; |
158 | child->m_prev = 0; | 158 | child->m_prev = 0; |
159 | child->m_next = 0; | 159 | child->m_next = 0; |
160 | } | 160 | } |
161 | 161 | ||
162 | void XMLElement::save( QTextStream &s, uint indent ) | 162 | void XMLElement::save( QTextStream &s, uint indent ) |
163 | { | 163 | { |
164 | if ( !m_value.isEmpty() ) | 164 | if ( !m_value.isEmpty() ) |
165 | { | 165 | { |
166 | s << encodeAttr( m_value ); | 166 | s << encodeAttr( m_value ); |
167 | return; | 167 | return; |
168 | } | 168 | } |
169 | 169 | ||
170 | for ( uint i = 0; i < indent; ++i ) | 170 | for ( uint i = 0; i < indent; ++i ) |
171 | s << " "; | 171 | s << " "; |
172 | 172 | ||
173 | s << "<" << m_tag; | 173 | s << "<" << m_tag; |
174 | 174 | ||
175 | if ( !m_attributes.isEmpty() ) | 175 | if ( !m_attributes.isEmpty() ) |
176 | { | 176 | { |
177 | s << " "; | 177 | s << " "; |
178 | AttributeMap::ConstIterator it = m_attributes.begin(); | 178 | AttributeMap::ConstIterator it = m_attributes.begin(); |
179 | AttributeMap::ConstIterator end = m_attributes.end(); | 179 | AttributeMap::ConstIterator end = m_attributes.end(); |
180 | for (; it != end; ++it ) | 180 | for (; it != end; ++it ) |
181 | { | 181 | { |
182 | s << it.key() << "=\"" << encodeAttr( it.data() ) << "\""; | 182 | s << it.key() << "=\"" << encodeAttr( it.data() ) << "\""; |
183 | s << " "; | 183 | s << " "; |
184 | } | 184 | } |
185 | } | 185 | } |
186 | 186 | ||
187 | if ( m_last ) | 187 | if ( m_last ) |
188 | { | 188 | { |
189 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) | 189 | if ( ( m_first && !m_first->value().isEmpty() ) || !m_parent ) |
190 | s << ">"; | 190 | s << ">"; |
191 | else | 191 | else |
192 | s << ">" << endl; | 192 | s << ">" << endl; |
193 | 193 | ||
194 | int newIndent = indent; | 194 | int newIndent = indent; |
195 | if ( m_parent ) | 195 | if ( m_parent ) |
196 | newIndent++; | 196 | newIndent++; |
197 | 197 | ||
198 | XMLElement *n = m_first; | 198 | XMLElement *n = m_first; |
199 | while ( n ) | 199 | while ( n ) |
200 | { | 200 | { |
201 | n->save( s, newIndent ); | 201 | n->save( s, newIndent ); |
202 | n = n->nextChild(); | 202 | n = n->nextChild(); |
203 | } | 203 | } |
204 | 204 | ||
205 | if ( m_last && m_last->value().isEmpty() && m_parent ) | 205 | if ( m_last && m_last->value().isEmpty() && m_parent ) |
206 | for ( uint i = 0; i < indent; ++i ) | 206 | for ( uint i = 0; i < indent; ++i ) |
207 | s << " "; | 207 | s << " "; |
208 | 208 | ||
209 | if ( m_parent ) | 209 | if ( m_parent ) |
210 | s << "</" << m_tag << ">" << endl; | 210 | s << "</" << m_tag << ">" << endl; |
211 | } | 211 | } |
212 | else | 212 | else |
213 | s << "/>" << endl; | 213 | s << "/>" << endl; |
214 | } | 214 | } |
215 | 215 | ||
216 | class Handler : public QXmlDefaultHandler | 216 | class Handler : public QXmlDefaultHandler |
217 | { | 217 | { |
218 | public: | 218 | public: |
219 | Handler() : m_node( 0 ), m_root( 0 ) {} | 219 | Handler() : m_node( 0 ), m_root( 0 ) {} |
220 | 220 | ||
221 | XMLElement *root() const { return m_root; } | 221 | XMLElement *root() const { return m_root; } |
222 | 222 | ||
223 | virtual bool startDocument(); | 223 | virtual bool startDocument(); |
224 | virtual bool endDocument(); | 224 | virtual bool endDocument(); |
225 | 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, |
226 | const QXmlAttributes &attr ); | 226 | const QXmlAttributes &attr ); |
227 | 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 ); |
228 | virtual bool characters( const QString &ch ); | 228 | virtual bool characters( const QString &ch ); |
229 | 229 | ||
230 | private: | 230 | private: |
231 | XMLElement *m_node; | 231 | XMLElement *m_node; |
232 | XMLElement *m_root; | 232 | XMLElement *m_root; |
233 | }; | 233 | }; |
234 | 234 | ||
235 | bool Handler::startDocument() | 235 | bool Handler::startDocument() |
236 | { | 236 | { |
237 | m_root = m_node = new XMLElement; | 237 | m_root = m_node = new XMLElement; |
238 | 238 | ||
239 | return true; | 239 | return true; |
240 | } | 240 | } |
241 | 241 | ||
242 | bool Handler::endDocument() | 242 | bool Handler::endDocument() |
243 | { | 243 | { |
244 | return m_root == m_node; | 244 | return m_root == m_node; |
245 | } | 245 | } |
246 | 246 | ||
247 | bool Handler::startElement( const QString &, const QString &, const QString &qName, | 247 | bool Handler::startElement( const QString &, const QString &, const QString &qName, |
248 | const QXmlAttributes &attr ) | 248 | const QXmlAttributes &attr ) |
249 | { | 249 | { |
250 | XMLElement *bm = new XMLElement; | 250 | XMLElement *bm = new XMLElement; |
251 | 251 | ||
252 | XMLElement::AttributeMap attributes; | 252 | XMLElement::AttributeMap attributes; |
253 | for ( int i = 0; i < attr.length(); ++i ) | 253 | for ( int i = 0; i < attr.length(); ++i ) |
254 | attributes[ attr.qName( i ) ] = attr.value( i ); | 254 | attributes[ attr.qName( i ) ] = attr.value( i ); |
255 | 255 | ||
256 | bm->setAttributes( attributes ); | 256 | bm->setAttributes( attributes ); |
257 | 257 | ||
258 | bm->setTagName( qName ); | 258 | bm->setTagName( qName ); |
259 | 259 | ||
260 | m_node->appendChild( bm ); | 260 | m_node->appendChild( bm ); |
261 | m_node = bm; | 261 | m_node = bm; |
262 | 262 | ||
263 | return true; | 263 | return true; |
264 | } | 264 | } |
265 | 265 | ||
266 | bool Handler::endElement( const QString &, const QString &, const QString & ) | 266 | bool Handler::endElement( const QString &, const QString &, const QString & ) |
267 | { | 267 | { |
268 | if ( m_node == m_root ) | 268 | if ( m_node == m_root ) |
269 | return false; | 269 | return false; |
270 | 270 | ||
271 | m_node = m_node->parent(); | 271 | m_node = m_node->parent(); |
272 | return true; | 272 | return true; |
273 | } | 273 | } |
274 | 274 | ||
275 | bool Handler::characters( const QString &ch ) | 275 | bool Handler::characters( const QString &ch ) |
276 | { | 276 | { |
277 | XMLElement *textNode = new XMLElement; | 277 | XMLElement *textNode = new XMLElement; |
278 | textNode->setValue( ch ); | 278 | textNode->setValue( ch ); |
279 | m_node->appendChild( textNode ); | 279 | m_node->appendChild( textNode ); |
280 | return true; | 280 | return true; |
281 | } | 281 | } |
282 | 282 | ||
283 | XMLElement *XMLElement::namedItem( const QString &name ) | 283 | XMLElement *XMLElement::namedItem( const QString &name ) |
284 | { | 284 | { |
285 | XMLElement *e = m_first; | 285 | XMLElement *e = m_first; |
286 | 286 | ||
287 | for (; e; e = e->nextChild() ) | 287 | for (; e; e = e->nextChild() ) |
288 | if ( e->tagName() == name ) | 288 | if ( e->tagName() == name ) |
289 | return e; | 289 | return e; |
290 | 290 | ||
291 | return 0; | 291 | return 0; |
292 | } | 292 | } |
293 | 293 | ||
294 | XMLElement *XMLElement::clone() const | 294 | XMLElement *XMLElement::clone() const |
295 | { | 295 | { |
296 | XMLElement *res = new XMLElement; | 296 | XMLElement *res = new XMLElement; |
297 | 297 | ||
298 | res->setTagName( m_tag ); | 298 | res->setTagName( m_tag ); |
299 | res->setValue( m_value ); | 299 | res->setValue( m_value ); |
300 | res->setAttributes( m_attributes ); | 300 | res->setAttributes( m_attributes ); |
301 | 301 | ||
302 | XMLElement *e = m_first; | 302 | XMLElement *e = m_first; |
303 | for (; e; e = e->m_next ) | 303 | for (; e; e = e->m_next ) |
304 | res->appendChild( e->clone() ); | 304 | res->appendChild( e->clone() ); |
305 | 305 | ||
306 | return res; | 306 | return res; |
307 | } | 307 | } |
308 | 308 | ||
309 | XMLElement *XMLElement::load( const QString &fileName ) | 309 | XMLElement *XMLElement::load( const QString &fileName ) |
310 | { | 310 | { |
311 | QFile f( fileName ); | 311 | QFile f( fileName ); |
312 | if ( !f.open( IO_ReadOnly ) ) | 312 | if ( !f.open( IO_ReadOnly ) ) |
313 | return 0; | 313 | return 0; |
314 | 314 | ||
315 | QTextStream stream( &f ); | 315 | QTextStream stream( &f ); |
316 | stream.setEncoding( QTextStream::UnicodeUTF8 ); | 316 | stream.setEncoding( QTextStream::UnicodeUTF8 ); |
317 | QXmlInputSource src( stream ); | 317 | QXmlInputSource src( stream ); |
318 | QXmlSimpleReader reader; | 318 | QXmlSimpleReader reader; |
319 | Handler handler; | 319 | Handler handler; |
320 | 320 | ||
321 | 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 ); |
322 | reader.setContentHandler( &handler ); | 322 | reader.setContentHandler( &handler ); |
323 | reader.parse( src ); | 323 | reader.parse( src ); |
324 | 324 | ||
325 | return handler.root();; | 325 | return handler.root();; |
326 | } | 326 | } |
diff --git a/libopie2/opiecore/xmltree.h b/libopie2/opiecore/xmltree.h new file mode 100644 index 0000000..7f1b7b7 --- a/dev/null +++ b/libopie2/opiecore/xmltree.h | |||
@@ -0,0 +1,110 @@ | |||
1 | /* This file is part of the KDE project | ||
2 | Copyright (C) 2000,2001 Simon Hausmann <hausmann@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | |||
20 | |||
21 | #ifndef __bookmarks_h__ | ||
22 | #define __bookmarks_h__ | ||
23 | |||
24 | #include <qstring.h> | ||
25 | #include <qmap.h> | ||
26 | #include <qtextstream.h> | ||
27 | |||
28 | /** | ||
29 | * A small xml lib written by Simon Hausmann. | ||
30 | */ | ||
31 | class XMLElement | ||
32 | { | ||
33 | public: | ||
34 | typedef QMap<QString, QString> AttributeMap; | ||
35 | |||
36 | /** | ||
37 | * The constructor of XMLElement | ||
38 | */ | ||
39 | XMLElement(); | ||
40 | ~XMLElement(); | ||
41 | |||
42 | /** appendChild appends a child to the XMLElement behind the last element. | ||
43 | * The ownership of the child get's transfered to the | ||
44 | * this XMLElement. | ||
45 | * If child is already the child of another parent | ||
46 | * it's get removed from the other parent first. | ||
47 | */ | ||
48 | void appendChild( XMLElement *child ); | ||
49 | |||
50 | /** inserts newChild after refChild. If newChild is the child | ||
51 | * of another parent the child will get removed. | ||
52 | * The ownership of child gets transfered. | ||
53 | * | ||
54 | */ | ||
55 | void insertAfter( XMLElement *newChild, XMLElement *refChild ); | ||
56 | |||
57 | /** same as insertAfter but the element get's inserted before refChild. | ||
58 | * | ||
59 | */ | ||
60 | void insertBefore( XMLElement *newChild, XMLElement *refChild ); | ||
61 | |||
62 | /** removeChild removes the child from the XMLElement. | ||
63 | * The ownership gets dropped. You need to delete the | ||
64 | * child yourself. | ||
65 | */ | ||
66 | void removeChild( XMLElement *child ); | ||
67 | |||
68 | /** parent() returns the parent of this XMLElement | ||
69 | * If there is no parent 0l gets returned | ||
70 | */ | ||
71 | XMLElement *parent() const { return m_parent; } | ||
72 | XMLElement *firstChild() const { return m_first; } | ||
73 | XMLElement *nextChild() const { return m_next; } | ||
74 | XMLElement *prevChild() const { return m_prev; } | ||
75 | XMLElement *lastChild() const { return m_last; } | ||
76 | |||
77 | void setTagName( const QString &tag ) { m_tag = tag; } | ||
78 | QString tagName() const { return m_tag; } | ||
79 | |||
80 | void setValue( const QString &val ) { m_value = val; } | ||
81 | QString value() const { return m_value; } | ||
82 | |||
83 | void setAttributes( const AttributeMap &attrs ) { m_attributes = attrs; } | ||
84 | AttributeMap attributes() const { return m_attributes; } | ||
85 | AttributeMap &attributes() { return m_attributes; } | ||
86 | |||
87 | void save( QTextStream &stream, uint indent = 0 ); | ||
88 | |||
89 | XMLElement *namedItem( const QString &name ); | ||
90 | |||
91 | XMLElement *clone() const; | ||
92 | |||
93 | static XMLElement *load( const QString &fileName ); | ||
94 | |||
95 | private: | ||
96 | QString m_tag; | ||
97 | QString m_value; | ||
98 | AttributeMap m_attributes; | ||
99 | |||
100 | XMLElement *m_parent; | ||
101 | XMLElement *m_next; | ||
102 | XMLElement *m_prev; | ||
103 | XMLElement *m_first; | ||
104 | XMLElement *m_last; | ||
105 | |||
106 | XMLElement( const XMLElement &rhs ); | ||
107 | XMLElement &operator=( const XMLElement &rhs ); | ||
108 | }; | ||
109 | |||
110 | #endif | ||