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 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * daniel@veillard.com |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 9 | #include "libxml.h" |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 10 | #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED) |
| 11 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | |
| 14 | #ifdef HAVE_SYS_TYPES_H |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 16 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 17 | #ifdef HAVE_SYS_STAT_H |
| 18 | #include <sys/stat.h> |
| 19 | #endif |
| 20 | #ifdef HAVE_FCNTL_H |
| 21 | #include <fcntl.h> |
| 22 | #endif |
| 23 | #ifdef HAVE_UNISTD_H |
| 24 | #include <unistd.h> |
| 25 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 26 | #ifdef HAVE_STDLIB_H |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 30 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 31 | #include <libxml/xpath.h> |
| 32 | #include <libxml/tree.h> |
| 33 | #include <libxml/parser.h> |
| 34 | #include <libxml/debugXML.h> |
| 35 | #include <libxml/xmlmemory.h> |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 36 | #include <libxml/parserInternals.h> |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 37 | #include <libxml/xpathInternals.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 38 | #include <libxml/xmlerror.h> |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 39 | #if defined(LIBXML_XPTR_ENABLED) |
| 40 | #include <libxml/xpointer.h> |
| 41 | static int xptr = 0; |
| 42 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 43 | static int debug = 0; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 44 | static int valid = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 45 | static int expr = 0; |
Daniel Veillard | b38bd55 | 2001-04-03 18:22:00 +0000 | [diff] [blame] | 46 | static int tree = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 47 | static xmlDocPtr document = NULL; |
| 48 | |
| 49 | /* |
| 50 | * Default document |
| 51 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 52 | static xmlChar buffer[] = |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 53 | "<?xml version=\"1.0\"?>\n\ |
| 54 | <EXAMPLE prop1=\"gnome is great\" prop2=\"& linux too\">\n\ |
| 55 | <head>\n\ |
| 56 | <title>Welcome to Gnome</title>\n\ |
| 57 | </head>\n\ |
| 58 | <chapter>\n\ |
| 59 | <title>The Linux adventure</title>\n\ |
| 60 | <p>bla bla bla ...</p>\n\ |
| 61 | <image href=\"linus.gif\"/>\n\ |
| 62 | <p>...</p>\n\ |
| 63 | </chapter>\n\ |
| 64 | <chapter>\n\ |
| 65 | <title>Chapter 2</title>\n\ |
| 66 | <p>this is chapter 2 ...</p>\n\ |
| 67 | </chapter>\n\ |
| 68 | <chapter>\n\ |
| 69 | <title>Chapter 3</title>\n\ |
| 70 | <p>this is chapter 3 ...</p>\n\ |
| 71 | </chapter>\n\ |
| 72 | <chapter>\n\ |
| 73 | <title>Chapter 4</title>\n\ |
| 74 | <p>this is chapter 4 ...</p>\n\ |
| 75 | </chapter>\n\ |
| 76 | <chapter>\n\ |
| 77 | <title>Chapter 5</title>\n\ |
| 78 | <p>this is chapter 5 ...</p>\n\ |
| 79 | </chapter>\n\ |
| 80 | </EXAMPLE>\n\ |
| 81 | "; |
| 82 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 83 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 84 | static void |
| 85 | testXPath(const char *str) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 86 | xmlXPathObjectPtr res; |
| 87 | xmlXPathContextPtr ctxt; |
| 88 | |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 89 | #if defined(LIBXML_XPTR_ENABLED) |
| 90 | if (xptr) { |
| 91 | ctxt = xmlXPtrNewContext(document, NULL, NULL); |
| 92 | res = xmlXPtrEval(BAD_CAST str, ctxt); |
| 93 | } else { |
| 94 | #endif |
| 95 | ctxt = xmlXPathNewContext(document); |
Daniel Veillard | 0810898 | 2001-01-03 15:24:58 +0000 | [diff] [blame] | 96 | ctxt->node = xmlDocGetRootElement(document); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 97 | if (expr) |
| 98 | res = xmlXPathEvalExpression(BAD_CAST str, ctxt); |
Daniel Veillard | b38bd55 | 2001-04-03 18:22:00 +0000 | [diff] [blame] | 99 | else { |
| 100 | /* res = xmlXPathEval(BAD_CAST str, ctxt); */ |
| 101 | xmlXPathCompExprPtr comp; |
| 102 | |
| 103 | comp = xmlXPathCompile(BAD_CAST str); |
| 104 | if (comp != NULL) { |
| 105 | if (tree) |
| 106 | xmlXPathDebugDumpCompExpr(stdout, comp, 0); |
| 107 | |
| 108 | res = xmlXPathCompiledEval(comp, ctxt); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 109 | xmlXPathFreeCompExpr(comp); |
Daniel Veillard | b38bd55 | 2001-04-03 18:22:00 +0000 | [diff] [blame] | 110 | } else |
| 111 | res = NULL; |
| 112 | } |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 113 | #if defined(LIBXML_XPTR_ENABLED) |
| 114 | } |
| 115 | #endif |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 116 | xmlXPathDebugDumpObject(stdout, res, 0); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 117 | xmlXPathFreeObject(res); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 118 | xmlXPathFreeContext(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 121 | static void |
| 122 | testXPathFile(const char *filename) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 123 | FILE *input; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 124 | char expression[5000]; |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 125 | int len; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 126 | |
| 127 | input = fopen(filename, "r"); |
| 128 | if (input == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 129 | xmlGenericError(xmlGenericErrorContext, |
| 130 | "Cannot open %s for reading\n", filename); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 133 | while (fgets(expression, 4500, input) != NULL) { |
| 134 | len = strlen(expression); |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 135 | len--; |
| 136 | while ((len >= 0) && |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 137 | ((expression[len] == '\n') || (expression[len] == '\t') || |
| 138 | (expression[len] == '\r') || (expression[len] == ' '))) len--; |
| 139 | expression[len + 1] = 0; |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 140 | if (len >= 0) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 141 | printf("\n========================\nExpression: %s\n", expression) ; |
| 142 | testXPath(expression); |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 143 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | fclose(input); |
| 147 | } |
| 148 | |
| 149 | int main(int argc, char **argv) { |
| 150 | int i; |
| 151 | int strings = 0; |
| 152 | int usefile = 0; |
| 153 | char *filename = NULL; |
| 154 | |
| 155 | for (i = 1; i < argc ; i++) { |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 156 | #if defined(LIBXML_XPTR_ENABLED) |
| 157 | if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr"))) |
| 158 | xptr++; |
| 159 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 160 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 161 | debug++; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 162 | if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid"))) |
| 163 | valid++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 164 | if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr"))) |
| 165 | expr++; |
Daniel Veillard | b38bd55 | 2001-04-03 18:22:00 +0000 | [diff] [blame] | 166 | if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) |
| 167 | tree++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 168 | if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) |
| 169 | filename = argv[++i]; |
| 170 | if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file"))) |
| 171 | usefile++; |
| 172 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 173 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 174 | if (document == NULL) { |
| 175 | if (filename == NULL) |
| 176 | document = xmlParseDoc(buffer); |
| 177 | else |
| 178 | document = xmlParseFile(filename); |
| 179 | } |
| 180 | for (i = 1; i < argc ; i++) { |
| 181 | if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) { |
| 182 | i++; continue; |
| 183 | } |
| 184 | if (argv[i][0] != '-') { |
| 185 | if (usefile) |
| 186 | testXPathFile(argv[i]); |
| 187 | else |
| 188 | testXPath(argv[i]); |
| 189 | strings ++; |
| 190 | } |
| 191 | } |
| 192 | if (strings == 0) { |
| 193 | printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n", |
| 194 | argv[0]); |
| 195 | printf("\tParse the XPath strings and output the result of the parsing\n"); |
| 196 | printf("\t--debug : dump a debug version of the result\n"); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 197 | printf("\t--valid : switch on DTD support in the parser\n"); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 198 | #if defined(LIBXML_XPTR_ENABLED) |
| 199 | printf("\t--xptr : expressions are XPointer expressions\n"); |
| 200 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 201 | printf("\t--expr : debug XPath expressions only\n"); |
Daniel Veillard | b38bd55 | 2001-04-03 18:22:00 +0000 | [diff] [blame] | 202 | printf("\t--tree : show the compiled XPath tree\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 203 | printf("\t--input filename : or\n"); |
| 204 | printf("\t-i filename : read the document from filename\n"); |
| 205 | printf("\t--file : or\n"); |
| 206 | printf("\t-f : read queries from files, args\n"); |
| 207 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 208 | if (document != NULL) |
| 209 | xmlFreeDoc(document); |
| 210 | xmlCleanupParser(); |
| 211 | xmlMemoryDump(); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 212 | |
| 213 | return(0); |
| 214 | } |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 215 | #else |
| 216 | #include <stdio.h> |
| 217 | int main(int argc, char **argv) { |
| 218 | printf("%s : XPath/Debug support not compiled in\n", argv[0]); |
| 219 | return(0); |
| 220 | } |
| 221 | #endif /* LIBXML_XPATH_ENABLED */ |