-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml.c
389 lines (336 loc) · 12 KB
/
xml.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xml_lexer.h"
#include "xml_parser.h"
#include "xpath.h"
#ifdef LEX_DEBUG
/* read entire file, and return contents. */
static char *read_file(const char *filename) {
FILE *fp = NULL;
size_t size_to_read = 0;
size_t size_read = 0;
long pos = 0;
char *file_contents = NULL;
fp = fopen(filename, "r");
if (!fp) return NULL;
fseek(fp, 0L, SEEK_END);
pos = ftell(fp);
if (pos < 0) {
fclose(fp);
return NULL;
}
size_to_read = pos;
rewind(fp);
file_contents = (char *)malloc(sizeof(char) * (size_to_read + 1));
if (!file_contents) {
fclose(fp);
return NULL;
}
size_read = fread(file_contents, 1, size_to_read, fp);
if (size_read == 0 || ferror(fp)) {
fclose(fp);
free(file_contents);
return NULL;
}
fclose(fp);
file_contents[size_read] = '\0';
return file_contents;
}
#endif
static bool NodeNameIsField(XMLNode *node, int idx, void *user_data) {
return strncmp(node->name, "field", 5) == 0;
}
static XMLNodeList* AgeGreaterEqualThan48(XMLNode *node, int idx, void *user_data) {
XMLNodeList *list = malloc(sizeof(XMLNodeList));
if (list == NULL) return NULL;
XMLNodeListInit(list);
char *age = (char *)user_data;
//for (size_t i = 0; i < node->children.count; ++i) {
for (size_t i = 0; i < XMLNodeChildrenCount(node); ++i) { //same as above 'for'
//XMLNode *child = node->children.nodes[i];
XMLNode *child = XMLNodeChildrenGet(node, i); //same as above
if (strncmp(child->name, age, strlen(age)) == 0) {
if (atoi(child->text) >= 48) {
XMLNodeListAdd(list, child);
}
}
}
return list;
}
/* Select 'food' node which price is greater than 5 */
static XMLNodeList *PriceGreaterThanFiveFood(XMLNode *node, int idx, void *user_data) {
XMLNodeList *list = malloc(sizeof(XMLNodeList));
if (list == NULL) return NULL;
XMLNodeListInit(list);
char *price = (char *)user_data;
for (size_t i = 0; i < node->children.count; ++i) {
XMLNode *child = node->children.nodes[i];
if (strncmp(child->name, price, strlen(price)) == 0) {
if (atof(child->text+1) >= 5.0) { //+1: skip the '$'
XMLNodeListAdd(list, child->parent);
}
}
}
return list;
}
/* Testing for 'simple.xml' file */
void Test_simplexml(XMLDocument *doc) {
fprintf(stdout, "\n============Search Node with Selector ============\n");
XMLNodeList *list = XMLFindNodeSelector(XML_ROOT(doc), PriceGreaterThanFiveFood, (void *)"price");
if (list != NULL) {
fprintf(stdout, "food count=[%ld]\n", list->count);
for (size_t i = 0; i < list->count; ++i) {
XMLNode *food_node = list->nodes[i];
fprintf(stdout, "MATCHES [%ld]:\n", i);
for (size_t j = 0; j < food_node->children.count; ++j) {
XMLNode *child = food_node->children.nodes[j];
if (child->type == NT_COMMENT) continue;
fprintf(stdout, "\t %s: %s\n", child->name, XMLDecodeText(child));
}
}
free(list);
}
}
/* Testing for 'test2.xml' file */
void Test_test2xml(XMLDocument *doc) {
XMLNode *root = XML_ROOT(doc);
XMLNode *n2 = XMLSelectNode(root, "/h:table/h:tr[3]/h:td[2]");
if (n2 != NULL) printf("n2.text=%s\n", n2->text);
XMLNode *n3 = XMLSelectNode(root, "/h:table/h:tr[-1]/h:td[2]");
if (n3 != NULL) printf("n3.text=%s\n", n3->text); //should be the same result with above code.
}
/* Testing 'test.xml' file */
void Test_testxml(XMLDocument *doc) {
XMLNode *root = XML_ROOT(doc);
fprintf(stdout, "\n============Search Node============\n");
XMLNode *m = XMLSelectNode(root, "/struct/fields");
if (m) {
XMLNodeList *list = XMLFindNode(m, "field");
if (list) {
fprintf(stdout, "list count=[%ld]\n", list->count);
for (size_t i = 0; i < list->count; ++i) {
XMLNode *node = list->nodes[i];
for (size_t j = 0; j < node->attrList.count; ++j) {
XMLAttr attr = node->attrList.attrs[j];
fprintf(stdout, "\t%s => %s\n", attr.key, attr.value);
}
}
free(list);
}
}
fprintf(stdout, "\n============Search Node with Predicate============\n");
XMLNodeList *list = XMLFindNodeWhere(m, NodeNameIsField, NULL);
if (list) {
fprintf(stdout, "list count=[%ld]\n", list->count);
for (size_t i = 0; i < list->count; ++i) {
XMLNode *node = list->nodes[i];
for (size_t j = 0; j < node->attrList.count; ++j) {
XMLAttr attr = node->attrList.attrs[j];
fprintf(stdout, "\t%s => %s\n", attr.key, attr.value);
}
}
free(list);
}
fprintf(stdout, "\n============Search Node with Selector============\n");
XMLNode *m2 = XMLSelectNode(root, "/struct/fields");
if (m) {
XMLNodeList *list = XMLFindNodeSelector(m2, AgeGreaterEqualThan48, (void*)"age");
if (list) {
fprintf(stdout, "list count=[%ld]\n", list->count);
for (size_t i = 0; i < list->count; ++i) {
XMLNode *node = list->nodes[i];
fprintf(stdout, "name: %s, age: %s\n", node->name, node->text);
}
free(list);
}
}
fprintf(stdout, "\n============XMLSelectNode============\n");
XMLNode *n = XMLSelectNode(root, "/struct/fields/field[1]/name");
if (n != NULL) printf("n.text=%s\n", n->text);
XMLNode *n2 = XMLSelectNode(root, "/struct/fields/field[2]/age");
if (n2 != NULL) printf("n2.text=%s\n", n2->text);
}
static XMLNodeList* SelectAllPricesNodes(XMLNode *node, int idx, void *user_data) {
XMLNodeList *list = malloc(sizeof(XMLNodeList));
if (list == NULL) return NULL;
XMLNodeListInit(list);
char *price = (char *)user_data;
for (size_t i = 0; i < node->children.count; ++i) {
XMLNode *child = node->children.nodes[i];
if (strncmp(child->name, price, strlen(price)) == 0) {
XMLNodeListAdd(list, child);
}
}
return list;
}
static XMLNodeList* SelectPriceGreaterThan35(XMLNode *node, int idx, void *user_data) {
XMLNodeList *list = malloc(sizeof(XMLNodeList));
if (list == NULL) return NULL;
XMLNodeListInit(list);
char *price = (char *)user_data;
for (size_t i = 0; i < node->children.count; ++i) {
XMLNode *child = node->children.nodes[i];
if (strncmp(child->name, price, strlen(price)) == 0) {
if (atof(child->text) > 35.0) XMLNodeListAdd(list, child);
}
}
return list;
}
static XMLNodeList* SelectTitleWithPriceGreaterThan35(XMLNode *node, int idx, void *user_data) {
XMLNodeList *list = malloc(sizeof(XMLNodeList));
if (list == NULL) return NULL;
XMLNodeListInit(list);
char *price = (char *)user_data;
XMLNode *title = NULL;
for (size_t i = 0; i < node->children.count; ++i) {
XMLNode *child = node->children.nodes[i];
if (strncmp(child->name, "title", 5) == 0) {
title = child;
continue;
}
if (strncmp(child->name, price, strlen(price)) == 0) {
if (atof(child->text) > 35.0) {
XMLNodeListAdd(list, title);
}
}
}
return list;
}
static void Test_test4xml(XMLDocument *doc) {
XMLNode *root = XML_ROOT(doc);
/* Select the title of the first book */
fprintf(stdout, "\n============Select the title of the first book============\n");
XMLNode *n1 = XMLSelectNode(root, "/bookstore/book[1]/title");
if (n1) {
printf("title=%s\n", n1->text);
}
fprintf(stdout, "\n============Select all the prices============\n");
XMLNodeList *list = XMLFindNodeSelector(root, SelectAllPricesNodes, (void *)"price");
if (list != NULL) {
fprintf(stdout, "prices count=[%ld]\n", list->count);
for (size_t i = 0; i < list->count; ++i) {
XMLNode *price = list->nodes[i];
fprintf(stdout, "%s\n", price->text);
}
free(list);
}
fprintf(stdout, "\n============Select price nodes with price>35============\n");
XMLNodeList *list2 = XMLFindNodeSelector(root, SelectPriceGreaterThan35, (void *)"price");
if (list2 != NULL) {
fprintf(stdout, "prices greater than 35 count=[%ld]\n", list2->count);
for (size_t i = 0; i < list2->count; ++i) {
XMLNode *price = list2->nodes[i];
fprintf(stdout, "%s\n", price->text);
}
free(list2);
}
fprintf(stdout, "\n============Select title nodes with price>35============\n");
XMLNodeList *list3 = XMLFindNodeSelector(root, SelectTitleWithPriceGreaterThan35, (void *)"price");
if (list3 != NULL) {
fprintf(stdout, "title nodes with prices greater than 35 count=[%ld]\n", list3->count);
for (size_t i = 0; i < list3->count; ++i) {
XMLNode *title = list3->nodes[i];
fprintf(stdout, "%s\n", title->text);
}
free(list3);
}
fprintf(stdout, "\n============Test XMLNodeNextSibling============\n");
XMLNode *author_node = XMLSelectNode(root, "/bookstore/book[3]/author"); //returns the third book's first author node
printf("node %s=%s\n", author_node->name, author_node->text);
XMLNode *node = NULL;
for (node = XMLNodeNextSibling(author_node); node; node = XMLNodeNextSibling(node)) { //print sibling node
printf("node %s=%s\n", node->name, node->text);
}
}
static void Test_cdataxml(XMLDocument *doc) {
XMLNode *root = XML_ROOT(doc);
XMLNode *m = XMLSelectNode(root, "/address/description");
if (m) {
fprintf(stdout, "description=%s\n", XMLDecodeText(m));
}
}
static void Test_doctypexml(XMLDocument *doc) {
XMLNode *root = XML_ROOT(doc);
XMLNode *m = XMLSelectNode(root, "/address/description");
if (m) {
fprintf(stdout, "description=%s\n", XMLDecodeText(m));
}
}
static void xpath_test(void) {
XMLDocument doc = { 0 };
bool result = XMLDocumentParseFile(&doc, "./bookstore.xml");
if (result != true) {
fprintf(stderr, "XMLDocumentParseFile failed!\n");
exit(1);
}
XPathResult result1 = xpath("/bookstore/book[@category=CHILDREN]//text()", doc.root);
printf("xpath result = %s\n", result1.text);
XPathResult result2 = xpath("/bookstore/book/title/../price/text()", doc.root);
printf("xpath result2 = %s\n", result2.text);
XPathResult result3 = xpath("/bookstore/book[1]/title/text()", doc.root);
printf("xpath result3 = %s\n", result3.text);
XPathResult result4 = xpath("/bookstore/book[@category=CHILDREN]/year", doc.root);
printf("xpath result4 = %s\n", result4.node->text);
/* below two lines are same as above */
//XPathResult result4 = xpath("/bookstore/book[@category=CHILDREN]/year/text()", root);
//printf("xpath result4 = %s\n", result4.text);
XPathResult result5 = xpath("/bookstore/book[1]//title", doc.root);
for (size_t i = 0; i < result5.nodes.count; ++i) {
XMLNode *child = result5.nodes.nodes[i];
printf("xpath result5 = %s\n", child->text);
}
//if the xpath result is a nodelist, we need to use 'xpath_free' to free the memory
xpath_free(&result5);
XMLDocumentFree(&doc);
}
int main(int argc, char **argv) {
char *filename = "./test.xml";
#ifdef LEX_DEBUG
char *contents = read_file(filename);
lexer_t lexer;
lexer_init(&lexer, contents, filename);
/* get next two tokens, so we have two positions */
lexer_next_token(&lexer);
lexer_next_token(&lexer);
while (!lexer_cur_token_is(&lexer, TOKEN_EOF)) {
#ifdef DEBUG
token_dump(lexer.cur_token);
#endif
lexer_next_token(&lexer);
} /* end while */
#else
if (argc == 2) filename = argv[1];
#endif
/* open xml file & parser it */
XMLDocument doc = { 0 };
bool result = XMLDocumentParseFile(&doc, filename);
if (result != true) {
fprintf(stderr, "XMLDocumentParseFile failed!\n");
exit(1);
}
fprintf(stdout, "\n============Pretty Print============\n");
XMLPrettyPrint(&doc, NULL, 4); /* output to console */
/* Output to file */
//FILE *out = fopen("./log.txt", "w");
//XMLPrettyPrint(&doc, out, 4);
//fclose(out);
if (strcmp(filename, "./test.xml") == 0) {
Test_testxml(&doc);
} else if (strcmp(filename, "./simple.xml") == 0) {
Test_simplexml(&doc);
} else if (strcmp(filename, "./test2.xml") == 0) {
Test_test2xml(&doc);
} else if (strcmp(filename, "./test4.xml") == 0) {
Test_test4xml(&doc);
} else if (strcmp(filename, "./cdata.xml") == 0) {
Test_cdataxml(&doc);
} else if (strcmp(filename, "./doctype.xml") == 0) {
Test_doctypexml(&doc);
}
XMLDocumentFree(&doc);
/* XPATH TEST */
fprintf(stdout, "\n\n============XPATH============\n");
xpath_test();
return 0;
}