]> git.sur5r.net Git - glabels/blob - glabels2/libglabels/xml-paper.c
7bf16288853a1e289fc55e6ad5a98a431d90f63d
[glabels] / glabels2 / 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
26 #include "libglabels-private.h"
27
28 #include <string.h>
29 #include <libintl.h>
30
31 #include "xml.h"
32 #include "xml-paper.h"
33
34 /*===========================================*/
35 /* Private types                             */
36 /*===========================================*/
37
38 /*===========================================*/
39 /* Private globals                           */
40 /*===========================================*/
41
42 /*===========================================*/
43 /* Local function prototypes                 */
44 /*===========================================*/
45
46 \f
47 /*****************************************************************************/
48 /* Read papers from paper file.                                              */
49 /*****************************************************************************/
50 GList *
51 gl_xml_paper_read_papers_from_file (gchar *utf8_filename)
52 {
53         gchar      *filename;
54         GList      *papers;
55         xmlDocPtr   papers_doc;
56
57         LIBXML_TEST_VERSION;
58
59         filename = g_filename_from_utf8 (utf8_filename, -1, NULL, NULL, NULL);
60         if (!filename) {
61                 g_warning ("Utf8 filename conversion error");
62                 return NULL;
63         }
64
65         papers_doc = xmlParseFile (filename);
66         if (!papers_doc) {
67                 g_warning ("\"%s\" is not a glabels paper file (not XML)",
68                            filename);
69                 return NULL;
70         }
71
72         papers = gl_xml_paper_parse_papers_doc (papers_doc);
73
74         g_free (filename);
75         xmlFreeDoc (papers_doc);
76
77         return papers;
78 }
79
80 /*****************************************************************************/
81 /* Read papers from paper xml doc tree.                                      */
82 /*****************************************************************************/
83 GList *
84 gl_xml_paper_parse_papers_doc (xmlDocPtr  papers_doc)
85 {
86         GList      *papers = NULL;
87         xmlNodePtr  root, node;
88         glPaper    *paper;
89
90         LIBXML_TEST_VERSION;
91
92         root = xmlDocGetRootElement (papers_doc);
93         if (!root || !root->name) {
94                 g_warning ("\"%s\" is not a glabels paper file (no root node)",
95                            papers_doc->name);
96                 xmlFreeDoc (papers_doc);
97                 return papers;
98         }
99         if (!xmlStrEqual (root->name, "Glabels-paper-sizes")) {
100                 g_warning ("\"%s\" is not a glabels paper file (wrong root node)",
101                            papers_doc->name);
102                 xmlFreeDoc (papers_doc);
103                 return papers;
104         }
105
106         for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
107
108                 if (xmlStrEqual (node->name, "Paper-size")) {
109                         paper = gl_xml_paper_parse_paper_node (node);
110                         papers = g_list_append (papers, paper);
111                 } else {
112                         if ( !xmlNodeIsText(node) ) {
113                                 if (!xmlStrEqual (node->name,"comment")) {
114                                         g_warning ("bad node =  \"%s\"",node->name);
115                                 }
116                         }
117                 }
118         }
119
120         return papers;
121 }
122
123 /*****************************************************************************/
124 /* Parse XML paper Node.                                                     */
125 /*****************************************************************************/
126 glPaper *
127 gl_xml_paper_parse_paper_node (xmlNodePtr paper_node)
128 {
129         glPaper               *paper;
130         gchar                 *id, *name;
131         gdouble                width, height;
132
133         LIBXML_TEST_VERSION;
134
135         id   = xmlGetProp (paper_node, "id");
136
137         name = xmlGetProp (paper_node, "_name");
138         if (name != NULL) {
139                 name = gettext (name);
140         } else {
141                 name = xmlGetProp (paper_node, "name");
142         }
143
144         width  = gl_xml_get_prop_length (paper_node, "width", 0);
145         height = gl_xml_get_prop_length (paper_node, "height", 0);
146
147         paper = gl_paper_new (id, name, width, height);
148
149         g_free (id);
150         g_free (name);
151
152         return paper;
153 }
154