]> git.sur5r.net Git - glabels/blob - libglabels/xml-category.c
Imported Upstream version 2.2.8
[glabels] / libglabels / xml-category.c
1 /*
2  *  (LIBGLABELS) Template library for GLABELS
3  *
4  *  xml-category.c:  category xml module
5  *
6  *  Copyright (C) 2006  Jim Evins <evins@snaught.com>.
7  *
8  *  This file is part of the LIBGLABELS library.
9  *
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.
14  *
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.
19  *
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,
23  *  MA 02111-1307, USA
24  */
25 #include <config.h>
26
27 #include "xml-category.h"
28
29 #include <glib/gi18n.h>
30 #include <glib/gmessages.h>
31 #include <string.h>
32 #include <libintl.h>
33
34 #include "libglabels-private.h"
35
36 #include "xml.h"
37
38 /*===========================================*/
39 /* Private types                             */
40 /*===========================================*/
41
42 /*===========================================*/
43 /* Private globals                           */
44 /*===========================================*/
45
46 /*===========================================*/
47 /* Local function prototypes                 */
48 /*===========================================*/
49
50
51 /**
52  * lgl_xml_category_read_categories_from_file:
53  * @utf8_filename:       Filename of categories file (name encoded as UTF-8)
54  *
55  * Read category definitions from a file.
56  *
57  * Returns: a list of #lglCategory structures.
58  *
59  */
60 GList *
61 lgl_xml_category_read_categories_from_file (gchar *utf8_filename)
62 {
63         gchar      *filename;
64         GList      *categories;
65         xmlDocPtr   categories_doc;
66
67         LIBXML_TEST_VERSION;
68
69         filename = g_filename_from_utf8 (utf8_filename, -1, NULL, NULL, NULL);
70         if (!filename) {
71                 g_message ("Utf8 filename conversion error");
72                 return NULL;
73         }
74
75         categories_doc = xmlParseFile (filename);
76         if (!categories_doc) {
77                 g_message ("\"%s\" is not a glabels category file (not XML)",
78                            filename);
79                 return NULL;
80         }
81
82         categories = lgl_xml_category_parse_categories_doc (categories_doc);
83
84         g_free (filename);
85         xmlFreeDoc (categories_doc);
86
87         return categories;
88 }
89
90
91 /**
92  * lgl_xml_category_parse_categories_doc:
93  * @categories_doc:  libxml #xmlDocPtr tree, representing a categories
94  * definition file.
95  *
96  * Read category definitions from a libxml #xmlDocPtr tree.
97  *
98  * Returns: a list of #lglCategory structures.
99  *
100  */
101 GList *
102 lgl_xml_category_parse_categories_doc (xmlDocPtr  categories_doc)
103 {
104         GList       *categories = NULL;
105         xmlNodePtr   root, node;
106         lglCategory *category;
107
108         LIBXML_TEST_VERSION;
109
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);
115                 return categories;
116         }
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);
121                 return categories;
122         }
123
124         for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
125
126                 if (lgl_xml_is_node (node, "Category")) {
127                         category = lgl_xml_category_parse_category_node (node);
128                         categories = g_list_append (categories, category);
129                 } else {
130                         if ( !xmlNodeIsText(node) ) {
131                                 if (!lgl_xml_is_node (node, "comment")) {
132                                         g_message ("bad node =  \"%s\"",node->name);
133                                 }
134                         }
135                 }
136         }
137
138         return categories;
139 }
140
141
142 /**
143  * lgl_xml_category_parse_category_node:
144  * @category_node:  libxml #xmlNodePtr category node from a #xmlDocPtr tree.
145  *
146  * Read a single category definition from a libxml #xmlNodePtr node.
147  *
148  * Returns: a pointer to a newly created #lglCategory structure.
149  *
150  */
151 lglCategory *
152 lgl_xml_category_parse_category_node (xmlNodePtr category_node)
153 {
154         lglCategory           *category;
155         gchar                 *id, *name;
156
157         LIBXML_TEST_VERSION;
158
159         id   = lgl_xml_get_prop_string (category_node, "id", NULL);
160         name = lgl_xml_get_prop_i18n_string (category_node, "name", NULL);
161
162         category = lgl_category_new (id, name);
163
164         g_free (id);
165         g_free (name);
166
167         return category;
168 }
169