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