blob: 295a66977a608636beb7011a2e2c5cdcb060c436 [file] [log] [blame]
Daniel Veillard01791d51998-07-24 19:24:09 +00001/*
2 * tester.c : a small tester program for XML input.
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00006 * Daniel.Veillard@w3.org
Daniel Veillard01791d51998-07-24 19:24:09 +00007 */
8
Daniel Veillard260a68f1998-08-13 03:39:55 +00009#ifdef WIN32
10#define HAVE_FCNTL_H
11#include <io.h>
12#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000013#include "config.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000014#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000015
16#include <stdio.h>
17#include <string.h>
18
19#ifdef HAVE_SYS_TYPES_H
Daniel Veillard01791d51998-07-24 19:24:09 +000020#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000021#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +000022#ifdef HAVE_SYS_STAT_H
Daniel Veillard01791d51998-07-24 19:24:09 +000023#include <sys/stat.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000024#endif
25#ifdef HAVE_FCNTL_H
Daniel Veillard01791d51998-07-24 19:24:09 +000026#include <fcntl.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000027#endif
28#ifdef HAVE_UNISTD_H
Daniel Veillard01791d51998-07-24 19:24:09 +000029#include <unistd.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000030#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000031#ifdef HAVE_STDLIB_H
Seth Alvese7f12e61998-10-01 20:51:15 +000032#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000033#endif
Daniel Veillard01791d51998-07-24 19:24:09 +000034
Daniel Veillard6454aec1999-09-02 22:04:43 +000035#include "xmlmemory.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000036#include "parser.h"
37#include "tree.h"
Daniel Veillardbaf4cd51998-10-27 22:56:57 +000038#include "debugXML.h"
39
40static int debug = 0;
Daniel Veillardbe36afe1998-11-27 06:39:50 +000041static int copy = 0;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000042static int recovery = 0;
Daniel Veillard011b63c1999-06-02 17:44:04 +000043static int noent = 0;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000044static int noout = 0;
Daniel Veillardb05deb71999-08-10 19:04:08 +000045static int valid = 0;
46static int repeat = 0;
Daniel Veillard7c1206f1999-10-14 09:10:25 +000047static int insert = 0;
Daniel Veillard11a48ec1999-11-23 10:40:46 +000048static int compress = 0;
Daniel Veillardb05deb71999-08-10 19:04:08 +000049
50extern int xmlDoValidityCheckingDefaultValue;
Daniel Veillard01791d51998-07-24 19:24:09 +000051
Daniel Veillard260a68f1998-08-13 03:39:55 +000052/*
53 * Note: there is a couple of errors introduced on purpose.
Daniel Veillarddd6b3671999-09-23 22:19:22 +000054static xmlChar buffer[] =
Daniel Veillard726c7e31999-02-08 15:13:10 +000055"<?xml version=\"1.0\"?>\n\
Daniel Veillard01791d51998-07-24 19:24:09 +000056<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
57<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
58<D:propertyupdate>\n\
59<D:set a=\"'toto'\" b>\n\
60 <D:prop>\n\
61 <Z:authors>\n\
62 <Z:Author>Jim Whitehead</Z:Author>\n\
63 <Z:Author>Roy Fielding</Z:Author>\n\
64 </Z:authors>\n\
65 </D:prop>\n\
66 </D:set>\n\
67 <D:remove>\n\
68 <D:prop><Z:Copyright-Owner/></D:prop>\n\
69 </D:remove>\n\
70</D:propertyupdate>\n\
71\n\
72";
Daniel Veillard1566d3a1999-07-15 14:24:29 +000073 */
Daniel Veillard01791d51998-07-24 19:24:09 +000074
Daniel Veillard25940b71998-10-29 05:51:30 +000075/************************************************************************
76 * *
77 * Debug *
78 * *
79 ************************************************************************/
80
81int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000082 xmlDocPtr doc, tmp;
83 xmlNodePtr tree, subtree;
84
Daniel Veillard25940b71998-10-29 05:51:30 +000085 /*
86 * build a fake XML document
87 */
Daniel Veillardb96e6431999-08-29 21:02:19 +000088 doc = xmlNewDoc(BAD_CAST "1.0");
89 doc->root = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
90 xmlSetProp(doc->root, BAD_CAST "prop1", BAD_CAST "gnome is great");
91 xmlSetProp(doc->root, BAD_CAST "prop2", BAD_CAST "&linux; too");
92 xmlSetProp(doc->root, BAD_CAST "emptyprop", BAD_CAST "");
93 tree = xmlNewChild(doc->root, NULL, BAD_CAST "head", NULL);
94 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
95 BAD_CAST "Welcome to Gnome");
96 tree = xmlNewChild(doc->root, NULL, BAD_CAST "chapter", NULL);
97 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
98 BAD_CAST "The Linux adventure");
99 subtree = xmlNewChild(tree, NULL, BAD_CAST "p", BAD_CAST "bla bla bla ...");
100 subtree = xmlNewChild(tree, NULL, BAD_CAST "image", NULL);
101 xmlSetProp(subtree, BAD_CAST "href", BAD_CAST "linus.gif");
Daniel Veillard25940b71998-10-29 05:51:30 +0000102
103 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000104 * test intermediate copy if needed.
105 */
106 if (copy) {
107 tmp = doc;
108 doc = xmlCopyDoc(doc, 1);
109 xmlFreeDoc(tmp);
110 }
111
112 /*
Daniel Veillard25940b71998-10-29 05:51:30 +0000113 * print it.
114 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000115 if (noout == 0)
116 xmlDocDump(stdout, doc);
Daniel Veillard25940b71998-10-29 05:51:30 +0000117
118 /*
119 * free it.
120 */
121 xmlFreeDoc(doc);
122 return(0);
123}
Daniel Veillard01791d51998-07-24 19:24:09 +0000124
Daniel Veillard260a68f1998-08-13 03:39:55 +0000125void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000126 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000127
128 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000129 * build an XML tree from a string;
130 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000131 if (recovery)
132 doc = xmlRecoverFile(filename);
133 else
134 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000135
136 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000137 * test intermediate copy if needed.
138 */
139 if (copy) {
140 tmp = doc;
141 doc = xmlCopyDoc(doc, 1);
142 xmlFreeDoc(tmp);
143 }
144
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000145 if (insert) {
146 const xmlChar* list[256];
147 int nb, i;
148 xmlNodePtr node;
149
150 if (doc->root != NULL) {
151 node = doc->root;
152 while ((node != NULL) && (node->last == NULL)) node = node->next;
153 if (node != NULL) {
154 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
155 if (nb < 0) {
156 printf("could not get valid list of elements\n");
157 } else if (nb == 0) {
158 printf("No element can be indersted under root\n");
159 } else {
160 printf("%d element types can be indersted under root:\n",
161 nb);
162 for (i = 0;i < nb;i++) {
163 printf("%s\n", list[i]);
164 }
165 }
166 }
167 }
168 }else if (noout == 0) {
169 /*
170 * print it.
171 */
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000172 if (!debug) {
173 if (compress)
174 xmlSaveFile("-", doc);
175 else
176 xmlDocDump(stdout, doc);
177 } else
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000178 xmlDebugDumpDocument(stdout, doc);
179 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000180
181 /*
182 * free it.
183 */
184 xmlFreeDoc(doc);
185}
186
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000187void parseAndPrintBuffer(xmlChar *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000188 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000189
190 /*
191 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000192 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000193 if (recovery)
194 doc = xmlRecoverDoc(buf);
195 else
196 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000197
198 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000199 * test intermediate copy if needed.
200 */
201 if (copy) {
202 tmp = doc;
203 doc = xmlCopyDoc(doc, 1);
204 xmlFreeDoc(tmp);
205 }
206
207 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000208 * print it.
209 */
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000210 if (!debug) {
211 if (compress)
212 xmlSaveFile("-", doc);
213 else
214 xmlDocDump(stdout, doc);
215 } else
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000216 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000217
218 /*
219 * free it.
220 */
221 xmlFreeDoc(doc);
222}
223
224int main(int argc, char **argv) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000225 int i, count;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000226 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000227
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000228 for (i = 1; i < argc ; i++) {
229 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
230 debug++;
231 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
232 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000233 else if ((!strcmp(argv[i], "-recover")) ||
234 (!strcmp(argv[i], "--recover")))
235 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000236 else if ((!strcmp(argv[i], "-noent")) ||
237 (!strcmp(argv[i], "--noent")))
238 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000239 else if ((!strcmp(argv[i], "-noout")) ||
240 (!strcmp(argv[i], "--noout")))
241 noout++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000242 else if ((!strcmp(argv[i], "-valid")) ||
243 (!strcmp(argv[i], "--valid")))
244 valid++;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000245 else if ((!strcmp(argv[i], "-insert")) ||
246 (!strcmp(argv[i], "--insert")))
247 insert++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000248 else if ((!strcmp(argv[i], "-repeat")) ||
249 (!strcmp(argv[i], "--repeat")))
250 repeat++;
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000251 else if ((!strcmp(argv[i], "-compress")) ||
252 (!strcmp(argv[i], "--compress"))) {
253 compress++;
254 xmlSetCompressMode(9);
255 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000256 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000257 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000258 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000259 for (i = 1; i < argc ; i++) {
260 if (argv[i][0] != '-') {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000261 if (repeat) {
262 for (count = 0;count < 100 * repeat;count++)
263 parseAndPrintFile(argv[i]);
264 } else
265 parseAndPrintFile(argv[i]);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000266 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000267 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000268 }
269 if (files == 0) {
Daniel Veillard6077d031999-10-09 09:11:45 +0000270 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] [--valid] [--repeat] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000271 argv[0]);
272 printf("\tParse the XML files and output the result of the parsing\n");
273 printf("\t--debug : dump a debug tree of the in-memory document\n");
274 printf("\t--copy : used to test the internal copy implementation\n");
275 printf("\t--recover : output what is parsable on broken XmL documents\n");
276 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000277 printf("\t--noout : don't output the result\n");
Daniel Veillard6077d031999-10-09 09:11:45 +0000278 printf("\t--valid : validate the document in addition to std well-formed check\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000279 printf("\t--repeat : parse the file 100 times, for timing or profiling\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000280 printf("\t--insert : test for valid insertions\n");
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000281 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000282 }
Daniel Veillarda819dac1999-11-24 18:04:22 +0000283 xmlCleanupParser();
Daniel Veillard6454aec1999-09-02 22:04:43 +0000284 xmlMemoryDump();
Daniel Veillard01791d51998-07-24 19:24:09 +0000285
286 return(0);
287}