/ libxml2 / testSchemas.c
testSchemas.c
  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/parser.h>
 13  #include <libxml/xmlreader.h>
 14  #include <libxml/xmlversion.h>
 15  
 16  #include <stdio.h>
 17  #include <string.h>
 18  #include <stdarg.h>
 19  
 20  
 21  #ifdef HAVE_SYS_TYPES_H
 22  #include <sys/types.h>
 23  #endif
 24  #ifdef HAVE_SYS_STAT_H
 25  #include <sys/stat.h>
 26  #endif
 27  #ifdef HAVE_FCNTL_H
 28  #include <fcntl.h>
 29  #endif
 30  #ifdef HAVE_UNISTD_H
 31  #include <unistd.h>
 32  #endif
 33  #ifdef HAVE_STDLIB_H
 34  #include <stdlib.h>
 35  #endif
 36  #ifdef HAVE_SYS_MMAN_H
 37  #include <sys/mman.h>
 38  /* seems needed for Solaris */
 39  #ifndef MAP_FAILED
 40  #define MAP_FAILED ((void *) -1)
 41  #endif
 42  #endif
 43  
 44  #include <libxml/xmlmemory.h>
 45  #include <libxml/debugXML.h>
 46  #include <libxml/xmlschemas.h>
 47  #include <libxml/xmlschemastypes.h>
 48  
 49  #ifdef LIBXML_DEBUG_ENABLED
 50  static int debug = 0;
 51  #endif
 52  #ifdef HAVE_MMAP
 53  static int memory = 0;
 54  #endif
 55  static int noout = 0;
 56  #ifdef LIBXML_READER_ENABLED
 57  static int stream = 0;
 58  #endif
 59  
 60  
 61  int main(int argc, char **argv) {
 62      int i;
 63      int files = 0;
 64      xmlSchemaPtr schema = NULL;
 65      const char *schemaPath = NULL;
 66  
 67      for (i = 1; i < argc ; i++) {
 68  #ifdef LIBXML_DEBUG_ENABLED
 69  	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
 70  	    debug++;
 71  #endif
 72  #ifdef HAVE_MMAP
 73  	else if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory")))
 74  	    memory++;
 75  #endif
 76  	else if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout")))
 77  	    noout++;
 78  #ifdef LIBXML_READER_ENABLED
 79  	else if ((!strcmp(argv[i], "-stream")) || (!strcmp(argv[i], "--stream")))
 80  	    stream++;
 81  #endif
 82      }
 83      xmlLineNumbersDefault(1);
 84      for (i = 1; i < argc ; i++) {
 85  	if (argv[i][0] != '-') {
 86  	    if (schema == NULL) {
 87  		xmlSchemaParserCtxtPtr ctxt;
 88  
 89                  schemaPath = argv[i];
 90  
 91                  xmlGetWarningsDefaultValue = 1;
 92  
 93  #ifdef HAVE_MMAP
 94  		if (memory) {
 95  		    int fd;
 96  		    struct stat info;
 97  		    const char *base;
 98  		    if (stat(argv[i], &info) < 0)
 99  			break;
100  		    if ((fd = open(argv[i], O_RDONLY)) < 0)
101  			break;
102  		    base = mmap(NULL, info.st_size, PROT_READ,
103  			        MAP_SHARED, fd, 0) ;
104  		    if (base == (void *) MAP_FAILED)
105  			break;
106  
107  		    ctxt = xmlSchemaNewMemParserCtxt((char *)base,info.st_size);
108  
109  		    xmlSchemaSetParserErrors(ctxt,
110  			    (xmlSchemaValidityErrorFunc) fprintf,
111  			    (xmlSchemaValidityWarningFunc) fprintf,
112  			    stderr);
113  		    schema = xmlSchemaParse(ctxt);
114  		    xmlSchemaFreeParserCtxt(ctxt);
115  		    munmap((char *) base, info.st_size);
116  		} else
117  #endif
118  		{
119  		    ctxt = xmlSchemaNewParserCtxt(argv[i]);
120  		    xmlSchemaSetParserErrors(ctxt,
121  			    (xmlSchemaValidityErrorFunc) fprintf,
122  			    (xmlSchemaValidityWarningFunc) fprintf,
123  			    stderr);
124  		    schema = xmlSchemaParse(ctxt);
125  		    xmlSchemaFreeParserCtxt(ctxt);
126  		}
127  #ifdef LIBXML_OUTPUT_ENABLED
128  #ifdef LIBXML_DEBUG_ENABLED
129  		if (debug)
130  		    xmlSchemaDump(stdout, schema);
131  #endif
132  #endif /* LIBXML_OUTPUT_ENABLED */
133                  xmlGetWarningsDefaultValue = 0;
134  
135  		if (schema == NULL)
136  		    goto failed_schemas;
137  #ifdef LIBXML_READER_ENABLED
138              } else if (stream) {
139                  xmlTextReaderPtr reader = NULL;
140                  int ret = 0;
141  #ifdef HAVE_MMAP
142                  int fd = -1;
143                  struct stat info;
144                  const char *base = NULL;
145  
146                  if (memory) {
147                      if (stat(argv[i], &info) < 0)
148                          break;
149                      if ((fd = open(argv[i], O_RDONLY)) < 0)
150                          break;
151                      base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0);
152                      if (base == (void *)MAP_FAILED) {
153                          close(fd);
154                          break;
155                      }
156  
157                      reader = xmlReaderForMemory(base, info.st_size, argv[i], NULL, 0);
158                  } else
159  #endif
160                  {
161                      reader = xmlReaderForFile(argv[i], NULL, 0);
162                  }
163  
164                  ret = xmlTextReaderSchemaValidate(reader, schemaPath);
165                  if (ret < 0)
166                      break;
167  
168                  do {
169                      ret = xmlTextReaderRead(reader);
170                  } while (ret == 1);
171  
172                  ret = xmlTextReaderIsValid(reader);
173                  if (ret == 1) {
174                      fprintf(stdout, "%s validates\n", argv[i]);
175                  } else if (ret == 0) {
176                      fprintf(stdout, "%s fails to validate\n", argv[i]);
177                  } else {
178                      fprintf(stdout, "%s validation generated an internal error\n",
179                             argv[i]);
180                  }
181  
182                  xmlFreeTextReader(reader);
183  #ifdef HAVE_MMAP
184                  if (memory) {
185                      munmap((char *) base, info.st_size);
186                      close(fd);
187                  }
188  #endif
189  #endif
190  	    } else {
191  		xmlDocPtr doc;
192  
193  		doc = xmlReadFile(argv[i],NULL,0);
194  
195  		if (doc == NULL) {
196  		    fprintf(stderr, "Could not parse %s\n", argv[i]);
197  		} else {
198  		    xmlSchemaValidCtxtPtr ctxt;
199  		    int ret;
200  
201  		    ctxt = xmlSchemaNewValidCtxt(schema);
202  		    xmlSchemaSetValidErrors(ctxt,
203  			    (xmlSchemaValidityErrorFunc) fprintf,
204  			    (xmlSchemaValidityWarningFunc) fprintf,
205  			    stderr);
206  		    ret = xmlSchemaValidateDoc(ctxt, doc);
207  		    if (ret == 0) {
208  			fprintf(stdout, "%s validates\n", argv[i]);
209  		    } else if (ret > 0) {
210  			fprintf(stdout, "%s fails to validate\n", argv[i]);
211  		    } else {
212  			fprintf(stdout, "%s validation generated an internal error\n",
213  			       argv[i]);
214  		    }
215  		    xmlSchemaFreeValidCtxt(ctxt);
216  		    xmlFreeDoc(doc);
217  		}
218  	    }
219  	    files ++;
220  	}
221      }
222      if (schema != NULL)
223  	xmlSchemaFree(schema);
224      if (files == 0) {
225  	printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n",
226  	       argv[0]);
227  	printf("\tParse the HTML files and output the result of the parsing\n");
228  #ifdef LIBXML_DEBUG_ENABLED
229  	printf("\t--debug : dump a debug tree of the in-memory document\n");
230  #endif
231  #ifdef HAVE_MMAP
232  	printf("\t--memory : test the schemas in memory parsing\n");
233  #endif
234  	printf("\t--noout : do not print the result\n");
235  #ifdef LIBXML_READER_ENABLED
236  	printf("\t--stream : use the streaming interface to process very large files\n");
237  #endif
238      }
239  failed_schemas:
240      xmlSchemaCleanupTypes();
241      xmlCleanupParser();
242      xmlMemoryDump();
243  
244      return(0);
245  }
246  
247  #else
248  #include <stdio.h>
249  int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
250      printf("%s : Schemas support not compiled in\n", argv[0]);
251      return(0);
252  }
253  #endif /* LIBXML_SCHEMAS_ENABLED */