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