Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > DoxygentestRelax.c
Go to the documentation of this file.
00001 /* 00002 * testRelax.c : a small tester program for RelaxNG 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/relaxng.h> 00046 00047 #ifdef LIBXML_DEBUG_ENABLED 00048 static int debug = 0; 00049 #endif 00050 static int noout = 0; 00051 static int tree = 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 xmlRelaxNGPtr 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 } else 00076 if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) { 00077 tree++; 00078 } 00079 } 00080 xmlLineNumbersDefault(1); 00081 xmlSubstituteEntitiesDefault(1); 00082 for (i = 1; i < argc ; i++) { 00083 if (argv[i][0] != '-') { 00084 if (schema == NULL) { 00085 xmlRelaxNGParserCtxtPtr ctxt; 00086 00087 #ifdef HAVE_SYS_MMAN_H 00088 if (memory) { 00089 int fd; 00090 struct stat info; 00091 const char *base; 00092 if (stat(argv[i], &info) < 0) 00093 break; 00094 if ((fd = open(argv[i], O_RDONLY)) < 0) 00095 break; 00096 base = mmap(NULL, info.st_size, PROT_READ, 00097 MAP_SHARED, fd, 0) ; 00098 if (base == (void *) MAP_FAILED) 00099 break; 00100 00101 ctxt = xmlRelaxNGNewMemParserCtxt((char *)base,info.st_size); 00102 00103 xmlRelaxNGSetParserErrors(ctxt, 00104 (xmlRelaxNGValidityErrorFunc) fprintf, 00105 (xmlRelaxNGValidityWarningFunc) fprintf, 00106 stderr); 00107 schema = xmlRelaxNGParse(ctxt); 00108 xmlRelaxNGFreeParserCtxt(ctxt); 00109 munmap((char *) base, info.st_size); 00110 } else 00111 #endif 00112 { 00113 ctxt = xmlRelaxNGNewParserCtxt(argv[i]); 00114 xmlRelaxNGSetParserErrors(ctxt, 00115 (xmlRelaxNGValidityErrorFunc) fprintf, 00116 (xmlRelaxNGValidityWarningFunc) fprintf, 00117 stderr); 00118 schema = xmlRelaxNGParse(ctxt); 00119 xmlRelaxNGFreeParserCtxt(ctxt); 00120 } 00121 if (schema == NULL) { 00122 printf("Relax-NG schema %s failed to compile\n", argv[i]); 00123 files = -1; 00124 break; 00125 } 00126 #ifdef LIBXML_OUTPUT_ENABLED 00127 #ifdef LIBXML_DEBUG_ENABLED 00128 if (debug) 00129 xmlRelaxNGDump(stdout, schema); 00130 #endif 00131 if (tree) 00132 xmlRelaxNGDumpTree(stdout, schema); 00133 #endif /* LIBXML_OUTPUT_ENABLED */ 00134 } else { 00135 xmlDocPtr doc; 00136 00137 doc = xmlReadFile(argv[i],NULL,0); 00138 00139 if (doc == NULL) { 00140 fprintf(stderr, "Could not parse %s\n", argv[i]); 00141 } else { 00142 xmlRelaxNGValidCtxtPtr ctxt; 00143 int ret; 00144 00145 ctxt = xmlRelaxNGNewValidCtxt(schema); 00146 xmlRelaxNGSetValidErrors(ctxt, 00147 (xmlRelaxNGValidityErrorFunc) fprintf, 00148 (xmlRelaxNGValidityWarningFunc) fprintf, 00149 stderr); 00150 ret = xmlRelaxNGValidateDoc(ctxt, doc); 00151 if (ret == 0) { 00152 printf("%s validates\n", argv[i]); 00153 } else if (ret > 0) { 00154 printf("%s fails to validate\n", argv[i]); 00155 } else { 00156 printf("%s validation generated an internal error\n", 00157 argv[i]); 00158 } 00159 xmlRelaxNGFreeValidCtxt(ctxt); 00160 xmlFreeDoc(doc); 00161 } 00162 } 00163 files ++; 00164 } 00165 } 00166 if (schema != NULL) 00167 xmlRelaxNGFree(schema); 00168 if (files == 0) { 00169 printf("Usage : %s [--debug] [--noout] schemas XMLfiles ...\n", 00170 argv[0]); 00171 printf("\tParse the HTML files and output the result of the parsing\n"); 00172 #ifdef LIBXML_DEBUG_ENABLED 00173 printf("\t--debug : dump a debug tree of the in-memory document\n"); 00174 #endif 00175 printf("\t--noout : do not print the result\n"); 00176 printf("\t--tree : print the intermediate Relax-NG document tree\n"); 00177 #ifdef HAVE_SYS_MMAN_H 00178 printf("\t--memory : test the schemas in memory parsing\n"); 00179 #endif 00180 } 00181 xmlRelaxNGCleanupTypes(); 00182 xmlCleanupParser(); 00183 xmlMemoryDump(); 00184 00185 return(0); 00186 } 00187 00188 #else 00189 #include <stdio.h> 00190 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { 00191 printf("%s : RelaxNG support not compiled in\n", argv[0]); 00192 return(0); 00193 } 00194 #endif /* LIBXML_SCHEMAS_ENABLED */ Generated on Fri May 25 2012 04:33:10 for ReactOS by
1.7.6.1
|