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