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