0
|
1 /* BMP - Cross-platform multimedia player
|
|
2 * Copyright (C) 2003-2004 BMP development team.
|
|
3 *
|
|
4 * This program is free software; you can redistribute it and/or modify
|
|
5 * it under the terms of the GNU General Public License as published by
|
|
6 * the Free Software Foundation; either version 2 of the License, or
|
|
7 * (at your option) any later version.
|
|
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 Free Software
|
1459
|
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
0
|
17 */
|
|
18
|
|
19 #include "xml_document.h"
|
|
20
|
|
21 #include <glib.h>
|
|
22 #include <string.h>
|
|
23
|
|
24 /* document builder callbacks */
|
|
25
|
|
26 static void bmp_xml_doc_build_start_element(GMarkupParseContext * context,
|
|
27 const gchar * element_name,
|
|
28 const gchar ** attrib_names,
|
|
29 const gchar ** attrib_values,
|
|
30 gpointer user_data,
|
|
31 GError ** error);
|
|
32
|
|
33 static void bmp_xml_doc_build_end_element(GMarkupParseContext * context,
|
|
34 const gchar * element_name,
|
|
35 gpointer user_data,
|
|
36 GError ** error);
|
|
37
|
|
38 static void bmp_xml_doc_build_text(GMarkupParseContext * context,
|
|
39 const gchar * text,
|
|
40 gsize text_len,
|
|
41 gpointer user_data,
|
|
42 GError ** error);
|
|
43
|
|
44 static void bmp_xml_doc_build_ignore(GMarkupParseContext * context,
|
|
45 const gchar * text,
|
|
46 gsize text_len,
|
|
47 gpointer user_data,
|
|
48 GError ** error);
|
|
49
|
|
50 static void bmp_xml_doc_build_error(GMarkupParseContext * context,
|
|
51 GError * error,
|
|
52 gpointer user_data);
|
|
53
|
|
54 static void bmp_xml_doc_build_destroy(BmpXmlDocument * data);
|
|
55
|
|
56 static GMarkupParser bmp_xml_doc_builder = {
|
|
57 bmp_xml_doc_build_start_element,
|
|
58 bmp_xml_doc_build_end_element,
|
|
59 bmp_xml_doc_build_text,
|
|
60 bmp_xml_doc_build_ignore,
|
|
61 bmp_xml_doc_build_error
|
|
62 };
|
|
63
|
|
64 static GDestroyNotify bmp_xml_node_data_free_func[] = {
|
|
65 (GDestroyNotify) bmp_xml_doc_node_data_free,
|
|
66 (GDestroyNotify) bmp_xml_element_node_data_free,
|
|
67 (GDestroyNotify) bmp_xml_attrib_node_data_free,
|
|
68 (GDestroyNotify) bmp_xml_text_node_data_free
|
|
69 };
|
|
70
|
|
71 GNode *
|
|
72 bmp_xml_doc_node_new(void)
|
|
73 {
|
|
74 BmpXmlDocNodeData *data;
|
|
75 data = g_new0(BmpXmlDocNodeData, 1);
|
|
76 data->type = BMP_XML_NODE_DOC;
|
|
77 return g_node_new(data);
|
|
78 }
|
|
79
|
|
80 void
|
|
81 bmp_xml_doc_node_data_free(BmpXmlDocNodeData * data)
|
|
82 {
|
|
83 g_return_if_fail(data != NULL);
|
|
84 g_free(data);
|
|
85 }
|
|
86
|
|
87 GNode *
|
|
88 bmp_xml_element_node_new(const gchar * name)
|
|
89 {
|
|
90 BmpXmlElementNodeData *data;
|
|
91 data = g_new0(BmpXmlElementNodeData, 1);
|
|
92 data->type = BMP_XML_NODE_ELEMENT;
|
|
93 data->name = g_strdup(name);
|
|
94 return g_node_new(data);
|
|
95 }
|
|
96
|
|
97 void
|
|
98 bmp_xml_element_node_data_free(BmpXmlElementNodeData * data)
|
|
99 {
|
|
100 g_return_if_fail(data != NULL);
|
|
101 g_free(data->name);
|
|
102 g_free(data);
|
|
103 }
|
|
104
|
|
105 GNode *
|
|
106 bmp_xml_attrib_node_new(const gchar * name,
|
|
107 const gchar * value)
|
|
108 {
|
|
109 BmpXmlAttribNodeData *data;
|
|
110 data = g_new0(BmpXmlAttribNodeData, 1);
|
|
111 data->type = BMP_XML_NODE_ATTRIB;
|
|
112 data->name = g_strdup(name);
|
|
113 data->value = g_strdup(value);
|
|
114 return g_node_new(data);
|
|
115 }
|
|
116
|
|
117 void
|
|
118 bmp_xml_attrib_node_data_free(BmpXmlAttribNodeData * data)
|
|
119 {
|
|
120 g_assert(data != NULL);
|
|
121 g_free(data->name);
|
|
122 g_free(data->value);
|
|
123 g_free(data);
|
|
124 }
|
|
125
|
|
126 GNode *
|
|
127 bmp_xml_text_node_new(const gchar * text, gsize length)
|
|
128 {
|
|
129 BmpXmlTextNodeData *data;
|
|
130 data = g_new0(BmpXmlTextNodeData, 1);
|
|
131 data->type = BMP_XML_NODE_TEXT;
|
|
132 data->text = g_new0(gchar, length);
|
|
133 memcpy(data->text, text, length);
|
|
134 data->length = length;
|
|
135 return g_node_new(data);
|
|
136 }
|
|
137
|
|
138 void
|
|
139 bmp_xml_text_node_data_free(BmpXmlTextNodeData * data)
|
|
140 {
|
|
141 g_return_if_fail(data != NULL);
|
|
142 g_free(data->text);
|
|
143 g_free(data);
|
|
144 }
|
|
145
|
|
146 void
|
|
147 bmp_xml_node_data_free(GNode * node)
|
|
148 {
|
|
149 BmpXmlNodeData *data;
|
|
150
|
|
151 g_return_if_fail(node != NULL);
|
|
152 g_return_if_fail(node->data != NULL);
|
|
153
|
|
154 data = BMP_XML_NODE_DATA(node->data);
|
|
155 (*bmp_xml_node_data_free_func[data->type]) (data);
|
|
156 }
|
|
157
|
|
158 BmpXmlDocument *
|
|
159 bmp_xml_document_new(void)
|
|
160 {
|
|
161 BmpXmlDocument *document;
|
|
162
|
|
163 document = g_new0(BmpXmlDocument, 1);
|
|
164
|
|
165 document->parse_context =
|
|
166 g_markup_parse_context_new(&bmp_xml_doc_builder, 0,
|
|
167 document, (GDestroyNotify)
|
|
168 bmp_xml_doc_build_destroy);
|
|
169 document->current_depth = 0;
|
|
170
|
|
171 document->tree = bmp_xml_doc_node_new();
|
|
172 document->current_node = document->tree;
|
|
173
|
|
174 return document;
|
|
175 }
|
|
176
|
|
177 void
|
|
178 bmp_xml_document_free(BmpXmlDocument * document)
|
|
179 {
|
|
180 g_return_if_fail(document != NULL);
|
|
181
|
|
182 g_node_traverse(document->tree, G_IN_ORDER, G_TRAVERSE_ALL, -1,
|
|
183 (GNodeTraverseFunc) bmp_xml_node_data_free, NULL);
|
|
184 g_node_destroy(document->tree);
|
|
185
|
|
186 g_free(document);
|
|
187 }
|
|
188
|
|
189 GNode *
|
|
190 bmp_xml_document_get_tree(BmpXmlDocument * document)
|
|
191 {
|
|
192 return document->tree;
|
|
193 }
|
|
194
|
|
195 gboolean
|
|
196 bmp_xml_document_load(BmpXmlDocument ** document_ref,
|
|
197 const gchar * filename, GError ** error_out)
|
|
198 {
|
|
199 BmpXmlDocument *document;
|
|
200 gchar *buffer;
|
|
201 gsize buffer_size;
|
|
202 GError *error = NULL;
|
|
203
|
|
204 g_assert(document_ref != NULL);
|
|
205 g_assert(filename != NULL);
|
|
206
|
|
207 *document_ref = NULL;
|
|
208
|
|
209 if (!g_file_get_contents(filename, &buffer, &buffer_size, &error)) {
|
|
210 g_propagate_error(error_out, error);
|
|
211 return FALSE;
|
|
212 }
|
|
213
|
|
214 if (!(document = bmp_xml_document_new())) {
|
|
215 g_free(buffer);
|
|
216 return FALSE;
|
|
217 }
|
|
218
|
|
219 if (!g_markup_parse_context_parse(document->parse_context, buffer,
|
|
220 buffer_size, &error)) {
|
|
221 bmp_xml_document_free(document);
|
|
222 g_free(buffer);
|
|
223 g_propagate_error(error_out, error);
|
|
224 return FALSE;
|
|
225 }
|
|
226
|
|
227 g_free(buffer);
|
|
228
|
|
229 if (!g_markup_parse_context_end_parse(document->parse_context, &error)) {
|
|
230 bmp_xml_document_free(document);
|
|
231 g_propagate_error(error_out, error);
|
|
232 return FALSE;
|
|
233 }
|
|
234
|
|
235 *document_ref = document;
|
|
236
|
|
237 return TRUE;
|
|
238 }
|
|
239
|
|
240
|
|
241 static void
|
|
242 bmp_xml_doc_build_start_element(GMarkupParseContext * context,
|
|
243 const gchar * element_name,
|
|
244 const gchar ** attrib_names,
|
|
245 const gchar ** attrib_values,
|
|
246 gpointer user_data,
|
|
247 GError ** error)
|
|
248 {
|
|
249 BmpXmlDocument *document;
|
|
250
|
|
251 document = BMP_XML_DOCUMENT(user_data);
|
|
252
|
|
253 document->current_node =
|
|
254 g_node_append(document->current_node,
|
|
255 bmp_xml_element_node_new(element_name));
|
|
256
|
|
257 while (*attrib_names) {
|
462
|
258 g_print("%s = %s\n", *attrib_names, *attrib_values);
|
0
|
259 g_node_append(document->current_node,
|
|
260 bmp_xml_attrib_node_new(*attrib_names++,
|
|
261 *attrib_values++));
|
|
262 }
|
|
263
|
|
264 document->current_depth++;
|
|
265 }
|
|
266
|
|
267 static void
|
|
268 bmp_xml_doc_build_end_element(GMarkupParseContext * context,
|
|
269 const gchar * element_name,
|
|
270 gpointer user_data,
|
|
271 GError ** error)
|
|
272 {
|
|
273 BmpXmlDocument *document;
|
|
274
|
|
275 document = BMP_XML_DOCUMENT(user_data);
|
|
276 document->current_node = document->current_node->parent;
|
|
277 document->current_depth--;
|
|
278 }
|
|
279
|
|
280 static void
|
|
281 bmp_xml_doc_build_text(GMarkupParseContext * context,
|
|
282 const gchar * text,
|
|
283 gsize text_len,
|
|
284 gpointer user_data,
|
|
285 GError ** error)
|
|
286 {
|
|
287 BmpXmlDocument *document;
|
|
288 document = BMP_XML_DOCUMENT(user_data);
|
|
289 g_node_append(document->current_node,
|
|
290 bmp_xml_text_node_new(text, text_len));
|
|
291 }
|
|
292
|
|
293 static void
|
|
294 bmp_xml_doc_build_ignore(GMarkupParseContext * context,
|
|
295 const gchar * text,
|
|
296 gsize text_len,
|
|
297 gpointer user_data,
|
|
298 GError ** error)
|
|
299 {
|
|
300 }
|
|
301
|
|
302 static void
|
|
303 bmp_xml_doc_build_error(GMarkupParseContext * context,
|
|
304 GError * error,
|
|
305 gpointer user_data)
|
|
306 {
|
|
307 }
|
|
308
|
|
309 static void
|
|
310 bmp_xml_doc_build_destroy(BmpXmlDocument * document)
|
|
311 {
|
|
312 g_markup_parse_context_free(document->parse_context);
|
|
313 }
|