blob: 6830917da7a9e6211a9736990b18227b62ee42dc [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 *
6 * $Id$
7 */
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 Veillard01791d51998-07-24 19:24:09 +000035
Daniel Veillard260a68f1998-08-13 03:39:55 +000036/*
37 * Note: there is a couple of errors introduced on purpose.
38 */
39static CHAR buffer[] =
Daniel Veillard01791d51998-07-24 19:24:09 +000040"\n\
41<?xml version=\"1.0\">\n\
42<?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");
77 tree = xmlNewChild(doc->root, NULL, "head", NULL);
78 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
79 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
80 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
81 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
82 subtree = xmlNewChild(tree, NULL, "image", NULL);
83 xmlSetProp(subtree, "href", "linus.gif");
84
85 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000086 * test intermediate copy if needed.
87 */
88 if (copy) {
89 tmp = doc;
90 doc = xmlCopyDoc(doc, 1);
91 xmlFreeDoc(tmp);
92 }
93
94 /*
Daniel Veillard25940b71998-10-29 05:51:30 +000095 * print it.
96 */
97 xmlDocDump(stdout, doc);
98
99 /*
100 * free it.
101 */
102 xmlFreeDoc(doc);
103 return(0);
104}
Daniel Veillard01791d51998-07-24 19:24:09 +0000105
Daniel Veillard260a68f1998-08-13 03:39:55 +0000106void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000107 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +0000108
109 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000110 * build an XML tree from a string;
111 */
112 doc = xmlParseFile(filename);
113
114 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000115 * test intermediate copy if needed.
116 */
117 if (copy) {
118 tmp = doc;
119 doc = xmlCopyDoc(doc, 1);
120 xmlFreeDoc(tmp);
121 }
122
123 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000124 * print it.
125 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000126 if (!debug)
127 xmlDocDump(stdout, doc);
128 else
129 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000130
131 /*
132 * free it.
133 */
134 xmlFreeDoc(doc);
135}
136
137void parseAndPrintBuffer(CHAR *buf) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000138 xmlDocPtr doc, tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000139
140 /*
141 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000142 */
143 doc = xmlParseDoc(buf);
144
145 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000146 * test intermediate copy if needed.
147 */
148 if (copy) {
149 tmp = doc;
150 doc = xmlCopyDoc(doc, 1);
151 xmlFreeDoc(tmp);
152 }
153
154 /*
Daniel Veillard01791d51998-07-24 19:24:09 +0000155 * print it.
156 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000157 if (!debug)
158 xmlDocDump(stdout, doc);
159 else
160 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000161
162 /*
163 * free it.
164 */
165 xmlFreeDoc(doc);
166}
167
168int main(int argc, char **argv) {
169 int i;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000170 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000171
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000172 for (i = 1; i < argc ; i++) {
173 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
174 debug++;
175 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
176 copy++;
177 }
178 for (i = 1; i < argc ; i++) {
179 if (argv[i][0] != '-') {
180 parseAndPrintFile(argv[i]);
181 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000182 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000183 }
184 if (files == 0) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000185 printf("\nFirst test for the parser, with errors\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000186 parseAndPrintBuffer(buffer);
Daniel Veillard25940b71998-10-29 05:51:30 +0000187 printf("\nBuilding a tree from scratch and printing it\n");
188 treeTest();
189 }
Daniel Veillard01791d51998-07-24 19:24:09 +0000190
191 return(0);
192}