blob: 43005f134a91c48a1baebdab3cc627786604d7ae [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 Veillard01791d51998-07-24 19:24:09 +000037
Daniel Veillard260a68f1998-08-13 03:39:55 +000038/*
39 * Note: there is a couple of errors introduced on purpose.
Daniel Veillard260a68f1998-08-13 03:39:55 +000040static CHAR buffer[] =
Daniel Veillard726c7e31999-02-08 15:13:10 +000041"<?xml version=\"1.0\"?>\n\
Daniel Veillard01791d51998-07-24 19:24:09 +000042<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
43<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
44<D:propertyupdate>\n\
45<D:set a=\"'toto'\" b>\n\
46 <D:prop>\n\
47 <Z:authors>\n\
48 <Z:Author>Jim Whitehead</Z:Author>\n\
49 <Z:Author>Roy Fielding</Z:Author>\n\
50 </Z:authors>\n\
51 </D:prop>\n\
52 </D:set>\n\
53 <D:remove>\n\
54 <D:prop><Z:Copyright-Owner/></D:prop>\n\
55 </D:remove>\n\
56</D:propertyupdate>\n\
57\n\
58";
Daniel Veillard1566d3a1999-07-15 14:24:29 +000059 */
Daniel Veillard01791d51998-07-24 19:24:09 +000060
Daniel Veillard25940b71998-10-29 05:51:30 +000061/************************************************************************
62 * *
63 * Debug *
64 * *
65 ************************************************************************/
66
67int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000068 xmlDocPtr doc, tmp;
69 xmlNodePtr tree, subtree;
70
Daniel Veillard25940b71998-10-29 05:51:30 +000071 /*
72 * build a fake XML document
73 */
Daniel Veillard25940b71998-10-29 05:51:30 +000074 doc = xmlNewDoc("1.0");
75 doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
76 xmlSetProp(doc->root, "prop1", "gnome is great");
77 xmlSetProp(doc->root, "prop2", "&linux; too");
Daniel Veillard726c7e31999-02-08 15:13:10 +000078 xmlSetProp(doc->root, "emptyprop", "");
Daniel Veillard25940b71998-10-29 05:51:30 +000079 tree = xmlNewChild(doc->root, NULL, "head", NULL);
80 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
81 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
82 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
83 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
84 subtree = xmlNewChild(tree, NULL, "image", NULL);
85 xmlSetProp(subtree, "href", "linus.gif");
86
87 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000088 * test intermediate copy if needed.
89 */
90 if (copy) {
91 tmp = doc;
92 doc = xmlCopyDoc(doc, 1);
93 xmlFreeDoc(tmp);
94 }
95
96 /*
Daniel Veillard25940b71998-10-29 05:51:30 +000097 * print it.
98 */
99 xmlDocDump(stdout, doc);
100
101 /*
102 * free it.
103 */
104 xmlFreeDoc(doc);
105 return(0);
106}
Daniel Veillard01791d51998-07-24 19:24:09 +0000107
Daniel Veillard260a68f1998-08-13 03:39:55 +0000108void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000109 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000110
111 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000112 * build an XML tree from a string;
113 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000114 if (recovery)
115 doc = xmlRecoverFile(filename);
116 else
117 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000118
119 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000120 * test intermediate copy if needed.
121 */
122 if (copy) {
123 tmp = doc;
124 doc = xmlCopyDoc(doc, 1);
125 xmlFreeDoc(tmp);
126 }
127
128 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000129 * print it.
130 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000131 if (!debug)
132 xmlDocDump(stdout, doc);
133 else
134 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000135
136 /*
137 * free it.
138 */
139 xmlFreeDoc(doc);
140}
141
142void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000143 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000144
145 /*
146 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000147 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000148 if (recovery)
149 doc = xmlRecoverDoc(buf);
150 else
151 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000152
153 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000154 * test intermediate copy if needed.
155 */
156 if (copy) {
157 tmp = doc;
158 doc = xmlCopyDoc(doc, 1);
159 xmlFreeDoc(tmp);
160 }
161
162 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000163 * print it.
164 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000165 if (!debug)
166 xmlDocDump(stdout, doc);
167 else
168 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000169
170 /*
171 * free it.
172 */
173 xmlFreeDoc(doc);
174}
175
176int main(int argc, char **argv) {
177 int i;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000178 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000179
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000180 for (i = 1; i < argc ; i++) {
181 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
182 debug++;
183 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
184 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000185 else if ((!strcmp(argv[i], "-recover")) ||
186 (!strcmp(argv[i], "--recover")))
187 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000188 else if ((!strcmp(argv[i], "-noent")) ||
189 (!strcmp(argv[i], "--noent")))
190 noent++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000191 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000192 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000193 for (i = 1; i < argc ; i++) {
194 if (argv[i][0] != '-') {
195 parseAndPrintFile(argv[i]);
196 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000197 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000198 }
199 if (files == 0) {
Daniel Veillard14fff061999-06-22 21:49:07 +0000200 printf("Usage : %s [--debug] [--copy] [--recover] [--noent] XMLfiles ...\n",
201 argv[0]);
202 printf("\tParse the XML files and output the result of the parsing\n");
203 printf("\t--debug : dump a debug tree of the in-memory document\n");
204 printf("\t--copy : used to test the internal copy implementation\n");
205 printf("\t--recover : output what is parsable on broken XmL documents\n");
206 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000207 }
Daniel Veillard01791d51998-07-24 19:24:09 +0000208
209 return(0);
210}