Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tester.c : a small tester program for XML input. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 6 | * Daniel.Veillard@w3.org |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 9 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 10 | #include "win32config.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 11 | #else |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 12 | #include "config.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 13 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #ifdef HAVE_SYS_TYPES_H |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 20 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 21 | #ifdef HAVE_SYS_STAT_H |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 22 | #include <sys/stat.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 23 | #endif |
| 24 | #ifdef HAVE_FCNTL_H |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 26 | #endif |
| 27 | #ifdef HAVE_UNISTD_H |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 29 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 30 | #ifdef HAVE_STDLIB_H |
Seth Alves | e7f12e6 | 1998-10-01 20:51:15 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 32 | #endif |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 33 | #ifdef HAVE_LIBREADLINE |
| 34 | #include <readline/readline.h> |
| 35 | #ifdef HAVE_LIBHISTORY |
| 36 | #include <readline/history.h> |
| 37 | #endif |
| 38 | #endif |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 39 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 40 | #include "xmlmemory.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 41 | #include "parser.h" |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 42 | #include "HTMLparser.h" |
| 43 | #include "HTMLtree.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 44 | #include "tree.h" |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 45 | #include "xpath.h" |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 46 | #include "debugXML.h" |
| 47 | |
| 48 | static int debug = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 49 | static int debugent = 0; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 50 | static int copy = 0; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 51 | static int recovery = 0; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 52 | static int noent = 0; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 53 | static int noout = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 54 | static int valid = 0; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 55 | static int postvalid = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 56 | static int repeat = 0; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 57 | static int insert = 0; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 58 | static int compress = 0; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 59 | static int html = 0; |
| 60 | static int shell = 0; |
| 61 | static int push = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 62 | |
| 63 | extern int xmlDoValidityCheckingDefaultValue; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 64 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 65 | /** |
| 66 | * xmlShellReadline: |
| 67 | * @prompt: the prompt value |
| 68 | * |
| 69 | * Read a string |
| 70 | * |
| 71 | * Returns a pointer to it or NULL on EOF the caller is expected to |
| 72 | * free the returned string. |
| 73 | */ |
| 74 | char * |
| 75 | xmlShellReadline(char *prompt) { |
| 76 | #ifdef HAVE_LIBREADLINE |
| 77 | char *line_read; |
| 78 | |
| 79 | /* Get a line from the user. */ |
| 80 | line_read = readline (prompt); |
| 81 | |
| 82 | /* If the line has any text in it, save it on the history. */ |
| 83 | if (line_read && *line_read) |
| 84 | add_history (line_read); |
| 85 | |
| 86 | return (line_read); |
| 87 | #else |
| 88 | char line_read[501]; |
| 89 | |
| 90 | if (prompt != NULL) |
| 91 | fprintf(stdout, "%s", prompt); |
| 92 | if (!fgets(line_read, 500, stdin)) |
| 93 | return(NULL); |
| 94 | line_read[500] = 0; |
| 95 | return(strdup(line_read)); |
| 96 | #endif |
| 97 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 98 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 99 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 100 | xmlDocPtr doc = NULL, tmp; |
| 101 | |
| 102 | if (html) { |
| 103 | doc = htmlParseFile(filename, NULL); |
| 104 | } else { |
| 105 | /* |
| 106 | * build an XML tree from a string; |
| 107 | */ |
| 108 | if (push) { |
| 109 | FILE *f; |
| 110 | |
| 111 | f = fopen(filename, "r"); |
| 112 | if (f != NULL) { |
| 113 | int res, size = 3; |
| 114 | char chars[1024]; |
| 115 | xmlParserCtxtPtr ctxt; |
| 116 | |
| 117 | if (repeat) |
| 118 | size = 1024; |
| 119 | res = fread(chars, 1, 4, f); |
| 120 | if (res > 0) { |
| 121 | ctxt = xmlCreatePushParserCtxt(NULL, NULL, |
| 122 | chars, res, filename); |
| 123 | while ((res = fread(chars, 1, size, f)) > 0) { |
| 124 | xmlParseChunk(ctxt, chars, res, 0); |
| 125 | } |
| 126 | xmlParseChunk(ctxt, chars, 0, 1); |
| 127 | doc = ctxt->myDoc; |
| 128 | xmlFreeParserCtxt(ctxt); |
| 129 | } |
| 130 | } |
| 131 | } else if (recovery) |
| 132 | doc = xmlRecoverFile(filename); |
| 133 | else |
| 134 | doc = xmlParseFile(filename); |
| 135 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 138 | * shell interraction |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 139 | */ |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 140 | if (shell) |
| 141 | xmlShell(doc, filename, xmlShellReadline, stdout); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 144 | * test intermediate copy if needed. |
| 145 | */ |
| 146 | if (copy) { |
| 147 | tmp = doc; |
| 148 | doc = xmlCopyDoc(doc, 1); |
| 149 | xmlFreeDoc(tmp); |
| 150 | } |
| 151 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 152 | if ((insert) && (!html)) { |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 153 | const xmlChar* list[256]; |
| 154 | int nb, i; |
| 155 | xmlNodePtr node; |
| 156 | |
| 157 | if (doc->root != NULL) { |
| 158 | node = doc->root; |
| 159 | while ((node != NULL) && (node->last == NULL)) node = node->next; |
| 160 | if (node != NULL) { |
| 161 | nb = xmlValidGetValidElements(node->last, NULL, list, 256); |
| 162 | if (nb < 0) { |
| 163 | printf("could not get valid list of elements\n"); |
| 164 | } else if (nb == 0) { |
| 165 | printf("No element can be indersted under root\n"); |
| 166 | } else { |
| 167 | printf("%d element types can be indersted under root:\n", |
| 168 | nb); |
| 169 | for (i = 0;i < nb;i++) { |
| 170 | printf("%s\n", list[i]); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | }else if (noout == 0) { |
| 176 | /* |
| 177 | * print it. |
| 178 | */ |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 179 | if (!debug) { |
| 180 | if (compress) |
| 181 | xmlSaveFile("-", doc); |
| 182 | else |
| 183 | xmlDocDump(stdout, doc); |
| 184 | } else |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 185 | xmlDebugDumpDocument(stdout, doc); |
| 186 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 189 | * A posteriori validation test |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 190 | */ |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 191 | if (postvalid) { |
| 192 | xmlValidCtxt cvp; |
| 193 | cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf; |
| 194 | xmlValidateDocument(&cvp, doc); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 197 | if ((debugent) && (!html)) |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 198 | xmlDebugDumpEntities(stdout, doc); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * free it. |
| 202 | */ |
| 203 | xmlFreeDoc(doc); |
| 204 | } |
| 205 | |
| 206 | int main(int argc, char **argv) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 207 | int i, count; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 208 | int files = 0; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 209 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 210 | for (i = 1; i < argc ; i++) { |
| 211 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 212 | debug++; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 213 | if ((!strcmp(argv[i], "-debugent")) || (!strcmp(argv[i], "--debugent"))) |
| 214 | debugent++; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 215 | else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 216 | copy++; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 217 | else if ((!strcmp(argv[i], "-recover")) || |
| 218 | (!strcmp(argv[i], "--recover"))) |
| 219 | recovery++; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 220 | else if ((!strcmp(argv[i], "-noent")) || |
| 221 | (!strcmp(argv[i], "--noent"))) |
| 222 | noent++; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 223 | else if ((!strcmp(argv[i], "-noout")) || |
| 224 | (!strcmp(argv[i], "--noout"))) |
| 225 | noout++; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 226 | else if ((!strcmp(argv[i], "-valid")) || |
| 227 | (!strcmp(argv[i], "--valid"))) |
| 228 | valid++; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 229 | else if ((!strcmp(argv[i], "-postvalid")) || |
| 230 | (!strcmp(argv[i], "--postvalid"))) |
| 231 | postvalid++; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 232 | else if ((!strcmp(argv[i], "-insert")) || |
| 233 | (!strcmp(argv[i], "--insert"))) |
| 234 | insert++; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 235 | else if ((!strcmp(argv[i], "-repeat")) || |
| 236 | (!strcmp(argv[i], "--repeat"))) |
| 237 | repeat++; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 238 | else if ((!strcmp(argv[i], "-push")) || |
| 239 | (!strcmp(argv[i], "--push"))) |
| 240 | push++; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 241 | else if ((!strcmp(argv[i], "-compress")) || |
| 242 | (!strcmp(argv[i], "--compress"))) { |
| 243 | compress++; |
| 244 | xmlSetCompressMode(9); |
| 245 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 246 | else if ((!strcmp(argv[i], "-html")) || |
| 247 | (!strcmp(argv[i], "--html"))) { |
| 248 | html++; |
| 249 | } |
| 250 | else if ((!strcmp(argv[i], "-shell")) || |
| 251 | (!strcmp(argv[i], "--shell"))) { |
| 252 | shell++; |
| 253 | noout = 1; |
| 254 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 255 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 256 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 257 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 258 | for (i = 1; i < argc ; i++) { |
| 259 | if (argv[i][0] != '-') { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 260 | if (repeat) { |
| 261 | for (count = 0;count < 100 * repeat;count++) |
| 262 | parseAndPrintFile(argv[i]); |
| 263 | } else |
| 264 | parseAndPrintFile(argv[i]); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 265 | files ++; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 266 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 267 | } |
| 268 | if (files == 0) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 269 | printf("Usage : %s [--debug] [--debugent] [--copy] [--recover] [--noent] [--noout] [--valid] [--repeat] XMLfiles ...\n", |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 270 | argv[0]); |
| 271 | printf("\tParse the XML files and output the result of the parsing\n"); |
| 272 | printf("\t--debug : dump a debug tree of the in-memory document\n"); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 273 | printf("\t--debugent : debug the entities defined in the document\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 274 | printf("\t--copy : used to test the internal copy implementation\n"); |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 275 | printf("\t--recover : output what was parsable on broken XML documents\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 276 | printf("\t--noent : substitute entity references by their value\n"); |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 277 | printf("\t--noout : don't output the result tree\n"); |
Daniel Veillard | 6077d03 | 1999-10-09 09:11:45 +0000 | [diff] [blame] | 278 | printf("\t--valid : validate the document in addition to std well-formed check\n"); |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 279 | printf("\t--postvalid : do a posteriori validation, i.e after parsing\n"); |
| 280 | printf("\t--repeat : repeat 100 times, for timing or profiling\n"); |
| 281 | printf("\t--insert : ad-hoc test for valid insertions\n"); |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 282 | printf("\t--compress : turn on gzip compression of output\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 283 | printf("\t--html : use the HTML parser\n"); |
| 284 | printf("\t--shell : run a navigating shell\n"); |
| 285 | printf("\t--push : use the push mode of the parser\n"); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 286 | } |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 287 | xmlCleanupParser(); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 288 | xmlMemoryDump(); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 289 | |
| 290 | return(0); |
| 291 | } |