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
30 #include "libxml/parser.h"
33 #include "sipe-backend-debug.h"
34 #include "sipe-utils.h"
44 GHashTable
*attributes
;
53 static void callback_start_element(void *user_data
, const xmlChar
*name
, const xmlChar
**attrs
)
55 struct _parser_data
*pd
= user_data
;
58 if (!name
|| pd
->error
) return;
60 node
= g_new0(sipe_xml
, 1);
61 node
->name
= g_strdup((gchar
*)name
);
66 sipe_xml
*current
= pd
->current
;
68 node
->parent
= current
;
70 current
->last
->sibling
= node
;
72 current
->first
= node
;
80 node
->attributes
= g_hash_table_new_full(g_str_hash
,
81 (GEqualFunc
) sipe_strcase_equal
,
83 while ((key
= *attrs
++) != NULL
) {
84 g_hash_table_insert(node
->attributes
,
85 g_strdup((gchar
*) key
),
86 g_strdup((gchar
*) *attrs
++));
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
;
108 if (!pd
->current
|| pd
->error
|| !text
|| !text_len
) return;
112 node
->data
= g_string_append_len(node
->data
, (gchar
*)text
, text_len
);
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
;
126 errmsg
= g_strdup_vprintf(msg
, args
);
129 SIPE_DEBUG_ERROR("error parsing xml string: %s", 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
)) {
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)");
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)");
148 /* *sigh* macro expects at least two parameters */
149 SIPE_DEBUG_WARNING("XML parser error%s", "");
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 */
177 callback_error
, /* error */
178 NULL
, /* fatalError */
179 NULL
, /* getParameterEntity */
180 NULL
, /* cdataBlock */
181 NULL
, /* externalSubset */
182 XML_SAX2_MAGIC
, /* initialized */
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
))
200 sipe_xml_free(pd
->root
);
211 void sipe_xml_free(sipe_xml
*node
)
217 /* we don't support partial tree deletion */
218 if (node
->parent
!= NULL
) {
219 SIPE_DEBUG_ERROR("sipe_xml_free: partial delete attempt! Expect crash or memory leaks...%s", "");
225 sipe_xml
*tmp
= child
->sibling
;
226 child
->parent
= NULL
; /* detach from tree, see above */
227 sipe_xml_free(child
);
233 if (node
->data
) g_string_free(node
->data
, TRUE
);
234 if (node
->attributes
) g_hash_table_destroy(node
->attributes
);
238 static void sipe_xml_stringify_attribute(gpointer key
, gpointer value
,
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
);
266 g_string_append(s
, "/>");
270 gchar
*sipe_xml_stringify(const sipe_xml
*node
)
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
)
284 const sipe_xml
*child
= NULL
;
286 if (!parent
|| !name
) return NULL
;
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
))
297 /* recurse into path */
298 if (child
&& names
[1])
299 child
= sipe_xml_child(child
, names
[1]);
305 const sipe_xml
*sipe_xml_twin(const sipe_xml
*node
)
309 if (!node
) return NULL
;
311 for (sibling
= node
->sibling
; sibling
; sibling
= sibling
->sibling
) {
312 if (sipe_strequal(node
->name
, sibling
->name
))
318 const gchar
*sipe_xml_attribute(const sipe_xml
*node
, const gchar
*attr
)
320 if (!node
|| !attr
|| !node
->attributes
) return NULL
;
321 return(g_hash_table_lookup(node
->attributes
, attr
));
324 gint
sipe_xml_int_attribute(const sipe_xml
*node
, const gchar
*attr
,
327 const gchar
*value
= sipe_xml_attribute(node
, attr
);
328 return(value
? atoi(value
) : fallback
);
331 gchar
*sipe_xml_data(const sipe_xml
*node
)
333 if (!node
|| !node
->data
|| !node
->data
->str
) return NULL
;
334 return g_strdup(node
->data
->str
);