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 50857 : 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 50857 : gchar** atptr = attrs;
43 :
44 50857 : if (parent_data == NULL)
45 : {
46 3138 : thing = xmlNewNode (global_namespace, BAD_CAST tag);
47 : /* only publish the result if we're the parent */
48 3138 : *result = thing;
49 : }
50 : else
51 : {
52 47719 : thing = xmlNewChild ((xmlNodePtr) parent_data,
53 : global_namespace,
54 : BAD_CAST tag,
55 : NULL);
56 47719 : *result = NULL;
57 : }
58 50857 : *data_for_children = thing;
59 :
60 50857 : if (attrs != NULL)
61 : {
62 25102 : while (*atptr != 0)
63 : {
64 12551 : gchar* attr0 = g_strdup (atptr[0]);
65 12551 : gchar* attr1 = g_strdup (atptr[1]);
66 12551 : xmlSetProp (thing, checked_char_cast (attr0),
67 12551 : checked_char_cast (attr1));
68 12551 : g_free (attr0);
69 12551 : g_free (attr1);
70 12551 : atptr += 2;
71 : }
72 : }
73 50857 : 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 103723 : 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 103723 : if (length > 0)
93 : {
94 103723 : gchar* newtext = g_strndup (text,length);
95 103723 : xmlNodeAddContentLen ((xmlNodePtr)parent_data,
96 103723 : checked_char_cast (newtext), length);
97 103723 : g_free (newtext);
98 : }
99 103723 : return TRUE;
100 : }
101 :
102 : sixtp*
103 1054 : 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 1054 : g_return_val_if_fail (ender, NULL);
110 :
111 1054 : if (! (top_level =
112 1054 : 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 1054 : if (cleanup_result_by_default_func)
123 : {
124 64 : sixtp_set_cleanup_result (top_level, cleanup_result_by_default_func);
125 : }
126 :
127 1054 : if (cleanup_result_by_default_func)
128 : {
129 64 : sixtp_set_result_fail (top_level, cleanup_result_on_fail_func);
130 : }
131 :
132 1054 : 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 1054 : return top_level;
139 : }
|