backend: added noformat debug macros
[siplcs.git] / src / core / sipe-xml.c
blob2ec395a85cff22f8e89152492e05b4f3b8a1d78e
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 <time.h>
30 #include "libxml/parser.h"
31 #include "glib.h"
33 #include "sipe-backend-debug.h"
34 #include "sipe-utils.h"
35 #include "sipe-xml.h"
37 struct _sipe_xml {
38 gchar *name;
39 sipe_xml *parent;
40 sipe_xml *sibling;
41 sipe_xml *first;
42 sipe_xml *last;
43 GString *data;
44 GHashTable *attributes;
47 struct _parser_data {
48 sipe_xml *root;
49 sipe_xml *current;
50 gboolean error;
53 static void callback_start_element(void *user_data, const xmlChar *name, const xmlChar **attrs)
55 struct _parser_data *pd = user_data;
56 sipe_xml *node;
58 if (!name || pd->error) return;
60 node = g_new0(sipe_xml, 1);
61 node->name = g_strdup((gchar *)name);
63 if (!pd->root) {
64 pd->root = node;
65 } else {
66 sipe_xml *current = pd->current;
68 node->parent = current;
69 if (current->last) {
70 current->last->sibling = node;
71 } else {
72 current->first = node;
74 current->last = node;
77 if (attrs) {
78 const xmlChar *key;
80 node->attributes = g_hash_table_new_full(g_str_hash,
81 (GEqualFunc) sipe_strcase_equal,
82 g_free, g_free);
83 while ((key = *attrs++) != NULL) {
84 g_hash_table_insert(node->attributes,
85 g_strdup((gchar *) key),
86 g_strdup((gchar *) *attrs++));
90 pd->current = node;
93 static void callback_end_element(void *user_data, const xmlChar *name)
95 struct _parser_data *pd = user_data;
97 if (!name || !pd->current || pd->error) return;
99 if (pd->current->parent)
100 pd->current = pd->current->parent;
103 static void callback_characters(void *user_data, const xmlChar *text, int text_len)
105 struct _parser_data *pd = user_data;
106 sipe_xml *node;
108 if (!pd->current || pd->error || !text || !text_len) return;
110 node = pd->current;
111 if (node->data)
112 node->data = g_string_append_len(node->data, (gchar *)text, text_len);
113 else
114 node->data = g_string_new_len((gchar *)text, text_len);
117 static void callback_error(void *user_data, const char *msg, ...)
119 struct _parser_data *pd = user_data;
120 gchar *errmsg;
121 va_list args;
123 pd->error = TRUE;
125 va_start(args, msg);
126 errmsg = g_strdup_vprintf(msg, args);
127 va_end(args);
129 SIPE_DEBUG_ERROR("error parsing xml string: %s", errmsg);
130 g_free(errmsg);
133 static void callback_serror(void *user_data, xmlErrorPtr error)
135 struct _parser_data *pd = user_data;
137 if (error && (error->level == XML_ERR_ERROR ||
138 error->level == XML_ERR_FATAL)) {
139 pd->error = TRUE;
140 SIPE_DEBUG_ERROR("XML parser error: Domain %i, code %i, level %i: %s",
141 error->domain, error->code, error->level,
142 error->message ? error->message : "(null)");
143 } else if (error) {
144 SIPE_DEBUG_WARNING("XML parser error: Domain %i, code %i, level %i: %s",
145 error->domain, error->code, error->level,
146 error->message ? error->message : "(null)");
147 } else {
148 /* *sigh* macro expects at least two parameters */
149 SIPE_DEBUG_WARNING_NOFORMAT("XML parser error");
153 /* API doesn't accept const data structure */
154 static xmlSAXHandler parser = {
155 NULL, /* internalSubset */
156 NULL, /* isStandalone */
157 NULL, /* hasInternalSubset */
158 NULL, /* hasExternalSubset */
159 NULL, /* resolveEntity */
160 NULL, /* getEntity */
161 NULL, /* entityDecl */
162 NULL, /* notationDecl */
163 NULL, /* attributeDecl */
164 NULL, /* elementDecl */
165 NULL, /* unparsedEntityDecl */
166 NULL, /* setDocumentLocator */
167 NULL, /* startDocument */
168 NULL, /* endDocument */
169 callback_start_element, /* startElement */
170 callback_end_element, /* endElement */
171 NULL, /* reference */
172 callback_characters, /* characters */
173 NULL, /* ignorableWhitespace */
174 NULL, /* processingInstruction */
175 NULL, /* comment */
176 NULL, /* warning */
177 callback_error, /* error */
178 NULL, /* fatalError */
179 NULL, /* getParameterEntity */
180 NULL, /* cdataBlock */
181 NULL, /* externalSubset */
182 XML_SAX2_MAGIC, /* initialized */
183 NULL, /* _private */
184 NULL, /* startElementNs */
185 NULL, /* endElementNs */
186 callback_serror, /* serror */
189 sipe_xml *sipe_xml_parse(const gchar *string, gsize length)
191 sipe_xml *result = NULL;
193 if (string && length) {
194 struct _parser_data *pd = g_new0(struct _parser_data, 1);
196 if (xmlSAXUserParseMemory(&parser, pd, string, length))
197 pd->error = TRUE;
199 if (pd->error) {
200 sipe_xml_free(pd->root);
201 } else {
202 result = pd->root;
205 g_free(pd);
208 return result;
211 void sipe_xml_free(sipe_xml *node)
213 sipe_xml *child;
215 if (!node) return;
217 /* we don't support partial tree deletion */
218 if (node->parent != NULL) {
219 SIPE_DEBUG_ERROR_NOFORMAT("sipe_xml_free: partial delete attempt! Expect crash or memory leaks...");
222 /* free children */
223 child = node->first;
224 while (child) {
225 sipe_xml *tmp = child->sibling;
226 child->parent = NULL; /* detach from tree, see above */
227 sipe_xml_free(child);
228 child = tmp;
231 /* free node */
232 g_free(node->name);
233 if (node->data) g_string_free(node->data, TRUE);
234 if (node->attributes) g_hash_table_destroy(node->attributes);
235 g_free(node);
238 static void sipe_xml_stringify_attribute(gpointer key, gpointer value,
239 gpointer user_data)
241 g_string_append_printf(user_data, " %s=\"%s\"",
242 (const gchar *) key, (const gchar *) value);
245 static void sipe_xml_stringify_node(GString *s, const sipe_xml *node)
247 g_string_append_printf(s, "<%s", node->name);
249 if (node->attributes) {
250 g_hash_table_foreach(node->attributes,
251 (GHFunc) sipe_xml_stringify_attribute,
255 if (node->data || node->first) {
256 const sipe_xml *child;
258 g_string_append_printf(s, ">%s",
259 node->data ? node->data->str : "");
261 for (child = node->first; child; child = child->sibling)
262 sipe_xml_stringify_node(s, child);
264 g_string_append_printf(s, "</%s>", node->name);
265 } else {
266 g_string_append(s, "/>");
270 gchar *sipe_xml_stringify(const sipe_xml *node)
272 GString *s;
274 if (!node) return NULL;
276 s = g_string_new("");
277 sipe_xml_stringify_node(s, node);
278 return g_string_free(s, FALSE);
281 const sipe_xml *sipe_xml_child(const sipe_xml *parent, const gchar *name)
283 gchar **names;
284 const sipe_xml *child = NULL;
286 if (!parent || !name) return NULL;
288 /* 0: child name */
289 /* 1: trailing path (optional) */
290 names = g_strsplit(name, "/", 2);
292 for (child = parent->first; child; child = child->sibling) {
293 if (sipe_strequal(names[0], child->name))
294 break;
297 /* recurse into path */
298 if (child && names[1])
299 child = sipe_xml_child(child, names[1]);
301 g_strfreev(names);
302 return child;
305 const sipe_xml *sipe_xml_twin(const sipe_xml *node)
307 sipe_xml *sibling;
309 if (!node) return NULL;
311 for (sibling = node->sibling; sibling; sibling = sibling->sibling) {
312 if (sipe_strequal(node->name, sibling->name))
313 return sibling;
315 return NULL;
318 const gchar *sipe_xml_name(const sipe_xml *node)
320 return(node ? node->name : NULL);
323 const gchar *sipe_xml_attribute(const sipe_xml *node, const gchar *attr)
325 if (!node || !attr || !node->attributes) return NULL;
326 return(g_hash_table_lookup(node->attributes, attr));
329 gint sipe_xml_int_attribute(const sipe_xml *node, const gchar *attr,
330 gint fallback)
332 const gchar *value = sipe_xml_attribute(node, attr);
333 return(value ? atoi(value) : fallback);
336 gchar *sipe_xml_data(const sipe_xml *node)
338 if (!node || !node->data || !node->data->str) return NULL;
339 return g_strdup(node->data->str);
343 Local Variables:
344 mode: c
345 c-file-style: "bsd"
346 indent-tabs-mode: t
347 tab-width: 8
348 End: