blob: 927eda2dc161bd5aa56e8c6eb36ce8b872ffb8e1 [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 Veillard01791d51998-07-24 19:24:09 +000036
Daniel Veillard260a68f1998-08-13 03:39:55 +000037/*
38 * Note: there is a couple of errors introduced on purpose.
39 */
40static 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";
59
Daniel Veillard25940b71998-10-29 05:51:30 +000060/************************************************************************
61 * *
62 * Debug *
63 * *
64 ************************************************************************/
65
66int treeTest(void) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000067 xmlDocPtr doc, tmp;
68 xmlNodePtr tree, subtree;
69
Daniel Veillard25940b71998-10-29 05:51:30 +000070 /*
71 * build a fake XML document
72 */
Daniel Veillard25940b71998-10-29 05:51:30 +000073 doc = xmlNewDoc("1.0");
74 doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
75 xmlSetProp(doc->root, "prop1", "gnome is great");
76 xmlSetProp(doc->root, "prop2", "&linux; too");
Daniel Veillard726c7e31999-02-08 15:13:10 +000077 xmlSetProp(doc->root, "emptyprop", "");
Daniel Veillard25940b71998-10-29 05:51:30 +000078 tree = xmlNewChild(doc->root, NULL, "head", NULL);
79 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
80 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
81 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
82 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
83 subtree = xmlNewChild(tree, NULL, "image", NULL);
84 xmlSetProp(subtree, "href", "linus.gif");
85
86 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000087 * test intermediate copy if needed.
88 */
89 if (copy) {
90 tmp = doc;
91 doc = xmlCopyDoc(doc, 1);
92 xmlFreeDoc(tmp);
93 }
94
95 /*
Daniel Veillard25940b71998-10-29 05:51:30 +000096 * print it.
97 */
98 xmlDocDump(stdout, doc);
99
100 /*
101 * free it.
102 */
103 xmlFreeDoc(doc);
104 return(0);
105}
Daniel Veillard01791d51998-07-24 19:24:09 +0000106
Daniel Veillard260a68f1998-08-13 03:39:55 +0000107void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000108 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000109
110 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000111 * build an XML tree from a string;
112 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000113 if (recovery)
114 doc = xmlRecoverFile(filename);
115 else
116 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000117
118 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000119 * test intermediate copy if needed.
120 */
121 if (copy) {
122 tmp = doc;
123 doc = xmlCopyDoc(doc, 1);
124 xmlFreeDoc(tmp);
125 }
126
127 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000128 * print it.
129 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000130 if (!debug)
131 xmlDocDump(stdout, doc);
132 else
133 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000134
135 /*
136 * free it.
137 */
138 xmlFreeDoc(doc);
139}
140
141void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000142 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000143
144 /*
145 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000146 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000147 if (recovery)
148 doc = xmlRecoverDoc(buf);
149 else
150 doc = xmlParseDoc(buf);
Daniel Veillard01791d51998-07-24 19:24:09 +0000151
152 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000153 * test intermediate copy if needed.
154 */
155 if (copy) {
156 tmp = doc;
157 doc = xmlCopyDoc(doc, 1);
158 xmlFreeDoc(tmp);
159 }
160
161 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000162 * print it.
163 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000164 if (!debug)
165 xmlDocDump(stdout, doc);
166 else
167 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000168
169 /*
170 * free it.
171 */
172 xmlFreeDoc(doc);
173}
174
175int main(int argc, char **argv) {
176 int i;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000177 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000178
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000179 for (i = 1; i < argc ; i++) {
180 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
181 debug++;
182 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
183 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000184 else if ((!strcmp(argv[i], "-recover")) ||
185 (!strcmp(argv[i], "--recover")))
186 recovery++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000187 }
188 for (i = 1; i < argc ; i++) {
189 if (argv[i][0] != '-') {
190 parseAndPrintFile(argv[i]);
191 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000192 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000193 }
194 if (files == 0) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000195 printf("\nFirst test for the parser, with errors\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000196 parseAndPrintBuffer(buffer);
Daniel Veillard25940b71998-10-29 05:51:30 +0000197 printf("\nBuilding a tree from scratch and printing it\n");
198 treeTest();
199 }
Daniel Veillard01791d51998-07-24 19:24:09 +0000200
201 return(0);
202}