blob: b25a13968d2e4c6251babf8826fe35cf60d79491 [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>
Daniel Veillard118aed72002-09-24 14:13:13 +000039#include <libxml/xmlschemastypes.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000040
41#ifdef LIBXML_DEBUG_ENABLED
42static int debug = 0;
43#endif
44static int noout = 0;
45
46
47int 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 Veillard42766c02002-08-22 20:52:17 +000053#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +000054 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
55 debug++;
56 else
Daniel Veillard42766c02002-08-22 20:52:17 +000057#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000058 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 Veillard42766c02002-08-22 20:52:17 +000075#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +000076 if (debug)
77 xmlSchemaDump(stdout, schema);
Daniel Veillard42766c02002-08-22 20:52:17 +000078#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000079 } 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 Veillard8651f532002-04-17 09:06:27 +000096 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 Veillard4255d502002-04-16 15:50:10 +0000104 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 Veillard42766c02002-08-22 20:52:17 +0000117#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +0000118 printf("\t--debug : dump a debug tree of the in-memory document\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000119#endif
Daniel Veillard4255d502002-04-16 15:50:10 +0000120 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>
131int 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 */