blob: 7002614b5cdc9dc8fb5180b7a11552d1670846f6 [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 Veillardb05deb71999-08-10 19:04:08 +000048
49extern int xmlDoValidityCheckingDefaultValue;
Daniel Veillard01791d51998-07-24 19:24:09 +000050
Daniel Veillard260a68f1998-08-13 03:39:55 +000051/*
52 * Note: there is a couple of errors introduced on purpose.
Daniel Veillarddd6b3671999-09-23 22:19:22 +000053static xmlChar buffer[] =
Daniel Veillard726c7e31999-02-08 15:13:10 +000054"<?xml version=\"1.0\"?>\n\
Daniel Veillard01791d51998-07-24 19:24:09 +000055<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
56<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
57<D:propertyupdate>\n\
58<D:set a=\"'toto'\" b>\n\
59 <D:prop>\n\
60 <Z:authors>\n\
61 <Z:Author>Jim Whitehead</Z:Author>\n\
62 <Z:Author>Roy Fielding</Z:Author>\n\
63 </Z:authors>\n\
64 </D:prop>\n\
65 </D:set>\n\
66 <D:remove>\n\
67 <D:prop><Z:Copyright-Owner/></D:prop>\n\
68 </D:remove>\n\
69</D:propertyupdate>\n\
70\n\
71";
Daniel Veillard1566d3a1999-07-15 14:24:29 +000072 */
Daniel Veillard01791d51998-07-24 19:24:09 +000073
Daniel Veillard25940b71998-10-29 05:51:30 +000074/************************************************************************
75 * *
76 * Debug *
77 * *
78 ************************************************************************/
79
80int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000081 xmlDocPtr doc, tmp;
82 xmlNodePtr tree, subtree;
83
Daniel Veillard25940b71998-10-29 05:51:30 +000084 /*
85 * build a fake XML document
86 */
Daniel Veillardb96e6431999-08-29 21:02:19 +000087 doc = xmlNewDoc(BAD_CAST "1.0");
88 doc->root = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
89 xmlSetProp(doc->root, BAD_CAST "prop1", BAD_CAST "gnome is great");
90 xmlSetProp(doc->root, BAD_CAST "prop2", BAD_CAST "&linux; too");
91 xmlSetProp(doc->root, BAD_CAST "emptyprop", BAD_CAST "");
92 tree = xmlNewChild(doc->root, NULL, BAD_CAST "head", NULL);
93 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
94 BAD_CAST "Welcome to Gnome");
95 tree = xmlNewChild(doc->root, NULL, BAD_CAST "chapter", NULL);
96 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
97 BAD_CAST "The Linux adventure");
98 subtree = xmlNewChild(tree, NULL, BAD_CAST "p", BAD_CAST "bla bla bla ...");
99 subtree = xmlNewChild(tree, NULL, BAD_CAST "image", NULL);
100 xmlSetProp(subtree, BAD_CAST "href", BAD_CAST "linus.gif");
Daniel Veillard25940b71998-10-29 05:51:30 +0000101
102 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000103 * test intermediate copy if needed.
104 */
105 if (copy) {
106 tmp = doc;
107 doc = xmlCopyDoc(doc, 1);
108 xmlFreeDoc(tmp);
109 }
110
111 /*
Daniel Veillard25940b71998-10-29 05:51:30 +0000112 * print it.
113 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000114 if (noout == 0)
115 xmlDocDump(stdout, doc);
Daniel Veillard25940b71998-10-29 05:51:30 +0000116
117 /*
118 * free it.
119 */
120 xmlFreeDoc(doc);
121 return(0);
122}
Daniel Veillard01791d51998-07-24 19:24:09 +0000123
Daniel Veillard260a68f1998-08-13 03:39:55 +0000124void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000125 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000126
127 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000128 * build an XML tree from a string;
129 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000130 if (recovery)
131 doc = xmlRecoverFile(filename);
132 else
133 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000134
135 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000136 * test intermediate copy if needed.
137 */
138 if (copy) {
139 tmp = doc;
140 doc = xmlCopyDoc(doc, 1);
141 xmlFreeDoc(tmp);
142 }
143
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000144 if (insert) {
145 const xmlChar* list[256];
146 int nb, i;
147 xmlNodePtr node;
148
149 if (doc->root != NULL) {
150 node = doc->root;
151 while ((node != NULL) && (node->last == NULL)) node = node->next;
152 if (node != NULL) {
153 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
154 if (nb < 0) {
155 printf("could not get valid list of elements\n");
156 } else if (nb == 0) {
157 printf("No element can be indersted under root\n");
158 } else {
159 printf("%d element types can be indersted under root:\n",
160 nb);
161 for (i = 0;i < nb;i++) {
162 printf("%s\n", list[i]);
163 }
164 }
165 }
166 }
167 }else if (noout == 0) {
168 /*
169 * print it.
170 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000171 if (!debug)
172 xmlDocDump(stdout, doc);
173 else
174 xmlDebugDumpDocument(stdout, doc);
175 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000176
177 /*
178 * free it.
179 */
180 xmlFreeDoc(doc);
181}
182
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000183void parseAndPrintBuffer(xmlChar *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000184 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000185
186 /*
187 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000188 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000189 if (recovery)
190 doc = xmlRecoverDoc(buf);
191 else
192 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000193
194 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000195 * test intermediate copy if needed.
196 */
197 if (copy) {
198 tmp = doc;
199 doc = xmlCopyDoc(doc, 1);
200 xmlFreeDoc(tmp);
201 }
202
203 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000204 * print it.
205 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000206 if (!debug)
207 xmlDocDump(stdout, doc);
208 else
209 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000210
211 /*
212 * free it.
213 */
214 xmlFreeDoc(doc);
215}
216
217int main(int argc, char **argv) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000218 int i, count;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000219 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000220
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000221 for (i = 1; i < argc ; i++) {
222 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
223 debug++;
224 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
225 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000226 else if ((!strcmp(argv[i], "-recover")) ||
227 (!strcmp(argv[i], "--recover")))
228 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000229 else if ((!strcmp(argv[i], "-noent")) ||
230 (!strcmp(argv[i], "--noent")))
231 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000232 else if ((!strcmp(argv[i], "-noout")) ||
233 (!strcmp(argv[i], "--noout")))
234 noout++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000235 else if ((!strcmp(argv[i], "-valid")) ||
236 (!strcmp(argv[i], "--valid")))
237 valid++;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000238 else if ((!strcmp(argv[i], "-insert")) ||
239 (!strcmp(argv[i], "--insert")))
240 insert++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000241 else if ((!strcmp(argv[i], "-repeat")) ||
242 (!strcmp(argv[i], "--repeat")))
243 repeat++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000244 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000245 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000246 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000247 for (i = 1; i < argc ; i++) {
248 if (argv[i][0] != '-') {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000249 if (repeat) {
250 for (count = 0;count < 100 * repeat;count++)
251 parseAndPrintFile(argv[i]);
252 } else
253 parseAndPrintFile(argv[i]);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000254 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000255 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000256 }
257 if (files == 0) {
Daniel Veillard6077d031999-10-09 09:11:45 +0000258 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] [--valid] [--repeat] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000259 argv[0]);
260 printf("\tParse the XML files and output the result of the parsing\n");
261 printf("\t--debug : dump a debug tree of the in-memory document\n");
262 printf("\t--copy : used to test the internal copy implementation\n");
263 printf("\t--recover : output what is parsable on broken XmL documents\n");
264 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000265 printf("\t--noout : don't output the result\n");
Daniel Veillard6077d031999-10-09 09:11:45 +0000266 printf("\t--valid : validate the document in addition to std well-formed check\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000267 printf("\t--repeat : parse the file 100 times, for timing or profiling\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000268 printf("\t--insert : test for valid insertions\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000269 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000270 xmlMemoryDump();
Daniel Veillard01791d51998-07-24 19:24:09 +0000271
272 return(0);
273}