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 | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 33 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 34 | #include "xmlmemory.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 35 | #include "parser.h" |
| 36 | #include "tree.h" |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 37 | #include "debugXML.h" |
| 38 | |
| 39 | static int debug = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 40 | static int debugent = 0; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 41 | static int copy = 0; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 42 | static int recovery = 0; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 43 | static int noent = 0; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 44 | static int noout = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 45 | static int valid = 0; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 46 | static int postvalid = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 47 | static int repeat = 0; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 48 | static int insert = 0; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 49 | static int compress = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 50 | |
| 51 | extern int xmlDoValidityCheckingDefaultValue; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 52 | |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 54 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 55 | xmlDocPtr doc, tmp; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 58 | * build an XML tree from a string; |
| 59 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 60 | if (recovery) |
| 61 | doc = xmlRecoverFile(filename); |
| 62 | else |
| 63 | doc = xmlParseFile(filename); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 66 | * test intermediate copy if needed. |
| 67 | */ |
| 68 | if (copy) { |
| 69 | tmp = doc; |
| 70 | doc = xmlCopyDoc(doc, 1); |
| 71 | xmlFreeDoc(tmp); |
| 72 | } |
| 73 | |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 74 | 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 Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 101 | if (!debug) { |
| 102 | if (compress) |
| 103 | xmlSaveFile("-", doc); |
| 104 | else |
| 105 | xmlDocDump(stdout, doc); |
| 106 | } else |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 107 | xmlDebugDumpDocument(stdout, doc); |
| 108 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 109 | |
| 110 | /* |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 111 | * A posteriori validation test |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 112 | */ |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 113 | if (postvalid) { |
| 114 | xmlValidCtxt cvp; |
| 115 | cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf; |
| 116 | xmlValidateDocument(&cvp, doc); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 119 | if (debugent) |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 120 | xmlDebugDumpEntities(stdout, doc); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * free it. |
| 124 | */ |
| 125 | xmlFreeDoc(doc); |
| 126 | } |
| 127 | |
| 128 | int main(int argc, char **argv) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 129 | int i, count; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 130 | int files = 0; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 131 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 132 | for (i = 1; i < argc ; i++) { |
| 133 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 134 | debug++; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 135 | if ((!strcmp(argv[i], "-debugent")) || (!strcmp(argv[i], "--debugent"))) |
| 136 | debugent++; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 137 | else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 138 | copy++; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 139 | else if ((!strcmp(argv[i], "-recover")) || |
| 140 | (!strcmp(argv[i], "--recover"))) |
| 141 | recovery++; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 142 | else if ((!strcmp(argv[i], "-noent")) || |
| 143 | (!strcmp(argv[i], "--noent"))) |
| 144 | noent++; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 145 | else if ((!strcmp(argv[i], "-noout")) || |
| 146 | (!strcmp(argv[i], "--noout"))) |
| 147 | noout++; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 148 | else if ((!strcmp(argv[i], "-valid")) || |
| 149 | (!strcmp(argv[i], "--valid"))) |
| 150 | valid++; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 151 | else if ((!strcmp(argv[i], "-postvalid")) || |
| 152 | (!strcmp(argv[i], "--postvalid"))) |
| 153 | postvalid++; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 154 | else if ((!strcmp(argv[i], "-insert")) || |
| 155 | (!strcmp(argv[i], "--insert"))) |
| 156 | insert++; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 157 | else if ((!strcmp(argv[i], "-repeat")) || |
| 158 | (!strcmp(argv[i], "--repeat"))) |
| 159 | repeat++; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 160 | else if ((!strcmp(argv[i], "-compress")) || |
| 161 | (!strcmp(argv[i], "--compress"))) { |
| 162 | compress++; |
| 163 | xmlSetCompressMode(9); |
| 164 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 165 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 166 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 167 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 168 | for (i = 1; i < argc ; i++) { |
| 169 | if (argv[i][0] != '-') { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 170 | if (repeat) { |
| 171 | for (count = 0;count < 100 * repeat;count++) |
| 172 | parseAndPrintFile(argv[i]); |
| 173 | } else |
| 174 | parseAndPrintFile(argv[i]); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 175 | files ++; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 176 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 177 | } |
| 178 | if (files == 0) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 179 | 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] | 180 | 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 Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 183 | printf("\t--debugent : debug the entities defined in the document\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 184 | printf("\t--copy : used to test the internal copy implementation\n"); |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 185 | printf("\t--recover : output what was parsable on broken XML documents\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 186 | printf("\t--noent : substitute entity references by their value\n"); |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 187 | printf("\t--noout : don't output the result tree\n"); |
Daniel Veillard | 6077d03 | 1999-10-09 09:11:45 +0000 | [diff] [blame] | 188 | 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] | 189 | 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 Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 192 | printf("\t--compress : turn on gzip compression of output\n"); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 193 | } |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 194 | xmlCleanupParser(); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 195 | xmlMemoryDump(); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 196 | |
| 197 | return(0); |
| 198 | } |