blob: e4960111ca866d16dba12209bf3d1c40befe4bd8 [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 Veillardb96e6431999-08-29 21:02:19 +000079 doc = xmlNewDoc(BAD_CAST "1.0");
80 doc->root = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
81 xmlSetProp(doc->root, BAD_CAST "prop1", BAD_CAST "gnome is great");
82 xmlSetProp(doc->root, BAD_CAST "prop2", BAD_CAST "&linux; too");
83 xmlSetProp(doc->root, BAD_CAST "emptyprop", BAD_CAST "");
84 tree = xmlNewChild(doc->root, NULL, BAD_CAST "head", NULL);
85 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
86 BAD_CAST "Welcome to Gnome");
87 tree = xmlNewChild(doc->root, NULL, BAD_CAST "chapter", NULL);
88 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
89 BAD_CAST "The Linux adventure");
90 subtree = xmlNewChild(tree, NULL, BAD_CAST "p", BAD_CAST "bla bla bla ...");
91 subtree = xmlNewChild(tree, NULL, BAD_CAST "image", NULL);
92 xmlSetProp(subtree, BAD_CAST "href", BAD_CAST "linus.gif");
Daniel Veillard25940b71998-10-29 05:51:30 +000093
94 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000095 * test intermediate copy if needed.
96 */
97 if (copy) {
98 tmp = doc;
99 doc = xmlCopyDoc(doc, 1);
100 xmlFreeDoc(tmp);
101 }
102
103 /*
Daniel Veillard25940b71998-10-29 05:51:30 +0000104 * print it.
105 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000106 if (noout == 0)
107 xmlDocDump(stdout, doc);
Daniel Veillard25940b71998-10-29 05:51:30 +0000108
109 /*
110 * free it.
111 */
112 xmlFreeDoc(doc);
113 return(0);
114}
Daniel Veillard01791d51998-07-24 19:24:09 +0000115
Daniel Veillard260a68f1998-08-13 03:39:55 +0000116void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000117 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000118
119 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000120 * build an XML tree from a string;
121 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000122 if (recovery)
123 doc = xmlRecoverFile(filename);
124 else
125 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000126
127 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000128 * test intermediate copy if needed.
129 */
130 if (copy) {
131 tmp = doc;
132 doc = xmlCopyDoc(doc, 1);
133 xmlFreeDoc(tmp);
134 }
135
136 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000137 * print it.
138 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000139 if (noout == 0) {
140 if (!debug)
141 xmlDocDump(stdout, doc);
142 else
143 xmlDebugDumpDocument(stdout, doc);
144 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000145
146 /*
147 * free it.
148 */
149 xmlFreeDoc(doc);
150}
151
152void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000153 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000154
155 /*
156 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000157 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000158 if (recovery)
159 doc = xmlRecoverDoc(buf);
160 else
161 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000162
163 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000164 * test intermediate copy if needed.
165 */
166 if (copy) {
167 tmp = doc;
168 doc = xmlCopyDoc(doc, 1);
169 xmlFreeDoc(tmp);
170 }
171
172 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000173 * print it.
174 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000175 if (!debug)
176 xmlDocDump(stdout, doc);
177 else
178 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000179
180 /*
181 * free it.
182 */
183 xmlFreeDoc(doc);
184}
185
186int main(int argc, char **argv) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000187 int i, count;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000188 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000189
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000190 for (i = 1; i < argc ; i++) {
191 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
192 debug++;
193 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
194 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000195 else if ((!strcmp(argv[i], "-recover")) ||
196 (!strcmp(argv[i], "--recover")))
197 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000198 else if ((!strcmp(argv[i], "-noent")) ||
199 (!strcmp(argv[i], "--noent")))
200 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000201 else if ((!strcmp(argv[i], "-noout")) ||
202 (!strcmp(argv[i], "--noout")))
203 noout++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000204 else if ((!strcmp(argv[i], "-valid")) ||
205 (!strcmp(argv[i], "--valid")))
206 valid++;
207 else if ((!strcmp(argv[i], "-repeat")) ||
208 (!strcmp(argv[i], "--repeat")))
209 repeat++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000210 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000211 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000212 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000213 for (i = 1; i < argc ; i++) {
214 if (argv[i][0] != '-') {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000215 if (repeat) {
216 for (count = 0;count < 100 * repeat;count++)
217 parseAndPrintFile(argv[i]);
218 } else
219 parseAndPrintFile(argv[i]);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000220 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000221 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000222 }
223 if (files == 0) {
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000224 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000225 argv[0]);
226 printf("\tParse the XML files and output the result of the parsing\n");
227 printf("\t--debug : dump a debug tree of the in-memory document\n");
228 printf("\t--copy : used to test the internal copy implementation\n");
229 printf("\t--recover : output what is parsable on broken XmL documents\n");
230 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000231 printf("\t--noout : don't output the result\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000232 printf("\t--repeat : parse the file 100 times, for timing or profiling\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000233 }
Daniel Veillard01791d51998-07-24 19:24:09 +0000234
235 return(0);
236}