blob: 2bd5eb59ab31839738bdf7cb7080e216ef8ab331 [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 *
6 * Daniel.Veillard@w3.org
7 */
8
9#ifdef WIN32
Daniel Veillard3c558c31999-12-22 11:30:41 +000010#include "win32config.h"
Daniel Veillard1566d3a1999-07-15 14:24:29 +000011#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000012#include "config.h"
Daniel Veillard1566d3a1999-07-15 14:24:29 +000013#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000014
Daniel Veillardb71379b2000-10-09 12:30:39 +000015#include <libxml/xmlversion.h>
Daniel Veillard361d8452000-04-03 19:48:13 +000016#if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED)
17
Daniel Veillard7f7d1111999-09-22 09:46:25 +000018#include <stdio.h>
19#include <string.h>
20
21#ifdef HAVE_SYS_TYPES_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000022#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000023#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000024#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 Veillard7f7d1111999-09-22 09:46:25 +000033#ifdef HAVE_STDLIB_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000034#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000035#endif
36
Daniel Veillard1566d3a1999-07-15 14:24:29 +000037
Daniel Veillard361d8452000-04-03 19:48:13 +000038#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 Veillard740abf52000-10-02 23:04:54 +000043#include <libxml/parserInternals.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000044#include <libxml/xpathInternals.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000045#include <libxml/xmlerror.h>
Daniel Veillardac260302000-10-04 13:33:43 +000046#if defined(LIBXML_XPTR_ENABLED)
47#include <libxml/xpointer.h>
48static int xptr = 0;
49#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000050static int debug = 0;
Daniel Veillard740abf52000-10-02 23:04:54 +000051static int valid = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000052static int expr = 0;
53static xmlDocPtr document = NULL;
54
55/*
56 * Default document
57 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000058static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000059"<?xml version=\"1.0\"?>\n\
60<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
61 <head>\n\
62 <title>Welcome to Gnome</title>\n\
63 </head>\n\
64 <chapter>\n\
65 <title>The Linux adventure</title>\n\
66 <p>bla bla bla ...</p>\n\
67 <image href=\"linus.gif\"/>\n\
68 <p>...</p>\n\
69 </chapter>\n\
70 <chapter>\n\
71 <title>Chapter 2</title>\n\
72 <p>this is chapter 2 ...</p>\n\
73 </chapter>\n\
74 <chapter>\n\
75 <title>Chapter 3</title>\n\
76 <p>this is chapter 3 ...</p>\n\
77 </chapter>\n\
78 <chapter>\n\
79 <title>Chapter 4</title>\n\
80 <p>this is chapter 4 ...</p>\n\
81 </chapter>\n\
82 <chapter>\n\
83 <title>Chapter 5</title>\n\
84 <p>this is chapter 5 ...</p>\n\
85 </chapter>\n\
86</EXAMPLE>\n\
87";
88
Daniel Veillard1566d3a1999-07-15 14:24:29 +000089
90void testXPath(const char *str) {
91 xmlXPathObjectPtr res;
92 xmlXPathContextPtr ctxt;
93
Daniel Veillardac260302000-10-04 13:33:43 +000094#if defined(LIBXML_XPTR_ENABLED)
95 if (xptr) {
96 ctxt = xmlXPtrNewContext(document, NULL, NULL);
97 res = xmlXPtrEval(BAD_CAST str, ctxt);
98 } else {
99#endif
100 ctxt = xmlXPathNewContext(document);
Daniel Veillard08108982001-01-03 15:24:58 +0000101 ctxt->node = xmlDocGetRootElement(document);
Daniel Veillardac260302000-10-04 13:33:43 +0000102 if (expr)
103 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
104 else
105 res = xmlXPathEval(BAD_CAST str, ctxt);
106#if defined(LIBXML_XPTR_ENABLED)
107 }
108#endif
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000109 xmlXPathDebugDumpObject(stdout, res, 0);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000110 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000111 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000112}
113
114void testXPathFile(const char *filename) {
115 FILE *input;
116 char expr[5000];
Daniel Veillard7e99c632000-10-06 12:59:53 +0000117 int len;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000118
119 input = fopen(filename, "r");
120 if (input == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000121 xmlGenericError(xmlGenericErrorContext,
122 "Cannot open %s for reading\n", filename);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000123 return;
124 }
Daniel Veillard7e99c632000-10-06 12:59:53 +0000125 while (fgets(expr, 4500, input) != NULL) {
126 len = strlen(expr);
127 len--;
128 while ((len >= 0) &&
129 ((expr[len] == '\n') || (expr[len] == '\t') ||
130 (expr[len] == '\r') || (expr[len] == ' '))) len--;
131 expr[len + 1] = 0;
132 if (len >= 0) {
133 printf("\n========================\nExpression: %s\n", expr) ;
134 testXPath(expr);
135 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000136 }
137
138 fclose(input);
139}
140
141int main(int argc, char **argv) {
142 int i;
143 int strings = 0;
144 int usefile = 0;
145 char *filename = NULL;
146
147 for (i = 1; i < argc ; i++) {
Daniel Veillardac260302000-10-04 13:33:43 +0000148#if defined(LIBXML_XPTR_ENABLED)
149 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
150 xptr++;
151#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000152 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
153 debug++;
Daniel Veillard740abf52000-10-02 23:04:54 +0000154 if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
155 valid++;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000156 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
157 expr++;
158 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
159 filename = argv[++i];
160 if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
161 usefile++;
162 }
Daniel Veillard740abf52000-10-02 23:04:54 +0000163 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000164 if (document == NULL) {
165 if (filename == NULL)
166 document = xmlParseDoc(buffer);
167 else
168 document = xmlParseFile(filename);
169 }
170 for (i = 1; i < argc ; i++) {
171 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
172 i++; continue;
173 }
174 if (argv[i][0] != '-') {
175 if (usefile)
176 testXPathFile(argv[i]);
177 else
178 testXPath(argv[i]);
179 strings ++;
180 }
181 }
182 if (strings == 0) {
183 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
184 argv[0]);
185 printf("\tParse the XPath strings and output the result of the parsing\n");
186 printf("\t--debug : dump a debug version of the result\n");
Daniel Veillard740abf52000-10-02 23:04:54 +0000187 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillardf6bf9212000-10-26 14:07:44 +0000188#if defined(LIBXML_XPTR_ENABLED)
189 printf("\t--xptr : expressions are XPointer expressions\n");
190#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000191 printf("\t--expr : debug XPath expressions only\n");
192 printf("\t--input filename : or\n");
193 printf("\t-i filename : read the document from filename\n");
194 printf("\t--file : or\n");
195 printf("\t-f : read queries from files, args\n");
196 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000197 if (document != NULL)
198 xmlFreeDoc(document);
199 xmlCleanupParser();
200 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000201
202 return(0);
203}
Daniel Veillard361d8452000-04-03 19:48:13 +0000204#else
205#include <stdio.h>
206int main(int argc, char **argv) {
207 printf("%s : XPath/Debug support not compiled in\n", argv[0]);
208 return(0);
209}
210#endif /* LIBXML_XPATH_ENABLED */