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