blob: 3ee565b6ace054686d15e7f29aaad3ed695806c8 [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
Daniel Veillard3c558c31999-12-22 11:30:41 +000010#include "win32config.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000011#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000012#include "config.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000013#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000014
15#include <stdio.h>
16#include <string.h>
17
18#ifdef HAVE_SYS_TYPES_H
Daniel Veillard01791d51998-07-24 19:24:09 +000019#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000020#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +000021#ifdef HAVE_SYS_STAT_H
Daniel Veillard01791d51998-07-24 19:24:09 +000022#include <sys/stat.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000023#endif
24#ifdef HAVE_FCNTL_H
Daniel Veillard01791d51998-07-24 19:24:09 +000025#include <fcntl.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000026#endif
27#ifdef HAVE_UNISTD_H
Daniel Veillard01791d51998-07-24 19:24:09 +000028#include <unistd.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000029#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000030#ifdef HAVE_STDLIB_H
Seth Alvese7f12e61998-10-01 20:51:15 +000031#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000032#endif
Daniel Veillard01791d51998-07-24 19:24:09 +000033
Daniel Veillard6454aec1999-09-02 22:04:43 +000034#include "xmlmemory.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000035#include "parser.h"
36#include "tree.h"
Daniel Veillardbaf4cd51998-10-27 22:56:57 +000037#include "debugXML.h"
38
39static int debug = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +000040static int debugent = 0;
Daniel Veillardbe36afe1998-11-27 06:39:50 +000041static int copy = 0;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000042static int recovery = 0;
Daniel Veillard011b63c1999-06-02 17:44:04 +000043static int noent = 0;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000044static int noout = 0;
Daniel Veillardb05deb71999-08-10 19:04:08 +000045static int valid = 0;
Daniel Veillard944b5ff1999-12-15 19:08:24 +000046static int postvalid = 0;
Daniel Veillardb05deb71999-08-10 19:04:08 +000047static int repeat = 0;
Daniel Veillard7c1206f1999-10-14 09:10:25 +000048static int insert = 0;
Daniel Veillard11a48ec1999-11-23 10:40:46 +000049static int compress = 0;
Daniel Veillardb05deb71999-08-10 19:04:08 +000050
51extern int xmlDoValidityCheckingDefaultValue;
Daniel Veillard01791d51998-07-24 19:24:09 +000052
Daniel Veillard01791d51998-07-24 19:24:09 +000053
Daniel Veillard260a68f1998-08-13 03:39:55 +000054void parseAndPrintFile(char *filename) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +000055 xmlDocPtr doc, tmp;
Daniel Veillard01791d51998-07-24 19:24:09 +000056
57 /*
Daniel Veillard260a68f1998-08-13 03:39:55 +000058 * build an XML tree from a string;
59 */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000060 if (recovery)
61 doc = xmlRecoverFile(filename);
62 else
63 doc = xmlParseFile(filename);
Daniel Veillard260a68f1998-08-13 03:39:55 +000064
65 /*
Daniel Veillardbe36afe1998-11-27 06:39:50 +000066 * test intermediate copy if needed.
67 */
68 if (copy) {
69 tmp = doc;
70 doc = xmlCopyDoc(doc, 1);
71 xmlFreeDoc(tmp);
72 }
73
Daniel Veillard7c1206f1999-10-14 09:10:25 +000074 if (insert) {
75 const xmlChar* list[256];
76 int nb, i;
77 xmlNodePtr node;
78
79 if (doc->root != NULL) {
80 node = doc->root;
81 while ((node != NULL) && (node->last == NULL)) node = node->next;
82 if (node != NULL) {
83 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
84 if (nb < 0) {
85 printf("could not get valid list of elements\n");
86 } else if (nb == 0) {
87 printf("No element can be indersted under root\n");
88 } else {
89 printf("%d element types can be indersted under root:\n",
90 nb);
91 for (i = 0;i < nb;i++) {
92 printf("%s\n", list[i]);
93 }
94 }
95 }
96 }
97 }else if (noout == 0) {
98 /*
99 * print it.
100 */
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000101 if (!debug) {
102 if (compress)
103 xmlSaveFile("-", doc);
104 else
105 xmlDocDump(stdout, doc);
106 } else
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000107 xmlDebugDumpDocument(stdout, doc);
108 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000109
110 /*
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000111 * A posteriori validation test
Daniel Veillard260a68f1998-08-13 03:39:55 +0000112 */
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000113 if (postvalid) {
114 xmlValidCtxt cvp;
115 cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf;
116 xmlValidateDocument(&cvp, doc);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000117 }
118
Daniel Veillard10a2c651999-12-12 13:03:50 +0000119 if (debugent)
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000120 xmlDebugDumpEntities(stdout, doc);
Daniel Veillard01791d51998-07-24 19:24:09 +0000121
122 /*
123 * free it.
124 */
125 xmlFreeDoc(doc);
126}
127
128int main(int argc, char **argv) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000129 int i, count;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000130 int files = 0;
Daniel Veillard01791d51998-07-24 19:24:09 +0000131
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000132 for (i = 1; i < argc ; i++) {
133 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
134 debug++;
Daniel Veillard10a2c651999-12-12 13:03:50 +0000135 if ((!strcmp(argv[i], "-debugent")) || (!strcmp(argv[i], "--debugent")))
136 debugent++;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000137 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
138 copy++;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000139 else if ((!strcmp(argv[i], "-recover")) ||
140 (!strcmp(argv[i], "--recover")))
141 recovery++;
Daniel Veillard011b63c1999-06-02 17:44:04 +0000142 else if ((!strcmp(argv[i], "-noent")) ||
143 (!strcmp(argv[i], "--noent")))
144 noent++;
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000145 else if ((!strcmp(argv[i], "-noout")) ||
146 (!strcmp(argv[i], "--noout")))
147 noout++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000148 else if ((!strcmp(argv[i], "-valid")) ||
149 (!strcmp(argv[i], "--valid")))
150 valid++;
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000151 else if ((!strcmp(argv[i], "-postvalid")) ||
152 (!strcmp(argv[i], "--postvalid")))
153 postvalid++;
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000154 else if ((!strcmp(argv[i], "-insert")) ||
155 (!strcmp(argv[i], "--insert")))
156 insert++;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000157 else if ((!strcmp(argv[i], "-repeat")) ||
158 (!strcmp(argv[i], "--repeat")))
159 repeat++;
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000160 else if ((!strcmp(argv[i], "-compress")) ||
161 (!strcmp(argv[i], "--compress"))) {
162 compress++;
163 xmlSetCompressMode(9);
164 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000165 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000166 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000167 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000168 for (i = 1; i < argc ; i++) {
169 if (argv[i][0] != '-') {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000170 if (repeat) {
171 for (count = 0;count < 100 * repeat;count++)
172 parseAndPrintFile(argv[i]);
173 } else
174 parseAndPrintFile(argv[i]);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000175 files ++;
Daniel Veillard01791d51998-07-24 19:24:09 +0000176 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000177 }
178 if (files == 0) {
Daniel Veillard10a2c651999-12-12 13:03:50 +0000179 printf("Usage : %s [--debug] [--debugent] [--copy] [--recover] [--noent] [--noout] [--valid] [--repeat] XMLfiles ...\n",
Daniel Veillard14fff061999-06-22 21:49:07 +0000180 argv[0]);
181 printf("\tParse the XML files and output the result of the parsing\n");
182 printf("\t--debug : dump a debug tree of the in-memory document\n");
Daniel Veillard10a2c651999-12-12 13:03:50 +0000183 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard14fff061999-06-22 21:49:07 +0000184 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000185 printf("\t--recover : output what was parsable on broken XML documents\n");
Daniel Veillard14fff061999-06-22 21:49:07 +0000186 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000187 printf("\t--noout : don't output the result tree\n");
Daniel Veillard6077d031999-10-09 09:11:45 +0000188 printf("\t--valid : validate the document in addition to std well-formed check\n");
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000189 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
190 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
191 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000192 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard25940b71998-10-29 05:51:30 +0000193 }
Daniel Veillarda819dac1999-11-24 18:04:22 +0000194 xmlCleanupParser();
Daniel Veillard6454aec1999-09-02 22:04:43 +0000195 xmlMemoryDump();
Daniel Veillard01791d51998-07-24 19:24:09 +0000196
197 return(0);
198}