Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * testXPath.c : a small tester program for XPath. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel.Veillard@w3.org |
| 7 | */ |
| 8 | |
| 9 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 10 | #include "win32config.h" |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 11 | #else |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 12 | #include "config.h" |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 13 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 14 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 15 | #include "xmlversion.h" |
| 16 | #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED) |
| 17 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #ifdef HAVE_SYS_TYPES_H |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 23 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 24 | #ifdef HAVE_SYS_STAT_H |
| 25 | #include <sys/stat.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_FCNTL_H |
| 28 | #include <fcntl.h> |
| 29 | #endif |
| 30 | #ifdef HAVE_UNISTD_H |
| 31 | #include <unistd.h> |
| 32 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 33 | #ifdef HAVE_STDLIB_H |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 37 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 38 | #include <libxml/xpath.h> |
| 39 | #include <libxml/tree.h> |
| 40 | #include <libxml/parser.h> |
| 41 | #include <libxml/debugXML.h> |
| 42 | #include <libxml/xmlmemory.h> |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 43 | |
| 44 | static int debug = 0; |
| 45 | static int expr = 0; |
| 46 | static xmlDocPtr document = NULL; |
| 47 | |
| 48 | /* |
| 49 | * Default document |
| 50 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 51 | static xmlChar buffer[] = |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 52 | "<?xml version=\"1.0\"?>\n\ |
| 53 | <EXAMPLE prop1=\"gnome is great\" prop2=\"& linux too\">\n\ |
| 54 | <head>\n\ |
| 55 | <title>Welcome to Gnome</title>\n\ |
| 56 | </head>\n\ |
| 57 | <chapter>\n\ |
| 58 | <title>The Linux adventure</title>\n\ |
| 59 | <p>bla bla bla ...</p>\n\ |
| 60 | <image href=\"linus.gif\"/>\n\ |
| 61 | <p>...</p>\n\ |
| 62 | </chapter>\n\ |
| 63 | <chapter>\n\ |
| 64 | <title>Chapter 2</title>\n\ |
| 65 | <p>this is chapter 2 ...</p>\n\ |
| 66 | </chapter>\n\ |
| 67 | <chapter>\n\ |
| 68 | <title>Chapter 3</title>\n\ |
| 69 | <p>this is chapter 3 ...</p>\n\ |
| 70 | </chapter>\n\ |
| 71 | <chapter>\n\ |
| 72 | <title>Chapter 4</title>\n\ |
| 73 | <p>this is chapter 4 ...</p>\n\ |
| 74 | </chapter>\n\ |
| 75 | <chapter>\n\ |
| 76 | <title>Chapter 5</title>\n\ |
| 77 | <p>this is chapter 5 ...</p>\n\ |
| 78 | </chapter>\n\ |
| 79 | </EXAMPLE>\n\ |
| 80 | "; |
| 81 | |
| 82 | void xmlXPAthDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur) { |
| 83 | int i; |
| 84 | |
| 85 | if (cur == NULL) { |
| 86 | fprintf(output, "NodeSet is NULL !\n"); |
| 87 | return; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 92 | for (i = 0;i < cur->nodeNr;i++) { |
| 93 | fprintf(output, "%d", i + 1); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 94 | if (cur->nodeTab[i] == NULL) |
| 95 | fprintf(output, " NULL\n"); |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 96 | else if ((cur->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 97 | (cur->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 98 | fprintf(output, " /\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 99 | else if (cur->nodeTab[i]->type == XML_ATTRIBUTE_NODE) |
| 100 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur->nodeTab[i], 2); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 101 | else |
| 102 | xmlDebugDumpOneNode(output, cur->nodeTab[i], 2); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | void xmlXPAthDebugDumpObject(FILE *output, xmlXPathObjectPtr cur) { |
| 107 | if (cur == NULL) { |
| 108 | fprintf(output, "Object is empty (NULL)\n"); |
| 109 | return; |
| 110 | } |
| 111 | switch(cur->type) { |
| 112 | case XPATH_UNDEFINED: |
| 113 | fprintf(output, "Object is uninitialized\n"); |
| 114 | break; |
| 115 | case XPATH_NODESET: |
| 116 | fprintf(output, "Object is a Node Set :\n"); |
| 117 | xmlXPAthDebugDumpNodeSet(output, cur->nodesetval); |
| 118 | break; |
| 119 | case XPATH_BOOLEAN: |
| 120 | fprintf(output, "Object is a Boolean : "); |
| 121 | if (cur->boolval) fprintf(output, "true\n"); |
| 122 | else fprintf(output, "false\n"); |
| 123 | break; |
| 124 | case XPATH_NUMBER: |
| 125 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 126 | break; |
| 127 | case XPATH_STRING: |
| 128 | fprintf(output, "Object is a string : "); |
| 129 | xmlDebugDumpString(output, cur->stringval); |
| 130 | fprintf(output, "\n"); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void testXPath(const char *str) { |
| 136 | xmlXPathObjectPtr res; |
| 137 | xmlXPathContextPtr ctxt; |
| 138 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 139 | ctxt = xmlXPathNewContext(document); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 140 | if (expr) |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 141 | res = xmlXPathEvalExpression(BAD_CAST str, ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 142 | else |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 143 | res = xmlXPathEval(BAD_CAST str, ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 144 | xmlXPAthDebugDumpObject(stdout, res); |
| 145 | xmlXPathFreeObject(res); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 146 | xmlXPathFreeContext(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void testXPathFile(const char *filename) { |
| 150 | FILE *input; |
| 151 | char expr[5000]; |
| 152 | |
| 153 | input = fopen(filename, "r"); |
| 154 | if (input == NULL) { |
| 155 | fprintf(stderr, "Cannot open %s for reading\n", filename); |
| 156 | return; |
| 157 | } |
| 158 | while (fscanf(input, "%s", expr) != EOF) { |
| 159 | testXPath(expr); |
| 160 | } |
| 161 | |
| 162 | fclose(input); |
| 163 | } |
| 164 | |
| 165 | int main(int argc, char **argv) { |
| 166 | int i; |
| 167 | int strings = 0; |
| 168 | int usefile = 0; |
| 169 | char *filename = NULL; |
| 170 | |
| 171 | for (i = 1; i < argc ; i++) { |
| 172 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 173 | debug++; |
| 174 | if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr"))) |
| 175 | expr++; |
| 176 | if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) |
| 177 | filename = argv[++i]; |
| 178 | if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file"))) |
| 179 | usefile++; |
| 180 | } |
| 181 | if (document == NULL) { |
| 182 | if (filename == NULL) |
| 183 | document = xmlParseDoc(buffer); |
| 184 | else |
| 185 | document = xmlParseFile(filename); |
| 186 | } |
| 187 | for (i = 1; i < argc ; i++) { |
| 188 | if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) { |
| 189 | i++; continue; |
| 190 | } |
| 191 | if (argv[i][0] != '-') { |
| 192 | if (usefile) |
| 193 | testXPathFile(argv[i]); |
| 194 | else |
| 195 | testXPath(argv[i]); |
| 196 | strings ++; |
| 197 | } |
| 198 | } |
| 199 | if (strings == 0) { |
| 200 | printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n", |
| 201 | argv[0]); |
| 202 | printf("\tParse the XPath strings and output the result of the parsing\n"); |
| 203 | printf("\t--debug : dump a debug version of the result\n"); |
| 204 | printf("\t--expr : debug XPath expressions only\n"); |
| 205 | printf("\t--input filename : or\n"); |
| 206 | printf("\t-i filename : read the document from filename\n"); |
| 207 | printf("\t--file : or\n"); |
| 208 | printf("\t-f : read queries from files, args\n"); |
| 209 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 210 | if (document != NULL) |
| 211 | xmlFreeDoc(document); |
| 212 | xmlCleanupParser(); |
| 213 | xmlMemoryDump(); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 214 | |
| 215 | return(0); |
| 216 | } |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 217 | #else |
| 218 | #include <stdio.h> |
| 219 | int main(int argc, char **argv) { |
| 220 | printf("%s : XPath/Debug support not compiled in\n", argv[0]); |
| 221 | return(0); |
| 222 | } |
| 223 | #endif /* LIBXML_XPATH_ENABLED */ |