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