blob: af7722e902efda33ccad5d9e10fd655a5e5be002 [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 Veillard01791d51998-07-24 19:24:09 +000038
Daniel Veillard260a68f1998-08-13 03:39:55 +000039/*
40 * Note: there is a couple of errors introduced on purpose.
Daniel Veillard260a68f1998-08-13 03:39:55 +000041static CHAR buffer[] =
Daniel Veillard726c7e31999-02-08 15:13:10 +000042"<?xml version=\"1.0\"?>\n\
Daniel Veillard01791d51998-07-24 19:24:09 +000043<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
44<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
45<D:propertyupdate>\n\
46<D:set a=\"'toto'\" b>\n\
47 <D:prop>\n\
48 <Z:authors>\n\
49 <Z:Author>Jim Whitehead</Z:Author>\n\
50 <Z:Author>Roy Fielding</Z:Author>\n\
51 </Z:authors>\n\
52 </D:prop>\n\
53 </D:set>\n\
54 <D:remove>\n\
55 <D:prop><Z:Copyright-Owner/></D:prop>\n\
56 </D:remove>\n\
57</D:propertyupdate>\n\
58\n\
59";
Daniel Veillard1566d3a1999-07-15 14:24:29 +000060 */
Daniel Veillard01791d51998-07-24 19:24:09 +000061
Daniel Veillard25940b71998-10-29 05:51:30 +000062/************************************************************************
63 * *
64 * Debug *
65 * *
66 ************************************************************************/
67
68int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000069 xmlDocPtr doc, tmp;
70 xmlNodePtr tree, subtree;
71
Daniel Veillard25940b71998-10-29 05:51:30 +000072 /*
73 * build a fake XML document
74 */
Daniel Veillard25940b71998-10-29 05:51:30 +000075 doc = xmlNewDoc("1.0");
76 doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
77 xmlSetProp(doc->root, "prop1", "gnome is great");
78 xmlSetProp(doc->root, "prop2", "&linux; too");
Daniel Veillard726c7e31999-02-08 15:13:10 +000079 xmlSetProp(doc->root, "emptyprop", "");
Daniel Veillard25940b71998-10-29 05:51:30 +000080 tree = xmlNewChild(doc->root, NULL, "head", NULL);
81 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
82 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
83 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
84 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
85 subtree = xmlNewChild(tree, NULL, "image", NULL);
86 xmlSetProp(subtree, "href", "linus.gif");
87
88 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000089 * test intermediate copy if needed.
90 */
91 if (copy) {
92 tmp = doc;
93 doc = xmlCopyDoc(doc, 1);
94 xmlFreeDoc(tmp);
95 }
96
97 /*
Daniel Veillard25940b71998-10-29 05:51:30 +000098 * print it.
99 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000100 if (noout == 0)
101 xmlDocDump(stdout, doc);
Daniel Veillard25940b71998-10-29 05:51:30 +0000102
103 /*
104 * free it.
105 */
106 xmlFreeDoc(doc);
107 return(0);
108}
Daniel Veillard01791d51998-07-24 19:24:09 +0000109
Daniel Veillard260a68f1998-08-13 03:39:55 +0000110void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000111 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000112
113 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000114 * build an XML tree from a string;
115 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000116 if (recovery)
117 doc = xmlRecoverFile(filename);
118 else
119 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000120
121 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000122 * test intermediate copy if needed.
123 */
124 if (copy) {
125 tmp = doc;
126 doc = xmlCopyDoc(doc, 1);
127 xmlFreeDoc(tmp);
128 }
129
130 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000131 * print it.
132 */
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000133 if (noout == 0) {
134 if (!debug)
135 xmlDocDump(stdout, doc);
136 else
137 xmlDebugDumpDocument(stdout, doc);
138 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000139
140 /*
141 * free it.
142 */
143 xmlFreeDoc(doc);
144}
145
146void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000147 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000148
149 /*
150 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000151 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000152 if (recovery)
153 doc = xmlRecoverDoc(buf);
154 else
155 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000156
157 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000158 * test intermediate copy if needed.
159 */
160 if (copy) {
161 tmp = doc;
162 doc = xmlCopyDoc(doc, 1);
163 xmlFreeDoc(tmp);
164 }
165
166 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000167 * print it.
168 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000169 if (!debug)
170 xmlDocDump(stdout, doc);
171 else
172 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000173
174 /*
175 * free it.
176 */
177 xmlFreeDoc(doc);
178}
179
180int main(int argc, char **argv) {
181 int i;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000182 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000183
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000184 for (i = 1; i < argc ; i++) {
185 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
186 debug++;
187 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
188 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000189 else if ((!strcmp(argv[i], "-recover")) ||
190 (!strcmp(argv[i], "--recover")))
191 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000192 else if ((!strcmp(argv[i], "-noent")) ||
193 (!strcmp(argv[i], "--noent")))
194 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000195 else if ((!strcmp(argv[i], "-noout")) ||
196 (!strcmp(argv[i], "--noout")))
197 noout++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000198 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000199 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000200 for (i = 1; i < argc ; i++) {
201 if (argv[i][0] != '-') {
202 parseAndPrintFile(argv[i]);
203 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000204 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000205 }
206 if (files == 0) {
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000207 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000208 argv[0]);
209 printf("\tParse the XML files and output the result of the parsing\n");
210 printf("\t--debug : dump a debug tree of the in-memory document\n");
211 printf("\t--copy : used to test the internal copy implementation\n");
212 printf("\t--recover : output what is parsable on broken XmL documents\n");
213 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000214 printf("\t--noout : don't output the result\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000215 }
Daniel Veillard01791d51998-07-24 19:24:09 +0000216
217 return(0);
218}