blob: 89d1ff67d3191f37b2902bf45b82b57d31fcddb0 [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
13#include <config.h>
14#endif
Daniel Veillard01791d51998-07-24 19:24:09 +000015#include <sys/types.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000016#ifdef HAVE_SYS_STAT_H
Daniel Veillard01791d51998-07-24 19:24:09 +000017#include <sys/stat.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000018#endif
19#ifdef HAVE_FCNTL_H
Daniel Veillard01791d51998-07-24 19:24:09 +000020#include <fcntl.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000021#endif
22#ifdef HAVE_UNISTD_H
Daniel Veillard01791d51998-07-24 19:24:09 +000023#include <unistd.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000024#endif
Daniel Veillard01791d51998-07-24 19:24:09 +000025#include <stdio.h>
26#include <string.h>
Seth Alvese7f12e61998-10-01 20:51:15 +000027#include <stdlib.h>
Daniel Veillard01791d51998-07-24 19:24:09 +000028
Daniel Veillard260a68f1998-08-13 03:39:55 +000029#include "parser.h"
30#include "tree.h"
Daniel Veillardbaf4cd51998-10-27 22:56:57 +000031#include "debugXML.h"
32
33static int debug = 0;
Daniel Veillardbe36afe1998-11-27 06:39:50 +000034static int copy = 0;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000035static int recovery = 0;
Daniel Veillard011b63c1999-06-02 17:44:04 +000036static int noent = 0;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000037static int noout = 0;
Daniel Veillardb05deb71999-08-10 19:04:08 +000038static int valid = 0;
39static int repeat = 0;
40
41extern int xmlDoValidityCheckingDefaultValue;
Daniel Veillard01791d51998-07-24 19:24:09 +000042
Daniel Veillard260a68f1998-08-13 03:39:55 +000043/*
44 * Note: there is a couple of errors introduced on purpose.
Daniel Veillard260a68f1998-08-13 03:39:55 +000045static CHAR buffer[] =
Daniel Veillard726c7e31999-02-08 15:13:10 +000046"<?xml version=\"1.0\"?>\n\
Daniel Veillard01791d51998-07-24 19:24:09 +000047<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
48<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
49<D:propertyupdate>\n\
50<D:set a=\"'toto'\" b>\n\
51 <D:prop>\n\
52 <Z:authors>\n\
53 <Z:Author>Jim Whitehead</Z:Author>\n\
54 <Z:Author>Roy Fielding</Z:Author>\n\
55 </Z:authors>\n\
56 </D:prop>\n\
57 </D:set>\n\
58 <D:remove>\n\
59 <D:prop><Z:Copyright-Owner/></D:prop>\n\
60 </D:remove>\n\
61</D:propertyupdate>\n\
62\n\
63";
Daniel Veillard1566d3a1999-07-15 14:24:29 +000064 */
Daniel Veillard01791d51998-07-24 19:24:09 +000065
Daniel Veillard25940b71998-10-29 05:51:30 +000066/************************************************************************
67 * *
68 * Debug *
69 * *
70 ************************************************************************/
71
72int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000073 xmlDocPtr doc, tmp;
74 xmlNodePtr tree, subtree;
75
Daniel Veillard25940b71998-10-29 05:51:30 +000076 /*
77 * build a fake XML document
78 */
Daniel Veillard25940b71998-10-29 05:51:30 +000079 doc = xmlNewDoc("1.0");
80 doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
81 xmlSetProp(doc->root, "prop1", "gnome is great");
82 xmlSetProp(doc->root, "prop2", "&linux; too");
Daniel Veillard726c7e31999-02-08 15:13:10 +000083 xmlSetProp(doc->root, "emptyprop", "");
Daniel Veillard25940b71998-10-29 05:51:30 +000084 tree = xmlNewChild(doc->root, NULL, "head", NULL);
85 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
86 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
87 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
88 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
89 subtree = xmlNewChild(tree, NULL, "image", NULL);
90 xmlSetProp(subtree, "href", "linus.gif");
91
92 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000093 * test intermediate copy if needed.
94 */
95 if (copy) {
96 tmp = doc;
97 doc = xmlCopyDoc(doc, 1);
98 xmlFreeDoc(tmp);
99 }
100
101 /*
Daniel Veillard25940b71998-10-29 05:51:30 +0000102 * print it.
103 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000104 if (noout == 0)
105 xmlDocDump(stdout, doc);
Daniel Veillard25940b71998-10-29 05:51:30 +0000106
107 /*
108 * free it.
109 */
110 xmlFreeDoc(doc);
111 return(0);
112}
Daniel Veillard01791d51998-07-24 19:24:09 +0000113
Daniel Veillard260a68f1998-08-13 03:39:55 +0000114void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000115 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000116
117 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000118 * build an XML tree from a string;
119 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000120 if (recovery)
121 doc = xmlRecoverFile(filename);
122 else
123 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000124
125 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000126 * test intermediate copy if needed.
127 */
128 if (copy) {
129 tmp = doc;
130 doc = xmlCopyDoc(doc, 1);
131 xmlFreeDoc(tmp);
132 }
133
134 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000135 * print it.
136 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000137 if (noout == 0) {
138 if (!debug)
139 xmlDocDump(stdout, doc);
140 else
141 xmlDebugDumpDocument(stdout, doc);
142 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000143
144 /*
145 * free it.
146 */
147 xmlFreeDoc(doc);
148}
149
150void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000151 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000152
153 /*
154 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000155 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000156 if (recovery)
157 doc = xmlRecoverDoc(buf);
158 else
159 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000160
161 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000162 * test intermediate copy if needed.
163 */
164 if (copy) {
165 tmp = doc;
166 doc = xmlCopyDoc(doc, 1);
167 xmlFreeDoc(tmp);
168 }
169
170 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000171 * print it.
172 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000173 if (!debug)
174 xmlDocDump(stdout, doc);
175 else
176 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000177
178 /*
179 * free it.
180 */
181 xmlFreeDoc(doc);
182}
183
184int main(int argc, char **argv) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000185 int i, count;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000186 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000187
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000188 for (i = 1; i < argc ; i++) {
189 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
190 debug++;
191 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
192 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000193 else if ((!strcmp(argv[i], "-recover")) ||
194 (!strcmp(argv[i], "--recover")))
195 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000196 else if ((!strcmp(argv[i], "-noent")) ||
197 (!strcmp(argv[i], "--noent")))
198 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000199 else if ((!strcmp(argv[i], "-noout")) ||
200 (!strcmp(argv[i], "--noout")))
201 noout++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000202 else if ((!strcmp(argv[i], "-valid")) ||
203 (!strcmp(argv[i], "--valid")))
204 valid++;
205 else if ((!strcmp(argv[i], "-repeat")) ||
206 (!strcmp(argv[i], "--repeat")))
207 repeat++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000208 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000209 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000210 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000211 for (i = 1; i < argc ; i++) {
212 if (argv[i][0] != '-') {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000213 if (repeat) {
214 for (count = 0;count < 100 * repeat;count++)
215 parseAndPrintFile(argv[i]);
216 } else
217 parseAndPrintFile(argv[i]);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000218 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000219 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000220 }
221 if (files == 0) {
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000222 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000223 argv[0]);
224 printf("\tParse the XML files and output the result of the parsing\n");
225 printf("\t--debug : dump a debug tree of the in-memory document\n");
226 printf("\t--copy : used to test the internal copy implementation\n");
227 printf("\t--recover : output what is parsable on broken XmL documents\n");
228 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000229 printf("\t--noout : don't output the result\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000230 printf("\t--repeat : parse the file 100 times, for timing or profiling\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000231 }
Daniel Veillard01791d51998-07-24 19:24:09 +0000232
233 return(0);
234}