blob: a22c42970b2de04315249f512572452ffbca38ec [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
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>
39
40#ifdef LIBXML_DEBUG_ENABLED
41static int debug = 0;
42#endif
43static int noout = 0;
44
45
46int main(int argc, char **argv) {
47 int i;
48 int files = 0;
49 xmlSchemaPtr schema = NULL;
50
51 for (i = 1; i < argc ; i++) {
Daniel Veillard42766c02002-08-22 20:52:17 +000052#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +000053 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
54 debug++;
55 else
Daniel Veillard42766c02002-08-22 20:52:17 +000056#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000057 if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
58 noout++;
59 }
60 }
61 xmlLineNumbersDefault(1);
62 for (i = 1; i < argc ; i++) {
63 if (argv[i][0] != '-') {
64 if (schema == NULL) {
65 xmlSchemaParserCtxtPtr ctxt;
66
67 ctxt = xmlSchemaNewParserCtxt(argv[i]);
68 xmlSchemaSetParserErrors(ctxt,
69 (xmlSchemaValidityErrorFunc) fprintf,
70 (xmlSchemaValidityWarningFunc) fprintf,
71 stderr);
72 schema = xmlSchemaParse(ctxt);
73 xmlSchemaFreeParserCtxt(ctxt);
Daniel Veillard42766c02002-08-22 20:52:17 +000074#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +000075 if (debug)
76 xmlSchemaDump(stdout, schema);
Daniel Veillard42766c02002-08-22 20:52:17 +000077#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000078 } else {
79 xmlDocPtr doc;
80
81 doc = xmlParseFile(argv[i]);
82
83 if (doc == NULL) {
84 fprintf(stderr, "Could not parse %s\n", argv[i]);
85 } else {
86 xmlSchemaValidCtxtPtr ctxt;
87 int ret;
88
89 ctxt = xmlSchemaNewValidCtxt(schema);
90 xmlSchemaSetValidErrors(ctxt,
91 (xmlSchemaValidityErrorFunc) fprintf,
92 (xmlSchemaValidityWarningFunc) fprintf,
93 stderr);
94 ret = xmlSchemaValidateDoc(ctxt, doc);
Daniel Veillard8651f532002-04-17 09:06:27 +000095 if (ret == 0) {
96 printf("%s validates\n", argv[i]);
97 } else if (ret > 0) {
98 printf("%s fails to validate\n", argv[i]);
99 } else {
100 printf("%s validation generated an internal error\n",
101 argv[i]);
102 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000103 xmlSchemaFreeValidCtxt(ctxt);
104 xmlFreeDoc(doc);
105 }
106 }
107 files ++;
108 }
109 }
110 if (schema != NULL)
111 xmlSchemaFree(schema);
112 if (files == 0) {
113 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
114 argv[0]);
115 printf("\tParse the HTML files and output the result of the parsing\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000116#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +0000117 printf("\t--debug : dump a debug tree of the in-memory document\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000118#endif
Daniel Veillard4255d502002-04-16 15:50:10 +0000119 printf("\t--noout : do not print the result\n");
120 }
121 xmlSchemaCleanupTypes();
122 xmlCleanupParser();
123 xmlMemoryDump();
124
125 return(0);
126}
127
128#else
129#include <stdio.h>
130int main(int argc, char **argv) {
131 printf("%s : Schemas support not compiled in\n", argv[0]);
132 return(0);
133}
134#endif /* LIBXML_SCHEMAS_ENABLED */