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