blob: b31d3ea7cdead6ad4e42eccded8de935a89ca8c5 [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"
Daniel Veillardf5c2c871999-12-01 09:51:45 +000040#include "xmlmemory.h"
Daniel Veillard1566d3a1999-07-15 14:24:29 +000041
42static int debug = 0;
43static int expr = 0;
44static xmlDocPtr document = NULL;
45
46/*
47 * Default document
48 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000049static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000050"<?xml version=\"1.0\"?>\n\
51<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
52 <head>\n\
53 <title>Welcome to Gnome</title>\n\
54 </head>\n\
55 <chapter>\n\
56 <title>The Linux adventure</title>\n\
57 <p>bla bla bla ...</p>\n\
58 <image href=\"linus.gif\"/>\n\
59 <p>...</p>\n\
60 </chapter>\n\
61 <chapter>\n\
62 <title>Chapter 2</title>\n\
63 <p>this is chapter 2 ...</p>\n\
64 </chapter>\n\
65 <chapter>\n\
66 <title>Chapter 3</title>\n\
67 <p>this is chapter 3 ...</p>\n\
68 </chapter>\n\
69 <chapter>\n\
70 <title>Chapter 4</title>\n\
71 <p>this is chapter 4 ...</p>\n\
72 </chapter>\n\
73 <chapter>\n\
74 <title>Chapter 5</title>\n\
75 <p>this is chapter 5 ...</p>\n\
76 </chapter>\n\
77</EXAMPLE>\n\
78";
79
80void xmlXPAthDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur) {
81 int i;
82
83 if (cur == NULL) {
84 fprintf(output, "NodeSet is NULL !\n");
85 return;
86
87 }
88
89 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
90 for (i = 0;i < cur->nodeNr;i++) {
91 fprintf(output, "%d", i + 1);
Daniel Veillardb05deb71999-08-10 19:04:08 +000092 if (cur->nodeTab[i] == NULL)
93 fprintf(output, " NULL\n");
Daniel Veillard7c1206f1999-10-14 09:10:25 +000094 else if ((cur->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
95 (cur->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
Daniel Veillardb05deb71999-08-10 19:04:08 +000096 fprintf(output, " /\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +000097 else if (cur->nodeTab[i]->type == XML_ATTRIBUTE_NODE)
98 xmlDebugDumpAttr(output, (xmlAttrPtr)cur->nodeTab[i], 2);
Daniel Veillardb05deb71999-08-10 19:04:08 +000099 else
100 xmlDebugDumpOneNode(output, cur->nodeTab[i], 2);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000101 }
102}
103
104void xmlXPAthDebugDumpObject(FILE *output, xmlXPathObjectPtr cur) {
105 if (cur == NULL) {
106 fprintf(output, "Object is empty (NULL)\n");
107 return;
108 }
109 switch(cur->type) {
110 case XPATH_UNDEFINED:
111 fprintf(output, "Object is uninitialized\n");
112 break;
113 case XPATH_NODESET:
114 fprintf(output, "Object is a Node Set :\n");
115 xmlXPAthDebugDumpNodeSet(output, cur->nodesetval);
116 break;
117 case XPATH_BOOLEAN:
118 fprintf(output, "Object is a Boolean : ");
119 if (cur->boolval) fprintf(output, "true\n");
120 else fprintf(output, "false\n");
121 break;
122 case XPATH_NUMBER:
123 fprintf(output, "Object is a number : %0g\n", cur->floatval);
124 break;
125 case XPATH_STRING:
126 fprintf(output, "Object is a string : ");
127 xmlDebugDumpString(output, cur->stringval);
128 fprintf(output, "\n");
129 break;
130 }
131}
132
133void testXPath(const char *str) {
134 xmlXPathObjectPtr res;
135 xmlXPathContextPtr ctxt;
136
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000137 ctxt = xmlXPathNewContext(document);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000138 if (expr)
Daniel Veillardb96e6431999-08-29 21:02:19 +0000139 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000140 else
Daniel Veillardb96e6431999-08-29 21:02:19 +0000141 res = xmlXPathEval(BAD_CAST str, ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000142 xmlXPAthDebugDumpObject(stdout, res);
143 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000144 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000145}
146
147void testXPathFile(const char *filename) {
148 FILE *input;
149 char expr[5000];
150
151 input = fopen(filename, "r");
152 if (input == NULL) {
153 fprintf(stderr, "Cannot open %s for reading\n", filename);
154 return;
155 }
156 while (fscanf(input, "%s", expr) != EOF) {
157 testXPath(expr);
158 }
159
160 fclose(input);
161}
162
163int main(int argc, char **argv) {
164 int i;
165 int strings = 0;
166 int usefile = 0;
167 char *filename = NULL;
168
169 for (i = 1; i < argc ; i++) {
170 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
171 debug++;
172 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
173 expr++;
174 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
175 filename = argv[++i];
176 if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
177 usefile++;
178 }
179 if (document == NULL) {
180 if (filename == NULL)
181 document = xmlParseDoc(buffer);
182 else
183 document = xmlParseFile(filename);
184 }
185 for (i = 1; i < argc ; i++) {
186 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
187 i++; continue;
188 }
189 if (argv[i][0] != '-') {
190 if (usefile)
191 testXPathFile(argv[i]);
192 else
193 testXPath(argv[i]);
194 strings ++;
195 }
196 }
197 if (strings == 0) {
198 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
199 argv[0]);
200 printf("\tParse the XPath strings and output the result of the parsing\n");
201 printf("\t--debug : dump a debug version of the result\n");
202 printf("\t--expr : debug XPath expressions only\n");
203 printf("\t--input filename : or\n");
204 printf("\t-i filename : read the document from filename\n");
205 printf("\t--file : or\n");
206 printf("\t-f : read queries from files, args\n");
207 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000208 if (document != NULL)
209 xmlFreeDoc(document);
210 xmlCleanupParser();
211 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000212
213 return(0);
214}