summaryrefslogtreecommitdiff
path: root/src/xml-helper.c
blob: 64934e771a1ea253585b50d6891502a3359724fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * xml-helper.c
 *
 *
 * Authors:
 *  Richard Hult <rhult@hem.passagen.se>
 *  Ricardo Markiewicz <rmarkie@fi.uba.ar>
 *  Andres de Barbara <adebarbara@fi.uba.ar>
 *
 * Web page: http://arrakis.lug.fi.uba.ar/
 *
 * Copyright (C) 1999-2001  Richard Hult
 * Copyright (C) 2003,2006  Ricardo Markiewicz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <gnome.h>

#include "xml-compat.h"
//#include <gnome-xml/parser.h>
//#include <gnome-xml/parserInternals.h>

#include "xml-helper.h"

/**
 * A modified version of XmlSAXParseFile in gnome-xml. This one lets us set
 * the user_data that is passed to the various callbacks, to make it possible
 * to avoid lots of global variables.
 */
int oreganoXmlSAXParseFile (xmlSAXHandlerPtr sax,
	gpointer user_data, const gchar *filename)
{
	int ret = 0;
	xmlParserCtxtPtr ctxt;

	ctxt = xmlCreateFileParserCtxt (filename);
	if (ctxt == NULL) return -1;
	ctxt->sax = sax;
	ctxt->userData = user_data;

#if defined(LIBXML_VERSION) && LIBXML_VERSION >= 20000
	xmlKeepBlanksDefault(0);
#endif
	xmlParseDocument (ctxt);

	if (ctxt->wellFormed)
		ret = 0;
	else
		ret = -1;
	if (sax != NULL)
		ctxt->sax = NULL;
	xmlFreeParserCtxt (ctxt);

	return ret;
}


/**
 * Set coodinate for a node, carried as the content of a child.
 */
void
xmlSetCoordinate (xmlNodePtr node, const char *name, double x, double y)
{
	xmlNodePtr child;
	gchar *str;
	xmlChar *ret;

	str = g_strdup_printf ("(%g %g)", x, y);
	ret = xmlGetProp (node, BAD_CAST name);
	if (ret != NULL){
		xmlSetProp (node, BAD_CAST name, BAD_CAST str);
		g_free (str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!xmlStrcmp (child->name, BAD_CAST name)){
			xmlNodeSetContent (child, BAD_CAST str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, BAD_CAST name, BAD_CAST str);
	g_free (str);
}



/**
 * Set coodinates for a node, carried as the content of a child.
 */
void
xmlSetCoordinates (xmlNodePtr node, const char *name,
	double x1, double y1, double x2, double y2)
{
	xmlNodePtr child;
	gchar *str;
	xmlChar *ret;

	str = g_strdup_printf ("(%g %g)(%g %g)", x1, y1, x2, y2);
	ret = xmlGetProp (node, BAD_CAST name);
	if (ret != NULL){
		xmlSetProp (node, BAD_CAST name, BAD_CAST str);
		g_free (str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!xmlStrcmp (child->name, BAD_CAST name)){
			xmlNodeSetContent (child, BAD_CAST str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, BAD_CAST name, BAD_CAST str);
	g_free (str);
}


/**
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
void
xmlSetGnomeCanvasPoints (xmlNodePtr node, const char *name,
			 GnomeCanvasPoints *val)
{
	xmlNodePtr child;
	char *str, *base;
	int i;

	if (val == NULL)
		return;
	if ((val->num_points < 0) || (val->num_points > 5000))
		return;
	base = str = g_malloc (val->num_points * 30 * sizeof (char));
	if (str == NULL)
		return;
	for (i = 0; i < val->num_points; i++){
		str += sprintf (str, "(%g %g)", val->coords[2 * i],
				val->coords[2 * i + 1]);
	}

	child = node->childs;
	while (child != NULL){
		if (!xmlStrcmp (child->name, BAD_CAST name)){
			xmlNodeSetContent (child, BAD_CAST base);
			free (base);
			return;
		}
		child = child->next;
	}
	xmlNewChild (node, NULL, BAD_CAST name,
		xmlEncodeEntitiesReentrant(node->doc, BAD_CAST base));
	g_free (base);
}


/**
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
void
xmlSetValue (xmlNodePtr node, const char *name, const char *val)
{
	xmlChar *ret;
	xmlNodePtr child;

	ret = xmlGetProp (node, BAD_CAST name);
	if (ret != NULL){
		xmlSetProp (node, BAD_CAST name, BAD_CAST val);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!xmlStrcmp (child->name, BAD_CAST name)){
			xmlNodeSetContent (child, BAD_CAST val);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, BAD_CAST name, BAD_CAST val);
}

/**
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
void
xmlSetIntValue (xmlNodePtr node, const char *name, int val)
{
	xmlChar *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%d", val);
	ret = xmlGetProp (node,BAD_CAST name);
	if (ret != NULL){
		xmlSetProp (node, BAD_CAST name, BAD_CAST str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!xmlStrcmp (child->name, BAD_CAST name)){
			xmlNodeSetContent (child, BAD_CAST str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, BAD_CAST name, BAD_CAST str);
}


/**
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
void
xmlSetDoubleValue (xmlNodePtr node, const char *name, double val)
{
	xmlChar *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%g", (float) val);
	ret = xmlGetProp (node, BAD_CAST name);
	if (ret != NULL){
		xmlSetProp (node, BAD_CAST name, BAD_CAST str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!xmlStrcmp (child->name, BAD_CAST name)){
			xmlNodeSetContent (child, BAD_CAST str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, BAD_CAST name, BAD_CAST str);
}