-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlparser.c
110 lines (96 loc) · 2.4 KB
/
xmlparser.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
#include <stdio.h>
#include <libxml/xmlreader.h>
#include "xmlparser.h"
int
parse_xml(xmlTextReaderPtr rd, const xml_schema * schema)
{
xmlReaderTypes ev_type;
int ret;
state_t state = schema->start;
char fake_close = 0;
uint8_t *token;
int len;
state_t *trans;
parser_ctx ctx = { rd };
const xmlChar *localname;
read:
if (state == 0)
goto error;
if (fake_close) {
fake_close = 0;
ev_type = XML_READER_TYPE_END_ELEMENT;
} else {
if ((ret = xmlTextReaderRead(rd)) != 1)
goto error;
ev_type = xmlTextReaderNodeType(rd);
}
len = 1;
trans = schema->target_list + schema->target_offset[state];
switch (ev_type) {
case XML_READER_TYPE_ELEMENT:
fake_close = xmlTextReaderIsEmptyElement(rd);
localname = xmlTextReaderConstLocalName(rd);
++ trans; // skip CLOSE transition
token = schema->token_list + schema->token_offset[state];
len = schema->token_length[state];
printf("OPEN '%s' ", localname);
while (len > 0) {
switch (schema->tokens[*token].type) {
case 0:
// printf("Testing '%s' for match with element name %s\n", schema->tokens[*token].localname, localname);
if (xmlStrcmp(BAD_CAST (schema->tokens[*token].localname),
localname) == 0)
goto match;
break;
case 1:
printf("Checking subgraph\n");
if (xmlStrcmp(BAD_CAST (schema->tokens[*token].localname),
localname) == 0)
{
goto match;
}
break;
case 2:
break;
default: {}
}
++token, --len, ++trans;
}
match:
break;
case XML_READER_TYPE_END_ELEMENT:
printf("CLOSE '%s'", xmlTextReaderConstLocalName(rd));
break;
case XML_READER_TYPE_TEXT:
case XML_READER_TYPE_CDATA:
printf("TEXT '%s'\n", xmlTextReaderConstValue(rd));
default: goto read;
}
printf(" -> %d / %d%s\n", *trans, trans - schema->target_list, (len == 0)? " (default)": "");
state = *trans;
schema->dispatch(trans - schema->target_list, &ctx);
goto read;
error:
/* return 0 if end of document encountered and no more
* data expected
*/
return !(ret == 0 && state >= schema->first_final);
}
extern xml_schema test_schema;
int
main(int argc, char **argv)
{
xmlTextReaderPtr reader;
if (argc != 2)
return(1);
LIBXML_TEST_VERSION
reader = xmlReaderForFile(argv[1], NULL, 0);
if (reader == NULL) {
fprintf(stderr, "Unable to open %s\n", argv[1]);
return (1);
}
parse_xml(reader, &test_schema);
xmlCleanupParser();
xmlMemoryDump();
return(0);
}