blob: 56d74beab4a649ca8afc094cd233496c2eac334b [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 Veillard01791d51998-07-24 19:24:09 +000034
Daniel Veillard260a68f1998-08-13 03:39:55 +000035/*
36 * Note: there is a couple of errors introduced on purpose.
37 */
38static CHAR buffer[] =
Daniel Veillard01791d51998-07-24 19:24:09 +000039"\n\
40<?xml version=\"1.0\">\n\
41<?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\
42<?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\
43<D:propertyupdate>\n\
44<D:set a=\"'toto'\" b>\n\
45 <D:prop>\n\
46 <Z:authors>\n\
47 <Z:Author>Jim Whitehead</Z:Author>\n\
48 <Z:Author>Roy Fielding</Z:Author>\n\
49 </Z:authors>\n\
50 </D:prop>\n\
51 </D:set>\n\
52 <D:remove>\n\
53 <D:prop><Z:Copyright-Owner/></D:prop>\n\
54 </D:remove>\n\
55</D:propertyupdate>\n\
56\n\
57";
58
Daniel Veillard25940b71998-10-29 05:51:30 +000059/************************************************************************
60 * *
61 * Debug *
62 * *
63 ************************************************************************/
64
65int treeTest(void) {
66 /*
67 * build a fake XML document
68 */
69 xmlDocPtr doc;
70 xmlNodePtr tree, subtree;
71
72 doc = xmlNewDoc("1.0");
73 doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
74 xmlSetProp(doc->root, "prop1", "gnome is great");
75 xmlSetProp(doc->root, "prop2", "&linux; too");
76 tree = xmlNewChild(doc->root, NULL, "head", NULL);
77 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
78 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
79 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
80 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
81 subtree = xmlNewChild(tree, NULL, "image", NULL);
82 xmlSetProp(subtree, "href", "linus.gif");
83
84 /*
85 * print it.
86 */
87 xmlDocDump(stdout, doc);
88
89 /*
90 * free it.
91 */
92 xmlFreeDoc(doc);
93 return(0);
94}
Daniel Veillard01791d51998-07-24 19:24:09 +000095
Daniel Veillard260a68f1998-08-13 03:39:55 +000096void parseAndPrintFile(char *filename) {
Daniel Veillard01791d51998-07-24 19:24:09 +000097 xmlDocPtr doc;
98
99 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +0000100 * build an XML tree from a string;
101 */
102 doc = xmlParseFile(filename);
103
104 /*
105 * print it.
106 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000107 if (!debug)
108 xmlDocDump(stdout, doc);
109 else
110 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000111
112 /*
113 * free it.
114 */
115 xmlFreeDoc(doc);
116}
117
118void parseAndPrintBuffer(CHAR *buf) {
119 xmlDocPtr doc;
120
121 /*
122 * build an XML tree from a string;
Daniel Veillard01791d51998-07-24 19:24:09 +0000123 */
124 doc = xmlParseDoc(buf);
125
126 /*
127 * print it.
128 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000129 if (!debug)
130 xmlDocDump(stdout, doc);
131 else
132 xmlDebugDumpDocument(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000133
134 /*
135 * free it.
136 */
137 xmlFreeDoc(doc);
138}
139
140int main(int argc, char **argv) {
141 int i;
142
143 if (argc > 1) {
144 for (i = 1; i < argc ; i++) {
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000145 if ((strcmp(argv[i], "-debug")) && (strcmp(argv[i], "--debug")))
146 parseAndPrintFile(argv[i]);
147 else
148 debug++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000149 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000150 } else {
151 printf("\nFirst test for the parser, with errors\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000152 parseAndPrintBuffer(buffer);
Daniel Veillard25940b71998-10-29 05:51:30 +0000153 printf("\nBuilding a tree from scratch and printing it\n");
154 treeTest();
155 }
Daniel Veillard01791d51998-07-24 19:24:09 +0000156
157 return(0);
158}