BRANCH release for 2.5.x critical patches
[baserock-morphs:libxml2.git] / testRelax.c
1 /*
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
48 static int debug = 0;
49 #endif
50 static int noout = 0;
51 static int tree = 0;
52 #ifdef HAVE_SYS_MMAN_H
53 static int memory = 0;
54 #endif
55
56
57 int main(int argc, char **argv) {
58     int i;
59     int files = 0;
60     xmlRelaxNGPtr schema = NULL;
61
62     for (i = 1; i < argc ; i++) {
63 #ifdef LIBXML_DEBUG_ENABLED
64         if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
65             debug++;
66         else
67 #endif
68 #ifdef HAVE_SYS_MMAN_H
69         if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) {
70             memory++;
71         } else
72 #endif
73         if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) {
74             noout++;
75         } else
76         if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) {
77             tree++;
78         }
79     }
80     xmlLineNumbersDefault(1);
81     xmlSubstituteEntitiesDefault(1);
82     for (i = 1; i < argc ; i++) {
83         if (argv[i][0] != '-') {
84             if (schema == NULL) {
85                 xmlRelaxNGParserCtxtPtr ctxt;
86
87 #ifdef HAVE_SYS_MMAN_H
88                 if (memory) {
89                     int fd;
90                     struct stat info;
91                     const char *base;
92                     if (stat(argv[i], &info) < 0) 
93                         break;
94                     if ((fd = open(argv[i], O_RDONLY)) < 0)
95                         break;
96                     base = mmap(NULL, info.st_size, PROT_READ,
97                                 MAP_SHARED, fd, 0) ;
98                     if (base == (void *) MAP_FAILED)
99                         break;
100
101                     ctxt = xmlRelaxNGNewMemParserCtxt((char *)base,info.st_size);
102
103                     xmlRelaxNGSetParserErrors(ctxt,
104                             (xmlRelaxNGValidityErrorFunc) fprintf,
105                             (xmlRelaxNGValidityWarningFunc) fprintf,
106                             stderr);
107                     schema = xmlRelaxNGParse(ctxt);
108                     xmlRelaxNGFreeParserCtxt(ctxt);
109                     munmap((char *) base, info.st_size);
110                 } else
111 #endif
112                 {
113                     ctxt = xmlRelaxNGNewParserCtxt(argv[i]);
114                     xmlRelaxNGSetParserErrors(ctxt,
115                             (xmlRelaxNGValidityErrorFunc) fprintf,
116                             (xmlRelaxNGValidityWarningFunc) fprintf,
117                             stderr);
118                     schema = xmlRelaxNGParse(ctxt);
119                     xmlRelaxNGFreeParserCtxt(ctxt);
120                 }
121                 if (schema == NULL) {
122                     printf("Relax-NG schema %s failed to compile\n", argv[i]);
123                     files = -1;
124                     break;
125                 }
126 #ifdef LIBXML_DEBUG_ENABLED
127                 if (debug)
128                     xmlRelaxNGDump(stdout, schema);
129 #endif
130                 if (tree)
131                     xmlRelaxNGDumpTree(stdout, schema);
132             } else {
133                 xmlDocPtr doc;
134
135                 doc = xmlParseFile(argv[i]);
136
137                 if (doc == NULL) {
138                     fprintf(stderr, "Could not parse %s\n", argv[i]);
139                 } else {
140                     xmlRelaxNGValidCtxtPtr ctxt;
141                     int ret;
142
143                     ctxt = xmlRelaxNGNewValidCtxt(schema);
144                     xmlRelaxNGSetValidErrors(ctxt,
145                             (xmlRelaxNGValidityErrorFunc) fprintf,
146                             (xmlRelaxNGValidityWarningFunc) fprintf,
147                             stderr);
148                     ret = xmlRelaxNGValidateDoc(ctxt, doc);
149                     if (ret == 0) {
150                         printf("%s validates\n", argv[i]);
151                     } else if (ret > 0) {
152                         printf("%s fails to validate\n", argv[i]);
153                     } else {
154                         printf("%s validation generated an internal error\n",
155                                argv[i]);
156                     }
157                     xmlRelaxNGFreeValidCtxt(ctxt);
158                     xmlFreeDoc(doc);
159                 }
160             }
161             files ++;
162         }
163     }
164     if (schema != NULL)
165         xmlRelaxNGFree(schema);
166     if (files == 0) {
167         printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
168                argv[0]);
169         printf("\tParse the HTML files and output the result of the parsing\n");
170 #ifdef LIBXML_DEBUG_ENABLED
171         printf("\t--debug : dump a debug tree of the in-memory document\n");
172 #endif
173         printf("\t--noout : do not print the result\n");
174         printf("\t--tree : print the intermediate Relax-NG document tree\n");
175 #ifdef HAVE_SYS_MMAN_H
176         printf("\t--memory : test the schemas in memory parsing\n");
177 #endif
178     }
179     xmlRelaxNGCleanupTypes();
180     xmlCleanupParser();
181     xmlMemoryDump();
182
183     return(0);
184 }
185
186 #else
187 #include <stdio.h>
188 int main(int argc, char **argv) {
189     printf("%s : RelaxNG support not compiled in\n", argv[0]);
190     return(0);
191 }
192 #endif /* LIBXML_SCHEMAS_ENABLED */