2 * testRelax.c : a small tester program for RelaxNG 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/relaxng.h>
47 #ifdef LIBXML_DEBUG_ENABLED
52 #ifdef HAVE_SYS_MMAN_H
53 static int memory
= 0;
57 int main(int argc
, char **argv
) {
60 xmlRelaxNGPtr 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"))) {
76 if ((!strcmp(argv
[i
], "-tree")) || (!strcmp(argv
[i
], "--tree"))) {
80 xmlLineNumbersDefault(1);
81 xmlSubstituteEntitiesDefault(1);
82 for (i
= 1; i
< argc
; i
++) {
83 if (argv
[i
][0] != '-') {
85 xmlRelaxNGParserCtxtPtr ctxt
;
87 #ifdef HAVE_SYS_MMAN_H
92 if (stat(argv
[i
], &info
) < 0)
94 if ((fd
= open(argv
[i
], O_RDONLY
)) < 0)
96 base
= mmap(NULL
, info
.st_size
, PROT_READ
,
98 if (base
== (void *) MAP_FAILED
)
101 ctxt
= xmlRelaxNGNewMemParserCtxt((char *)base
,info
.st_size
);
103 xmlRelaxNGSetParserErrors(ctxt
,
104 (xmlRelaxNGValidityErrorFunc
) fprintf
,
105 (xmlRelaxNGValidityWarningFunc
) fprintf
,
107 schema
= xmlRelaxNGParse(ctxt
);
108 xmlRelaxNGFreeParserCtxt(ctxt
);
109 munmap((char *) base
, info
.st_size
);
113 ctxt
= xmlRelaxNGNewParserCtxt(argv
[i
]);
114 xmlRelaxNGSetParserErrors(ctxt
,
115 (xmlRelaxNGValidityErrorFunc
) fprintf
,
116 (xmlRelaxNGValidityWarningFunc
) fprintf
,
118 schema
= xmlRelaxNGParse(ctxt
);
119 xmlRelaxNGFreeParserCtxt(ctxt
);
121 if (schema
== NULL
) {
122 printf("Relax-NG schema %s failed to compile\n", argv
[i
]);
126 #ifdef LIBXML_OUTPUT_ENABLED
127 #ifdef LIBXML_DEBUG_ENABLED
129 xmlRelaxNGDump(stdout
, schema
);
132 xmlRelaxNGDumpTree(stdout
, schema
);
133 #endif /* LIBXML_OUTPUT_ENABLED */
137 doc
= xmlReadFile(argv
[i
],NULL
,0);
140 fprintf(stderr
, "Could not parse %s\n", argv
[i
]);
142 xmlRelaxNGValidCtxtPtr ctxt
;
145 ctxt
= xmlRelaxNGNewValidCtxt(schema
);
146 xmlRelaxNGSetValidErrors(ctxt
,
147 (xmlRelaxNGValidityErrorFunc
) fprintf
,
148 (xmlRelaxNGValidityWarningFunc
) fprintf
,
150 ret
= xmlRelaxNGValidateDoc(ctxt
, doc
);
152 printf("%s validates\n", argv
[i
]);
153 } else if (ret
> 0) {
154 printf("%s fails to validate\n", argv
[i
]);
156 printf("%s validation generated an internal error\n",
159 xmlRelaxNGFreeValidCtxt(ctxt
);
167 xmlRelaxNGFree(schema
);
169 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
171 printf("\tParse the HTML files and output the result of the parsing\n");
172 #ifdef LIBXML_DEBUG_ENABLED
173 printf("\t--debug : dump a debug tree of the in-memory document\n");
175 printf("\t--noout : do not print the result\n");
176 printf("\t--tree : print the intermediate Relax-NG document tree\n");
177 #ifdef HAVE_SYS_MMAN_H
178 printf("\t--memory : test the schemas in memory parsing\n");
181 xmlRelaxNGCleanupTypes();
190 int main(int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
) {
191 printf("%s : RelaxNG support not compiled in\n", argv
[0]);
194 #endif /* LIBXML_SCHEMAS_ENABLED */