blob: 270251875e63ced1e6a1d8a22b885af52502e919 [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
Daniel Veillard6045c902002-10-09 21:13:59 +000035#ifdef HAVE_SYS_MMAN_H
36#include <sys/mman.h>
37/* seems needed for Solaris */
38#ifndef MAP_FAILED
39#define MAP_FAILED ((void *) -1)
40#endif
41#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000042
43#include <libxml/xmlmemory.h>
44#include <libxml/debugXML.h>
45#include <libxml/xmlschemas.h>
Daniel Veillard118aed72002-09-24 14:13:13 +000046#include <libxml/xmlschemastypes.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000047
48#ifdef LIBXML_DEBUG_ENABLED
49static int debug = 0;
50#endif
51static int noout = 0;
Daniel Veillard6045c902002-10-09 21:13:59 +000052#ifdef HAVE_SYS_MMAN_H
53static int memory = 0;
54#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000055
56
57int main(int argc, char **argv) {
58 int i;
59 int files = 0;
60 xmlSchemaPtr schema = NULL;
61
62 for (i = 1; i < argc ; i++) {
Daniel Veillard42766c02002-08-22 20:52:17 +000063#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +000064 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
65 debug++;
66 else
Daniel Veillard42766c02002-08-22 20:52:17 +000067#endif
Daniel Veillard6045c902002-10-09 21:13:59 +000068#ifdef HAVE_SYS_MMAN_H
69 if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) {
70 memory++;
71 } else
72#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000073 if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
74 noout++;
75 }
76 }
77 xmlLineNumbersDefault(1);
78 for (i = 1; i < argc ; i++) {
79 if (argv[i][0] != '-') {
80 if (schema == NULL) {
81 xmlSchemaParserCtxtPtr ctxt;
82
Daniel Veillard6045c902002-10-09 21:13:59 +000083#ifdef HAVE_SYS_MMAN_H
84 if (memory) {
85 int fd;
86 struct stat info;
87 const char *base;
88 if (stat(argv[i], &info) < 0)
89 break;
90 if ((fd = open(argv[i], O_RDONLY)) < 0)
91 break;
92 base = mmap(NULL, info.st_size, PROT_READ,
93 MAP_SHARED, fd, 0) ;
94 if (base == (void *) MAP_FAILED)
95 break;
96
97 ctxt = xmlSchemaNewMemParserCtxt((char *)base,info.st_size);
98
99 xmlSchemaSetParserErrors(ctxt,
100 (xmlSchemaValidityErrorFunc) fprintf,
101 (xmlSchemaValidityWarningFunc) fprintf,
102 stderr);
103 schema = xmlSchemaParse(ctxt);
104 xmlSchemaFreeParserCtxt(ctxt);
105 munmap((char *) base, info.st_size);
106 } else
107#endif
108 {
109 ctxt = xmlSchemaNewParserCtxt(argv[i]);
110 xmlSchemaSetParserErrors(ctxt,
111 (xmlSchemaValidityErrorFunc) fprintf,
112 (xmlSchemaValidityWarningFunc) fprintf,
113 stderr);
114 schema = xmlSchemaParse(ctxt);
115 xmlSchemaFreeParserCtxt(ctxt);
116 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000117#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard42766c02002-08-22 20:52:17 +0000118#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +0000119 if (debug)
120 xmlSchemaDump(stdout, schema);
Daniel Veillard42766c02002-08-22 20:52:17 +0000121#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000122#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +0000123 } else {
124 xmlDocPtr doc;
125
126 doc = xmlParseFile(argv[i]);
127
128 if (doc == NULL) {
129 fprintf(stderr, "Could not parse %s\n", argv[i]);
130 } else {
131 xmlSchemaValidCtxtPtr ctxt;
132 int ret;
133
134 ctxt = xmlSchemaNewValidCtxt(schema);
135 xmlSchemaSetValidErrors(ctxt,
136 (xmlSchemaValidityErrorFunc) fprintf,
137 (xmlSchemaValidityWarningFunc) fprintf,
138 stderr);
139 ret = xmlSchemaValidateDoc(ctxt, doc);
Daniel Veillard8651f532002-04-17 09:06:27 +0000140 if (ret == 0) {
141 printf("%s validates\n", argv[i]);
142 } else if (ret > 0) {
143 printf("%s fails to validate\n", argv[i]);
144 } else {
145 printf("%s validation generated an internal error\n",
146 argv[i]);
147 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000148 xmlSchemaFreeValidCtxt(ctxt);
149 xmlFreeDoc(doc);
150 }
151 }
152 files ++;
153 }
154 }
155 if (schema != NULL)
156 xmlSchemaFree(schema);
157 if (files == 0) {
158 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
159 argv[0]);
160 printf("\tParse the HTML files and output the result of the parsing\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000161#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard4255d502002-04-16 15:50:10 +0000162 printf("\t--debug : dump a debug tree of the in-memory document\n");
Daniel Veillard42766c02002-08-22 20:52:17 +0000163#endif
Daniel Veillard4255d502002-04-16 15:50:10 +0000164 printf("\t--noout : do not print the result\n");
Daniel Veillard6045c902002-10-09 21:13:59 +0000165#ifdef HAVE_SYS_MMAN_H
166 printf("\t--memory : test the schemas in memory parsing\n");
167#endif
Daniel Veillard4255d502002-04-16 15:50:10 +0000168 }
169 xmlSchemaCleanupTypes();
170 xmlCleanupParser();
171 xmlMemoryDump();
172
173 return(0);
174}
175
176#else
177#include <stdio.h>
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000178int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000179 printf("%s : Schemas support not compiled in\n", argv[0]);
180 return(0);
181}
182#endif /* LIBXML_SCHEMAS_ENABLED */