testXPath.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * testXPath.c : a small tester program for XPath.
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. */
  8. #include "libxml.h"
  9. #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED)
  10. #include <string.h>
  11. #ifdef HAVE_SYS_TYPES_H
  12. #include <sys/types.h>
  13. #endif
  14. #ifdef HAVE_SYS_STAT_H
  15. #include <sys/stat.h>
  16. #endif
  17. #ifdef HAVE_FCNTL_H
  18. #include <fcntl.h>
  19. #endif
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #ifdef HAVE_STDLIB_H
  24. #include <stdlib.h>
  25. #endif
  26. #include <libxml/xpath.h>
  27. #include <libxml/tree.h>
  28. #include <libxml/parser.h>
  29. #include <libxml/debugXML.h>
  30. #include <libxml/xmlmemory.h>
  31. #include <libxml/parserInternals.h>
  32. #include <libxml/xpathInternals.h>
  33. #include <libxml/xmlerror.h>
  34. #include <libxml/globals.h>
  35. #if defined(LIBXML_XPTR_ENABLED)
  36. #include <libxml/xpointer.h>
  37. static int xptr = 0;
  38. #endif
  39. static int debug = 0;
  40. static int valid = 0;
  41. static int expr = 0;
  42. static int tree = 0;
  43. static int nocdata = 0;
  44. static xmlDocPtr document = NULL;
  45. /*
  46. * Default document
  47. */
  48. static xmlChar buffer[] =
  49. "<?xml version=\"1.0\"?>\n\
  50. <EXAMPLE prop1=\"gnome is great\" prop2=\"&amp; linux too\">\n\
  51. <head>\n\
  52. <title>Welcome to Gnome</title>\n\
  53. </head>\n\
  54. <chapter>\n\
  55. <title>The Linux adventure</title>\n\
  56. <p>bla bla bla ...</p>\n\
  57. <image href=\"linus.gif\"/>\n\
  58. <p>...</p>\n\
  59. </chapter>\n\
  60. <chapter>\n\
  61. <title>Chapter 2</title>\n\
  62. <p>this is chapter 2 ...</p>\n\
  63. </chapter>\n\
  64. <chapter>\n\
  65. <title>Chapter 3</title>\n\
  66. <p>this is chapter 3 ...</p>\n\
  67. </chapter>\n\
  68. </EXAMPLE>\n\
  69. ";
  70. static void
  71. testXPath(const char *str) {
  72. xmlXPathObjectPtr res;
  73. xmlXPathContextPtr ctxt;
  74. #if defined(LIBXML_XPTR_ENABLED)
  75. if (xptr) {
  76. ctxt = xmlXPtrNewContext(document, NULL, NULL);
  77. res = xmlXPtrEval(BAD_CAST str, ctxt);
  78. } else {
  79. #endif
  80. ctxt = xmlXPathNewContext(document);
  81. ctxt->node = xmlDocGetRootElement(document);
  82. if (expr)
  83. res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
  84. else {
  85. /* res = xmlXPathEval(BAD_CAST str, ctxt); */
  86. xmlXPathCompExprPtr comp;
  87. comp = xmlXPathCompile(BAD_CAST str);
  88. if (comp != NULL) {
  89. if (tree)
  90. xmlXPathDebugDumpCompExpr(stdout, comp, 0);
  91. res = xmlXPathCompiledEval(comp, ctxt);
  92. xmlXPathFreeCompExpr(comp);
  93. } else
  94. res = NULL;
  95. }
  96. #if defined(LIBXML_XPTR_ENABLED)
  97. }
  98. #endif
  99. xmlXPathDebugDumpObject(stdout, res, 0);
  100. xmlXPathFreeObject(res);
  101. xmlXPathFreeContext(ctxt);
  102. }
  103. static void
  104. testXPathFile(const char *filename) {
  105. FILE *input;
  106. char expression[5000];
  107. int len;
  108. input = fopen(filename, "r");
  109. if (input == NULL) {
  110. xmlGenericError(xmlGenericErrorContext,
  111. "Cannot open %s for reading\n", filename);
  112. return;
  113. }
  114. while (fgets(expression, 4500, input) != NULL) {
  115. len = strlen(expression);
  116. len--;
  117. while ((len >= 0) &&
  118. ((expression[len] == '\n') || (expression[len] == '\t') ||
  119. (expression[len] == '\r') || (expression[len] == ' '))) len--;
  120. expression[len + 1] = 0;
  121. if (len >= 0) {
  122. printf("\n========================\nExpression: %s\n", expression) ;
  123. testXPath(expression);
  124. }
  125. }
  126. fclose(input);
  127. }
  128. int main(int argc, char **argv) {
  129. int i;
  130. int strings = 0;
  131. int usefile = 0;
  132. char *filename = NULL;
  133. for (i = 1; i < argc ; i++) {
  134. #if defined(LIBXML_XPTR_ENABLED)
  135. if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr")))
  136. xptr++;
  137. else
  138. #endif
  139. if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
  140. debug++;
  141. else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
  142. valid++;
  143. else if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr")))
  144. expr++;
  145. else if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree")))
  146. tree++;
  147. else if ((!strcmp(argv[i], "-nocdata")) ||
  148. (!strcmp(argv[i], "--nocdata")))
  149. nocdata++;
  150. else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input")))
  151. filename = argv[++i];
  152. else if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file")))
  153. usefile++;
  154. }
  155. if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
  156. xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
  157. xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
  158. xmlSubstituteEntitiesDefaultValue = 1;
  159. if (nocdata != 0) {
  160. xmlDefaultSAXHandlerInit();
  161. xmlDefaultSAXHandler.cdataBlock = NULL;
  162. }
  163. if (document == NULL) {
  164. if (filename == NULL)
  165. document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT);
  166. else
  167. document = xmlReadFile(filename,NULL,XML_PARSE_COMPACT);
  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");
  186. printf("\t--valid : switch on DTD support in the parser\n");
  187. #if defined(LIBXML_XPTR_ENABLED)
  188. printf("\t--xptr : expressions are XPointer expressions\n");
  189. #endif
  190. printf("\t--expr : debug XPath expressions only\n");
  191. printf("\t--tree : show the compiled XPath tree\n");
  192. printf("\t--nocdata : do not generate CDATA nodes\n");
  193. printf("\t--input filename : or\n");
  194. printf("\t-i filename : read the document from filename\n");
  195. printf("\t--file : or\n");
  196. printf("\t-f : read queries from files, args\n");
  197. }
  198. if (document != NULL)
  199. xmlFreeDoc(document);
  200. xmlCleanupParser();
  201. xmlMemoryDump();
  202. return(0);
  203. }
  204. #else
  205. #include <stdio.h>
  206. int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  207. printf("%s : XPath/Debug support not compiled in\n", argv[0]);
  208. return(0);
  209. }
  210. #endif /* LIBXML_XPATH_ENABLED */