blob: c226cf0fab462e3fd3eb770c4e8648ca713a6fdd [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 Veillardb71379b2000-10-09 12:30:39 +000015#include <libxml/xmlversion.h>
Daniel Veillard361d8452000-04-03 19:48:13 +000016#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 Veillard740abf52000-10-02 23:04:54 +000043#include <libxml/parserInternals.h>
Daniel Veillardac260302000-10-04 13:33:43 +000044#if defined(LIBXML_XPTR_ENABLED)
45#include <libxml/xpointer.h>
46static int xptr = 0;
47#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000048static int debug = 0;
Daniel Veillard740abf52000-10-02 23:04:54 +000049static int valid = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000050static int expr = 0;
51static xmlDocPtr document = NULL;
52
53/*
54 * Default document
55 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000056static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000057"<?xml version=\"1.0\"?>\n\
58<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
59 <head>\n\
60 <title>Welcome to Gnome</title>\n\
61 </head>\n\
62 <chapter>\n\
63 <title>The Linux adventure</title>\n\
64 <p>bla bla bla ...</p>\n\
65 <image href=\"linus.gif\"/>\n\
66 <p>...</p>\n\
67 </chapter>\n\
68 <chapter>\n\
69 <title>Chapter 2</title>\n\
70 <p>this is chapter 2 ...</p>\n\
71 </chapter>\n\
72 <chapter>\n\
73 <title>Chapter 3</title>\n\
74 <p>this is chapter 3 ...</p>\n\
75 </chapter>\n\
76 <chapter>\n\
77 <title>Chapter 4</title>\n\
78 <p>this is chapter 4 ...</p>\n\
79 </chapter>\n\
80 <chapter>\n\
81 <title>Chapter 5</title>\n\
82 <p>this is chapter 5 ...</p>\n\
83 </chapter>\n\
84</EXAMPLE>\n\
85";
86
Daniel Veillard1566d3a1999-07-15 14:24:29 +000087
88void testXPath(const char *str) {
89 xmlXPathObjectPtr res;
90 xmlXPathContextPtr ctxt;
91
Daniel Veillardac260302000-10-04 13:33:43 +000092#if defined(LIBXML_XPTR_ENABLED)
93 if (xptr) {
94 ctxt = xmlXPtrNewContext(document, NULL, NULL);
95 res = xmlXPtrEval(BAD_CAST str, ctxt);
96 } else {
97#endif
98 ctxt = xmlXPathNewContext(document);
99 if (expr)
100 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
101 else
102 res = xmlXPathEval(BAD_CAST str, ctxt);
103#if defined(LIBXML_XPTR_ENABLED)
104 }
105#endif
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000106 xmlXPathDebugDumpObject(stdout, res, 0);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000107 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000108 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000109}
110
111void testXPathFile(const char *filename) {
112 FILE *input;
113 char expr[5000];
Daniel Veillard7e99c632000-10-06 12:59:53 +0000114 int len;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000115
116 input = fopen(filename, "r");
117 if (input == NULL) {
118 fprintf(stderr, "Cannot open %s for reading\n", filename);
119 return;
120 }
Daniel Veillard7e99c632000-10-06 12:59:53 +0000121 while (fgets(expr, 4500, input) != NULL) {
122 len = strlen(expr);
123 len--;
124 while ((len >= 0) &&
125 ((expr[len] == '\n') || (expr[len] == '\t') ||
126 (expr[len] == '\r') || (expr[len] == ' '))) len--;
127 expr[len + 1] = 0;
128 if (len >= 0) {
129 printf("\n========================\nExpression: %s\n", expr) ;
130 testXPath(expr);
131 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000132 }
133
134 fclose(input);
135}
136
137int main(int argc, char **argv) {
138 int i;
139 int strings = 0;
140 int usefile = 0;
141 char *filename = NULL;
142
143 for (i = 1; i < argc ; i++) {
Daniel Veillardac260302000-10-04 13:33:43 +0000144#if defined(LIBXML_XPTR_ENABLED)
145 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
146 xptr++;
147#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000148 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
149 debug++;
Daniel Veillard740abf52000-10-02 23:04:54 +0000150 if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
151 valid++;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000152 if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
153 expr++;
154 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
155 filename = argv[++i];
156 if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
157 usefile++;
158 }
Daniel Veillard740abf52000-10-02 23:04:54 +0000159 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000160 if (document == NULL) {
161 if (filename == NULL)
162 document = xmlParseDoc(buffer);
163 else
164 document = xmlParseFile(filename);
165 }
166 for (i = 1; i < argc ; i++) {
167 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
168 i++; continue;
169 }
170 if (argv[i][0] != '-') {
171 if (usefile)
172 testXPathFile(argv[i]);
173 else
174 testXPath(argv[i]);
175 strings ++;
176 }
177 }
178 if (strings == 0) {
179 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
180 argv[0]);
181 printf("\tParse the XPath strings and output the result of the parsing\n");
182 printf("\t--debug : dump a debug version of the result\n");
Daniel Veillard740abf52000-10-02 23:04:54 +0000183 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000184 printf("\t--expr : debug XPath expressions only\n");
185 printf("\t--input filename : or\n");
186 printf("\t-i filename : read the document from filename\n");
187 printf("\t--file : or\n");
188 printf("\t-f : read queries from files, args\n");
189 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000190 if (document != NULL)
191 xmlFreeDoc(document);
192 xmlCleanupParser();
193 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000194
195 return(0);
196}
Daniel Veillard361d8452000-04-03 19:48:13 +0000197#else
198#include <stdio.h>
199int main(int argc, char **argv) {
200 printf("%s : XPath/Debug support not compiled in\n", argv[0]);
201 return(0);
202}
203#endif /* LIBXML_XPATH_ENABLED */