blob: a1e7c03657a2aa86c63f46a872165eea76093d14 [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 Veillard6454aec1999-09-02 22:04:43 +000029#include "xmlmemory.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000030#include "parser.h"
31#include "tree.h"
Daniel Veillardbaf4cd51998-10-27 22:56:57 +000032#include "debugXML.h"
33
34static int debug = 0;
Daniel Veillardbe36afe1998-11-27 06:39:50 +000035static int copy = 0;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000036static int recovery = 0;
Daniel Veillard011b63c1999-06-02 17:44:04 +000037static int noent = 0;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000038static int noout = 0;
Daniel Veillardb05deb71999-08-10 19:04:08 +000039static int valid = 0;
40static int repeat = 0;
41
42extern int xmlDoValidityCheckingDefaultValue;
Daniel Veillard01791d51998-07-24 19:24:09 +000043
Daniel Veillard260a68f1998-08-13 03:39:55 +000044/*
45 * Note: there is a couple of errors introduced on purpose.
Daniel Veillard260a68f1998-08-13 03:39:55 +000046static CHAR buffer[] =
Daniel Veillard726c7e31999-02-08 15:13:10 +000047"<?xml version=\"1.0\"?>\n\
Daniel Veillard01791d51998-07-24 19:24:09 +000048<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
49<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
50<D:propertyupdate>\n\
51<D:set a=\"'toto'\" b>\n\
52 <D:prop>\n\
53 <Z:authors>\n\
54 <Z:Author>Jim Whitehead</Z:Author>\n\
55 <Z:Author>Roy Fielding</Z:Author>\n\
56 </Z:authors>\n\
57 </D:prop>\n\
58 </D:set>\n\
59 <D:remove>\n\
60 <D:prop><Z:Copyright-Owner/></D:prop>\n\
61 </D:remove>\n\
62</D:propertyupdate>\n\
63\n\
64";
Daniel Veillard1566d3a1999-07-15 14:24:29 +000065 */
Daniel Veillard01791d51998-07-24 19:24:09 +000066
Daniel Veillard25940b71998-10-29 05:51:30 +000067/************************************************************************
68 * *
69 * Debug *
70 * *
71 ************************************************************************/
72
73int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000074 xmlDocPtr doc, tmp;
75 xmlNodePtr tree, subtree;
76
Daniel Veillard25940b71998-10-29 05:51:30 +000077 /*
78 * build a fake XML document
79 */
Daniel Veillardb96e6431999-08-29 21:02:19 +000080 doc = xmlNewDoc(BAD_CAST "1.0");
81 doc->root = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
82 xmlSetProp(doc->root, BAD_CAST "prop1", BAD_CAST "gnome is great");
83 xmlSetProp(doc->root, BAD_CAST "prop2", BAD_CAST "&linux; too");
84 xmlSetProp(doc->root, BAD_CAST "emptyprop", BAD_CAST "");
85 tree = xmlNewChild(doc->root, NULL, BAD_CAST "head", NULL);
86 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
87 BAD_CAST "Welcome to Gnome");
88 tree = xmlNewChild(doc->root, NULL, BAD_CAST "chapter", NULL);
89 subtree = xmlNewChild(tree, NULL, BAD_CAST "title",
90 BAD_CAST "The Linux adventure");
91 subtree = xmlNewChild(tree, NULL, BAD_CAST "p", BAD_CAST "bla bla bla ...");
92 subtree = xmlNewChild(tree, NULL, BAD_CAST "image", NULL);
93 xmlSetProp(subtree, BAD_CAST "href", BAD_CAST "linus.gif");
Daniel Veillard25940b71998-10-29 05:51:30 +000094
95 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000096 * test intermediate copy if needed.
97 */
98 if (copy) {
99 tmp = doc;
100 doc = xmlCopyDoc(doc, 1);
101 xmlFreeDoc(tmp);
102 }
103
104 /*
Daniel Veillard25940b71998-10-29 05:51:30 +0000105 * print it.
106 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000107 if (noout == 0)
108 xmlDocDump(stdout, doc);
Daniel Veillard25940b71998-10-29 05:51:30 +0000109
110 /*
111 * free it.
112 */
113 xmlFreeDoc(doc);
114 return(0);
115}
Daniel Veillard01791d51998-07-24 19:24:09 +0000116
Daniel Veillard260a68f1998-08-13 03:39:55 +0000117void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000118 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000119
120 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000121 * build an XML tree from a string;
122 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000123 if (recovery)
124 doc = xmlRecoverFile(filename);
125 else
126 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000127
128 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000129 * test intermediate copy if needed.
130 */
131 if (copy) {
132 tmp = doc;
133 doc = xmlCopyDoc(doc, 1);
134 xmlFreeDoc(tmp);
135 }
136
137 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000138 * print it.
139 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000140 if (noout == 0) {
141 if (!debug)
142 xmlDocDump(stdout, doc);
143 else
144 xmlDebugDumpDocument(stdout, doc);
145 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000146
147 /*
148 * free it.
149 */
150 xmlFreeDoc(doc);
151}
152
153void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000154 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000155
156 /*
157 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000158 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000159 if (recovery)
160 doc = xmlRecoverDoc(buf);
161 else
162 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000163
164 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000165 * test intermediate copy if needed.
166 */
167 if (copy) {
168 tmp = doc;
169 doc = xmlCopyDoc(doc, 1);
170 xmlFreeDoc(tmp);
171 }
172
173 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000174 * print it.
175 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000176 if (!debug)
177 xmlDocDump(stdout, doc);
178 else
179 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000180
181 /*
182 * free it.
183 */
184 xmlFreeDoc(doc);
185}
186
187int main(int argc, char **argv) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000188 int i, count;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000189 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000190
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000191 for (i = 1; i < argc ; i++) {
192 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
193 debug++;
194 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
195 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000196 else if ((!strcmp(argv[i], "-recover")) ||
197 (!strcmp(argv[i], "--recover")))
198 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000199 else if ((!strcmp(argv[i], "-noent")) ||
200 (!strcmp(argv[i], "--noent")))
201 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000202 else if ((!strcmp(argv[i], "-noout")) ||
203 (!strcmp(argv[i], "--noout")))
204 noout++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000205 else if ((!strcmp(argv[i], "-valid")) ||
206 (!strcmp(argv[i], "--valid")))
207 valid++;
208 else if ((!strcmp(argv[i], "-repeat")) ||
209 (!strcmp(argv[i], "--repeat")))
210 repeat++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000211 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000212 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000213 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000214 for (i = 1; i < argc ; i++) {
215 if (argv[i][0] != '-') {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000216 if (repeat) {
217 for (count = 0;count < 100 * repeat;count++)
218 parseAndPrintFile(argv[i]);
219 } else
220 parseAndPrintFile(argv[i]);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000221 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000222 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000223 }
224 if (files == 0) {
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000225 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000226 argv[0]);
227 printf("\tParse the XML files and output the result of the parsing\n");
228 printf("\t--debug : dump a debug tree of the in-memory document\n");
229 printf("\t--copy : used to test the internal copy implementation\n");
230 printf("\t--recover : output what is parsable on broken XmL documents\n");
231 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000232 printf("\t--noout : don't output the result\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000233 printf("\t--repeat : parse the file 100 times, for timing or profiling\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000234 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000235 xmlMemoryDump();
Daniel Veillard01791d51998-07-24 19:24:09 +0000236
237 return(0);
238}