author | hallski <hallski> |
Mon, 19 May 2003 19:01:30 +0000 | |
changeset 3 | 5c632650c603 |
parent 1 | 50e230cf7818 |
child 4 | 52f92481d8de |
permissions | -rw-r--r-- |
1 | 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 |
/* |
|
3
5c632650c603
2003-05-19 Mikael Hallendal <micke@imendo.com>
hallski <hallski>
parents:
1
diff
changeset
|
3 |
* Copyright (C) 2003 Mikael Hallendal <micke@imendo.com> |
1 | 4 |
* Copyright (C) 2003 CodeFactory AB. |
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU Lesser General Public License as |
|
8 |
* published by the Free Software Foundation; either version 2 of the |
|
9 |
* License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* Lesser General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU Lesser General Public |
|
17 |
* License along with this program; if not, write to the |
|
18 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
19 |
* Boston, MA 02111-1307, USA. |
|
20 |
*/ |
|
21 |
||
22 |
#include <config.h> |
|
23 |
#include <string.h> |
|
24 |
||
25 |
#include "lm-debug.h" |
|
26 |
#include "lm-internals.h" |
|
27 |
#include "lm-message-node.h" |
|
28 |
#include "lm-parser.h" |
|
29 |
||
30 |
#define SHORT_END_TAG "/>" |
|
31 |
#define XML_MAX_DEPTH 5 |
|
32 |
||
33 |
#define LM_PARSER(o) ((LmParser *) o) |
|
34 |
||
35 |
struct LmParser { |
|
36 |
LmParserMessageFunction function; |
|
37 |
gpointer user_data; |
|
38 |
GDestroyNotify notify; |
|
39 |
||
40 |
LmMessageNode *cur_root; |
|
41 |
LmMessageNode *cur_node; |
|
42 |
||
43 |
GMarkupParser *m_parser; |
|
44 |
GMarkupParseContext *context; |
|
45 |
}; |
|
46 |
||
47 |
||
48 |
/* Used while parsing */ |
|
49 |
static void parser_start_node_cb (GMarkupParseContext *context, |
|
50 |
const gchar *node_name, |
|
51 |
const gchar **attribute_names, |
|
52 |
const gchar **attribute_values, |
|
53 |
gpointer user_data, |
|
54 |
GError **error); |
|
55 |
static void parser_end_node_cb (GMarkupParseContext *context, |
|
56 |
const gchar *node_name, |
|
57 |
gpointer user_data, |
|
58 |
GError **error); |
|
59 |
static void parser_text_cb (GMarkupParseContext *context, |
|
60 |
const gchar *text, |
|
61 |
gsize text_len, |
|
62 |
gpointer user_data, |
|
63 |
GError **error); |
|
64 |
static void parser_error_cb (GMarkupParseContext *context, |
|
65 |
GError *error, |
|
66 |
gpointer user_data); |
|
67 |
||
68 |
static void |
|
69 |
parser_start_node_cb (GMarkupParseContext *context, |
|
70 |
const gchar *node_name, |
|
71 |
const gchar **attribute_names, |
|
72 |
const gchar **attribute_values, |
|
73 |
gpointer user_data, |
|
74 |
GError **error) |
|
75 |
{ |
|
76 |
LmParser *parser; |
|
77 |
gint i; |
|
78 |
||
79 |
parser = LM_PARSER (user_data);; |
|
80 |
||
81 |
||
82 |
/* parser->cur_depth++; */ |
|
83 |
||
84 |
if (!parser->cur_root) { |
|
85 |
/* New toplevel element */ |
|
86 |
parser->cur_root = _lm_message_node_new (node_name); |
|
87 |
parser->cur_node = parser->cur_root; |
|
88 |
} else { |
|
89 |
LmMessageNode *parent_node; |
|
90 |
||
91 |
parent_node = parser->cur_node; |
|
92 |
||
93 |
parser->cur_node = _lm_message_node_new (node_name); |
|
94 |
_lm_message_node_add_child_node (parent_node, |
|
95 |
parser->cur_node); |
|
96 |
} |
|
97 |
||
98 |
for (i = 0; attribute_names[i]; ++i) { |
|
99 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER, |
|
100 |
"ATTRIBUTE: %s = %s\n", |
|
101 |
attribute_names[i], |
|
102 |
attribute_values[i]); |
|
103 |
||
104 |
lm_message_node_set_attributes (parser->cur_node, |
|
105 |
attribute_names[i], |
|
106 |
attribute_values[i], |
|
107 |
NULL); |
|
108 |
} |
|
109 |
||
110 |
if (g_ascii_strcasecmp ("stream:stream", node_name) == 0) { |
|
111 |
parser_end_node_cb (context, |
|
112 |
"stream:stream", |
|
113 |
user_data, |
|
114 |
error); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
static void |
|
119 |
parser_end_node_cb (GMarkupParseContext *context, |
|
120 |
const gchar *node_name, |
|
121 |
gpointer user_data, |
|
122 |
GError **error) |
|
123 |
{ |
|
124 |
LmParser *parser; |
|
125 |
||
126 |
parser = LM_PARSER (user_data); |
|
127 |
||
128 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER, |
|
129 |
"Trying to close node: %s\n", node_name); |
|
130 |
||
131 |
if (g_ascii_strcasecmp (parser->cur_node->name, node_name) != 0) { |
|
132 |
if (g_ascii_strcasecmp (node_name, "stream:stream")) { |
|
133 |
g_print ("Got an stream:stream end\n"); |
|
134 |
} |
|
135 |
g_warning ("Trying to close node that isn't open: %s", |
|
136 |
node_name); |
|
137 |
return; |
|
138 |
} |
|
139 |
||
140 |
if (parser->cur_node == parser->cur_root) { |
|
141 |
LmMessage *m; |
|
142 |
||
143 |
m = _lm_message_new_from_node (parser->cur_root); |
|
144 |
||
145 |
if (!m) { |
|
146 |
g_warning ("Couldn't create message: %s\n", |
|
147 |
parser->cur_root->name); |
|
148 |
return; |
|
149 |
} |
|
150 |
||
151 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_PARSER, |
|
152 |
"Have a new message\n"); |
|
153 |
if (parser->function) { |
|
154 |
(* parser->function) (parser, m, parser->user_data); |
|
155 |
} |
|
156 |
||
157 |
lm_message_unref (m); |
|
158 |
lm_message_node_unref (parser->cur_root); |
|
159 |
||
160 |
parser->cur_node = parser->cur_root = NULL; |
|
161 |
} else { |
|
162 |
LmMessageNode *tmp_node; |
|
163 |
tmp_node = parser->cur_node; |
|
164 |
parser->cur_node = parser->cur_node->parent; |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
static void |
|
169 |
parser_text_cb (GMarkupParseContext *context, |
|
170 |
const gchar *text, |
|
171 |
gsize text_len, |
|
172 |
gpointer user_data, |
|
173 |
GError **error) |
|
174 |
{ |
|
175 |
LmParser *parser; |
|
176 |
||
177 |
g_return_if_fail (user_data != NULL); |
|
178 |
||
179 |
parser = LM_PARSER (user_data); |
|
180 |
||
181 |
if (parser->cur_node && strcmp (text, "") != 0) { |
|
182 |
lm_message_node_set_value (parser->cur_node, text); |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
static void |
|
187 |
parser_error_cb (GMarkupParseContext *context, |
|
188 |
GError *error, |
|
189 |
gpointer user_data) |
|
190 |
{ |
|
191 |
LmParser *parser; |
|
192 |
||
193 |
g_return_if_fail (user_data != NULL); |
|
194 |
||
195 |
parser = LM_PARSER (user_data); |
|
196 |
||
197 |
g_markup_parse_context_free (parser->context); |
|
198 |
parser->context = NULL; |
|
199 |
} |
|
200 |
||
201 |
LmParser * |
|
202 |
lm_parser_new (LmParserMessageFunction function, |
|
203 |
gpointer user_data, |
|
204 |
GDestroyNotify notify) |
|
205 |
{ |
|
206 |
LmParser *parser; |
|
207 |
||
208 |
parser = g_new0 (LmParser, 1); |
|
209 |
if (!parser) { |
|
210 |
return NULL; |
|
211 |
} |
|
212 |
||
213 |
parser->m_parser = g_new0 (GMarkupParser, 1); |
|
214 |
if (!parser->m_parser) { |
|
215 |
g_free (parser); |
|
216 |
return NULL; |
|
217 |
} |
|
218 |
||
219 |
parser->function = function; |
|
220 |
parser->user_data = user_data; |
|
221 |
parser->notify = notify; |
|
222 |
||
223 |
parser->m_parser->start_element = parser_start_node_cb; |
|
224 |
parser->m_parser->end_element = parser_end_node_cb; |
|
225 |
parser->m_parser->text = parser_text_cb; |
|
226 |
parser->m_parser->error = parser_error_cb; |
|
227 |
||
228 |
parser->context = g_markup_parse_context_new (parser->m_parser, 0, |
|
229 |
parser, NULL); |
|
230 |
||
231 |
parser->cur_root = NULL; |
|
232 |
parser->cur_node = NULL; |
|
233 |
||
234 |
return parser; |
|
235 |
} |
|
236 |
||
237 |
void |
|
238 |
lm_parser_parse (LmParser *parser, const gchar *string) |
|
239 |
{ |
|
240 |
g_return_if_fail (parser != NULL); |
|
241 |
||
242 |
if (!parser->context) { |
|
243 |
parser->context = g_markup_parse_context_new (parser->m_parser, 0, |
|
244 |
parser, NULL); |
|
245 |
} |
|
246 |
||
247 |
if (!g_utf8_validate (string, -1, NULL)) { |
|
248 |
gchar *tmp; |
|
249 |
||
250 |
/* We can't do much better than guess what charset the string is |
|
251 |
* encoded in... Try ISO-8859-1. |
|
252 |
*/ |
|
253 |
tmp = g_convert (string, -1, |
|
254 |
"UTF-8", "ISO-8859-1", |
|
255 |
NULL, NULL, |
|
256 |
NULL); |
|
257 |
||
258 |
if (tmp) { |
|
259 |
if (g_markup_parse_context_parse (parser->context, tmp, |
|
260 |
strlen (tmp), NULL)) { |
|
261 |
} else { |
|
262 |
g_warning ("Parsing failed\n"); |
|
263 |
} |
|
264 |
||
265 |
g_free (tmp); |
|
266 |
} else { |
|
267 |
g_warning ("Trying to parse a string that is not UTF-8 and not ISO-8859-1."); |
|
268 |
} |
|
269 |
} else { |
|
270 |
if (g_markup_parse_context_parse (parser->context, string, |
|
271 |
strlen (string), NULL)) { |
|
272 |
} else { |
|
273 |
g_warning ("Parsing failed\n"); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
void |
|
279 |
lm_parser_free (LmParser *parser) |
|
280 |
{ |
|
281 |
if (parser->notify) { |
|
282 |
(* parser->notify) (parser->user_data); |
|
283 |
} |
|
284 |
||
285 |
g_free (parser); |
|
286 |
g_print ("Free parser\n"); |
|
287 |
} |
|
288 |