/
XmlSubset.c
171 lines (149 loc) · 3.84 KB
/
XmlSubset.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
#include "XmlSubset.h"
#include "File.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
enum states {
IN_TAG,
WAITING_FOR_TEXT_OR_SUBNODES,
IN_TEXT,
INVALID_XML,
};
static const char *state_labels[] = {
[IN_TAG] = "IN_TAG",
[WAITING_FOR_TEXT_OR_SUBNODES] = "WAITING_FOR_TEXT_OR_SUBNODES",
[IN_TEXT] = "IN_TEXT",
[INVALID_XML] = "INVALID_XML",
};
static int debug = 0;
Node * xml_string_to_nodetree(String *str)
{
/* Read an XML file and generate a "Node *" tree. */
int depth = 0;
int state = WAITING_FOR_TEXT_OR_SUBNODES;
String * tag_buffer = String_new("");
String * text_buffer = String_new("");
if (!str || String_is_none(str)) {
return NULL;
}
/* This top node has an empty-string tag, and no text. It serves as the invisible
top-level "document" node.*/
Node * top = node_new("");
Node * current = top;
int i=0;
while (1) {
int c = String_get_char(str, i++);
if (c == -1) {
break;
}
char ch = c;
// fprintf(stderr, ": %c\n", ch);
if (state == IN_TAG) {
if (ch == '>') {
state = WAITING_FOR_TEXT_OR_SUBNODES;
if (debug) printf("tag: %s\n", s(tag_buffer));
if (String_get_char(tag_buffer, 0) == '/') {
/* Compare tags, close existing node, adjust current */
if (streq(current->tag, s(tag_buffer)+1)) {
depth -=1;
if (debug) printf("-depth=%d\n", depth);
current = current->parent;
}
else {
fprintf(stderr, "tags do not match: %s /%s\n", current->tag, s(tag_buffer)+1);
state = INVALID_XML;
break;
}
}
else {
/* Append new node, adjust current */
current = node_add_new(current, s(tag_buffer));
depth += 1;
if (debug) printf("+depth=%d\n", depth);
if (String_begins_with(tag_buffer, "?xml")) {
/* XML declaration, back up one level. */
current = current->parent;
depth -= 1;
}
}
/* Clear the tag buffer. */
String_set_local(tag_buffer, "");
}
else {
String_append_bytes(tag_buffer, &ch, 1);
}
}
else if (state == WAITING_FOR_TEXT_OR_SUBNODES) {
if (ch == '<') {
state = IN_TAG;
}
else if (!isspace(ch)) {
state = IN_TEXT;
String_append_bytes(text_buffer, &ch, 1);
}
else {
/* isspace(ch) is true, continue along... */
}
}
else if (state == IN_TEXT) {
if (ch == '<') {
node_add(current, Text(s(text_buffer)));
if (debug) printf(" %s\n", s(text_buffer));
String_set_local(text_buffer, "");
state = IN_TAG;
}
else {
String_append_bytes(text_buffer, &ch, 1);
}
}
}
if (state != WAITING_FOR_TEXT_OR_SUBNODES || depth != 0) {
fprintf(stderr, "XML error (state=%d(%s) depth=%d)\n",
state, state_labels[state], depth);
/* FIXME: Clean up node tree... */
top = NULL;
}
return top;
}
Node * xml_file_to_nodetree(String * filename)
{
String * contents = File_load_text(filename);
Node * nt = xml_string_to_nodetree(contents);
String_free(&contents);
return nt;
}
int xml_simple_node(Node *node)
{
/* Return 1 if node represents the common case of <tag>value</tag> */
if (node
&& node->tag
&& node->subnodes_count == 1
&& node->subnodes[0]->text) {
return 1;
}
else {
return 0;
}
}
String * xml_simple_node_value(Node *node)
{
/* Return "value" for the common-case of <tag>value</tag> */
if (!xml_simple_node(node)) {
return String_value_none();
}
else {
return String_new(node->subnodes[0]->text);
}
}
#ifdef TEST
int main(int argc, char *argv[])
{
debug = 1;
Node * n;
int i;
if (argc > 1) {
n = xml_file_to_nodetree(S(argv[1]));
}
return 0;
}
#endif