blob: 2c298493bab2f34d81a1b2bbf45a36e32981869c [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
10#define HAVE_FCNTL_H
11#include <io.h>
12#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000013#include "config.h"
Daniel Veillard1566d3a1999-07-15 14:24:29 +000014#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000015
16#include <stdio.h>
17#include <string.h>
18
19#ifdef HAVE_SYS_TYPES_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000020#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000021#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000022#ifdef HAVE_SYS_STAT_H
23#include <sys/stat.h>
24#endif
25#ifdef HAVE_FCNTL_H
26#include <fcntl.h>
27#endif
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000031#ifdef HAVE_STDLIB_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000032#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000033#endif
34
Daniel Veillard1566d3a1999-07-15 14:24:29 +000035
36#include "xpath.h"
37#include "tree.h"
38#include "parser.h"
39#include "debugXML.h"
40
41static int debug = 0;
42static int expr = 0;
43static xmlDocPtr document = NULL;
44
45/*
46 * Default document
47 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000048static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000049"<?xml version=\"1.0\"?>\n\
50<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
51 <head>\n\
52 <title>Welcome to Gnome</title>\n\
53 </head>\n\
54 <chapter>\n\
55 <title>The Linux adventure</title>\n\
56 <p>bla bla bla ...</p>\n\
57 <image href=\"linus.gif\"/>\n\
58 <p>...</p>\n\
59 </chapter>\n\
60 <chapter>\n\
61 <title>Chapter 2</title>\n\
62 <p>this is chapter 2 ...</p>\n\
63 </chapter>\n\
64 <chapter>\n\
65 <title>Chapter 3</title>\n\
66 <p>this is chapter 3 ...</p>\n\
67 </chapter>\n\
68 <chapter>\n\
69 <title>Chapter 4</title>\n\
70 <p>this is chapter 4 ...</p>\n\
71 </chapter>\n\
72 <chapter>\n\
73 <title>Chapter 5</title>\n\
74 <p>this is chapter 5 ...</p>\n\
75 </chapter>\n\
76</EXAMPLE>\n\
77";
78
79void xmlXPAthDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur) {
80 int i;
81
82 if (cur == NULL) {
83 fprintf(output, "NodeSet is NULL !\n");
84 return;
85
86 }
87
88 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
89 for (i = 0;i < cur->nodeNr;i++) {
90 fprintf(output, "%d", i + 1);
Daniel Veillardb05deb71999-08-10 19:04:08 +000091 if (cur->nodeTab[i] == NULL)
92 fprintf(output, " NULL\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +000093 else if ((cur->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
94 (cur->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
Daniel Veillardb05deb71999-08-10 19:04:08 +000095 fprintf(output, " /\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +000096 else if (cur->nodeTab[i]->type == XML_ATTRIBUTE_NODE)
97 xmlDebugDumpAttr(output, (xmlAttrPtr)cur->nodeTab[i], 2);
Daniel Veillardb05deb71999-08-10 19:04:08 +000098 else
99 xmlDebugDumpOneNode(output, cur->nodeTab[i], 2);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000100 }
101}
102
103void xmlXPAthDebugDumpObject(FILE *output, xmlXPathObjectPtr cur) {
104 if (cur == NULL) {
105 fprintf(output, "Object is empty (NULL)\n");
106 return;
107 }
108 switch(cur->type) {
109 case XPATH_UNDEFINED:
110 fprintf(output, "Object is uninitialized\n");
111 break;
112 case XPATH_NODESET:
113 fprintf(output, "Object is a Node Set :\n");
114 xmlXPAthDebugDumpNodeSet(output, cur->nodesetval);
115 break;
116 case XPATH_BOOLEAN:
117 fprintf(output, "Object is a Boolean : ");
118 if (cur->boolval) fprintf(output, "true\n");
119 else fprintf(output, "false\n");
120 break;
121 case XPATH_NUMBER:
122 fprintf(output, "Object is a number : %0g\n", cur->floatval);
123 break;
124 case XPATH_STRING:
125 fprintf(output, "Object is a string : ");
126 xmlDebugDumpString(output, cur->stringval);
127 fprintf(output, "\n");
128 break;
129 }
130}
131
132void testXPath(const char *str) {
133 xmlXPathObjectPtr res;
134 xmlXPathContextPtr ctxt;
135
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000136 ctxt = xmlXPathNewContext(document);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000137 if (expr)
Daniel Veillardb96e6431999-08-29 21:02:19 +0000138 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000139 else
Daniel Veillardb96e6431999-08-29 21:02:19 +0000140 res = xmlXPathEval(BAD_CAST str, ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000141 xmlXPAthDebugDumpObject(stdout, res);
142 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000143 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000144}
145
146void testXPathFile(const char *filename) {
147 FILE *input;
148 char expr[5000];
149
150 input = fopen(filename, "r");
151 if (input == NULL) {
152 fprintf(stderr, "Cannot open %s for reading\n", filename);
153 return;
154 }
155 while (fscanf(input, "%s", expr) != EOF) {
156 testXPath(expr);
157 }
158
159 fclose(input);
160}
161
162int main(int argc, char **argv) {
163 int i;
164 int strings = 0;
165 int usefile = 0;
166 char *filename = NULL;
167
168 for (i = 1; i < argc ; i++) {
169 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
170 debug++;
171 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
172 expr++;
173 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
174 filename = argv[++i];
175 if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
176 usefile++;
177 }
178 if (document == NULL) {
179 if (filename == NULL)
180 document = xmlParseDoc(buffer);
181 else
182 document = xmlParseFile(filename);
183 }
184 for (i = 1; i < argc ; i++) {
185 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
186 i++; continue;
187 }
188 if (argv[i][0] != '-') {
189 if (usefile)
190 testXPathFile(argv[i]);
191 else
192 testXPath(argv[i]);
193 strings ++;
194 }
195 }
196 if (strings == 0) {
197 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
198 argv[0]);
199 printf("\tParse the XPath strings and output the result of the parsing\n");
200 printf("\t--debug : dump a debug version of the result\n");
201 printf("\t--expr : debug XPath expressions only\n");
202 printf("\t--input filename : or\n");
203 printf("\t-i filename : read the document from filename\n");
204 printf("\t--file : or\n");
205 printf("\t-f : read queries from files, args\n");
206 }
207
208 return(0);
209}