blob: bca7173c6c0a077994f422a65927a4f7dbb912c8 [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 Veillard9e8bfae2000-11-06 16:43:11 +000044#include <libxml/xpathInternals.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000045#include <libxml/xmlerror.h>
Daniel Veillardac260302000-10-04 13:33:43 +000046#if defined(LIBXML_XPTR_ENABLED)
47#include <libxml/xpointer.h>
48static int xptr = 0;
49#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000050static int debug = 0;
Daniel Veillard740abf52000-10-02 23:04:54 +000051static int valid = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000052static int expr = 0;
53static xmlDocPtr document = NULL;
54
55/*
56 * Default document
57 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +000058static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000059"<?xml version=\"1.0\"?>\n\
60<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
61 <head>\n\
62 <title>Welcome to Gnome</title>\n\
63 </head>\n\
64 <chapter>\n\
65 <title>The Linux adventure</title>\n\
66 <p>bla bla bla ...</p>\n\
67 <image href=\"linus.gif\"/>\n\
68 <p>...</p>\n\
69 </chapter>\n\
70 <chapter>\n\
71 <title>Chapter 2</title>\n\
72 <p>this is chapter 2 ...</p>\n\
73 </chapter>\n\
74 <chapter>\n\
75 <title>Chapter 3</title>\n\
76 <p>this is chapter 3 ...</p>\n\
77 </chapter>\n\
78 <chapter>\n\
79 <title>Chapter 4</title>\n\
80 <p>this is chapter 4 ...</p>\n\
81 </chapter>\n\
82 <chapter>\n\
83 <title>Chapter 5</title>\n\
84 <p>this is chapter 5 ...</p>\n\
85 </chapter>\n\
86</EXAMPLE>\n\
87";
88
Daniel Veillard1566d3a1999-07-15 14:24:29 +000089
90void testXPath(const char *str) {
91 xmlXPathObjectPtr res;
92 xmlXPathContextPtr ctxt;
93
Daniel Veillardac260302000-10-04 13:33:43 +000094#if defined(LIBXML_XPTR_ENABLED)
95 if (xptr) {
96 ctxt = xmlXPtrNewContext(document, NULL, NULL);
97 res = xmlXPtrEval(BAD_CAST str, ctxt);
98 } else {
99#endif
100 ctxt = xmlXPathNewContext(document);
101 if (expr)
102 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
103 else
104 res = xmlXPathEval(BAD_CAST str, ctxt);
105#if defined(LIBXML_XPTR_ENABLED)
106 }
107#endif
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000108 xmlXPathDebugDumpObject(stdout, res, 0);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000109 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000110 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000111}
112
113void testXPathFile(const char *filename) {
114 FILE *input;
115 char expr[5000];
Daniel Veillard7e99c632000-10-06 12:59:53 +0000116 int len;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000117
118 input = fopen(filename, "r");
119 if (input == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000120 xmlGenericError(xmlGenericErrorContext,
121 "Cannot open %s for reading\n", filename);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000122 return;
123 }
Daniel Veillard7e99c632000-10-06 12:59:53 +0000124 while (fgets(expr, 4500, input) != NULL) {
125 len = strlen(expr);
126 len--;
127 while ((len >= 0) &&
128 ((expr[len] == '\n') || (expr[len] == '\t') ||
129 (expr[len] == '\r') || (expr[len] == ' '))) len--;
130 expr[len + 1] = 0;
131 if (len >= 0) {
132 printf("\n========================\nExpression: %s\n", expr) ;
133 testXPath(expr);
134 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000135 }
136
137 fclose(input);
138}
139
140int main(int argc, char **argv) {
141 int i;
142 int strings = 0;
143 int usefile = 0;
144 char *filename = NULL;
145
146 for (i = 1; i < argc ; i++) {
Daniel Veillardac260302000-10-04 13:33:43 +0000147#if defined(LIBXML_XPTR_ENABLED)
148 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
149 xptr++;
150#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000151 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
152 debug++;
Daniel Veillard740abf52000-10-02 23:04:54 +0000153 if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
154 valid++;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000155 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 }
Daniel Veillard740abf52000-10-02 23:04:54 +0000162 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000163 if (document == NULL) {
164 if (filename == NULL)
165 document = xmlParseDoc(buffer);
166 else
167 document = xmlParseFile(filename);
168 }
169 for (i = 1; i < argc ; i++) {
170 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) {
171 i++; continue;
172 }
173 if (argv[i][0] != '-') {
174 if (usefile)
175 testXPathFile(argv[i]);
176 else
177 testXPath(argv[i]);
178 strings ++;
179 }
180 }
181 if (strings == 0) {
182 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
183 argv[0]);
184 printf("\tParse the XPath strings and output the result of the parsing\n");
185 printf("\t--debug : dump a debug version of the result\n");
Daniel Veillard740abf52000-10-02 23:04:54 +0000186 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillardf6bf9212000-10-26 14:07:44 +0000187#if defined(LIBXML_XPTR_ENABLED)
188 printf("\t--xptr : expressions are XPointer expressions\n");
189#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000190 printf("\t--expr : debug XPath expressions only\n");
191 printf("\t--input filename : or\n");
192 printf("\t-i filename : read the document from filename\n");
193 printf("\t--file : or\n");
194 printf("\t-f : read queries from files, args\n");
195 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000196 if (document != NULL)
197 xmlFreeDoc(document);
198 xmlCleanupParser();
199 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000200
201 return(0);
202}
Daniel Veillard361d8452000-04-03 19:48:13 +0000203#else
204#include <stdio.h>
205int main(int argc, char **argv) {
206 printf("%s : XPath/Debug support not compiled in\n", argv[0]);
207 return(0);
208}
209#endif /* LIBXML_XPATH_ENABLED */