testRelax.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * testRelax.c : a small tester program for RelaxNG validation
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * Daniel.Veillard@w3.org
  7. */
  8. #include "libxml.h"
  9. #ifdef LIBXML_SCHEMAS_ENABLED
  10. #include <libxml/xmlversion.h>
  11. #include <libxml/parser.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15. #ifdef HAVE_SYS_TYPES_H
  16. #include <sys/types.h>
  17. #endif
  18. #ifdef HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #ifdef HAVE_FCNTL_H
  22. #include <fcntl.h>
  23. #endif
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #ifdef HAVE_STDLIB_H
  28. #include <stdlib.h>
  29. #endif
  30. #ifdef HAVE_SYS_MMAN_H
  31. #include <sys/mman.h>
  32. /* seems needed for Solaris */
  33. #ifndef MAP_FAILED
  34. #define MAP_FAILED ((void *) -1)
  35. #endif
  36. #endif
  37. #include <libxml/xmlmemory.h>
  38. #include <libxml/debugXML.h>
  39. #include <libxml/relaxng.h>
  40. #ifdef LIBXML_DEBUG_ENABLED
  41. static int debug = 0;
  42. #endif
  43. static int noout = 0;
  44. static int tree = 0;
  45. #ifdef HAVE_SYS_MMAN_H
  46. static int memory = 0;
  47. #endif
  48. int main(int argc, char **argv) {
  49. int i;
  50. int files = 0;
  51. xmlRelaxNGPtr schema = NULL;
  52. for (i = 1; i < argc ; i++) {
  53. #ifdef LIBXML_DEBUG_ENABLED
  54. if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
  55. debug++;
  56. else
  57. #endif
  58. #ifdef HAVE_SYS_MMAN_H
  59. if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) {
  60. memory++;
  61. } else
  62. #endif
  63. if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
  64. noout++;
  65. } else
  66. if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) {
  67. tree++;
  68. }
  69. }
  70. xmlLineNumbersDefault(1);
  71. xmlSubstituteEntitiesDefault(1);
  72. for (i = 1; i < argc ; i++) {
  73. if (argv[i][0] != '-') {
  74. if (schema == NULL) {
  75. xmlRelaxNGParserCtxtPtr ctxt;
  76. #ifdef HAVE_SYS_MMAN_H
  77. if (memory) {
  78. int fd;
  79. struct stat info;
  80. const char *base;
  81. if (stat(argv[i], &info) < 0)
  82. break;
  83. if ((fd = open(argv[i], O_RDONLY)) < 0)
  84. break;
  85. base = mmap(NULL, info.st_size, PROT_READ,
  86. MAP_SHARED, fd, 0) ;
  87. if (base == (void *) MAP_FAILED)
  88. break;
  89. ctxt = xmlRelaxNGNewMemParserCtxt((char *)base,info.st_size);
  90. xmlRelaxNGSetParserErrors(ctxt,
  91. (xmlRelaxNGValidityErrorFunc) fprintf,
  92. (xmlRelaxNGValidityWarningFunc) fprintf,
  93. stderr);
  94. schema = xmlRelaxNGParse(ctxt);
  95. xmlRelaxNGFreeParserCtxt(ctxt);
  96. munmap((char *) base, info.st_size);
  97. } else
  98. #endif
  99. {
  100. ctxt = xmlRelaxNGNewParserCtxt(argv[i]);
  101. xmlRelaxNGSetParserErrors(ctxt,
  102. (xmlRelaxNGValidityErrorFunc) fprintf,
  103. (xmlRelaxNGValidityWarningFunc) fprintf,
  104. stderr);
  105. schema = xmlRelaxNGParse(ctxt);
  106. xmlRelaxNGFreeParserCtxt(ctxt);
  107. }
  108. if (schema == NULL) {
  109. printf("Relax-NG schema %s failed to compile\n", argv[i]);
  110. files = -1;
  111. break;
  112. }
  113. #ifdef LIBXML_OUTPUT_ENABLED
  114. #ifdef LIBXML_DEBUG_ENABLED
  115. if (debug)
  116. xmlRelaxNGDump(stdout, schema);
  117. #endif
  118. if (tree)
  119. xmlRelaxNGDumpTree(stdout, schema);
  120. #endif /* LIBXML_OUTPUT_ENABLED */
  121. } else {
  122. xmlDocPtr doc;
  123. doc = xmlReadFile(argv[i],NULL,0);
  124. if (doc == NULL) {
  125. fprintf(stderr, "Could not parse %s\n", argv[i]);
  126. } else {
  127. xmlRelaxNGValidCtxtPtr ctxt;
  128. int ret;
  129. ctxt = xmlRelaxNGNewValidCtxt(schema);
  130. xmlRelaxNGSetValidErrors(ctxt,
  131. (xmlRelaxNGValidityErrorFunc) fprintf,
  132. (xmlRelaxNGValidityWarningFunc) fprintf,
  133. stderr);
  134. ret = xmlRelaxNGValidateDoc(ctxt, doc);
  135. if (ret == 0) {
  136. printf("%s validates\n", argv[i]);
  137. } else if (ret > 0) {
  138. printf("%s fails to validate\n", argv[i]);
  139. } else {
  140. printf("%s validation generated an internal error\n",
  141. argv[i]);
  142. }
  143. xmlRelaxNGFreeValidCtxt(ctxt);
  144. xmlFreeDoc(doc);
  145. }
  146. }
  147. files ++;
  148. }
  149. }
  150. if (schema != NULL)
  151. xmlRelaxNGFree(schema);
  152. if (files == 0) {
  153. printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
  154. argv[0]);
  155. printf("\tParse the HTML files and output the result of the parsing\n");
  156. #ifdef LIBXML_DEBUG_ENABLED
  157. printf("\t--debug : dump a debug tree of the in-memory document\n");
  158. #endif
  159. printf("\t--noout : do not print the result\n");
  160. printf("\t--tree : print the intermediate Relax-NG document tree\n");
  161. #ifdef HAVE_SYS_MMAN_H
  162. printf("\t--memory : test the schemas in memory parsing\n");
  163. #endif
  164. }
  165. xmlRelaxNGCleanupTypes();
  166. xmlCleanupParser();
  167. xmlMemoryDump();
  168. return(0);
  169. }
  170. #else
  171. #include <stdio.h>
  172. int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  173. printf("%s : RelaxNG support not compiled in\n", argv[0]);
  174. return(0);
  175. }
  176. #endif /* LIBXML_SCHEMAS_ENABLED */