blob: d8c24ca34f03c001160e8b5672a8cd921e667cdb [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 Veillard361d8452000-04-03 19:48:13 +000015#include "xmlversion.h"
16#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 Veillardac260302000-10-04 13:33:43 +000044#if defined(LIBXML_XPTR_ENABLED)
45#include <libxml/xpointer.h>
46static int xptr = 0;
47#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000048static int debug = 0;
Daniel Veillard740abf52000-10-02 23:04:54 +000049static int valid = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000050static int expr = 0;
51static xmlDocPtr document = NULL;
52
53/*
54 * Default document
55 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000056static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000057"<?xml version=\"1.0\"?>\n\
58<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
59 <head>\n\
60 <title>Welcome to Gnome</title>\n\
61 </head>\n\
62 <chapter>\n\
63 <title>The Linux adventure</title>\n\
64 <p>bla bla bla ...</p>\n\
65 <image href=\"linus.gif\"/>\n\
66 <p>...</p>\n\
67 </chapter>\n\
68 <chapter>\n\
69 <title>Chapter 2</title>\n\
70 <p>this is chapter 2 ...</p>\n\
71 </chapter>\n\
72 <chapter>\n\
73 <title>Chapter 3</title>\n\
74 <p>this is chapter 3 ...</p>\n\
75 </chapter>\n\
76 <chapter>\n\
77 <title>Chapter 4</title>\n\
78 <p>this is chapter 4 ...</p>\n\
79 </chapter>\n\
80 <chapter>\n\
81 <title>Chapter 5</title>\n\
82 <p>this is chapter 5 ...</p>\n\
83 </chapter>\n\
84</EXAMPLE>\n\
85";
86
87void xmlXPAthDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur) {
88 int i;
89
90 if (cur == NULL) {
91 fprintf(output, "NodeSet is NULL !\n");
92 return;
93
94 }
95
96 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
97 for (i = 0;i < cur->nodeNr;i++) {
98 fprintf(output, "%d", i + 1);
Daniel Veillardb05deb71999-08-10 19:04:08 +000099 if (cur->nodeTab[i] == NULL)
100 fprintf(output, " NULL\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +0000101 else if ((cur->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
102 (cur->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000103 fprintf(output, " /\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +0000104 else if (cur->nodeTab[i]->type == XML_ATTRIBUTE_NODE)
105 xmlDebugDumpAttr(output, (xmlAttrPtr)cur->nodeTab[i], 2);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000106 else
107 xmlDebugDumpOneNode(output, cur->nodeTab[i], 2);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000108 }
109}
110
111void xmlXPAthDebugDumpObject(FILE *output, xmlXPathObjectPtr cur) {
112 if (cur == NULL) {
113 fprintf(output, "Object is empty (NULL)\n");
114 return;
115 }
116 switch(cur->type) {
117 case XPATH_UNDEFINED:
118 fprintf(output, "Object is uninitialized\n");
119 break;
120 case XPATH_NODESET:
121 fprintf(output, "Object is a Node Set :\n");
122 xmlXPAthDebugDumpNodeSet(output, cur->nodesetval);
123 break;
124 case XPATH_BOOLEAN:
125 fprintf(output, "Object is a Boolean : ");
126 if (cur->boolval) fprintf(output, "true\n");
127 else fprintf(output, "false\n");
128 break;
129 case XPATH_NUMBER:
130 fprintf(output, "Object is a number : %0g\n", cur->floatval);
131 break;
132 case XPATH_STRING:
133 fprintf(output, "Object is a string : ");
134 xmlDebugDumpString(output, cur->stringval);
135 fprintf(output, "\n");
136 break;
137 }
138}
139
140void testXPath(const char *str) {
141 xmlXPathObjectPtr res;
142 xmlXPathContextPtr ctxt;
143
Daniel Veillardac260302000-10-04 13:33:43 +0000144#if defined(LIBXML_XPTR_ENABLED)
145 if (xptr) {
146 ctxt = xmlXPtrNewContext(document, NULL, NULL);
147 res = xmlXPtrEval(BAD_CAST str, ctxt);
148 } else {
149#endif
150 ctxt = xmlXPathNewContext(document);
151 if (expr)
152 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
153 else
154 res = xmlXPathEval(BAD_CAST str, ctxt);
155#if defined(LIBXML_XPTR_ENABLED)
156 }
157#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000158 xmlXPAthDebugDumpObject(stdout, res);
159 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000160 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000161}
162
163void testXPathFile(const char *filename) {
164 FILE *input;
165 char expr[5000];
166
167 input = fopen(filename, "r");
168 if (input == NULL) {
169 fprintf(stderr, "Cannot open %s for reading\n", filename);
170 return;
171 }
172 while (fscanf(input, "%s", expr) != EOF) {
Daniel Veillardac260302000-10-04 13:33:43 +0000173 printf("\n========================\nExpression: %s\n", expr) ;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000174 testXPath(expr);
175 }
176
177 fclose(input);
178}
179
180int main(int argc, char **argv) {
181 int i;
182 int strings = 0;
183 int usefile = 0;
184 char *filename = NULL;
185
186 for (i = 1; i < argc ; i++) {
Daniel Veillardac260302000-10-04 13:33:43 +0000187#if defined(LIBXML_XPTR_ENABLED)
188 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
189 xptr++;
190#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000191 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
192 debug++;
Daniel Veillard740abf52000-10-02 23:04:54 +0000193 if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
194 valid++;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000195 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
196 expr++;
197 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
198 filename = argv[++i];
199 if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
200 usefile++;
201 }
Daniel Veillard740abf52000-10-02 23:04:54 +0000202 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000203 if (document == NULL) {
204 if (filename == NULL)
205 document = xmlParseDoc(buffer);
206 else
207 document = xmlParseFile(filename);
208 }
209 for (i = 1; i < argc ; i++) {
210 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
211 i++; continue;
212 }
213 if (argv[i][0] != '-') {
214 if (usefile)
215 testXPathFile(argv[i]);
216 else
217 testXPath(argv[i]);
218 strings ++;
219 }
220 }
221 if (strings == 0) {
222 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
223 argv[0]);
224 printf("\tParse the XPath strings and output the result of the parsing\n");
225 printf("\t--debug : dump a debug version of the result\n");
Daniel Veillard740abf52000-10-02 23:04:54 +0000226 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000227 printf("\t--expr : debug XPath expressions only\n");
228 printf("\t--input filename : or\n");
229 printf("\t-i filename : read the document from filename\n");
230 printf("\t--file : or\n");
231 printf("\t-f : read queries from files, args\n");
232 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000233 if (document != NULL)
234 xmlFreeDoc(document);
235 xmlCleanupParser();
236 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000237
238 return(0);
239}
Daniel Veillard361d8452000-04-03 19:48:13 +0000240#else
241#include <stdio.h>
242int main(int argc, char **argv) {
243 printf("%s : XPath/Debug support not compiled in\n", argv[0]);
244 return(0);
245}
246#endif /* LIBXML_XPATH_ENABLED */