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