Branch data Line data Source code
1 : : /********************************************************************
2 : : * sixtp-to-dom-parser.c *
3 : : * Copyright 2001 Gnumatic, Inc. *
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 as *
7 : : * published by the Free Software Foundation; either version 2 of *
8 : : * the License, or (at your option) any later version. *
9 : : * *
10 : : * This program is distributed in the hope that it will be useful, *
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 : : * GNU General Public License for more details. *
14 : : * *
15 : : * You should have received a copy of the GNU General Public License*
16 : : * along with this program; if not, contact: *
17 : : * *
18 : : * Free Software Foundation Voice: +1-617-542-5942 *
19 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
21 : : * *
22 : : ********************************************************************/
23 : : #include <config.h>
24 : : #include <ctype.h>
25 : :
26 : : #include <glib.h>
27 : :
28 : : #include "sixtp-parsers.h"
29 : : #include "sixtp-utils.h"
30 : : #include "sixtp.h"
31 : :
32 : : static xmlNsPtr global_namespace = NULL;
33 : :
34 : : /* Don't pass anything in the data_for_children value to this
35 : : function. It'll cause a segfault */
36 : 50432 : static gboolean dom_start_handler (
37 : : GSList* sibling_data, gpointer parent_data, gpointer global_data,
38 : : gpointer* data_for_children, gpointer* result, const gchar* tag,
39 : : gchar** attrs)
40 : : {
41 : : xmlNodePtr thing;
42 : 50432 : gchar** atptr = attrs;
43 : :
44 : 50432 : if (parent_data == NULL)
45 : : {
46 : 3113 : thing = xmlNewNode (global_namespace, BAD_CAST tag);
47 : : /* only publish the result if we're the parent */
48 : 3113 : *result = thing;
49 : : }
50 : : else
51 : : {
52 : 47319 : thing = xmlNewChild ((xmlNodePtr) parent_data,
53 : : global_namespace,
54 : : BAD_CAST tag,
55 : : NULL);
56 : 47319 : *result = NULL;
57 : : }
58 : 50432 : *data_for_children = thing;
59 : :
60 : 50432 : if (attrs != NULL)
61 : : {
62 : 24858 : while (*atptr != 0)
63 : : {
64 : 12429 : gchar* attr0 = g_strdup (atptr[0]);
65 : 12429 : gchar* attr1 = g_strdup (atptr[1]);
66 : 12429 : xmlSetProp (thing, checked_char_cast (attr0),
67 : 12429 : checked_char_cast (attr1));
68 : 12429 : g_free (attr0);
69 : 12429 : g_free (attr1);
70 : 12429 : atptr += 2;
71 : : }
72 : : }
73 : 50432 : return TRUE;
74 : : }
75 : :
76 : : static void
77 : 0 : dom_fail_handler (gpointer data_for_children,
78 : : GSList* data_from_children,
79 : : GSList* sibling_data,
80 : : gpointer parent_data,
81 : : gpointer global_data,
82 : : gpointer* result,
83 : : const gchar* tag)
84 : : {
85 : 0 : if (*result) xmlFreeNode (static_cast<xmlNodePtr> (*result));
86 : 0 : }
87 : :
88 : 102905 : static gboolean dom_chars_handler (
89 : : GSList* sibling_data, gpointer parent_data, gpointer global_data,
90 : : gpointer* result, const char* text, int length)
91 : : {
92 : 102905 : if (length > 0)
93 : : {
94 : 102905 : gchar* newtext = g_strndup (text,length);
95 : 102905 : xmlNodeAddContentLen ((xmlNodePtr)parent_data,
96 : 102905 : checked_char_cast (newtext), length);
97 : 102905 : g_free (newtext);
98 : : }
99 : 102905 : return TRUE;
100 : : }
101 : :
102 : : sixtp*
103 : 1027 : sixtp_dom_parser_new (sixtp_end_handler ender,
104 : : sixtp_result_handler cleanup_result_by_default_func,
105 : : sixtp_result_handler cleanup_result_on_fail_func)
106 : : {
107 : : sixtp* top_level;
108 : :
109 : 1027 : g_return_val_if_fail (ender, NULL);
110 : :
111 : 1027 : if (! (top_level =
112 : 1027 : sixtp_set_any (sixtp_new (), FALSE,
113 : : SIXTP_START_HANDLER_ID, dom_start_handler,
114 : : SIXTP_CHARACTERS_HANDLER_ID, dom_chars_handler,
115 : : SIXTP_END_HANDLER_ID, ender,
116 : : SIXTP_FAIL_HANDLER_ID, dom_fail_handler,
117 : : SIXTP_NO_MORE_HANDLERS)))
118 : : {
119 : 0 : return NULL;
120 : : }
121 : :
122 : 1027 : if (cleanup_result_by_default_func)
123 : : {
124 : 62 : sixtp_set_cleanup_result (top_level, cleanup_result_by_default_func);
125 : : }
126 : :
127 : 1027 : if (cleanup_result_by_default_func)
128 : : {
129 : 62 : sixtp_set_result_fail (top_level, cleanup_result_on_fail_func);
130 : : }
131 : :
132 : 1027 : if (!sixtp_add_sub_parser (top_level, SIXTP_MAGIC_CATCHER, top_level))
133 : : {
134 : 0 : sixtp_destroy (top_level);
135 : 0 : return NULL;
136 : : }
137 : :
138 : 1027 : return top_level;
139 : : }
|