blob: ebb9ff7bbd854eb35b6b07d4de6a39800ad869ca [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 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Daniel Veillard1566d3a1999-07-15 14:24:29 +00007 */
8
Bjorn Reese70a9da52001-04-21 16:57:29 +00009#include "libxml.h"
Daniel Veillard361d8452000-04-03 19:48:13 +000010#if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED)
11
Daniel Veillard7f7d1111999-09-22 09:46:25 +000012#include <string.h>
13
14#ifdef HAVE_SYS_TYPES_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000015#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000016#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000017#ifdef HAVE_SYS_STAT_H
18#include <sys/stat.h>
19#endif
20#ifdef HAVE_FCNTL_H
21#include <fcntl.h>
22#endif
23#ifdef HAVE_UNISTD_H
24#include <unistd.h>
25#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000026#ifdef HAVE_STDLIB_H
Daniel Veillard1566d3a1999-07-15 14:24:29 +000027#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000028#endif
29
Daniel Veillard1566d3a1999-07-15 14:24:29 +000030
Daniel Veillard361d8452000-04-03 19:48:13 +000031#include <libxml/xpath.h>
32#include <libxml/tree.h>
33#include <libxml/parser.h>
34#include <libxml/debugXML.h>
35#include <libxml/xmlmemory.h>
Daniel Veillard740abf52000-10-02 23:04:54 +000036#include <libxml/parserInternals.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000037#include <libxml/xpathInternals.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000038#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000039#include <libxml/globals.h>
Daniel Veillardac260302000-10-04 13:33:43 +000040#if defined(LIBXML_XPTR_ENABLED)
41#include <libxml/xpointer.h>
42static int xptr = 0;
43#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000044static int debug = 0;
Daniel Veillard740abf52000-10-02 23:04:54 +000045static int valid = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000046static int expr = 0;
Daniel Veillardb38bd552001-04-03 18:22:00 +000047static int tree = 0;
Daniel Veillard7583a592001-07-08 13:15:55 +000048static int nocdata = 0;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000049static xmlDocPtr document = NULL;
50
51/*
52 * Default document
53 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080054static xmlChar buffer[] =
Daniel Veillard1566d3a1999-07-15 14:24:29 +000055"<?xml version=\"1.0\"?>\n\
56<EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
57 <head>\n\
58 <title>Welcome to Gnome</title>\n\
59 </head>\n\
60 <chapter>\n\
61 <title>The Linux adventure</title>\n\
62 <p>bla bla bla ...</p>\n\
63 <image href=\"linus.gif\"/>\n\
64 <p>...</p>\n\
65 </chapter>\n\
66 <chapter>\n\
67 <title>Chapter 2</title>\n\
68 <p>this is chapter 2 ...</p>\n\
69 </chapter>\n\
70 <chapter>\n\
71 <title>Chapter 3</title>\n\
72 <p>this is chapter 3 ...</p>\n\
73 </chapter>\n\
Daniel Veillard1566d3a1999-07-15 14:24:29 +000074</EXAMPLE>\n\
75";
76
Daniel Veillard1566d3a1999-07-15 14:24:29 +000077
Daniel Veillard56a4cb82001-03-24 17:00:36 +000078static void
79testXPath(const char *str) {
Daniel Veillard1566d3a1999-07-15 14:24:29 +000080 xmlXPathObjectPtr res;
81 xmlXPathContextPtr ctxt;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080082
Daniel Veillardac260302000-10-04 13:33:43 +000083#if defined(LIBXML_XPTR_ENABLED)
84 if (xptr) {
85 ctxt = xmlXPtrNewContext(document, NULL, NULL);
86 res = xmlXPtrEval(BAD_CAST str, ctxt);
87 } else {
88#endif
89 ctxt = xmlXPathNewContext(document);
Daniel Veillard08108982001-01-03 15:24:58 +000090 ctxt->node = xmlDocGetRootElement(document);
Daniel Veillardac260302000-10-04 13:33:43 +000091 if (expr)
92 res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
Daniel Veillardb38bd552001-04-03 18:22:00 +000093 else {
94 /* res = xmlXPathEval(BAD_CAST str, ctxt); */
95 xmlXPathCompExprPtr comp;
96
97 comp = xmlXPathCompile(BAD_CAST str);
98 if (comp != NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +080099 if (tree)
Daniel Veillardb38bd552001-04-03 18:22:00 +0000100 xmlXPathDebugDumpCompExpr(stdout, comp, 0);
101
102 res = xmlXPathCompiledEval(comp, ctxt);
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000103 xmlXPathFreeCompExpr(comp);
Daniel Veillardb38bd552001-04-03 18:22:00 +0000104 } else
105 res = NULL;
106 }
Daniel Veillardac260302000-10-04 13:33:43 +0000107#if defined(LIBXML_XPTR_ENABLED)
108 }
109#endif
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000110 xmlXPathDebugDumpObject(stdout, res, 0);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000111 xmlXPathFreeObject(res);
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000112 xmlXPathFreeContext(ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000113}
114
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000115static void
116testXPathFile(const char *filename) {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000117 FILE *input;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000118 char expression[5000];
Daniel Veillard7e99c632000-10-06 12:59:53 +0000119 int len;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000120
121 input = fopen(filename, "r");
122 if (input == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000123 xmlGenericError(xmlGenericErrorContext,
124 "Cannot open %s for reading\n", filename);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000125 return;
126 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000127 while (fgets(expression, 4500, input) != NULL) {
128 len = strlen(expression);
Daniel Veillard7e99c632000-10-06 12:59:53 +0000129 len--;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800130 while ((len >= 0) &&
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000131 ((expression[len] == '\n') || (expression[len] == '\t') ||
132 (expression[len] == '\r') || (expression[len] == ' '))) len--;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800133 expression[len + 1] = 0;
Daniel Veillard7e99c632000-10-06 12:59:53 +0000134 if (len >= 0) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000135 printf("\n========================\nExpression: %s\n", expression) ;
136 testXPath(expression);
Daniel Veillard7e99c632000-10-06 12:59:53 +0000137 }
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000138 }
139
140 fclose(input);
141}
142
143int main(int argc, char **argv) {
144 int i;
145 int strings = 0;
146 int usefile = 0;
147 char *filename = NULL;
148
149 for (i = 1; i < argc ; i++) {
Daniel Veillardac260302000-10-04 13:33:43 +0000150#if defined(LIBXML_XPTR_ENABLED)
151 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
152 xptr++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800153 else
Daniel Veillardac260302000-10-04 13:33:43 +0000154#endif
Daniel Veillard09190202001-08-16 16:27:41 +0000155 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000156 debug++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000157 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
Daniel Veillard740abf52000-10-02 23:04:54 +0000158 valid++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000159 else if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000160 expr++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000161 else if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree")))
Daniel Veillardb38bd552001-04-03 18:22:00 +0000162 tree++;
Daniel Veillard7583a592001-07-08 13:15:55 +0000163 else if ((!strcmp(argv[i], "-nocdata")) ||
164 (!strcmp(argv[i], "--nocdata")))
165 nocdata++;
166 else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000167 filename = argv[++i];
Daniel Veillard7583a592001-07-08 13:15:55 +0000168 else if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000169 usefile++;
170 }
Daniel Veillard740abf52000-10-02 23:04:54 +0000171 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard48da9102001-08-07 01:10:10 +0000172 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
173 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillard68e9e742002-11-16 15:35:11 +0000174 xmlSubstituteEntitiesDefaultValue = 1;
Akira TAGOH961b5352012-07-03 14:13:59 +0900175#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard7583a592001-07-08 13:15:55 +0000176 if (nocdata != 0) {
177 xmlDefaultSAXHandlerInit();
178 xmlDefaultSAXHandler.cdataBlock = NULL;
179 }
Akira TAGOH961b5352012-07-03 14:13:59 +0900180#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000181 if (document == NULL) {
182 if (filename == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +0000183 document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000184 else
Daniel Veillard8874b942005-08-25 13:19:21 +0000185 document = xmlReadFile(filename,NULL,XML_PARSE_COMPACT);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000186 }
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");
Daniel Veillard740abf52000-10-02 23:04:54 +0000204 printf("\t--valid : switch on DTD support in the parser\n");
Daniel Veillardf6bf9212000-10-26 14:07:44 +0000205#if defined(LIBXML_XPTR_ENABLED)
206 printf("\t--xptr : expressions are XPointer expressions\n");
207#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000208 printf("\t--expr : debug XPath expressions only\n");
Daniel Veillardb38bd552001-04-03 18:22:00 +0000209 printf("\t--tree : show the compiled XPath tree\n");
Daniel Veillard7583a592001-07-08 13:15:55 +0000210 printf("\t--nocdata : do not generate CDATA nodes\n");
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000211 printf("\t--input filename : or\n");
212 printf("\t-i filename : read the document from filename\n");
213 printf("\t--file : or\n");
214 printf("\t-f : read queries from files, args\n");
215 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800216 if (document != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000217 xmlFreeDoc(document);
218 xmlCleanupParser();
219 xmlMemoryDump();
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000220
221 return(0);
222}
Daniel Veillard361d8452000-04-03 19:48:13 +0000223#else
224#include <stdio.h>
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000225int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
Daniel Veillard361d8452000-04-03 19:48:13 +0000226 printf("%s : XPath/Debug support not compiled in\n", argv[0]);
227 return(0);
228}
229#endif /* LIBXML_XPATH_ENABLED */