forked from AravisProject/aravis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharvdomparser.c
391 lines (311 loc) · 9.74 KB
/
arvdomparser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Aravis - Digital camera library
*
* Copyright © 2009-2022 Emmanuel Pacaud
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author:
* Emmanuel Pacaud <[email protected]>
*/
#include <arvdebugprivate.h>
#include <arvdomimplementation.h>
#include <arvdomnode.h>
#include <arvdomelement.h>
#include <arvdomparser.h>
#include <arvstr.h>
#include <libxml/parser.h>
#include <gio/gio.h>
#include <string.h>
typedef enum {
STATE
} ArvDomSaxParserStateEnum;
typedef struct {
ArvDomSaxParserStateEnum state;
ArvDomDocument *document;
ArvDomNode *current_node;
gboolean is_error;
int error_depth;
GHashTable *entities;
} ArvDomSaxParserState;
static void
_free_entity (void *data)
{
xmlEntity *entity = data;
xmlFree ((xmlChar *) entity->name);
xmlFree ((xmlChar *) entity->ExternalID);
xmlFree ((xmlChar *) entity->SystemID);
xmlFree (entity->content);
xmlFree (entity->orig);
g_free (entity);
}
static void
arv_dom_parser_start_document (void *user_data)
{
ArvDomSaxParserState *state = user_data;
state->state = STATE;
state->is_error = FALSE;
state->error_depth = 0;
state->entities = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, _free_entity);
}
static void
arv_dom_parser_end_document (void *user_data)
{
ArvDomSaxParserState *state = user_data;
g_hash_table_unref (state->entities);
}
static void
arv_dom_parser_start_element(void *user_data,
const xmlChar *name,
const xmlChar **attrs)
{
ArvDomSaxParserState *state = user_data;
ArvDomNode *node;
int i;
if (state->is_error) {
state->error_depth++;
return;
}
if (state->document == NULL) {
state->document = arv_dom_implementation_create_document (NULL, (char *) name);
state->current_node = ARV_DOM_NODE (state->document);
g_return_if_fail (ARV_IS_DOM_DOCUMENT (state->document));
}
node = ARV_DOM_NODE (arv_dom_document_create_element (ARV_DOM_DOCUMENT (state->document), (char *) name));
if (ARV_IS_DOM_NODE (node) && arv_dom_node_append_child (state->current_node, node) != NULL) {
if (attrs != NULL)
for (i = 0; attrs[i] != NULL && attrs[i+1] != NULL; i += 2)
arv_dom_element_set_attribute (ARV_DOM_ELEMENT (node),
(char *) attrs[i],
(char *) attrs[i+1]);
state->current_node = node;
state->is_error = FALSE;
state->error_depth = 0;
} else {
state->is_error = TRUE;
state->error_depth = 1;
}
}
static void
arv_dom_parser_end_element (void *user_data,
const xmlChar *name)
{
ArvDomSaxParserState *state = user_data;
if (state->is_error) {
state->error_depth--;
if (state->error_depth > 0) {
return;
}
state->is_error = FALSE;
return;
}
state->current_node = arv_dom_node_get_parent_node (state->current_node);
}
static void
arv_dom_parser_characters (void *user_data, const xmlChar *ch, int len)
{
ArvDomSaxParserState *state = user_data;
if (!state->is_error) {
ArvDomNode *node;
char *text;
text = g_strndup ((char *) ch, len);
node = ARV_DOM_NODE (arv_dom_document_create_text_node (ARV_DOM_DOCUMENT (state->document), text));
arv_dom_node_append_child (state->current_node, node);
g_free (text);
}
}
static void arv_dom_parser_warning (void *user_data, const char *msg, ...) G_GNUC_PRINTF(2,3);
static void arv_dom_parser_error (void *user_data, const char *msg, ...) G_GNUC_PRINTF(2,3);
static void arv_dom_parser_fatal_error (void *user_data, const char *msg, ...) G_GNUC_PRINTF(2,3);
static void
arv_dom_parser_warning (void *user_data, const char *msg, ...)
{
va_list args;
char *message;
va_start(args, msg);
message = g_strdup_vprintf (msg, args);
arv_warning (ARV_DEBUG_CATEGORY_DOM, "[DomParser::parse] %s", message);
g_free (message);
va_end(args);
}
static void
arv_dom_parser_error (void *user_data, const char *msg, ...)
{
va_list args;
char *message;
va_start(args, msg);
message = g_strdup_vprintf (msg, args);
arv_warning (ARV_DEBUG_CATEGORY_DOM, "[DomParser::parse] %s", message);
g_free (message);
va_end(args);
}
static void
arv_dom_parser_fatal_error (void *user_data, const char *msg, ...)
{
va_list args;
char *message;
va_start(args, msg);
message = g_strdup_vprintf (msg, args);
arv_warning (ARV_DEBUG_CATEGORY_DOM, "[DomParser::parse] %s", message);
g_free (message);
va_end(args);
}
static xmlSAXHandler sax_handler = {
.warning = arv_dom_parser_warning,
.error = arv_dom_parser_error,
.fatalError = arv_dom_parser_fatal_error,
.startDocument = arv_dom_parser_start_document,
.endDocument = arv_dom_parser_end_document,
.startElement = arv_dom_parser_start_element,
.endElement = arv_dom_parser_end_element,
.characters = arv_dom_parser_characters
};
static GQuark
arv_dom_document_error_quark (void)
{
return g_quark_from_static_string ("arv-dom-error-quark");
}
#define ARV_DOM_DOCUMENT_ERROR arv_dom_document_error_quark ()
typedef enum {
ARV_DOM_DOCUMENT_ERROR_INVALID_XML
} ArvDomDocumentError;
#if LIBXML_VERSION >= 21100
static ArvDomDocument *
_parse_memory (ArvDomDocument *document, ArvDomNode *node,
const void *buffer, int size, GError **error)
{
static ArvDomSaxParserState state;
xmlParserCtxt *xml_parser_ctxt;
state.document = document;
if (node != NULL)
state.current_node = node;
else
state.current_node = ARV_DOM_NODE (document);
if (size < 0)
size = strlen (buffer);
xml_parser_ctxt = xmlNewSAXParserCtxt (&sax_handler, &state);
if (xml_parser_ctxt == NULL) {
g_set_error (error,
ARV_DOM_DOCUMENT_ERROR,
ARV_DOM_DOCUMENT_ERROR_INVALID_XML,
"Failed to create parser context");
return NULL;
}
xmlCtxtReadMemory (xml_parser_ctxt, buffer, size, NULL, NULL, 0);
if (!xml_parser_ctxt->wellFormed) {
if (state.document != NULL)
g_object_unref (state.document);
state.document = NULL;
arv_warning_dom ("[DomParser::parse] Invalid document");
g_set_error (error,
ARV_DOM_DOCUMENT_ERROR,
ARV_DOM_DOCUMENT_ERROR_INVALID_XML,
"Invalid document");
}
xmlFreeParserCtxt(xml_parser_ctxt);
return state.document;
}
#else
static ArvDomDocument *
_parse_memory (ArvDomDocument *document, ArvDomNode *node,
const void *buffer, int size, GError **error)
{
static ArvDomSaxParserState state;
state.document = document;
if (node != NULL)
state.current_node = node;
else
state.current_node = ARV_DOM_NODE (document);
if (size < 0)
size = strlen (buffer);
if (xmlSAXUserParseMemory (&sax_handler, &state, buffer, size) < 0) {
if (state.document != NULL)
g_object_unref (state.document);
state.document = NULL;
arv_warning_dom ("[ArvDomParser::from_memory] Invalid document");
g_set_error (error,
ARV_DOM_DOCUMENT_ERROR,
ARV_DOM_DOCUMENT_ERROR_INVALID_XML,
"Invalid document");
}
return state.document;
}
#endif
/**
* arv_dom_document_append_from_memory:
* @document: a #ArvDomDocument
* @node: a #ArvDomNode
* @buffer: a memory buffer holding xml data
* @size: size of the xml data, in bytes
* @error: an error placeholder
*
* Append a chunk of xml tree to an existing document. The resulting nodes will be appended to
* @node, or to @document if @node == NULL.
*
* Size set to a negative value indicated an unknow xml data size.
*/
void
arv_dom_document_append_from_memory (ArvDomDocument *document, ArvDomNode *node,
const void *buffer, int size, GError **error)
{
g_return_if_fail (ARV_IS_DOM_DOCUMENT (document));
g_return_if_fail (ARV_IS_DOM_NODE (node) || node == NULL);
g_return_if_fail (buffer != NULL);
_parse_memory (document, node, buffer, size, error);
}
ArvDomDocument *
arv_dom_document_new_from_memory (const void *buffer, int size, GError **error)
{
g_return_val_if_fail (buffer != NULL, NULL);
return _parse_memory (NULL, NULL, buffer, size, error);
}
static ArvDomDocument *
arv_dom_document_new_from_file (GFile *file, GError **error)
{
ArvDomDocument *document;
gsize size = 0;
char *contents = NULL;
if (!g_file_load_contents (file, NULL, &contents, &size, NULL, error))
return NULL;
document = arv_dom_document_new_from_memory (contents, size, error);
g_free (contents);
return document;
}
ArvDomDocument *
arv_dom_document_new_from_path (const char *path, GError **error)
{
ArvDomDocument *document;
GFile *file;
g_return_val_if_fail (path != NULL, NULL);
file = g_file_new_for_path (path);
document = arv_dom_document_new_from_file (file, error);
g_object_unref (file);
if (document != NULL)
arv_dom_document_set_path (document, path);
return document;
}
ArvDomDocument *
arv_dom_document_new_from_url (const char *url, GError **error)
{
ArvDomDocument *document;
GFile *file;
g_return_val_if_fail (url != NULL, NULL);
file = g_file_new_for_uri (url);
document = arv_dom_document_new_from_file (file, error);
g_object_unref (file);
if (document != NULL)
arv_dom_document_set_url (document, url);
return document;
}