OLD | NEW |
1 /* | 1 /* |
2 * testXPath.c : a small tester program for XPath. | 2 * testXPath.c : a small tester program for XPath. |
3 * | 3 * |
4 * See Copyright for the status of this software. | 4 * See Copyright for the status of this software. |
5 * | 5 * |
6 * daniel@veillard.com | 6 * daniel@veillard.com |
7 */ | 7 */ |
8 | 8 |
9 #include "libxml.h" | 9 #include "libxml.h" |
10 #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED) | 10 #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 static int debug = 0; | 44 static int debug = 0; |
45 static int valid = 0; | 45 static int valid = 0; |
46 static int expr = 0; | 46 static int expr = 0; |
47 static int tree = 0; | 47 static int tree = 0; |
48 static int nocdata = 0; | 48 static int nocdata = 0; |
49 static xmlDocPtr document = NULL; | 49 static xmlDocPtr document = NULL; |
50 | 50 |
51 /* | 51 /* |
52 * Default document | 52 * Default document |
53 */ | 53 */ |
54 static xmlChar buffer[] = | 54 static xmlChar buffer[] = |
55 "<?xml version=\"1.0\"?>\n\ | 55 "<?xml version=\"1.0\"?>\n\ |
56 <EXAMPLE prop1=\"gnome is great\" prop2=\"& linux too\">\n\ | 56 <EXAMPLE prop1=\"gnome is great\" prop2=\"& linux too\">\n\ |
57 <head>\n\ | 57 <head>\n\ |
58 <title>Welcome to Gnome</title>\n\ | 58 <title>Welcome to Gnome</title>\n\ |
59 </head>\n\ | 59 </head>\n\ |
60 <chapter>\n\ | 60 <chapter>\n\ |
61 <title>The Linux adventure</title>\n\ | 61 <title>The Linux adventure</title>\n\ |
62 <p>bla bla bla ...</p>\n\ | 62 <p>bla bla bla ...</p>\n\ |
63 <image href=\"linus.gif\"/>\n\ | 63 <image href=\"linus.gif\"/>\n\ |
64 <p>...</p>\n\ | 64 <p>...</p>\n\ |
65 </chapter>\n\ | 65 </chapter>\n\ |
66 <chapter>\n\ | 66 <chapter>\n\ |
67 <title>Chapter 2</title>\n\ | 67 <title>Chapter 2</title>\n\ |
68 <p>this is chapter 2 ...</p>\n\ | 68 <p>this is chapter 2 ...</p>\n\ |
69 </chapter>\n\ | 69 </chapter>\n\ |
70 <chapter>\n\ | 70 <chapter>\n\ |
71 <title>Chapter 3</title>\n\ | 71 <title>Chapter 3</title>\n\ |
72 <p>this is chapter 3 ...</p>\n\ | 72 <p>this is chapter 3 ...</p>\n\ |
73 </chapter>\n\ | 73 </chapter>\n\ |
74 </EXAMPLE>\n\ | 74 </EXAMPLE>\n\ |
75 "; | 75 "; |
76 | 76 |
77 | 77 |
78 static void | 78 static void |
79 testXPath(const char *str) { | 79 testXPath(const char *str) { |
80 xmlXPathObjectPtr res; | 80 xmlXPathObjectPtr res; |
81 xmlXPathContextPtr ctxt; | 81 xmlXPathContextPtr ctxt; |
82 | 82 |
83 #if defined(LIBXML_XPTR_ENABLED) | 83 #if defined(LIBXML_XPTR_ENABLED) |
84 if (xptr) { | 84 if (xptr) { |
85 ctxt = xmlXPtrNewContext(document, NULL, NULL); | 85 ctxt = xmlXPtrNewContext(document, NULL, NULL); |
86 res = xmlXPtrEval(BAD_CAST str, ctxt); | 86 res = xmlXPtrEval(BAD_CAST str, ctxt); |
87 } else { | 87 } else { |
88 #endif | 88 #endif |
89 ctxt = xmlXPathNewContext(document); | 89 ctxt = xmlXPathNewContext(document); |
90 ctxt->node = xmlDocGetRootElement(document); | 90 ctxt->node = xmlDocGetRootElement(document); |
91 if (expr) | 91 if (expr) |
92 res = xmlXPathEvalExpression(BAD_CAST str, ctxt); | 92 res = xmlXPathEvalExpression(BAD_CAST str, ctxt); |
93 else { | 93 else { |
94 /* res = xmlXPathEval(BAD_CAST str, ctxt); */ | 94 /* res = xmlXPathEval(BAD_CAST str, ctxt); */ |
95 xmlXPathCompExprPtr comp; | 95 xmlXPathCompExprPtr comp; |
96 | 96 |
97 comp = xmlXPathCompile(BAD_CAST str); | 97 comp = xmlXPathCompile(BAD_CAST str); |
98 if (comp != NULL) { | 98 if (comp != NULL) { |
99 » » if (tree) | 99 » » if (tree) |
100 xmlXPathDebugDumpCompExpr(stdout, comp, 0); | 100 xmlXPathDebugDumpCompExpr(stdout, comp, 0); |
101 | 101 |
102 res = xmlXPathCompiledEval(comp, ctxt); | 102 res = xmlXPathCompiledEval(comp, ctxt); |
103 xmlXPathFreeCompExpr(comp); | 103 xmlXPathFreeCompExpr(comp); |
104 } else | 104 } else |
105 res = NULL; | 105 res = NULL; |
106 } | 106 } |
107 #if defined(LIBXML_XPTR_ENABLED) | 107 #if defined(LIBXML_XPTR_ENABLED) |
108 } | 108 } |
109 #endif | 109 #endif |
(...skipping 10 matching lines...) Expand all Loading... |
120 | 120 |
121 input = fopen(filename, "r"); | 121 input = fopen(filename, "r"); |
122 if (input == NULL) { | 122 if (input == NULL) { |
123 xmlGenericError(xmlGenericErrorContext, | 123 xmlGenericError(xmlGenericErrorContext, |
124 "Cannot open %s for reading\n", filename); | 124 "Cannot open %s for reading\n", filename); |
125 return; | 125 return; |
126 } | 126 } |
127 while (fgets(expression, 4500, input) != NULL) { | 127 while (fgets(expression, 4500, input) != NULL) { |
128 len = strlen(expression); | 128 len = strlen(expression); |
129 len--; | 129 len--; |
130 » while ((len >= 0) && | 130 » while ((len >= 0) && |
131 ((expression[len] == '\n') || (expression[len] == '\t') || | 131 ((expression[len] == '\n') || (expression[len] == '\t') || |
132 (expression[len] == '\r') || (expression[len] == ' '))) len--; | 132 (expression[len] == '\r') || (expression[len] == ' '))) len--; |
133 » expression[len + 1] = 0; | 133 » expression[len + 1] = 0; |
134 if (len >= 0) { | 134 if (len >= 0) { |
135 printf("\n========================\nExpression: %s\n", expression) ; | 135 printf("\n========================\nExpression: %s\n", expression) ; |
136 testXPath(expression); | 136 testXPath(expression); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 fclose(input); | 140 fclose(input); |
141 } | 141 } |
142 | 142 |
143 int main(int argc, char **argv) { | 143 int main(int argc, char **argv) { |
144 int i; | 144 int i; |
145 int strings = 0; | 145 int strings = 0; |
146 int usefile = 0; | 146 int usefile = 0; |
147 char *filename = NULL; | 147 char *filename = NULL; |
148 | 148 |
149 for (i = 1; i < argc ; i++) { | 149 for (i = 1; i < argc ; i++) { |
150 #if defined(LIBXML_XPTR_ENABLED) | 150 #if defined(LIBXML_XPTR_ENABLED) |
151 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr"))) | 151 if ((!strcmp(argv[i], "-xptr")) || (!strcmp(argv[i], "--xptr"))) |
152 xptr++; | 152 xptr++; |
153 » else | 153 » else |
154 #endif | 154 #endif |
155 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) | 155 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
156 debug++; | 156 debug++; |
157 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid"))) | 157 else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid"))) |
158 valid++; | 158 valid++; |
159 else if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr"))) | 159 else if ((!strcmp(argv[i], "-expr")) || (!strcmp(argv[i], "--expr"))) |
160 expr++; | 160 expr++; |
161 else if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) | 161 else if ((!strcmp(argv[i], "-tree")) || (!strcmp(argv[i], "--tree"))) |
162 tree++; | 162 tree++; |
163 else if ((!strcmp(argv[i], "-nocdata")) || | 163 else if ((!strcmp(argv[i], "-nocdata")) || |
164 (!strcmp(argv[i], "--nocdata"))) | 164 (!strcmp(argv[i], "--nocdata"))) |
165 nocdata++; | 165 nocdata++; |
166 else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) | 166 else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) |
167 filename = argv[++i]; | 167 filename = argv[++i]; |
168 else if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file"))) | 168 else if ((!strcmp(argv[i], "-f")) || (!strcmp(argv[i], "--file"))) |
169 usefile++; | 169 usefile++; |
170 } | 170 } |
171 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; | 171 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
172 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; | 172 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; |
173 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS; | 173 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS; |
174 xmlSubstituteEntitiesDefaultValue = 1; | 174 xmlSubstituteEntitiesDefaultValue = 1; |
| 175 #ifdef LIBXML_SAX1_ENABLED |
175 if (nocdata != 0) { | 176 if (nocdata != 0) { |
176 xmlDefaultSAXHandlerInit(); | 177 xmlDefaultSAXHandlerInit(); |
177 xmlDefaultSAXHandler.cdataBlock = NULL; | 178 xmlDefaultSAXHandler.cdataBlock = NULL; |
178 } | 179 } |
| 180 #endif |
179 if (document == NULL) { | 181 if (document == NULL) { |
180 if (filename == NULL) | 182 if (filename == NULL) |
181 document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT); | 183 document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT); |
182 else | 184 else |
183 document = xmlReadFile(filename,NULL,XML_PARSE_COMPACT); | 185 document = xmlReadFile(filename,NULL,XML_PARSE_COMPACT); |
184 } | 186 } |
185 for (i = 1; i < argc ; i++) { | 187 for (i = 1; i < argc ; i++) { |
186 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) { | 188 if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "--input"))) { |
187 i++; continue; | 189 i++; continue; |
188 } | 190 } |
(...skipping 15 matching lines...) Expand all Loading... |
204 printf("\t--xptr : expressions are XPointer expressions\n"); | 206 printf("\t--xptr : expressions are XPointer expressions\n"); |
205 #endif | 207 #endif |
206 printf("\t--expr : debug XPath expressions only\n"); | 208 printf("\t--expr : debug XPath expressions only\n"); |
207 printf("\t--tree : show the compiled XPath tree\n"); | 209 printf("\t--tree : show the compiled XPath tree\n"); |
208 printf("\t--nocdata : do not generate CDATA nodes\n"); | 210 printf("\t--nocdata : do not generate CDATA nodes\n"); |
209 printf("\t--input filename : or\n"); | 211 printf("\t--input filename : or\n"); |
210 printf("\t-i filename : read the document from filename\n"); | 212 printf("\t-i filename : read the document from filename\n"); |
211 printf("\t--file : or\n"); | 213 printf("\t--file : or\n"); |
212 printf("\t-f : read queries from files, args\n"); | 214 printf("\t-f : read queries from files, args\n"); |
213 } | 215 } |
214 if (document != NULL) | 216 if (document != NULL) |
215 xmlFreeDoc(document); | 217 xmlFreeDoc(document); |
216 xmlCleanupParser(); | 218 xmlCleanupParser(); |
217 xmlMemoryDump(); | 219 xmlMemoryDump(); |
218 | 220 |
219 return(0); | 221 return(0); |
220 } | 222 } |
221 #else | 223 #else |
222 #include <stdio.h> | 224 #include <stdio.h> |
223 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { | 225 int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { |
224 printf("%s : XPath/Debug support not compiled in\n", argv[0]); | 226 printf("%s : XPath/Debug support not compiled in\n", argv[0]); |
225 return(0); | 227 return(0); |
226 } | 228 } |
227 #endif /* LIBXML_XPATH_ENABLED */ | 229 #endif /* LIBXML_XPATH_ENABLED */ |
OLD | NEW |