7131
|
1 /**
|
|
2 * @file xmlnode.c XML DOM functions
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
8046
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
8 * source distribution.
|
7131
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 */
|
|
24
|
|
25 /* A lot of this code at least resembles the code in libxode, but since
|
|
26 * libxode uses memory pools that we simply have no need for, I decided to
|
|
27 * write my own stuff. Also, re-writing this lets me be as lightweight
|
|
28 * as I want to be. Thank you libxode for giving me a good starting point */
|
|
29
|
|
30 #include "internal.h"
|
|
31
|
|
32 #include <string.h>
|
|
33 #include <glib.h>
|
|
34
|
|
35 #include "xmlnode.h"
|
|
36
|
|
37 static xmlnode*
|
|
38 new_node(const char *name, NodeType type)
|
|
39 {
|
|
40 xmlnode *node = g_new0(xmlnode, 1);
|
|
41 if(name)
|
|
42 node->name = g_strdup(name);
|
|
43 node->type = type;
|
|
44
|
|
45 return node;
|
|
46 }
|
|
47
|
|
48 xmlnode*
|
|
49 xmlnode_new(const char *name)
|
|
50 {
|
|
51 g_return_val_if_fail(name != NULL, NULL);
|
|
52
|
|
53 return new_node(name, NODE_TYPE_TAG);
|
|
54 }
|
|
55
|
|
56 xmlnode *xmlnode_new_child(xmlnode *parent, const char *name)
|
|
57 {
|
|
58 xmlnode *node;
|
|
59
|
|
60 g_return_val_if_fail(parent != NULL, NULL);
|
|
61 g_return_val_if_fail(name != NULL, NULL);
|
|
62
|
|
63 node = new_node(name, NODE_TYPE_TAG);
|
|
64
|
|
65 xmlnode_insert_child(parent, node);
|
|
66
|
|
67 return node;
|
|
68 }
|
|
69
|
|
70 void
|
|
71 xmlnode_insert_child(xmlnode *parent, xmlnode *child)
|
|
72 {
|
|
73 g_return_if_fail(parent != NULL);
|
|
74 g_return_if_fail(child != NULL);
|
|
75
|
|
76 child->parent = parent;
|
|
77
|
|
78 if(parent->child) {
|
|
79 xmlnode *x;
|
|
80 for(x = parent->child; x->next; x = x->next);
|
|
81 x->next = child;
|
|
82 } else {
|
|
83 parent->child = child;
|
|
84 }
|
|
85 }
|
|
86
|
|
87 void
|
|
88 xmlnode_insert_data(xmlnode *parent, const char *data, size_t size)
|
|
89 {
|
|
90 xmlnode *node;
|
|
91 size_t real_size;
|
|
92
|
|
93 g_return_if_fail(parent != NULL);
|
|
94 g_return_if_fail(data != NULL);
|
|
95 g_return_if_fail(size != 0);
|
|
96
|
|
97 real_size = size == -1 ? strlen(data) : size;
|
|
98
|
|
99 node = new_node(NULL, NODE_TYPE_DATA);
|
|
100
|
|
101 node->data = g_memdup(data, real_size);
|
|
102 node->data_sz = real_size;
|
|
103
|
|
104 xmlnode_insert_child(parent, node);
|
|
105 }
|
|
106
|
|
107 void
|
|
108 xmlnode_remove_attrib(xmlnode *node, const char *attr)
|
|
109 {
|
|
110 xmlnode *attr_node, *sibling = NULL;
|
|
111
|
|
112 g_return_if_fail(node != NULL);
|
|
113 g_return_if_fail(attr != NULL);
|
|
114
|
|
115 for(attr_node = node->child; attr_node; attr_node = attr_node->next)
|
|
116 {
|
|
117 if(attr_node->type == NODE_TYPE_ATTRIB &&
|
|
118 !strcmp(attr_node->name, attr)) {
|
|
119 if(node->child == attr_node) {
|
|
120 node->child = attr_node->next;
|
|
121 } else {
|
|
122 sibling->next = attr_node->next;
|
|
123 }
|
|
124 xmlnode_free(attr_node);
|
|
125 return;
|
|
126 }
|
|
127 sibling = attr_node;
|
|
128 }
|
|
129 }
|
|
130
|
|
131 void
|
|
132 xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value)
|
|
133 {
|
|
134 xmlnode *attrib_node;
|
|
135
|
|
136 g_return_if_fail(node != NULL);
|
|
137 g_return_if_fail(attr != NULL);
|
|
138 g_return_if_fail(value != NULL);
|
|
139
|
|
140 xmlnode_remove_attrib(node, attr);
|
|
141
|
|
142 attrib_node = new_node(attr, NODE_TYPE_ATTRIB);
|
|
143
|
|
144 attrib_node->data = g_strdup(value);
|
|
145
|
|
146 xmlnode_insert_child(node, attrib_node);
|
|
147 }
|
|
148
|
|
149 const char*
|
|
150 xmlnode_get_attrib(xmlnode *node, const char *attr)
|
|
151 {
|
|
152 xmlnode *x;
|
|
153
|
|
154 g_return_val_if_fail(node != NULL, NULL);
|
|
155
|
|
156 for(x = node->child; x; x = x->next) {
|
|
157 if(x->type == NODE_TYPE_ATTRIB && !strcmp(attr, x->name)) {
|
|
158 return x->data;
|
|
159 }
|
|
160 }
|
|
161
|
|
162 return NULL;
|
|
163 }
|
|
164
|
|
165 void xmlnode_free(xmlnode *node)
|
|
166 {
|
|
167 xmlnode *x, *y;
|
|
168
|
|
169 g_return_if_fail(node != NULL);
|
|
170
|
|
171 x = node->child;
|
|
172 while(x) {
|
|
173 y = x->next;
|
|
174 xmlnode_free(x);
|
|
175 x = y;
|
|
176 }
|
|
177
|
|
178 if(node->name)
|
|
179 g_free(node->name);
|
|
180 if(node->data)
|
|
181 g_free(node->data);
|
|
182 g_free(node);
|
|
183 }
|
|
184
|
|
185 xmlnode*
|
|
186 xmlnode_get_child(xmlnode *parent, const char *name)
|
|
187 {
|
|
188 xmlnode *x, *ret = NULL;
|
|
189 char **names;
|
|
190 char *parent_name, *child_name;
|
|
191
|
|
192 g_return_val_if_fail(parent != NULL, NULL);
|
|
193
|
|
194 names = g_strsplit(name, "/", 2);
|
|
195 parent_name = names[0];
|
|
196 child_name = names[1];
|
|
197
|
|
198 for(x = parent->child; x; x = x->next) {
|
|
199 if(x->type == NODE_TYPE_TAG && name && !strcmp(parent_name, x->name)) {
|
|
200 ret = x;
|
|
201 break;
|
|
202 }
|
|
203 }
|
|
204
|
|
205 if(child_name && ret)
|
|
206 ret = xmlnode_get_child(x, child_name);
|
|
207
|
|
208 g_strfreev(names);
|
|
209 return ret;
|
|
210 }
|
|
211
|
|
212 char *
|
|
213 xmlnode_get_data(xmlnode *node)
|
|
214 {
|
|
215 GString *str = NULL;
|
|
216 char *ret = NULL;
|
|
217 xmlnode *c;
|
|
218
|
|
219 g_return_val_if_fail(node != NULL, NULL);
|
|
220
|
|
221
|
|
222 for(c = node->child; c; c = c->next) {
|
|
223 if(c->type == NODE_TYPE_DATA) {
|
|
224 if(!str)
|
|
225 str = g_string_new("");
|
|
226 str = g_string_append_len(str, c->data, c->data_sz);
|
|
227 }
|
|
228 }
|
|
229
|
|
230 if(str) {
|
|
231 ret = str->str;
|
|
232 g_string_free(str, FALSE);
|
|
233 }
|
|
234
|
|
235 return ret;
|
|
236 }
|
|
237
|
7642
|
238 char *xmlnode_to_str(xmlnode *node, int *len)
|
7131
|
239 {
|
|
240 char *ret;
|
|
241 GString *text = g_string_new("");
|
|
242 xmlnode *c;
|
|
243 char *node_name, *esc, *esc2;
|
|
244 gboolean need_end = FALSE;
|
|
245
|
|
246 node_name = g_markup_escape_text(node->name, -1);
|
|
247 g_string_append_printf(text, "<%s", node_name);
|
|
248
|
|
249
|
|
250 for(c = node->child; c; c = c->next)
|
|
251 {
|
|
252 if(c->type == NODE_TYPE_ATTRIB) {
|
|
253 esc = g_markup_escape_text(c->name, -1);
|
|
254 esc2 = g_markup_escape_text(c->data, -1);
|
|
255 g_string_append_printf(text, " %s='%s'", esc, esc2);
|
|
256 g_free(esc);
|
|
257 g_free(esc2);
|
|
258 } else if(c->type == NODE_TYPE_TAG || c->type == NODE_TYPE_DATA) {
|
|
259 need_end = TRUE;
|
|
260 }
|
|
261 }
|
|
262
|
|
263 if(need_end) {
|
|
264 text = g_string_append_c(text, '>');
|
|
265
|
|
266 for(c = node->child; c; c = c->next)
|
|
267 {
|
|
268 if(c->type == NODE_TYPE_TAG) {
|
7642
|
269 int esc_len;
|
|
270 esc = xmlnode_to_str(c, &esc_len);
|
|
271 text = g_string_append_len(text, esc, esc_len);
|
7131
|
272 g_free(esc);
|
|
273 } else if(c->type == NODE_TYPE_DATA) {
|
|
274 esc = g_markup_escape_text(c->data, c->data_sz);
|
7642
|
275 text = g_string_append(text, esc);
|
7131
|
276 g_free(esc);
|
|
277 }
|
|
278 }
|
|
279
|
|
280 g_string_append_printf(text, "</%s>", node_name);
|
|
281 } else {
|
|
282 g_string_append_printf(text, "/>");
|
|
283 }
|
|
284
|
|
285 g_free(node_name);
|
|
286
|
|
287 ret = text->str;
|
7642
|
288 if(len)
|
|
289 *len = text->len;
|
7131
|
290 g_string_free(text, FALSE);
|
|
291 return ret;
|
|
292 }
|
|
293
|
|
294 struct _xmlnode_parser_data {
|
|
295 xmlnode *current;
|
|
296 };
|
|
297
|
|
298 static void
|
|
299 xmlnode_parser_element_start(GMarkupParseContext *context,
|
|
300 const char *element_name, const char **attrib_names,
|
|
301 const char **attrib_values, gpointer user_data, GError **error)
|
|
302 {
|
|
303 struct _xmlnode_parser_data *xpd = user_data;
|
|
304 xmlnode *node;
|
|
305 int i;
|
|
306
|
|
307 if(!element_name) {
|
|
308 return;
|
|
309 } else {
|
|
310 if(xpd->current)
|
|
311 node = xmlnode_new_child(xpd->current, element_name);
|
|
312 else
|
|
313 node = xmlnode_new(element_name);
|
|
314
|
|
315 for(i=0; attrib_names[i]; i++)
|
|
316 xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]);
|
|
317
|
|
318 xpd->current = node;
|
|
319 }
|
|
320 }
|
|
321
|
|
322 static void
|
|
323 xmlnode_parser_element_end(GMarkupParseContext *context,
|
|
324 const char *element_name, gpointer user_data, GError **error)
|
|
325 {
|
|
326 struct _xmlnode_parser_data *xpd = user_data;
|
|
327
|
|
328 if(!element_name || !xpd->current)
|
|
329 return;
|
|
330
|
|
331 if(xpd->current->parent) {
|
|
332 if(!strcmp(xpd->current->name, element_name))
|
|
333 xpd->current = xpd->current->parent;
|
|
334 }
|
|
335 }
|
|
336
|
|
337 static void
|
|
338 xmlnode_parser_element_text(GMarkupParseContext *context, const char *text,
|
|
339 gsize text_len, gpointer user_data, GError **error)
|
|
340 {
|
|
341 struct _xmlnode_parser_data *xpd = user_data;
|
|
342
|
|
343 if(!xpd->current)
|
|
344 return;
|
|
345
|
|
346 if(!text || !text_len)
|
|
347 return;
|
|
348
|
|
349 xmlnode_insert_data(xpd->current, text, text_len);
|
|
350 }
|
|
351
|
|
352 static GMarkupParser xmlnode_parser = {
|
|
353 xmlnode_parser_element_start,
|
|
354 xmlnode_parser_element_end,
|
|
355 xmlnode_parser_element_text,
|
|
356 NULL,
|
|
357 NULL
|
|
358 };
|
|
359
|
|
360
|
|
361 xmlnode *xmlnode_from_str(const char *str, size_t size)
|
|
362 {
|
|
363 struct _xmlnode_parser_data *xpd = g_new0(struct _xmlnode_parser_data, 1);
|
|
364 xmlnode *ret;
|
|
365 GMarkupParseContext *context;
|
|
366 size_t real_size = size == -1 ? strlen(str) : size;
|
|
367
|
|
368 context = g_markup_parse_context_new(&xmlnode_parser, 0, xpd, NULL);
|
|
369
|
|
370 if(!g_markup_parse_context_parse(context, str, real_size, NULL)) {
|
|
371 while(xpd->current && xpd->current->parent)
|
|
372 xpd->current = xpd->current->parent;
|
|
373 if(xpd->current)
|
|
374 xmlnode_free(xpd->current);
|
|
375 xpd->current = NULL;
|
|
376 }
|
|
377 g_markup_parse_context_free(context);
|
|
378
|
|
379 ret = xpd->current;
|
|
380 g_free(xpd);
|
|
381 return ret;
|
|
382 }
|