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