]> git.sur5r.net Git - glabels/blob - libglabels/xml-category.c
Organized master branch to be top-level directory for glabels, instead of
[glabels] / libglabels / xml-category.c
1 /*
2  *  xml-category.c
3  *  Copyright (C) 2006-2009  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 "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 "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                 g_message ("Utf8 filename conversion error");
68                 return NULL;
69         }
70
71         categories_doc = xmlParseFile (filename);
72         if (!categories_doc) {
73                 g_message ("\"%s\" is not a glabels category file (not XML)",
74                            filename);
75                 return NULL;
76         }
77
78         categories = lgl_xml_category_parse_categories_doc (categories_doc);
79
80         g_free (filename);
81         xmlFreeDoc (categories_doc);
82
83         return categories;
84 }
85
86
87 /**
88  * lgl_xml_category_parse_categories_doc:
89  * @categories_doc:  libxml #xmlDocPtr tree, representing a categories
90  * definition file.
91  *
92  * Read category definitions from a libxml #xmlDocPtr tree.
93  *
94  * Returns: a list of #lglCategory structures.
95  *
96  */
97 GList *
98 lgl_xml_category_parse_categories_doc (xmlDocPtr  categories_doc)
99 {
100         GList       *categories = NULL;
101         xmlNodePtr   root, node;
102         lglCategory *category;
103
104         LIBXML_TEST_VERSION;
105
106         root = xmlDocGetRootElement (categories_doc);
107         if (!root || !root->name) {
108                 g_message ("\"%s\" is not a glabels category file (no root node)",
109                            categories_doc->name);
110                 xmlFreeDoc (categories_doc);
111                 return categories;
112         }
113         if (!lgl_xml_is_node (root, "Glabels-categories")) {
114                 g_message ("\"%s\" is not a glabels category file (wrong root node)",
115                            categories_doc->name);
116                 xmlFreeDoc (categories_doc);
117                 return categories;
118         }
119
120         for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
121
122                 if (lgl_xml_is_node (node, "Category")) {
123                         category = lgl_xml_category_parse_category_node (node);
124                         categories = g_list_append (categories, category);
125                 } else {
126                         if ( !xmlNodeIsText(node) ) {
127                                 if (!lgl_xml_is_node (node, "comment")) {
128                                         g_message ("bad node =  \"%s\"",node->name);
129                                 }
130                         }
131                 }
132         }
133
134         return categories;
135 }
136
137
138 /**
139  * lgl_xml_category_parse_category_node:
140  * @category_node:  libxml #xmlNodePtr category node from a #xmlDocPtr tree.
141  *
142  * Read a single category definition from a libxml #xmlNodePtr node.
143  *
144  * Returns: a pointer to a newly created #lglCategory structure.
145  *
146  */
147 lglCategory *
148 lgl_xml_category_parse_category_node (xmlNodePtr category_node)
149 {
150         lglCategory           *category;
151         gchar                 *id, *name;
152
153         LIBXML_TEST_VERSION;
154
155         id   = lgl_xml_get_prop_string (category_node, "id", NULL);
156         name = lgl_xml_get_prop_i18n_string (category_node, "name", NULL);
157
158         category = lgl_category_new (id, name);
159
160         g_free (id);
161         g_free (name);
162
163         return category;
164 }
165
166
167
168 /*
169  * Local Variables:       -- emacs
170  * mode: C                -- emacs
171  * c-basic-offset: 8      -- emacs
172  * tab-width: 8           -- emacs
173  * indent-tabs-mode: nil  -- emacs
174  * End:                   -- emacs
175  */