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 |
| 10 | #define HAVE_FCNTL_H |
| 11 | #include <io.h> |
| 12 | #else |
| 13 | #include <config.h> |
| 14 | #endif |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 16 | #ifdef HAVE_SYS_STAT_H |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 17 | #include <sys/stat.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 18 | #endif |
| 19 | #ifdef HAVE_FCNTL_H |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #ifdef HAVE_UNISTD_H |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 24 | #endif |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <string.h> |
Seth Alves | e7f12e6 | 1998-10-01 20:51:15 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 28 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 29 | #include "parser.h" |
| 30 | #include "tree.h" |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 31 | #include "debugXML.h" |
| 32 | |
| 33 | static int debug = 0; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 34 | static int copy = 0; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 35 | static int recovery = 0; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 36 | static int noent = 0; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 37 | static int noout = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 38 | static int valid = 0; |
| 39 | static int repeat = 0; |
| 40 | |
| 41 | extern int xmlDoValidityCheckingDefaultValue; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 42 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Note: there is a couple of errors introduced on purpose. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 45 | static CHAR buffer[] = |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 46 | "<?xml version=\"1.0\"?>\n\ |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 47 | <?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\ |
| 48 | <?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\ |
| 49 | <D:propertyupdate>\n\ |
| 50 | <D:set a=\"'toto'\" b>\n\ |
| 51 | <D:prop>\n\ |
| 52 | <Z:authors>\n\ |
| 53 | <Z:Author>Jim Whitehead</Z:Author>\n\ |
| 54 | <Z:Author>Roy Fielding</Z:Author>\n\ |
| 55 | </Z:authors>\n\ |
| 56 | </D:prop>\n\ |
| 57 | </D:set>\n\ |
| 58 | <D:remove>\n\ |
| 59 | <D:prop><Z:Copyright-Owner/></D:prop>\n\ |
| 60 | </D:remove>\n\ |
| 61 | </D:propertyupdate>\n\ |
| 62 | \n\ |
| 63 | "; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 64 | */ |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 66 | /************************************************************************ |
| 67 | * * |
| 68 | * Debug * |
| 69 | * * |
| 70 | ************************************************************************/ |
| 71 | |
| 72 | int treeTest(void) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 73 | xmlDocPtr doc, tmp; |
| 74 | xmlNodePtr tree, subtree; |
| 75 | |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 76 | /* |
| 77 | * build a fake XML document |
| 78 | */ |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 79 | doc = xmlNewDoc("1.0"); |
| 80 | doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL); |
| 81 | xmlSetProp(doc->root, "prop1", "gnome is great"); |
| 82 | xmlSetProp(doc->root, "prop2", "&linux; too"); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 83 | xmlSetProp(doc->root, "emptyprop", ""); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 84 | tree = xmlNewChild(doc->root, NULL, "head", NULL); |
| 85 | subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome"); |
| 86 | tree = xmlNewChild(doc->root, NULL, "chapter", NULL); |
| 87 | subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure"); |
| 88 | subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ..."); |
| 89 | subtree = xmlNewChild(tree, NULL, "image", NULL); |
| 90 | xmlSetProp(subtree, "href", "linus.gif"); |
| 91 | |
| 92 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 93 | * test intermediate copy if needed. |
| 94 | */ |
| 95 | if (copy) { |
| 96 | tmp = doc; |
| 97 | doc = xmlCopyDoc(doc, 1); |
| 98 | xmlFreeDoc(tmp); |
| 99 | } |
| 100 | |
| 101 | /* |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 102 | * print it. |
| 103 | */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 104 | if (noout == 0) |
| 105 | xmlDocDump(stdout, doc); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * free it. |
| 109 | */ |
| 110 | xmlFreeDoc(doc); |
| 111 | return(0); |
| 112 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 113 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 114 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 115 | xmlDocPtr doc, tmp; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 118 | * build an XML tree from a string; |
| 119 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 120 | if (recovery) |
| 121 | doc = xmlRecoverFile(filename); |
| 122 | else |
| 123 | doc = xmlParseFile(filename); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 124 | |
| 125 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 126 | * test intermediate copy if needed. |
| 127 | */ |
| 128 | if (copy) { |
| 129 | tmp = doc; |
| 130 | doc = xmlCopyDoc(doc, 1); |
| 131 | xmlFreeDoc(tmp); |
| 132 | } |
| 133 | |
| 134 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 135 | * print it. |
| 136 | */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 137 | if (noout == 0) { |
| 138 | if (!debug) |
| 139 | xmlDocDump(stdout, doc); |
| 140 | else |
| 141 | xmlDebugDumpDocument(stdout, doc); |
| 142 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * free it. |
| 146 | */ |
| 147 | xmlFreeDoc(doc); |
| 148 | } |
| 149 | |
| 150 | void parseAndPrintBuffer(CHAR *buf) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 151 | xmlDocPtr doc, tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * build an XML tree from a string; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 155 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 156 | if (recovery) |
| 157 | doc = xmlRecoverDoc(buf); |
| 158 | else |
| 159 | doc = xmlParseDoc(buf); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 160 | |
| 161 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 162 | * test intermediate copy if needed. |
| 163 | */ |
| 164 | if (copy) { |
| 165 | tmp = doc; |
| 166 | doc = xmlCopyDoc(doc, 1); |
| 167 | xmlFreeDoc(tmp); |
| 168 | } |
| 169 | |
| 170 | /* |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 171 | * print it. |
| 172 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 173 | if (!debug) |
| 174 | xmlDocDump(stdout, doc); |
| 175 | else |
| 176 | xmlDebugDumpDocument(stdout, doc); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * free it. |
| 180 | */ |
| 181 | xmlFreeDoc(doc); |
| 182 | } |
| 183 | |
| 184 | int main(int argc, char **argv) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 185 | int i, count; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 186 | int files = 0; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 187 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 188 | for (i = 1; i < argc ; i++) { |
| 189 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 190 | debug++; |
| 191 | else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 192 | copy++; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 193 | else if ((!strcmp(argv[i], "-recover")) || |
| 194 | (!strcmp(argv[i], "--recover"))) |
| 195 | recovery++; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 196 | else if ((!strcmp(argv[i], "-noent")) || |
| 197 | (!strcmp(argv[i], "--noent"))) |
| 198 | noent++; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 199 | else if ((!strcmp(argv[i], "-noout")) || |
| 200 | (!strcmp(argv[i], "--noout"))) |
| 201 | noout++; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 202 | else if ((!strcmp(argv[i], "-valid")) || |
| 203 | (!strcmp(argv[i], "--valid"))) |
| 204 | valid++; |
| 205 | else if ((!strcmp(argv[i], "-repeat")) || |
| 206 | (!strcmp(argv[i], "--repeat"))) |
| 207 | repeat++; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 208 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 209 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 210 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 211 | for (i = 1; i < argc ; i++) { |
| 212 | if (argv[i][0] != '-') { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 213 | if (repeat) { |
| 214 | for (count = 0;count < 100 * repeat;count++) |
| 215 | parseAndPrintFile(argv[i]); |
| 216 | } else |
| 217 | parseAndPrintFile(argv[i]); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 218 | files ++; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 219 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 220 | } |
| 221 | if (files == 0) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 222 | printf("Usage : %s [--debug] [--copy] [--recover] [--noent] [--noout] XMLfiles ...\n", |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 223 | argv[0]); |
| 224 | printf("\tParse the XML files and output the result of the parsing\n"); |
| 225 | printf("\t--debug : dump a debug tree of the in-memory document\n"); |
| 226 | printf("\t--copy : used to test the internal copy implementation\n"); |
| 227 | printf("\t--recover : output what is parsable on broken XmL documents\n"); |
| 228 | printf("\t--noent : substitute entity references by their value\n"); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 229 | printf("\t--noout : don't output the result\n"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 230 | printf("\t--repeat : parse the file 100 times, for timing or profiling\n"); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 231 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 232 | |
| 233 | return(0); |
| 234 | } |