Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > DoxygentestXPath.c
Go to the documentation of this file.
00001 /* 00002 * testXPath.c : a small tester program for XPath. 00003 * 00004 * See Copyright for the status of this software. 00005 * 00006 * daniel@veillard.com 00007 */ 00008 00009 #include "libxml.h" 00010 #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED) 00011 00012 #include <string.h> 00013 00014 #ifdef HAVE_SYS_TYPES_H 00015 #include <sys/types.h> 00016 #endif 00017 #ifdef HAVE_SYS_STAT_H 00018 #include <sys/stat.h> 00019 #endif 00020 #ifdef HAVE_FCNTL_H 00021 #include <fcntl.h> 00022 #endif 00023 #ifdef HAVE_UNISTD_H 00024 #include <unistd.h> 00025 #endif 00026 #ifdef HAVE_STDLIB_H 00027 #include <stdlib.h> 00028 #endif 00029 00030 00031 #include <libxml/xpath.h> 00032 #include <libxml/tree.h> 00033 #include <libxml/parser.h> 00034 #include <libxml/debugXML.h> 00035 #include <libxml/xmlmemory.h> 00036 #include <libxml/parserInternals.h> 00037 #include <libxml/xpathInternals.h> 00038 #include <libxml/xmlerror.h> 00039 #include <libxml/globals.h> 00040 #if defined(LIBXML_XPTR_ENABLED) 00041 #include <libxml/xpointer.h> 00042 static int xptr = 0; 00043 #endif 00044 static int debug = 0; 00045 static int valid = 0; 00046 static int expr = 0; 00047 static int tree = 0; 00048 static int nocdata = 0; 00049 static xmlDocPtr document = NULL; 00050 00051 /* 00052 * Default document 00053 */ 00054 static xmlChar buffer[] = 00055 "<?xml version=\"1.0\"?>\n\ 00056 <EXAMPLE prop1=\"gnome is great\" prop2=\"& linux too\">\n\ 00057 <head>\n\ 00058 <title>Welcome to Gnome</title>\n\ 00059 </head>\n\ 00060 <chapter>\n\ 00061 <title>The Linux adventure</title>\n\ 00062 <p>bla bla bla ...</p>\n\ 00063 <image href=\"linus.gif\"/>\n\ 00064 <p>...</p>\n\ 00065 </chapter>\n\ 00066 <chapter>\n\ 00067 <title>Chapter 2</title>\n\ 00068 <p>this is chapter 2 ...</p>\n\ 00069 </chapter>\n\ 00070 <chapter>\n\ 00071 <title>Chapter 3</title>\n\ 00072 <p>this is chapter 3 ...</p>\n\ 00073 </chapter>\n\ 00074 </EXAMPLE>\n\ 00075 "; 00076 00077 00078 static void 00079 testXPath(const char *str) { 00080 xmlXPathObjectPtr res; 00081 xmlXPathContextPtr ctxt; 00082 00083 #if defined(LIBXML_XPTR_ENABLED) 00084 if (xptr) { 00085 ctxt = xmlXPtrNewContext(document, NULL, NULL); 00086 res = xmlXPtrEval(BAD_CAST str, ctxt); 00087 } else { 00088 #endif 00089 ctxt = xmlXPathNewContext(document); 00090 ctxt->node = xmlDocGetRootElement(document); 00091 if (expr) 00092 res = xmlXPathEvalExpression(BAD_CAST str, ctxt); 00093 else { 00094 /* res = xmlXPathEval(BAD_CAST str, ctxt); */ 00095 xmlXPathCompExprPtr comp; 00096 00097 comp = xmlXPathCompile(BAD_CAST str); 00098 if (comp != NULL) { 00099 if (tree) 00100 xmlXPathDebugDumpCompExpr(stdout, comp, 0); 00101 00102 res = xmlXPathCompiledEval(comp, ctxt); 00103 xmlXPathFreeCompExpr(comp); 00104 } else 00105 res = NULL; 00106 } 00107 #if defined(LIBXML_XPTR_ENABLED) 00108 } 00109 #endif 00110 xmlXPathDebugDumpObject(stdout, res, 0); 00111 xmlXPathFreeObject(res); 00112 xmlXPathFreeContext(ctxt); 00113 } 00114 00115 static void 00116 testXPathFile(const char *filename) { 00117 FILE *input; 00118 char expression[5000]; 00119 int len; 00120 00121 input = fopen(filename, "r"); 00122 if (input == NULL) { 00123 xmlGenericError(xmlGenericErrorContext, 00124 "Cannot open %s for reading\n", filename); 00125 return; 00126 } 00127 while (fgets(expression, 4500, input) != NULL) { 00128 len = strlen(expression); 00129 len--; 00130 while ((len >= 0) && 00131 ((expression[len] == '\n') || (expression[len] == '\t') || 00132 (expression[len] == '\r') || (expression[len] == ' '))) len--; 00133 expression[len + 1] = 0; 00134 if (len >= 0) { 00135 printf("\n========================\nExpression: %s\n", expression) ; 00136 testXPath(expression); 00137 } 00138 } 00139 00140 fclose(input); 00141 } 00142 00143 int main(int argc, char **argv) { 00144 int i; 00145 int strings = 0; 00146 int usefile = 0; 00147 char *filename = NULL; 00148 00149 for (i = 1; i < argc ; i++) { 00150 #if defined(LIBXML_XPTR_ENABLED) 00151 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr"))) 00152 xptr++; 00153 else 00154 #endif 00155 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) 00156 debug++; 00157 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid"))) 00158 valid++; 00159 else if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr"))) 00160 expr++; 00161 else if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) 00162 tree++; 00163 else if ((!strcmp(argv[i], "-nocdata")) || 00164 (!strcmp(argv[i], "--nocdata"))) 00165 nocdata++; 00166 else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) 00167 filename = argv[++i]; 00168 else if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file"))) 00169 usefile++; 00170 } 00171 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; 00172 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; 00173 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS; 00174 xmlSubstituteEntitiesDefaultValue = 1; 00175 if (nocdata != 0) { 00176 xmlDefaultSAXHandlerInit(); 00177 xmlDefaultSAXHandler.cdataBlock = NULL; 00178 } 00179 if (document == NULL) { 00180 if (filename == NULL) 00181 document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT); 00182 else 00183 document = xmlReadFile(filename,NULL,XML_PARSE_COMPACT); 00184 } 00185 for (i = 1; i < argc ; i++) { 00186 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) { 00187 i++; continue; 00188 } 00189 if (argv[i][0] != '-') { 00190 if (usefile) 00191 testXPathFile(argv[i]); 00192 else 00193 testXPath(argv[i]); 00194 strings ++; 00195 } 00196 } 00197 if (strings == 0) { 00198 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n", 00199 argv[0]); 00200 printf("\tParse the XPath strings and output the result of the parsing\n"); 00201 printf("\t--debug : dump a debug version of the result\n"); 00202 printf("\t--valid : switch on DTD support in the parser\n"); 00203 #if defined(LIBXML_XPTR_ENABLED) 00204 printf("\t--xptr : expressions are XPointer expressions\n"); 00205 #endif 00206 printf("\t--expr : debug XPath expressions only\n"); 00207 printf("\t--tree : show the compiled XPath tree\n"); 00208 printf("\t--nocdata : do not generate CDATA nodes\n"); 00209 printf("\t--input filename : or\n"); 00210 printf("\t-i filename : read the document from filename\n"); 00211 printf("\t--file : or\n"); 00212 printf("\t-f : read queries from files, args\n"); 00213 } 00214 if (document != NULL) 00215 xmlFreeDoc(document); 00216 xmlCleanupParser(); 00217 xmlMemoryDump(); 00218 00219 return(0); 00220 } 00221 #else 00222 #include <stdio.h> 00223 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { 00224 printf("%s : XPath/Debug support not compiled in\n", argv[0]); 00225 return(0); 00226 } 00227 #endif /* LIBXML_XPATH_ENABLED */ Generated on Sat May 26 2012 04:33:33 for ReactOS by
1.7.6.1
|