blob: f91a2e65ae6beb2fe3e303f8e7605733984f63c5 [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 Veillard1566d3a1999-07-15 14:24:29 +000047static xmlDocPtr document = NULL;
48
49/*
50 * Default document
51 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000052static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000053"<?xml version=\"1.0\"?>\n\
54<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; 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 Veillard1566d3a1999-07-15 14:24:29 +000083
Daniel Veillard56a4cb82001-03-24 17:00:36 +000084static void
85testXPath(const char *str) {
Daniel Veillard1566d3a1999-07-15 14:24:29 +000086 xmlXPathObjectPtr res;
87 xmlXPathContextPtr ctxt;
88
Daniel Veillardac260302000-10-04 13:33:43 +000089#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 Veillard08108982001-01-03 15:24:58 +000096 ctxt->node = xmlDocGetRootElement(document);
Daniel Veillardac260302000-10-04 13:33:43 +000097 if (expr)
98 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
Daniel Veillardb38bd552001-04-03 18:22:00 +000099 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 Veillardd8df6c02001-04-05 16:54:14 +0000109 xmlXPathFreeCompExpr(comp);
Daniel Veillardb38bd552001-04-03 18:22:00 +0000110 } else
111 res = NULL;
112 }
Daniel Veillardac260302000-10-04 13:33:43 +0000113#if defined(LIBXML_XPTR_ENABLED)
114 }
115#endif
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000116 xmlXPathDebugDumpObject(stdout, res, 0);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000117 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000118 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000119}
120
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000121static void
122testXPathFile(const char *filename) {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000123 FILE *input;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000124 char expression[5000];
Daniel Veillard7e99c632000-10-06 12:59:53 +0000125 int len;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000126
127 input = fopen(filename, "r");
128 if (input == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000129 xmlGenericError(xmlGenericErrorContext,
130 "Cannot open %s for reading\n", filename);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000131 return;
132 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000133 while (fgets(expression, 4500, input) != NULL) {
134 len = strlen(expression);
Daniel Veillard7e99c632000-10-06 12:59:53 +0000135 len--;
136 while ((len >= 0) &&
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000137 ((expression[len] == '\n') || (expression[len] == '\t') ||
138 (expression[len] == '\r') || (expression[len] == ' '))) len--;
139 expression[len + 1] = 0;
Daniel Veillard7e99c632000-10-06 12:59:53 +0000140 if (len >= 0) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000141 printf("\n========================\nExpression: %s\n", expression) ;
142 testXPath(expression);
Daniel Veillard7e99c632000-10-06 12:59:53 +0000143 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000144 }
145
146 fclose(input);
147}
148
149int 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 Veillardac260302000-10-04 13:33:43 +0000156#if defined(LIBXML_XPTR_ENABLED)
157 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
158 xptr++;
159#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000160 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
161 debug++;
Daniel Veillard740abf52000-10-02 23:04:54 +0000162 if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
163 valid++;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000164 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
165 expr++;
Daniel Veillardb38bd552001-04-03 18:22:00 +0000166 if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree")))
167 tree++;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000168 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 Veillard740abf52000-10-02 23:04:54 +0000173 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000174 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 Veillard740abf52000-10-02 23:04:54 +0000197 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillardf6bf9212000-10-26 14:07:44 +0000198#if defined(LIBXML_XPTR_ENABLED)
199 printf("\t--xptr : expressions are XPointer expressions\n");
200#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000201 printf("\t--expr : debug XPath expressions only\n");
Daniel Veillardb38bd552001-04-03 18:22:00 +0000202 printf("\t--tree : show the compiled XPath tree\n");
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000203 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 Veillardf5c2c871999-12-01 09:51:45 +0000208 if (document != NULL)
209 xmlFreeDoc(document);
210 xmlCleanupParser();
211 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000212
213 return(0);
214}
Daniel Veillard361d8452000-04-03 19:48:13 +0000215#else
216#include <stdio.h>
217int 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 */