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
31 #include "libxml/parser.h"
34 #include "sipe-backend.h"
35 #include "sipe-utils.h"
45 GHashTable
*attributes
;
54 static void callback_start_element(void *user_data
, const xmlChar
*name
, const xmlChar
**attrs
)
56 struct _parser_data
*pd
= user_data
;
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
);
72 sipe_xml
*current
= pd
->current
;
74 node
->parent
= current
;
76 current
->last
->sibling
= node
;
78 current
->first
= node
;
86 node
->attributes
= g_hash_table_new_full(g_str_hash
,
87 (GEqualFunc
) sipe_strcase_equal
,
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
++));
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
;
117 if (!pd
->current
|| pd
->error
|| !text
|| !text_len
) return;
121 node
->data
= g_string_append_len(node
->data
, (gchar
*)text
, text_len
);
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
;
135 errmsg
= g_strdup_vprintf(msg
, args
);
138 SIPE_DEBUG_ERROR("error parsing xml string: %s", 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
)) {
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)");
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)");
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 */
186 callback_error
, /* error */
187 NULL
, /* fatalError */
188 NULL
, /* getParameterEntity */
189 NULL
, /* cdataBlock */
190 NULL
, /* externalSubset */
191 XML_SAX2_MAGIC
, /* initialized */
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
))
209 sipe_xml_free(pd
->root
);
220 void sipe_xml_free(sipe_xml
*node
)
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...");
234 sipe_xml
*tmp
= child
->sibling
;
235 child
->parent
= NULL
; /* detach from tree, see above */
236 sipe_xml_free(child
);
242 if (node
->data
) g_string_free(node
->data
, TRUE
);
243 if (node
->attributes
) g_hash_table_destroy(node
->attributes
);
247 static void sipe_xml_stringify_attribute(gpointer key
, gpointer value
,
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
);
275 g_string_append(s
, "/>");
279 gchar
*sipe_xml_stringify(const sipe_xml
*node
)
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
)
293 const sipe_xml
*child
= NULL
;
295 if (!parent
|| !name
) return NULL
;
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
))
306 /* recurse into path */
307 if (child
&& names
[1])
308 child
= sipe_xml_child(child
, names
[1]);
314 const sipe_xml
*sipe_xml_twin(const sipe_xml
*node
)
318 if (!node
) return NULL
;
320 for (sibling
= node
->sibling
; sibling
; sibling
= sibling
->sibling
) {
321 if (sipe_strequal(node
->name
, sibling
->name
))
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
,
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
);