blob: 49df1c451bf505d6f2a8f749703c0a2ec96f4ee3 [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 Veillard1566d3a1999-07-15 14:24:29 +000044
45static int debug = 0;
Daniel Veillard740abf52000-10-02 23:04:54 +000046static int valid = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000047static int expr = 0;
48static 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
84void xmlXPAthDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur) {
85 int i;
86
87 if (cur == NULL) {
88 fprintf(output, "NodeSet is NULL !\n");
89 return;
90
91 }
92
93 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
94 for (i = 0;i < cur->nodeNr;i++) {
95 fprintf(output, "%d", i + 1);
Daniel Veillardb05deb71999-08-10 19:04:08 +000096 if (cur->nodeTab[i] == NULL)
97 fprintf(output, " NULL\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +000098 else if ((cur->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
99 (cur->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000100 fprintf(output, " /\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +0000101 else if (cur->nodeTab[i]->type == XML_ATTRIBUTE_NODE)
102 xmlDebugDumpAttr(output, (xmlAttrPtr)cur->nodeTab[i], 2);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000103 else
104 xmlDebugDumpOneNode(output, cur->nodeTab[i], 2);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000105 }
106}
107
108void xmlXPAthDebugDumpObject(FILE *output, xmlXPathObjectPtr cur) {
109 if (cur == NULL) {
110 fprintf(output, "Object is empty (NULL)\n");
111 return;
112 }
113 switch(cur->type) {
114 case XPATH_UNDEFINED:
115 fprintf(output, "Object is uninitialized\n");
116 break;
117 case XPATH_NODESET:
118 fprintf(output, "Object is a Node Set :\n");
119 xmlXPAthDebugDumpNodeSet(output, cur->nodesetval);
120 break;
121 case XPATH_BOOLEAN:
122 fprintf(output, "Object is a Boolean : ");
123 if (cur->boolval) fprintf(output, "true\n");
124 else fprintf(output, "false\n");
125 break;
126 case XPATH_NUMBER:
127 fprintf(output, "Object is a number : %0g\n", cur->floatval);
128 break;
129 case XPATH_STRING:
130 fprintf(output, "Object is a string : ");
131 xmlDebugDumpString(output, cur->stringval);
132 fprintf(output, "\n");
133 break;
134 }
135}
136
137void testXPath(const char *str) {
138 xmlXPathObjectPtr res;
139 xmlXPathContextPtr ctxt;
140
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000141 ctxt = xmlXPathNewContext(document);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000142 if (expr)
Daniel Veillardb96e6431999-08-29 21:02:19 +0000143 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000144 else
Daniel Veillardb96e6431999-08-29 21:02:19 +0000145 res = xmlXPathEval(BAD_CAST str, ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000146 xmlXPAthDebugDumpObject(stdout, res);
147 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000148 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000149}
150
151void testXPathFile(const char *filename) {
152 FILE *input;
153 char expr[5000];
154
155 input = fopen(filename, "r");
156 if (input == NULL) {
157 fprintf(stderr, "Cannot open %s for reading\n", filename);
158 return;
159 }
160 while (fscanf(input, "%s", expr) != EOF) {
161 testXPath(expr);
162 }
163
164 fclose(input);
165}
166
167int main(int argc, char **argv) {
168 int i;
169 int strings = 0;
170 int usefile = 0;
171 char *filename = NULL;
172
173 for (i = 1; i < argc ; i++) {
174 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
175 debug++;
Daniel Veillard740abf52000-10-02 23:04:54 +0000176 if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
177 valid++;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000178 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
179 expr++;
180 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
181 filename = argv[++i];
182 if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
183 usefile++;
184 }
Daniel Veillard740abf52000-10-02 23:04:54 +0000185 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000186 if (document == NULL) {
187 if (filename == NULL)
188 document = xmlParseDoc(buffer);
189 else
190 document = xmlParseFile(filename);
191 }
192 for (i = 1; i < argc ; i++) {
193 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
194 i++; continue;
195 }
196 if (argv[i][0] != '-') {
197 if (usefile)
198 testXPathFile(argv[i]);
199 else
200 testXPath(argv[i]);
201 strings ++;
202 }
203 }
204 if (strings == 0) {
205 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
206 argv[0]);
207 printf("\tParse the XPath strings and output the result of the parsing\n");
208 printf("\t--debug : dump a debug version of the result\n");
Daniel Veillard740abf52000-10-02 23:04:54 +0000209 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000210 printf("\t--expr : debug XPath expressions only\n");
211 printf("\t--input filename : or\n");
212 printf("\t-i filename : read the document from filename\n");
213 printf("\t--file : or\n");
214 printf("\t-f : read queries from files, args\n");
215 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000216 if (document != NULL)
217 xmlFreeDoc(document);
218 xmlCleanupParser();
219 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000220
221 return(0);
222}
Daniel Veillard361d8452000-04-03 19:48:13 +0000223#else
224#include <stdio.h>
225int main(int argc, char **argv) {
226 printf("%s : XPath/Debug support not compiled in\n", argv[0]);
227 return(0);
228}
229#endif /* LIBXML_XPATH_ENABLED */