testSchemas.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * testSchemas.c : a small tester program for Schema 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/xmlschemas.h>
  40. #include <libxml/xmlschemastypes.h>
  41. #ifdef LIBXML_DEBUG_ENABLED
  42. static int debug = 0;
  43. #endif
  44. static int noout = 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. xmlSchemaPtr 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. }
  66. }
  67. xmlLineNumbersDefault(1);
  68. for (i = 1; i < argc ; i++) {
  69. if (argv[i][0] != '-') {
  70. if (schema == NULL) {
  71. xmlSchemaParserCtxtPtr ctxt;
  72. #ifdef HAVE_SYS_MMAN_H
  73. if (memory) {
  74. int fd;
  75. struct stat info;
  76. const char *base;
  77. if (stat(argv[i], &info) < 0)
  78. break;
  79. if ((fd = open(argv[i], O_RDONLY)) < 0)
  80. break;
  81. base = mmap(NULL, info.st_size, PROT_READ,
  82. MAP_SHARED, fd, 0) ;
  83. if (base == (void *) MAP_FAILED)
  84. break;
  85. ctxt = xmlSchemaNewMemParserCtxt((char *)base,info.st_size);
  86. xmlSchemaSetParserErrors(ctxt,
  87. (xmlSchemaValidityErrorFunc) fprintf,
  88. (xmlSchemaValidityWarningFunc) fprintf,
  89. stderr);
  90. schema = xmlSchemaParse(ctxt);
  91. xmlSchemaFreeParserCtxt(ctxt);
  92. munmap((char *) base, info.st_size);
  93. } else
  94. #endif
  95. {
  96. ctxt = xmlSchemaNewParserCtxt(argv[i]);
  97. xmlSchemaSetParserErrors(ctxt,
  98. (xmlSchemaValidityErrorFunc) fprintf,
  99. (xmlSchemaValidityWarningFunc) fprintf,
  100. stderr);
  101. schema = xmlSchemaParse(ctxt);
  102. xmlSchemaFreeParserCtxt(ctxt);
  103. }
  104. #ifdef LIBXML_OUTPUT_ENABLED
  105. #ifdef LIBXML_DEBUG_ENABLED
  106. if (debug)
  107. xmlSchemaDump(stdout, schema);
  108. #endif
  109. #endif /* LIBXML_OUTPUT_ENABLED */
  110. if (schema == NULL)
  111. goto failed_schemas;
  112. } else {
  113. xmlDocPtr doc;
  114. doc = xmlReadFile(argv[i],NULL,0);
  115. if (doc == NULL) {
  116. fprintf(stderr, "Could not parse %s\n", argv[i]);
  117. } else {
  118. xmlSchemaValidCtxtPtr ctxt;
  119. int ret;
  120. ctxt = xmlSchemaNewValidCtxt(schema);
  121. xmlSchemaSetValidErrors(ctxt,
  122. (xmlSchemaValidityErrorFunc) fprintf,
  123. (xmlSchemaValidityWarningFunc) fprintf,
  124. stderr);
  125. ret = xmlSchemaValidateDoc(ctxt, doc);
  126. if (ret == 0) {
  127. printf("%s validates\n", argv[i]);
  128. } else if (ret > 0) {
  129. printf("%s fails to validate\n", argv[i]);
  130. } else {
  131. printf("%s validation generated an internal error\n",
  132. argv[i]);
  133. }
  134. xmlSchemaFreeValidCtxt(ctxt);
  135. xmlFreeDoc(doc);
  136. }
  137. }
  138. files ++;
  139. }
  140. }
  141. if (schema != NULL)
  142. xmlSchemaFree(schema);
  143. if (files == 0) {
  144. printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
  145. argv[0]);
  146. printf("\tParse the HTML files and output the result of the parsing\n");
  147. #ifdef LIBXML_DEBUG_ENABLED
  148. printf("\t--debug : dump a debug tree of the in-memory document\n");
  149. #endif
  150. printf("\t--noout : do not print the result\n");
  151. #ifdef HAVE_SYS_MMAN_H
  152. printf("\t--memory : test the schemas in memory parsing\n");
  153. #endif
  154. }
  155. failed_schemas:
  156. xmlSchemaCleanupTypes();
  157. xmlCleanupParser();
  158. xmlMemoryDump();
  159. return(0);
  160. }
  161. #else
  162. #include <stdio.h>
  163. int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
  164. printf("%s : Schemas support not compiled in\n", argv[0]);
  165. return(0);
  166. }
  167. #endif /* LIBXML_SCHEMAS_ENABLED */