blob: c795c812cc4876a9ddceaa9604003fe624d73b2c [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;
47
48extern int xmlDoValidityCheckingDefaultValue;
Daniel Veillard01791d51998-07-24 19:24:09 +000049
Daniel Veillard260a68f1998-08-13 03:39:55 +000050/*
51 * Note: there is a couple of errors introduced on purpose.
Daniel Veillard260a68f1998-08-13 03:39:55 +000052static CHAR buffer[] =
Daniel Veillard726c7e31999-02-08 15:13:10 +000053"<?xml version=\"1.0\"?>\n\
Daniel Veillard01791d51998-07-24 19:24:09 +000054<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
55<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
56<D:propertyupdate>\n\
57<D:set a=\"'toto'\" b>\n\
58 <D:prop>\n\
59 <Z:authors>\n\
60 <Z:Author>Jim Whitehead</Z:Author>\n\
61 <Z:Author>Roy Fielding</Z:Author>\n\
62 </Z:authors>\n\
63 </D:prop>\n\
64 </D:set>\n\
65 <D:remove>\n\
66 <D:prop><Z:Copyright-Owner/></D:prop>\n\
67 </D:remove>\n\
68</D:propertyupdate>\n\
69\n\
70";
Daniel Veillard1566d3a1999-07-15 14:24:29 +000071 */
Daniel Veillard01791d51998-07-24 19:24:09 +000072
Daniel Veillard25940b71998-10-29 05:51:30 +000073/************************************************************************
74 * *
75 * Debug *
76 * *
77 ************************************************************************/
78
79int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000080 xmlDocPtr doc, tmp;
81 xmlNodePtr tree, subtree;
82
Daniel Veillard25940b71998-10-29 05:51:30 +000083 /*
84 * build a fake XML document
85 */
Daniel Veillardb96e6431999-08-29 21:02:19 +000086 doc = xmlNewDoc(BAD_CAST "1.0");
87 doc->root = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
88 xmlSetProp(doc->root, BAD_CAST "prop1", BAD_CAST "gnome is great");
89 xmlSetProp(doc->root, BAD_CAST "prop2", BAD_CAST "&linux; too");
90 xmlSetProp(doc->root, BAD_CAST "emptyprop", BAD_CAST "");
91 tree = xmlNewChild(doc->root, NULL, BAD_CAST "head", NULL);
92 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
93 BAD_CAST "Welcome to Gnome");
94 tree = xmlNewChild(doc->root, NULL, BAD_CAST "chapter", NULL);
95 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
96 BAD_CAST "The Linux adventure");
97 subtree = xmlNewChild(tree, NULL, BAD_CAST "p", BAD_CAST "bla bla bla ...");
98 subtree = xmlNewChild(tree, NULL, BAD_CAST "image", NULL);
99 xmlSetProp(subtree, BAD_CAST "href", BAD_CAST "linus.gif");
Daniel Veillard25940b71998-10-29 05:51:30 +0000100
101 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000102 * test intermediate copy if needed.
103 */
104 if (copy) {
105 tmp = doc;
106 doc = xmlCopyDoc(doc, 1);
107 xmlFreeDoc(tmp);
108 }
109
110 /*
Daniel Veillard25940b71998-10-29 05:51:30 +0000111 * print it.
112 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000113 if (noout == 0)
114 xmlDocDump(stdout, doc);
Daniel Veillard25940b71998-10-29 05:51:30 +0000115
116 /*
117 * free it.
118 */
119 xmlFreeDoc(doc);
120 return(0);
121}
Daniel Veillard01791d51998-07-24 19:24:09 +0000122
Daniel Veillard260a68f1998-08-13 03:39:55 +0000123void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000124 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000125
126 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000127 * build an XML tree from a string;
128 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000129 if (recovery)
130 doc = xmlRecoverFile(filename);
131 else
132 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000133
134 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000135 * test intermediate copy if needed.
136 */
137 if (copy) {
138 tmp = doc;
139 doc = xmlCopyDoc(doc, 1);
140 xmlFreeDoc(tmp);
141 }
142
143 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000144 * print it.
145 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000146 if (noout == 0) {
147 if (!debug)
148 xmlDocDump(stdout, doc);
149 else
150 xmlDebugDumpDocument(stdout, doc);
151 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000152
153 /*
154 * free it.
155 */
156 xmlFreeDoc(doc);
157}
158
159void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000160 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000161
162 /*
163 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000164 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000165 if (recovery)
166 doc = xmlRecoverDoc(buf);
167 else
168 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000169
170 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000171 * test intermediate copy if needed.
172 */
173 if (copy) {
174 tmp = doc;
175 doc = xmlCopyDoc(doc, 1);
176 xmlFreeDoc(tmp);
177 }
178
179 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000180 * print it.
181 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000182 if (!debug)
183 xmlDocDump(stdout, doc);
184 else
185 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000186
187 /*
188 * free it.
189 */
190 xmlFreeDoc(doc);
191}
192
193int main(int argc, char **argv) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000194 int i, count;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000195 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000196
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000197 for (i = 1; i < argc ; i++) {
198 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
199 debug++;
200 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
201 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000202 else if ((!strcmp(argv[i], "-recover")) ||
203 (!strcmp(argv[i], "--recover")))
204 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000205 else if ((!strcmp(argv[i], "-noent")) ||
206 (!strcmp(argv[i], "--noent")))
207 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000208 else if ((!strcmp(argv[i], "-noout")) ||
209 (!strcmp(argv[i], "--noout")))
210 noout++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000211 else if ((!strcmp(argv[i], "-valid")) ||
212 (!strcmp(argv[i], "--valid")))
213 valid++;
214 else if ((!strcmp(argv[i], "-repeat")) ||
215 (!strcmp(argv[i], "--repeat")))
216 repeat++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000217 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000218 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000219 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000220 for (i = 1; i < argc ; i++) {
221 if (argv[i][0] != '-') {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000222 if (repeat) {
223 for (count = 0;count < 100 * repeat;count++)
224 parseAndPrintFile(argv[i]);
225 } else
226 parseAndPrintFile(argv[i]);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000227 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000228 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000229 }
230 if (files == 0) {
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000231 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000232 argv[0]);
233 printf("\tParse the XML files and output the result of the parsing\n");
234 printf("\t--debug : dump a debug tree of the in-memory document\n");
235 printf("\t--copy : used to test the internal copy implementation\n");
236 printf("\t--recover : output what is parsable on broken XmL documents\n");
237 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000238 printf("\t--noout : don't output the result\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000239 printf("\t--repeat : parse the file 100 times, for timing or profiling\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000240 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000241 xmlMemoryDump();
Daniel Veillard01791d51998-07-24 19:24:09 +0000242
243 return(0);
244}