blob: 27e17ab726cd98a821793cfcbdb8357e1e4ec0b7 [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");
88 else
89 xmlDebugDumpOneNode(output, cur->nodeTab[i], 2);
Daniel Veillard1566d3a1999-07-15 14:24:29 +000090 }
91}
92
93void xmlXPAthDebugDumpObject(FILE *output, xmlXPathObjectPtr cur) {
94 if (cur == NULL) {
95 fprintf(output, "Object is empty (NULL)\n");
96 return;
97 }
98 switch(cur->type) {
99 case XPATH_UNDEFINED:
100 fprintf(output, "Object is uninitialized\n");
101 break;
102 case XPATH_NODESET:
103 fprintf(output, "Object is a Node Set :\n");
104 xmlXPAthDebugDumpNodeSet(output, cur->nodesetval);
105 break;
106 case XPATH_BOOLEAN:
107 fprintf(output, "Object is a Boolean : ");
108 if (cur->boolval) fprintf(output, "true\n");
109 else fprintf(output, "false\n");
110 break;
111 case XPATH_NUMBER:
112 fprintf(output, "Object is a number : %0g\n", cur->floatval);
113 break;
114 case XPATH_STRING:
115 fprintf(output, "Object is a string : ");
116 xmlDebugDumpString(output, cur->stringval);
117 fprintf(output, "\n");
118 break;
119 }
120}
121
122void testXPath(const char *str) {
123 xmlXPathObjectPtr res;
124 xmlXPathContextPtr ctxt;
125
126 ctxt = xmlXPathNewContext(document, NULL, NULL, NULL);
127 if (expr)
128 res = xmlXPathEvalExpression(str, ctxt);
129 else
130 res = xmlXPathEval(str, ctxt);
131 xmlXPAthDebugDumpObject(stdout, res);
132 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000133 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000134}
135
136void testXPathFile(const char *filename) {
137 FILE *input;
138 char expr[5000];
139
140 input = fopen(filename, "r");
141 if (input == NULL) {
142 fprintf(stderr, "Cannot open %s for reading\n", filename);
143 return;
144 }
145 while (fscanf(input, "%s", expr) != EOF) {
146 testXPath(expr);
147 }
148
149 fclose(input);
150}
151
152int main(int argc, char **argv) {
153 int i;
154 int strings = 0;
155 int usefile = 0;
156 char *filename = NULL;
157
158 for (i = 1; i < argc ; i++) {
159 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
160 debug++;
161 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
162 expr++;
163 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
164 filename = argv[++i];
165 if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
166 usefile++;
167 }
168 if (document == NULL) {
169 if (filename == NULL)
170 document = xmlParseDoc(buffer);
171 else
172 document = xmlParseFile(filename);
173 }
174 for (i = 1; i < argc ; i++) {
175 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
176 i++; continue;
177 }
178 if (argv[i][0] != '-') {
179 if (usefile)
180 testXPathFile(argv[i]);
181 else
182 testXPath(argv[i]);
183 strings ++;
184 }
185 }
186 if (strings == 0) {
187 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
188 argv[0]);
189 printf("\tParse the XPath strings and output the result of the parsing\n");
190 printf("\t--debug : dump a debug version of the result\n");
191 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 }
197
198 return(0);
199}