-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
390 lines (323 loc) · 14.9 KB
/
main.cpp
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
/*
This is an example source code and a regression test program for the TinyXPath project
We load a very small test.xml file and test the return value of the string variant of
TinyXPath against a known output.
The LIBXML_CHECK define may be turned ON if we need to verify the output against libxml
(from the Gnome project).
*/
#include <stdexcept>
#include "htmlutil.h"
#include "xpath_static.h"
using namespace std;
using namespace tinyxml2;
/*
@history:
Modified on 18 December 2006 by Aman Aggarwal
::Added test cases for translate()
*/
static FILE* Fp_out_html;
// #define LIBXML_CHECK
static void v_out_one_line(const char* cp_expr, const char* cp_res, const char* cp_expected, bool o_ok) {
printf("%s ", cp_expr);
fprintf(Fp_out_html, "<tr><td>%s</td>", cp_expr);
if (o_ok) {
printf("PASS %s %s\n", cp_res, cp_expected);
fprintf(Fp_out_html, "<td>%s</td><td>%s</td></tr>\n", cp_res, cp_expected);
} else {
printf("FAIL %s %s\n", cp_res, cp_expected);
fprintf(Fp_out_html, "<td><em>%s</em></td><td><em>%s</em></td></tr>\n", cp_res, cp_expected);
}
}
#ifdef LIBXML_CHECK
#include "libxml/tree.h"
#include "libxml/xpath.h"
/// Return the text result (if any) of the xpath expression
string S_xpath_expr(const xmlDoc* Dp_ptr, const char* cp_xpath_expr) {
xmlXPathObjectPtr XPOp_ptr;
xmlXPathContextPtr XPCp_ptr;
const xmlChar* Cp_ptr;
string S_out;
S_out = "";
if (Dp_ptr) {
XPCp_ptr = xmlXPathNewContext((xmlDoc*)Dp_ptr);
if (XPCp_ptr) {
// Evaluate
XPOp_ptr = xmlXPathEval((const xmlChar*)cp_xpath_expr, XPCp_ptr);
if (XPOp_ptr) {
Cp_ptr = xmlXPathCastToString(XPOp_ptr);
if (Cp_ptr)
S_out = (const char*)Cp_ptr;
xmlXPathFreeObject(XPOp_ptr);
}
}
if (XPCp_ptr)
xmlXPathFreeContext(XPCp_ptr);
}
return S_out;
}
static void v_test_one_string_libxml(const XMLNode* XNp_root, const char* cp_expr, const xmlDoc* Dp_ptr) {
string S_expected, S_res;
bool o_ok;
S_expected = S_xpath_expr(Dp_ptr, cp_expr);
printf("-- expr : [%s] --\n", cp_expr);
TinyXPath::xpath_processor xp_proc(XNp_root, cp_expr);
S_res = xp_proc.S_compute_xpath();
o_ok = strcmp(S_res.c_str(), S_expected.c_str()) == 0;
v_out_one_line(cp_expr, S_res.c_str(), S_expected.c_str(), o_ok);
}
#endif
static void v_test_one_string_tiny(const XMLNode* XNp_root, const char* cp_expr, const char* cp_expected) {
string S_res;
bool o_ok;
printf("-- expr : [%s] --\n", cp_expr);
S_res = TinyXPath::S_xpath_string(XNp_root, cp_expr);
o_ok = strcmp(S_res.c_str(), cp_expected) == 0;
v_out_one_line(cp_expr, S_res.c_str(), cp_expected, o_ok);
if (!o_ok)
throw std::runtime_error("Test [" + std::string(cp_expr) + "] failed");
}
#ifdef LIBXML_CHECK
#define v_test_one_string(x, y, z) v_test_one_string_libxml(x, y, Dp_doc)
#else
#define v_test_one_string v_test_one_string_tiny
#endif
int main() {
XMLDocument* XDp_doc;
XMLElement* XEp_main;
int i_res;
char ca_res[80];
bool o_ok, o_res;
double d_out;
string S_res;
XDp_doc = new XMLDocument;
if (XDp_doc->LoadFile("openoff.xml") == XML_SUCCESS) {
S_res = TinyXPath::S_xpath_string(
XDp_doc->RootElement(), "/office:document-content/office:body/office:text/text:p/text()");
S_res = TinyXPath::S_xpath_string(XDp_doc->RootElement(), "//text:sequence-decls/*[1]/@text:name");
}
delete XDp_doc;
XDp_doc = new XMLDocument;
if (XDp_doc->LoadFile("test.xml") != XML_SUCCESS) {
printf("Can't load test.xml !\n");
return 99;
}
#ifdef LIBXML_CHECK
xmlDoc* Dp_doc;
Dp_doc = xmlParseFile("test.xml");
#endif
Fp_out_html = fopen("out.htm", "wt");
if (!Fp_out_html)
return 1;
fprintf(Fp_out_html, "<html><head><title>Result</title>\n<style>");
fprintf(Fp_out_html, "em{color: red;}</style>\n");
fprintf(Fp_out_html, "</head><body>\n");
fprintf(Fp_out_html, "<h1>TinyXPath examples / regression tests</h1>\n");
fprintf(Fp_out_html, "<h2>Input XML tree</h2>\n");
v_out_html(Fp_out_html, XDp_doc, 0);
fprintf(Fp_out_html, "<br /><br />\n");
fprintf(Fp_out_html, "<table border='1'><tr><th>Expression</th><th>Result</th><th>Expected (%s)</th></tr>\n",
#ifdef LIBXML_CHECK
"libXML"
#else
"compiled"
#endif
);
XEp_main = XDp_doc->RootElement();
fprintf(Fp_out_html, "<h2>Results</h2>\n");
XMLElement* XEp_sub = XEp_main->FirstChildElement("b");
v_test_one_string(XEp_main, "/a/*[name()!='b']", "x");
v_test_one_string(XEp_main, "//b/@val", "123");
v_test_one_string(XEp_main, "//x/text()", "sub text");
v_test_one_string(XEp_main, "//*/comment()", " -122.0 ");
v_test_one_string(XEp_main, "( //dummy1 or //dummy2 or /dummy/dummy2 or /a/b )", "true");
v_test_one_string(XEp_main, "( //dummy1 or //dummy2 or /dummy/dummy2 )", "false");
v_test_one_string(XEp_main, "translate('aabbccdd','aaabc','AXYB')", "AABBdd");
v_test_one_string(XEp_main, "translate('aabbccdde','abcd','')", "e");
v_test_one_string(XEp_main, "translate('aabbccdd','','ASDF')", "aabbccdd");
v_test_one_string(XEp_main, "translate('aabbccdd','','')", "aabbccdd");
v_test_one_string(XEp_main,
"translate('The quick brown fox jumps over the lazy "
"dog','abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')",
"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");
v_test_one_string(XEp_main, "count(//*/comment())", "2");
v_test_one_string(XEp_main, "sum(//@*)", "123");
v_test_one_string(XEp_main, "sum(//*/comment())", "378");
v_test_one_string(XEp_main, "true()", "true");
v_test_one_string(XEp_main, "not(false())", "true");
v_test_one_string(XEp_main, "count(//*[position()=2])", "2");
v_test_one_string(XEp_main, "name(/*/*/*[position()=2])", "c");
v_test_one_string(XEp_main, "name(/*/*/*[last()])", "d");
v_test_one_string(XEp_main, "count(//c/following::*)", "2");
v_test_one_string(XEp_main, "count(/a/b/b/following::*)", "3");
v_test_one_string(XEp_main, "count(//d/preceding::*)", "2");
v_test_one_string(XEp_main, "name(//attribute::*)", "val");
v_test_one_string(XEp_main, "count(//b/child::*)", "3");
v_test_one_string(XEp_main, "count(//x/ancestor-or-self::*)", "2");
v_test_one_string(XEp_main, "count(//b/descendant-or-self::*)", "4");
v_test_one_string(XEp_main, "count(//self::*)", "6");
v_test_one_string(XEp_main, "count(/a/descendant::*)", "5");
v_test_one_string(XEp_main, "count(/a/descendant::x)", "1");
v_test_one_string(XEp_main, "count(/a/descendant::b)", "2");
v_test_one_string(XEp_main, "count(/a/descendant::b[@val=123])", "1");
v_test_one_string(XEp_main, "count(//c/ancestor::a)", "1");
v_test_one_string(XEp_main, "name(//d/parent::*)", "b");
v_test_one_string(XEp_main, "count(//c/ancestor::*)", "2");
v_test_one_string(XEp_main, "name(/a/b/ancestor::*)", "a");
v_test_one_string(XEp_main, "name(/a/b/c/following-sibling::*)", "d");
v_test_one_string(XEp_main, "count(//b/following-sibling::*)", "3");
v_test_one_string(XEp_main, "count(//b|//a)", "3");
v_test_one_string(XEp_main, "count(//d/preceding-sibling::*)", "2");
v_test_one_string(XEp_main, "-3 * 4", "-12");
v_test_one_string(XEp_main, "-3.1 * 4", "-12.4");
v_test_one_string(XEp_main, "12 div 5", "2.4");
v_test_one_string(XEp_main, "3 * 7", "21");
v_test_one_string(XEp_main, "-5.5 >= -5.5", "true");
v_test_one_string(XEp_main, "-5.5 < 3", "true");
v_test_one_string(XEp_main, "-6.0 < -7", "false");
v_test_one_string(XEp_main, "12 < 14", "true");
v_test_one_string(XEp_main, "12 > 14", "false");
v_test_one_string(XEp_main, "14 <= 14", "true");
v_test_one_string(XEp_main, "/a or /b", "true");
v_test_one_string(XEp_main, "/c or /b", "false");
v_test_one_string(XEp_main, "/a and /b", "false");
v_test_one_string(XEp_main, "/a and /*/b", "true");
v_test_one_string(XEp_main, "18-12", "6");
v_test_one_string(XEp_main, "18+12", "30");
v_test_one_string(XEp_main, "count(//a|//b)", "3");
v_test_one_string(XEp_main, "count(//*[@val])", "1");
v_test_one_string(XEp_main, "name(//*[@val=123])", "b");
v_test_one_string(XEp_main, "3=4", "false");
v_test_one_string(XEp_main, "3!=4", "true");
v_test_one_string(XEp_main, "12=12", "true");
v_test_one_string(XEp_main, "'here is a string'='here is a string'", "true");
v_test_one_string(XEp_main, "'here is a string'!='here is a string'", "false");
v_test_one_string(XEp_main, "/a/b/@val", "123");
v_test_one_string(XEp_main, "count(//*/b)", "2");
v_test_one_string(XEp_main, "name(/*/*/*[2])", "c");
v_test_one_string(XEp_main, "name(/*)", "a");
v_test_one_string(XEp_main, "name(/a)", "a");
v_test_one_string(XEp_main, "name(/a/b)", "b");
v_test_one_string(XEp_main, "name(/*/*)", "b");
v_test_one_string(XEp_main, "name(/a/b/c)", "c");
v_test_one_string(XEp_main, "count(/a/b/*)", "3");
v_test_one_string(XEp_main, "ceiling(3.5)", "4");
v_test_one_string(XEp_main, "concat('first ','second',' third','')", "first second third");
v_test_one_string(XEp_main, "ceiling(5)", "5");
v_test_one_string(XEp_main, "floor(3.5)", "3");
v_test_one_string(XEp_main, "floor(5)", "5");
v_test_one_string(XEp_main, "string-length('try')", "3");
v_test_one_string(XEp_main, "concat(name(/a/b[1]/*[1]),' ',name(/a/b/*[2]))", "b c");
v_test_one_string(XEp_main, "count(/a/b/*)", "3");
v_test_one_string(XEp_main, "count(//*)", "6");
v_test_one_string(XEp_main, "count(//b)", "2");
v_test_one_string(XEp_main, "contains('base','as')", "true");
v_test_one_string(XEp_main, "contains('base','x')", "false");
v_test_one_string(XEp_main, "not(contains('base','as'))", "false");
v_test_one_string(XEp_main, "starts-with('blabla','bla')", "true");
v_test_one_string(XEp_main, "starts-with('blebla','bla')", "false");
v_test_one_string(XEp_main, "substring('12345',2,3)", "234");
v_test_one_string(XEp_main, "substring('12345',2)", "2345");
v_test_one_string(XEp_main, "substring('12345',2,6)", "2345");
v_test_one_string(XEp_main, "concat('[',normalize-space(' before and after '),']')", "[before and after]");
v_test_one_string(XEp_main, "string(count(//*))", "6");
v_test_one_string(XEp_main, "number(contains('base','as'))", "1");
v_test_one_string(XEp_main, "boolean(contains('base','as'))", "true");
// test for u_compute_xpath_node_set
unsigned u_nb;
TinyXPath::xpath_processor xp_proc(XEp_main, "//*"); // build the list of all element nodes in the tree
u_nb = xp_proc.u_compute_xpath_node_set(); // retrieve number of nodes
sprintf(ca_res, "%d", u_nb);
o_ok = (u_nb == 6);
v_out_one_line("//*", ca_res, "6", o_ok);
// regression test for multiple additive expressions
v_test_one_string(XEp_main, "2+3+4+5", "14");
v_test_one_string(XEp_main, "20-2-3+5", "20");
// regression test for predicate count bug
v_test_one_string(XEp_main, "count(/a/x[1])", "1");
v_test_one_string(XEp_main, "name(/a/*[2])", "x");
v_test_one_string(XEp_main, "name(/a/*[1])", "b");
v_test_one_string(XEp_main, "name(/a/x[1])", "x");
// regression test for bug in position()
v_test_one_string(XEp_main, "count(/a/b/c[1])", "1");
v_test_one_string(XEp_main, "count(/a/b/c[position()=1])", "1");
v_test_one_string(XEp_main, "count(/a/b/d[position()=3])", "0");
// regression test for bug in i_compute_xpath
i_res = TinyXPath::i_xpath_int(XEp_main, "//*[@val]/@val");
sprintf(ca_res, "%d", i_res);
v_out_one_line("//*[@val]/@val", ca_res, "123", i_res == 123);
// regression test for bug in text in expressions
v_test_one_string(XEp_main, "//x[text()='sub text']/@target", "xyz");
// regression test for bug in o_xpath_double
o_res = TinyXPath::o_xpath_double(XEp_main, "substring('123.4',1)", d_out);
if (o_res) {
sprintf(ca_res, "%.1f", d_out);
o_ok = !strcmp(ca_res, "123.4");
} else
o_ok = false;
v_out_one_line("substring('123.4',1)", ca_res, "123.4", o_ok);
#ifdef NDEBUG
// testing for syntax error
TinyXPath::xpath_processor xp_proc_2(XEp_main, "//**");
i_res = xp_proc_2.i_compute_xpath();
if (xp_proc_2.e_error == TinyXPath::xpath_processor::e_error_syntax) {
o_ok = true;
strcpy(ca_res, "syntax error");
} else {
o_ok = false;
sprintf(ca_res, "error %d", xp_proc_2.e_error);
}
v_out_one_line("//**", ca_res, "syntax error", o_ok);
#endif // NDEBUG
// regression test for bug in "text" being an element
fprintf(Fp_out_html, "</table>\n");
delete XDp_doc;
XDp_doc = new XMLDocument();
XDp_doc->Parse("<xml><text>within</text></xml>");
XEp_main = XDp_doc->RootElement();
fprintf(Fp_out_html, "<h2>Input XML tree</h2>\n");
v_out_html(Fp_out_html, XDp_doc, 0);
fprintf(Fp_out_html, "<br /><br />\n");
fprintf(Fp_out_html, "<table border='1'><tr><th>Expression</th><th>Result</th><th>Expected (%s)</th></tr>\n",
#ifdef LIBXML_CHECK
"libXML"
#else
"compiled"
#endif
);
v_test_one_string(XEp_main, "/xml/text/text()", "within");
#ifdef LIBXML_CHECK
xmlFreeDoc(Dp_doc);
#endif
delete XDp_doc;
fprintf(Fp_out_html, "</table>\n");
fprintf(Fp_out_html, "<h2>RSS feed examples</h2>\n");
fprintf(Fp_out_html,
"These examples show how to decode a typical XML file : the <a "
"href='http://sourceforge.net/export/rss2_projnews.php?group_id=53396&rss_fulltext=1'>TinyXPath RSS "
"feed</a><br /><br />");
XDp_doc = new XMLDocument;
if (XDp_doc->LoadFile("rss2_projnews.xml") != XML_SUCCESS) {
printf("Can't find rss2_projnews.xml !\n");
return 99;
}
const char* cp_rss_count = "count(/rss/channel/item)";
const char* cp_rss_ver = "/rss/@version";
int i_nb_news, i_news;
char ca_expr[1000];
i_nb_news = TinyXPath::i_xpath_int(XDp_doc->RootElement(), cp_rss_count);
fprintf(Fp_out_html, "RSS version (XPath expr : <b>%s</b>) : %s<br />\n", cp_rss_ver,
TinyXPath::S_xpath_string(XDp_doc->RootElement(), cp_rss_ver).c_str());
fprintf(Fp_out_html, "Nb of news messages (XPath expr : <b>%s</b>) : %d<br />\n", cp_rss_count, i_nb_news);
fprintf(Fp_out_html, "<br /><table border='1'><tr><th>Xpath expr</th><th>value</th></tr>\n");
for (i_news = 0; i_news < i_nb_news; i_news++) {
sprintf(ca_expr, "concat(/rss/channel/item[%d]/pubDate/text(),' : ',/rss/channel/item[%d]/title/text())",
i_news + 1, i_news + 1);
fprintf(Fp_out_html, "<tr><td>%s</td><td>%s</td></tr>\n", ca_expr,
TinyXPath::S_xpath_string(XDp_doc->RootElement(), ca_expr).c_str());
}
fprintf(Fp_out_html, "</table>\n");
delete XDp_doc;
fprintf(Fp_out_html, "<br /> <br /></body></html>\n");
fclose(Fp_out_html);
return 0;
}