blob: 316816766c0f07439a0d80593b38617e437beca9 [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++) {
52 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
53 debug++;
54 else
55 if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
56 noout++;
57 }
58 }
59 xmlLineNumbersDefault(1);
60 for (i = 1; i < argc ; i++) {
61 if (argv[i][0] != '-') {
62 if (schema == NULL) {
63 xmlSchemaParserCtxtPtr ctxt;
64
65 ctxt = xmlSchemaNewParserCtxt(argv[i]);
66 xmlSchemaSetParserErrors(ctxt,
67 (xmlSchemaValidityErrorFunc) fprintf,
68 (xmlSchemaValidityWarningFunc) fprintf,
69 stderr);
70 schema = xmlSchemaParse(ctxt);
71 xmlSchemaFreeParserCtxt(ctxt);
72 if (debug)
73 xmlSchemaDump(stdout, schema);
74 } else {
75 xmlDocPtr doc;
76
77 doc = xmlParseFile(argv[i]);
78
79 if (doc == NULL) {
80 fprintf(stderr, "Could not parse %s\n", argv[i]);
81 } else {
82 xmlSchemaValidCtxtPtr ctxt;
83 int ret;
84
85 ctxt = xmlSchemaNewValidCtxt(schema);
86 xmlSchemaSetValidErrors(ctxt,
87 (xmlSchemaValidityErrorFunc) fprintf,
88 (xmlSchemaValidityWarningFunc) fprintf,
89 stderr);
90 ret = xmlSchemaValidateDoc(ctxt, doc);
91 xmlSchemaFreeValidCtxt(ctxt);
92 xmlFreeDoc(doc);
93 }
94 }
95 files ++;
96 }
97 }
98 if (schema != NULL)
99 xmlSchemaFree(schema);
100 if (files == 0) {
101 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
102 argv[0]);
103 printf("\tParse the HTML files and output the result of the parsing\n");
104 printf("\t--debug : dump a debug tree of the in-memory document\n");
105 printf("\t--noout : do not print the result\n");
106 }
107 xmlSchemaCleanupTypes();
108 xmlCleanupParser();
109 xmlMemoryDump();
110
111 return(0);
112}
113
114#else
115#include <stdio.h>
116int main(int argc, char **argv) {
117 printf("%s : Schemas support not compiled in\n", argv[0]);
118 return(0);
119}
120#endif /* LIBXML_SCHEMAS_ENABLED */