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 | * |
| 6 | * $Id$ |
| 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 | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 35 | /* |
| 36 | * Note: there is a couple of errors introduced on purpose. |
| 37 | */ |
| 38 | static CHAR buffer[] = |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 39 | "\n\ |
| 40 | <?xml version=\"1.0\">\n\ |
| 41 | <?xml:namespace ns = \"http://www.ietf.org/standards/dav/\" prefix = \"D\"?>\n\ |
| 42 | <?xml:namespace ns = \"http://www.w3.com/standards/z39.50/\" prefix = \"Z\"?>\n\ |
| 43 | <D:propertyupdate>\n\ |
| 44 | <D:set a=\"'toto'\" b>\n\ |
| 45 | <D:prop>\n\ |
| 46 | <Z:authors>\n\ |
| 47 | <Z:Author>Jim Whitehead</Z:Author>\n\ |
| 48 | <Z:Author>Roy Fielding</Z:Author>\n\ |
| 49 | </Z:authors>\n\ |
| 50 | </D:prop>\n\ |
| 51 | </D:set>\n\ |
| 52 | <D:remove>\n\ |
| 53 | <D:prop><Z:Copyright-Owner/></D:prop>\n\ |
| 54 | </D:remove>\n\ |
| 55 | </D:propertyupdate>\n\ |
| 56 | \n\ |
| 57 | "; |
| 58 | |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame^] | 59 | /************************************************************************ |
| 60 | * * |
| 61 | * Debug * |
| 62 | * * |
| 63 | ************************************************************************/ |
| 64 | |
| 65 | int treeTest(void) { |
| 66 | /* |
| 67 | * build a fake XML document |
| 68 | */ |
| 69 | xmlDocPtr doc; |
| 70 | xmlNodePtr tree, subtree; |
| 71 | |
| 72 | doc = xmlNewDoc("1.0"); |
| 73 | doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL); |
| 74 | xmlSetProp(doc->root, "prop1", "gnome is great"); |
| 75 | xmlSetProp(doc->root, "prop2", "&linux; too"); |
| 76 | tree = xmlNewChild(doc->root, NULL, "head", NULL); |
| 77 | subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome"); |
| 78 | tree = xmlNewChild(doc->root, NULL, "chapter", NULL); |
| 79 | subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure"); |
| 80 | subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ..."); |
| 81 | subtree = xmlNewChild(tree, NULL, "image", NULL); |
| 82 | xmlSetProp(subtree, "href", "linus.gif"); |
| 83 | |
| 84 | /* |
| 85 | * print it. |
| 86 | */ |
| 87 | xmlDocDump(stdout, doc); |
| 88 | |
| 89 | /* |
| 90 | * free it. |
| 91 | */ |
| 92 | xmlFreeDoc(doc); |
| 93 | return(0); |
| 94 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 95 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 96 | void parseAndPrintFile(char *filename) { |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 97 | xmlDocPtr doc; |
| 98 | |
| 99 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 100 | * build an XML tree from a string; |
| 101 | */ |
| 102 | doc = xmlParseFile(filename); |
| 103 | |
| 104 | /* |
| 105 | * print it. |
| 106 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 107 | if (!debug) |
| 108 | xmlDocDump(stdout, doc); |
| 109 | else |
| 110 | xmlDebugDumpDocument(stdout, doc); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * free it. |
| 114 | */ |
| 115 | xmlFreeDoc(doc); |
| 116 | } |
| 117 | |
| 118 | void parseAndPrintBuffer(CHAR *buf) { |
| 119 | xmlDocPtr doc; |
| 120 | |
| 121 | /* |
| 122 | * build an XML tree from a string; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 123 | */ |
| 124 | doc = xmlParseDoc(buf); |
| 125 | |
| 126 | /* |
| 127 | * print it. |
| 128 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 129 | if (!debug) |
| 130 | xmlDocDump(stdout, doc); |
| 131 | else |
| 132 | xmlDebugDumpDocument(stdout, doc); |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * free it. |
| 136 | */ |
| 137 | xmlFreeDoc(doc); |
| 138 | } |
| 139 | |
| 140 | int main(int argc, char **argv) { |
| 141 | int i; |
| 142 | |
| 143 | if (argc > 1) { |
| 144 | for (i = 1; i < argc ; i++) { |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 145 | if ((strcmp(argv[i], "-debug")) && (strcmp(argv[i], "--debug"))) |
| 146 | parseAndPrintFile(argv[i]); |
| 147 | else |
| 148 | debug++; |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 149 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame^] | 150 | } else { |
| 151 | printf("\nFirst test for the parser, with errors\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 152 | parseAndPrintBuffer(buffer); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame^] | 153 | printf("\nBuilding a tree from scratch and printing it\n"); |
| 154 | treeTest(); |
| 155 | } |
Daniel Veillard | 01791d5 | 1998-07-24 19:24:09 +0000 | [diff] [blame] | 156 | |
| 157 | return(0); |
| 158 | } |