• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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
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/xmlschemas.h>
46 #include <libxml/xmlschemastypes.h>
47 
48 #ifdef LIBXML_DEBUG_ENABLED
49 static int debug = 0;
50 #endif
51 static int noout = 0;
52 #ifdef HAVE_MMAP
53 static int memory = 0;
54 #endif
55 
56 
main(int argc,char ** argv)57 int main(int argc, char **argv) {
58     int i;
59     int files = 0;
60     xmlSchemaPtr 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_MMAP
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         }
76     }
77     xmlLineNumbersDefault(1);
78     for (i = 1; i < argc ; i++) {
79 	if (argv[i][0] != '-') {
80 	    if (schema == NULL) {
81 		xmlSchemaParserCtxtPtr ctxt;
82 
83 #ifdef HAVE_MMAP
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                             xmlGenericError, xmlGenericError, NULL);
101 		    schema = xmlSchemaParse(ctxt);
102 		    xmlSchemaFreeParserCtxt(ctxt);
103 		    munmap((char *) base, info.st_size);
104 		} else
105 #endif
106 		{
107 		    ctxt = xmlSchemaNewParserCtxt(argv[i]);
108 		    xmlSchemaSetParserErrors(ctxt,
109                             xmlGenericError, xmlGenericError, NULL);
110 		    schema = xmlSchemaParse(ctxt);
111 		    xmlSchemaFreeParserCtxt(ctxt);
112 		}
113 #ifdef LIBXML_OUTPUT_ENABLED
114 #ifdef LIBXML_DEBUG_ENABLED
115 		if (debug)
116 		    xmlSchemaDump(stdout, schema);
117 #endif
118 #endif /* LIBXML_OUTPUT_ENABLED */
119 		if (schema == NULL)
120 		    goto failed_schemas;
121 	    } else {
122 		xmlDocPtr doc;
123 
124 		doc = xmlReadFile(argv[i],NULL,0);
125 
126 		if (doc == NULL) {
127 		    fprintf(stderr, "Could not parse %s\n", argv[i]);
128 		} else {
129 		    xmlSchemaValidCtxtPtr ctxt;
130 		    int ret;
131 
132 		    ctxt = xmlSchemaNewValidCtxt(schema);
133 		    xmlSchemaSetValidErrors(ctxt,
134                             xmlGenericError, xmlGenericError, NULL);
135 		    ret = xmlSchemaValidateDoc(ctxt, doc);
136 		    if (ret == 0) {
137 			printf("%s validates\n", argv[i]);
138 		    } else if (ret > 0) {
139 			printf("%s fails to validate\n", argv[i]);
140 		    } else {
141 			printf("%s validation generated an internal error\n",
142 			       argv[i]);
143 		    }
144 		    xmlSchemaFreeValidCtxt(ctxt);
145 		    xmlFreeDoc(doc);
146 		}
147 	    }
148 	    files ++;
149 	}
150     }
151     if (schema != NULL)
152 	xmlSchemaFree(schema);
153     if (files == 0) {
154 	printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
155 	       argv[0]);
156 	printf("\tParse the HTML files and output the result of the parsing\n");
157 #ifdef LIBXML_DEBUG_ENABLED
158 	printf("\t--debug : dump a debug tree of the in-memory document\n");
159 #endif
160 	printf("\t--noout : do not print the result\n");
161 #ifdef HAVE_MMAP
162 	printf("\t--memory : test the schemas in memory parsing\n");
163 #endif
164     }
165 failed_schemas:
166     xmlSchemaCleanupTypes();
167     xmlCleanupParser();
168     xmlMemoryDump();
169 
170     return(0);
171 }
172 
173 #else
174 #include <stdio.h>
main(int argc ATTRIBUTE_UNUSED,char ** argv ATTRIBUTE_UNUSED)175 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
176     printf("%s : Schemas support not compiled in\n", argv[0]);
177     return(0);
178 }
179 #endif /* LIBXML_SCHEMAS_ENABLED */
180