Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 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 | |
| 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 | |
| 36 | #include <libxml/xmlmemory.h> |
| 37 | #include <libxml/debugXML.h> |
| 38 | #include <libxml/xmlschemas.h> |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 39 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 40 | |
| 41 | #ifdef LIBXML_DEBUG_ENABLED |
| 42 | static int debug = 0; |
| 43 | #endif |
| 44 | static int noout = 0; |
| 45 | |
| 46 | |
| 47 | int main(int argc, char **argv) { |
| 48 | int i; |
| 49 | int files = 0; |
| 50 | xmlSchemaPtr schema = NULL; |
| 51 | |
| 52 | for (i = 1; i < argc ; i++) { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 53 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 54 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 55 | debug++; |
| 56 | else |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 57 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 58 | if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) { |
| 59 | noout++; |
| 60 | } |
| 61 | } |
| 62 | xmlLineNumbersDefault(1); |
| 63 | for (i = 1; i < argc ; i++) { |
| 64 | if (argv[i][0] != '-') { |
| 65 | if (schema == NULL) { |
| 66 | xmlSchemaParserCtxtPtr ctxt; |
| 67 | |
| 68 | ctxt = xmlSchemaNewParserCtxt(argv[i]); |
| 69 | xmlSchemaSetParserErrors(ctxt, |
| 70 | (xmlSchemaValidityErrorFunc) fprintf, |
| 71 | (xmlSchemaValidityWarningFunc) fprintf, |
| 72 | stderr); |
| 73 | schema = xmlSchemaParse(ctxt); |
| 74 | xmlSchemaFreeParserCtxt(ctxt); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 75 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 76 | if (debug) |
| 77 | xmlSchemaDump(stdout, schema); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 78 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 79 | } else { |
| 80 | xmlDocPtr doc; |
| 81 | |
| 82 | doc = xmlParseFile(argv[i]); |
| 83 | |
| 84 | if (doc == NULL) { |
| 85 | fprintf(stderr, "Could not parse %s\n", argv[i]); |
| 86 | } else { |
| 87 | xmlSchemaValidCtxtPtr ctxt; |
| 88 | int ret; |
| 89 | |
| 90 | ctxt = xmlSchemaNewValidCtxt(schema); |
| 91 | xmlSchemaSetValidErrors(ctxt, |
| 92 | (xmlSchemaValidityErrorFunc) fprintf, |
| 93 | (xmlSchemaValidityWarningFunc) fprintf, |
| 94 | stderr); |
| 95 | ret = xmlSchemaValidateDoc(ctxt, doc); |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 96 | if (ret == 0) { |
| 97 | printf("%s validates\n", argv[i]); |
| 98 | } else if (ret > 0) { |
| 99 | printf("%s fails to validate\n", argv[i]); |
| 100 | } else { |
| 101 | printf("%s validation generated an internal error\n", |
| 102 | argv[i]); |
| 103 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 104 | xmlSchemaFreeValidCtxt(ctxt); |
| 105 | xmlFreeDoc(doc); |
| 106 | } |
| 107 | } |
| 108 | files ++; |
| 109 | } |
| 110 | } |
| 111 | if (schema != NULL) |
| 112 | xmlSchemaFree(schema); |
| 113 | if (files == 0) { |
| 114 | printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n", |
| 115 | argv[0]); |
| 116 | printf("\tParse the HTML files and output the result of the parsing\n"); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 117 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 118 | printf("\t--debug : dump a debug tree of the in-memory document\n"); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 119 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 120 | printf("\t--noout : do not print the result\n"); |
| 121 | } |
| 122 | xmlSchemaCleanupTypes(); |
| 123 | xmlCleanupParser(); |
| 124 | xmlMemoryDump(); |
| 125 | |
| 126 | return(0); |
| 127 | } |
| 128 | |
| 129 | #else |
| 130 | #include <stdio.h> |
| 131 | int main(int argc, char **argv) { |
| 132 | printf("%s : Schemas support not compiled in\n", argv[0]); |
| 133 | return(0); |
| 134 | } |
| 135 | #endif /* LIBXML_SCHEMAS_ENABLED */ |