/[zanavi_public1]/navit/navit/xmlconfig.c
ZANavi

Contents of /navit/navit/xmlconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (11 years, 1 month ago) by zoff99
File MIME type: text/plain
File size: 33538 byte(s)
new map version, lots of fixes and experimental new features
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2009 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 /* see http://library.gnome.org/devel/glib/stable/glib-Simple-XML-Subset-Parser.html
40 * for details on how the xml file parser works.
41 */
42
43 #include <stdlib.h>
44 #include <glib.h>
45 #include <glib/gprintf.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include "debug.h"
49 #include "config.h"
50 #include "file.h"
51 #include "coord.h"
52 #include "layout.h"
53 #include "mapset.h"
54 #include "projection.h"
55 #include "map.h"
56 #include "navigation.h"
57 #include "navit.h"
58 #include "plugin.h"
59 #include "route.h"
60 #include "speech.h"
61 #include "track.h"
62 #include "vehicle.h"
63 #include "point.h"
64 #include "graphics.h"
65 #include "gui.h"
66 #include "osd.h"
67 #include "log.h"
68 #include "announcement.h"
69 #include "vehicleprofile.h"
70 #include "roadprofile.h"
71 #include "config_.h"
72 #include "xmlconfig.h"
73
74 #ifdef HAVE_GLIB
75 #define ATTR_DISTANCE 1
76 const int xml_attr_distance=1;
77 #else
78 #include "ezxml.h"
79 const int xml_attr_distance = 2;
80 #define ATTR_DISTANCE 2
81 #define G_MARKUP_ERROR 0
82 #define G_MARKUP_ERROR_INVALID_CONTENT 0
83 #define G_MARKUP_ERROR_PARSE 0
84 #define G_MARKUP_ERROR_UNKNOWN_ELEMENT 0
85 typedef void * GMarkupParseContext;
86 #endif
87
88 struct xistate
89 {
90 struct xistate *parent;
91 struct xistate *child;
92 const gchar *element;
93 const gchar **attribute_names;
94 const gchar **attribute_values;
95 };
96
97 struct xmldocument
98 {
99 const gchar *href;
100 const gchar *xpointer;
101 gpointer user_data;
102 struct xistate *first;
103 struct xistate *last;
104 int active;
105 int level;
106 };
107
108 struct xmlstate
109 {
110 const gchar **attribute_names;
111 const gchar **attribute_values;
112 struct xmlstate *parent;
113 struct attr element_attr;
114 const gchar *element;
115 xmlerror **error;
116 struct element_func *func;
117 struct object_func *object_func;
118 struct xmldocument *document;
119 };
120
121 struct attr_fixme
122 {
123 char *element;
124 char **attr_fixme;
125 };
126
127 static struct attr ** convert_to_attrs(struct xmlstate *state, struct attr_fixme *fixme)
128 {
129 const gchar **attribute_name = state->attribute_names;
130 const gchar **attribute_value = state->attribute_values;
131 const gchar *name;
132 int count = 0;
133 struct attr **ret;
134 static int fixme_count;
135
136 while (*attribute_name)
137 {
138 count++;
139 attribute_name++;
140 }ret=g_new(struct attr *, count+1);
141 attribute_name = state->attribute_names;
142 count = 0;
143 while (*attribute_name)
144 {
145 name = *attribute_name;
146 if (fixme)
147 {
148 char **attr_fixme = fixme->attr_fixme;
149 while (attr_fixme[0])
150 {
151 if (!strcmp(name, attr_fixme[0]))
152 {
153 name = attr_fixme[1];
154 if (fixme_count++ < 10)
155 {
156 //dbg(0,"Please change attribute '%s' to '%s' in <%s />\n",attr_fixme[0], attr_fixme[1], fixme->element);
157 }
158 break;
159 }
160 attr_fixme += 2;
161 }
162 }
163 ret[count] = attr_new_from_text(name, *attribute_value);
164 if (ret[count])
165 {
166 count++;
167 }
168 else if (strcmp(*attribute_name, "enabled") && strcmp(*attribute_name, "xmlns:xi"))
169 {
170 // dbg(0, "failed to create attribute '%s' with value '%s'\n", *attribute_name, *attribute_value);
171 }
172 attribute_name++;
173 attribute_value++;
174 }
175 ret[count] = NULL;
176 //dbg(1, "ret=%p\n", ret);
177 return ret;
178 }
179
180 static const char * find_attribute(struct xmlstate *state, const char *attribute, int required)
181 {
182 const gchar **attribute_name = state->attribute_names;
183 const gchar **attribute_value = state->attribute_values;
184 while (*attribute_name)
185 {
186 if (!g_ascii_strcasecmp(attribute, *attribute_name))
187 return *attribute_value;
188 attribute_name++;
189 attribute_value++;
190 }
191 if (required)
192 g_set_error(state->error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "element '%s' is missing attribute '%s'", state->element, attribute);
193 return NULL;
194 }
195
196 static int find_boolean(struct xmlstate *state, const char *attribute, int deflt, int required)
197 {
198 const char *value;
199
200 value = find_attribute(state, attribute, required);
201 if (!value)
202 return deflt;
203 if (g_ascii_strcasecmp(value, "no") && g_ascii_strcasecmp(value, "0") && g_ascii_strcasecmp(value, "false"))
204 return 1;
205 return 0;
206 }
207
208 /**
209 * * Convert a string number to int
210 * *
211 * * @param val the string value to convert
212 * * @returns int value of converted string
213 * */
214 static int convert_number(const char *val)
215 {
216 if (val)
217 return g_ascii_strtoull(val, NULL, 0);
218 else
219 return 0;
220 }
221
222 static int xmlconfig_announce(struct xmlstate *state)
223 {
224 const char *type, *value;
225 char key[32];
226 int level[3];
227 int i;
228 enum item_type itype;
229 char *tok, *type_str, *str;
230
231 type = find_attribute(state, "type", 1);
232 if (!type)
233 return 0;
234 for (i = 0; i < 3; i++)
235 {
236 sprintf(key, "level%d", i);
237 value = find_attribute(state, key, 0);
238 if (value)
239 level[i] = convert_number(value);
240 else
241 level[i] = -1;
242 }
243 type_str = g_strdup(type);
244 str = type_str;
245 while ((tok = strtok(str, ",")))
246 {
247 itype = item_from_name(tok);
248 navigation_set_announce(state->parent->element_attr.u.data, itype, level);
249 str = NULL;
250 }
251 g_free(type_str);
252 return 1;
253 }
254 /**
255 * * Define the elements in our config
256 * *
257 * */
258
259 #define NEW(x) (void *(*)(struct attr *, struct attr **))(x)
260 #define GET(x) (int (*)(void *, enum attr_type type, struct attr *attr, struct attr_iter *iter))(x)
261 #define ITERN(x) (struct attr_iter * (*)(void *))(x)
262 #define ITERD(x) (void (*)(struct attr_iter *iter))(x)
263 #define SET(x) (int (*)(void *, struct attr *attr))(x)
264 #define ADD(x) (int (*)(void *, struct attr *attr))(x)
265 #define REMOVE(x) (int (*)(void *, struct attr *attr))(x)
266 #define INIT(x) (int (*)(void *))(x)
267 #define DESTROY(x) (void (*)(void *))(x)
268
269 static struct object_func
270 object_funcs[] =
271 { { attr_announcement, NEW(announcement_new), GET(announcement_get_attr), NULL, NULL, SET(announcement_set_attr), ADD(announcement_add_attr) }, { attr_arrows, NEW(arrows_new) }, { attr_circle, NEW(circle_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) }, { attr_config, NEW(config_new), GET(config_get_attr), ITERN(config_attr_iter_new), ITERD(config_attr_iter_destroy), SET(config_set_attr), ADD(config_add_attr), REMOVE(config_remove_attr), NULL, DESTROY(config_destroy) }, { attr_coord, NEW(coord_new_from_attrs) }, { attr_cursor, NEW(cursor_new), NULL, NULL, NULL, NULL, ADD(cursor_add_attr) }, { attr_debug, NEW(debug_new) }, { attr_graphics, NEW(graphics_new) }, { attr_gui, NEW(gui_new), GET(gui_get_attr), NULL, NULL, SET(gui_set_attr), ADD(gui_add_attr) }, { attr_icon, NEW(icon_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) }, { attr_image, NEW(image_new) }, { attr_itemgra, NEW(itemgra_new), NULL, NULL, NULL, NULL, ADD(itemgra_add_attr) }, { attr_layer, NEW(layer_new), NULL, NULL, NULL, NULL, ADD(layer_add_attr) }, { attr_layout, NEW(layout_new), NULL, NULL, NULL, NULL, ADD(layout_add_attr) }, { attr_log, NEW(log_new) }, { attr_map, NEW(map_new) }, { attr_mapset, NEW(mapset_new), NULL, NULL, NULL, NULL, ADD(mapset_add_attr_name) }, { attr_navigation, NEW(navigation_new), GET(navigation_get_attr) }, { attr_navit, NEW(navit_new), GET(navit_get_attr), ITERN(navit_attr_iter_new), ITERD(navit_attr_iter_destroy), SET(navit_set_attr), ADD(navit_add_attr), REMOVE(navit_remove_attr), INIT(navit_init), DESTROY(navit_destroy) }, { attr_osd, NEW(osd_new) }, { attr_plugins, NEW(plugins_new), NULL, NULL, NULL, NULL, NULL, NULL, INIT(plugins_init) }, { attr_plugin, NEW(plugin_new) }, { attr_polygon, NEW(polygon_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) }, { attr_polyline, NEW(polyline_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) }, { attr_roadprofile, NEW(roadprofile_new), GET(roadprofile_get_attr), NULL, NULL, SET(roadprofile_set_attr), ADD(roadprofile_add_attr) }, { attr_route, NEW(route_new), GET(route_get_attr), NULL, NULL, SET(route_set_attr), ADD(route_add_attr), REMOVE(route_remove_attr) }, { attr_speech, NEW(speech_new), GET(speech_get_attr), NULL, NULL, SET(speech_set_attr) }, { attr_text, NEW(text_new) }, { attr_tracking, NEW(tracking_new) }, { attr_vehicle, NEW(vehicle_new), GET(vehicle_get_attr), NULL, NULL, SET(vehicle_set_attr), ADD(vehicle_add_attr), REMOVE(vehicle_remove_attr) }, { attr_vehicleprofile, NEW(vehicleprofile_new), GET(vehicleprofile_get_attr), NULL, NULL, SET(vehicleprofile_set_attr), ADD(vehicleprofile_add_attr) }, };
272
273 struct object_func *
274 object_func_lookup(enum attr_type type)
275 {
276 int i;
277 for (i = 0; i < sizeof(object_funcs) / sizeof(struct object_func); i++)
278 {
279 if (object_funcs[i].type == type)
280 return &object_funcs[i];
281 }
282 return NULL;
283 }
284
285 struct element_func
286 {
287 char *name;
288 char *parent;
289 int (*func)(struct xmlstate *state);
290 enum attr_type type;
291 };
292 struct element_func *elements;
293
294 static char *attr_fixme_itemgra[] = { "type", "item_types", NULL, NULL, };
295
296 static char *attr_fixme_text[] = { "label_size", "text_size", NULL, NULL, };
297
298 static char *attr_fixme_circle[] = { "label_size", "text_size", NULL, NULL, };
299
300 static struct attr_fixme attr_fixmes[] = { { "item", attr_fixme_itemgra }, { "itemgra", attr_fixme_itemgra }, { "text", attr_fixme_text }, { "label", attr_fixme_text }, { "circle", attr_fixme_circle }, { NULL, NULL }, };
301
302 static char *element_fixmes[] = { "item", "itemgra", "label", "text", NULL, NULL, };
303
304 static void initStatic(void)
305 {
306 elements=g_new0(struct element_func,40); //39 is a number of elements + ending NULL element
307
308 elements[0].name = "config";
309 elements[0].parent = NULL;
310 elements[0].func = NULL;
311 elements[0].type = attr_config;
312
313 elements[1].name = "announce";
314 elements[1].parent = "navigation";
315 elements[1].func = xmlconfig_announce;
316
317 elements[2].name = "speech";
318 elements[2].parent = "navit";
319 elements[2].func = NULL;
320 elements[2].type = attr_speech;
321
322 elements[3].name = "tracking";
323 elements[3].parent = "navit";
324 elements[3].func = NULL;
325 elements[3].type = attr_tracking;
326
327 elements[4].name = "route";
328 elements[4].parent = "navit";
329 elements[4].func = NULL;
330 elements[4].type = attr_route;
331
332 elements[5].name = "mapset";
333 elements[5].parent = "navit";
334 elements[5].func = NULL;
335 elements[5].type = attr_mapset;
336
337 elements[6].name = "map";
338 elements[6].parent = "mapset";
339 elements[6].func = NULL;
340 elements[6].type = attr_map;
341
342 elements[7].name = "debug";
343 elements[7].parent = "config";
344 elements[7].func = NULL;
345 elements[7].type = attr_debug;
346
347 elements[8].name = "osd";
348 elements[8].parent = "navit";
349 elements[8].func = NULL;
350 elements[8].type = attr_osd;
351
352 elements[9].name = "navigation";
353 elements[9].parent = "navit";
354 elements[9].func = NULL;
355 elements[9].type = attr_navigation;
356
357 elements[10].name = "navit";
358 elements[10].parent = "config";
359 elements[10].func = NULL;
360 elements[10].type = attr_navit;
361
362 elements[11].name = "graphics";
363 elements[11].parent = "navit";
364 elements[11].func = NULL;
365 elements[11].type = attr_graphics;
366
367 elements[12].name = "gui";
368 elements[12].parent = "navit";
369 elements[12].func = NULL;
370 elements[12].type = attr_gui;
371
372 elements[13].name = "layout";
373 elements[13].parent = "navit";
374 elements[13].func = NULL;
375 elements[13].type = attr_layout;
376
377 elements[14].name = "cursor";
378 elements[14].parent = "layout";
379 elements[14].func = NULL;
380 elements[14].type = attr_cursor;
381
382 elements[15].name = "layer";
383 elements[15].parent = "layout";
384 elements[15].func = NULL;
385 elements[15].type = attr_layer;
386
387 elements[16].name = "itemgra";
388 elements[16].parent = "layer";
389 elements[16].func = NULL;
390 elements[16].type = attr_itemgra;
391
392 elements[17].name = "circle";
393 elements[17].parent = "itemgra";
394 elements[17].func = NULL;
395 elements[17].type = attr_circle;
396
397 elements[18].name = "coord";
398 elements[18].parent = "circle";
399 elements[18].func = NULL;
400 elements[18].type = attr_coord;
401
402 elements[19].name = "icon";
403 elements[19].parent = "itemgra";
404 elements[19].func = NULL;
405 elements[19].type = attr_icon;
406
407 elements[20].name = "coord";
408 elements[20].parent = "icon";
409 elements[20].func = NULL;
410 elements[20].type = attr_coord;
411
412 elements[21].name = "image";
413 elements[21].parent = "itemgra";
414 elements[21].func = NULL;
415 elements[21].type = attr_image;
416
417 elements[22].name = "text";
418 elements[22].parent = "itemgra";
419 elements[22].func = NULL;
420 elements[22].type = attr_text;
421
422 elements[23].name = "polygon";
423 elements[23].parent = "itemgra";
424 elements[23].func = NULL;
425 elements[23].type = attr_polygon;
426
427 elements[24].name = "coord";
428 elements[24].parent = "polygon";
429 elements[24].func = NULL;
430 elements[24].type = attr_coord;
431
432 elements[25].name = "polyline";
433 elements[25].parent = "itemgra";
434 elements[25].func = NULL;
435 elements[25].type = attr_polyline;
436
437 elements[26].name = "coord";
438 elements[26].parent = "polyline";
439 elements[26].func = NULL;
440 elements[26].type = attr_coord;
441
442 elements[27].name = "arrows";
443 elements[27].parent = "itemgra";
444 elements[27].func = NULL;
445 elements[27].type = attr_arrows;
446
447 elements[28].name = "vehicle";
448 elements[28].parent = "navit";
449 elements[28].func = NULL;
450 elements[28].type = attr_vehicle;
451
452 elements[29].name = "vehicleprofile";
453 elements[29].parent = "navit";
454 elements[29].func = NULL;
455 elements[29].type = attr_vehicleprofile;
456
457 elements[30].name = "roadprofile";
458 elements[30].parent = "vehicleprofile";
459 elements[30].func = NULL;
460 elements[30].type = attr_roadprofile;
461
462 elements[31].name = "announcement";
463 elements[31].parent = "roadprofile";
464 elements[31].func = NULL;
465 elements[31].type = attr_announcement;
466
467 elements[32].name = "cursor";
468 elements[32].parent = "vehicle";
469 elements[32].func = NULL;
470 elements[32].type = attr_cursor;
471
472 elements[33].name = "itemgra";
473 elements[33].parent = "cursor";
474 elements[33].func = NULL;
475 elements[33].type = attr_itemgra;
476
477 elements[34].name = "log";
478 elements[34].parent = "vehicle";
479 elements[34].func = NULL;
480 elements[34].type = attr_log;
481
482 elements[35].name = "log";
483 elements[35].parent = "navit";
484 elements[35].func = NULL;
485 elements[35].type = attr_log;
486
487 elements[36].name = "plugins";
488 elements[36].parent = "config";
489 elements[36].func = NULL;
490 elements[36].type = attr_plugins;
491
492 elements[37].name = "plugin";
493 elements[37].parent = "plugins";
494 elements[37].func = NULL;
495 elements[37].type = attr_plugin;
496 }
497
498 /**
499 * * Parse the opening tag of a config element
500 * *
501 * * @param context document parse context
502 * * @param element_name the current tag name
503 * * @param attribute_names ptr to return the set of attribute names
504 * * @param attribute_values ptr return the set of attribute values
505 * * @param user_data ptr to xmlstate structure
506 * * @param error ptr return error context
507 * * @returns nothing
508 * */
509
510 static void start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, xmlerror **error)
511 {
512 struct xmlstate *new = NULL, **parent = user_data;
513 struct element_func *e = elements, *func = NULL;
514 struct attr_fixme *attr_fixme = attr_fixmes;
515 char **element_fixme = element_fixmes;
516 int found = 0;
517 static int fixme_count;
518 const char *parent_name = NULL;
519 char *s, *sep = "", *possible_parents;
520 struct attr *parent_attr;
521 // dbg(2, "name='%s' parent='%s'\n", element_name, *parent ? (*parent)->element : NULL);
522
523 if (!strcmp(element_name, "xml"))
524 return;
525 /* determine if we have to fix any attributes */
526 while (attr_fixme[0].element)
527 {
528 if (!strcmp(element_name, attr_fixme[0].element))
529 break;
530 attr_fixme++;
531 }
532 if (!attr_fixme[0].element)
533 attr_fixme = NULL;
534
535 /* tell user to fix deprecated element names */
536 while (element_fixme[0])
537 {
538 if (!strcmp(element_name, element_fixme[0]))
539 {
540 element_name = element_fixme[1];
541 if (fixme_count++ < 10)
542 {
543 // dbg(0, "Please change <%s /> to <%s /> in config file\n",element_fixme[0], element_fixme[1]);
544 }
545 }
546 element_fixme += 2;
547 }
548 /* validate that this element is valid
549 * and that the element has a valid parent */
550 possible_parents = g_strdup("");
551 if (*parent)
552 parent_name = (*parent)->element;
553 while (e->name)
554 {
555 if (!g_ascii_strcasecmp(element_name, e->name))
556 {
557 found = 1;
558 s = g_strconcat(possible_parents, sep, e->parent, NULL);
559 g_free(possible_parents);
560 possible_parents = s;
561 sep = ",";
562 if ((parent_name && e->parent && !g_ascii_strcasecmp(parent_name, e->parent)) || (!parent_name && !e->parent))
563 func = e;
564 }
565 e++;
566 }
567 if (!found)
568 {
569 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT, "Unknown element '%s'", element_name);
570 g_free(possible_parents);
571 return;
572 }
573 if (!func)
574 {
575 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "Element '%s' within unexpected context '%s'. Expected '%s'%s", element_name, parent_name, possible_parents, !strcmp(possible_parents, "config") ? "\nPlease add <config> </config> tags at the beginning/end of your navit.xml" : "");
576 g_free(possible_parents);
577 return;
578 }
579 g_free(possible_parents);
580
581 new=g_new(struct xmlstate, 1);
582 new->attribute_names = attribute_names;
583 new->attribute_values = attribute_values;
584 new->parent = *parent;
585 new->element_attr.u.data = NULL;
586 new->element = element_name;
587 new->error = error;
588 new->func = func;
589 new->object_func = NULL;
590 *parent = new;
591 if (!find_boolean(new, "enabled", 1, 0))
592 return;
593 if (new->parent && !new->parent->element_attr.u.data)
594 return;
595 if (func->func)
596 {
597 if (!func->func(new))
598 {
599 return;
600 }
601 }
602 else
603 {
604 struct attr **attrs;
605
606 new->object_func = object_func_lookup(func->type);
607 if (!new->object_func)
608 return;
609 attrs = convert_to_attrs(new, attr_fixme);
610 new->element_attr.type = attr_none;
611 if (!new->parent || new->parent->element_attr.type == attr_none)
612 parent_attr = NULL;
613 else
614 parent_attr = &new->parent->element_attr;
615 new->element_attr.u.data = new->object_func->create(parent_attr, attrs);
616 if (!new->element_attr.u.data)
617 return;
618 new->element_attr.type = attr_from_name(element_name);
619 if (new->element_attr.type == attr_none)
620 {
621 // dbg(0, "failed to create object of type '%s'\n", element_name);
622 }
623 if (new->parent && new->parent->object_func && new->parent->object_func->add_attr)
624 new->parent->object_func->add_attr(new->parent->element_attr.u.data, &new->element_attr);
625 }
626 return;
627 }
628
629 /* Called for close tags </foo> */
630 static void end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, xmlerror **error)
631 {
632 struct xmlstate *curr, **state = user_data;
633
634 if (!strcmp(element_name, "xml"))
635 {
636 return;
637 }
638 // dbg(2, "name='%s'\n", element_name);
639 curr = *state;
640 if (curr->object_func && curr->object_func->init)
641 curr->object_func->init(curr->element_attr.u.data);
642 *state = curr->parent;
643 g_free(curr);
644 }
645
646 static gboolean parse_file(struct xmldocument *document, xmlerror **error);
647
648 static void xinclude(GMarkupParseContext *context, const gchar **attribute_names, const gchar **attribute_values, struct xmldocument *doc_old, xmlerror **error)
649 {
650 struct xmldocument doc_new;
651 struct file_wordexp *we;
652 int i, count;
653 const char *href = NULL;
654 char **we_files;
655
656 if (doc_old->level >= 16)
657 {
658 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include recursion too deep");
659 return;
660 }
661 memset(&doc_new, 0, sizeof(doc_new));
662 i = 0;
663 while (attribute_names[i])
664 {
665 if (!g_ascii_strcasecmp("href", attribute_names[i]))
666 {
667 if (!href)
668 href = attribute_values[i];
669 else
670 {
671 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has more than one href");
672 return;
673 }
674 }
675 else if (!g_ascii_strcasecmp("xpointer", attribute_names[i]))
676 {
677 if (!doc_new.xpointer)
678 doc_new.xpointer = attribute_values[i];
679 else
680 {
681 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has more than one xpointer");
682 return;
683 }
684 }
685 else
686 {
687 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has invalid attributes");
688 return;
689 }
690 i++;
691 }
692 if (!doc_new.xpointer && !href)
693 {
694 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has neither href nor xpointer");
695 return;
696 }
697 doc_new.level = doc_old->level + 1;
698 doc_new.user_data = doc_old->user_data;
699 if (!href)
700 {
701 //dbg(1, "no href, using '%s'\n", doc_old->href);
702 doc_new.href = doc_old->href;
703 if (file_exists(doc_new.href))
704 {
705 parse_file(&doc_new, error);
706 }
707 else
708 {
709 //dbg(0, "Unable to include %s\n", doc_new.href);
710 }
711 }
712 else
713 {
714 //dbg(1, "expanding '%s'\n", href);
715 we = file_wordexp_new(href);
716 we_files = file_wordexp_get_array(we);
717 count = file_wordexp_get_count(we);
718 //dbg(1, "%d results\n", count);
719 if (file_exists(we_files[0]))
720 {
721 for (i = 0; i < count; i++)
722 {
723 //dbg(1, "result[%d]='%s'\n", i, we_files[i]);
724 doc_new.href = we_files[i];
725 parse_file(&doc_new, error);
726 }
727 }
728 else
729 {
730 //dbg(0, "Unable to include %s\n", we_files[0]);
731 }
732 file_wordexp_destroy(we);
733
734 }
735
736 }
737
738 static int strncmp_len(const char *s1, int s1len, const char *s2)
739 {
740 int ret;
741 #if 0
742 char c[s1len+1];
743 strncpy(c, s1, s1len);
744 c[s1len]='\0';
745 //dbg(0,"'%s' vs '%s'\n", c, s2);
746 #endif
747
748 ret = strncmp(s1, s2, s1len);
749 if (ret)
750 return ret;
751 return strlen(s2) - s1len;
752 }
753
754 static int xpointer_value(const char *test, int len, struct xistate *elem, const char **out, int out_len)
755 {
756 int i, ret = 0;
757 if (len <= 0 || out_len <= 0)
758 {
759 return 0;
760 }
761 if (!(strncmp_len(test, len, "name(.)")))
762 {
763 out[0] = elem->element;
764 return 1;
765 }
766 if (test[0] == '@')
767 {
768 i = 0;
769 while (elem->attribute_names[i] && out_len > 0)
770 {
771 if (!strncmp_len(test + 1, len - 1, elem->attribute_names[i]))
772 {
773 out[ret++] = elem->attribute_values[i];
774 out_len--;
775 }
776 i++;
777 }
778 return ret;
779 }
780 return 0;
781 }
782
783 static int xpointer_test(const char *test, int len, struct xistate *elem)
784 {
785 int eq, i, count, vlen, cond_req = 1, cond = 0;
786 char c;
787 const char *tmp[16];
788 #if 0
789 char test2[len+1];
790
791 strncpy(test2, test, len);
792 test2[len]='\0';
793 //dbg(0,"%s\n", test2);
794 #endif
795 if (!len)
796 return 0;
797 c = test[len - 1];
798 if (c != '\'' && c != '"')
799 return 0;
800 eq = strcspn(test, "=");
801 if (eq >= len || test[eq + 1] != c)
802 return 0;
803 vlen = eq;
804 if (eq > 0 && test[eq - 1] == '!')
805 {
806 cond_req = 0;
807 vlen--;
808 }
809 count = xpointer_value(test, vlen, elem, tmp, 16);
810 for (i = 0; i < count; i++)
811 {
812 if (!strncmp_len(test + eq + 2, len - eq - 3, tmp[i]))
813 cond = 1;
814 }
815 if (cond == cond_req)
816 return 1;
817 return 0;
818 }
819
820 static int xpointer_element_match(const char *xpointer, int len, struct xistate *elem)
821 {
822 int start, tlen, tlen2;
823 #if 0
824 char test2[len+1];
825
826 strncpy(test2, xpointer, len);
827 test2[len]='\0';
828 //dbg(0,"%s\n", test2);
829 #endif
830 start = strcspn(xpointer, "[");
831 if (start > len)
832 start = len;
833 if (strncmp_len(xpointer, start, elem->element) && (start != 1 || xpointer[0] != '*'))
834 return 0;
835 if (start == len)
836 return 1;
837 if (xpointer[len - 1] != ']')
838 return 0;
839 tlen = len - start - 2;
840 for (;;)
841 {
842 start++;
843 tlen2 = strcspn(xpointer + start, "]");
844 if (start + tlen2 > len)
845 return 1;
846 if (!xpointer_test(xpointer + start, tlen2, elem))
847 return 0;
848 start += tlen2 + 1;
849 }
850 }
851
852 static int xpointer_xpointer_match(const char *xpointer, int len, struct xistate *first)
853 {
854 const char *c;
855 int s;
856 //dbg(2, "%s\n", xpointer);
857 if (xpointer[0] != '/')
858 return 0;
859 c = xpointer + 1;
860 len--;
861 do
862 {
863 s = strcspn(c, "/");
864 if (s > len)
865 s = len;
866 if (!xpointer_element_match(c, s, first))
867 return 0;
868 first = first->child;
869 c += s + 1;
870 len -= s + 1;
871 }
872 while (len > 0 && first);
873 if (len > 0)
874 return 0;
875 return 1;
876 }
877
878 static int xpointer_match(const char *xpointer, struct xistate *first)
879 {
880 char *prefix = "xpointer(";
881 int len;
882 if (!xpointer)
883 return 1;
884 len = strlen(xpointer);
885 if (strncmp(xpointer, prefix, strlen(prefix)))
886 return 0;
887 if (xpointer[len - 1] != ')')
888 return 0;
889 return xpointer_xpointer_match(xpointer + strlen(prefix), len - strlen(prefix) - 1, first);
890
891 }
892
893 static void xi_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, xmlerror **error)
894 {
895 struct xmldocument *doc = user_data;
896 struct xistate *xistate;
897 int i, count = 0;
898 while (attribute_names[count++ * ATTR_DISTANCE])
899 ;xistate=g_new0(struct xistate, 1);
900 xistate->element = element_name;
901 xistate->attribute_names=g_new0(const char *, count);
902 xistate->attribute_values=g_new0(const char *, count);
903 for (i = 0; i < count; i++)
904 {
905 if (attribute_names[i * ATTR_DISTANCE] && attribute_values[i * ATTR_DISTANCE])
906 {
907 xistate->attribute_names[i] = g_strdup(attribute_names[i * ATTR_DISTANCE]);
908 xistate->attribute_values[i] = g_strdup(attribute_values[i * ATTR_DISTANCE]);
909 }
910 }
911 xistate->parent = doc->last;
912
913 if (doc->last)
914 {
915 doc->last->child = xistate;
916 }
917 else
918 doc->first = xistate;
919 doc->last = xistate;
920 if (doc->active > 0 || xpointer_match(doc->xpointer, doc->first))
921 {
922 if (!g_ascii_strcasecmp("xi:include", element_name))
923 {
924 xinclude(context, xistate->attribute_names, xistate->attribute_values, doc, error);
925 return;
926 }
927 start_element(context, element_name, xistate->attribute_names, xistate->attribute_values, doc->user_data, error);
928 doc->active++;
929 }
930
931 }
932 /**
933 * * Reached closing tag of a config element
934 * *
935 * * @param context
936 * * @param element name
937 * * @param user_data ptr to xmldocument
938 * * @param error ptr to struct for error information
939 * * @returns nothing
940 * */
941
942 static void xi_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, xmlerror **error)
943 {
944 struct xmldocument *doc = user_data;
945 struct xistate *xistate = doc->last;
946 int i = 0;
947 doc->last = doc->last->parent;
948 if (!doc->last)
949 doc->first = NULL;
950 else
951 doc->last->child = NULL;
952 if (doc->active > 0)
953 {
954 if (!g_ascii_strcasecmp("xi:include", element_name))
955 {
956 return;
957 }
958 end_element(context, element_name, doc->user_data, error);
959 doc->active--;
960 }
961 while (xistate->attribute_names[i])
962 {
963 g_free((char *) (xistate->attribute_names[i]));
964 g_free((char *) (xistate->attribute_values[i]));
965 i++;
966 }
967 g_free(xistate->attribute_names);
968 g_free(xistate->attribute_values);
969 g_free(xistate);
970 }
971
972 /* Called for character data */
973 /* text is not nul-terminated */
974 static void xi_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, xmlerror **error)
975 {
976 struct xmldocument *doc = user_data;
977 int i;
978 if (doc->active)
979 {
980 for (i = 0; i < text_len; i++)
981 {
982 if (!isspace(text[i]))
983 {
984 struct xmldocument *doc = user_data;
985 struct xmlstate *curr, **state = doc->user_data;
986 struct attr attr;
987 char *text_dup = malloc(text_len + 1);
988
989 curr = *state;
990 strncpy(text_dup, text, text_len);
991 text_dup[text_len] = '\0';
992 attr.type = attr_xml_text;
993 attr.u.str = text_dup;
994 if (curr->object_func && curr->object_func->add_attr && curr->element_attr.u.data)
995 curr->object_func->add_attr(curr->element_attr.u.data, &attr);
996 free(text_dup);
997 return;
998 }
999 }
1000 }
1001 }
1002
1003 #ifndef HAVE_GLIB
1004 static void parse_node_text(ezxml_t node, void *data, void(*start)(void *, const char *, const char **, const char **, void *, void *), void(*end)(void *, const char *, void *, void *), void(*text)(void *, const char *, int, void *, void *))
1005 {
1006 while (node)
1007 {
1008 if (start)
1009 start(NULL, node->name, (const char **) node->attr, (const char **) (node->attr + 1), data, NULL);
1010 if (text && node->txt)
1011 text(NULL, node->txt, strlen(node->txt), data, NULL);
1012 if (node->child)
1013 parse_node_text(node->child, data, start, end, text);
1014 if (end)
1015 end(NULL, node->name, data, NULL);
1016 node = node->ordered;
1017 }
1018 }
1019 #endif
1020
1021 void xml_parse_text(const char *document, void *data, void(*start)(void *, const char *, const char **, const char **, void *, void *), void(*end)(void *, const char *, void *, void *), void(*text)(void *, const char *, int, void *, void *))
1022 {
1023 #ifdef HAVE_GLIB
1024 GMarkupParser parser =
1025 { start, end, text, NULL, NULL};
1026 GMarkupParseContext *context;
1027 gboolean result;
1028
1029 context = g_markup_parse_context_new (&parser, 0, data, NULL);
1030 result = g_markup_parse_context_parse (context, document, strlen(document), NULL);
1031 g_markup_parse_context_free (context);
1032 #else
1033 char *str = g_strdup(document);
1034 ezxml_t root = ezxml_parse_str(str, strlen(str));
1035 if (!root)
1036 return;
1037 parse_node_text(root, data, start, end, text);
1038 ezxml_free(root);
1039 g_free(str);
1040 #endif
1041 }
1042
1043 #ifdef HAVE_GLIB
1044
1045 static const GMarkupParser parser =
1046 {
1047 xi_start_element,
1048 xi_end_element,
1049 xi_text,
1050 NULL,
1051 NULL
1052 };
1053 /**
1054 * * Parse the contents of the configuration file
1055 * *
1056 * * @param document struct holding info about the config file
1057 * * @param error info on any errors detected
1058 * * @returns boolean TRUE or FALSE
1059 * */
1060
1061 static gboolean
1062 parse_file(struct xmldocument *document, xmlerror **error)
1063 {
1064 //dbg(0,"EEnter\n");
1065
1066 GMarkupParseContext *context;
1067 gchar *contents, *message;
1068 gsize len;
1069 gint line, chr;
1070 gboolean result;
1071 char *xmldir,*newxmldir,*xmlfile,*newxmlfile,*sep;
1072
1073 //dbg(1,"enter filename='%s'\n", document->href);
1074 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 12
1075 #define G_MARKUP_TREAT_CDATA_AS_TEXT 0
1076 #endif
1077
1078 context = g_markup_parse_context_new(&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, document, NULL);
1079
1080 if (!g_file_get_contents(document->href, &contents, &len, error))
1081 {
1082 g_markup_parse_context_free(context);
1083 return FALSE;
1084 }
1085 xmldir=getenv("XMLDIR");
1086 xmlfile=getenv("XMLFILE");
1087 newxmlfile=g_strdup(document->href);
1088 newxmldir=g_strdup(document->href);
1089 if ((sep=strrchr(newxmldir,'/')))
1090 *sep='\0';
1091 else
1092 {
1093 g_free(newxmldir);
1094 newxmldir=g_strdup(".");
1095 }
1096 setenv("XMLDIR",newxmldir,1);
1097 setenv("XMLFILE",newxmlfile,1);
1098 document->active=document->xpointer ? 0:1;
1099 document->first=NULL;
1100 document->last=NULL;
1101 result = g_markup_parse_context_parse(context, contents, len, error);
1102 if (!result && error && *error)
1103 {
1104 g_markup_parse_context_get_position(context, &line, &chr);
1105 message=g_strdup_printf("%s at line %d, char %d\n", (*error)->message, line, chr);
1106 g_free((*error)->message);
1107 (*error)->message=message;
1108 }
1109 g_markup_parse_context_free(context);
1110 g_free (contents);
1111 if (xmldir)
1112 setenv("XMLDIR",xmldir,1);
1113 else
1114 #ifndef __MINGW32__
1115 unsetenv("XMLDIR");
1116 #else
1117 putenv("XMLDIR=");
1118 #endif /* __MINGW32__ */
1119 if (xmlfile)
1120 setenv("XMLFILE",xmlfile,1);
1121 else
1122 #ifndef __MINGW32__
1123 unsetenv("XMLFILE");
1124 #else
1125 putenv("XMLFILE=");
1126 #endif /* __MINGW32__ */
1127 g_free(newxmldir);
1128 g_free(newxmlfile);
1129 //dbg(1,"return %d\n", result);
1130
1131 return result;
1132 }
1133 #else
1134
1135 // this is used on android!!!!
1136 // this is used on android!!!!
1137 // this is used on android!!!!
1138 static void parse_node(struct xmldocument *document, ezxml_t node)
1139 {
1140
1141 //dbg(0,"EEnter\n");
1142
1143 while (node)
1144 {
1145 //dbg(0,"name:%s\n", node->name);
1146
1147 xi_start_element(NULL, node->name, node->attr, node->attr + 1, document, NULL);
1148 if (node->txt)
1149 {
1150 //dbg(0," txt:start\n");
1151 xi_text(NULL, node->txt, strlen(node->txt), document, NULL);
1152 //dbg(0," txt:end\n");
1153 }
1154
1155 if (node->child)
1156 {
1157 parse_node(document, node->child);
1158 }
1159
1160 xi_end_element(NULL, node->name, document, NULL);
1161 node = node->ordered;
1162 }
1163 }
1164
1165 // this is used on android!!!!
1166 // this is used on android!!!!
1167 // this is used on android!!!!
1168 // this is used on android!!!!
1169 static gboolean parse_file(struct xmldocument *document, xmlerror **error)
1170 {
1171 //dbg(0,"EEnter 2\n");
1172
1173 FILE *f;
1174 ezxml_t root;
1175
1176 f = fopen(document->href, "rb");
1177 if (!f)
1178 return FALSE;
1179 root = ezxml_parse_fp(f);
1180 fclose(f);
1181 if (!root)
1182 return FALSE;
1183 document->active = document->xpointer ? 0 : 1;
1184 document->first = NULL;
1185 document->last = NULL;
1186
1187 parse_node(document, root);
1188
1189 return TRUE;
1190 }
1191 #endif
1192
1193 /**
1194 * * Load and parse the master config file
1195 * *
1196 * * @param filename FQFN of the file
1197 * * @param error ptr to error details, if any
1198 * * @returns boolean TRUE or FALSE (if error detected)
1199 * */
1200
1201 gboolean config_load(const char *filename, xmlerror **error)
1202 {
1203 struct xmldocument document;
1204 struct xmlstate *curr = NULL;
1205 gboolean result;
1206
1207 initStatic();
1208
1209 dbg(0, "XML-----XML-----enter filename='%s'\n", filename);
1210 memset(&document, 0, sizeof(document));
1211 document.href = filename;
1212 document.user_data = &curr;
1213 result = parse_file(&document, error);
1214 //dbg(0,"xml 003\n");
1215 if (result && curr)
1216 {
1217 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE, "element '%s' not closed", curr->element);
1218 result = FALSE;
1219 }
1220 dbg(0, "XML-----XML-----return %d\n", result);
1221 return result;
1222 }
1223

   
Visit the ZANavi Wiki