Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > DoxygentestSchemas.c
Go to the documentation of this file.
00001 /* 00002 * testSchemas.c : a small tester program for Schema validation 00003 * 00004 * See Copyright for the status of this software. 00005 * 00006 * Daniel.Veillard@w3.org 00007 */ 00008 00009 #include "libxml.h" 00010 #ifdef LIBXML_SCHEMAS_ENABLED 00011 00012 #include <libxml/xmlversion.h> 00013 #include <libxml/parser.h> 00014 00015 #include <stdio.h> 00016 #include <string.h> 00017 #include <stdarg.h> 00018 00019 00020 #ifdef HAVE_SYS_TYPES_H 00021 #include <sys/types.h> 00022 #endif 00023 #ifdef HAVE_SYS_STAT_H 00024 #include <sys/stat.h> 00025 #endif 00026 #ifdef HAVE_FCNTL_H 00027 #include <fcntl.h> 00028 #endif 00029 #ifdef HAVE_UNISTD_H 00030 #include <unistd.h> 00031 #endif 00032 #ifdef HAVE_STDLIB_H 00033 #include <stdlib.h> 00034 #endif 00035 #ifdef HAVE_SYS_MMAN_H 00036 #include <sys/mman.h> 00037 /* seems needed for Solaris */ 00038 #ifndef MAP_FAILED 00039 #define MAP_FAILED ((void *) -1) 00040 #endif 00041 #endif 00042 00043 #include <libxml/xmlmemory.h> 00044 #include <libxml/debugXML.h> 00045 #include <libxml/xmlschemas.h> 00046 #include <libxml/xmlschemastypes.h> 00047 00048 #ifdef LIBXML_DEBUG_ENABLED 00049 static int debug = 0; 00050 #endif 00051 static int noout = 0; 00052 #ifdef HAVE_SYS_MMAN_H 00053 static int memory = 0; 00054 #endif 00055 00056 00057 int main(int argc, char **argv) { 00058 int i; 00059 int files = 0; 00060 xmlSchemaPtr schema = NULL; 00061 00062 for (i = 1; i < argc ; i++) { 00063 #ifdef LIBXML_DEBUG_ENABLED 00064 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) 00065 debug++; 00066 else 00067 #endif 00068 #ifdef HAVE_SYS_MMAN_H 00069 if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) { 00070 memory++; 00071 } else 00072 #endif 00073 if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout"))) { 00074 noout++; 00075 } 00076 } 00077 xmlLineNumbersDefault(1); 00078 for (i = 1; i < argc ; i++) { 00079 if (argv[i][0] != '-') { 00080 if (schema == NULL) { 00081 xmlSchemaParserCtxtPtr ctxt; 00082 00083 #ifdef HAVE_SYS_MMAN_H 00084 if (memory) { 00085 int fd; 00086 struct stat info; 00087 const char *base; 00088 if (stat(argv[i], &info) < 0) 00089 break; 00090 if ((fd = open(argv[i], O_RDONLY)) < 0) 00091 break; 00092 base = mmap(NULL, info.st_size, PROT_READ, 00093 MAP_SHARED, fd, 0) ; 00094 if (base == (void *) MAP_FAILED) 00095 break; 00096 00097 ctxt = xmlSchemaNewMemParserCtxt((char *)base,info.st_size); 00098 00099 xmlSchemaSetParserErrors(ctxt, 00100 (xmlSchemaValidityErrorFunc) fprintf, 00101 (xmlSchemaValidityWarningFunc) fprintf, 00102 stderr); 00103 schema = xmlSchemaParse(ctxt); 00104 xmlSchemaFreeParserCtxt(ctxt); 00105 munmap((char *) base, info.st_size); 00106 } else 00107 #endif 00108 { 00109 ctxt = xmlSchemaNewParserCtxt(argv[i]); 00110 xmlSchemaSetParserErrors(ctxt, 00111 (xmlSchemaValidityErrorFunc) fprintf, 00112 (xmlSchemaValidityWarningFunc) fprintf, 00113 stderr); 00114 schema = xmlSchemaParse(ctxt); 00115 xmlSchemaFreeParserCtxt(ctxt); 00116 } 00117 #ifdef LIBXML_OUTPUT_ENABLED 00118 #ifdef LIBXML_DEBUG_ENABLED 00119 if (debug) 00120 xmlSchemaDump(stdout, schema); 00121 #endif 00122 #endif /* LIBXML_OUTPUT_ENABLED */ 00123 if (schema == NULL) 00124 goto failed_schemas; 00125 } else { 00126 xmlDocPtr doc; 00127 00128 doc = xmlReadFile(argv[i],NULL,0); 00129 00130 if (doc == NULL) { 00131 fprintf(stderr, "Could not parse %s\n", argv[i]); 00132 } else { 00133 xmlSchemaValidCtxtPtr ctxt; 00134 int ret; 00135 00136 ctxt = xmlSchemaNewValidCtxt(schema); 00137 xmlSchemaSetValidErrors(ctxt, 00138 (xmlSchemaValidityErrorFunc) fprintf, 00139 (xmlSchemaValidityWarningFunc) fprintf, 00140 stderr); 00141 ret = xmlSchemaValidateDoc(ctxt, doc); 00142 if (ret == 0) { 00143 printf("%s validates\n", argv[i]); 00144 } else if (ret > 0) { 00145 printf("%s fails to validate\n", argv[i]); 00146 } else { 00147 printf("%s validation generated an internal error\n", 00148 argv[i]); 00149 } 00150 xmlSchemaFreeValidCtxt(ctxt); 00151 xmlFreeDoc(doc); 00152 } 00153 } 00154 files ++; 00155 } 00156 } 00157 if (schema != NULL) 00158 xmlSchemaFree(schema); 00159 if (files == 0) { 00160 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n", 00161 argv[0]); 00162 printf("\tParse the HTML files and output the result of the parsing\n"); 00163 #ifdef LIBXML_DEBUG_ENABLED 00164 printf("\t--debug : dump a debug tree of the in-memory document\n"); 00165 #endif 00166 printf("\t--noout : do not print the result\n"); 00167 #ifdef HAVE_SYS_MMAN_H 00168 printf("\t--memory : test the schemas in memory parsing\n"); 00169 #endif 00170 } 00171 failed_schemas: 00172 xmlSchemaCleanupTypes(); 00173 xmlCleanupParser(); 00174 xmlMemoryDump(); 00175 00176 return(0); 00177 } 00178 00179 #else 00180 #include <stdio.h> 00181 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { 00182 printf("%s : Schemas support not compiled in\n", argv[0]); 00183 return(0); 00184 } 00185 #endif /* LIBXML_SCHEMAS_ENABLED */ Generated on Sun May 27 2012 04:34:42 for ReactOS by
1.7.6.1
|