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
|
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
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) {
|
|
258 g_node_append(document->current_node,
|
|
259 bmp_xml_attrib_node_new(*attrib_names++,
|
|
260 *attrib_values++));
|
|
261 }
|
|
262
|
|
263 document->current_depth++;
|
|
264 }
|
|
265
|
|
266 static void
|
|
267 bmp_xml_doc_build_end_element(GMarkupParseContext * context,
|
|
268 const gchar * element_name,
|
|
269 gpointer user_data,
|
|
270 GError ** error)
|
|
271 {
|
|
272 BmpXmlDocument *document;
|
|
273
|
|
274 document = BMP_XML_DOCUMENT(user_data);
|
|
275 document->current_node = document->current_node->parent;
|
|
276 document->current_depth--;
|
|
277 }
|
|
278
|
|
279 static void
|
|
280 bmp_xml_doc_build_text(GMarkupParseContext * context,
|
|
281 const gchar * text,
|
|
282 gsize text_len,
|
|
283 gpointer user_data,
|
|
284 GError ** error)
|
|
285 {
|
|
286 BmpXmlDocument *document;
|
|
287 document = BMP_XML_DOCUMENT(user_data);
|
|
288 g_node_append(document->current_node,
|
|
289 bmp_xml_text_node_new(text, text_len));
|
|
290 }
|
|
291
|
|
292 static void
|
|
293 bmp_xml_doc_build_ignore(GMarkupParseContext * context,
|
|
294 const gchar * text,
|
|
295 gsize text_len,
|
|
296 gpointer user_data,
|
|
297 GError ** error)
|
|
298 {
|
|
299 }
|
|
300
|
|
301 static void
|
|
302 bmp_xml_doc_build_error(GMarkupParseContext * context,
|
|
303 GError * error,
|
|
304 gpointer user_data)
|
|
305 {
|
|
306 }
|
|
307
|
|
308 static void
|
|
309 bmp_xml_doc_build_destroy(BmpXmlDocument * document)
|
|
310 {
|
|
311 g_markup_parse_context_free(document->parse_context);
|
|
312 }
|