6 * Copyright (C) 2010-11 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
31 #include "libxml/parser.h"
32 #include "libxml/c14n.h"
33 #include "libxml/xmlversion.h"
37 #include "sipe-backend.h"
38 #include "sipe-utils.h"
48 GHashTable
*attributes
;
57 static void callback_start_element(void *user_data
, const xmlChar
*name
, const xmlChar
**attrs
)
59 struct _parser_data
*pd
= user_data
;
63 if (!name
|| pd
->error
) return;
65 node
= g_new0(sipe_xml
, 1);
67 if ((tmp
= strchr((char *)name
, ':')) != NULL
) {
68 name
= (xmlChar
*)tmp
+ 1;
70 node
->name
= g_strdup((gchar
*)name
);
75 sipe_xml
*current
= pd
->current
;
77 node
->parent
= current
;
79 current
->last
->sibling
= node
;
81 current
->first
= node
;
89 node
->attributes
= g_hash_table_new_full(g_str_hash
,
90 (GEqualFunc
) sipe_strcase_equal
,
92 while ((key
= *attrs
++) != NULL
) {
93 if ((tmp
= strchr((char *)key
, ':')) != NULL
) {
94 key
= (xmlChar
*)tmp
+ 1;
96 /* libxml2 decodes all entities except &.
97 & is replaced by the equivalent & */
98 g_hash_table_insert(node
->attributes
,
99 g_strdup((gchar
*) key
),
100 replace((gchar
*) *attrs
++, "&", "&"));
107 static void callback_end_element(void *user_data
, const xmlChar
*name
)
109 struct _parser_data
*pd
= user_data
;
111 if (!name
|| !pd
->current
|| pd
->error
) return;
113 if (pd
->current
->parent
)
114 pd
->current
= pd
->current
->parent
;
117 static void callback_characters(void *user_data
, const xmlChar
*text
, int text_len
)
119 struct _parser_data
*pd
= user_data
;
122 if (!pd
->current
|| pd
->error
|| !text
|| !text_len
) return;
126 node
->data
= g_string_append_len(node
->data
, (gchar
*)text
, text_len
);
128 node
->data
= g_string_new_len((gchar
*)text
, text_len
);
131 static void callback_error(void *user_data
, const char *msg
, ...)
133 struct _parser_data
*pd
= user_data
;
140 errmsg
= g_strdup_vprintf(msg
, args
);
143 SIPE_DEBUG_ERROR("error parsing xml string: %s", errmsg
);
147 static void callback_serror(void *user_data
, xmlErrorPtr error
)
149 struct _parser_data
*pd
= user_data
;
151 if (error
&& (error
->level
== XML_ERR_ERROR
||
152 error
->level
== XML_ERR_FATAL
)) {
154 SIPE_DEBUG_ERROR("XML parser error: Domain %i, code %i, level %i: %s",
155 error
->domain
, error
->code
, error
->level
,
156 error
->message
? error
->message
: "(null)");
158 SIPE_DEBUG_WARNING("XML parser error: Domain %i, code %i, level %i: %s",
159 error
->domain
, error
->code
, error
->level
,
160 error
->message
? error
->message
: "(null)");
162 /* *sigh* macro expects at least two parameters */
163 SIPE_DEBUG_WARNING_NOFORMAT("XML parser error");
167 /* API doesn't accept const data structure */
168 static xmlSAXHandler parser
= {
169 NULL
, /* internalSubset */
170 NULL
, /* isStandalone */
171 NULL
, /* hasInternalSubset */
172 NULL
, /* hasExternalSubset */
173 NULL
, /* resolveEntity */
174 NULL
, /* getEntity */
175 NULL
, /* entityDecl */
176 NULL
, /* notationDecl */
177 NULL
, /* attributeDecl */
178 NULL
, /* elementDecl */
179 NULL
, /* unparsedEntityDecl */
180 NULL
, /* setDocumentLocator */
181 NULL
, /* startDocument */
182 NULL
, /* endDocument */
183 callback_start_element
, /* startElement */
184 callback_end_element
, /* endElement */
185 NULL
, /* reference */
186 callback_characters
, /* characters */
187 NULL
, /* ignorableWhitespace */
188 NULL
, /* processingInstruction */
191 callback_error
, /* error */
192 NULL
, /* fatalError */
193 NULL
, /* getParameterEntity */
194 NULL
, /* cdataBlock */
195 NULL
, /* externalSubset */
196 XML_SAX2_MAGIC
, /* initialized */
198 NULL
, /* startElementNs */
199 NULL
, /* endElementNs */
200 callback_serror
, /* serror */
203 sipe_xml
*sipe_xml_parse(const gchar
*string
, gsize length
)
205 sipe_xml
*result
= NULL
;
207 if (string
&& length
) {
208 struct _parser_data
*pd
= g_new0(struct _parser_data
, 1);
210 if (xmlSAXUserParseMemory(&parser
, pd
, string
, length
))
214 sipe_xml_free(pd
->root
);
225 void sipe_xml_free(sipe_xml
*node
)
231 /* we don't support partial tree deletion */
232 if (node
->parent
!= NULL
) {
233 SIPE_DEBUG_ERROR_NOFORMAT("sipe_xml_free: partial delete attempt! Expect crash or memory leaks...");
239 sipe_xml
*tmp
= child
->sibling
;
240 child
->parent
= NULL
; /* detach from tree, see above */
241 sipe_xml_free(child
);
247 if (node
->data
) g_string_free(node
->data
, TRUE
);
248 if (node
->attributes
) g_hash_table_destroy(node
->attributes
);
252 static void sipe_xml_stringify_attribute(gpointer key
, gpointer value
,
255 g_string_append_printf(user_data
, " %s=\"%s\"",
256 (const gchar
*) key
, (const gchar
*) value
);
259 static void sipe_xml_stringify_node(GString
*s
, const sipe_xml
*node
)
261 g_string_append_printf(s
, "<%s", node
->name
);
263 if (node
->attributes
) {
264 g_hash_table_foreach(node
->attributes
,
265 (GHFunc
) sipe_xml_stringify_attribute
,
269 if (node
->data
|| node
->first
) {
270 const sipe_xml
*child
;
272 g_string_append_printf(s
, ">%s",
273 node
->data
? node
->data
->str
: "");
275 for (child
= node
->first
; child
; child
= child
->sibling
)
276 sipe_xml_stringify_node(s
, child
);
278 g_string_append_printf(s
, "</%s>", node
->name
);
280 g_string_append(s
, "/>");
284 gchar
*sipe_xml_stringify(const sipe_xml
*node
)
288 if (!node
) return NULL
;
290 s
= g_string_new("");
291 sipe_xml_stringify_node(s
, node
);
292 return g_string_free(s
, FALSE
);
295 const sipe_xml
*sipe_xml_child(const sipe_xml
*parent
, const gchar
*name
)
298 const sipe_xml
*child
= NULL
;
300 if (!parent
|| !name
) return NULL
;
303 /* 1: trailing path (optional) */
304 names
= g_strsplit(name
, "/", 2);
306 for (child
= parent
->first
; child
; child
= child
->sibling
) {
307 if (sipe_strequal(names
[0], child
->name
))
311 /* recurse into path */
312 if (child
&& names
[1])
313 child
= sipe_xml_child(child
, names
[1]);
319 const sipe_xml
*sipe_xml_twin(const sipe_xml
*node
)
323 if (!node
) return NULL
;
325 for (sibling
= node
->sibling
; sibling
; sibling
= sibling
->sibling
) {
326 if (sipe_strequal(node
->name
, sibling
->name
))
332 const gchar
*sipe_xml_name(const sipe_xml
*node
)
334 return(node
? node
->name
: NULL
);
337 const gchar
*sipe_xml_attribute(const sipe_xml
*node
, const gchar
*attr
)
339 if (!node
|| !attr
|| !node
->attributes
) return NULL
;
340 return(g_hash_table_lookup(node
->attributes
, attr
));
343 guint
sipe_xml_int_attribute(const sipe_xml
*node
, const gchar
*attr
,
346 const gchar
*value
= sipe_xml_attribute(node
, attr
);
347 return(value
? g_ascii_strtoll(value
, NULL
, 10) : fallback
);
350 gchar
*sipe_xml_data(const sipe_xml
*node
)
352 if (!node
|| !node
->data
|| !node
->data
->str
) return NULL
;
353 return g_strdup(node
->data
->str
);
357 * Set to 1 to enable debugging code and then add this line to your code:
359 * sipe_xml_dump(node, NULL);
362 void sipe_xml_dump(const sipe_xml
*node
, const gchar
*path
)
364 const sipe_xml
*child
;
367 new_path
= g_strdup_printf("%s/%s", path
? path
: "", node
->name
);
368 if (node
->attributes
) {
369 GList
*attrs
= g_hash_table_get_keys(node
->attributes
);
370 GString
*buf
= g_string_new("");
371 GList
*entry
= attrs
;
373 g_string_append_printf(buf
, "%s ", (gchar
*)entry
->data
);
376 SIPE_DEBUG_INFO("%s [%s]", new_path
, buf
->str
);
377 g_string_free(buf
, TRUE
);
380 SIPE_DEBUG_INFO_NOFORMAT(new_path
);
382 for (child
= node
->first
; child
; child
= child
->sibling
)
383 sipe_xml_dump(child
, new_path
);
389 * Other XML convenience functions not based on libpurple xmlnode.c
392 gchar
*sipe_xml_exc_c14n(const gchar
*string
)
394 /* Parse string to XML document */
395 xmlDocPtr doc
= xmlReadMemory(string
, strlen(string
), "", NULL
, 0);
402 /* Apply canonicalization */
403 size
= xmlC14NDocDumpMemory(doc
,
405 #if LIBXML_VERSION > 20703
406 /* new API: int mode (a xmlC14NMode) */
407 XML_C14N_EXCLUSIVE_1_0
,
409 /* old API: int exclusive */
418 SIPE_DEBUG_INFO("sipe_xml_exc_c14n:\noriginal: %s\ncanonicalized: %s",
420 canon
= g_strndup((gchar
*) buffer
, size
);
423 SIPE_DEBUG_ERROR("sipe_xml_exc_c14n: failed to canonicalize xml string:\n%s",
427 SIPE_DEBUG_ERROR("sipe_xml_exc_c14n: error parsing xml string:\n%s",