1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008 Christophe Fergeau <teuf@gnome.org>
4 * Based on itdb_plist parser from the gtkpod project.
6 * The code contained in this file is free software; you can redistribute
7 * it and/or modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either version
9 * 2.1 of the License, or (at your option) any later version.
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this code; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/dbus.h>
29 #include "empathy-plist.h"
31 static GValue *empathy_plist_parse_node (xmlNode *a_node);
34 empathy_plist_parse_integer (xmlNode *a_node)
40 str_val = (char *) xmlNodeGetContent (a_node);
41 int_val = strtol (str_val, &end_ptr, 0);
42 if (*end_ptr != '\0') {
48 return tp_g_value_slice_new_int (int_val);
52 empathy_plist_parse_string (xmlNode *a_node)
57 str_val = (char *) xmlNodeGetContent (a_node);
59 value = tp_g_value_slice_new_string (str_val);
67 empathy_plist_parse_real (xmlNode *a_node)
73 str_val = (char *) xmlNodeGetContent (a_node);
74 double_val = g_ascii_strtod (str_val, &end_ptr);
75 if (*end_ptr != '\0') {
81 return tp_g_value_slice_new_double (double_val);
85 empathy_plist_parse_boolean (xmlNode *a_node)
89 if (strcmp ((char *) a_node->name, "true") == 0) {
91 } else if (strcmp ((char *) a_node->name, "false") == 0) {
97 return tp_g_value_slice_new_boolean (bool_val);
101 empathy_plist_parse_data (xmlNode *a_node)
108 str_val = (char *) xmlNodeGetContent (a_node);
109 raw_data = g_base64_decode (str_val, &len);
112 value = tp_g_value_slice_new_bytes (len, raw_data);
120 empathy_plist_parse_one_dict_entry (xmlNode *a_node, GHashTable *dict)
122 xmlNode *cur_node = a_node;
127 (xmlStrcmp (cur_node->name, (xmlChar *) "key") != 0)) {
128 cur_node = cur_node->next;
133 key_name = xmlNodeGetContent (cur_node);
134 cur_node = cur_node->next;
135 while (cur_node && xmlIsBlankNode (cur_node)) {
136 cur_node = cur_node->next;
143 value = empathy_plist_parse_node (cur_node);
145 g_hash_table_insert (dict, g_strdup ((char *) key_name), value);
149 return cur_node->next;
153 empathy_plist_parse_dict (xmlNode *a_node)
155 xmlNode *cur_node = a_node->children;
158 dict = g_hash_table_new_full (g_str_hash, g_str_equal,
159 g_free, (GDestroyNotify) tp_g_value_slice_free);
162 if (xmlIsBlankNode (cur_node)) {
163 cur_node = cur_node->next;
165 cur_node = empathy_plist_parse_one_dict_entry (cur_node, dict);
169 return tp_g_value_slice_new_take_boxed (G_TYPE_HASH_TABLE, dict);
172 typedef GValue *(*ParseCallback) (xmlNode *);
175 const char * const type_name;
176 ParseCallback parser;
179 static const struct Parser parsers[] = { {"integer", empathy_plist_parse_integer},
180 {"real", empathy_plist_parse_real},
181 {"string", empathy_plist_parse_string},
182 {"true", empathy_plist_parse_boolean},
183 {"false", empathy_plist_parse_boolean},
184 {"data", empathy_plist_parse_data},
185 {"dict", empathy_plist_parse_dict},
189 empathy_plist_get_parser_for_type (const xmlChar *type)
193 while (parsers[i].type_name) {
194 if (xmlStrcmp (type, (xmlChar *) parsers[i].type_name) == 0) {
195 if (parsers[i].parser) {
196 return parsers[i].parser;
205 empathy_plist_parse_node (xmlNode *a_node)
207 ParseCallback parser;
209 g_return_val_if_fail (a_node != NULL, NULL);
210 parser = empathy_plist_get_parser_for_type (a_node->name);
212 return parser (a_node);
219 empathy_plist_parse (xmlNode * a_node)
226 if (xmlStrcmp (a_node->name, (xmlChar *) "plist") != 0) {
229 cur_node = a_node->xmlChildrenNode;
230 while (cur_node && (xmlIsBlankNode (cur_node))) {
231 cur_node = cur_node->next;
234 return empathy_plist_parse_node (cur_node);
241 * empathy_plist_parse_from_file:
242 * @filename: file containing XML plist data to parse
244 * Parses the XML plist file. If an error occurs during the parsing,
245 * empathy_plist_parse_from_file() will return NULL.
247 * Returns: NULL on error, a newly allocated
248 * #GValue otherwise. Free it using tp_g_value_slice_free()
251 empathy_plist_parse_from_file (const char *filename)
254 xmlNode *root_element = NULL;
257 doc = xmlReadFile (filename, NULL, 0);
263 root_element = xmlDocGetRootElement (doc);
265 parsed_doc = empathy_plist_parse (root_element);
273 * empathy_plist_parse_from_memory:
274 * @data: memory location containing XML plist data to parse
275 * @len: length in bytes of the string to parse
277 * Parses the XML plist file stored in @data which length is @len
278 * bytes. If an error occurs during the parsing,
279 * empathy_plist_parse_from_memory() will return NULL.
281 * Returns: NULL on error, a newly allocated
282 * #GValue otherwise. Free it using tp_g_value_slice_free()
285 empathy_plist_parse_from_memory (const char *data, gsize len)
288 xmlNode *root_element = NULL;
291 doc = xmlReadMemory (data, len, "noname.xml", NULL, 0);
297 root_element = xmlDocGetRootElement (doc);
299 parsed_doc = empathy_plist_parse (root_element);