]> git.sur5r.net Git - glabels/blob - libglabels/xml-paper.c
Imported Upstream version 2.2.8
[glabels] / libglabels / xml-paper.c
1 /*
2  *  (LIBGLABELS) Template library for GLABELS
3  *
4  *  xml-paper.c:  paper xml module
5  *
6  *  Copyright (C) 2003, 2004  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-paper.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_paper_read_papers_from_file:
53  * @utf8_filename:       Filename of papers file (name encoded as UTF-8)
54  *
55  * Read paper definitions from a file.
56  *
57  * Returns: a list of #lglPaper structures.
58  *
59  */
60 GList *
61 lgl_xml_paper_read_papers_from_file (gchar *utf8_filename)
62 {
63         gchar      *filename;
64         GList      *papers;
65         xmlDocPtr   papers_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         papers_doc = xmlParseFile (filename);
76         if (!papers_doc) {
77                 g_message ("\"%s\" is not a glabels paper file (not XML)",
78                            filename);
79                 return NULL;
80         }
81
82         papers = lgl_xml_paper_parse_papers_doc (papers_doc);
83
84         g_free (filename);
85         xmlFreeDoc (papers_doc);
86
87         return papers;
88 }
89
90
91 /**
92  * lgl_xml_paper_parse_papers_doc:
93  * @papers_doc:  libxml #xmlDocPtr tree, representing a papers definition file.
94  *
95  * Read paper definitions from a libxml #xmlDocPtr tree.
96  *
97  * Returns: a list of #lglPaper structures.
98  *
99  */
100 GList *
101 lgl_xml_paper_parse_papers_doc (xmlDocPtr  papers_doc)
102 {
103         GList      *papers = NULL;
104         xmlNodePtr  root, node;
105         lglPaper   *paper;
106
107         LIBXML_TEST_VERSION;
108
109         root = xmlDocGetRootElement (papers_doc);
110         if (!root || !root->name) {
111                 g_message ("\"%s\" is not a glabels paper file (no root node)",
112                            papers_doc->name);
113                 xmlFreeDoc (papers_doc);
114                 return papers;
115         }
116         if (!lgl_xml_is_node (root, "Glabels-paper-sizes")) {
117                 g_message ("\"%s\" is not a glabels paper file (wrong root node)",
118                            papers_doc->name);
119                 xmlFreeDoc (papers_doc);
120                 return papers;
121         }
122
123         for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
124
125                 if (lgl_xml_is_node (node, "Paper-size")) {
126                         paper = lgl_xml_paper_parse_paper_node (node);
127                         papers = g_list_append (papers, paper);
128                 } else {
129                         if ( !xmlNodeIsText(node) ) {
130                                 if (!lgl_xml_is_node (node, "comment")) {
131                                         g_message ("bad node =  \"%s\"",node->name);
132                                 }
133                         }
134                 }
135         }
136
137         return papers;
138 }
139
140
141 /**
142  * lgl_xml_paper_parse_paper_node:
143  * @paper_node:  libxml #xmlNodePtr paper node from a #xmlDocPtr tree.
144  *
145  * Read a single paper definition from a libxml #xmlNodePtr node.
146  *
147  * Returns: a pointer to a newly created #lglPaper structure.
148  *
149  */
150 lglPaper *
151 lgl_xml_paper_parse_paper_node (xmlNodePtr paper_node)
152 {
153         lglPaper              *paper;
154         gchar                 *id, *name, *pwg_size;
155         gdouble                width, height;
156
157         LIBXML_TEST_VERSION;
158
159         id   = lgl_xml_get_prop_string (paper_node, "id", NULL);
160
161         name = lgl_xml_get_prop_i18n_string (paper_node, "name", NULL);
162
163         width  = lgl_xml_get_prop_length (paper_node, "width", 0);
164         height = lgl_xml_get_prop_length (paper_node, "height", 0);
165
166         pwg_size = lgl_xml_get_prop_string (paper_node, "pwg_size", NULL);
167
168         paper = lgl_paper_new (id, name, width, height, pwg_size);
169
170         g_free (id);
171         g_free (name);
172         g_free (pwg_size);
173
174         return paper;
175 }
176