2 * (LIBGLABELS) Template library for GLABELS
4 * xml-category.c: category xml module
6 * Copyright (C) 2006 Jim Evins <evins@snaught.com>.
8 * This file is part of the LIBGLABELS library.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 #include "xml-category.h"
29 #include <glib/gi18n.h>
30 #include <glib/gmessages.h>
34 #include "libglabels-private.h"
38 /*===========================================*/
40 /*===========================================*/
42 /*===========================================*/
44 /*===========================================*/
46 /*===========================================*/
47 /* Local function prototypes */
48 /*===========================================*/
52 * lgl_xml_category_read_categories_from_file:
53 * @utf8_filename: Filename of categories file (name encoded as UTF-8)
55 * Read category definitions from a file.
57 * Returns: a list of #lglCategory structures.
61 lgl_xml_category_read_categories_from_file (gchar *utf8_filename)
65 xmlDocPtr categories_doc;
69 filename = g_filename_from_utf8 (utf8_filename, -1, NULL, NULL, NULL);
71 g_message ("Utf8 filename conversion error");
75 categories_doc = xmlParseFile (filename);
76 if (!categories_doc) {
77 g_message ("\"%s\" is not a glabels category file (not XML)",
82 categories = lgl_xml_category_parse_categories_doc (categories_doc);
85 xmlFreeDoc (categories_doc);
92 * lgl_xml_category_parse_categories_doc:
93 * @categories_doc: libxml #xmlDocPtr tree, representing a categories
96 * Read category definitions from a libxml #xmlDocPtr tree.
98 * Returns: a list of #lglCategory structures.
102 lgl_xml_category_parse_categories_doc (xmlDocPtr categories_doc)
104 GList *categories = NULL;
105 xmlNodePtr root, node;
106 lglCategory *category;
110 root = xmlDocGetRootElement (categories_doc);
111 if (!root || !root->name) {
112 g_message ("\"%s\" is not a glabels category file (no root node)",
113 categories_doc->name);
114 xmlFreeDoc (categories_doc);
117 if (!lgl_xml_is_node (root, "Glabels-categories")) {
118 g_message ("\"%s\" is not a glabels category file (wrong root node)",
119 categories_doc->name);
120 xmlFreeDoc (categories_doc);
124 for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
126 if (lgl_xml_is_node (node, "Category")) {
127 category = lgl_xml_category_parse_category_node (node);
128 categories = g_list_append (categories, category);
130 if ( !xmlNodeIsText(node) ) {
131 if (!lgl_xml_is_node (node, "comment")) {
132 g_message ("bad node = \"%s\"",node->name);
143 * lgl_xml_category_parse_category_node:
144 * @category_node: libxml #xmlNodePtr category node from a #xmlDocPtr tree.
146 * Read a single category definition from a libxml #xmlNodePtr node.
148 * Returns: a pointer to a newly created #lglCategory structure.
152 lgl_xml_category_parse_category_node (xmlNodePtr category_node)
154 lglCategory *category;
159 id = lgl_xml_get_prop_string (category_node, "id", NULL);
160 name = lgl_xml_get_prop_i18n_string (category_node, "name", NULL);
162 category = lgl_category_new (id, name);