]> git.sur5r.net Git - glabels/blob - libglabels/lgl-xml-category.c
Upload to unstable
[glabels] / libglabels / lgl-xml-category.c
1 /*
2  *  lgl-xml-category.c
3  *  Copyright (C) 2006-2010  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of libglabels.
6  *
7  *  libglabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  libglabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with libglabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "lgl-xml-category.h"
24
25 #include <glib/gi18n.h>
26 #include <glib.h>
27 #include <string.h>
28 #include <libintl.h>
29
30 #include "libglabels-private.h"
31
32 #include "lgl-xml.h"
33
34 /*===========================================*/
35 /* Private types                             */
36 /*===========================================*/
37
38 /*===========================================*/
39 /* Private globals                           */
40 /*===========================================*/
41
42 /*===========================================*/
43 /* Local function prototypes                 */
44 /*===========================================*/
45
46
47 /**
48  * lgl_xml_category_read_categories_from_file:
49  * @utf8_filename:       Filename of categories file (name encoded as UTF-8)
50  *
51  * Read category definitions from a file.
52  *
53  * Returns: a list of #lglCategory structures.
54  *
55  */
56 GList *
57 lgl_xml_category_read_categories_from_file (gchar *utf8_filename)
58 {
59         gchar      *filename;
60         GList      *categories;
61         xmlDocPtr   categories_doc;
62
63         LIBXML_TEST_VERSION;
64
65         filename = g_filename_from_utf8 (utf8_filename, -1, NULL, NULL, NULL);
66         if (!filename)
67         {
68                 g_message ("Utf8 filename conversion error");
69                 return NULL;
70         }
71
72         categories_doc = xmlParseFile (filename);
73         if (!categories_doc)
74         {
75                 g_message ("\"%s\" is not a glabels category file (not XML)",
76                            filename);
77                 return NULL;
78         }
79
80         categories = lgl_xml_category_parse_categories_doc (categories_doc);
81
82         g_free (filename);
83         xmlFreeDoc (categories_doc);
84
85         return categories;
86 }
87
88
89 /**
90  * lgl_xml_category_parse_categories_doc:
91  * @categories_doc:  libxml #xmlDocPtr tree, representing a categories
92  * definition file.
93  *
94  * Read category definitions from a libxml #xmlDocPtr tree.
95  *
96  * Returns: a list of #lglCategory structures.
97  *
98  */
99 GList *
100 lgl_xml_category_parse_categories_doc (xmlDocPtr  categories_doc)
101 {
102         GList       *categories = NULL;
103         xmlNodePtr   root, node;
104         lglCategory *category;
105
106         LIBXML_TEST_VERSION;
107
108         root = xmlDocGetRootElement (categories_doc);
109         if (!root || !root->name)
110         {
111                 g_message ("\"%s\" is not a glabels category file (no root node)",
112                            categories_doc->name);
113                 xmlFreeDoc (categories_doc);
114                 return categories;
115         }
116         if (!lgl_xml_is_node (root, "Glabels-categories"))
117         {
118                 g_message ("\"%s\" is not a glabels category file (wrong root node)",
119                            categories_doc->name);
120                 xmlFreeDoc (categories_doc);
121                 return categories;
122         }
123
124         for (node = root->xmlChildrenNode; node != NULL; node = node->next)
125         {
126
127                 if (lgl_xml_is_node (node, "Category"))
128                 {
129                         category = lgl_xml_category_parse_category_node (node);
130                         categories = g_list_append (categories, category);
131                 }
132                 else
133                 {
134                         if ( !xmlNodeIsText(node) )
135                         {
136                                 if (!lgl_xml_is_node (node, "comment"))
137                                 {
138                                         g_message ("bad node =  \"%s\"",node->name);
139                                 }
140                         }
141                 }
142         }
143
144         return categories;
145 }
146
147
148 /**
149  * lgl_xml_category_parse_category_node:
150  * @category_node:  libxml #xmlNodePtr category node from a #xmlDocPtr tree.
151  *
152  * Read a single category definition from a libxml #xmlNodePtr node.
153  *
154  * Returns: a pointer to a newly created #lglCategory structure.
155  *
156  */
157 lglCategory *
158 lgl_xml_category_parse_category_node (xmlNodePtr category_node)
159 {
160         lglCategory           *category;
161         gchar                 *id, *name;
162
163         LIBXML_TEST_VERSION;
164
165         id   = lgl_xml_get_prop_string (category_node, "id", NULL);
166         name = lgl_xml_get_prop_i18n_string (category_node, "name", NULL);
167
168         category = lgl_category_new (id, name);
169
170         g_free (id);
171         g_free (name);
172
173         return category;
174 }
175
176
177
178 /*
179  * Local Variables:       -- emacs
180  * mode: C                -- emacs
181  * c-basic-offset: 8      -- emacs
182  * tab-width: 8           -- emacs
183  * indent-tabs-mode: nil  -- emacs
184  * End:                   -- emacs
185  */