2 * testSchemas.c : a small tester program for Schema validation
4 * See Copyright for the status of this software.
6 * Daniel.Veillard@w3.org
10 #ifdef LIBXML_SCHEMAS_ENABLED
12 #include <libxml/xmlversion.h>
13 #include <libxml/parser.h>
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
23 #ifdef HAVE_SYS_STAT_H
35 #ifdef HAVE_SYS_MMAN_H
37 /* seems needed for Solaris */
39 #define MAP_FAILED ((void *) -1)
43 #include <libxml/xmlmemory.h>
44 #include <libxml/debugXML.h>
45 #include <libxml/xmlschemas.h>
46 #include <libxml/xmlschemastypes.h>
48 #ifdef LIBXML_DEBUG_ENABLED
52 #ifdef HAVE_SYS_MMAN_H
53 static int memory
= 0;
57 int main(int argc
, char **argv
) {
60 xmlSchemaPtr schema
= NULL
;
62 for (i
= 1; i
< argc
; i
++) {
63 #ifdef LIBXML_DEBUG_ENABLED
64 if ((!strcmp(argv
[i
], "-debug")) || (!strcmp(argv
[i
], "--debug")))
68 #ifdef HAVE_SYS_MMAN_H
69 if ((!strcmp(argv
[i
], "-memory")) || (!strcmp(argv
[i
], "--memory"))) {
73 if ((!strcmp(argv
[i
], "-noout")) || (!strcmp(argv
[i
], "--noout"))) {
77 xmlLineNumbersDefault(1);
78 for (i
= 1; i
< argc
; i
++) {
79 if (argv
[i
][0] != '-') {
81 xmlSchemaParserCtxtPtr ctxt
;
83 #ifdef HAVE_SYS_MMAN_H
88 if (stat(argv
[i
], &info
) < 0)
90 if ((fd
= open(argv
[i
], O_RDONLY
)) < 0)
92 base
= mmap(NULL
, info
.st_size
, PROT_READ
,
94 if (base
== (void *) MAP_FAILED
)
97 ctxt
= xmlSchemaNewMemParserCtxt((char *)base
,info
.st_size
);
99 xmlSchemaSetParserErrors(ctxt
,
100 (xmlSchemaValidityErrorFunc
) fprintf
,
101 (xmlSchemaValidityWarningFunc
) fprintf
,
103 schema
= xmlSchemaParse(ctxt
);
104 xmlSchemaFreeParserCtxt(ctxt
);
105 munmap((char *) base
, info
.st_size
);
109 ctxt
= xmlSchemaNewParserCtxt(argv
[i
]);
110 xmlSchemaSetParserErrors(ctxt
,
111 (xmlSchemaValidityErrorFunc
) fprintf
,
112 (xmlSchemaValidityWarningFunc
) fprintf
,
114 schema
= xmlSchemaParse(ctxt
);
115 xmlSchemaFreeParserCtxt(ctxt
);
117 #ifdef LIBXML_OUTPUT_ENABLED
118 #ifdef LIBXML_DEBUG_ENABLED
120 xmlSchemaDump(stdout
, schema
);
122 #endif /* LIBXML_OUTPUT_ENABLED */
128 doc
= xmlReadFile(argv
[i
],NULL
,0);
131 fprintf(stderr
, "Could not parse %s\n", argv
[i
]);
133 xmlSchemaValidCtxtPtr ctxt
;
136 ctxt
= xmlSchemaNewValidCtxt(schema
);
137 xmlSchemaSetValidErrors(ctxt
,
138 (xmlSchemaValidityErrorFunc
) fprintf
,
139 (xmlSchemaValidityWarningFunc
) fprintf
,
141 ret
= xmlSchemaValidateDoc(ctxt
, doc
);
143 printf("%s validates\n", argv
[i
]);
144 } else if (ret
> 0) {
145 printf("%s fails to validate\n", argv
[i
]);
147 printf("%s validation generated an internal error\n",
150 xmlSchemaFreeValidCtxt(ctxt
);
158 xmlSchemaFree(schema
);
160 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
162 printf("\tParse the HTML files and output the result of the parsing\n");
163 #ifdef LIBXML_DEBUG_ENABLED
164 printf("\t--debug : dump a debug tree of the in-memory document\n");
166 printf("\t--noout : do not print the result\n");
167 #ifdef HAVE_SYS_MMAN_H
168 printf("\t--memory : test the schemas in memory parsing\n");
172 xmlSchemaCleanupTypes();
181 int main(int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
) {
182 printf("%s : Schemas support not compiled in\n", argv
[0]);
185 #endif /* LIBXML_SCHEMAS_ENABLED */