blob: ac1052b88079689e8dd3678b48961f7f221dc0f1 [file] [log] [blame]
Daniel Veillard98bf67a2003-01-23 22:00:09 +00001/*
2 * testRelax.c : a small tester program for RelaxNG 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#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
42
43#include <libxml/xmlmemory.h>
44#include <libxml/debugXML.h>
45#include <libxml/relaxng.h>
46
47#ifdef LIBXML_DEBUG_ENABLED
48static int debug = 0;
49#endif
50static int noout = 0;
51#ifdef HAVE_SYS_MMAN_H
52static int memory = 0;
53#endif
54
55
56int main(int argc, char **argv) {
57 int i;
58 int files = 0;
59 xmlRelaxNGPtr schema = NULL;
60
61 for (i = 1; i < argc ; i++) {
62#ifdef LIBXML_DEBUG_ENABLED
63 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
64 debug++;
65 else
66#endif
67#ifdef HAVE_SYS_MMAN_H
68 if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) {
69 memory++;
70 } else
71#endif
72 if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
73 noout++;
74 }
75 }
76 xmlLineNumbersDefault(1);
77 for (i = 1; i < argc ; i++) {
78 if (argv[i][0] != '-') {
79 if (schema == NULL) {
80 xmlRelaxNGParserCtxtPtr ctxt;
81
82#ifdef HAVE_SYS_MMAN_H
83 if (memory) {
84 int fd;
85 struct stat info;
86 const char *base;
87 if (stat(argv[i], &info) < 0)
88 break;
89 if ((fd = open(argv[i], O_RDONLY)) < 0)
90 break;
91 base = mmap(NULL, info.st_size, PROT_READ,
92 MAP_SHARED, fd, 0) ;
93 if (base == (void *) MAP_FAILED)
94 break;
95
96 ctxt = xmlRelaxNGNewMemParserCtxt((char *)base,info.st_size);
97
98 xmlRelaxNGSetParserErrors(ctxt,
99 (xmlRelaxNGValidityErrorFunc) fprintf,
100 (xmlRelaxNGValidityWarningFunc) fprintf,
101 stderr);
102 schema = xmlRelaxNGParse(ctxt);
103 xmlRelaxNGFreeParserCtxt(ctxt);
104 munmap((char *) base, info.st_size);
105 } else
106#endif
107 {
108 ctxt = xmlRelaxNGNewParserCtxt(argv[i]);
109 xmlRelaxNGSetParserErrors(ctxt,
110 (xmlRelaxNGValidityErrorFunc) fprintf,
111 (xmlRelaxNGValidityWarningFunc) fprintf,
112 stderr);
113 schema = xmlRelaxNGParse(ctxt);
114 xmlRelaxNGFreeParserCtxt(ctxt);
115 }
116 if (schema == NULL) {
117 printf("Relax-NG schema %s failed to compile\n", argv[i]);
118 files = -1;
119 break;
120 }
121#ifdef LIBXML_DEBUG_ENABLED
122 if (debug)
123 xmlRelaxNGDump(stdout, schema);
124#endif
125 } else {
126 xmlDocPtr doc;
127
128 doc = xmlParseFile(argv[i]);
129
130 if (doc == NULL) {
131 fprintf(stderr, "Could not parse %s\n", argv[i]);
132 } else {
133 xmlRelaxNGValidCtxtPtr ctxt;
134 int ret;
135
136 ctxt = xmlRelaxNGNewValidCtxt(schema);
137 xmlRelaxNGSetValidErrors(ctxt,
138 (xmlRelaxNGValidityErrorFunc) fprintf,
139 (xmlRelaxNGValidityWarningFunc) fprintf,
140 stderr);
141 ret = xmlRelaxNGValidateDoc(ctxt, doc);
142 if (ret == 0) {
143 printf("%s validates\n", argv[i]);
144 } else if (ret > 0) {
145 printf("%s fails to validate\n", argv[i]);
146 } else {
147 printf("%s validation generated an internal error\n",
148 argv[i]);
149 }
150 xmlRelaxNGFreeValidCtxt(ctxt);
151 xmlFreeDoc(doc);
152 }
153 }
154 files ++;
155 }
156 }
157 if (schema != NULL)
158 xmlRelaxNGFree(schema);
159 if (files == 0) {
160 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
161 argv[0]);
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");
165#endif
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");
169#endif
170 }
171 xmlRelaxNGCleanupTypes();
172 xmlCleanupParser();
173 xmlMemoryDump();
174
175 return(0);
176}
177
178#else
179#include <stdio.h>
180int main(int argc, char **argv) {
181 printf("%s : RelaxNG support not compiled in\n", argv[0]);
182 return(0);
183}
184#endif /* LIBXML_SCHEMAS_ENABLED */