blob: bfbb56ac1f82eb5fd0bd3eb5ba5d7e0032e477c2 [file] [log] [blame]
Daniel Veillard1566d3a1999-07-15 14:24:29 +00001/*
2 * testXPath.c : a small tester program for XPath.
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Daniel Veillard1566d3a1999-07-15 14:24:29 +00007 */
8
Bjorn Reese70a9da52001-04-21 16:57:29 +00009#include "libxml.h"
Daniel Veillard361d8452000-04-03 19:48:13 +000010#if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED)
11
Daniel Veillard7f7d1111999-09-22 09:46:25 +000012#include <string.h>
13
14#ifdef HAVE_SYS_TYPES_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000015#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000016#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000017#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 Veillard7f7d1111999-09-22 09:46:25 +000026#ifdef HAVE_STDLIB_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000027#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000028#endif
29
Daniel Veillard1566d3a1999-07-15 14:24:29 +000030
Daniel Veillard361d8452000-04-03 19:48:13 +000031#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 Veillard740abf52000-10-02 23:04:54 +000036#include <libxml/parserInternals.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000037#include <libxml/xpathInternals.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000038#include <libxml/xmlerror.h>
Daniel Veillardac260302000-10-04 13:33:43 +000039#if defined(LIBXML_XPTR_ENABLED)
40#include <libxml/xpointer.h>
41static int xptr = 0;
42#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000043static int debug = 0;
Daniel Veillard740abf52000-10-02 23:04:54 +000044static int valid = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000045static int expr = 0;
Daniel Veillardb38bd552001-04-03 18:22:00 +000046static int tree = 0;
Daniel Veillard7583a592001-07-08 13:15:55 +000047static int nocdata = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000048static xmlDocPtr document = NULL;
49
50/*
51 * Default document
52 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000053static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000054"<?xml version=\"1.0\"?>\n\
55<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
56 <head>\n\
57 <title>Welcome to Gnome</title>\n\
58 </head>\n\
59 <chapter>\n\
60 <title>The Linux adventure</title>\n\
61 <p>bla bla bla ...</p>\n\
62 <image href=\"linus.gif\"/>\n\
63 <p>...</p>\n\
64 </chapter>\n\
65 <chapter>\n\
66 <title>Chapter 2</title>\n\
67 <p>this is chapter 2 ...</p>\n\
68 </chapter>\n\
69 <chapter>\n\
70 <title>Chapter 3</title>\n\
71 <p>this is chapter 3 ...</p>\n\
72 </chapter>\n\
73 <chapter>\n\
74 <title>Chapter 4</title>\n\
75 <p>this is chapter 4 ...</p>\n\
76 </chapter>\n\
77 <chapter>\n\
78 <title>Chapter 5</title>\n\
79 <p>this is chapter 5 ...</p>\n\
80 </chapter>\n\
81</EXAMPLE>\n\
82";
83
Daniel Veillard1566d3a1999-07-15 14:24:29 +000084
Daniel Veillard56a4cb82001-03-24 17:00:36 +000085static void
86testXPath(const char *str) {
Daniel Veillard1566d3a1999-07-15 14:24:29 +000087 xmlXPathObjectPtr res;
88 xmlXPathContextPtr ctxt;
89
Daniel Veillardac260302000-10-04 13:33:43 +000090#if defined(LIBXML_XPTR_ENABLED)
91 if (xptr) {
92 ctxt = xmlXPtrNewContext(document, NULL, NULL);
93 res = xmlXPtrEval(BAD_CAST str, ctxt);
94 } else {
95#endif
96 ctxt = xmlXPathNewContext(document);
Daniel Veillard08108982001-01-03 15:24:58 +000097 ctxt->node = xmlDocGetRootElement(document);
Daniel Veillardac260302000-10-04 13:33:43 +000098 if (expr)
99 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
Daniel Veillardb38bd552001-04-03 18:22:00 +0000100 else {
101 /* res = xmlXPathEval(BAD_CAST str, ctxt); */
102 xmlXPathCompExprPtr comp;
103
104 comp = xmlXPathCompile(BAD_CAST str);
105 if (comp != NULL) {
106 if (tree)
107 xmlXPathDebugDumpCompExpr(stdout, comp, 0);
108
109 res = xmlXPathCompiledEval(comp, ctxt);
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000110 xmlXPathFreeCompExpr(comp);
Daniel Veillardb38bd552001-04-03 18:22:00 +0000111 } else
112 res = NULL;
113 }
Daniel Veillardac260302000-10-04 13:33:43 +0000114#if defined(LIBXML_XPTR_ENABLED)
115 }
116#endif
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000117 xmlXPathDebugDumpObject(stdout, res, 0);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000118 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000119 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000120}
121
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000122static void
123testXPathFile(const char *filename) {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000124 FILE *input;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000125 char expression[5000];
Daniel Veillard7e99c632000-10-06 12:59:53 +0000126 int len;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000127
128 input = fopen(filename, "r");
129 if (input == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000130 xmlGenericError(xmlGenericErrorContext,
131 "Cannot open %s for reading\n", filename);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000132 return;
133 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000134 while (fgets(expression, 4500, input) != NULL) {
135 len = strlen(expression);
Daniel Veillard7e99c632000-10-06 12:59:53 +0000136 len--;
137 while ((len >= 0) &&
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000138 ((expression[len] == '\n') || (expression[len] == '\t') ||
139 (expression[len] == '\r') || (expression[len] == ' '))) len--;
140 expression[len + 1] = 0;
Daniel Veillard7e99c632000-10-06 12:59:53 +0000141 if (len >= 0) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000142 printf("\n========================\nExpression: %s\n", expression) ;
143 testXPath(expression);
Daniel Veillard7e99c632000-10-06 12:59:53 +0000144 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000145 }
146
147 fclose(input);
148}
149
150int main(int argc, char **argv) {
151 int i;
152 int strings = 0;
153 int usefile = 0;
154 char *filename = NULL;
155
156 for (i = 1; i < argc ; i++) {
Daniel Veillardac260302000-10-04 13:33:43 +0000157#if defined(LIBXML_XPTR_ENABLED)
158 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
159 xptr++;
Daniel Veillard09190202001-08-16 16:27:41 +0000160 else
Daniel Veillardac260302000-10-04 13:33:43 +0000161#endif
Daniel Veillard09190202001-08-16 16:27:41 +0000162 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000163 debug++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000164 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
Daniel Veillard740abf52000-10-02 23:04:54 +0000165 valid++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000166 else if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000167 expr++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000168 else if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree")))
Daniel Veillardb38bd552001-04-03 18:22:00 +0000169 tree++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000170 else if ((!strcmp(argv[i], "-nocdata")) ||
171 (!strcmp(argv[i], "--nocdata")))
172 nocdata++;
173 else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000174 filename = argv[++i];
Daniel Veillard7583a592001-07-08 13:15:55 +0000175 else if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000176 usefile++;
177 }
Daniel Veillard740abf52000-10-02 23:04:54 +0000178 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard48da9102001-08-07 01:10:10 +0000179 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
180 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillard7583a592001-07-08 13:15:55 +0000181 if (nocdata != 0) {
182 xmlDefaultSAXHandlerInit();
183 xmlDefaultSAXHandler.cdataBlock = NULL;
184 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000185 if (document == NULL) {
186 if (filename == NULL)
187 document = xmlParseDoc(buffer);
188 else
189 document = xmlParseFile(filename);
190 }
191 for (i = 1; i < argc ; i++) {
192 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
193 i++; continue;
194 }
195 if (argv[i][0] != '-') {
196 if (usefile)
197 testXPathFile(argv[i]);
198 else
199 testXPath(argv[i]);
200 strings ++;
201 }
202 }
203 if (strings == 0) {
204 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
205 argv[0]);
206 printf("\tParse the XPath strings and output the result of the parsing\n");
207 printf("\t--debug : dump a debug version of the result\n");
Daniel Veillard740abf52000-10-02 23:04:54 +0000208 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillardf6bf9212000-10-26 14:07:44 +0000209#if defined(LIBXML_XPTR_ENABLED)
210 printf("\t--xptr : expressions are XPointer expressions\n");
211#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000212 printf("\t--expr : debug XPath expressions only\n");
Daniel Veillardb38bd552001-04-03 18:22:00 +0000213 printf("\t--tree : show the compiled XPath tree\n");
Daniel Veillard7583a592001-07-08 13:15:55 +0000214 printf("\t--nocdata : do not generate CDATA nodes\n");
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000215 printf("\t--input filename : or\n");
216 printf("\t-i filename : read the document from filename\n");
217 printf("\t--file : or\n");
218 printf("\t-f : read queries from files, args\n");
219 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000220 if (document != NULL)
221 xmlFreeDoc(document);
222 xmlCleanupParser();
223 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000224
225 return(0);
226}
Daniel Veillard361d8452000-04-03 19:48:13 +0000227#else
228#include <stdio.h>
229int main(int argc, char **argv) {
230 printf("%s : XPath/Debug support not compiled in\n", argv[0]);
231 return(0);
232}
233#endif /* LIBXML_XPATH_ENABLED */