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/telepathy-glib.h>
28 #include "empathy-plist.h"
30 static GValue *empathy_plist_parse_node (xmlNode *a_node);
33 empathy_plist_parse_integer (xmlNode *a_node)
39 str_val = (char *) xmlNodeGetContent (a_node);
40 int_val = strtol (str_val, &end_ptr, 0);
41 if (*end_ptr != '\0') {
47 return tp_g_value_slice_new_int (int_val);
51 empathy_plist_parse_string (xmlNode *a_node)
56 str_val = (char *) xmlNodeGetContent (a_node);
58 value = tp_g_value_slice_new_string (str_val);
66 empathy_plist_parse_real (xmlNode *a_node)
72 str_val = (char *) xmlNodeGetContent (a_node);
73 double_val = g_ascii_strtod (str_val, &end_ptr);
74 if (*end_ptr != '\0') {
80 return tp_g_value_slice_new_double (double_val);
84 empathy_plist_parse_boolean (xmlNode *a_node)
88 if (strcmp ((char *) a_node->name, "true") == 0) {
90 } else if (strcmp ((char *) a_node->name, "false") == 0) {
96 return tp_g_value_slice_new_boolean (bool_val);
100 empathy_plist_parse_data (xmlNode *a_node)
107 str_val = (char *) xmlNodeGetContent (a_node);
108 raw_data = g_base64_decode (str_val, &len);
111 value = tp_g_value_slice_new_bytes (len, raw_data);
119 empathy_plist_parse_one_dict_entry (xmlNode *a_node, GHashTable *dict)
121 xmlNode *cur_node = a_node;
126 (xmlStrcmp (cur_node->name, (xmlChar *) "key") != 0)) {
127 cur_node = cur_node->next;
132 key_name = xmlNodeGetContent (cur_node);
133 cur_node = cur_node->next;
134 while (cur_node && xmlIsBlankNode (cur_node)) {
135 cur_node = cur_node->next;
142 value = empathy_plist_parse_node (cur_node);
144 g_hash_table_insert (dict, g_strdup ((char *) key_name), value);
148 return cur_node->next;
152 empathy_plist_parse_dict (xmlNode *a_node)
154 xmlNode *cur_node = a_node->children;
157 dict = g_hash_table_new_full (g_str_hash, g_str_equal,
158 g_free, (GDestroyNotify) tp_g_value_slice_free);
161 if (xmlIsBlankNode (cur_node)) {
162 cur_node = cur_node->next;
164 cur_node = empathy_plist_parse_one_dict_entry (cur_node, dict);
168 return tp_g_value_slice_new_take_boxed (G_TYPE_HASH_TABLE, dict);
171 typedef GValue *(*ParseCallback) (xmlNode *);
174 const char * const type_name;
175 ParseCallback parser;
178 static const struct Parser parsers[] = { {"integer", empathy_plist_parse_integer},
179 {"real", empathy_plist_parse_real},
180 {"string", empathy_plist_parse_string},
181 {"true", empathy_plist_parse_boolean},
182 {"false", empathy_plist_parse_boolean},
183 {"data", empathy_plist_parse_data},
184 {"dict", empathy_plist_parse_dict},
188 empathy_plist_get_parser_for_type (const xmlChar *type)
192 while (parsers[i].type_name) {
193 if (xmlStrcmp (type, (xmlChar *) parsers[i].type_name) == 0) {
194 if (parsers[i].parser) {
195 return parsers[i].parser;
204 empathy_plist_parse_node (xmlNode *a_node)
206 ParseCallback parser;
208 g_return_val_if_fail (a_node != NULL, NULL);
209 parser = empathy_plist_get_parser_for_type (a_node->name);
211 return parser (a_node);
218 empathy_plist_parse (xmlNode * a_node)
225 if (xmlStrcmp (a_node->name, (xmlChar *) "plist") != 0) {
228 cur_node = a_node->xmlChildrenNode;
229 while (cur_node && (xmlIsBlankNode (cur_node))) {
230 cur_node = cur_node->next;
233 return empathy_plist_parse_node (cur_node);
240 * empathy_plist_parse_from_file:
241 * @filename: file containing XML plist data to parse
243 * Parses the XML plist file. If an error occurs during the parsing,
244 * empathy_plist_parse_from_file() will return NULL.
246 * Returns: NULL on error, a newly allocated
247 * #GValue otherwise. Free it using tp_g_value_slice_free()
250 empathy_plist_parse_from_file (const char *filename)
253 xmlNode *root_element = NULL;
256 doc = xmlReadFile (filename, NULL, 0);
262 root_element = xmlDocGetRootElement (doc);
264 parsed_doc = empathy_plist_parse (root_element);
272 * empathy_plist_parse_from_memory:
273 * @data: memory location containing XML plist data to parse
274 * @len: length in bytes of the string to parse
276 * Parses the XML plist file stored in @data which length is @len
277 * bytes. If an error occurs during the parsing,
278 * empathy_plist_parse_from_memory() will return NULL.
280 * Returns: NULL on error, a newly allocated
281 * #GValue otherwise. Free it using tp_g_value_slice_free()
284 empathy_plist_parse_from_memory (const char *data, gsize len)
287 xmlNode *root_element = NULL;
290 doc = xmlReadMemory (data, len, "noname.xml", NULL, 0);
296 root_element = xmlDocGetRootElement (doc);
298 parsed_doc = empathy_plist_parse (root_element);