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 | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 36 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 37 | /* |
| 38 | * Note: there is a couple of errors introduced on purpose. |
| 39 | */ |
| 40 | static CHAR buffer[] = |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 41 | "\n\ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame^] | 42 | <?xml version=\"1.0\"?>\n\ |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 43 | <?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\ |
| 44 | <?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\ |
| 45 | <D:propertyupdate>\n\ |
| 46 | <D:set a=\"'toto'\" b>\n\ |
| 47 | <D:prop>\n\ |
| 48 | <Z:authors>\n\ |
| 49 | <Z:Author>Jim Whitehead</Z:Author>\n\ |
| 50 | <Z:Author>Roy Fielding</Z:Author>\n\ |
| 51 | </Z:authors>\n\ |
| 52 | </D:prop>\n\ |
| 53 | </D:set>\n\ |
| 54 | <D:remove>\n\ |
| 55 | <D:prop><Z:Copyright-Owner/></D:prop>\n\ |
| 56 | </D:remove>\n\ |
| 57 | </D:propertyupdate>\n\ |
| 58 | \n\ |
| 59 | "; |
| 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"); |
| 78 | tree = xmlNewChild(doc->root, NULL, "head", NULL); |
| 79 | subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome"); |
| 80 | tree = xmlNewChild(doc->root, NULL, "chapter", NULL); |
| 81 | subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure"); |
| 82 | subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ..."); |
| 83 | subtree = xmlNewChild(tree, NULL, "image", NULL); |
| 84 | xmlSetProp(subtree, "href", "linus.gif"); |
| 85 | |
| 86 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 87 | * test intermediate copy if needed. |
| 88 | */ |
| 89 | if (copy) { |
| 90 | tmp = doc; |
| 91 | doc = xmlCopyDoc(doc, 1); |
| 92 | xmlFreeDoc(tmp); |
| 93 | } |
| 94 | |
| 95 | /* |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 96 | * print it. |
| 97 | */ |
| 98 | xmlDocDump(stdout, doc); |
| 99 | |
| 100 | /* |
| 101 | * free it. |
| 102 | */ |
| 103 | xmlFreeDoc(doc); |
| 104 | return(0); |
| 105 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 106 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 107 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 108 | xmlDocPtr doc, tmp; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 109 | |
| 110 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 111 | * build an XML tree from a string; |
| 112 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame^] | 113 | if (recovery) |
| 114 | doc = xmlRecoverFile(filename); |
| 115 | else |
| 116 | doc = xmlParseFile(filename); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 119 | * test intermediate copy if needed. |
| 120 | */ |
| 121 | if (copy) { |
| 122 | tmp = doc; |
| 123 | doc = xmlCopyDoc(doc, 1); |
| 124 | xmlFreeDoc(tmp); |
| 125 | } |
| 126 | |
| 127 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 128 | * print it. |
| 129 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 130 | if (!debug) |
| 131 | xmlDocDump(stdout, doc); |
| 132 | else |
| 133 | xmlDebugDumpDocument(stdout, doc); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * free it. |
| 137 | */ |
| 138 | xmlFreeDoc(doc); |
| 139 | } |
| 140 | |
| 141 | void parseAndPrintBuffer(CHAR *buf) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 142 | xmlDocPtr doc, tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * build an XML tree from a string; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 146 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame^] | 147 | if (recovery) |
| 148 | doc = xmlRecoverDoc(buf); |
| 149 | else |
| 150 | doc = xmlParseDoc(buf); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 153 | * test intermediate copy if needed. |
| 154 | */ |
| 155 | if (copy) { |
| 156 | tmp = doc; |
| 157 | doc = xmlCopyDoc(doc, 1); |
| 158 | xmlFreeDoc(tmp); |
| 159 | } |
| 160 | |
| 161 | /* |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 162 | * print it. |
| 163 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 164 | if (!debug) |
| 165 | xmlDocDump(stdout, doc); |
| 166 | else |
| 167 | xmlDebugDumpDocument(stdout, doc); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * free it. |
| 171 | */ |
| 172 | xmlFreeDoc(doc); |
| 173 | } |
| 174 | |
| 175 | int main(int argc, char **argv) { |
| 176 | int i; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 177 | int files = 0; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 178 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 179 | for (i = 1; i < argc ; i++) { |
| 180 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 181 | debug++; |
| 182 | else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 183 | copy++; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame^] | 184 | else if ((!strcmp(argv[i], "-recover")) || |
| 185 | (!strcmp(argv[i], "--recover"))) |
| 186 | recovery++; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 187 | } |
| 188 | for (i = 1; i < argc ; i++) { |
| 189 | if (argv[i][0] != '-') { |
| 190 | parseAndPrintFile(argv[i]); |
| 191 | files ++; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 192 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 193 | } |
| 194 | if (files == 0) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 195 | printf("\nFirst test for the parser, with errors\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 196 | parseAndPrintBuffer(buffer); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 197 | printf("\nBuilding a tree from scratch and printing it\n"); |
| 198 | treeTest(); |
| 199 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 200 | |
| 201 | return(0); |
| 202 | } |