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