updated localization files
[siplcs.git] / src / core / sipe-xml.c
bloba966ae068123133a4bee6685d63aba30af9032ae
1 /**
2 * @file sipe-xml.c
4 * pidgin-sipe
6 * Copyright (C) 2010 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This code is loosely based on libpurple xmlnode.c
27 #include <stdarg.h>
28 #include <string.h>
29 #include <time.h>
31 #include "libxml/parser.h"
32 #include "glib.h"
34 #include "sipe-backend.h"
35 #include "sipe-utils.h"
36 #include "sipe-xml.h"
38 struct _sipe_xml {
39 gchar *name;
40 sipe_xml *parent;
41 sipe_xml *sibling;
42 sipe_xml *first;
43 sipe_xml *last;
44 GString *data;
45 GHashTable *attributes;
48 struct _parser_data {
49 sipe_xml *root;
50 sipe_xml *current;
51 gboolean error;
54 static void callback_start_element(void *user_data, const xmlChar *name, const xmlChar **attrs)
56 struct _parser_data *pd = user_data;
57 const char *tmp;
58 sipe_xml *node;
60 if (!name || pd->error) return;
62 node = g_new0(sipe_xml, 1);
64 if ((tmp = strchr((char *)name, ':')) != NULL) {
65 name = (xmlChar *)tmp + 1;
67 node->name = g_strdup((gchar *)name);
69 if (!pd->root) {
70 pd->root = node;
71 } else {
72 sipe_xml *current = pd->current;
74 node->parent = current;
75 if (current->last) {
76 current->last->sibling = node;
77 } else {
78 current->first = node;
80 current->last = node;
83 if (attrs) {
84 const xmlChar *key;
86 node->attributes = g_hash_table_new_full(g_str_hash,
87 (GEqualFunc) sipe_strcase_equal,
88 g_free, g_free);
89 while ((key = *attrs++) != NULL) {
90 if ((tmp = strchr((char *)key, ':')) != NULL) {
91 key = (xmlChar *)tmp + 1;
93 g_hash_table_insert(node->attributes,
94 g_strdup((gchar *) key),
95 g_strdup((gchar *) *attrs++));
99 pd->current = node;
102 static void callback_end_element(void *user_data, const xmlChar *name)
104 struct _parser_data *pd = user_data;
106 if (!name || !pd->current || pd->error) return;
108 if (pd->current->parent)
109 pd->current = pd->current->parent;
112 static void callback_characters(void *user_data, const xmlChar *text, int text_len)
114 struct _parser_data *pd = user_data;
115 sipe_xml *node;
117 if (!pd->current || pd->error || !text || !text_len) return;
119 node = pd->current;
120 if (node->data)
121 node->data = g_string_append_len(node->data, (gchar *)text, text_len);
122 else
123 node->data = g_string_new_len((gchar *)text, text_len);
126 static void callback_error(void *user_data, const char *msg, ...)
128 struct _parser_data *pd = user_data;
129 gchar *errmsg;
130 va_list args;
132 pd->error = TRUE;
134 va_start(args, msg);
135 errmsg = g_strdup_vprintf(msg, args);
136 va_end(args);
138 SIPE_DEBUG_ERROR("error parsing xml string: %s", errmsg);
139 g_free(errmsg);
142 static void callback_serror(void *user_data, xmlErrorPtr error)
144 struct _parser_data *pd = user_data;
146 if (error && (error->level == XML_ERR_ERROR ||
147 error->level == XML_ERR_FATAL)) {
148 pd->error = TRUE;
149 SIPE_DEBUG_ERROR("XML parser error: Domain %i, code %i, level %i: %s",
150 error->domain, error->code, error->level,
151 error->message ? error->message : "(null)");
152 } else if (error) {
153 SIPE_DEBUG_WARNING("XML parser error: Domain %i, code %i, level %i: %s",
154 error->domain, error->code, error->level,
155 error->message ? error->message : "(null)");
156 } else {
157 /* *sigh* macro expects at least two parameters */
158 SIPE_DEBUG_WARNING_NOFORMAT("XML parser error");
162 /* API doesn't accept const data structure */
163 static xmlSAXHandler parser = {
164 NULL, /* internalSubset */
165 NULL, /* isStandalone */
166 NULL, /* hasInternalSubset */
167 NULL, /* hasExternalSubset */
168 NULL, /* resolveEntity */
169 NULL, /* getEntity */
170 NULL, /* entityDecl */
171 NULL, /* notationDecl */
172 NULL, /* attributeDecl */
173 NULL, /* elementDecl */
174 NULL, /* unparsedEntityDecl */
175 NULL, /* setDocumentLocator */
176 NULL, /* startDocument */
177 NULL, /* endDocument */
178 callback_start_element, /* startElement */
179 callback_end_element, /* endElement */
180 NULL, /* reference */
181 callback_characters, /* characters */
182 NULL, /* ignorableWhitespace */
183 NULL, /* processingInstruction */
184 NULL, /* comment */
185 NULL, /* warning */
186 callback_error, /* error */
187 NULL, /* fatalError */
188 NULL, /* getParameterEntity */
189 NULL, /* cdataBlock */
190 NULL, /* externalSubset */
191 XML_SAX2_MAGIC, /* initialized */
192 NULL, /* _private */
193 NULL, /* startElementNs */
194 NULL, /* endElementNs */
195 callback_serror, /* serror */
198 sipe_xml *sipe_xml_parse(const gchar *string, gsize length)
200 sipe_xml *result = NULL;
202 if (string && length) {
203 struct _parser_data *pd = g_new0(struct _parser_data, 1);
205 if (xmlSAXUserParseMemory(&parser, pd, string, length))
206 pd->error = TRUE;
208 if (pd->error) {
209 sipe_xml_free(pd->root);
210 } else {
211 result = pd->root;
214 g_free(pd);
217 return result;
220 void sipe_xml_free(sipe_xml *node)
222 sipe_xml *child;
224 if (!node) return;
226 /* we don't support partial tree deletion */
227 if (node->parent != NULL) {
228 SIPE_DEBUG_ERROR_NOFORMAT("sipe_xml_free: partial delete attempt! Expect crash or memory leaks...");
231 /* free children */
232 child = node->first;
233 while (child) {
234 sipe_xml *tmp = child->sibling;
235 child->parent = NULL; /* detach from tree, see above */
236 sipe_xml_free(child);
237 child = tmp;
240 /* free node */
241 g_free(node->name);
242 if (node->data) g_string_free(node->data, TRUE);
243 if (node->attributes) g_hash_table_destroy(node->attributes);
244 g_free(node);
247 static void sipe_xml_stringify_attribute(gpointer key, gpointer value,
248 gpointer user_data)
250 g_string_append_printf(user_data, " %s=\"%s\"",
251 (const gchar *) key, (const gchar *) value);
254 static void sipe_xml_stringify_node(GString *s, const sipe_xml *node)
256 g_string_append_printf(s, "<%s", node->name);
258 if (node->attributes) {
259 g_hash_table_foreach(node->attributes,
260 (GHFunc) sipe_xml_stringify_attribute,
264 if (node->data || node->first) {
265 const sipe_xml *child;
267 g_string_append_printf(s, ">%s",
268 node->data ? node->data->str : "");
270 for (child = node->first; child; child = child->sibling)
271 sipe_xml_stringify_node(s, child);
273 g_string_append_printf(s, "</%s>", node->name);
274 } else {
275 g_string_append(s, "/>");
279 gchar *sipe_xml_stringify(const sipe_xml *node)
281 GString *s;
283 if (!node) return NULL;
285 s = g_string_new("");
286 sipe_xml_stringify_node(s, node);
287 return g_string_free(s, FALSE);
290 const sipe_xml *sipe_xml_child(const sipe_xml *parent, const gchar *name)
292 gchar **names;
293 const sipe_xml *child = NULL;
295 if (!parent || !name) return NULL;
297 /* 0: child name */
298 /* 1: trailing path (optional) */
299 names = g_strsplit(name, "/", 2);
301 for (child = parent->first; child; child = child->sibling) {
302 if (sipe_strequal(names[0], child->name))
303 break;
306 /* recurse into path */
307 if (child && names[1])
308 child = sipe_xml_child(child, names[1]);
310 g_strfreev(names);
311 return child;
314 const sipe_xml *sipe_xml_twin(const sipe_xml *node)
316 sipe_xml *sibling;
318 if (!node) return NULL;
320 for (sibling = node->sibling; sibling; sibling = sibling->sibling) {
321 if (sipe_strequal(node->name, sibling->name))
322 return sibling;
324 return NULL;
327 const gchar *sipe_xml_name(const sipe_xml *node)
329 return(node ? node->name : NULL);
332 const gchar *sipe_xml_attribute(const sipe_xml *node, const gchar *attr)
334 if (!node || !attr || !node->attributes) return NULL;
335 return(g_hash_table_lookup(node->attributes, attr));
338 guint sipe_xml_int_attribute(const sipe_xml *node, const gchar *attr,
339 guint fallback)
341 const gchar *value = sipe_xml_attribute(node, attr);
342 return(value ? g_ascii_strtoll(value, NULL, 10) : fallback);
345 gchar *sipe_xml_data(const sipe_xml *node)
347 if (!node || !node->data || !node->data->str) return NULL;
348 return g_strdup(node->data->str);
352 Local Variables:
353 mode: c
354 c-file-style: "bsd"
355 indent-tabs-mode: t
356 tab-width: 8
357 End: