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);
173 empathy_plist_parse_array (xmlNode *a_node)
175 xmlNode *cur_node = a_node->children;
178 array = g_value_array_new (4);
183 cur_value = empathy_plist_parse_node (cur_node);
185 g_value_array_append (array, cur_value);
186 tp_g_value_slice_free (cur_value);
189 /* When an array contains an element enclosed in "unknown" tags (ie
190 * non-type ones), we silently skip them since early
191 * SysInfoExtended files used to have <key> values enclosed within
194 cur_node = cur_node->next;
197 return tp_g_value_slice_new_take_boxed (G_TYPE_VALUE_ARRAY, array);
200 typedef GValue *(*ParseCallback) (xmlNode *);
203 const char * const type_name;
204 ParseCallback parser;
207 static const struct Parser parsers[] = { {"integer", empathy_plist_parse_integer},
208 {"real", empathy_plist_parse_real},
209 {"string", empathy_plist_parse_string},
210 {"true", empathy_plist_parse_boolean},
211 {"false", empathy_plist_parse_boolean},
212 {"data", empathy_plist_parse_data},
213 {"dict", empathy_plist_parse_dict},
214 {"array", empathy_plist_parse_array},
218 empathy_plist_get_parser_for_type (const xmlChar *type)
222 while (parsers[i].type_name) {
223 if (xmlStrcmp (type, (xmlChar *) parsers[i].type_name) == 0) {
224 if (parsers[i].parser) {
225 return parsers[i].parser;
234 empathy_plist_parse_node (xmlNode *a_node)
236 ParseCallback parser;
238 g_return_val_if_fail (a_node != NULL, NULL);
239 parser = empathy_plist_get_parser_for_type (a_node->name);
241 return parser (a_node);
248 empathy_plist_parse (xmlNode * a_node)
255 if (xmlStrcmp (a_node->name, (xmlChar *) "plist") != 0) {
258 cur_node = a_node->xmlChildrenNode;
259 while (cur_node && (xmlIsBlankNode (cur_node))) {
260 cur_node = cur_node->next;
263 return empathy_plist_parse_node (cur_node);
270 * empathy_plist_parse_from_file:
271 * @filename: file containing XML plist data to parse
273 * Parses the XML plist file. If an error occurs during the parsing,
274 * empathy_plist_parse_from_file() will return NULL.
276 * Returns: NULL on error, a newly allocated
277 * #GValue otherwise. Free it using tp_g_value_slice_free()
280 empathy_plist_parse_from_file (const char *filename)
283 xmlNode *root_element = NULL;
286 doc = xmlReadFile (filename, NULL, 0);
292 root_element = xmlDocGetRootElement (doc);
294 parsed_doc = empathy_plist_parse (root_element);
302 * empathy_plist_parse_from_memory:
303 * @data: memory location containing XML plist data to parse
304 * @len: length in bytes of the string to parse
306 * Parses the XML plist file stored in @data which length is @len
307 * bytes. If an error occurs during the parsing,
308 * empathy_plist_parse_from_memory() will return NULL.
310 * Returns: NULL on error, a newly allocated
311 * #GValue otherwise. Free it using tp_g_value_slice_free()
314 empathy_plist_parse_from_memory (const char *data, gsize len)
317 xmlNode *root_element = NULL;
320 doc = xmlReadMemory (data, len, "noname.xml", NULL, 0);
326 root_element = xmlDocGetRootElement (doc);
328 parsed_doc = empathy_plist_parse (root_element);