Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * xmlreader.c: implements the xmlTextReader streaming node API |
3 | | * |
4 | | * NOTE: |
5 | | * XmlTextReader.Normalization Property won't be supported, since |
6 | | * it makes the parser non compliant to the XML recommendation |
7 | | * |
8 | | * See Copyright for the status of this software. |
9 | | * |
10 | | * Author: Daniel Veillard |
11 | | */ |
12 | | |
13 | | /* |
14 | | * TODOs: |
15 | | * - XML Schemas validation |
16 | | */ |
17 | | #define IN_LIBXML |
18 | | #include "libxml.h" |
19 | | |
20 | | #ifdef LIBXML_READER_ENABLED |
21 | | #include <string.h> /* for memset() only ! */ |
22 | | #include <stdarg.h> |
23 | | #include <ctype.h> |
24 | | #include <stdlib.h> |
25 | | |
26 | | #include <libxml/xmlmemory.h> |
27 | | #include <libxml/xmlIO.h> |
28 | | #include <libxml/xmlreader.h> |
29 | | #include <libxml/parserInternals.h> |
30 | | #ifdef LIBXML_RELAXNG_ENABLED |
31 | | #include <libxml/relaxng.h> |
32 | | #endif |
33 | | #ifdef LIBXML_SCHEMAS_ENABLED |
34 | | #include <libxml/xmlschemas.h> |
35 | | #endif |
36 | | #include <libxml/uri.h> |
37 | | #ifdef LIBXML_XINCLUDE_ENABLED |
38 | | #include <libxml/xinclude.h> |
39 | | #endif |
40 | | #ifdef LIBXML_PATTERN_ENABLED |
41 | | #include <libxml/pattern.h> |
42 | | #endif |
43 | | |
44 | | #include "private/buf.h" |
45 | | #include "private/error.h" |
46 | | #include "private/io.h" |
47 | | #include "private/memory.h" |
48 | | #include "private/parser.h" |
49 | | #include "private/tree.h" |
50 | | #ifdef LIBXML_XINCLUDE_ENABLED |
51 | | #include "private/xinclude.h" |
52 | | #endif |
53 | | |
54 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
55 | | /* Keeping free objects can hide memory errors. */ |
56 | 0 | #define MAX_FREE_NODES 1 |
57 | | #else |
58 | | #define MAX_FREE_NODES 100 |
59 | | #endif |
60 | | |
61 | | #ifndef va_copy |
62 | | #ifdef __va_copy |
63 | | #define va_copy(dest, src) __va_copy(dest, src) |
64 | | #else |
65 | | #define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list)) |
66 | | #endif |
67 | | #endif |
68 | | |
69 | 0 | #define CHUNK_SIZE 512 |
70 | | /************************************************************************ |
71 | | * * |
72 | | * The parser: maps the Text Reader API on top of the existing * |
73 | | * parsing routines building a tree * |
74 | | * * |
75 | | ************************************************************************/ |
76 | | |
77 | 0 | #define XML_TEXTREADER_INPUT 1 |
78 | 0 | #define XML_TEXTREADER_CTXT 2 |
79 | | |
80 | | typedef enum { |
81 | | XML_TEXTREADER_NONE = -1, |
82 | | XML_TEXTREADER_START= 0, |
83 | | XML_TEXTREADER_ELEMENT= 1, |
84 | | XML_TEXTREADER_END= 2, |
85 | | XML_TEXTREADER_EMPTY= 3, |
86 | | XML_TEXTREADER_BACKTRACK= 4, |
87 | | XML_TEXTREADER_DONE= 5, |
88 | | XML_TEXTREADER_ERROR= 6 |
89 | | } xmlTextReaderState; |
90 | | |
91 | | typedef enum { |
92 | | XML_TEXTREADER_NOT_VALIDATE = 0, |
93 | | XML_TEXTREADER_VALIDATE_DTD = 1, |
94 | | XML_TEXTREADER_VALIDATE_RNG = 2, |
95 | | XML_TEXTREADER_VALIDATE_XSD = 4 |
96 | | } xmlTextReaderValidate; |
97 | | |
98 | | struct _xmlTextReader { |
99 | | int mode; /* the parsing mode */ |
100 | | xmlDocPtr doc; /* when walking an existing doc */ |
101 | | xmlTextReaderValidate validate;/* is there any validation */ |
102 | | int allocs; /* what structure were deallocated */ |
103 | | xmlTextReaderState state; |
104 | | xmlParserCtxtPtr ctxt; /* the parser context */ |
105 | | xmlSAXHandlerPtr sax; /* the parser SAX callbacks */ |
106 | | xmlParserInputBufferPtr input; /* the input */ |
107 | | startElementSAXFunc startElement;/* initial SAX callbacks */ |
108 | | endElementSAXFunc endElement; /* idem */ |
109 | | startElementNsSAX2Func startElementNs;/* idem */ |
110 | | endElementNsSAX2Func endElementNs; /* idem */ |
111 | | charactersSAXFunc characters; |
112 | | cdataBlockSAXFunc cdataBlock; |
113 | | unsigned int base; /* base of the segment in the input */ |
114 | | unsigned int cur; /* current position in the input */ |
115 | | xmlNodePtr node; /* current node */ |
116 | | xmlNodePtr curnode;/* current attribute node */ |
117 | | int depth; /* depth of the current node */ |
118 | | xmlNodePtr faketext;/* fake xmlNs chld */ |
119 | | int preserve;/* preserve the resulting document */ |
120 | | xmlBufPtr buffer; /* used to return const xmlChar * */ |
121 | | xmlDictPtr dict; /* the context dictionary */ |
122 | | |
123 | | /* entity stack when traversing entities content */ |
124 | | xmlNodePtr ent; /* Current Entity Ref Node */ |
125 | | int entNr; /* Depth of the entities stack */ |
126 | | int entMax; /* Max depth of the entities stack */ |
127 | | xmlNodePtr *entTab; /* array of entities */ |
128 | | |
129 | | /* error handling */ |
130 | | xmlTextReaderErrorFunc errorFunc; /* callback function */ |
131 | | void *errorFuncArg; /* callback function user argument */ |
132 | | |
133 | | #ifdef LIBXML_RELAXNG_ENABLED |
134 | | /* Handling of RelaxNG validation */ |
135 | | xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */ |
136 | | xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */ |
137 | | int rngPreserveCtxt; /* 1 if the context was provided by the user */ |
138 | | int rngValidErrors;/* The number of errors detected */ |
139 | | xmlNodePtr rngFullNode; /* the node if RNG not progressive */ |
140 | | #endif |
141 | | #ifdef LIBXML_SCHEMAS_ENABLED |
142 | | /* Handling of Schemas validation */ |
143 | | xmlSchemaPtr xsdSchemas; /* The Schemas schemas */ |
144 | | xmlSchemaValidCtxtPtr xsdValidCtxt;/* The Schemas validation context */ |
145 | | int xsdPreserveCtxt; /* 1 if the context was provided by the user */ |
146 | | int xsdValidErrors;/* The number of errors detected */ |
147 | | xmlSchemaSAXPlugPtr xsdPlug; /* the schemas plug in SAX pipeline */ |
148 | | #endif |
149 | | #ifdef LIBXML_XINCLUDE_ENABLED |
150 | | /* Handling of XInclude processing */ |
151 | | int xinclude; /* is xinclude asked for */ |
152 | | const xmlChar * xinclude_name; /* the xinclude name from dict */ |
153 | | xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */ |
154 | | int in_xinclude; /* counts for xinclude */ |
155 | | #endif |
156 | | #ifdef LIBXML_PATTERN_ENABLED |
157 | | int patternNr; /* number of preserve patterns */ |
158 | | int patternMax; /* max preserve patterns */ |
159 | | xmlPatternPtr *patternTab; /* array of preserve patterns */ |
160 | | #endif |
161 | | int preserves; /* level of preserves */ |
162 | | int parserFlags; /* the set of options set */ |
163 | | /* Structured error handling */ |
164 | | xmlStructuredErrorFunc sErrorFunc; /* callback function */ |
165 | | |
166 | | xmlResourceLoader resourceLoader; |
167 | | void *resourceCtxt; |
168 | | }; |
169 | | |
170 | 0 | #define NODE_IS_EMPTY 0x1 |
171 | 0 | #define NODE_IS_PRESERVED 0x2 |
172 | 0 | #define NODE_IS_SPRESERVED 0x4 |
173 | | |
174 | | static int xmlTextReaderReadTree(xmlTextReaderPtr reader); |
175 | | static int xmlTextReaderNextTree(xmlTextReaderPtr reader); |
176 | | |
177 | | /** |
178 | | * Free a string if it is not owned by the "dict" dictionary in the |
179 | | * current scope |
180 | | * |
181 | | * @param str a string |
182 | | */ |
183 | | #define DICT_FREE(str) \ |
184 | 0 | if ((str) && ((!dict) || \ |
185 | 0 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
186 | 0 | xmlFree((char *)(str)); |
187 | | |
188 | | static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur); |
189 | | static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur); |
190 | | |
191 | | static void |
192 | 0 | xmlTextReaderErr(xmlParserErrors code, const char *msg, ...) { |
193 | 0 | va_list ap; |
194 | 0 | int res; |
195 | |
|
196 | 0 | va_start(ap, msg); |
197 | 0 | res = xmlVRaiseError(NULL, NULL, NULL, NULL, NULL, |
198 | 0 | XML_FROM_PARSER, code, XML_ERR_FATAL, |
199 | 0 | NULL, 0, NULL, NULL, NULL, 0, 0, |
200 | 0 | msg, ap); |
201 | 0 | va_end(ap); |
202 | 0 | if (res < 0) |
203 | 0 | xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL); |
204 | 0 | } |
205 | | |
206 | | static void |
207 | 0 | xmlTextReaderErrMemory(xmlTextReaderPtr reader) { |
208 | 0 | if (reader == NULL) { |
209 | 0 | xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL); |
210 | 0 | return; |
211 | 0 | } |
212 | | |
213 | 0 | if (reader->ctxt != NULL) |
214 | 0 | xmlCtxtErrMemory(reader->ctxt); |
215 | 0 | else |
216 | 0 | xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL); |
217 | |
|
218 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
219 | 0 | reader->state = XML_TEXTREADER_ERROR; |
220 | 0 | } |
221 | | |
222 | | static xmlChar * |
223 | 0 | readerStrdup(xmlTextReaderPtr reader, const xmlChar *string) { |
224 | 0 | xmlChar *copy; |
225 | |
|
226 | 0 | if (string == NULL) |
227 | 0 | return(NULL); |
228 | | |
229 | 0 | copy = xmlStrdup(string); |
230 | 0 | if (copy == NULL) |
231 | 0 | xmlTextReaderErrMemory(reader); |
232 | |
|
233 | 0 | return(copy); |
234 | 0 | } |
235 | | |
236 | | static const xmlChar * |
237 | 0 | constString(xmlTextReaderPtr reader, const xmlChar *string) { |
238 | 0 | const xmlChar *dictString; |
239 | |
|
240 | 0 | if (string == NULL) |
241 | 0 | return(NULL); |
242 | | |
243 | 0 | dictString = xmlDictLookup(reader->dict, string, -1); |
244 | 0 | if (dictString == NULL) |
245 | 0 | xmlTextReaderErrMemory(reader); |
246 | |
|
247 | 0 | return(dictString); |
248 | 0 | } |
249 | | |
250 | | static const xmlChar * |
251 | | constQString(xmlTextReaderPtr reader, const xmlChar *prefix, |
252 | 0 | const xmlChar *name) { |
253 | 0 | const xmlChar *dictString; |
254 | |
|
255 | 0 | if (name == NULL) |
256 | 0 | return(NULL); |
257 | | |
258 | 0 | dictString = xmlDictQLookup(reader->dict, prefix, name); |
259 | 0 | if (dictString == NULL) |
260 | 0 | xmlTextReaderErrMemory(reader); |
261 | |
|
262 | 0 | return(dictString); |
263 | 0 | } |
264 | | |
265 | | /************************************************************************ |
266 | | * * |
267 | | * Our own version of the freeing routines as we recycle nodes * |
268 | | * * |
269 | | ************************************************************************/ |
270 | | |
271 | | /** |
272 | | * Free a node. |
273 | | * |
274 | | * @param reader the xmlTextReader used |
275 | | * @param cur the node |
276 | | */ |
277 | | static void |
278 | 0 | xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) { |
279 | 0 | xmlDictPtr dict; |
280 | |
|
281 | 0 | if ((reader != NULL) && (reader->ctxt != NULL)) |
282 | 0 | dict = reader->ctxt->dict; |
283 | 0 | else |
284 | 0 | dict = NULL; |
285 | 0 | if (cur == NULL) return; |
286 | | |
287 | 0 | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
288 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr) cur); |
289 | |
|
290 | 0 | if (cur->children != NULL) |
291 | 0 | xmlTextReaderFreeNodeList(reader, cur->children); |
292 | |
|
293 | 0 | if (cur->id != NULL) { |
294 | | /* |
295 | | * Operating in streaming mode, attr is gonna disappear |
296 | | */ |
297 | 0 | cur->id->attr = NULL; |
298 | 0 | if (cur->id->name != NULL) |
299 | 0 | DICT_FREE(cur->id->name); |
300 | 0 | cur->id->name = cur->name; |
301 | 0 | cur->name = NULL; |
302 | 0 | } else { |
303 | 0 | DICT_FREE(cur->name); |
304 | 0 | } |
305 | |
|
306 | 0 | if ((reader != NULL) && (reader->ctxt != NULL) && |
307 | 0 | (reader->ctxt->freeAttrsNr < MAX_FREE_NODES)) { |
308 | 0 | cur->next = reader->ctxt->freeAttrs; |
309 | 0 | reader->ctxt->freeAttrs = cur; |
310 | 0 | reader->ctxt->freeAttrsNr++; |
311 | 0 | } else { |
312 | 0 | xmlFree(cur); |
313 | 0 | } |
314 | 0 | } |
315 | | |
316 | | /** |
317 | | * Free a property and all its siblings, all the children are freed too. |
318 | | * |
319 | | * @param reader the xmlTextReader used |
320 | | * @param cur the first property in the list |
321 | | */ |
322 | | static void |
323 | 0 | xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) { |
324 | 0 | xmlAttrPtr next; |
325 | |
|
326 | 0 | while (cur != NULL) { |
327 | 0 | next = cur->next; |
328 | 0 | xmlTextReaderFreeProp(reader, cur); |
329 | 0 | cur = next; |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | | /** |
334 | | * Free a node and all its siblings, this is a recursive behaviour, all |
335 | | * the children are freed too. |
336 | | * |
337 | | * @param reader the xmlTextReader used |
338 | | * @param cur the first node in the list |
339 | | */ |
340 | | static void |
341 | 0 | xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) { |
342 | 0 | xmlNodePtr next; |
343 | 0 | xmlNodePtr parent; |
344 | 0 | xmlDictPtr dict; |
345 | 0 | size_t depth = 0; |
346 | |
|
347 | 0 | if ((reader != NULL) && (reader->ctxt != NULL)) |
348 | 0 | dict = reader->ctxt->dict; |
349 | 0 | else |
350 | 0 | dict = NULL; |
351 | 0 | if (cur == NULL) return; |
352 | 0 | if (cur->type == XML_NAMESPACE_DECL) { |
353 | 0 | xmlFreeNsList((xmlNsPtr) cur); |
354 | 0 | return; |
355 | 0 | } |
356 | 0 | if ((cur->type == XML_DOCUMENT_NODE) || |
357 | 0 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
358 | 0 | xmlFreeDoc((xmlDocPtr) cur); |
359 | 0 | return; |
360 | 0 | } |
361 | 0 | while (1) { |
362 | 0 | while ((cur->type != XML_DTD_NODE) && |
363 | 0 | (cur->type != XML_ENTITY_REF_NODE) && |
364 | 0 | (cur->children != NULL) && |
365 | 0 | (cur->children->parent == cur)) { |
366 | 0 | cur = cur->children; |
367 | 0 | depth += 1; |
368 | 0 | } |
369 | |
|
370 | 0 | next = cur->next; |
371 | 0 | parent = cur->parent; |
372 | | |
373 | | /* unroll to speed up freeing the document */ |
374 | 0 | if (cur->type != XML_DTD_NODE) { |
375 | |
|
376 | 0 | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
377 | 0 | xmlDeregisterNodeDefaultValue(cur); |
378 | |
|
379 | 0 | if (((cur->type == XML_ELEMENT_NODE) || |
380 | 0 | (cur->type == XML_XINCLUDE_START) || |
381 | 0 | (cur->type == XML_XINCLUDE_END)) && |
382 | 0 | (cur->properties != NULL)) |
383 | 0 | xmlTextReaderFreePropList(reader, cur->properties); |
384 | 0 | if ((cur->content != (xmlChar *) &(cur->properties)) && |
385 | 0 | (cur->type != XML_ELEMENT_NODE) && |
386 | 0 | (cur->type != XML_XINCLUDE_START) && |
387 | 0 | (cur->type != XML_XINCLUDE_END) && |
388 | 0 | (cur->type != XML_ENTITY_REF_NODE)) { |
389 | 0 | DICT_FREE(cur->content); |
390 | 0 | } |
391 | 0 | if (((cur->type == XML_ELEMENT_NODE) || |
392 | 0 | (cur->type == XML_XINCLUDE_START) || |
393 | 0 | (cur->type == XML_XINCLUDE_END)) && |
394 | 0 | (cur->nsDef != NULL)) |
395 | 0 | xmlFreeNsList(cur->nsDef); |
396 | | |
397 | | /* |
398 | | * we don't free element names here they are interned now |
399 | | */ |
400 | 0 | if ((cur->type != XML_TEXT_NODE) && |
401 | 0 | (cur->type != XML_COMMENT_NODE)) |
402 | 0 | DICT_FREE(cur->name); |
403 | 0 | if (((cur->type == XML_ELEMENT_NODE) || |
404 | 0 | (cur->type == XML_TEXT_NODE)) && |
405 | 0 | (reader != NULL) && (reader->ctxt != NULL) && |
406 | 0 | (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) { |
407 | 0 | cur->next = reader->ctxt->freeElems; |
408 | 0 | reader->ctxt->freeElems = cur; |
409 | 0 | reader->ctxt->freeElemsNr++; |
410 | 0 | } else { |
411 | 0 | xmlFree(cur); |
412 | 0 | } |
413 | 0 | } |
414 | |
|
415 | 0 | if (next != NULL) { |
416 | 0 | cur = next; |
417 | 0 | } else { |
418 | 0 | if ((depth == 0) || (parent == NULL)) |
419 | 0 | break; |
420 | 0 | depth -= 1; |
421 | 0 | cur = parent; |
422 | 0 | cur->children = NULL; |
423 | 0 | } |
424 | 0 | } |
425 | 0 | } |
426 | | |
427 | | /** |
428 | | * Free a node, this is a recursive behaviour, all the children are freed too. |
429 | | * This doesn't unlink the child from the list, use #xmlUnlinkNode first. |
430 | | * |
431 | | * @param reader the xmlTextReader used |
432 | | * @param cur the node |
433 | | */ |
434 | | static void |
435 | 0 | xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) { |
436 | 0 | xmlDictPtr dict; |
437 | |
|
438 | 0 | if ((reader != NULL) && (reader->ctxt != NULL)) |
439 | 0 | dict = reader->ctxt->dict; |
440 | 0 | else |
441 | 0 | dict = NULL; |
442 | 0 | if (cur->type == XML_DTD_NODE) { |
443 | 0 | xmlFreeDtd((xmlDtdPtr) cur); |
444 | 0 | return; |
445 | 0 | } |
446 | 0 | if (cur->type == XML_NAMESPACE_DECL) { |
447 | 0 | xmlFreeNs((xmlNsPtr) cur); |
448 | 0 | return; |
449 | 0 | } |
450 | 0 | if (cur->type == XML_ATTRIBUTE_NODE) { |
451 | 0 | xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur); |
452 | 0 | return; |
453 | 0 | } |
454 | | |
455 | 0 | if ((cur->children != NULL) && |
456 | 0 | (cur->type != XML_ENTITY_REF_NODE)) { |
457 | 0 | if (cur->children->parent == cur) |
458 | 0 | xmlTextReaderFreeNodeList(reader, cur->children); |
459 | 0 | cur->children = NULL; |
460 | 0 | } |
461 | |
|
462 | 0 | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
463 | 0 | xmlDeregisterNodeDefaultValue(cur); |
464 | |
|
465 | 0 | if (((cur->type == XML_ELEMENT_NODE) || |
466 | 0 | (cur->type == XML_XINCLUDE_START) || |
467 | 0 | (cur->type == XML_XINCLUDE_END)) && |
468 | 0 | (cur->properties != NULL)) |
469 | 0 | xmlTextReaderFreePropList(reader, cur->properties); |
470 | 0 | if ((cur->content != (xmlChar *) &(cur->properties)) && |
471 | 0 | (cur->type != XML_ELEMENT_NODE) && |
472 | 0 | (cur->type != XML_XINCLUDE_START) && |
473 | 0 | (cur->type != XML_XINCLUDE_END) && |
474 | 0 | (cur->type != XML_ENTITY_REF_NODE)) { |
475 | 0 | DICT_FREE(cur->content); |
476 | 0 | } |
477 | 0 | if (((cur->type == XML_ELEMENT_NODE) || |
478 | 0 | (cur->type == XML_XINCLUDE_START) || |
479 | 0 | (cur->type == XML_XINCLUDE_END)) && |
480 | 0 | (cur->nsDef != NULL)) |
481 | 0 | xmlFreeNsList(cur->nsDef); |
482 | | |
483 | | /* |
484 | | * we don't free names here they are interned now |
485 | | */ |
486 | 0 | if ((cur->type != XML_TEXT_NODE) && |
487 | 0 | (cur->type != XML_COMMENT_NODE)) |
488 | 0 | DICT_FREE(cur->name); |
489 | |
|
490 | 0 | if (((cur->type == XML_ELEMENT_NODE) || |
491 | 0 | (cur->type == XML_TEXT_NODE)) && |
492 | 0 | (reader != NULL) && (reader->ctxt != NULL) && |
493 | 0 | (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) { |
494 | 0 | cur->next = reader->ctxt->freeElems; |
495 | 0 | reader->ctxt->freeElems = cur; |
496 | 0 | reader->ctxt->freeElemsNr++; |
497 | 0 | } else { |
498 | 0 | xmlFree(cur); |
499 | 0 | } |
500 | 0 | } |
501 | | |
502 | | /** |
503 | | * Free up all the structures used by a document, tree included. |
504 | | * |
505 | | * @param reader the xmlTextReader used |
506 | | * @param cur pointer to the document |
507 | | */ |
508 | | static void |
509 | 0 | xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) { |
510 | 0 | xmlDtdPtr extSubset, intSubset; |
511 | |
|
512 | 0 | if (cur == NULL) return; |
513 | | |
514 | 0 | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
515 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr) cur); |
516 | | |
517 | | /* |
518 | | * Do this before freeing the children list to avoid ID lookups |
519 | | */ |
520 | 0 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
521 | 0 | cur->ids = NULL; |
522 | 0 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
523 | 0 | cur->refs = NULL; |
524 | 0 | extSubset = cur->extSubset; |
525 | 0 | intSubset = cur->intSubset; |
526 | 0 | if (intSubset == extSubset) |
527 | 0 | extSubset = NULL; |
528 | 0 | if (extSubset != NULL) { |
529 | 0 | xmlUnlinkNode((xmlNodePtr) cur->extSubset); |
530 | 0 | cur->extSubset = NULL; |
531 | 0 | xmlFreeDtd(extSubset); |
532 | 0 | } |
533 | 0 | if (intSubset != NULL) { |
534 | 0 | xmlUnlinkNode((xmlNodePtr) cur->intSubset); |
535 | 0 | cur->intSubset = NULL; |
536 | 0 | xmlFreeDtd(intSubset); |
537 | 0 | } |
538 | |
|
539 | 0 | if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children); |
540 | |
|
541 | 0 | if (cur->version != NULL) xmlFree((char *) cur->version); |
542 | 0 | if (cur->name != NULL) xmlFree((char *) cur->name); |
543 | 0 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
544 | 0 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
545 | 0 | if (cur->URL != NULL) xmlFree((char *) cur->URL); |
546 | 0 | if (cur->dict != NULL) xmlDictFree(cur->dict); |
547 | |
|
548 | 0 | xmlFree(cur); |
549 | 0 | } |
550 | | |
551 | | /************************************************************************ |
552 | | * * |
553 | | * The reader core parser * |
554 | | * * |
555 | | ************************************************************************/ |
556 | | |
557 | | static void |
558 | | xmlTextReaderStructuredRelay(void *userData, const xmlError *error) |
559 | 0 | { |
560 | 0 | xmlTextReaderPtr reader = (xmlTextReaderPtr) userData; |
561 | |
|
562 | 0 | if (reader->sErrorFunc != NULL) { |
563 | 0 | reader->sErrorFunc(reader->errorFuncArg, error); |
564 | 0 | } else if (reader->errorFunc != NULL) { |
565 | 0 | xmlParserSeverities severity; |
566 | |
|
567 | 0 | if ((error->domain == XML_FROM_VALID) || |
568 | 0 | (error->domain == XML_FROM_DTD)) { |
569 | 0 | if (error->level == XML_ERR_WARNING) |
570 | 0 | severity = XML_PARSER_SEVERITY_VALIDITY_WARNING; |
571 | 0 | else |
572 | 0 | severity = XML_PARSER_SEVERITY_VALIDITY_ERROR; |
573 | 0 | } else { |
574 | 0 | if (error->level == XML_ERR_WARNING) |
575 | 0 | severity = XML_PARSER_SEVERITY_WARNING; |
576 | 0 | else |
577 | 0 | severity = XML_PARSER_SEVERITY_ERROR; |
578 | 0 | } |
579 | |
|
580 | 0 | reader->errorFunc(reader->errorFuncArg, error->message, severity, |
581 | 0 | reader->ctxt); |
582 | 0 | } |
583 | 0 | } |
584 | | |
585 | | /** |
586 | | * Pushes a new entity reference node on top of the entities stack |
587 | | * |
588 | | * @param reader the xmlTextReader used |
589 | | * @param value the entity reference node |
590 | | * @returns -1 in case of error, the index in the stack otherwise |
591 | | */ |
592 | | static int |
593 | | xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value) |
594 | 0 | { |
595 | 0 | if (reader->entNr >= reader->entMax) { |
596 | 0 | xmlNodePtr *tmp; |
597 | 0 | int newSize; |
598 | |
|
599 | 0 | newSize = xmlGrowCapacity(reader->entMax, sizeof(tmp[0]), |
600 | 0 | 10, XML_MAX_ITEMS); |
601 | 0 | if (newSize < 0) { |
602 | 0 | xmlTextReaderErrMemory(reader); |
603 | 0 | return (-1); |
604 | 0 | } |
605 | 0 | tmp = xmlRealloc(reader->entTab, newSize * sizeof(tmp[0])); |
606 | 0 | if (tmp == NULL) { |
607 | 0 | xmlTextReaderErrMemory(reader); |
608 | 0 | return (-1); |
609 | 0 | } |
610 | 0 | reader->entTab = tmp; |
611 | 0 | reader->entMax = newSize; |
612 | 0 | } |
613 | 0 | reader->entTab[reader->entNr] = value; |
614 | 0 | reader->ent = value; |
615 | 0 | return (reader->entNr++); |
616 | 0 | } |
617 | | |
618 | | /** |
619 | | * Pops the top element entity from the entities stack |
620 | | * |
621 | | * @param reader the xmlTextReader used |
622 | | * @returns the entity just removed |
623 | | */ |
624 | | static xmlNodePtr |
625 | | xmlTextReaderEntPop(xmlTextReaderPtr reader) |
626 | 0 | { |
627 | 0 | xmlNodePtr ret; |
628 | |
|
629 | 0 | if (reader->entNr <= 0) |
630 | 0 | return (NULL); |
631 | 0 | reader->entNr--; |
632 | 0 | if (reader->entNr > 0) |
633 | 0 | reader->ent = reader->entTab[reader->entNr - 1]; |
634 | 0 | else |
635 | 0 | reader->ent = NULL; |
636 | 0 | ret = reader->entTab[reader->entNr]; |
637 | 0 | reader->entTab[reader->entNr] = NULL; |
638 | 0 | return (ret); |
639 | 0 | } |
640 | | |
641 | | /** |
642 | | * called when an opening tag has been processed. |
643 | | * |
644 | | * @param ctx the user data (XML parser context) |
645 | | * @param fullname The element name, including namespace prefix |
646 | | * @param atts An array of name/value attributes pairs, NULL terminated |
647 | | */ |
648 | | static void |
649 | | xmlTextReaderStartElement(void *ctx, const xmlChar *fullname, |
650 | 0 | const xmlChar **atts) { |
651 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
652 | 0 | xmlTextReaderPtr reader = ctxt->_private; |
653 | |
|
654 | 0 | if ((reader != NULL) && (reader->startElement != NULL)) { |
655 | 0 | reader->startElement(ctx, fullname, atts); |
656 | 0 | if ((ctxt->node != NULL) && (ctxt->input != NULL) && |
657 | 0 | (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') && |
658 | 0 | (ctxt->input->cur[1] == '>')) |
659 | 0 | ctxt->node->extra = NODE_IS_EMPTY; |
660 | 0 | } |
661 | 0 | if (reader != NULL) |
662 | 0 | reader->state = XML_TEXTREADER_ELEMENT; |
663 | 0 | } |
664 | | |
665 | | /** |
666 | | * called when an ending tag has been processed. |
667 | | * |
668 | | * @param ctx the user data (XML parser context) |
669 | | * @param fullname The element name, including namespace prefix |
670 | | */ |
671 | | static void |
672 | 0 | xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) { |
673 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
674 | 0 | xmlTextReaderPtr reader = ctxt->_private; |
675 | |
|
676 | 0 | if ((reader != NULL) && (reader->endElement != NULL)) { |
677 | 0 | reader->endElement(ctx, fullname); |
678 | 0 | } |
679 | 0 | } |
680 | | |
681 | | /** |
682 | | * called when an opening tag has been processed. |
683 | | * |
684 | | * @param ctx the user data (XML parser context) |
685 | | * @param localname the local name of the element |
686 | | * @param prefix the element namespace prefix if available |
687 | | * @param URI the element namespace name if available |
688 | | * @param nb_namespaces number of namespace definitions on that node |
689 | | * @param namespaces pointer to the array of prefix/URI pairs namespace definitions |
690 | | * @param nb_attributes the number of attributes on that node |
691 | | * @param nb_defaulted the number of defaulted attributes. |
692 | | * @param attributes pointer to the array of (localname/prefix/URI/value/end) |
693 | | * attribute values. |
694 | | */ |
695 | | static void |
696 | | xmlTextReaderStartElementNs(void *ctx, |
697 | | const xmlChar *localname, |
698 | | const xmlChar *prefix, |
699 | | const xmlChar *URI, |
700 | | int nb_namespaces, |
701 | | const xmlChar **namespaces, |
702 | | int nb_attributes, |
703 | | int nb_defaulted, |
704 | | const xmlChar **attributes) |
705 | 0 | { |
706 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
707 | 0 | xmlTextReaderPtr reader = ctxt->_private; |
708 | |
|
709 | 0 | if ((reader != NULL) && (reader->startElementNs != NULL)) { |
710 | 0 | reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces, |
711 | 0 | namespaces, nb_attributes, nb_defaulted, |
712 | 0 | attributes); |
713 | 0 | if ((ctxt->node != NULL) && (ctxt->input != NULL) && |
714 | 0 | (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') && |
715 | 0 | (ctxt->input->cur[1] == '>')) |
716 | 0 | ctxt->node->extra = NODE_IS_EMPTY; |
717 | 0 | } |
718 | 0 | if (reader != NULL) |
719 | 0 | reader->state = XML_TEXTREADER_ELEMENT; |
720 | 0 | } |
721 | | |
722 | | /** |
723 | | * called when an ending tag has been processed. |
724 | | * |
725 | | * @param ctx the user data (XML parser context) |
726 | | * @param localname the local name of the element |
727 | | * @param prefix the element namespace prefix if available |
728 | | * @param URI the element namespace name if available |
729 | | */ |
730 | | static void |
731 | | xmlTextReaderEndElementNs(void *ctx, |
732 | | const xmlChar * localname, |
733 | | const xmlChar * prefix, |
734 | | const xmlChar * URI) |
735 | 0 | { |
736 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
737 | 0 | xmlTextReaderPtr reader = ctxt->_private; |
738 | |
|
739 | 0 | if ((reader != NULL) && (reader->endElementNs != NULL)) { |
740 | 0 | reader->endElementNs(ctx, localname, prefix, URI); |
741 | 0 | } |
742 | 0 | } |
743 | | |
744 | | |
745 | | /** |
746 | | * receiving some chars from the parser. |
747 | | * |
748 | | * @param ctx the user data (XML parser context) |
749 | | * @param ch a xmlChar string |
750 | | * @param len the number of xmlChar |
751 | | */ |
752 | | static void |
753 | | xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len) |
754 | 0 | { |
755 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
756 | 0 | xmlTextReaderPtr reader = ctxt->_private; |
757 | |
|
758 | 0 | if ((reader != NULL) && (reader->characters != NULL)) { |
759 | 0 | reader->characters(ctx, ch, len); |
760 | 0 | } |
761 | 0 | } |
762 | | |
763 | | /** |
764 | | * called when a pcdata block has been parsed |
765 | | * |
766 | | * @param ctx the user data (XML parser context) |
767 | | * @param ch The pcdata content |
768 | | * @param len the block length |
769 | | */ |
770 | | static void |
771 | | xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len) |
772 | 0 | { |
773 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
774 | 0 | xmlTextReaderPtr reader = ctxt->_private; |
775 | |
|
776 | 0 | if ((reader != NULL) && (reader->cdataBlock != NULL)) { |
777 | 0 | reader->cdataBlock(ctx, ch, len); |
778 | 0 | } |
779 | 0 | } |
780 | | |
781 | | /** |
782 | | * Push data down the progressive parser until a significant callback |
783 | | * got raised. |
784 | | * |
785 | | * @param reader the xmlTextReader used |
786 | | * @returns -1 in case of failure, 0 otherwise |
787 | | */ |
788 | | static int |
789 | 0 | xmlTextReaderPushData(xmlTextReaderPtr reader) { |
790 | 0 | xmlBufPtr inbuf; |
791 | 0 | int val, s; |
792 | 0 | xmlTextReaderState oldstate; |
793 | |
|
794 | 0 | if ((reader->input == NULL) || (reader->input->buffer == NULL)) |
795 | 0 | return(-1); |
796 | | |
797 | 0 | oldstate = reader->state; |
798 | 0 | reader->state = XML_TEXTREADER_NONE; |
799 | 0 | inbuf = reader->input->buffer; |
800 | |
|
801 | 0 | while (reader->state == XML_TEXTREADER_NONE) { |
802 | 0 | if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) { |
803 | | /* |
804 | | * Refill the buffer unless we are at the end of the stream |
805 | | */ |
806 | 0 | if (reader->mode != XML_TEXTREADER_MODE_EOF) { |
807 | 0 | val = xmlParserInputBufferRead(reader->input, 4096); |
808 | 0 | if (val == 0) { |
809 | 0 | if (xmlBufUse(inbuf) == reader->cur) { |
810 | 0 | reader->mode = XML_TEXTREADER_MODE_EOF; |
811 | 0 | break; |
812 | 0 | } |
813 | 0 | } else if (val < 0) { |
814 | 0 | xmlCtxtErrIO(reader->ctxt, reader->input->error, NULL); |
815 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
816 | 0 | reader->state = XML_TEXTREADER_ERROR; |
817 | 0 | return(-1); |
818 | 0 | } |
819 | |
|
820 | 0 | } else |
821 | 0 | break; |
822 | 0 | } |
823 | | /* |
824 | | * parse by block of CHUNK_SIZE bytes, various tests show that |
825 | | * it's the best tradeoff at least on a 1.2GH Duron |
826 | | */ |
827 | 0 | if (xmlBufUse(inbuf) >= reader->cur + CHUNK_SIZE) { |
828 | 0 | val = xmlParseChunk(reader->ctxt, |
829 | 0 | (const char *) xmlBufContent(inbuf) + reader->cur, |
830 | 0 | CHUNK_SIZE, 0); |
831 | 0 | reader->cur += CHUNK_SIZE; |
832 | 0 | if (val != 0) |
833 | 0 | reader->ctxt->wellFormed = 0; |
834 | 0 | if (reader->ctxt->wellFormed == 0) |
835 | 0 | break; |
836 | 0 | } else { |
837 | 0 | s = xmlBufUse(inbuf) - reader->cur; |
838 | 0 | val = xmlParseChunk(reader->ctxt, |
839 | 0 | (const char *) xmlBufContent(inbuf) + reader->cur, |
840 | 0 | s, 0); |
841 | 0 | reader->cur += s; |
842 | 0 | if (val != 0) |
843 | 0 | reader->ctxt->wellFormed = 0; |
844 | 0 | break; |
845 | 0 | } |
846 | 0 | } |
847 | 0 | reader->state = oldstate; |
848 | | |
849 | | /* |
850 | | * Discard the consumed input when needed and possible |
851 | | */ |
852 | 0 | if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) { |
853 | 0 | if (reader->cur > 80 /* LINE_LEN */) { |
854 | 0 | val = xmlBufShrink(inbuf, reader->cur - 80); |
855 | 0 | if (val >= 0) { |
856 | 0 | reader->cur -= val; |
857 | 0 | } |
858 | 0 | } |
859 | 0 | } |
860 | | |
861 | | /* |
862 | | * At the end of the stream signal that the work is done to the Push |
863 | | * parser. |
864 | | */ |
865 | 0 | else if (reader->mode == XML_TEXTREADER_MODE_EOF) { |
866 | 0 | if (reader->state != XML_TEXTREADER_DONE) { |
867 | 0 | s = xmlBufUse(inbuf) - reader->cur; |
868 | 0 | val = xmlParseChunk(reader->ctxt, |
869 | 0 | (const char *) xmlBufContent(inbuf) + reader->cur, |
870 | 0 | s, 1); |
871 | 0 | reader->cur = xmlBufUse(inbuf); |
872 | 0 | reader->state = XML_TEXTREADER_DONE; |
873 | 0 | if (val != 0) { |
874 | 0 | if (reader->ctxt->wellFormed) |
875 | 0 | reader->ctxt->wellFormed = 0; |
876 | 0 | else |
877 | 0 | return(-1); |
878 | 0 | } |
879 | 0 | } |
880 | 0 | } |
881 | 0 | if (reader->ctxt->wellFormed == 0) { |
882 | 0 | reader->mode = XML_TEXTREADER_MODE_EOF; |
883 | 0 | return(-1); |
884 | 0 | } |
885 | | |
886 | 0 | return(0); |
887 | 0 | } |
888 | | |
889 | | #ifdef LIBXML_REGEXP_ENABLED |
890 | | /** |
891 | | * Push the current node for validation |
892 | | * |
893 | | * @param reader the xmlTextReader used |
894 | | */ |
895 | | static int |
896 | 0 | xmlTextReaderValidatePush(xmlTextReaderPtr reader) { |
897 | 0 | xmlNodePtr node = reader->node; |
898 | |
|
899 | 0 | #ifdef LIBXML_VALID_ENABLED |
900 | 0 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && |
901 | 0 | (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) { |
902 | 0 | if ((node->ns == NULL) || (node->ns->prefix == NULL)) { |
903 | 0 | reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt, |
904 | 0 | reader->ctxt->myDoc, node, node->name); |
905 | 0 | } else { |
906 | 0 | xmlChar buf[50]; |
907 | 0 | xmlChar *qname; |
908 | |
|
909 | 0 | qname = xmlBuildQName(node->name, node->ns->prefix, buf, 50); |
910 | 0 | if (qname == NULL) { |
911 | 0 | xmlTextReaderErrMemory(reader); |
912 | 0 | return(-1); |
913 | 0 | } |
914 | 0 | reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt, |
915 | 0 | reader->ctxt->myDoc, node, qname); |
916 | 0 | if (qname != buf) |
917 | 0 | xmlFree(qname); |
918 | 0 | } |
919 | | /*if (reader->ctxt->errNo == XML_ERR_NO_MEMORY) { |
920 | | reader->mode = XML_TEXTREADER_MODE_ERROR; |
921 | | reader->state = XML_TEXTREADER_ERROR; |
922 | | return(-1); |
923 | | }*/ |
924 | 0 | } |
925 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
926 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
927 | 0 | if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) && |
928 | 0 | (reader->rngValidCtxt != NULL)) { |
929 | 0 | int ret; |
930 | |
|
931 | 0 | if (reader->rngFullNode != NULL) return(0); |
932 | 0 | ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt, |
933 | 0 | reader->ctxt->myDoc, |
934 | 0 | node); |
935 | 0 | if (ret == 0) { |
936 | | /* |
937 | | * this element requires a full tree |
938 | | */ |
939 | 0 | node = xmlTextReaderExpand(reader); |
940 | 0 | if (node == NULL) { |
941 | 0 | ret = -1; |
942 | 0 | } else { |
943 | 0 | ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt, |
944 | 0 | reader->ctxt->myDoc, |
945 | 0 | node); |
946 | 0 | reader->rngFullNode = node; |
947 | 0 | } |
948 | 0 | } |
949 | 0 | if (ret != 1) |
950 | 0 | reader->rngValidErrors++; |
951 | 0 | } |
952 | 0 | #endif |
953 | | |
954 | 0 | return(0); |
955 | 0 | } |
956 | | |
957 | | /** |
958 | | * Push some CData for validation |
959 | | * |
960 | | * @param reader the xmlTextReader used |
961 | | * @param data pointer to the CData |
962 | | * @param len length of the CData block in bytes. |
963 | | */ |
964 | | static void |
965 | | xmlTextReaderValidateCData(xmlTextReaderPtr reader, |
966 | 0 | const xmlChar *data, int len) { |
967 | 0 | #ifdef LIBXML_VALID_ENABLED |
968 | 0 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && |
969 | 0 | (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) { |
970 | 0 | reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt, |
971 | 0 | data, len); |
972 | 0 | } |
973 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
974 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
975 | 0 | if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) && |
976 | 0 | (reader->rngValidCtxt != NULL)) { |
977 | 0 | int ret; |
978 | |
|
979 | 0 | if (reader->rngFullNode != NULL) return; |
980 | 0 | ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len); |
981 | 0 | if (ret != 1) |
982 | 0 | reader->rngValidErrors++; |
983 | 0 | } |
984 | 0 | #endif |
985 | 0 | } |
986 | | |
987 | | /** |
988 | | * Pop the current node from validation |
989 | | * |
990 | | * @param reader the xmlTextReader used |
991 | | */ |
992 | | static int |
993 | 0 | xmlTextReaderValidatePop(xmlTextReaderPtr reader) { |
994 | 0 | xmlNodePtr node = reader->node; |
995 | |
|
996 | 0 | #ifdef LIBXML_VALID_ENABLED |
997 | 0 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && |
998 | 0 | (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) { |
999 | 0 | if ((node->ns == NULL) || (node->ns->prefix == NULL)) { |
1000 | 0 | reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt, |
1001 | 0 | reader->ctxt->myDoc, node, node->name); |
1002 | 0 | } else { |
1003 | 0 | xmlChar buf[50]; |
1004 | 0 | xmlChar *qname; |
1005 | |
|
1006 | 0 | qname = xmlBuildQName(node->name, node->ns->prefix, buf, 50); |
1007 | 0 | if (qname == NULL) { |
1008 | 0 | xmlTextReaderErrMemory(reader); |
1009 | 0 | return(-1); |
1010 | 0 | } |
1011 | 0 | reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt, |
1012 | 0 | reader->ctxt->myDoc, node, qname); |
1013 | 0 | if (qname != buf) |
1014 | 0 | xmlFree(qname); |
1015 | 0 | } |
1016 | | /*if (reader->ctxt->errNo == XML_ERR_NO_MEMORY) { |
1017 | | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1018 | | reader->state = XML_TEXTREADER_ERROR; |
1019 | | return(-1); |
1020 | | }*/ |
1021 | 0 | } |
1022 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
1023 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
1024 | 0 | if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) && |
1025 | 0 | (reader->rngValidCtxt != NULL)) { |
1026 | 0 | int ret; |
1027 | |
|
1028 | 0 | if (reader->rngFullNode != NULL) { |
1029 | 0 | if (node == reader->rngFullNode) |
1030 | 0 | reader->rngFullNode = NULL; |
1031 | 0 | return(0); |
1032 | 0 | } |
1033 | 0 | ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt, |
1034 | 0 | reader->ctxt->myDoc, |
1035 | 0 | node); |
1036 | 0 | if (ret != 1) |
1037 | 0 | reader->rngValidErrors++; |
1038 | 0 | } |
1039 | 0 | #endif |
1040 | | |
1041 | 0 | return(0); |
1042 | 0 | } |
1043 | | |
1044 | | /** |
1045 | | * Handle the validation when an entity reference is encountered and |
1046 | | * entity substitution is not activated. As a result the parser interface |
1047 | | * must walk through the entity and do the validation calls |
1048 | | * |
1049 | | * @param reader the xmlTextReader used |
1050 | | */ |
1051 | | static int |
1052 | 0 | xmlTextReaderValidateEntity(xmlTextReaderPtr reader) { |
1053 | 0 | xmlNodePtr oldnode = reader->node; |
1054 | 0 | xmlNodePtr node = reader->node; |
1055 | |
|
1056 | 0 | do { |
1057 | 0 | if (node->type == XML_ENTITY_REF_NODE) { |
1058 | 0 | if ((node->children != NULL) && |
1059 | 0 | (node->children->type == XML_ENTITY_DECL) && |
1060 | 0 | (node->children->children != NULL)) { |
1061 | 0 | if (xmlTextReaderEntPush(reader, node) < 0) { |
1062 | 0 | if (node == oldnode) |
1063 | 0 | break; |
1064 | 0 | goto skip_children; |
1065 | 0 | } |
1066 | 0 | node = node->children->children; |
1067 | 0 | continue; |
1068 | 0 | } else { |
1069 | | /* |
1070 | | * The error has probably been raised already. |
1071 | | */ |
1072 | 0 | if (node == oldnode) |
1073 | 0 | break; |
1074 | 0 | goto skip_children; |
1075 | 0 | } |
1076 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
1077 | 0 | } else if (node->type == XML_ELEMENT_NODE) { |
1078 | 0 | reader->node = node; |
1079 | 0 | if (xmlTextReaderValidatePush(reader) < 0) |
1080 | 0 | return(-1); |
1081 | 0 | } else if ((node->type == XML_TEXT_NODE) || |
1082 | 0 | (node->type == XML_CDATA_SECTION_NODE)) { |
1083 | 0 | xmlTextReaderValidateCData(reader, node->content, |
1084 | 0 | xmlStrlen(node->content)); |
1085 | 0 | #endif |
1086 | 0 | } |
1087 | | |
1088 | | /* |
1089 | | * go to next node |
1090 | | */ |
1091 | 0 | if (node->children != NULL) { |
1092 | 0 | node = node->children; |
1093 | 0 | continue; |
1094 | 0 | } else if (node->type == XML_ELEMENT_NODE) { |
1095 | 0 | if (xmlTextReaderValidatePop(reader) < 0) |
1096 | 0 | return(-1); |
1097 | 0 | } |
1098 | 0 | skip_children: |
1099 | 0 | if (node->next != NULL) { |
1100 | 0 | node = node->next; |
1101 | 0 | continue; |
1102 | 0 | } |
1103 | 0 | do { |
1104 | 0 | node = node->parent; |
1105 | 0 | if (node->type == XML_ELEMENT_NODE) { |
1106 | 0 | xmlNodePtr tmp; |
1107 | 0 | if (reader->entNr == 0) { |
1108 | 0 | while ((tmp = node->last) != NULL) { |
1109 | 0 | if ((tmp->extra & NODE_IS_PRESERVED) == 0) { |
1110 | 0 | xmlUnlinkNode(tmp); |
1111 | 0 | xmlTextReaderFreeNode(reader, tmp); |
1112 | 0 | } else |
1113 | 0 | break; |
1114 | 0 | } |
1115 | 0 | } |
1116 | 0 | reader->node = node; |
1117 | 0 | if (xmlTextReaderValidatePop(reader) < 0) |
1118 | 0 | return(-1); |
1119 | 0 | } |
1120 | 0 | if ((node->type == XML_ENTITY_DECL) && |
1121 | 0 | (reader->ent != NULL) && (reader->ent->children == node)) { |
1122 | 0 | node = xmlTextReaderEntPop(reader); |
1123 | 0 | } |
1124 | 0 | if (node == oldnode) |
1125 | 0 | break; |
1126 | 0 | if (node->next != NULL) { |
1127 | 0 | node = node->next; |
1128 | 0 | break; |
1129 | 0 | } |
1130 | 0 | } while ((node != NULL) && (node != oldnode)); |
1131 | 0 | } while ((node != NULL) && (node != oldnode)); |
1132 | 0 | reader->node = oldnode; |
1133 | |
|
1134 | 0 | return(0); |
1135 | 0 | } |
1136 | | #endif /* LIBXML_REGEXP_ENABLED */ |
1137 | | |
1138 | | |
1139 | | /** |
1140 | | * Get the successor of a node if available. |
1141 | | * |
1142 | | * @param cur the current node |
1143 | | * @returns the successor node or NULL |
1144 | | */ |
1145 | | static xmlNodePtr |
1146 | 0 | xmlTextReaderGetSuccessor(xmlNodePtr cur) { |
1147 | 0 | if (cur == NULL) return(NULL) ; /* ERROR */ |
1148 | 0 | if (cur->next != NULL) return(cur->next) ; |
1149 | 0 | do { |
1150 | 0 | cur = cur->parent; |
1151 | 0 | if (cur == NULL) break; |
1152 | 0 | if (cur->next != NULL) return(cur->next); |
1153 | 0 | } while (cur != NULL); |
1154 | 0 | return(cur); |
1155 | 0 | } |
1156 | | |
1157 | | /** |
1158 | | * Makes sure that the current node is fully read as well as all its |
1159 | | * descendant. It means the full DOM subtree must be available at the |
1160 | | * end of the call. |
1161 | | * |
1162 | | * @param reader the xmlTextReader used |
1163 | | * @returns 1 if the node was expanded successfully, 0 if there is no more |
1164 | | * nodes to read, or -1 in case of error |
1165 | | */ |
1166 | | static int |
1167 | 0 | xmlTextReaderDoExpand(xmlTextReaderPtr reader) { |
1168 | 0 | int val; |
1169 | |
|
1170 | 0 | if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL)) |
1171 | 0 | return(-1); |
1172 | 0 | do { |
1173 | 0 | if (PARSER_STOPPED(reader->ctxt)) |
1174 | 0 | return(1); |
1175 | | |
1176 | 0 | if (xmlTextReaderGetSuccessor(reader->node) != NULL) |
1177 | 0 | return(1); |
1178 | 0 | if (reader->ctxt->nodeNr < reader->depth) |
1179 | 0 | return(1); |
1180 | 0 | if (reader->mode == XML_TEXTREADER_MODE_EOF) |
1181 | 0 | return(1); |
1182 | 0 | val = xmlTextReaderPushData(reader); |
1183 | 0 | if (val < 0){ |
1184 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1185 | 0 | reader->state = XML_TEXTREADER_ERROR; |
1186 | 0 | return(-1); |
1187 | 0 | } |
1188 | 0 | } while(reader->mode != XML_TEXTREADER_MODE_EOF); |
1189 | 0 | return(1); |
1190 | 0 | } |
1191 | | |
1192 | | /** |
1193 | | * Moves the position of the current instance to the next node in |
1194 | | * the stream, exposing its properties. |
1195 | | * |
1196 | | * @param reader the xmlTextReader used |
1197 | | * @returns 1 if the node was read successfully, 0 if there is no more |
1198 | | * nodes to read, or -1 in case of error |
1199 | | */ |
1200 | | int |
1201 | 0 | xmlTextReaderRead(xmlTextReader *reader) { |
1202 | 0 | int val, olddepth = 0; |
1203 | 0 | xmlTextReaderState oldstate = XML_TEXTREADER_START; |
1204 | 0 | xmlNodePtr oldnode = NULL; |
1205 | |
|
1206 | 0 | if (reader == NULL) |
1207 | 0 | return(-1); |
1208 | 0 | if (reader->state == XML_TEXTREADER_ERROR) |
1209 | 0 | return(-1); |
1210 | | |
1211 | 0 | reader->curnode = NULL; |
1212 | 0 | if (reader->doc != NULL) |
1213 | 0 | return(xmlTextReaderReadTree(reader)); |
1214 | 0 | if (reader->ctxt == NULL) |
1215 | 0 | return(-1); |
1216 | | |
1217 | 0 | if (reader->mode == XML_TEXTREADER_MODE_INITIAL) { |
1218 | 0 | reader->mode = XML_TEXTREADER_MODE_INTERACTIVE; |
1219 | | /* |
1220 | | * Initial state |
1221 | | */ |
1222 | 0 | do { |
1223 | 0 | val = xmlTextReaderPushData(reader); |
1224 | 0 | if (val < 0) { |
1225 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1226 | 0 | reader->state = XML_TEXTREADER_ERROR; |
1227 | 0 | return(-1); |
1228 | 0 | } |
1229 | 0 | } while ((reader->ctxt->node == NULL) && |
1230 | 0 | ((reader->mode != XML_TEXTREADER_MODE_EOF) && |
1231 | 0 | (reader->state != XML_TEXTREADER_DONE))); |
1232 | 0 | if (reader->ctxt->node == NULL) { |
1233 | 0 | if (reader->ctxt->myDoc != NULL) { |
1234 | 0 | reader->node = reader->ctxt->myDoc->children; |
1235 | 0 | } |
1236 | 0 | if (reader->node == NULL) { |
1237 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1238 | 0 | reader->state = XML_TEXTREADER_ERROR; |
1239 | 0 | return(-1); |
1240 | 0 | } |
1241 | 0 | reader->state = XML_TEXTREADER_ELEMENT; |
1242 | 0 | } else { |
1243 | 0 | if (reader->ctxt->myDoc != NULL) { |
1244 | 0 | reader->node = reader->ctxt->myDoc->children; |
1245 | 0 | } |
1246 | 0 | if (reader->node == NULL) |
1247 | 0 | reader->node = reader->ctxt->nodeTab[0]; |
1248 | 0 | reader->state = XML_TEXTREADER_ELEMENT; |
1249 | 0 | } |
1250 | 0 | reader->depth = 0; |
1251 | 0 | reader->ctxt->parseMode = XML_PARSE_READER; |
1252 | 0 | goto node_found; |
1253 | 0 | } |
1254 | 0 | oldstate = reader->state; |
1255 | 0 | olddepth = reader->ctxt->nodeNr; |
1256 | 0 | oldnode = reader->node; |
1257 | |
|
1258 | 0 | get_next_node: |
1259 | 0 | if (reader->node == NULL) { |
1260 | 0 | if (reader->mode == XML_TEXTREADER_MODE_EOF) { |
1261 | 0 | return(0); |
1262 | 0 | } else { |
1263 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1264 | 0 | reader->state = XML_TEXTREADER_ERROR; |
1265 | 0 | return(-1); |
1266 | 0 | } |
1267 | 0 | } |
1268 | | |
1269 | | /* |
1270 | | * If we are not backtracking on ancestors or examined nodes, |
1271 | | * that the parser didn't finished or that we aren't at the end |
1272 | | * of stream, continue processing. |
1273 | | */ |
1274 | 0 | while ((reader->node != NULL) && (reader->node->next == NULL) && |
1275 | 0 | (reader->ctxt->nodeNr == olddepth) && |
1276 | 0 | ((oldstate == XML_TEXTREADER_BACKTRACK) || |
1277 | 0 | (reader->node->children == NULL) || |
1278 | 0 | (reader->node->type == XML_ENTITY_REF_NODE) || |
1279 | 0 | ((reader->node->children != NULL) && |
1280 | 0 | (reader->node->children->type == XML_TEXT_NODE) && |
1281 | 0 | (reader->node->children->next == NULL)) || |
1282 | 0 | (reader->node->type == XML_DTD_NODE) || |
1283 | 0 | (reader->node->type == XML_DOCUMENT_NODE) || |
1284 | 0 | (reader->node->type == XML_HTML_DOCUMENT_NODE)) && |
1285 | 0 | ((reader->ctxt->node == NULL) || |
1286 | 0 | (reader->ctxt->node == reader->node) || |
1287 | 0 | (reader->ctxt->node == reader->node->parent)) && |
1288 | 0 | (reader->ctxt->instate != XML_PARSER_EOF) && |
1289 | 0 | (PARSER_STOPPED(reader->ctxt) == 0)) { |
1290 | 0 | val = xmlTextReaderPushData(reader); |
1291 | 0 | if (val < 0) { |
1292 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1293 | 0 | reader->state = XML_TEXTREADER_ERROR; |
1294 | 0 | return(-1); |
1295 | 0 | } |
1296 | 0 | if (reader->node == NULL) |
1297 | 0 | goto node_end; |
1298 | 0 | } |
1299 | 0 | if (oldstate != XML_TEXTREADER_BACKTRACK) { |
1300 | 0 | if ((reader->node->children != NULL) && |
1301 | 0 | (reader->node->type != XML_ENTITY_REF_NODE) && |
1302 | 0 | (reader->node->type != XML_XINCLUDE_START) && |
1303 | 0 | (reader->node->type != XML_DTD_NODE)) { |
1304 | 0 | reader->node = reader->node->children; |
1305 | 0 | reader->depth++; |
1306 | 0 | reader->state = XML_TEXTREADER_ELEMENT; |
1307 | 0 | goto node_found; |
1308 | 0 | } |
1309 | 0 | } |
1310 | 0 | if (reader->node->next != NULL) { |
1311 | 0 | if ((oldstate == XML_TEXTREADER_ELEMENT) && |
1312 | 0 | (reader->node->type == XML_ELEMENT_NODE) && |
1313 | 0 | (reader->node->children == NULL) && |
1314 | 0 | ((reader->node->extra & NODE_IS_EMPTY) == 0) |
1315 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
1316 | 0 | && (reader->in_xinclude <= 0) |
1317 | 0 | #endif |
1318 | 0 | ) { |
1319 | 0 | reader->state = XML_TEXTREADER_END; |
1320 | 0 | goto node_found; |
1321 | 0 | } |
1322 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
1323 | 0 | if ((reader->validate) && |
1324 | 0 | (reader->node->type == XML_ELEMENT_NODE)) |
1325 | 0 | if (xmlTextReaderValidatePop(reader) < 0) |
1326 | 0 | return(-1); |
1327 | 0 | #endif /* LIBXML_REGEXP_ENABLED */ |
1328 | 0 | if ((reader->preserves > 0) && |
1329 | 0 | (reader->node->extra & NODE_IS_SPRESERVED)) |
1330 | 0 | reader->preserves--; |
1331 | 0 | reader->node = reader->node->next; |
1332 | 0 | reader->state = XML_TEXTREADER_ELEMENT; |
1333 | | |
1334 | | /* |
1335 | | * Cleanup of the old node |
1336 | | */ |
1337 | 0 | if ((reader->preserves == 0) && |
1338 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
1339 | 0 | (reader->in_xinclude == 0) && |
1340 | 0 | #endif |
1341 | 0 | (reader->entNr == 0) && |
1342 | 0 | (reader->node->prev != NULL) && |
1343 | 0 | (reader->node->prev->type != XML_DTD_NODE)) { |
1344 | 0 | xmlNodePtr tmp = reader->node->prev; |
1345 | 0 | if ((tmp->extra & NODE_IS_PRESERVED) == 0) { |
1346 | 0 | if (oldnode == tmp) |
1347 | 0 | oldnode = NULL; |
1348 | 0 | xmlUnlinkNode(tmp); |
1349 | 0 | xmlTextReaderFreeNode(reader, tmp); |
1350 | 0 | } |
1351 | 0 | } |
1352 | |
|
1353 | 0 | goto node_found; |
1354 | 0 | } |
1355 | 0 | if ((oldstate == XML_TEXTREADER_ELEMENT) && |
1356 | 0 | (reader->node->type == XML_ELEMENT_NODE) && |
1357 | 0 | (reader->node->children == NULL) && |
1358 | 0 | ((reader->node->extra & NODE_IS_EMPTY) == 0)) {; |
1359 | 0 | reader->state = XML_TEXTREADER_END; |
1360 | 0 | goto node_found; |
1361 | 0 | } |
1362 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
1363 | 0 | if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) && |
1364 | 0 | (reader->node->type == XML_ELEMENT_NODE)) { |
1365 | 0 | if (xmlTextReaderValidatePop(reader) < 0) |
1366 | 0 | return(-1); |
1367 | 0 | } |
1368 | 0 | #endif /* LIBXML_REGEXP_ENABLED */ |
1369 | 0 | if ((reader->preserves > 0) && |
1370 | 0 | (reader->node->extra & NODE_IS_SPRESERVED)) |
1371 | 0 | reader->preserves--; |
1372 | 0 | reader->node = reader->node->parent; |
1373 | 0 | if ((reader->node == NULL) || |
1374 | 0 | (reader->node->type == XML_DOCUMENT_NODE) || |
1375 | 0 | (reader->node->type == XML_HTML_DOCUMENT_NODE)) { |
1376 | 0 | if (reader->mode != XML_TEXTREADER_MODE_EOF) { |
1377 | 0 | val = xmlParseChunk(reader->ctxt, "", 0, 1); |
1378 | 0 | reader->state = XML_TEXTREADER_DONE; |
1379 | 0 | if (val != 0) { |
1380 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1381 | 0 | reader->state = XML_TEXTREADER_ERROR; |
1382 | 0 | return(-1); |
1383 | 0 | } |
1384 | 0 | } |
1385 | 0 | reader->node = NULL; |
1386 | 0 | reader->depth = -1; |
1387 | | |
1388 | | /* |
1389 | | * Cleanup of the old node |
1390 | | */ |
1391 | 0 | if ((oldnode != NULL) && (reader->preserves == 0) && |
1392 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
1393 | 0 | (reader->in_xinclude == 0) && |
1394 | 0 | #endif |
1395 | 0 | (reader->entNr == 0) && |
1396 | 0 | (oldnode->type != XML_DTD_NODE) && |
1397 | 0 | ((oldnode->extra & NODE_IS_PRESERVED) == 0)) { |
1398 | 0 | xmlUnlinkNode(oldnode); |
1399 | 0 | xmlTextReaderFreeNode(reader, oldnode); |
1400 | 0 | } |
1401 | |
|
1402 | 0 | goto node_end; |
1403 | 0 | } |
1404 | 0 | if ((reader->preserves == 0) && |
1405 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
1406 | 0 | (reader->in_xinclude == 0) && |
1407 | 0 | #endif |
1408 | 0 | (reader->entNr == 0) && |
1409 | 0 | (reader->node->last != NULL) && |
1410 | 0 | ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) { |
1411 | 0 | xmlNodePtr tmp = reader->node->last; |
1412 | 0 | xmlUnlinkNode(tmp); |
1413 | 0 | xmlTextReaderFreeNode(reader, tmp); |
1414 | 0 | } |
1415 | 0 | reader->depth--; |
1416 | 0 | reader->state = XML_TEXTREADER_BACKTRACK; |
1417 | |
|
1418 | 0 | node_found: |
1419 | | /* |
1420 | | * If we are in the middle of a piece of CDATA make sure it's finished |
1421 | | */ |
1422 | 0 | if ((reader->node != NULL) && |
1423 | 0 | (reader->node->next == NULL) && |
1424 | 0 | ((reader->node->type == XML_TEXT_NODE) || |
1425 | 0 | (reader->node->type == XML_CDATA_SECTION_NODE))) { |
1426 | 0 | if (xmlTextReaderExpand(reader) == NULL) |
1427 | 0 | return -1; |
1428 | 0 | } |
1429 | | |
1430 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
1431 | | /* |
1432 | | * Handle XInclude if asked for |
1433 | | */ |
1434 | 0 | if ((reader->xinclude) && (reader->in_xinclude == 0) && |
1435 | 0 | (reader->state != XML_TEXTREADER_BACKTRACK) && |
1436 | 0 | (reader->node != NULL) && |
1437 | 0 | (reader->node->type == XML_ELEMENT_NODE) && |
1438 | 0 | (reader->node->ns != NULL) && |
1439 | 0 | ((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) || |
1440 | 0 | (xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) { |
1441 | 0 | if (reader->xincctxt == NULL) { |
1442 | 0 | reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc); |
1443 | 0 | if (reader->xincctxt == NULL) { |
1444 | 0 | xmlTextReaderErrMemory(reader); |
1445 | 0 | return(-1); |
1446 | 0 | } |
1447 | 0 | xmlXIncludeSetFlags(reader->xincctxt, |
1448 | 0 | reader->parserFlags & (~XML_PARSE_NOXINCNODE)); |
1449 | 0 | xmlXIncludeSetStreamingMode(reader->xincctxt, 1); |
1450 | 0 | if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL)) |
1451 | 0 | xmlXIncludeSetErrorHandler(reader->xincctxt, |
1452 | 0 | xmlTextReaderStructuredRelay, reader); |
1453 | 0 | if (reader->resourceLoader != NULL) |
1454 | 0 | xmlXIncludeSetResourceLoader(reader->xincctxt, |
1455 | 0 | reader->resourceLoader, reader->resourceCtxt); |
1456 | 0 | } |
1457 | | /* |
1458 | | * expand that node and process it |
1459 | | */ |
1460 | 0 | if (xmlTextReaderExpand(reader) == NULL) |
1461 | 0 | return(-1); |
1462 | 0 | if (xmlXIncludeProcessNode(reader->xincctxt, reader->node) < 0) { |
1463 | 0 | int err = xmlXIncludeGetLastError(reader->xincctxt); |
1464 | |
|
1465 | 0 | if (xmlIsCatastrophicError(XML_ERR_FATAL, err)) { |
1466 | 0 | xmlFatalErr(reader->ctxt, err, NULL); |
1467 | 0 | reader->mode = XML_TEXTREADER_MODE_ERROR; |
1468 | 0 | reader->state = XML_TEXTREADER_ERROR; |
1469 | 0 | } |
1470 | 0 | return(-1); |
1471 | 0 | } |
1472 | 0 | } |
1473 | 0 | if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_START)) { |
1474 | 0 | reader->in_xinclude++; |
1475 | 0 | goto get_next_node; |
1476 | 0 | } |
1477 | 0 | if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_END)) { |
1478 | 0 | reader->in_xinclude--; |
1479 | 0 | goto get_next_node; |
1480 | 0 | } |
1481 | 0 | #endif |
1482 | | /* |
1483 | | * Handle entities enter and exit when in entity replacement mode |
1484 | | */ |
1485 | 0 | if ((reader->node != NULL) && |
1486 | 0 | (reader->node->type == XML_ENTITY_REF_NODE) && |
1487 | 0 | (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) { |
1488 | 0 | if ((reader->node->children != NULL) && |
1489 | 0 | (reader->node->children->type == XML_ENTITY_DECL) && |
1490 | 0 | (reader->node->children->children != NULL)) { |
1491 | 0 | if (xmlTextReaderEntPush(reader, reader->node) < 0) |
1492 | 0 | goto get_next_node; |
1493 | 0 | reader->node = reader->node->children->children; |
1494 | 0 | } |
1495 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
1496 | 0 | } else if ((reader->node != NULL) && |
1497 | 0 | (reader->node->type == XML_ENTITY_REF_NODE) && |
1498 | 0 | (reader->ctxt != NULL) && (reader->validate)) { |
1499 | 0 | if (xmlTextReaderValidateEntity(reader) < 0) |
1500 | 0 | return(-1); |
1501 | 0 | #endif /* LIBXML_REGEXP_ENABLED */ |
1502 | 0 | } |
1503 | 0 | if ((reader->node != NULL) && |
1504 | 0 | (reader->node->type == XML_ENTITY_DECL) && |
1505 | 0 | (reader->ent != NULL) && (reader->ent->children == reader->node)) { |
1506 | 0 | reader->node = xmlTextReaderEntPop(reader); |
1507 | 0 | reader->depth++; |
1508 | 0 | goto get_next_node; |
1509 | 0 | } |
1510 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
1511 | 0 | if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) && (reader->node != NULL)) { |
1512 | 0 | xmlNodePtr node = reader->node; |
1513 | |
|
1514 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
1515 | 0 | ((reader->state != XML_TEXTREADER_END) && |
1516 | 0 | (reader->state != XML_TEXTREADER_BACKTRACK))) { |
1517 | 0 | if (xmlTextReaderValidatePush(reader) < 0) |
1518 | 0 | return(-1); |
1519 | 0 | } else if ((node->type == XML_TEXT_NODE) || |
1520 | 0 | (node->type == XML_CDATA_SECTION_NODE)) { |
1521 | 0 | xmlTextReaderValidateCData(reader, node->content, |
1522 | 0 | xmlStrlen(node->content)); |
1523 | 0 | } |
1524 | 0 | } |
1525 | 0 | #endif /* LIBXML_REGEXP_ENABLED */ |
1526 | 0 | #ifdef LIBXML_PATTERN_ENABLED |
1527 | 0 | if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) && |
1528 | 0 | (reader->state != XML_TEXTREADER_BACKTRACK)) { |
1529 | 0 | int i; |
1530 | 0 | for (i = 0;i < reader->patternNr;i++) { |
1531 | 0 | if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) { |
1532 | 0 | xmlTextReaderPreserve(reader); |
1533 | 0 | break; |
1534 | 0 | } |
1535 | 0 | } |
1536 | 0 | } |
1537 | 0 | #endif /* LIBXML_PATTERN_ENABLED */ |
1538 | 0 | #ifdef LIBXML_SCHEMAS_ENABLED |
1539 | 0 | if ((reader->validate == XML_TEXTREADER_VALIDATE_XSD) && |
1540 | 0 | (reader->xsdValidErrors == 0) && |
1541 | 0 | (reader->xsdValidCtxt != NULL)) { |
1542 | 0 | reader->xsdValidErrors = !xmlSchemaIsValid(reader->xsdValidCtxt); |
1543 | 0 | } |
1544 | 0 | #endif /* LIBXML_PATTERN_ENABLED */ |
1545 | 0 | return(1); |
1546 | 0 | node_end: |
1547 | 0 | reader->state = XML_TEXTREADER_DONE; |
1548 | 0 | return(0); |
1549 | 0 | } |
1550 | | |
1551 | | /** |
1552 | | * Gets the read state of the reader. |
1553 | | * |
1554 | | * @param reader the xmlTextReader used |
1555 | | * @returns the state value, or -1 in case of error |
1556 | | */ |
1557 | | int |
1558 | 0 | xmlTextReaderReadState(xmlTextReader *reader) { |
1559 | 0 | if (reader == NULL) |
1560 | 0 | return(-1); |
1561 | 0 | return(reader->mode); |
1562 | 0 | } |
1563 | | |
1564 | | /** |
1565 | | * Reads the contents of the current node and the full subtree. It then makes |
1566 | | * the subtree available until the next #xmlTextReaderRead call |
1567 | | * |
1568 | | * @param reader the xmlTextReader used |
1569 | | * @returns a node pointer valid until the next #xmlTextReaderRead call |
1570 | | * or NULL in case of error. |
1571 | | */ |
1572 | | xmlNode * |
1573 | 0 | xmlTextReaderExpand(xmlTextReader *reader) { |
1574 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
1575 | 0 | return(NULL); |
1576 | 0 | if (reader->doc != NULL) |
1577 | 0 | return(reader->node); |
1578 | 0 | if (reader->ctxt == NULL) |
1579 | 0 | return(NULL); |
1580 | 0 | if (xmlTextReaderDoExpand(reader) < 0) |
1581 | 0 | return(NULL); |
1582 | 0 | return(reader->node); |
1583 | 0 | } |
1584 | | |
1585 | | /** |
1586 | | * Skip to the node following the current one in document order while |
1587 | | * avoiding the subtree if any. |
1588 | | * |
1589 | | * @param reader the xmlTextReader used |
1590 | | * @returns 1 if the node was read successfully, 0 if there is no more |
1591 | | * nodes to read, or -1 in case of error |
1592 | | */ |
1593 | | int |
1594 | 0 | xmlTextReaderNext(xmlTextReader *reader) { |
1595 | 0 | int ret; |
1596 | 0 | xmlNodePtr cur; |
1597 | |
|
1598 | 0 | if (reader == NULL) |
1599 | 0 | return(-1); |
1600 | 0 | if (reader->doc != NULL) |
1601 | 0 | return(xmlTextReaderNextTree(reader)); |
1602 | 0 | cur = reader->node; |
1603 | 0 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
1604 | 0 | return(xmlTextReaderRead(reader)); |
1605 | 0 | if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK) |
1606 | 0 | return(xmlTextReaderRead(reader)); |
1607 | 0 | if (cur->extra & NODE_IS_EMPTY) |
1608 | 0 | return(xmlTextReaderRead(reader)); |
1609 | 0 | do { |
1610 | 0 | ret = xmlTextReaderRead(reader); |
1611 | 0 | if (ret != 1) |
1612 | 0 | return(ret); |
1613 | 0 | } while (reader->node != cur); |
1614 | 0 | return(xmlTextReaderRead(reader)); |
1615 | 0 | } |
1616 | | |
1617 | | #ifdef LIBXML_WRITER_ENABLED |
1618 | | static void |
1619 | | xmlTextReaderDumpCopy(xmlTextReaderPtr reader, xmlOutputBufferPtr output, |
1620 | 0 | xmlNodePtr node) { |
1621 | 0 | if ((node->type == XML_DTD_NODE) || |
1622 | 0 | (node->type == XML_ELEMENT_DECL) || |
1623 | 0 | (node->type == XML_ATTRIBUTE_DECL) || |
1624 | 0 | (node->type == XML_ENTITY_DECL)) |
1625 | 0 | return; |
1626 | | |
1627 | 0 | if ((node->type == XML_DOCUMENT_NODE) || |
1628 | 0 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
1629 | 0 | xmlNodeDumpOutput(output, node->doc, node, 0, 0, NULL); |
1630 | 0 | } else { |
1631 | 0 | xmlNodePtr copy; |
1632 | | |
1633 | | /* |
1634 | | * Create a copy to make sure that namespace declarations from |
1635 | | * ancestors are added. |
1636 | | */ |
1637 | 0 | copy = xmlDocCopyNode(node, node->doc, 1); |
1638 | 0 | if (copy == NULL) { |
1639 | 0 | xmlTextReaderErrMemory(reader); |
1640 | 0 | return; |
1641 | 0 | } |
1642 | | |
1643 | 0 | xmlNodeDumpOutput(output, copy->doc, copy, 0, 0, NULL); |
1644 | |
|
1645 | 0 | xmlFreeNode(copy); |
1646 | 0 | } |
1647 | 0 | } |
1648 | | |
1649 | | /** |
1650 | | * Reads the contents of the current node, including child nodes and markup. |
1651 | | * |
1652 | | * @param reader the xmlTextReader used |
1653 | | * @returns a string containing the XML content, or NULL if the current node |
1654 | | * is neither an element nor attribute, or has no child nodes. The |
1655 | | * string must be deallocated by the caller. |
1656 | | */ |
1657 | | xmlChar * |
1658 | | xmlTextReaderReadInnerXml(xmlTextReader *reader) |
1659 | 0 | { |
1660 | 0 | xmlOutputBufferPtr output; |
1661 | 0 | xmlNodePtr cur; |
1662 | 0 | xmlChar *ret; |
1663 | |
|
1664 | 0 | if (xmlTextReaderExpand(reader) == NULL) |
1665 | 0 | return(NULL); |
1666 | | |
1667 | 0 | if (reader->node == NULL) |
1668 | 0 | return(NULL); |
1669 | | |
1670 | 0 | output = xmlAllocOutputBuffer(NULL); |
1671 | 0 | if (output == NULL) { |
1672 | 0 | xmlTextReaderErrMemory(reader); |
1673 | 0 | return(NULL); |
1674 | 0 | } |
1675 | | |
1676 | 0 | for (cur = reader->node->children; cur != NULL; cur = cur->next) |
1677 | 0 | xmlTextReaderDumpCopy(reader, output, cur); |
1678 | |
|
1679 | 0 | if (output->error) |
1680 | 0 | xmlCtxtErrIO(reader->ctxt, output->error, NULL); |
1681 | |
|
1682 | 0 | ret = xmlBufDetach(output->buffer); |
1683 | 0 | xmlOutputBufferClose(output); |
1684 | |
|
1685 | 0 | return(ret); |
1686 | 0 | } |
1687 | | |
1688 | | /** |
1689 | | * Reads the contents of the current node, including child nodes and markup. |
1690 | | * |
1691 | | * @param reader the xmlTextReader used |
1692 | | * @returns a string containing the node and any XML content, or NULL if the |
1693 | | * current node cannot be serialized. The string must be deallocated |
1694 | | * by the caller. |
1695 | | */ |
1696 | | xmlChar * |
1697 | | xmlTextReaderReadOuterXml(xmlTextReader *reader) |
1698 | 0 | { |
1699 | 0 | xmlOutputBufferPtr output; |
1700 | 0 | xmlNodePtr node; |
1701 | 0 | xmlChar *ret; |
1702 | |
|
1703 | 0 | if (xmlTextReaderExpand(reader) == NULL) |
1704 | 0 | return(NULL); |
1705 | | |
1706 | 0 | node = reader->node; |
1707 | 0 | if (node == NULL) |
1708 | 0 | return(NULL); |
1709 | | |
1710 | 0 | output = xmlAllocOutputBuffer(NULL); |
1711 | 0 | if (output == NULL) { |
1712 | 0 | xmlTextReaderErrMemory(reader); |
1713 | 0 | return(NULL); |
1714 | 0 | } |
1715 | | |
1716 | 0 | xmlTextReaderDumpCopy(reader, output, node); |
1717 | 0 | if (output->error) |
1718 | 0 | xmlCtxtErrIO(reader->ctxt, output->error, NULL); |
1719 | |
|
1720 | 0 | ret = xmlBufDetach(output->buffer); |
1721 | 0 | xmlOutputBufferClose(output); |
1722 | |
|
1723 | 0 | return(ret); |
1724 | 0 | } |
1725 | | #endif |
1726 | | |
1727 | | /** |
1728 | | * Reads the contents of an element or a text node as a string. |
1729 | | * |
1730 | | * @param reader the xmlTextReader used |
1731 | | * @returns a string containing the contents of the non-empty Element or |
1732 | | * Text node (including CDATA sections), or NULL if the reader |
1733 | | * is positioned on any other type of node. |
1734 | | * The string must be deallocated by the caller. |
1735 | | */ |
1736 | | xmlChar * |
1737 | | xmlTextReaderReadString(xmlTextReader *reader) |
1738 | 0 | { |
1739 | 0 | xmlNodePtr node, cur; |
1740 | 0 | xmlBufPtr buf; |
1741 | 0 | xmlChar *ret; |
1742 | |
|
1743 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
1744 | 0 | return(NULL); |
1745 | | |
1746 | 0 | node = (reader->curnode != NULL) ? reader->curnode : reader->node; |
1747 | 0 | switch (node->type) { |
1748 | 0 | case XML_TEXT_NODE: |
1749 | 0 | case XML_CDATA_SECTION_NODE: |
1750 | 0 | break; |
1751 | 0 | case XML_ELEMENT_NODE: |
1752 | 0 | if ((xmlTextReaderDoExpand(reader) == -1) || |
1753 | 0 | (node->children == NULL)) |
1754 | 0 | return(NULL); |
1755 | 0 | break; |
1756 | 0 | default: |
1757 | 0 | return(NULL); |
1758 | 0 | } |
1759 | | |
1760 | 0 | buf = xmlBufCreate(50); |
1761 | 0 | if (buf == NULL) { |
1762 | 0 | xmlTextReaderErrMemory(reader); |
1763 | 0 | return(NULL); |
1764 | 0 | } |
1765 | | |
1766 | 0 | cur = node; |
1767 | 0 | while (cur != NULL) { |
1768 | 0 | switch (cur->type) { |
1769 | 0 | case XML_TEXT_NODE: |
1770 | 0 | case XML_CDATA_SECTION_NODE: |
1771 | 0 | xmlBufCat(buf, cur->content); |
1772 | 0 | break; |
1773 | | |
1774 | 0 | case XML_ELEMENT_NODE: |
1775 | 0 | if (cur->children != NULL) { |
1776 | 0 | cur = cur->children; |
1777 | 0 | continue; |
1778 | 0 | } |
1779 | 0 | break; |
1780 | | |
1781 | 0 | default: |
1782 | 0 | break; |
1783 | 0 | } |
1784 | | |
1785 | 0 | if (cur == node) |
1786 | 0 | goto done; |
1787 | | |
1788 | 0 | while (cur->next == NULL) { |
1789 | 0 | cur = cur->parent; |
1790 | 0 | if (cur == node) |
1791 | 0 | goto done; |
1792 | 0 | } |
1793 | 0 | cur = cur->next; |
1794 | 0 | } |
1795 | | |
1796 | 0 | done: |
1797 | 0 | ret = xmlBufDetach(buf); |
1798 | 0 | if (ret == NULL) |
1799 | 0 | xmlTextReaderErrMemory(reader); |
1800 | |
|
1801 | 0 | xmlBufFree(buf); |
1802 | 0 | return(ret); |
1803 | 0 | } |
1804 | | |
1805 | | /************************************************************************ |
1806 | | * * |
1807 | | * Operating on a preparsed tree * |
1808 | | * * |
1809 | | ************************************************************************/ |
1810 | | static int |
1811 | | xmlTextReaderNextTree(xmlTextReaderPtr reader) |
1812 | 0 | { |
1813 | 0 | if (reader == NULL) |
1814 | 0 | return(-1); |
1815 | | |
1816 | 0 | if (reader->state == XML_TEXTREADER_END) |
1817 | 0 | return(0); |
1818 | | |
1819 | 0 | if (reader->node == NULL) { |
1820 | 0 | if (reader->doc->children == NULL) { |
1821 | 0 | reader->state = XML_TEXTREADER_END; |
1822 | 0 | return(0); |
1823 | 0 | } |
1824 | | |
1825 | 0 | reader->node = reader->doc->children; |
1826 | 0 | reader->state = XML_TEXTREADER_START; |
1827 | 0 | return(1); |
1828 | 0 | } |
1829 | | |
1830 | 0 | if (reader->state != XML_TEXTREADER_BACKTRACK) { |
1831 | | /* Here removed traversal to child, because we want to skip the subtree, |
1832 | | replace with traversal to sibling to skip subtree */ |
1833 | 0 | if (reader->node->next != 0) { |
1834 | | /* Move to sibling if present,skipping sub-tree */ |
1835 | 0 | reader->node = reader->node->next; |
1836 | 0 | reader->state = XML_TEXTREADER_START; |
1837 | 0 | return(1); |
1838 | 0 | } |
1839 | | |
1840 | | /* if reader->node->next is NULL mean no subtree for current node, |
1841 | | so need to move to sibling of parent node if present */ |
1842 | 0 | reader->state = XML_TEXTREADER_BACKTRACK; |
1843 | | /* This will move to parent if present */ |
1844 | 0 | xmlTextReaderRead(reader); |
1845 | 0 | } |
1846 | | |
1847 | 0 | if (reader->node->next != 0) { |
1848 | 0 | reader->node = reader->node->next; |
1849 | 0 | reader->state = XML_TEXTREADER_START; |
1850 | 0 | return(1); |
1851 | 0 | } |
1852 | | |
1853 | 0 | if (reader->node->parent != 0) { |
1854 | 0 | if (reader->node->parent->type == XML_DOCUMENT_NODE) { |
1855 | 0 | reader->state = XML_TEXTREADER_END; |
1856 | 0 | return(0); |
1857 | 0 | } |
1858 | | |
1859 | 0 | reader->node = reader->node->parent; |
1860 | 0 | reader->depth--; |
1861 | 0 | reader->state = XML_TEXTREADER_BACKTRACK; |
1862 | | /* Repeat process to move to sibling of parent node if present */ |
1863 | 0 | xmlTextReaderNextTree(reader); |
1864 | 0 | } |
1865 | | |
1866 | 0 | reader->state = XML_TEXTREADER_END; |
1867 | |
|
1868 | 0 | return(1); |
1869 | 0 | } |
1870 | | |
1871 | | /** |
1872 | | * Moves the position of the current instance to the next node in |
1873 | | * the stream, exposing its properties. |
1874 | | * |
1875 | | * @param reader the xmlTextReader used |
1876 | | * @returns 1 if the node was read successfully, 0 if there is no more |
1877 | | * nodes to read, or -1 in case of error |
1878 | | */ |
1879 | | static int |
1880 | 0 | xmlTextReaderReadTree(xmlTextReaderPtr reader) { |
1881 | 0 | if (reader->state == XML_TEXTREADER_END) |
1882 | 0 | return(0); |
1883 | | |
1884 | 0 | next_node: |
1885 | 0 | if (reader->node == NULL) { |
1886 | 0 | if (reader->doc->children == NULL) { |
1887 | 0 | reader->state = XML_TEXTREADER_END; |
1888 | 0 | return(0); |
1889 | 0 | } |
1890 | | |
1891 | 0 | reader->node = reader->doc->children; |
1892 | 0 | reader->state = XML_TEXTREADER_START; |
1893 | 0 | goto found_node; |
1894 | 0 | } |
1895 | | |
1896 | 0 | if ((reader->state != XML_TEXTREADER_BACKTRACK) && |
1897 | 0 | (reader->node->type != XML_DTD_NODE) && |
1898 | 0 | (reader->node->type != XML_XINCLUDE_START) && |
1899 | 0 | (reader->node->type != XML_ENTITY_REF_NODE)) { |
1900 | 0 | if (reader->node->children != NULL) { |
1901 | 0 | reader->node = reader->node->children; |
1902 | 0 | reader->depth++; |
1903 | 0 | reader->state = XML_TEXTREADER_START; |
1904 | 0 | goto found_node; |
1905 | 0 | } |
1906 | | |
1907 | 0 | if (reader->node->type == XML_ATTRIBUTE_NODE) { |
1908 | 0 | reader->state = XML_TEXTREADER_BACKTRACK; |
1909 | 0 | goto found_node; |
1910 | 0 | } |
1911 | 0 | } |
1912 | | |
1913 | 0 | if (reader->node->next != NULL) { |
1914 | 0 | reader->node = reader->node->next; |
1915 | 0 | reader->state = XML_TEXTREADER_START; |
1916 | 0 | goto found_node; |
1917 | 0 | } |
1918 | | |
1919 | 0 | if (reader->node->parent != NULL) { |
1920 | 0 | if ((reader->node->parent->type == XML_DOCUMENT_NODE) || |
1921 | 0 | (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) { |
1922 | 0 | reader->state = XML_TEXTREADER_END; |
1923 | 0 | return(0); |
1924 | 0 | } |
1925 | | |
1926 | 0 | reader->node = reader->node->parent; |
1927 | 0 | reader->depth--; |
1928 | 0 | reader->state = XML_TEXTREADER_BACKTRACK; |
1929 | 0 | goto found_node; |
1930 | 0 | } |
1931 | | |
1932 | 0 | reader->state = XML_TEXTREADER_END; |
1933 | |
|
1934 | 0 | found_node: |
1935 | 0 | if ((reader->node->type == XML_XINCLUDE_START) || |
1936 | 0 | (reader->node->type == XML_XINCLUDE_END)) |
1937 | 0 | goto next_node; |
1938 | | |
1939 | 0 | return(1); |
1940 | 0 | } |
1941 | | |
1942 | | /** |
1943 | | * Skip to the node following the current one in document order while |
1944 | | * avoiding the subtree if any. |
1945 | | * Currently implemented only for Readers built on a document |
1946 | | * |
1947 | | * @param reader the xmlTextReader used |
1948 | | * @returns 1 if the node was read successfully, 0 if there is no more |
1949 | | * nodes to read, or -1 in case of error |
1950 | | */ |
1951 | | int |
1952 | 0 | xmlTextReaderNextSibling(xmlTextReader *reader) { |
1953 | 0 | if (reader == NULL) |
1954 | 0 | return(-1); |
1955 | 0 | if (reader->doc == NULL) { |
1956 | | /* TODO */ |
1957 | 0 | return(-1); |
1958 | 0 | } |
1959 | | |
1960 | 0 | if (reader->state == XML_TEXTREADER_END) |
1961 | 0 | return(0); |
1962 | | |
1963 | 0 | if (reader->node == NULL) |
1964 | 0 | return(xmlTextReaderNextTree(reader)); |
1965 | | |
1966 | 0 | if (reader->node->next != NULL) { |
1967 | 0 | reader->node = reader->node->next; |
1968 | 0 | reader->state = XML_TEXTREADER_START; |
1969 | 0 | return(1); |
1970 | 0 | } |
1971 | | |
1972 | 0 | return(0); |
1973 | 0 | } |
1974 | | |
1975 | | /************************************************************************ |
1976 | | * * |
1977 | | * Constructor and destructors * |
1978 | | * * |
1979 | | ************************************************************************/ |
1980 | | /** |
1981 | | * Create an xmlTextReader structure fed with `input` |
1982 | | * |
1983 | | * @param input the xmlParserInputBuffer used to read data |
1984 | | * @param URI the URI information for the source if available |
1985 | | * @returns the new xmlTextReader or NULL in case of error |
1986 | | */ |
1987 | | xmlTextReader * |
1988 | 0 | xmlNewTextReader(xmlParserInputBuffer *input, const char *URI) { |
1989 | 0 | xmlTextReaderPtr ret; |
1990 | |
|
1991 | 0 | if (input == NULL) |
1992 | 0 | return(NULL); |
1993 | 0 | ret = xmlMalloc(sizeof(xmlTextReader)); |
1994 | 0 | if (ret == NULL) |
1995 | 0 | return(NULL); |
1996 | 0 | memset(ret, 0, sizeof(xmlTextReader)); |
1997 | 0 | ret->doc = NULL; |
1998 | 0 | ret->entTab = NULL; |
1999 | 0 | ret->entMax = 0; |
2000 | 0 | ret->entNr = 0; |
2001 | 0 | ret->input = input; |
2002 | 0 | ret->buffer = xmlBufCreate(50); |
2003 | 0 | if (ret->buffer == NULL) { |
2004 | 0 | xmlFree(ret); |
2005 | 0 | return(NULL); |
2006 | 0 | } |
2007 | 0 | ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
2008 | 0 | if (ret->sax == NULL) { |
2009 | 0 | xmlBufFree(ret->buffer); |
2010 | 0 | xmlFree(ret); |
2011 | 0 | return(NULL); |
2012 | 0 | } |
2013 | 0 | xmlSAXVersion(ret->sax, 2); |
2014 | 0 | ret->startElement = ret->sax->startElement; |
2015 | 0 | ret->sax->startElement = xmlTextReaderStartElement; |
2016 | 0 | ret->endElement = ret->sax->endElement; |
2017 | 0 | ret->sax->endElement = xmlTextReaderEndElement; |
2018 | 0 | #ifdef LIBXML_SAX1_ENABLED |
2019 | 0 | if (ret->sax->initialized == XML_SAX2_MAGIC) { |
2020 | 0 | #endif /* LIBXML_SAX1_ENABLED */ |
2021 | 0 | ret->startElementNs = ret->sax->startElementNs; |
2022 | 0 | ret->sax->startElementNs = xmlTextReaderStartElementNs; |
2023 | 0 | ret->endElementNs = ret->sax->endElementNs; |
2024 | 0 | ret->sax->endElementNs = xmlTextReaderEndElementNs; |
2025 | 0 | #ifdef LIBXML_SAX1_ENABLED |
2026 | 0 | } else { |
2027 | 0 | ret->startElementNs = NULL; |
2028 | 0 | ret->endElementNs = NULL; |
2029 | 0 | } |
2030 | 0 | #endif /* LIBXML_SAX1_ENABLED */ |
2031 | 0 | ret->characters = ret->sax->characters; |
2032 | 0 | ret->sax->characters = xmlTextReaderCharacters; |
2033 | 0 | ret->sax->ignorableWhitespace = xmlTextReaderCharacters; |
2034 | 0 | ret->cdataBlock = ret->sax->cdataBlock; |
2035 | 0 | ret->sax->cdataBlock = xmlTextReaderCDataBlock; |
2036 | |
|
2037 | 0 | ret->mode = XML_TEXTREADER_MODE_INITIAL; |
2038 | 0 | ret->node = NULL; |
2039 | 0 | ret->curnode = NULL; |
2040 | 0 | if (xmlBufUse(ret->input->buffer) < 4) { |
2041 | 0 | xmlParserInputBufferRead(input, 4); |
2042 | 0 | } |
2043 | 0 | if (xmlBufUse(ret->input->buffer) >= 4) { |
2044 | 0 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, |
2045 | 0 | (const char *) xmlBufContent(ret->input->buffer), |
2046 | 0 | 4, URI); |
2047 | 0 | ret->base = 0; |
2048 | 0 | ret->cur = 4; |
2049 | 0 | } else { |
2050 | 0 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI); |
2051 | 0 | ret->base = 0; |
2052 | 0 | ret->cur = 0; |
2053 | 0 | } |
2054 | |
|
2055 | 0 | if (ret->ctxt == NULL) { |
2056 | 0 | xmlBufFree(ret->buffer); |
2057 | 0 | xmlFree(ret->sax); |
2058 | 0 | xmlFree(ret); |
2059 | 0 | return(NULL); |
2060 | 0 | } |
2061 | 0 | ret->ctxt->parseMode = XML_PARSE_READER; |
2062 | 0 | ret->ctxt->_private = ret; |
2063 | 0 | ret->ctxt->dictNames = 1; |
2064 | 0 | ret->allocs = XML_TEXTREADER_CTXT; |
2065 | | /* |
2066 | | * use the parser dictionary to allocate all elements and attributes names |
2067 | | */ |
2068 | 0 | ret->dict = ret->ctxt->dict; |
2069 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
2070 | 0 | ret->xinclude = 0; |
2071 | 0 | #endif |
2072 | 0 | #ifdef LIBXML_PATTERN_ENABLED |
2073 | 0 | ret->patternMax = 0; |
2074 | 0 | ret->patternTab = NULL; |
2075 | 0 | #endif |
2076 | 0 | return(ret); |
2077 | 0 | } |
2078 | | |
2079 | | /** |
2080 | | * Create an xmlTextReader structure fed with the resource at `URI` |
2081 | | * |
2082 | | * @param URI the URI of the resource to process |
2083 | | * @returns the new xmlTextReader or NULL in case of error |
2084 | | */ |
2085 | | xmlTextReader * |
2086 | 0 | xmlNewTextReaderFilename(const char *URI) { |
2087 | 0 | xmlParserInputBufferPtr input; |
2088 | 0 | xmlTextReaderPtr ret; |
2089 | |
|
2090 | 0 | if (xmlParserInputBufferCreateFilenameValue != NULL) { |
2091 | 0 | input = xmlParserInputBufferCreateFilenameValue(URI, |
2092 | 0 | XML_CHAR_ENCODING_NONE); |
2093 | 0 | if (input == NULL) { |
2094 | 0 | xmlTextReaderErr(XML_IO_ENOENT, "filaed to open %s", URI); |
2095 | 0 | return(NULL); |
2096 | 0 | } |
2097 | 0 | } else { |
2098 | 0 | xmlParserErrors code; |
2099 | | |
2100 | | /* |
2101 | | * TODO: Remove XML_INPUT_UNZIP |
2102 | | */ |
2103 | 0 | code = xmlParserInputBufferCreateUrl(URI, XML_CHAR_ENCODING_NONE, |
2104 | 0 | XML_INPUT_UNZIP, &input); |
2105 | 0 | if (code != XML_ERR_OK) { |
2106 | 0 | xmlTextReaderErr(code, "failed to open %s", URI); |
2107 | 0 | return(NULL); |
2108 | 0 | } |
2109 | 0 | } |
2110 | | |
2111 | 0 | ret = xmlNewTextReader(input, URI); |
2112 | 0 | if (ret == NULL) { |
2113 | 0 | xmlTextReaderErrMemory(NULL); |
2114 | 0 | xmlFreeParserInputBuffer(input); |
2115 | 0 | return(NULL); |
2116 | 0 | } |
2117 | 0 | ret->allocs |= XML_TEXTREADER_INPUT; |
2118 | 0 | return(ret); |
2119 | 0 | } |
2120 | | |
2121 | | /** |
2122 | | * Deallocate all the resources associated to the reader |
2123 | | * |
2124 | | * @param reader the xmlTextReader |
2125 | | */ |
2126 | | void |
2127 | 0 | xmlFreeTextReader(xmlTextReader *reader) { |
2128 | 0 | if (reader == NULL) |
2129 | 0 | return; |
2130 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
2131 | 0 | if (reader->rngSchemas != NULL) { |
2132 | 0 | xmlRelaxNGFree(reader->rngSchemas); |
2133 | 0 | reader->rngSchemas = NULL; |
2134 | 0 | } |
2135 | 0 | if (reader->rngValidCtxt != NULL) { |
2136 | 0 | if (! reader->rngPreserveCtxt) |
2137 | 0 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
2138 | 0 | reader->rngValidCtxt = NULL; |
2139 | 0 | } |
2140 | 0 | #endif |
2141 | 0 | #ifdef LIBXML_SCHEMAS_ENABLED |
2142 | 0 | if (reader->xsdPlug != NULL) { |
2143 | 0 | xmlSchemaSAXUnplug(reader->xsdPlug); |
2144 | 0 | reader->xsdPlug = NULL; |
2145 | 0 | } |
2146 | 0 | if (reader->xsdValidCtxt != NULL) { |
2147 | 0 | if (! reader->xsdPreserveCtxt) |
2148 | 0 | xmlSchemaFreeValidCtxt(reader->xsdValidCtxt); |
2149 | 0 | reader->xsdValidCtxt = NULL; |
2150 | 0 | } |
2151 | 0 | if (reader->xsdSchemas != NULL) { |
2152 | 0 | xmlSchemaFree(reader->xsdSchemas); |
2153 | 0 | reader->xsdSchemas = NULL; |
2154 | 0 | } |
2155 | 0 | #endif |
2156 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
2157 | 0 | if (reader->xincctxt != NULL) |
2158 | 0 | xmlXIncludeFreeContext(reader->xincctxt); |
2159 | 0 | #endif |
2160 | 0 | #ifdef LIBXML_PATTERN_ENABLED |
2161 | 0 | if (reader->patternTab != NULL) { |
2162 | 0 | int i; |
2163 | 0 | for (i = 0;i < reader->patternNr;i++) { |
2164 | 0 | if (reader->patternTab[i] != NULL) |
2165 | 0 | xmlFreePattern(reader->patternTab[i]); |
2166 | 0 | } |
2167 | 0 | xmlFree(reader->patternTab); |
2168 | 0 | } |
2169 | 0 | #endif |
2170 | 0 | if (reader->mode != XML_TEXTREADER_MODE_CLOSED) |
2171 | 0 | xmlTextReaderClose(reader); |
2172 | 0 | if (reader->ctxt != NULL) { |
2173 | 0 | if (reader->dict == reader->ctxt->dict) |
2174 | 0 | reader->dict = NULL; |
2175 | 0 | if (reader->allocs & XML_TEXTREADER_CTXT) |
2176 | 0 | xmlFreeParserCtxt(reader->ctxt); |
2177 | 0 | } |
2178 | 0 | if (reader->sax != NULL) |
2179 | 0 | xmlFree(reader->sax); |
2180 | 0 | if (reader->buffer != NULL) |
2181 | 0 | xmlBufFree(reader->buffer); |
2182 | 0 | if (reader->entTab != NULL) |
2183 | 0 | xmlFree(reader->entTab); |
2184 | 0 | if (reader->dict != NULL) |
2185 | 0 | xmlDictFree(reader->dict); |
2186 | 0 | xmlFree(reader); |
2187 | 0 | } |
2188 | | |
2189 | | /************************************************************************ |
2190 | | * * |
2191 | | * Methods for XmlTextReader * |
2192 | | * * |
2193 | | ************************************************************************/ |
2194 | | |
2195 | | /** |
2196 | | * This method releases any resources allocated by the current instance |
2197 | | * changes the state to Closed and close any underlying input. |
2198 | | * |
2199 | | * @param reader the xmlTextReader used |
2200 | | * @returns 0 or -1 in case of error |
2201 | | */ |
2202 | | int |
2203 | 0 | xmlTextReaderClose(xmlTextReader *reader) { |
2204 | 0 | if (reader == NULL) |
2205 | 0 | return(-1); |
2206 | 0 | reader->node = NULL; |
2207 | 0 | reader->curnode = NULL; |
2208 | 0 | reader->mode = XML_TEXTREADER_MODE_CLOSED; |
2209 | 0 | if (reader->faketext != NULL) { |
2210 | 0 | xmlFreeNode(reader->faketext); |
2211 | 0 | reader->faketext = NULL; |
2212 | 0 | } |
2213 | 0 | if (reader->ctxt != NULL) { |
2214 | 0 | #ifdef LIBXML_VALID_ENABLED |
2215 | 0 | if ((reader->ctxt->vctxt.vstateTab != NULL) && |
2216 | 0 | (reader->ctxt->vctxt.vstateMax > 0)){ |
2217 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
2218 | 0 | while (reader->ctxt->vctxt.vstateNr > 0) |
2219 | 0 | xmlValidatePopElement(&reader->ctxt->vctxt, NULL, NULL, NULL); |
2220 | 0 | #endif /* LIBXML_REGEXP_ENABLED */ |
2221 | 0 | xmlFree(reader->ctxt->vctxt.vstateTab); |
2222 | 0 | reader->ctxt->vctxt.vstateTab = NULL; |
2223 | 0 | reader->ctxt->vctxt.vstateMax = 0; |
2224 | 0 | } |
2225 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
2226 | 0 | xmlStopParser(reader->ctxt); |
2227 | 0 | if (reader->ctxt->myDoc != NULL) { |
2228 | 0 | if (reader->preserve == 0) |
2229 | 0 | xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc); |
2230 | 0 | reader->ctxt->myDoc = NULL; |
2231 | 0 | } |
2232 | 0 | } |
2233 | 0 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) { |
2234 | 0 | xmlFreeParserInputBuffer(reader->input); |
2235 | 0 | reader->allocs -= XML_TEXTREADER_INPUT; |
2236 | 0 | } |
2237 | 0 | return(0); |
2238 | 0 | } |
2239 | | |
2240 | | /** |
2241 | | * Provides the value of the attribute with the specified index relative |
2242 | | * to the containing element. |
2243 | | * |
2244 | | * @param reader the xmlTextReader used |
2245 | | * @param no the zero-based index of the attribute relative to the containing element |
2246 | | * @returns a string containing the value of the specified attribute, or NULL |
2247 | | * in case of error. The string must be deallocated by the caller. |
2248 | | */ |
2249 | | xmlChar * |
2250 | 0 | xmlTextReaderGetAttributeNo(xmlTextReader *reader, int no) { |
2251 | 0 | xmlChar *ret; |
2252 | 0 | int i; |
2253 | 0 | xmlAttrPtr cur; |
2254 | 0 | xmlNsPtr ns; |
2255 | |
|
2256 | 0 | if (reader == NULL) |
2257 | 0 | return(NULL); |
2258 | 0 | if (reader->node == NULL) |
2259 | 0 | return(NULL); |
2260 | 0 | if (reader->curnode != NULL) |
2261 | 0 | return(NULL); |
2262 | | /* TODO: handle the xmlDecl */ |
2263 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2264 | 0 | return(NULL); |
2265 | | |
2266 | 0 | ns = reader->node->nsDef; |
2267 | 0 | for (i = 0;(i < no) && (ns != NULL);i++) { |
2268 | 0 | ns = ns->next; |
2269 | 0 | } |
2270 | 0 | if (ns != NULL) |
2271 | 0 | return(readerStrdup(reader, ns->href)); |
2272 | | |
2273 | 0 | cur = reader->node->properties; |
2274 | 0 | if (cur == NULL) |
2275 | 0 | return(NULL); |
2276 | 0 | for (;i < no;i++) { |
2277 | 0 | cur = cur->next; |
2278 | 0 | if (cur == NULL) |
2279 | 0 | return(NULL); |
2280 | 0 | } |
2281 | | /* TODO walk the DTD if present */ |
2282 | | |
2283 | 0 | if (cur->children == NULL) |
2284 | 0 | return(NULL); |
2285 | 0 | ret = xmlNodeListGetString(reader->node->doc, cur->children, 1); |
2286 | 0 | if (ret == NULL) |
2287 | 0 | xmlTextReaderErrMemory(reader); |
2288 | 0 | return(ret); |
2289 | 0 | } |
2290 | | |
2291 | | /** |
2292 | | * Provides the value of the attribute with the specified qualified name. |
2293 | | * |
2294 | | * @param reader the xmlTextReader used |
2295 | | * @param name the qualified name of the attribute. |
2296 | | * @returns a string containing the value of the specified attribute, or NULL |
2297 | | * in case of error. The string must be deallocated by the caller. |
2298 | | */ |
2299 | | xmlChar * |
2300 | 0 | xmlTextReaderGetAttribute(xmlTextReader *reader, const xmlChar *name) { |
2301 | 0 | xmlChar *prefix = NULL; |
2302 | 0 | const xmlChar *localname; |
2303 | 0 | xmlNsPtr ns; |
2304 | 0 | xmlChar *ret = NULL; |
2305 | 0 | int result; |
2306 | |
|
2307 | 0 | if ((reader == NULL) || (name == NULL)) |
2308 | 0 | return(NULL); |
2309 | 0 | if (reader->node == NULL) |
2310 | 0 | return(NULL); |
2311 | 0 | if (reader->curnode != NULL) |
2312 | 0 | return(NULL); |
2313 | | |
2314 | | /* TODO: handle the xmlDecl */ |
2315 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2316 | 0 | return(NULL); |
2317 | | |
2318 | 0 | localname = xmlSplitQName4(name, &prefix); |
2319 | 0 | if (localname == NULL) { |
2320 | 0 | xmlTextReaderErrMemory(reader); |
2321 | 0 | return(NULL); |
2322 | 0 | } |
2323 | 0 | if (prefix == NULL) { |
2324 | | /* |
2325 | | * Namespace default decl |
2326 | | */ |
2327 | 0 | if (xmlStrEqual(name, BAD_CAST "xmlns")) { |
2328 | 0 | ns = reader->node->nsDef; |
2329 | 0 | while (ns != NULL) { |
2330 | 0 | if (ns->prefix == NULL) { |
2331 | 0 | return(readerStrdup(reader, ns->href)); |
2332 | 0 | } |
2333 | 0 | ns = ns->next; |
2334 | 0 | } |
2335 | 0 | return NULL; |
2336 | 0 | } |
2337 | | |
2338 | 0 | result = xmlNodeGetAttrValue(reader->node, name, NULL, &ret); |
2339 | 0 | if (result < 0) |
2340 | 0 | xmlTextReaderErrMemory(reader); |
2341 | 0 | return(ret); |
2342 | 0 | } |
2343 | | |
2344 | | /* |
2345 | | * Namespace default decl |
2346 | | */ |
2347 | 0 | if (xmlStrEqual(prefix, BAD_CAST "xmlns")) { |
2348 | 0 | ns = reader->node->nsDef; |
2349 | 0 | while (ns != NULL) { |
2350 | 0 | if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) { |
2351 | 0 | ret = readerStrdup(reader, ns->href); |
2352 | 0 | break; |
2353 | 0 | } |
2354 | 0 | ns = ns->next; |
2355 | 0 | } |
2356 | 0 | } else { |
2357 | 0 | result = xmlSearchNsSafe(reader->node, prefix, &ns); |
2358 | 0 | if (result < 0) |
2359 | 0 | xmlTextReaderErrMemory(reader); |
2360 | 0 | if (ns != NULL) { |
2361 | 0 | result = xmlNodeGetAttrValue(reader->node, localname, ns->href, |
2362 | 0 | &ret); |
2363 | 0 | if (result < 0) |
2364 | 0 | xmlTextReaderErrMemory(reader); |
2365 | 0 | } |
2366 | 0 | } |
2367 | |
|
2368 | 0 | if (prefix != NULL) |
2369 | 0 | xmlFree(prefix); |
2370 | 0 | return(ret); |
2371 | 0 | } |
2372 | | |
2373 | | |
2374 | | /** |
2375 | | * Provides the value of the specified attribute |
2376 | | * |
2377 | | * @param reader the xmlTextReader used |
2378 | | * @param localName the local name of the attribute. |
2379 | | * @param namespaceURI the namespace URI of the attribute. |
2380 | | * @returns a string containing the value of the specified attribute, or NULL |
2381 | | * in case of error. The string must be deallocated by the caller. |
2382 | | */ |
2383 | | xmlChar * |
2384 | | xmlTextReaderGetAttributeNs(xmlTextReader *reader, const xmlChar *localName, |
2385 | 0 | const xmlChar *namespaceURI) { |
2386 | 0 | xmlChar *ret = NULL; |
2387 | 0 | xmlChar *prefix = NULL; |
2388 | 0 | xmlNsPtr ns; |
2389 | 0 | int result; |
2390 | |
|
2391 | 0 | if ((reader == NULL) || (localName == NULL)) |
2392 | 0 | return(NULL); |
2393 | 0 | if (reader->node == NULL) |
2394 | 0 | return(NULL); |
2395 | 0 | if (reader->curnode != NULL) |
2396 | 0 | return(NULL); |
2397 | | |
2398 | | /* TODO: handle the xmlDecl */ |
2399 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2400 | 0 | return(NULL); |
2401 | | |
2402 | 0 | if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) { |
2403 | 0 | if (! xmlStrEqual(localName, BAD_CAST "xmlns")) { |
2404 | 0 | prefix = BAD_CAST localName; |
2405 | 0 | } |
2406 | 0 | ns = reader->node->nsDef; |
2407 | 0 | while (ns != NULL) { |
2408 | 0 | if ((prefix == NULL && ns->prefix == NULL) || |
2409 | 0 | ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) { |
2410 | 0 | return readerStrdup(reader, ns->href); |
2411 | 0 | } |
2412 | 0 | ns = ns->next; |
2413 | 0 | } |
2414 | 0 | return NULL; |
2415 | 0 | } |
2416 | | |
2417 | 0 | result = xmlNodeGetAttrValue(reader->node, localName, namespaceURI, &ret); |
2418 | 0 | if (result < 0) |
2419 | 0 | xmlTextReaderErrMemory(reader); |
2420 | |
|
2421 | 0 | return(ret); |
2422 | 0 | } |
2423 | | |
2424 | | /** |
2425 | | * Method to get the remainder of the buffered XML. this method stops the |
2426 | | * parser, set its state to End Of File and return the input stream with |
2427 | | * what is left that the parser did not use. |
2428 | | * |
2429 | | * The implementation is not good, the parser certainly progressed past |
2430 | | * what's left in reader->input, and there is an allocation problem. Best |
2431 | | * would be to rewrite it differently. |
2432 | | * |
2433 | | * @param reader the xmlTextReader used |
2434 | | * @returns the xmlParserInputBuffer attached to the XML or NULL |
2435 | | * in case of error. |
2436 | | */ |
2437 | | xmlParserInputBuffer * |
2438 | 0 | xmlTextReaderGetRemainder(xmlTextReader *reader) { |
2439 | 0 | xmlParserInputBufferPtr ret = NULL; |
2440 | |
|
2441 | 0 | if (reader == NULL) |
2442 | 0 | return(NULL); |
2443 | 0 | if (reader->node == NULL) |
2444 | 0 | return(NULL); |
2445 | | |
2446 | 0 | reader->node = NULL; |
2447 | 0 | reader->curnode = NULL; |
2448 | 0 | reader->mode = XML_TEXTREADER_MODE_EOF; |
2449 | 0 | if (reader->ctxt != NULL) { |
2450 | 0 | xmlStopParser(reader->ctxt); |
2451 | 0 | if (reader->ctxt->myDoc != NULL) { |
2452 | 0 | if (reader->preserve == 0) |
2453 | 0 | xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc); |
2454 | 0 | reader->ctxt->myDoc = NULL; |
2455 | 0 | } |
2456 | 0 | } |
2457 | 0 | if (reader->allocs & XML_TEXTREADER_INPUT) { |
2458 | 0 | ret = reader->input; |
2459 | 0 | reader->input = NULL; |
2460 | 0 | reader->allocs -= XML_TEXTREADER_INPUT; |
2461 | 0 | } else { |
2462 | | /* |
2463 | | * Hum, one may need to duplicate the data structure because |
2464 | | * without reference counting the input may be freed twice: |
2465 | | * - by the layer which allocated it. |
2466 | | * - by the layer to which would have been returned to. |
2467 | | */ |
2468 | 0 | return(NULL); |
2469 | 0 | } |
2470 | 0 | return(ret); |
2471 | 0 | } |
2472 | | |
2473 | | /** |
2474 | | * Resolves a namespace prefix in the scope of the current element. |
2475 | | * |
2476 | | * @param reader the xmlTextReader used |
2477 | | * @param prefix the prefix whose namespace URI is to be resolved. To return |
2478 | | * the default namespace, specify NULL |
2479 | | * @returns a string containing the namespace URI to which the prefix maps |
2480 | | * or NULL in case of error. The string must be deallocated by the caller. |
2481 | | */ |
2482 | | xmlChar * |
2483 | 0 | xmlTextReaderLookupNamespace(xmlTextReader *reader, const xmlChar *prefix) { |
2484 | 0 | xmlNsPtr ns; |
2485 | 0 | int result; |
2486 | |
|
2487 | 0 | if (reader == NULL) |
2488 | 0 | return(NULL); |
2489 | 0 | if (reader->node == NULL) |
2490 | 0 | return(NULL); |
2491 | | |
2492 | 0 | result = xmlSearchNsSafe(reader->node, prefix, &ns); |
2493 | 0 | if (result < 0) { |
2494 | 0 | xmlTextReaderErrMemory(reader); |
2495 | 0 | return(NULL); |
2496 | 0 | } |
2497 | 0 | if (ns == NULL) |
2498 | 0 | return(NULL); |
2499 | 0 | return(readerStrdup(reader, ns->href)); |
2500 | 0 | } |
2501 | | |
2502 | | /** |
2503 | | * Moves the position of the current instance to the attribute with |
2504 | | * the specified index relative to the containing element. |
2505 | | * |
2506 | | * @param reader the xmlTextReader used |
2507 | | * @param no the zero-based index of the attribute relative to the containing |
2508 | | * element. |
2509 | | * @returns 1 in case of success, -1 in case of error, 0 if not found |
2510 | | */ |
2511 | | int |
2512 | 0 | xmlTextReaderMoveToAttributeNo(xmlTextReader *reader, int no) { |
2513 | 0 | int i; |
2514 | 0 | xmlAttrPtr cur; |
2515 | 0 | xmlNsPtr ns; |
2516 | |
|
2517 | 0 | if (reader == NULL) |
2518 | 0 | return(-1); |
2519 | 0 | if (reader->node == NULL) |
2520 | 0 | return(-1); |
2521 | | /* TODO: handle the xmlDecl */ |
2522 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2523 | 0 | return(-1); |
2524 | | |
2525 | 0 | reader->curnode = NULL; |
2526 | |
|
2527 | 0 | ns = reader->node->nsDef; |
2528 | 0 | for (i = 0;(i < no) && (ns != NULL);i++) { |
2529 | 0 | ns = ns->next; |
2530 | 0 | } |
2531 | 0 | if (ns != NULL) { |
2532 | 0 | reader->curnode = (xmlNodePtr) ns; |
2533 | 0 | return(1); |
2534 | 0 | } |
2535 | | |
2536 | 0 | cur = reader->node->properties; |
2537 | 0 | if (cur == NULL) |
2538 | 0 | return(0); |
2539 | 0 | for (;i < no;i++) { |
2540 | 0 | cur = cur->next; |
2541 | 0 | if (cur == NULL) |
2542 | 0 | return(0); |
2543 | 0 | } |
2544 | | /* TODO walk the DTD if present */ |
2545 | | |
2546 | 0 | reader->curnode = (xmlNodePtr) cur; |
2547 | 0 | return(1); |
2548 | 0 | } |
2549 | | |
2550 | | /** |
2551 | | * Moves the position of the current instance to the attribute with |
2552 | | * the specified qualified name. |
2553 | | * |
2554 | | * @param reader the xmlTextReader used |
2555 | | * @param name the qualified name of the attribute. |
2556 | | * @returns 1 in case of success, -1 in case of error, 0 if not found |
2557 | | */ |
2558 | | int |
2559 | 0 | xmlTextReaderMoveToAttribute(xmlTextReader *reader, const xmlChar *name) { |
2560 | 0 | xmlChar *prefix = NULL; |
2561 | 0 | const xmlChar *localname; |
2562 | 0 | xmlNsPtr ns; |
2563 | 0 | xmlAttrPtr prop; |
2564 | |
|
2565 | 0 | if ((reader == NULL) || (name == NULL)) |
2566 | 0 | return(-1); |
2567 | 0 | if (reader->node == NULL) |
2568 | 0 | return(-1); |
2569 | | |
2570 | | /* TODO: handle the xmlDecl */ |
2571 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2572 | 0 | return(0); |
2573 | | |
2574 | 0 | localname = xmlSplitQName4(name, &prefix); |
2575 | 0 | if (localname == NULL) { |
2576 | 0 | xmlTextReaderErrMemory(reader); |
2577 | 0 | return(-1); |
2578 | 0 | } |
2579 | 0 | if (prefix == NULL) { |
2580 | | /* |
2581 | | * Namespace default decl |
2582 | | */ |
2583 | 0 | if (xmlStrEqual(name, BAD_CAST "xmlns")) { |
2584 | 0 | ns = reader->node->nsDef; |
2585 | 0 | while (ns != NULL) { |
2586 | 0 | if (ns->prefix == NULL) { |
2587 | 0 | reader->curnode = (xmlNodePtr) ns; |
2588 | 0 | return(1); |
2589 | 0 | } |
2590 | 0 | ns = ns->next; |
2591 | 0 | } |
2592 | 0 | return(0); |
2593 | 0 | } |
2594 | | |
2595 | 0 | prop = reader->node->properties; |
2596 | 0 | while (prop != NULL) { |
2597 | | /* |
2598 | | * One need to have |
2599 | | * - same attribute names |
2600 | | * - and the attribute carrying that namespace |
2601 | | */ |
2602 | 0 | if ((xmlStrEqual(prop->name, name)) && |
2603 | 0 | ((prop->ns == NULL) || (prop->ns->prefix == NULL))) { |
2604 | 0 | reader->curnode = (xmlNodePtr) prop; |
2605 | 0 | return(1); |
2606 | 0 | } |
2607 | 0 | prop = prop->next; |
2608 | 0 | } |
2609 | 0 | return(0); |
2610 | 0 | } |
2611 | | |
2612 | | /* |
2613 | | * Namespace default decl |
2614 | | */ |
2615 | 0 | if (xmlStrEqual(prefix, BAD_CAST "xmlns")) { |
2616 | 0 | ns = reader->node->nsDef; |
2617 | 0 | while (ns != NULL) { |
2618 | 0 | if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) { |
2619 | 0 | reader->curnode = (xmlNodePtr) ns; |
2620 | 0 | goto found; |
2621 | 0 | } |
2622 | 0 | ns = ns->next; |
2623 | 0 | } |
2624 | 0 | goto not_found; |
2625 | 0 | } |
2626 | 0 | prop = reader->node->properties; |
2627 | 0 | while (prop != NULL) { |
2628 | | /* |
2629 | | * One need to have |
2630 | | * - same attribute names |
2631 | | * - and the attribute carrying that namespace |
2632 | | */ |
2633 | 0 | if ((xmlStrEqual(prop->name, localname)) && |
2634 | 0 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) { |
2635 | 0 | reader->curnode = (xmlNodePtr) prop; |
2636 | 0 | goto found; |
2637 | 0 | } |
2638 | 0 | prop = prop->next; |
2639 | 0 | } |
2640 | 0 | not_found: |
2641 | 0 | if (prefix != NULL) |
2642 | 0 | xmlFree(prefix); |
2643 | 0 | return(0); |
2644 | | |
2645 | 0 | found: |
2646 | 0 | if (prefix != NULL) |
2647 | 0 | xmlFree(prefix); |
2648 | 0 | return(1); |
2649 | 0 | } |
2650 | | |
2651 | | /** |
2652 | | * Moves the position of the current instance to the attribute with the |
2653 | | * specified local name and namespace URI. |
2654 | | * |
2655 | | * @param reader the xmlTextReader used |
2656 | | * @param localName the local name of the attribute. |
2657 | | * @param namespaceURI the namespace URI of the attribute. |
2658 | | * @returns 1 in case of success, -1 in case of error, 0 if not found |
2659 | | */ |
2660 | | int |
2661 | | xmlTextReaderMoveToAttributeNs(xmlTextReader *reader, |
2662 | 0 | const xmlChar *localName, const xmlChar *namespaceURI) { |
2663 | 0 | xmlAttrPtr prop; |
2664 | 0 | xmlNodePtr node; |
2665 | 0 | xmlNsPtr ns; |
2666 | 0 | xmlChar *prefix = NULL; |
2667 | |
|
2668 | 0 | if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL)) |
2669 | 0 | return(-1); |
2670 | 0 | if (reader->node == NULL) |
2671 | 0 | return(-1); |
2672 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2673 | 0 | return(0); |
2674 | 0 | node = reader->node; |
2675 | |
|
2676 | 0 | if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) { |
2677 | 0 | if (! xmlStrEqual(localName, BAD_CAST "xmlns")) { |
2678 | 0 | prefix = BAD_CAST localName; |
2679 | 0 | } |
2680 | 0 | ns = reader->node->nsDef; |
2681 | 0 | while (ns != NULL) { |
2682 | 0 | if ((prefix == NULL && ns->prefix == NULL) || |
2683 | 0 | ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) { |
2684 | 0 | reader->curnode = (xmlNodePtr) ns; |
2685 | 0 | return(1); |
2686 | 0 | } |
2687 | 0 | ns = ns->next; |
2688 | 0 | } |
2689 | 0 | return(0); |
2690 | 0 | } |
2691 | | |
2692 | 0 | prop = node->properties; |
2693 | 0 | while (prop != NULL) { |
2694 | | /* |
2695 | | * One need to have |
2696 | | * - same attribute names |
2697 | | * - and the attribute carrying that namespace |
2698 | | */ |
2699 | 0 | if (xmlStrEqual(prop->name, localName) && |
2700 | 0 | ((prop->ns != NULL) && |
2701 | 0 | (xmlStrEqual(prop->ns->href, namespaceURI)))) { |
2702 | 0 | reader->curnode = (xmlNodePtr) prop; |
2703 | 0 | return(1); |
2704 | 0 | } |
2705 | 0 | prop = prop->next; |
2706 | 0 | } |
2707 | 0 | return(0); |
2708 | 0 | } |
2709 | | |
2710 | | /** |
2711 | | * Moves the position of the current instance to the first attribute |
2712 | | * associated with the current node. |
2713 | | * |
2714 | | * @param reader the xmlTextReader used |
2715 | | * @returns 1 in case of success, -1 in case of error, 0 if not found |
2716 | | */ |
2717 | | int |
2718 | 0 | xmlTextReaderMoveToFirstAttribute(xmlTextReader *reader) { |
2719 | 0 | if (reader == NULL) |
2720 | 0 | return(-1); |
2721 | 0 | if (reader->node == NULL) |
2722 | 0 | return(-1); |
2723 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2724 | 0 | return(0); |
2725 | | |
2726 | 0 | if (reader->node->nsDef != NULL) { |
2727 | 0 | reader->curnode = (xmlNodePtr) reader->node->nsDef; |
2728 | 0 | return(1); |
2729 | 0 | } |
2730 | 0 | if (reader->node->properties != NULL) { |
2731 | 0 | reader->curnode = (xmlNodePtr) reader->node->properties; |
2732 | 0 | return(1); |
2733 | 0 | } |
2734 | 0 | return(0); |
2735 | 0 | } |
2736 | | |
2737 | | /** |
2738 | | * Moves the position of the current instance to the next attribute |
2739 | | * associated with the current node. |
2740 | | * |
2741 | | * @param reader the xmlTextReader used |
2742 | | * @returns 1 in case of success, -1 in case of error, 0 if not found |
2743 | | */ |
2744 | | int |
2745 | 0 | xmlTextReaderMoveToNextAttribute(xmlTextReader *reader) { |
2746 | 0 | if (reader == NULL) |
2747 | 0 | return(-1); |
2748 | 0 | if (reader->node == NULL) |
2749 | 0 | return(-1); |
2750 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2751 | 0 | return(0); |
2752 | 0 | if (reader->curnode == NULL) |
2753 | 0 | return(xmlTextReaderMoveToFirstAttribute(reader)); |
2754 | | |
2755 | 0 | if (reader->curnode->type == XML_NAMESPACE_DECL) { |
2756 | 0 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
2757 | 0 | if (ns->next != NULL) { |
2758 | 0 | reader->curnode = (xmlNodePtr) ns->next; |
2759 | 0 | return(1); |
2760 | 0 | } |
2761 | 0 | if (reader->node->properties != NULL) { |
2762 | 0 | reader->curnode = (xmlNodePtr) reader->node->properties; |
2763 | 0 | return(1); |
2764 | 0 | } |
2765 | 0 | return(0); |
2766 | 0 | } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) && |
2767 | 0 | (reader->curnode->next != NULL)) { |
2768 | 0 | reader->curnode = reader->curnode->next; |
2769 | 0 | return(1); |
2770 | 0 | } |
2771 | 0 | return(0); |
2772 | 0 | } |
2773 | | |
2774 | | /** |
2775 | | * Moves the position of the current instance to the node that |
2776 | | * contains the current Attribute node. |
2777 | | * |
2778 | | * @param reader the xmlTextReader used |
2779 | | * @returns 1 in case of success, -1 in case of error, 0 if not moved |
2780 | | */ |
2781 | | int |
2782 | 0 | xmlTextReaderMoveToElement(xmlTextReader *reader) { |
2783 | 0 | if (reader == NULL) |
2784 | 0 | return(-1); |
2785 | 0 | if (reader->node == NULL) |
2786 | 0 | return(-1); |
2787 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
2788 | 0 | return(0); |
2789 | 0 | if (reader->curnode != NULL) { |
2790 | 0 | reader->curnode = NULL; |
2791 | 0 | return(1); |
2792 | 0 | } |
2793 | 0 | return(0); |
2794 | 0 | } |
2795 | | |
2796 | | /** |
2797 | | * Parses an attribute value into one or more Text and EntityReference nodes. |
2798 | | * |
2799 | | * @param reader the xmlTextReader used |
2800 | | * @returns 1 in case of success, 0 if the reader was not positioned on an |
2801 | | * attribute node or all the attribute values have been read, or -1 |
2802 | | * in case of error. |
2803 | | */ |
2804 | | int |
2805 | 0 | xmlTextReaderReadAttributeValue(xmlTextReader *reader) { |
2806 | 0 | if (reader == NULL) |
2807 | 0 | return(-1); |
2808 | 0 | if (reader->node == NULL) |
2809 | 0 | return(-1); |
2810 | 0 | if (reader->curnode == NULL) |
2811 | 0 | return(0); |
2812 | 0 | if (reader->curnode->type == XML_ATTRIBUTE_NODE) { |
2813 | 0 | if (reader->curnode->children == NULL) |
2814 | 0 | return(0); |
2815 | 0 | reader->curnode = reader->curnode->children; |
2816 | 0 | } else if (reader->curnode->type == XML_NAMESPACE_DECL) { |
2817 | 0 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
2818 | |
|
2819 | 0 | if (reader->faketext == NULL) { |
2820 | 0 | reader->faketext = xmlNewDocText(reader->node->doc, |
2821 | 0 | ns->href); |
2822 | 0 | if (reader->faketext == NULL) { |
2823 | 0 | xmlTextReaderErrMemory(reader); |
2824 | 0 | return(-1); |
2825 | 0 | } |
2826 | 0 | } else { |
2827 | 0 | if ((reader->faketext->content != NULL) && |
2828 | 0 | (reader->faketext->content != |
2829 | 0 | (xmlChar *) &(reader->faketext->properties))) |
2830 | 0 | xmlFree(reader->faketext->content); |
2831 | 0 | if (ns->href == NULL) { |
2832 | 0 | reader->faketext->content = NULL; |
2833 | 0 | } else { |
2834 | 0 | reader->faketext->content = xmlStrdup(ns->href); |
2835 | 0 | if (reader->faketext->content == NULL) { |
2836 | 0 | xmlTextReaderErrMemory(reader); |
2837 | 0 | return(-1); |
2838 | 0 | } |
2839 | 0 | } |
2840 | 0 | } |
2841 | 0 | reader->curnode = reader->faketext; |
2842 | 0 | } else { |
2843 | 0 | if (reader->curnode->next == NULL) |
2844 | 0 | return(0); |
2845 | 0 | reader->curnode = reader->curnode->next; |
2846 | 0 | } |
2847 | 0 | return(1); |
2848 | 0 | } |
2849 | | |
2850 | | /** |
2851 | | * Determine the encoding of the document being read. |
2852 | | * |
2853 | | * @param reader the xmlTextReader used |
2854 | | * @returns a string containing the encoding of the document or NULL in |
2855 | | * case of error. The string is deallocated with the reader. |
2856 | | */ |
2857 | | const xmlChar * |
2858 | 0 | xmlTextReaderConstEncoding(xmlTextReader *reader) { |
2859 | 0 | const xmlChar *encoding = NULL; |
2860 | |
|
2861 | 0 | if (reader == NULL) |
2862 | 0 | return(NULL); |
2863 | | |
2864 | 0 | if (reader->ctxt != NULL) |
2865 | 0 | encoding = xmlGetActualEncoding(reader->ctxt); |
2866 | 0 | else if (reader->doc != NULL) |
2867 | 0 | encoding = reader->doc->encoding; |
2868 | |
|
2869 | 0 | return(constString(reader, encoding)); |
2870 | 0 | } |
2871 | | |
2872 | | |
2873 | | /************************************************************************ |
2874 | | * * |
2875 | | * Access API to the current node * |
2876 | | * * |
2877 | | ************************************************************************/ |
2878 | | /** |
2879 | | * Provides the number of attributes of the current node |
2880 | | * |
2881 | | * @param reader the xmlTextReader used |
2882 | | * @returns 0 i no attributes, -1 in case of error or the attribute count |
2883 | | */ |
2884 | | int |
2885 | 0 | xmlTextReaderAttributeCount(xmlTextReader *reader) { |
2886 | 0 | int ret; |
2887 | 0 | xmlAttrPtr attr; |
2888 | 0 | xmlNsPtr ns; |
2889 | 0 | xmlNodePtr node; |
2890 | |
|
2891 | 0 | if (reader == NULL) |
2892 | 0 | return(-1); |
2893 | 0 | if (reader->node == NULL) |
2894 | 0 | return(0); |
2895 | | |
2896 | 0 | if (reader->curnode != NULL) |
2897 | 0 | node = reader->curnode; |
2898 | 0 | else |
2899 | 0 | node = reader->node; |
2900 | |
|
2901 | 0 | if (node->type != XML_ELEMENT_NODE) |
2902 | 0 | return(0); |
2903 | 0 | if ((reader->state == XML_TEXTREADER_END) || |
2904 | 0 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
2905 | 0 | return(0); |
2906 | 0 | ret = 0; |
2907 | 0 | attr = node->properties; |
2908 | 0 | while (attr != NULL) { |
2909 | 0 | ret++; |
2910 | 0 | attr = attr->next; |
2911 | 0 | } |
2912 | 0 | ns = node->nsDef; |
2913 | 0 | while (ns != NULL) { |
2914 | 0 | ret++; |
2915 | 0 | ns = ns->next; |
2916 | 0 | } |
2917 | 0 | return(ret); |
2918 | 0 | } |
2919 | | |
2920 | | /** |
2921 | | * Get the node type of the current node |
2922 | | * Reference: |
2923 | | * http://www.gnu.org/software/dotgnu/pnetlib-doc/System/Xml/XmlNodeType.html |
2924 | | * |
2925 | | * @param reader the xmlTextReader used |
2926 | | * @returns the xmlReaderTypes of the current node or -1 in case of error |
2927 | | */ |
2928 | | int |
2929 | 0 | xmlTextReaderNodeType(xmlTextReader *reader) { |
2930 | 0 | xmlNodePtr node; |
2931 | |
|
2932 | 0 | if (reader == NULL) |
2933 | 0 | return(-1); |
2934 | 0 | if (reader->node == NULL) |
2935 | 0 | return(XML_READER_TYPE_NONE); |
2936 | 0 | if (reader->curnode != NULL) |
2937 | 0 | node = reader->curnode; |
2938 | 0 | else |
2939 | 0 | node = reader->node; |
2940 | 0 | switch (node->type) { |
2941 | 0 | case XML_ELEMENT_NODE: |
2942 | 0 | if ((reader->state == XML_TEXTREADER_END) || |
2943 | 0 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
2944 | 0 | return(XML_READER_TYPE_END_ELEMENT); |
2945 | 0 | return(XML_READER_TYPE_ELEMENT); |
2946 | 0 | case XML_NAMESPACE_DECL: |
2947 | 0 | case XML_ATTRIBUTE_NODE: |
2948 | 0 | return(XML_READER_TYPE_ATTRIBUTE); |
2949 | 0 | case XML_TEXT_NODE: |
2950 | 0 | if (xmlIsBlankNode(reader->node)) { |
2951 | 0 | if (xmlNodeGetSpacePreserve(reader->node)) |
2952 | 0 | return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE); |
2953 | 0 | else |
2954 | 0 | return(XML_READER_TYPE_WHITESPACE); |
2955 | 0 | } else { |
2956 | 0 | return(XML_READER_TYPE_TEXT); |
2957 | 0 | } |
2958 | 0 | case XML_CDATA_SECTION_NODE: |
2959 | 0 | return(XML_READER_TYPE_CDATA); |
2960 | 0 | case XML_ENTITY_REF_NODE: |
2961 | 0 | return(XML_READER_TYPE_ENTITY_REFERENCE); |
2962 | 0 | case XML_ENTITY_NODE: |
2963 | 0 | return(XML_READER_TYPE_ENTITY); |
2964 | 0 | case XML_PI_NODE: |
2965 | 0 | return(XML_READER_TYPE_PROCESSING_INSTRUCTION); |
2966 | 0 | case XML_COMMENT_NODE: |
2967 | 0 | return(XML_READER_TYPE_COMMENT); |
2968 | 0 | case XML_DOCUMENT_NODE: |
2969 | 0 | case XML_HTML_DOCUMENT_NODE: |
2970 | 0 | return(XML_READER_TYPE_DOCUMENT); |
2971 | 0 | case XML_DOCUMENT_FRAG_NODE: |
2972 | 0 | return(XML_READER_TYPE_DOCUMENT_FRAGMENT); |
2973 | 0 | case XML_NOTATION_NODE: |
2974 | 0 | return(XML_READER_TYPE_NOTATION); |
2975 | 0 | case XML_DOCUMENT_TYPE_NODE: |
2976 | 0 | case XML_DTD_NODE: |
2977 | 0 | return(XML_READER_TYPE_DOCUMENT_TYPE); |
2978 | | |
2979 | 0 | case XML_ELEMENT_DECL: |
2980 | 0 | case XML_ATTRIBUTE_DECL: |
2981 | 0 | case XML_ENTITY_DECL: |
2982 | 0 | case XML_XINCLUDE_START: |
2983 | 0 | case XML_XINCLUDE_END: |
2984 | 0 | return(XML_READER_TYPE_NONE); |
2985 | 0 | } |
2986 | 0 | return(-1); |
2987 | 0 | } |
2988 | | |
2989 | | /** |
2990 | | * Check if the current node is empty |
2991 | | * |
2992 | | * @param reader the xmlTextReader used |
2993 | | * @returns 1 if empty, 0 if not and -1 in case of error |
2994 | | */ |
2995 | | int |
2996 | 0 | xmlTextReaderIsEmptyElement(xmlTextReader *reader) { |
2997 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
2998 | 0 | return(-1); |
2999 | 0 | if (reader->node->type != XML_ELEMENT_NODE) |
3000 | 0 | return(0); |
3001 | 0 | if (reader->curnode != NULL) |
3002 | 0 | return(0); |
3003 | 0 | if (reader->node->children != NULL) |
3004 | 0 | return(0); |
3005 | 0 | if (reader->state == XML_TEXTREADER_END) |
3006 | 0 | return(0); |
3007 | 0 | if (reader->doc != NULL) |
3008 | 0 | return(1); |
3009 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
3010 | 0 | if (reader->in_xinclude > 0) |
3011 | 0 | return(1); |
3012 | 0 | #endif |
3013 | 0 | return((reader->node->extra & NODE_IS_EMPTY) != 0); |
3014 | 0 | } |
3015 | | |
3016 | | /** |
3017 | | * The local name of the node. |
3018 | | * |
3019 | | * @param reader the xmlTextReader used |
3020 | | * @returns the local name or NULL if not available, |
3021 | | * if non NULL it need to be freed by the caller. |
3022 | | */ |
3023 | | xmlChar * |
3024 | 0 | xmlTextReaderLocalName(xmlTextReader *reader) { |
3025 | 0 | xmlNodePtr node; |
3026 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3027 | 0 | return(NULL); |
3028 | 0 | if (reader->curnode != NULL) |
3029 | 0 | node = reader->curnode; |
3030 | 0 | else |
3031 | 0 | node = reader->node; |
3032 | 0 | if (node->type == XML_NAMESPACE_DECL) { |
3033 | 0 | xmlNsPtr ns = (xmlNsPtr) node; |
3034 | 0 | if (ns->prefix == NULL) |
3035 | 0 | return(readerStrdup(reader, BAD_CAST "xmlns")); |
3036 | 0 | else |
3037 | 0 | return(readerStrdup(reader, ns->prefix)); |
3038 | 0 | } |
3039 | 0 | if ((node->type != XML_ELEMENT_NODE) && |
3040 | 0 | (node->type != XML_ATTRIBUTE_NODE)) |
3041 | 0 | return(xmlTextReaderName(reader)); |
3042 | 0 | return(readerStrdup(reader, node->name)); |
3043 | 0 | } |
3044 | | |
3045 | | /** |
3046 | | * The local name of the node. |
3047 | | * |
3048 | | * @param reader the xmlTextReader used |
3049 | | * @returns the local name or NULL if not available, the |
3050 | | * string will be deallocated with the reader. |
3051 | | */ |
3052 | | const xmlChar * |
3053 | 0 | xmlTextReaderConstLocalName(xmlTextReader *reader) { |
3054 | 0 | xmlNodePtr node; |
3055 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3056 | 0 | return(NULL); |
3057 | 0 | if (reader->curnode != NULL) |
3058 | 0 | node = reader->curnode; |
3059 | 0 | else |
3060 | 0 | node = reader->node; |
3061 | 0 | if (node->type == XML_NAMESPACE_DECL) { |
3062 | 0 | xmlNsPtr ns = (xmlNsPtr) node; |
3063 | 0 | if (ns->prefix == NULL) |
3064 | 0 | return(constString(reader, BAD_CAST "xmlns")); |
3065 | 0 | else |
3066 | 0 | return(ns->prefix); |
3067 | 0 | } |
3068 | 0 | if ((node->type != XML_ELEMENT_NODE) && |
3069 | 0 | (node->type != XML_ATTRIBUTE_NODE)) |
3070 | 0 | return(xmlTextReaderConstName(reader)); |
3071 | 0 | return(node->name); |
3072 | 0 | } |
3073 | | |
3074 | | /** |
3075 | | * The qualified name of the node, equal to Prefix :LocalName. |
3076 | | * |
3077 | | * @param reader the xmlTextReader used |
3078 | | * @returns the local name or NULL if not available, |
3079 | | * if non NULL it need to be freed by the caller. |
3080 | | */ |
3081 | | xmlChar * |
3082 | 0 | xmlTextReaderName(xmlTextReader *reader) { |
3083 | 0 | xmlNodePtr node; |
3084 | 0 | xmlChar *ret; |
3085 | |
|
3086 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3087 | 0 | return(NULL); |
3088 | 0 | if (reader->curnode != NULL) |
3089 | 0 | node = reader->curnode; |
3090 | 0 | else |
3091 | 0 | node = reader->node; |
3092 | 0 | switch (node->type) { |
3093 | 0 | case XML_ELEMENT_NODE: |
3094 | 0 | case XML_ATTRIBUTE_NODE: |
3095 | 0 | if ((node->ns == NULL) || |
3096 | 0 | (node->ns->prefix == NULL)) |
3097 | 0 | return(readerStrdup(reader, node->name)); |
3098 | | |
3099 | 0 | ret = xmlBuildQName(node->name, node->ns->prefix, NULL, 0); |
3100 | 0 | if (ret == NULL) |
3101 | 0 | xmlTextReaderErrMemory(reader); |
3102 | 0 | return(ret); |
3103 | 0 | case XML_TEXT_NODE: |
3104 | 0 | return(readerStrdup(reader, BAD_CAST "#text")); |
3105 | 0 | case XML_CDATA_SECTION_NODE: |
3106 | 0 | return(readerStrdup(reader, BAD_CAST "#cdata-section")); |
3107 | 0 | case XML_ENTITY_NODE: |
3108 | 0 | case XML_ENTITY_REF_NODE: |
3109 | 0 | return(readerStrdup(reader, node->name)); |
3110 | 0 | case XML_PI_NODE: |
3111 | 0 | return(readerStrdup(reader, node->name)); |
3112 | 0 | case XML_COMMENT_NODE: |
3113 | 0 | return(readerStrdup(reader, BAD_CAST "#comment")); |
3114 | 0 | case XML_DOCUMENT_NODE: |
3115 | 0 | case XML_HTML_DOCUMENT_NODE: |
3116 | 0 | return(readerStrdup(reader, BAD_CAST "#document")); |
3117 | 0 | case XML_DOCUMENT_FRAG_NODE: |
3118 | 0 | return(readerStrdup(reader, BAD_CAST "#document-fragment")); |
3119 | 0 | case XML_NOTATION_NODE: |
3120 | 0 | return(readerStrdup(reader, node->name)); |
3121 | 0 | case XML_DOCUMENT_TYPE_NODE: |
3122 | 0 | case XML_DTD_NODE: |
3123 | 0 | return(readerStrdup(reader, node->name)); |
3124 | 0 | case XML_NAMESPACE_DECL: { |
3125 | 0 | xmlNsPtr ns = (xmlNsPtr) node; |
3126 | |
|
3127 | 0 | if (ns->prefix == NULL) |
3128 | 0 | return(readerStrdup(reader, BAD_CAST "xmlns")); |
3129 | 0 | ret = xmlBuildQName(ns->prefix, BAD_CAST "xmlns", NULL, 0); |
3130 | 0 | if (ret == NULL) |
3131 | 0 | xmlTextReaderErrMemory(reader); |
3132 | 0 | return(ret); |
3133 | 0 | } |
3134 | | |
3135 | 0 | case XML_ELEMENT_DECL: |
3136 | 0 | case XML_ATTRIBUTE_DECL: |
3137 | 0 | case XML_ENTITY_DECL: |
3138 | 0 | case XML_XINCLUDE_START: |
3139 | 0 | case XML_XINCLUDE_END: |
3140 | 0 | return(NULL); |
3141 | 0 | } |
3142 | 0 | return(NULL); |
3143 | 0 | } |
3144 | | |
3145 | | /** |
3146 | | * The qualified name of the node, equal to Prefix :LocalName. |
3147 | | * |
3148 | | * @param reader the xmlTextReader used |
3149 | | * @returns the local name or NULL if not available, the string is |
3150 | | * deallocated with the reader. |
3151 | | */ |
3152 | | const xmlChar * |
3153 | 0 | xmlTextReaderConstName(xmlTextReader *reader) { |
3154 | 0 | xmlNodePtr node; |
3155 | |
|
3156 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3157 | 0 | return(NULL); |
3158 | 0 | if (reader->curnode != NULL) |
3159 | 0 | node = reader->curnode; |
3160 | 0 | else |
3161 | 0 | node = reader->node; |
3162 | 0 | switch (node->type) { |
3163 | 0 | case XML_ELEMENT_NODE: |
3164 | 0 | case XML_ATTRIBUTE_NODE: |
3165 | 0 | if ((node->ns == NULL) || |
3166 | 0 | (node->ns->prefix == NULL)) |
3167 | 0 | return(node->name); |
3168 | 0 | return(constQString(reader, node->ns->prefix, node->name)); |
3169 | 0 | case XML_TEXT_NODE: |
3170 | 0 | return(constString(reader, BAD_CAST "#text")); |
3171 | 0 | case XML_CDATA_SECTION_NODE: |
3172 | 0 | return(constString(reader, BAD_CAST "#cdata-section")); |
3173 | 0 | case XML_ENTITY_NODE: |
3174 | 0 | case XML_ENTITY_REF_NODE: |
3175 | 0 | return(constString(reader, node->name)); |
3176 | 0 | case XML_PI_NODE: |
3177 | 0 | return(constString(reader, node->name)); |
3178 | 0 | case XML_COMMENT_NODE: |
3179 | 0 | return(constString(reader, BAD_CAST "#comment")); |
3180 | 0 | case XML_DOCUMENT_NODE: |
3181 | 0 | case XML_HTML_DOCUMENT_NODE: |
3182 | 0 | return(constString(reader, BAD_CAST "#document")); |
3183 | 0 | case XML_DOCUMENT_FRAG_NODE: |
3184 | 0 | return(constString(reader, BAD_CAST "#document-fragment")); |
3185 | 0 | case XML_NOTATION_NODE: |
3186 | 0 | return(constString(reader, node->name)); |
3187 | 0 | case XML_DOCUMENT_TYPE_NODE: |
3188 | 0 | case XML_DTD_NODE: |
3189 | 0 | return(constString(reader, node->name)); |
3190 | 0 | case XML_NAMESPACE_DECL: { |
3191 | 0 | xmlNsPtr ns = (xmlNsPtr) node; |
3192 | |
|
3193 | 0 | if (ns->prefix == NULL) |
3194 | 0 | return(constString(reader, BAD_CAST "xmlns")); |
3195 | 0 | return(constQString(reader, BAD_CAST "xmlns", ns->prefix)); |
3196 | 0 | } |
3197 | | |
3198 | 0 | case XML_ELEMENT_DECL: |
3199 | 0 | case XML_ATTRIBUTE_DECL: |
3200 | 0 | case XML_ENTITY_DECL: |
3201 | 0 | case XML_XINCLUDE_START: |
3202 | 0 | case XML_XINCLUDE_END: |
3203 | 0 | return(NULL); |
3204 | 0 | } |
3205 | 0 | return(NULL); |
3206 | 0 | } |
3207 | | |
3208 | | /** |
3209 | | * A shorthand reference to the namespace associated with the node. |
3210 | | * |
3211 | | * @param reader the xmlTextReader used |
3212 | | * @returns the prefix or NULL if not available, |
3213 | | * if non NULL it need to be freed by the caller. |
3214 | | */ |
3215 | | xmlChar * |
3216 | 0 | xmlTextReaderPrefix(xmlTextReader *reader) { |
3217 | 0 | xmlNodePtr node; |
3218 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3219 | 0 | return(NULL); |
3220 | 0 | if (reader->curnode != NULL) |
3221 | 0 | node = reader->curnode; |
3222 | 0 | else |
3223 | 0 | node = reader->node; |
3224 | 0 | if (node->type == XML_NAMESPACE_DECL) { |
3225 | 0 | xmlNsPtr ns = (xmlNsPtr) node; |
3226 | 0 | if (ns->prefix == NULL) |
3227 | 0 | return(NULL); |
3228 | 0 | return(readerStrdup(reader, BAD_CAST "xmlns")); |
3229 | 0 | } |
3230 | 0 | if ((node->type != XML_ELEMENT_NODE) && |
3231 | 0 | (node->type != XML_ATTRIBUTE_NODE)) |
3232 | 0 | return(NULL); |
3233 | 0 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) |
3234 | 0 | return(readerStrdup(reader, node->ns->prefix)); |
3235 | 0 | return(NULL); |
3236 | 0 | } |
3237 | | |
3238 | | /** |
3239 | | * A shorthand reference to the namespace associated with the node. |
3240 | | * |
3241 | | * @param reader the xmlTextReader used |
3242 | | * @returns the prefix or NULL if not available, the string is deallocated |
3243 | | * with the reader. |
3244 | | */ |
3245 | | const xmlChar * |
3246 | 0 | xmlTextReaderConstPrefix(xmlTextReader *reader) { |
3247 | 0 | xmlNodePtr node; |
3248 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3249 | 0 | return(NULL); |
3250 | 0 | if (reader->curnode != NULL) |
3251 | 0 | node = reader->curnode; |
3252 | 0 | else |
3253 | 0 | node = reader->node; |
3254 | 0 | if (node->type == XML_NAMESPACE_DECL) { |
3255 | 0 | xmlNsPtr ns = (xmlNsPtr) node; |
3256 | 0 | if (ns->prefix == NULL) |
3257 | 0 | return(NULL); |
3258 | 0 | return(constString(reader, BAD_CAST "xmlns")); |
3259 | 0 | } |
3260 | 0 | if ((node->type != XML_ELEMENT_NODE) && |
3261 | 0 | (node->type != XML_ATTRIBUTE_NODE)) |
3262 | 0 | return(NULL); |
3263 | 0 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) |
3264 | 0 | return(constString(reader, node->ns->prefix)); |
3265 | 0 | return(NULL); |
3266 | 0 | } |
3267 | | |
3268 | | /** |
3269 | | * The URI defining the namespace associated with the node. |
3270 | | * |
3271 | | * @param reader the xmlTextReader used |
3272 | | * @returns the namespace URI or NULL if not available, |
3273 | | * if non NULL it need to be freed by the caller. |
3274 | | */ |
3275 | | xmlChar * |
3276 | 0 | xmlTextReaderNamespaceUri(xmlTextReader *reader) { |
3277 | 0 | xmlNodePtr node; |
3278 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3279 | 0 | return(NULL); |
3280 | 0 | if (reader->curnode != NULL) |
3281 | 0 | node = reader->curnode; |
3282 | 0 | else |
3283 | 0 | node = reader->node; |
3284 | 0 | if (node->type == XML_NAMESPACE_DECL) |
3285 | 0 | return(readerStrdup(reader, BAD_CAST "http://www.w3.org/2000/xmlns/")); |
3286 | 0 | if ((node->type != XML_ELEMENT_NODE) && |
3287 | 0 | (node->type != XML_ATTRIBUTE_NODE)) |
3288 | 0 | return(NULL); |
3289 | 0 | if (node->ns != NULL) |
3290 | 0 | return(readerStrdup(reader, node->ns->href)); |
3291 | 0 | return(NULL); |
3292 | 0 | } |
3293 | | |
3294 | | /** |
3295 | | * The URI defining the namespace associated with the node. |
3296 | | * |
3297 | | * @param reader the xmlTextReader used |
3298 | | * @returns the namespace URI or NULL if not available, the string |
3299 | | * will be deallocated with the reader |
3300 | | */ |
3301 | | const xmlChar * |
3302 | 0 | xmlTextReaderConstNamespaceUri(xmlTextReader *reader) { |
3303 | 0 | xmlNodePtr node; |
3304 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3305 | 0 | return(NULL); |
3306 | 0 | if (reader->curnode != NULL) |
3307 | 0 | node = reader->curnode; |
3308 | 0 | else |
3309 | 0 | node = reader->node; |
3310 | 0 | if (node->type == XML_NAMESPACE_DECL) |
3311 | 0 | return(constString(reader, BAD_CAST "http://www.w3.org/2000/xmlns/")); |
3312 | 0 | if ((node->type != XML_ELEMENT_NODE) && |
3313 | 0 | (node->type != XML_ATTRIBUTE_NODE)) |
3314 | 0 | return(NULL); |
3315 | 0 | if (node->ns != NULL) |
3316 | 0 | return(constString(reader, node->ns->href)); |
3317 | 0 | return(NULL); |
3318 | 0 | } |
3319 | | |
3320 | | /** |
3321 | | * The base URI of the node. |
3322 | | * |
3323 | | * @param reader the xmlTextReader used |
3324 | | * @returns the base URI or NULL if not available, |
3325 | | * if non NULL it need to be freed by the caller. |
3326 | | */ |
3327 | | xmlChar * |
3328 | 0 | xmlTextReaderBaseUri(xmlTextReader *reader) { |
3329 | 0 | xmlChar *ret = NULL; |
3330 | 0 | int result; |
3331 | |
|
3332 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3333 | 0 | return(NULL); |
3334 | 0 | result = xmlNodeGetBaseSafe(NULL, reader->node, &ret); |
3335 | 0 | if (result < 0) |
3336 | 0 | xmlTextReaderErrMemory(reader); |
3337 | |
|
3338 | 0 | return(ret); |
3339 | 0 | } |
3340 | | |
3341 | | /** |
3342 | | * The base URI of the node. |
3343 | | * |
3344 | | * @param reader the xmlTextReader used |
3345 | | * @returns the base URI or NULL if not available, the string |
3346 | | * will be deallocated with the reader |
3347 | | */ |
3348 | | const xmlChar * |
3349 | 0 | xmlTextReaderConstBaseUri(xmlTextReader *reader) { |
3350 | 0 | xmlChar *tmp; |
3351 | 0 | const xmlChar *ret; |
3352 | 0 | int result; |
3353 | |
|
3354 | 0 | if ((reader == NULL) || (reader->node == NULL)) |
3355 | 0 | return(NULL); |
3356 | 0 | result = xmlNodeGetBaseSafe(NULL, reader->node, &tmp); |
3357 | 0 | if (result < 0) |
3358 | 0 | xmlTextReaderErrMemory(reader); |
3359 | 0 | if (tmp == NULL) |
3360 | 0 | return(NULL); |
3361 | 0 | ret = constString(reader, tmp); |
3362 | 0 | xmlFree(tmp); |
3363 | 0 | return(ret); |
3364 | 0 | } |
3365 | | |
3366 | | /** |
3367 | | * The depth of the node in the tree. |
3368 | | * |
3369 | | * @param reader the xmlTextReader used |
3370 | | * @returns the depth or -1 in case of error |
3371 | | */ |
3372 | | int |
3373 | 0 | xmlTextReaderDepth(xmlTextReader *reader) { |
3374 | 0 | if (reader == NULL) |
3375 | 0 | return(-1); |
3376 | 0 | if (reader->node == NULL) |
3377 | 0 | return(0); |
3378 | | |
3379 | 0 | if (reader->curnode != NULL) { |
3380 | 0 | if ((reader->curnode->type == XML_ATTRIBUTE_NODE) || |
3381 | 0 | (reader->curnode->type == XML_NAMESPACE_DECL)) |
3382 | 0 | return(reader->depth + 1); |
3383 | 0 | return(reader->depth + 2); |
3384 | 0 | } |
3385 | 0 | return(reader->depth); |
3386 | 0 | } |
3387 | | |
3388 | | /** |
3389 | | * Whether the node has attributes. |
3390 | | * |
3391 | | * @param reader the xmlTextReader used |
3392 | | * @returns 1 if true, 0 if false, and -1 in case or error |
3393 | | */ |
3394 | | int |
3395 | 0 | xmlTextReaderHasAttributes(xmlTextReader *reader) { |
3396 | 0 | xmlNodePtr node; |
3397 | 0 | if (reader == NULL) |
3398 | 0 | return(-1); |
3399 | 0 | if (reader->node == NULL) |
3400 | 0 | return(0); |
3401 | 0 | if (reader->curnode != NULL) |
3402 | 0 | node = reader->curnode; |
3403 | 0 | else |
3404 | 0 | node = reader->node; |
3405 | |
|
3406 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
3407 | 0 | ((node->properties != NULL) || (node->nsDef != NULL))) |
3408 | 0 | return(1); |
3409 | | /* TODO: handle the xmlDecl */ |
3410 | 0 | return(0); |
3411 | 0 | } |
3412 | | |
3413 | | /** |
3414 | | * Whether the node can have a text value. |
3415 | | * |
3416 | | * @param reader the xmlTextReader used |
3417 | | * @returns 1 if true, 0 if false, and -1 in case or error |
3418 | | */ |
3419 | | int |
3420 | 0 | xmlTextReaderHasValue(xmlTextReader *reader) { |
3421 | 0 | xmlNodePtr node; |
3422 | 0 | if (reader == NULL) |
3423 | 0 | return(-1); |
3424 | 0 | if (reader->node == NULL) |
3425 | 0 | return(0); |
3426 | 0 | if (reader->curnode != NULL) |
3427 | 0 | node = reader->curnode; |
3428 | 0 | else |
3429 | 0 | node = reader->node; |
3430 | |
|
3431 | 0 | switch (node->type) { |
3432 | 0 | case XML_ATTRIBUTE_NODE: |
3433 | 0 | case XML_TEXT_NODE: |
3434 | 0 | case XML_CDATA_SECTION_NODE: |
3435 | 0 | case XML_PI_NODE: |
3436 | 0 | case XML_COMMENT_NODE: |
3437 | 0 | case XML_NAMESPACE_DECL: |
3438 | 0 | return(1); |
3439 | 0 | default: |
3440 | 0 | break; |
3441 | 0 | } |
3442 | 0 | return(0); |
3443 | 0 | } |
3444 | | |
3445 | | /** |
3446 | | * Provides the text value of the node if present |
3447 | | * |
3448 | | * @param reader the xmlTextReader used |
3449 | | * @returns the string or NULL if not available. The result must be deallocated |
3450 | | * with #xmlFree |
3451 | | */ |
3452 | | xmlChar * |
3453 | 0 | xmlTextReaderValue(xmlTextReader *reader) { |
3454 | 0 | xmlNodePtr node; |
3455 | 0 | if (reader == NULL) |
3456 | 0 | return(NULL); |
3457 | 0 | if (reader->node == NULL) |
3458 | 0 | return(NULL); |
3459 | 0 | if (reader->curnode != NULL) |
3460 | 0 | node = reader->curnode; |
3461 | 0 | else |
3462 | 0 | node = reader->node; |
3463 | |
|
3464 | 0 | switch (node->type) { |
3465 | 0 | case XML_NAMESPACE_DECL: |
3466 | 0 | return(readerStrdup(reader, ((xmlNsPtr) node)->href)); |
3467 | 0 | case XML_ATTRIBUTE_NODE:{ |
3468 | 0 | xmlAttrPtr attr = (xmlAttrPtr) node; |
3469 | 0 | xmlDocPtr doc = NULL; |
3470 | 0 | xmlChar *ret; |
3471 | |
|
3472 | 0 | if (attr->children == NULL) |
3473 | 0 | return(NULL); |
3474 | 0 | if (attr->parent != NULL) |
3475 | 0 | doc = attr->parent->doc; |
3476 | 0 | ret = xmlNodeListGetString(doc, attr->children, 1); |
3477 | 0 | if (ret == NULL) |
3478 | 0 | xmlTextReaderErrMemory(reader); |
3479 | 0 | return(ret); |
3480 | 0 | } |
3481 | 0 | case XML_TEXT_NODE: |
3482 | 0 | case XML_CDATA_SECTION_NODE: |
3483 | 0 | case XML_PI_NODE: |
3484 | 0 | case XML_COMMENT_NODE: |
3485 | 0 | return(readerStrdup(reader, node->content)); |
3486 | 0 | default: |
3487 | 0 | break; |
3488 | 0 | } |
3489 | 0 | return(NULL); |
3490 | 0 | } |
3491 | | |
3492 | | /** |
3493 | | * Provides the text value of the node if present |
3494 | | * |
3495 | | * @param reader the xmlTextReader used |
3496 | | * @returns the string or NULL if not available. The result will be |
3497 | | * deallocated on the next Read() operation. |
3498 | | */ |
3499 | | const xmlChar * |
3500 | 0 | xmlTextReaderConstValue(xmlTextReader *reader) { |
3501 | 0 | xmlNodePtr node; |
3502 | 0 | if (reader == NULL) |
3503 | 0 | return(NULL); |
3504 | 0 | if (reader->node == NULL) |
3505 | 0 | return(NULL); |
3506 | 0 | if (reader->curnode != NULL) |
3507 | 0 | node = reader->curnode; |
3508 | 0 | else |
3509 | 0 | node = reader->node; |
3510 | |
|
3511 | 0 | switch (node->type) { |
3512 | 0 | case XML_NAMESPACE_DECL: |
3513 | 0 | return(((xmlNsPtr) node)->href); |
3514 | 0 | case XML_ATTRIBUTE_NODE:{ |
3515 | 0 | xmlAttrPtr attr = (xmlAttrPtr) node; |
3516 | 0 | const xmlChar *ret; |
3517 | |
|
3518 | 0 | if ((attr->children != NULL) && |
3519 | 0 | (attr->children->type == XML_TEXT_NODE) && |
3520 | 0 | (attr->children->next == NULL)) |
3521 | 0 | return(attr->children->content); |
3522 | 0 | else { |
3523 | 0 | if (reader->buffer == NULL) { |
3524 | 0 | reader->buffer = xmlBufCreate(50); |
3525 | 0 | if (reader->buffer == NULL) |
3526 | 0 | return (NULL); |
3527 | 0 | } else |
3528 | 0 | xmlBufEmpty(reader->buffer); |
3529 | 0 | xmlBufGetNodeContent(reader->buffer, node); |
3530 | 0 | ret = xmlBufContent(reader->buffer); |
3531 | 0 | if (ret == NULL) { |
3532 | 0 | xmlTextReaderErrMemory(reader); |
3533 | | /* error on the buffer best to reallocate */ |
3534 | 0 | xmlBufFree(reader->buffer); |
3535 | 0 | reader->buffer = xmlBufCreate(50); |
3536 | 0 | } |
3537 | 0 | return(ret); |
3538 | 0 | } |
3539 | 0 | break; |
3540 | 0 | } |
3541 | 0 | case XML_TEXT_NODE: |
3542 | 0 | case XML_CDATA_SECTION_NODE: |
3543 | 0 | case XML_PI_NODE: |
3544 | 0 | case XML_COMMENT_NODE: |
3545 | 0 | return(node->content); |
3546 | 0 | default: |
3547 | 0 | break; |
3548 | 0 | } |
3549 | 0 | return(NULL); |
3550 | 0 | } |
3551 | | |
3552 | | /** |
3553 | | * Whether an Attribute node was generated from the default value |
3554 | | * defined in the DTD or schema. |
3555 | | * |
3556 | | * @param reader the xmlTextReader used |
3557 | | * @returns 0 if not defaulted, 1 if defaulted, and -1 in case of error |
3558 | | */ |
3559 | | int |
3560 | 0 | xmlTextReaderIsDefault(xmlTextReader *reader) { |
3561 | 0 | if (reader == NULL) |
3562 | 0 | return(-1); |
3563 | 0 | return(0); |
3564 | 0 | } |
3565 | | |
3566 | | /** |
3567 | | * The quotation mark character used to enclose the value of an attribute. |
3568 | | * |
3569 | | * @param reader the xmlTextReader used |
3570 | | * @returns " or ' and -1 in case of error |
3571 | | */ |
3572 | | int |
3573 | 0 | xmlTextReaderQuoteChar(xmlTextReader *reader) { |
3574 | 0 | if (reader == NULL) |
3575 | 0 | return(-1); |
3576 | | /* TODO maybe lookup the attribute value for " first */ |
3577 | 0 | return('"'); |
3578 | 0 | } |
3579 | | |
3580 | | /** |
3581 | | * The xml:lang scope within which the node resides. |
3582 | | * |
3583 | | * @param reader the xmlTextReader used |
3584 | | * @returns the xml:lang value or NULL if none exists., |
3585 | | * if non NULL it need to be freed by the caller. |
3586 | | */ |
3587 | | xmlChar * |
3588 | 0 | xmlTextReaderXmlLang(xmlTextReader *reader) { |
3589 | 0 | if (reader == NULL) |
3590 | 0 | return(NULL); |
3591 | 0 | if (reader->node == NULL) |
3592 | 0 | return(NULL); |
3593 | 0 | return(xmlNodeGetLang(reader->node)); |
3594 | 0 | } |
3595 | | |
3596 | | /** |
3597 | | * The xml:lang scope within which the node resides. |
3598 | | * |
3599 | | * @param reader the xmlTextReader used |
3600 | | * @returns the xml:lang value or NULL if none exists. |
3601 | | */ |
3602 | | const xmlChar * |
3603 | 0 | xmlTextReaderConstXmlLang(xmlTextReader *reader) { |
3604 | 0 | xmlChar *tmp; |
3605 | 0 | const xmlChar *ret; |
3606 | |
|
3607 | 0 | if (reader == NULL) |
3608 | 0 | return(NULL); |
3609 | 0 | if (reader->node == NULL) |
3610 | 0 | return(NULL); |
3611 | 0 | tmp = xmlNodeGetLang(reader->node); |
3612 | 0 | if (tmp == NULL) |
3613 | 0 | return(NULL); |
3614 | 0 | ret = constString(reader, tmp); |
3615 | 0 | xmlFree(tmp); |
3616 | 0 | return(ret); |
3617 | 0 | } |
3618 | | |
3619 | | /** |
3620 | | * Get an interned string from the reader, allows for example to |
3621 | | * speedup string name comparisons |
3622 | | * |
3623 | | * @param reader the xmlTextReader used |
3624 | | * @param str the string to intern. |
3625 | | * @returns an interned copy of the string or NULL in case of error. The |
3626 | | * string will be deallocated with the reader. |
3627 | | */ |
3628 | | const xmlChar * |
3629 | 0 | xmlTextReaderConstString(xmlTextReader *reader, const xmlChar *str) { |
3630 | 0 | if (reader == NULL) |
3631 | 0 | return(NULL); |
3632 | 0 | return(constString(reader, str)); |
3633 | 0 | } |
3634 | | |
3635 | | /** |
3636 | | * The value indicating whether to normalize white space and attribute values. |
3637 | | * Since attribute value and end of line normalizations are a MUST in the XML |
3638 | | * specification only the value true is accepted. The broken behaviour of |
3639 | | * accepting out of range character entities like &\#0; is of course not |
3640 | | * supported either. |
3641 | | * |
3642 | | * @param reader the xmlTextReader used |
3643 | | * @returns 1 or -1 in case of error. |
3644 | | */ |
3645 | | int |
3646 | 0 | xmlTextReaderNormalization(xmlTextReader *reader) { |
3647 | 0 | if (reader == NULL) |
3648 | 0 | return(-1); |
3649 | 0 | return(1); |
3650 | 0 | } |
3651 | | |
3652 | | /************************************************************************ |
3653 | | * * |
3654 | | * Extensions to the base APIs * |
3655 | | * * |
3656 | | ************************************************************************/ |
3657 | | |
3658 | | /** |
3659 | | * Change the parser processing behaviour by changing some of its internal |
3660 | | * properties. Note that some properties can only be changed before any |
3661 | | * read has been done. |
3662 | | * |
3663 | | * @param reader the xmlTextReader used |
3664 | | * @param prop the xmlParserProperties to set |
3665 | | * @param value usually 0 or 1 to (de)activate it |
3666 | | * @returns 0 if the call was successful, or -1 in case of error |
3667 | | */ |
3668 | | int |
3669 | 0 | xmlTextReaderSetParserProp(xmlTextReader *reader, int prop, int value) { |
3670 | 0 | xmlParserProperties p = (xmlParserProperties) prop; |
3671 | 0 | xmlParserCtxtPtr ctxt; |
3672 | |
|
3673 | 0 | if ((reader == NULL) || (reader->ctxt == NULL)) |
3674 | 0 | return(-1); |
3675 | 0 | ctxt = reader->ctxt; |
3676 | |
|
3677 | 0 | switch (p) { |
3678 | 0 | case XML_PARSER_LOADDTD: |
3679 | 0 | if (value != 0) { |
3680 | 0 | if (ctxt->loadsubset == 0) { |
3681 | 0 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
3682 | 0 | return(-1); |
3683 | 0 | ctxt->options |= XML_PARSE_DTDLOAD; |
3684 | 0 | ctxt->loadsubset |= XML_DETECT_IDS; |
3685 | 0 | } |
3686 | 0 | } else { |
3687 | 0 | ctxt->options &= ~XML_PARSE_DTDLOAD; |
3688 | 0 | ctxt->loadsubset &= ~XML_DETECT_IDS; |
3689 | 0 | } |
3690 | 0 | return(0); |
3691 | 0 | case XML_PARSER_DEFAULTATTRS: |
3692 | 0 | if (value != 0) { |
3693 | 0 | ctxt->options |= XML_PARSE_DTDATTR; |
3694 | 0 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
3695 | 0 | } else { |
3696 | 0 | ctxt->options &= ~XML_PARSE_DTDATTR; |
3697 | 0 | ctxt->loadsubset &= ~XML_COMPLETE_ATTRS; |
3698 | 0 | } |
3699 | 0 | return(0); |
3700 | 0 | case XML_PARSER_VALIDATE: |
3701 | 0 | if (value != 0) { |
3702 | 0 | ctxt->options |= XML_PARSE_DTDVALID; |
3703 | 0 | ctxt->validate = 1; |
3704 | 0 | reader->validate = XML_TEXTREADER_VALIDATE_DTD; |
3705 | 0 | } else { |
3706 | 0 | ctxt->options &= ~XML_PARSE_DTDVALID; |
3707 | 0 | ctxt->validate = 0; |
3708 | 0 | } |
3709 | 0 | return(0); |
3710 | 0 | case XML_PARSER_SUBST_ENTITIES: |
3711 | 0 | if (value != 0) { |
3712 | 0 | ctxt->options |= XML_PARSE_NOENT; |
3713 | 0 | ctxt->replaceEntities = 1; |
3714 | 0 | } else { |
3715 | 0 | ctxt->options &= ~XML_PARSE_NOENT; |
3716 | 0 | ctxt->replaceEntities = 0; |
3717 | 0 | } |
3718 | 0 | return(0); |
3719 | 0 | } |
3720 | 0 | return(-1); |
3721 | 0 | } |
3722 | | |
3723 | | /** |
3724 | | * Read the parser internal property. |
3725 | | * |
3726 | | * @param reader the xmlTextReader used |
3727 | | * @param prop the xmlParserProperties to get |
3728 | | * @returns the value, usually 0 or 1, or -1 in case of error. |
3729 | | */ |
3730 | | int |
3731 | 0 | xmlTextReaderGetParserProp(xmlTextReader *reader, int prop) { |
3732 | 0 | xmlParserProperties p = (xmlParserProperties) prop; |
3733 | 0 | xmlParserCtxtPtr ctxt; |
3734 | |
|
3735 | 0 | if ((reader == NULL) || (reader->ctxt == NULL)) |
3736 | 0 | return(-1); |
3737 | 0 | ctxt = reader->ctxt; |
3738 | |
|
3739 | 0 | switch (p) { |
3740 | 0 | case XML_PARSER_LOADDTD: |
3741 | 0 | if ((ctxt->loadsubset != 0) || (ctxt->validate != 0)) |
3742 | 0 | return(1); |
3743 | 0 | return(0); |
3744 | 0 | case XML_PARSER_DEFAULTATTRS: |
3745 | 0 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
3746 | 0 | return(1); |
3747 | 0 | return(0); |
3748 | 0 | case XML_PARSER_VALIDATE: |
3749 | 0 | return(reader->validate); |
3750 | 0 | case XML_PARSER_SUBST_ENTITIES: |
3751 | 0 | return(ctxt->replaceEntities); |
3752 | 0 | } |
3753 | 0 | return(-1); |
3754 | 0 | } |
3755 | | |
3756 | | |
3757 | | /** |
3758 | | * Provide the line number of the current parsing point. |
3759 | | * |
3760 | | * @param reader the user data (XML reader context) |
3761 | | * @returns an int or 0 if not available |
3762 | | */ |
3763 | | int |
3764 | | xmlTextReaderGetParserLineNumber(xmlTextReader *reader) |
3765 | 0 | { |
3766 | 0 | if ((reader == NULL) || (reader->ctxt == NULL) || |
3767 | 0 | (reader->ctxt->input == NULL)) { |
3768 | 0 | return (0); |
3769 | 0 | } |
3770 | 0 | return (reader->ctxt->input->line); |
3771 | 0 | } |
3772 | | |
3773 | | /** |
3774 | | * Provide the column number of the current parsing point. |
3775 | | * |
3776 | | * @param reader the user data (XML reader context) |
3777 | | * @returns an int or 0 if not available |
3778 | | */ |
3779 | | int |
3780 | | xmlTextReaderGetParserColumnNumber(xmlTextReader *reader) |
3781 | 0 | { |
3782 | 0 | if ((reader == NULL) || (reader->ctxt == NULL) || |
3783 | 0 | (reader->ctxt->input == NULL)) { |
3784 | 0 | return (0); |
3785 | 0 | } |
3786 | 0 | return (reader->ctxt->input->col); |
3787 | 0 | } |
3788 | | |
3789 | | /** |
3790 | | * Hacking interface allowing to get the xmlNode corresponding to the |
3791 | | * current node being accessed by the xmlTextReader. This is dangerous |
3792 | | * because the underlying node may be destroyed on the next Reads. |
3793 | | * |
3794 | | * @param reader the xmlTextReader used |
3795 | | * @returns the xmlNode or NULL in case of error. |
3796 | | */ |
3797 | | xmlNode * |
3798 | 0 | xmlTextReaderCurrentNode(xmlTextReader *reader) { |
3799 | 0 | if (reader == NULL) |
3800 | 0 | return(NULL); |
3801 | | |
3802 | 0 | if (reader->curnode != NULL) |
3803 | 0 | return(reader->curnode); |
3804 | 0 | return(reader->node); |
3805 | 0 | } |
3806 | | |
3807 | | /** |
3808 | | * This tells the XML Reader to preserve the current node. |
3809 | | * The caller must also use #xmlTextReaderCurrentDoc to |
3810 | | * keep an handle on the resulting document once parsing has finished |
3811 | | * |
3812 | | * @param reader the xmlTextReader used |
3813 | | * @returns the xmlNode or NULL in case of error. |
3814 | | */ |
3815 | | xmlNode * |
3816 | 0 | xmlTextReaderPreserve(xmlTextReader *reader) { |
3817 | 0 | xmlNodePtr cur, parent; |
3818 | |
|
3819 | 0 | if (reader == NULL) |
3820 | 0 | return(NULL); |
3821 | | |
3822 | 0 | cur = reader->node; |
3823 | 0 | if (cur == NULL) |
3824 | 0 | return(NULL); |
3825 | | |
3826 | 0 | if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) { |
3827 | 0 | cur->extra |= NODE_IS_PRESERVED; |
3828 | 0 | cur->extra |= NODE_IS_SPRESERVED; |
3829 | 0 | } |
3830 | 0 | reader->preserves++; |
3831 | |
|
3832 | 0 | parent = cur->parent;; |
3833 | 0 | while (parent != NULL) { |
3834 | 0 | if (parent->type == XML_ELEMENT_NODE) |
3835 | 0 | parent->extra |= NODE_IS_PRESERVED; |
3836 | 0 | parent = parent->parent; |
3837 | 0 | } |
3838 | 0 | return(cur); |
3839 | 0 | } |
3840 | | |
3841 | | #ifdef LIBXML_PATTERN_ENABLED |
3842 | | /** |
3843 | | * This tells the XML Reader to preserve all nodes matched by the |
3844 | | * pattern. The caller must also use #xmlTextReaderCurrentDoc to |
3845 | | * keep an handle on the resulting document once parsing has finished |
3846 | | * |
3847 | | * @param reader the xmlTextReader used |
3848 | | * @param pattern an XPath subset pattern |
3849 | | * @param namespaces the prefix definitions, array of [URI, prefix] or NULL |
3850 | | * @returns a non-negative number in case of success and -1 in case of error |
3851 | | */ |
3852 | | int |
3853 | | xmlTextReaderPreservePattern(xmlTextReader *reader, const xmlChar *pattern, |
3854 | | const xmlChar **namespaces) |
3855 | 0 | { |
3856 | 0 | xmlPatternPtr comp; |
3857 | |
|
3858 | 0 | if ((reader == NULL) || (pattern == NULL)) |
3859 | 0 | return(-1); |
3860 | | |
3861 | 0 | comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces); |
3862 | 0 | if (comp == NULL) |
3863 | 0 | return(-1); |
3864 | | |
3865 | 0 | if (reader->patternNr >= reader->patternMax) { |
3866 | 0 | xmlPatternPtr *tmp; |
3867 | 0 | int newSize; |
3868 | |
|
3869 | 0 | newSize = xmlGrowCapacity(reader->patternMax, sizeof(tmp[0]), |
3870 | 0 | 4, XML_MAX_ITEMS); |
3871 | 0 | if (newSize < 0) { |
3872 | 0 | xmlTextReaderErrMemory(reader); |
3873 | 0 | return(-1); |
3874 | 0 | } |
3875 | 0 | tmp = xmlRealloc(reader->patternTab, newSize * sizeof(tmp[0])); |
3876 | 0 | if (tmp == NULL) { |
3877 | 0 | xmlTextReaderErrMemory(reader); |
3878 | 0 | return(-1); |
3879 | 0 | } |
3880 | 0 | reader->patternTab = tmp; |
3881 | 0 | reader->patternMax = newSize; |
3882 | 0 | } |
3883 | 0 | reader->patternTab[reader->patternNr] = comp; |
3884 | 0 | return(reader->patternNr++); |
3885 | 0 | } |
3886 | | #endif |
3887 | | |
3888 | | /** |
3889 | | * Hacking interface allowing to get the xmlDoc corresponding to the |
3890 | | * current document being accessed by the xmlTextReader. |
3891 | | * NOTE: as a result of this call, the reader will not destroy the |
3892 | | * associated XML document and calling #xmlFreeDoc on the result |
3893 | | * is needed once the reader parsing has finished. |
3894 | | * |
3895 | | * @param reader the xmlTextReader used |
3896 | | * @returns the xmlDoc or NULL in case of error. |
3897 | | */ |
3898 | | xmlDoc * |
3899 | 0 | xmlTextReaderCurrentDoc(xmlTextReader *reader) { |
3900 | 0 | if (reader == NULL) |
3901 | 0 | return(NULL); |
3902 | 0 | if (reader->doc != NULL) |
3903 | 0 | return(reader->doc); |
3904 | 0 | if ((reader->ctxt == NULL) || (reader->ctxt->myDoc == NULL)) |
3905 | 0 | return(NULL); |
3906 | | |
3907 | 0 | reader->preserve = 1; |
3908 | 0 | return(reader->ctxt->myDoc); |
3909 | 0 | } |
3910 | | |
3911 | | #ifdef LIBXML_RELAXNG_ENABLED |
3912 | | /** |
3913 | | * Use RelaxNG to validate the document as it is processed. |
3914 | | * Activation is only possible before the first Read(). |
3915 | | * if `schema` is NULL, then RelaxNG validation is deactivated. |
3916 | | * The `schema` should not be freed until the reader is deallocated |
3917 | | * or its use has been deactivated. |
3918 | | * |
3919 | | * @param reader the xmlTextReader used |
3920 | | * @param schema a precompiled RelaxNG schema |
3921 | | * @returns 0 in case the RelaxNG validation could be (de)activated and |
3922 | | * -1 in case of error. |
3923 | | */ |
3924 | | int |
3925 | 0 | xmlTextReaderRelaxNGSetSchema(xmlTextReader *reader, xmlRelaxNG *schema) { |
3926 | 0 | if (reader == NULL) |
3927 | 0 | return(-1); |
3928 | 0 | if (schema == NULL) { |
3929 | 0 | if (reader->rngSchemas != NULL) { |
3930 | 0 | xmlRelaxNGFree(reader->rngSchemas); |
3931 | 0 | reader->rngSchemas = NULL; |
3932 | 0 | } |
3933 | 0 | if (reader->rngValidCtxt != NULL) { |
3934 | 0 | if (! reader->rngPreserveCtxt) |
3935 | 0 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
3936 | 0 | reader->rngValidCtxt = NULL; |
3937 | 0 | } |
3938 | 0 | reader->rngPreserveCtxt = 0; |
3939 | 0 | return(0); |
3940 | 0 | } |
3941 | 0 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
3942 | 0 | return(-1); |
3943 | 0 | if (reader->rngSchemas != NULL) { |
3944 | 0 | xmlRelaxNGFree(reader->rngSchemas); |
3945 | 0 | reader->rngSchemas = NULL; |
3946 | 0 | } |
3947 | 0 | if (reader->rngValidCtxt != NULL) { |
3948 | 0 | if (! reader->rngPreserveCtxt) |
3949 | 0 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
3950 | 0 | reader->rngValidCtxt = NULL; |
3951 | 0 | } |
3952 | 0 | reader->rngPreserveCtxt = 0; |
3953 | 0 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema); |
3954 | 0 | if (reader->rngValidCtxt == NULL) |
3955 | 0 | return(-1); |
3956 | 0 | if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL)) |
3957 | 0 | xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, |
3958 | 0 | xmlTextReaderStructuredRelay, reader); |
3959 | 0 | reader->rngValidErrors = 0; |
3960 | 0 | reader->rngFullNode = NULL; |
3961 | 0 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; |
3962 | 0 | return(0); |
3963 | 0 | } |
3964 | | #endif /* LIBXML_RELAXNG_ENABLED */ |
3965 | | |
3966 | | #ifdef LIBXML_SCHEMAS_ENABLED |
3967 | | /** |
3968 | | * Internal locator function for the readers |
3969 | | * |
3970 | | * @param ctx the xmlTextReader used |
3971 | | * @param file returned file information |
3972 | | * @param line returned line information |
3973 | | * @returns 0 in case the Schema validation could be (de)activated and |
3974 | | * -1 in case of error. |
3975 | | */ |
3976 | | static int |
3977 | 0 | xmlTextReaderLocator(void *ctx, const char **file, unsigned long *line) { |
3978 | 0 | xmlTextReaderPtr reader; |
3979 | |
|
3980 | 0 | if ((ctx == NULL) || ((file == NULL) && (line == NULL))) |
3981 | 0 | return(-1); |
3982 | | |
3983 | 0 | if (file != NULL) |
3984 | 0 | *file = NULL; |
3985 | 0 | if (line != NULL) |
3986 | 0 | *line = 0; |
3987 | |
|
3988 | 0 | reader = (xmlTextReaderPtr) ctx; |
3989 | 0 | if ((reader->ctxt != NULL) && (reader->ctxt->input != NULL)) { |
3990 | 0 | if (file != NULL) |
3991 | 0 | *file = reader->ctxt->input->filename; |
3992 | 0 | if (line != NULL) |
3993 | 0 | *line = reader->ctxt->input->line; |
3994 | 0 | return(0); |
3995 | 0 | } |
3996 | 0 | if (reader->node != NULL) { |
3997 | 0 | long res; |
3998 | 0 | int ret = 0; |
3999 | |
|
4000 | 0 | if (line != NULL) { |
4001 | 0 | res = xmlGetLineNo(reader->node); |
4002 | 0 | if (res > 0) |
4003 | 0 | *line = (unsigned long) res; |
4004 | 0 | else |
4005 | 0 | ret = -1; |
4006 | 0 | } |
4007 | 0 | if (file != NULL) { |
4008 | 0 | xmlDocPtr doc = reader->node->doc; |
4009 | 0 | if ((doc != NULL) && (doc->URL != NULL)) |
4010 | 0 | *file = (const char *) doc->URL; |
4011 | 0 | else |
4012 | 0 | ret = -1; |
4013 | 0 | } |
4014 | 0 | return(ret); |
4015 | 0 | } |
4016 | 0 | return(-1); |
4017 | 0 | } |
4018 | | |
4019 | | /** |
4020 | | * Use XSD Schema to validate the document as it is processed. |
4021 | | * Activation is only possible before the first Read(). |
4022 | | * if `schema` is NULL, then Schema validation is deactivated. |
4023 | | * The `schema` should not be freed until the reader is deallocated |
4024 | | * or its use has been deactivated. |
4025 | | * |
4026 | | * @param reader the xmlTextReader used |
4027 | | * @param schema a precompiled Schema schema |
4028 | | * @returns 0 in case the Schema validation could be (de)activated and |
4029 | | * -1 in case of error. |
4030 | | */ |
4031 | | int |
4032 | 0 | xmlTextReaderSetSchema(xmlTextReader *reader, xmlSchema *schema) { |
4033 | 0 | if (reader == NULL) |
4034 | 0 | return(-1); |
4035 | 0 | if (schema == NULL) { |
4036 | 0 | if (reader->xsdPlug != NULL) { |
4037 | 0 | xmlSchemaSAXUnplug(reader->xsdPlug); |
4038 | 0 | reader->xsdPlug = NULL; |
4039 | 0 | } |
4040 | 0 | if (reader->xsdValidCtxt != NULL) { |
4041 | 0 | if (! reader->xsdPreserveCtxt) |
4042 | 0 | xmlSchemaFreeValidCtxt(reader->xsdValidCtxt); |
4043 | 0 | reader->xsdValidCtxt = NULL; |
4044 | 0 | } |
4045 | 0 | reader->xsdPreserveCtxt = 0; |
4046 | 0 | if (reader->xsdSchemas != NULL) { |
4047 | 0 | xmlSchemaFree(reader->xsdSchemas); |
4048 | 0 | reader->xsdSchemas = NULL; |
4049 | 0 | } |
4050 | 0 | return(0); |
4051 | 0 | } |
4052 | 0 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
4053 | 0 | return(-1); |
4054 | 0 | if (reader->xsdPlug != NULL) { |
4055 | 0 | xmlSchemaSAXUnplug(reader->xsdPlug); |
4056 | 0 | reader->xsdPlug = NULL; |
4057 | 0 | } |
4058 | 0 | if (reader->xsdValidCtxt != NULL) { |
4059 | 0 | if (! reader->xsdPreserveCtxt) |
4060 | 0 | xmlSchemaFreeValidCtxt(reader->xsdValidCtxt); |
4061 | 0 | reader->xsdValidCtxt = NULL; |
4062 | 0 | } |
4063 | 0 | reader->xsdPreserveCtxt = 0; |
4064 | 0 | if (reader->xsdSchemas != NULL) { |
4065 | 0 | xmlSchemaFree(reader->xsdSchemas); |
4066 | 0 | reader->xsdSchemas = NULL; |
4067 | 0 | } |
4068 | 0 | reader->xsdValidCtxt = xmlSchemaNewValidCtxt(schema); |
4069 | 0 | if (reader->xsdValidCtxt == NULL) { |
4070 | 0 | xmlSchemaFree(reader->xsdSchemas); |
4071 | 0 | reader->xsdSchemas = NULL; |
4072 | 0 | return(-1); |
4073 | 0 | } |
4074 | 0 | reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt, |
4075 | 0 | &(reader->ctxt->sax), |
4076 | 0 | &(reader->ctxt->userData)); |
4077 | 0 | if (reader->xsdPlug == NULL) { |
4078 | 0 | xmlSchemaFree(reader->xsdSchemas); |
4079 | 0 | reader->xsdSchemas = NULL; |
4080 | 0 | xmlSchemaFreeValidCtxt(reader->xsdValidCtxt); |
4081 | 0 | reader->xsdValidCtxt = NULL; |
4082 | 0 | return(-1); |
4083 | 0 | } |
4084 | 0 | xmlSchemaValidateSetLocator(reader->xsdValidCtxt, |
4085 | 0 | xmlTextReaderLocator, |
4086 | 0 | (void *) reader); |
4087 | |
|
4088 | 0 | if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL)) |
4089 | 0 | xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, |
4090 | 0 | xmlTextReaderStructuredRelay, reader); |
4091 | 0 | reader->xsdValidErrors = 0; |
4092 | 0 | reader->validate = XML_TEXTREADER_VALIDATE_XSD; |
4093 | 0 | return(0); |
4094 | 0 | } |
4095 | | #endif /* LIBXML_SCHEMAS_ENABLED */ |
4096 | | |
4097 | | #ifdef LIBXML_RELAXNG_ENABLED |
4098 | | /** |
4099 | | * Use RelaxNG to validate the document as it is processed. |
4100 | | * Activation is only possible before the first Read(). |
4101 | | * If both `rng` and `ctxt` are NULL, then RelaxNG validation is deactivated. |
4102 | | * |
4103 | | * @param reader the xmlTextReader used |
4104 | | * @param rng the path to a RelaxNG schema or NULL |
4105 | | * @param ctxt the RelaxNG schema validation context or NULL |
4106 | | * @param options options (not yet used) |
4107 | | * @returns 0 in case the RelaxNG validation could be (de)activated and |
4108 | | * -1 in case of error. |
4109 | | */ |
4110 | | static int |
4111 | | xmlTextReaderRelaxNGValidateInternal(xmlTextReaderPtr reader, |
4112 | | const char *rng, |
4113 | | xmlRelaxNGValidCtxtPtr ctxt, |
4114 | | int options ATTRIBUTE_UNUSED) |
4115 | 0 | { |
4116 | 0 | if (reader == NULL) |
4117 | 0 | return(-1); |
4118 | | |
4119 | 0 | if ((rng != NULL) && (ctxt != NULL)) |
4120 | 0 | return (-1); |
4121 | | |
4122 | 0 | if (((rng != NULL) || (ctxt != NULL)) && |
4123 | 0 | ((reader->mode != XML_TEXTREADER_MODE_INITIAL) || |
4124 | 0 | (reader->ctxt == NULL))) |
4125 | 0 | return(-1); |
4126 | | |
4127 | | /* Cleanup previous validation stuff. */ |
4128 | 0 | if (reader->rngValidCtxt != NULL) { |
4129 | 0 | if ( !reader->rngPreserveCtxt) |
4130 | 0 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
4131 | 0 | reader->rngValidCtxt = NULL; |
4132 | 0 | } |
4133 | 0 | reader->rngPreserveCtxt = 0; |
4134 | 0 | if (reader->rngSchemas != NULL) { |
4135 | 0 | xmlRelaxNGFree(reader->rngSchemas); |
4136 | 0 | reader->rngSchemas = NULL; |
4137 | 0 | } |
4138 | |
|
4139 | 0 | if ((rng == NULL) && (ctxt == NULL)) { |
4140 | | /* We just want to deactivate the validation, so get out. */ |
4141 | 0 | return(0); |
4142 | 0 | } |
4143 | | |
4144 | | |
4145 | 0 | if (rng != NULL) { |
4146 | 0 | xmlRelaxNGParserCtxtPtr pctxt; |
4147 | | /* Parse the schema and create validation environment. */ |
4148 | |
|
4149 | 0 | pctxt = xmlRelaxNGNewParserCtxt(rng); |
4150 | 0 | if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL)) |
4151 | 0 | xmlRelaxNGSetParserStructuredErrors(pctxt, |
4152 | 0 | xmlTextReaderStructuredRelay, reader); |
4153 | 0 | reader->rngSchemas = xmlRelaxNGParse(pctxt); |
4154 | 0 | xmlRelaxNGFreeParserCtxt(pctxt); |
4155 | 0 | if (reader->rngSchemas == NULL) |
4156 | 0 | return(-1); |
4157 | | |
4158 | 0 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas); |
4159 | 0 | if (reader->rngValidCtxt == NULL) { |
4160 | 0 | xmlRelaxNGFree(reader->rngSchemas); |
4161 | 0 | reader->rngSchemas = NULL; |
4162 | 0 | return(-1); |
4163 | 0 | } |
4164 | 0 | } else { |
4165 | | /* Use the given validation context. */ |
4166 | 0 | reader->rngValidCtxt = ctxt; |
4167 | 0 | reader->rngPreserveCtxt = 1; |
4168 | 0 | } |
4169 | | /* |
4170 | | * Redirect the validation context's error channels to use |
4171 | | * the reader channels. |
4172 | | * TODO: In case the user provides the validation context we |
4173 | | * could make this redirection optional. |
4174 | | */ |
4175 | 0 | if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL)) |
4176 | 0 | xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, |
4177 | 0 | xmlTextReaderStructuredRelay, reader); |
4178 | 0 | reader->rngValidErrors = 0; |
4179 | 0 | reader->rngFullNode = NULL; |
4180 | 0 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; |
4181 | 0 | return(0); |
4182 | 0 | } |
4183 | | #endif /* LIBXML_RELAXNG_ENABLED */ |
4184 | | |
4185 | | #ifdef LIBXML_SCHEMAS_ENABLED |
4186 | | /** |
4187 | | * Validate the document as it is processed using XML Schema. |
4188 | | * Activation is only possible before the first Read(). |
4189 | | * If both `xsd` and `ctxt` are NULL then XML Schema validation is deactivated. |
4190 | | * |
4191 | | * @param reader the xmlTextReader used |
4192 | | * @param xsd the path to a W3C XSD schema or NULL |
4193 | | * @param ctxt the XML Schema validation context or NULL |
4194 | | * @param options options (not used yet) |
4195 | | * @returns 0 in case the schemas validation could be (de)activated and |
4196 | | * -1 in case of error. |
4197 | | */ |
4198 | | static int |
4199 | | xmlTextReaderSchemaValidateInternal(xmlTextReaderPtr reader, |
4200 | | const char *xsd, |
4201 | | xmlSchemaValidCtxtPtr ctxt, |
4202 | | int options ATTRIBUTE_UNUSED) |
4203 | 0 | { |
4204 | 0 | if (reader == NULL) |
4205 | 0 | return(-1); |
4206 | | |
4207 | 0 | if ((xsd != NULL) && (ctxt != NULL)) |
4208 | 0 | return(-1); |
4209 | | |
4210 | 0 | if (((xsd != NULL) || (ctxt != NULL)) && |
4211 | 0 | ((reader->mode != XML_TEXTREADER_MODE_INITIAL) || |
4212 | 0 | (reader->ctxt == NULL))) |
4213 | 0 | return(-1); |
4214 | | |
4215 | | /* Cleanup previous validation stuff. */ |
4216 | 0 | if (reader->xsdPlug != NULL) { |
4217 | 0 | xmlSchemaSAXUnplug(reader->xsdPlug); |
4218 | 0 | reader->xsdPlug = NULL; |
4219 | 0 | } |
4220 | 0 | if (reader->xsdValidCtxt != NULL) { |
4221 | 0 | if (! reader->xsdPreserveCtxt) |
4222 | 0 | xmlSchemaFreeValidCtxt(reader->xsdValidCtxt); |
4223 | 0 | reader->xsdValidCtxt = NULL; |
4224 | 0 | } |
4225 | 0 | reader->xsdPreserveCtxt = 0; |
4226 | 0 | if (reader->xsdSchemas != NULL) { |
4227 | 0 | xmlSchemaFree(reader->xsdSchemas); |
4228 | 0 | reader->xsdSchemas = NULL; |
4229 | 0 | } |
4230 | |
|
4231 | 0 | if ((xsd == NULL) && (ctxt == NULL)) { |
4232 | | /* We just want to deactivate the validation, so get out. */ |
4233 | 0 | return(0); |
4234 | 0 | } |
4235 | | |
4236 | 0 | if (xsd != NULL) { |
4237 | 0 | xmlSchemaParserCtxtPtr pctxt; |
4238 | | /* Parse the schema and create validation environment. */ |
4239 | 0 | pctxt = xmlSchemaNewParserCtxt(xsd); |
4240 | 0 | if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL)) |
4241 | 0 | xmlSchemaSetParserStructuredErrors(pctxt, |
4242 | 0 | xmlTextReaderStructuredRelay, reader); |
4243 | 0 | reader->xsdSchemas = xmlSchemaParse(pctxt); |
4244 | 0 | xmlSchemaFreeParserCtxt(pctxt); |
4245 | 0 | if (reader->xsdSchemas == NULL) |
4246 | 0 | return(-1); |
4247 | 0 | reader->xsdValidCtxt = xmlSchemaNewValidCtxt(reader->xsdSchemas); |
4248 | 0 | if (reader->xsdValidCtxt == NULL) { |
4249 | 0 | xmlSchemaFree(reader->xsdSchemas); |
4250 | 0 | reader->xsdSchemas = NULL; |
4251 | 0 | return(-1); |
4252 | 0 | } |
4253 | 0 | reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt, |
4254 | 0 | &(reader->ctxt->sax), |
4255 | 0 | &(reader->ctxt->userData)); |
4256 | 0 | if (reader->xsdPlug == NULL) { |
4257 | 0 | xmlSchemaFree(reader->xsdSchemas); |
4258 | 0 | reader->xsdSchemas = NULL; |
4259 | 0 | xmlSchemaFreeValidCtxt(reader->xsdValidCtxt); |
4260 | 0 | reader->xsdValidCtxt = NULL; |
4261 | 0 | return(-1); |
4262 | 0 | } |
4263 | 0 | } else { |
4264 | | /* Use the given validation context. */ |
4265 | 0 | reader->xsdValidCtxt = ctxt; |
4266 | 0 | reader->xsdPreserveCtxt = 1; |
4267 | 0 | reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt, |
4268 | 0 | &(reader->ctxt->sax), |
4269 | 0 | &(reader->ctxt->userData)); |
4270 | 0 | if (reader->xsdPlug == NULL) { |
4271 | 0 | reader->xsdValidCtxt = NULL; |
4272 | 0 | reader->xsdPreserveCtxt = 0; |
4273 | 0 | return(-1); |
4274 | 0 | } |
4275 | 0 | } |
4276 | 0 | xmlSchemaValidateSetLocator(reader->xsdValidCtxt, |
4277 | 0 | xmlTextReaderLocator, |
4278 | 0 | (void *) reader); |
4279 | | /* |
4280 | | * Redirect the validation context's error channels to use |
4281 | | * the reader channels. |
4282 | | * TODO: In case the user provides the validation context we |
4283 | | * could make this redirection optional. |
4284 | | */ |
4285 | 0 | if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL)) |
4286 | 0 | xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, |
4287 | 0 | xmlTextReaderStructuredRelay, reader); |
4288 | 0 | reader->xsdValidErrors = 0; |
4289 | 0 | reader->validate = XML_TEXTREADER_VALIDATE_XSD; |
4290 | 0 | return(0); |
4291 | 0 | } |
4292 | | |
4293 | | /** |
4294 | | * Use W3C XSD schema context to validate the document as it is processed. |
4295 | | * Activation is only possible before the first Read(). |
4296 | | * If `ctxt` is NULL, then XML Schema validation is deactivated. |
4297 | | * |
4298 | | * @param reader the xmlTextReader used |
4299 | | * @param ctxt the XML Schema validation context or NULL |
4300 | | * @param options options (not used yet) |
4301 | | * @returns 0 in case the schemas validation could be (de)activated and |
4302 | | * -1 in case of error. |
4303 | | */ |
4304 | | int |
4305 | | xmlTextReaderSchemaValidateCtxt(xmlTextReader *reader, |
4306 | | xmlSchemaValidCtxt *ctxt, |
4307 | | int options) |
4308 | 0 | { |
4309 | 0 | return(xmlTextReaderSchemaValidateInternal(reader, NULL, ctxt, options)); |
4310 | 0 | } |
4311 | | |
4312 | | /** |
4313 | | * Use W3C XSD schema to validate the document as it is processed. |
4314 | | * Activation is only possible before the first Read(). |
4315 | | * If `xsd` is NULL, then XML Schema validation is deactivated. |
4316 | | * |
4317 | | * @param reader the xmlTextReader used |
4318 | | * @param xsd the path to a W3C XSD schema or NULL |
4319 | | * @returns 0 in case the schemas validation could be (de)activated and |
4320 | | * -1 in case of error. |
4321 | | */ |
4322 | | int |
4323 | | xmlTextReaderSchemaValidate(xmlTextReader *reader, const char *xsd) |
4324 | 0 | { |
4325 | 0 | return(xmlTextReaderSchemaValidateInternal(reader, xsd, NULL, 0)); |
4326 | 0 | } |
4327 | | #endif /* LIBXML_SCHEMAS_ENABLED */ |
4328 | | |
4329 | | #ifdef LIBXML_RELAXNG_ENABLED |
4330 | | /** |
4331 | | * Use RelaxNG schema context to validate the document as it is processed. |
4332 | | * Activation is only possible before the first Read(). |
4333 | | * If `ctxt` is NULL, then RelaxNG schema validation is deactivated. |
4334 | | * |
4335 | | * @param reader the xmlTextReader used |
4336 | | * @param ctxt the RelaxNG schema validation context or NULL |
4337 | | * @param options options (not used yet) |
4338 | | * @returns 0 in case the schemas validation could be (de)activated and |
4339 | | * -1 in case of error. |
4340 | | */ |
4341 | | int |
4342 | | xmlTextReaderRelaxNGValidateCtxt(xmlTextReader *reader, |
4343 | | xmlRelaxNGValidCtxt *ctxt, |
4344 | | int options) |
4345 | 0 | { |
4346 | 0 | return(xmlTextReaderRelaxNGValidateInternal(reader, NULL, ctxt, options)); |
4347 | 0 | } |
4348 | | |
4349 | | /** |
4350 | | * Use RelaxNG schema to validate the document as it is processed. |
4351 | | * Activation is only possible before the first Read(). |
4352 | | * If `rng` is NULL, then RelaxNG schema validation is deactivated. |
4353 | | * |
4354 | | * @param reader the xmlTextReader used |
4355 | | * @param rng the path to a RelaxNG schema or NULL |
4356 | | * @returns 0 in case the schemas validation could be (de)activated and |
4357 | | * -1 in case of error. |
4358 | | */ |
4359 | | int |
4360 | | xmlTextReaderRelaxNGValidate(xmlTextReader *reader, const char *rng) |
4361 | 0 | { |
4362 | 0 | return(xmlTextReaderRelaxNGValidateInternal(reader, rng, NULL, 0)); |
4363 | 0 | } |
4364 | | #endif /* LIBXML_RELAXNG_ENABLED */ |
4365 | | |
4366 | | /** |
4367 | | * Determine whether the current node is a namespace declaration |
4368 | | * rather than a regular attribute. |
4369 | | * |
4370 | | * @param reader the xmlTextReader used |
4371 | | * @returns 1 if the current node is a namespace declaration, 0 if it |
4372 | | * is a regular attribute or other type of node, or -1 in case of |
4373 | | * error. |
4374 | | */ |
4375 | | int |
4376 | 0 | xmlTextReaderIsNamespaceDecl(xmlTextReader *reader) { |
4377 | 0 | xmlNodePtr node; |
4378 | 0 | if (reader == NULL) |
4379 | 0 | return(-1); |
4380 | 0 | if (reader->node == NULL) |
4381 | 0 | return(-1); |
4382 | 0 | if (reader->curnode != NULL) |
4383 | 0 | node = reader->curnode; |
4384 | 0 | else |
4385 | 0 | node = reader->node; |
4386 | |
|
4387 | 0 | if (XML_NAMESPACE_DECL == node->type) |
4388 | 0 | return(1); |
4389 | 0 | else |
4390 | 0 | return(0); |
4391 | 0 | } |
4392 | | |
4393 | | /** |
4394 | | * Determine the XML version of the document being read. |
4395 | | * |
4396 | | * @param reader the xmlTextReader used |
4397 | | * @returns a string containing the XML version of the document or NULL |
4398 | | * in case of error. The string is deallocated with the reader. |
4399 | | */ |
4400 | | const xmlChar * |
4401 | 0 | xmlTextReaderConstXmlVersion(xmlTextReader *reader) { |
4402 | 0 | xmlDocPtr doc = NULL; |
4403 | 0 | if (reader == NULL) |
4404 | 0 | return(NULL); |
4405 | 0 | if (reader->doc != NULL) |
4406 | 0 | doc = reader->doc; |
4407 | 0 | else if (reader->ctxt != NULL) |
4408 | 0 | doc = reader->ctxt->myDoc; |
4409 | 0 | if (doc == NULL) |
4410 | 0 | return(NULL); |
4411 | | |
4412 | 0 | if (doc->version == NULL) |
4413 | 0 | return(NULL); |
4414 | 0 | else |
4415 | 0 | return(constString(reader, doc->version)); |
4416 | 0 | } |
4417 | | |
4418 | | /** |
4419 | | * Determine the standalone status of the document being read. |
4420 | | * |
4421 | | * @param reader the xmlTextReader used |
4422 | | * @returns 1 if the document was declared to be standalone, 0 if it |
4423 | | * was declared to be not standalone, or -1 if the document did not |
4424 | | * specify its standalone status or in case of error. |
4425 | | */ |
4426 | | int |
4427 | 0 | xmlTextReaderStandalone(xmlTextReader *reader) { |
4428 | 0 | xmlDocPtr doc = NULL; |
4429 | 0 | if (reader == NULL) |
4430 | 0 | return(-1); |
4431 | 0 | if (reader->doc != NULL) |
4432 | 0 | doc = reader->doc; |
4433 | 0 | else if (reader->ctxt != NULL) |
4434 | 0 | doc = reader->ctxt->myDoc; |
4435 | 0 | if (doc == NULL) |
4436 | 0 | return(-1); |
4437 | | |
4438 | 0 | return(doc->standalone); |
4439 | 0 | } |
4440 | | |
4441 | | /************************************************************************ |
4442 | | * * |
4443 | | * Error Handling Extensions * |
4444 | | * * |
4445 | | ************************************************************************/ |
4446 | | |
4447 | | /** |
4448 | | * Obtain the line number for the given locator. |
4449 | | * |
4450 | | * @param locator the void used |
4451 | | * @returns the line number or -1 in case of error. |
4452 | | */ |
4453 | | int |
4454 | 0 | xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) { |
4455 | | /* we know that locator is a xmlParserCtxtPtr */ |
4456 | 0 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
4457 | 0 | int ret = -1; |
4458 | |
|
4459 | 0 | if (locator == NULL) |
4460 | 0 | return(-1); |
4461 | 0 | if (ctx->node != NULL) { |
4462 | 0 | ret = xmlGetLineNo(ctx->node); |
4463 | 0 | } |
4464 | 0 | else { |
4465 | | /* inspired from error.c */ |
4466 | 0 | xmlParserInputPtr input; |
4467 | 0 | input = ctx->input; |
4468 | 0 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
4469 | 0 | input = ctx->inputTab[ctx->inputNr - 2]; |
4470 | 0 | if (input != NULL) { |
4471 | 0 | ret = input->line; |
4472 | 0 | } |
4473 | 0 | else { |
4474 | 0 | ret = -1; |
4475 | 0 | } |
4476 | 0 | } |
4477 | |
|
4478 | 0 | return ret; |
4479 | 0 | } |
4480 | | |
4481 | | /** |
4482 | | * Obtain the base URI for the given locator. |
4483 | | * |
4484 | | * @param locator the void used |
4485 | | * @returns the base URI or NULL in case of error, |
4486 | | * if non NULL it need to be freed by the caller. |
4487 | | */ |
4488 | | xmlChar * |
4489 | 0 | xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) { |
4490 | | /* we know that locator is a xmlParserCtxtPtr */ |
4491 | 0 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
4492 | 0 | xmlChar *ret = NULL; |
4493 | |
|
4494 | 0 | if (locator == NULL) |
4495 | 0 | return(NULL); |
4496 | 0 | if (ctx->node != NULL) { |
4497 | 0 | ret = xmlNodeGetBase(NULL,ctx->node); |
4498 | 0 | } |
4499 | 0 | else { |
4500 | | /* inspired from error.c */ |
4501 | 0 | xmlParserInputPtr input; |
4502 | 0 | input = ctx->input; |
4503 | 0 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
4504 | 0 | input = ctx->inputTab[ctx->inputNr - 2]; |
4505 | 0 | if (input != NULL) { |
4506 | 0 | ret = xmlStrdup(BAD_CAST input->filename); |
4507 | 0 | } |
4508 | 0 | else { |
4509 | 0 | ret = NULL; |
4510 | 0 | } |
4511 | 0 | } |
4512 | |
|
4513 | 0 | return ret; |
4514 | 0 | } |
4515 | | |
4516 | | /** |
4517 | | * Register a callback function that will be called on error and warnings. |
4518 | | * |
4519 | | * @deprecated Use #xmlTextReaderSetStructuredErrorHandler. |
4520 | | * |
4521 | | * If `f` is NULL, the default error and warning handlers are restored. |
4522 | | * |
4523 | | * @param reader the xmlTextReader used |
4524 | | * @param f the callback function to call on error and warnings |
4525 | | * @param arg a user argument to pass to the callback function |
4526 | | */ |
4527 | | void |
4528 | | xmlTextReaderSetErrorHandler(xmlTextReader *reader, |
4529 | | xmlTextReaderErrorFunc f, void *arg) |
4530 | 0 | { |
4531 | 0 | if (reader == NULL) |
4532 | 0 | return; |
4533 | | |
4534 | 0 | if (f != NULL) { |
4535 | 0 | reader->errorFunc = f; |
4536 | 0 | reader->sErrorFunc = NULL; |
4537 | 0 | reader->errorFuncArg = arg; |
4538 | 0 | xmlCtxtSetErrorHandler(reader->ctxt, |
4539 | 0 | xmlTextReaderStructuredRelay, reader); |
4540 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
4541 | 0 | if (reader->rngValidCtxt) { |
4542 | 0 | xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, |
4543 | 0 | xmlTextReaderStructuredRelay, reader); |
4544 | 0 | } |
4545 | 0 | #endif |
4546 | 0 | #ifdef LIBXML_SCHEMAS_ENABLED |
4547 | 0 | if (reader->xsdValidCtxt) { |
4548 | 0 | xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, |
4549 | 0 | xmlTextReaderStructuredRelay, reader); |
4550 | 0 | } |
4551 | 0 | #endif |
4552 | 0 | } else { |
4553 | | /* restore defaults */ |
4554 | 0 | reader->errorFunc = NULL; |
4555 | 0 | reader->sErrorFunc = NULL; |
4556 | 0 | reader->errorFuncArg = NULL; |
4557 | 0 | xmlCtxtSetErrorHandler(reader->ctxt, NULL, NULL); |
4558 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
4559 | 0 | if (reader->rngValidCtxt) { |
4560 | 0 | xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL, |
4561 | 0 | NULL); |
4562 | 0 | } |
4563 | 0 | #endif |
4564 | 0 | #ifdef LIBXML_SCHEMAS_ENABLED |
4565 | 0 | if (reader->xsdValidCtxt) { |
4566 | 0 | xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL, |
4567 | 0 | NULL); |
4568 | 0 | } |
4569 | 0 | #endif |
4570 | 0 | } |
4571 | 0 | } |
4572 | | |
4573 | | /** |
4574 | | * xmlTextReaderSetStructuredErrorHandler: |
4575 | | * |
4576 | | * Register a callback function that will be called on error and warnings. |
4577 | | * |
4578 | | * If `f` is NULL, the default error and warning handlers are restored. |
4579 | | * |
4580 | | * @param reader the xmlTextReader used |
4581 | | * @param f the callback function to call on error and warnings |
4582 | | * @param arg a user argument to pass to the callback function |
4583 | | */ |
4584 | | void |
4585 | | xmlTextReaderSetStructuredErrorHandler(xmlTextReader *reader, |
4586 | | xmlStructuredErrorFunc f, void *arg) |
4587 | 0 | { |
4588 | 0 | if (reader == NULL) |
4589 | 0 | return; |
4590 | | |
4591 | 0 | if (f != NULL) { |
4592 | 0 | reader->sErrorFunc = f; |
4593 | 0 | reader->errorFunc = NULL; |
4594 | 0 | reader->errorFuncArg = arg; |
4595 | 0 | xmlCtxtSetErrorHandler(reader->ctxt, |
4596 | 0 | xmlTextReaderStructuredRelay, reader); |
4597 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
4598 | 0 | if (reader->rngValidCtxt) { |
4599 | 0 | xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, |
4600 | 0 | xmlTextReaderStructuredRelay, reader); |
4601 | 0 | } |
4602 | 0 | #endif |
4603 | 0 | #ifdef LIBXML_SCHEMAS_ENABLED |
4604 | 0 | if (reader->xsdValidCtxt) { |
4605 | 0 | xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, |
4606 | 0 | xmlTextReaderStructuredRelay, reader); |
4607 | 0 | } |
4608 | 0 | #endif |
4609 | 0 | } else { |
4610 | | /* restore defaults */ |
4611 | 0 | reader->errorFunc = NULL; |
4612 | 0 | reader->sErrorFunc = NULL; |
4613 | 0 | reader->errorFuncArg = NULL; |
4614 | 0 | xmlCtxtSetErrorHandler(reader->ctxt, NULL, NULL); |
4615 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
4616 | 0 | if (reader->rngValidCtxt) { |
4617 | 0 | xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL, |
4618 | 0 | NULL); |
4619 | 0 | } |
4620 | 0 | #endif |
4621 | 0 | #ifdef LIBXML_SCHEMAS_ENABLED |
4622 | 0 | if (reader->xsdValidCtxt) { |
4623 | 0 | xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL, |
4624 | 0 | NULL); |
4625 | 0 | } |
4626 | 0 | #endif |
4627 | 0 | } |
4628 | 0 | } |
4629 | | |
4630 | | /** |
4631 | | * Retrieve the error callback function and user argument. |
4632 | | * |
4633 | | * @param reader the xmlTextReader used |
4634 | | * @param f the callback function or NULL is no callback has been registered |
4635 | | * @param arg a user argument |
4636 | | */ |
4637 | | void |
4638 | | xmlTextReaderGetErrorHandler(xmlTextReader *reader, |
4639 | | xmlTextReaderErrorFunc * f, void **arg) |
4640 | 0 | { |
4641 | 0 | if (f != NULL) |
4642 | 0 | *f = reader->errorFunc; |
4643 | 0 | if (arg != NULL) |
4644 | 0 | *arg = reader->errorFuncArg; |
4645 | 0 | } |
4646 | | |
4647 | | /** |
4648 | | * Register a callback function that will be called to load external |
4649 | | * resources like entities. |
4650 | | * |
4651 | | * @since 2.14.0 |
4652 | | * @param reader thr reader |
4653 | | * @param loader resource loader |
4654 | | * @param data user data which will be passed to the loader |
4655 | | */ |
4656 | | void |
4657 | | xmlTextReaderSetResourceLoader(xmlTextReader *reader, |
4658 | 0 | xmlResourceLoader loader, void *data) { |
4659 | 0 | if ((reader == NULL) || (reader->ctxt == NULL)) |
4660 | 0 | return; |
4661 | | |
4662 | 0 | reader->resourceLoader = loader; |
4663 | 0 | reader->resourceCtxt = data; |
4664 | |
|
4665 | 0 | xmlCtxtSetResourceLoader(reader->ctxt, loader, data); |
4666 | 0 | } |
4667 | | |
4668 | | /** |
4669 | | * Retrieve the validity status from the parser context |
4670 | | * |
4671 | | * @param reader the xmlTextReader used |
4672 | | * @returns the flag value 1 if valid, 0 if no, and -1 in case of error |
4673 | | */ |
4674 | | int |
4675 | | xmlTextReaderIsValid(xmlTextReader *reader) |
4676 | 0 | { |
4677 | 0 | if (reader == NULL) |
4678 | 0 | return (-1); |
4679 | 0 | #ifdef LIBXML_RELAXNG_ENABLED |
4680 | 0 | if (reader->validate == XML_TEXTREADER_VALIDATE_RNG) |
4681 | 0 | return (reader->rngValidErrors == 0); |
4682 | 0 | #endif |
4683 | 0 | #ifdef LIBXML_SCHEMAS_ENABLED |
4684 | 0 | if (reader->validate == XML_TEXTREADER_VALIDATE_XSD) |
4685 | 0 | return (reader->xsdValidErrors == 0); |
4686 | 0 | #endif |
4687 | 0 | if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1)) |
4688 | 0 | return (reader->ctxt->valid); |
4689 | 0 | return (0); |
4690 | 0 | } |
4691 | | |
4692 | | /************************************************************************ |
4693 | | * * |
4694 | | * New set (2.6.0) of simpler and more flexible APIs * |
4695 | | * * |
4696 | | ************************************************************************/ |
4697 | | |
4698 | | /** |
4699 | | * Setup an XML reader with new options |
4700 | | * |
4701 | | * @param reader an XML reader |
4702 | | * @param input xmlParserInputBuffer used to feed the reader, will |
4703 | | * be destroyed with it. |
4704 | | * @param URL the base URL to use for the document |
4705 | | * @param encoding the document encoding, or NULL |
4706 | | * @param options a combination of xmlParserOption |
4707 | | * @returns 0 in case of success and -1 in case of error. |
4708 | | */ |
4709 | | int |
4710 | | xmlTextReaderSetup(xmlTextReader *reader, |
4711 | | xmlParserInputBuffer *input, const char *URL, |
4712 | | const char *encoding, int options) |
4713 | 0 | { |
4714 | 0 | if (reader == NULL) { |
4715 | 0 | if (input != NULL) |
4716 | 0 | xmlFreeParserInputBuffer(input); |
4717 | 0 | return (-1); |
4718 | 0 | } |
4719 | | |
4720 | | /* |
4721 | | * we force the generation of compact text nodes on the reader |
4722 | | * since usr applications should never modify the tree |
4723 | | */ |
4724 | 0 | options |= XML_PARSE_COMPACT; |
4725 | |
|
4726 | 0 | reader->doc = NULL; |
4727 | 0 | reader->entNr = 0; |
4728 | 0 | reader->parserFlags = options; |
4729 | 0 | reader->validate = XML_TEXTREADER_NOT_VALIDATE; |
4730 | 0 | if ((input != NULL) && (reader->input != NULL) && |
4731 | 0 | (reader->allocs & XML_TEXTREADER_INPUT)) { |
4732 | 0 | xmlFreeParserInputBuffer(reader->input); |
4733 | 0 | reader->input = NULL; |
4734 | 0 | reader->allocs -= XML_TEXTREADER_INPUT; |
4735 | 0 | } |
4736 | 0 | if (input != NULL) { |
4737 | 0 | reader->input = input; |
4738 | 0 | reader->allocs |= XML_TEXTREADER_INPUT; |
4739 | 0 | } |
4740 | 0 | if (reader->buffer == NULL) |
4741 | 0 | reader->buffer = xmlBufCreate(50); |
4742 | 0 | if (reader->buffer == NULL) { |
4743 | 0 | return (-1); |
4744 | 0 | } |
4745 | 0 | if (reader->sax == NULL) |
4746 | 0 | reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
4747 | 0 | if (reader->sax == NULL) { |
4748 | 0 | return (-1); |
4749 | 0 | } |
4750 | 0 | xmlSAXVersion(reader->sax, 2); |
4751 | 0 | reader->startElement = reader->sax->startElement; |
4752 | 0 | reader->sax->startElement = xmlTextReaderStartElement; |
4753 | 0 | reader->endElement = reader->sax->endElement; |
4754 | 0 | reader->sax->endElement = xmlTextReaderEndElement; |
4755 | 0 | #ifdef LIBXML_SAX1_ENABLED |
4756 | 0 | if (reader->sax->initialized == XML_SAX2_MAGIC) { |
4757 | 0 | #endif /* LIBXML_SAX1_ENABLED */ |
4758 | 0 | reader->startElementNs = reader->sax->startElementNs; |
4759 | 0 | reader->sax->startElementNs = xmlTextReaderStartElementNs; |
4760 | 0 | reader->endElementNs = reader->sax->endElementNs; |
4761 | 0 | reader->sax->endElementNs = xmlTextReaderEndElementNs; |
4762 | 0 | #ifdef LIBXML_SAX1_ENABLED |
4763 | 0 | } else { |
4764 | 0 | reader->startElementNs = NULL; |
4765 | 0 | reader->endElementNs = NULL; |
4766 | 0 | } |
4767 | 0 | #endif /* LIBXML_SAX1_ENABLED */ |
4768 | 0 | reader->characters = reader->sax->characters; |
4769 | 0 | reader->sax->characters = xmlTextReaderCharacters; |
4770 | 0 | reader->sax->ignorableWhitespace = xmlTextReaderCharacters; |
4771 | 0 | reader->cdataBlock = reader->sax->cdataBlock; |
4772 | 0 | reader->sax->cdataBlock = xmlTextReaderCDataBlock; |
4773 | |
|
4774 | 0 | reader->mode = XML_TEXTREADER_MODE_INITIAL; |
4775 | 0 | reader->node = NULL; |
4776 | 0 | reader->curnode = NULL; |
4777 | 0 | if (input != NULL) { |
4778 | 0 | if (xmlBufUse(reader->input->buffer) < 4) { |
4779 | 0 | xmlParserInputBufferRead(input, 4); |
4780 | 0 | } |
4781 | 0 | if (reader->ctxt == NULL) { |
4782 | 0 | if (xmlBufUse(reader->input->buffer) >= 4) { |
4783 | 0 | reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL, |
4784 | 0 | (const char *) xmlBufContent(reader->input->buffer), |
4785 | 0 | 4, URL); |
4786 | 0 | reader->base = 0; |
4787 | 0 | reader->cur = 4; |
4788 | 0 | } else { |
4789 | 0 | reader->ctxt = |
4790 | 0 | xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL); |
4791 | 0 | reader->base = 0; |
4792 | 0 | reader->cur = 0; |
4793 | 0 | } |
4794 | 0 | if (reader->ctxt == NULL) { |
4795 | 0 | return (-1); |
4796 | 0 | } |
4797 | 0 | } else { |
4798 | 0 | xmlParserInputPtr inputStream; |
4799 | 0 | xmlParserInputBufferPtr buf; |
4800 | |
|
4801 | 0 | xmlCtxtReset(reader->ctxt); |
4802 | 0 | buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
4803 | 0 | if (buf == NULL) return(-1); |
4804 | 0 | inputStream = xmlNewInputStream(reader->ctxt); |
4805 | 0 | if (inputStream == NULL) { |
4806 | 0 | xmlFreeParserInputBuffer(buf); |
4807 | 0 | return(-1); |
4808 | 0 | } |
4809 | | |
4810 | 0 | if (URL == NULL) |
4811 | 0 | inputStream->filename = NULL; |
4812 | 0 | else |
4813 | 0 | inputStream->filename = (char *) |
4814 | 0 | xmlCanonicPath((const xmlChar *) URL); |
4815 | 0 | inputStream->buf = buf; |
4816 | 0 | xmlBufResetInput(buf->buffer, inputStream); |
4817 | |
|
4818 | 0 | if (xmlCtxtPushInput(reader->ctxt, inputStream) < 0) { |
4819 | 0 | xmlFreeInputStream(inputStream); |
4820 | 0 | return(-1); |
4821 | 0 | } |
4822 | 0 | reader->cur = 0; |
4823 | 0 | } |
4824 | 0 | } |
4825 | 0 | if (reader->dict != NULL) { |
4826 | 0 | if (reader->ctxt->dict != NULL) { |
4827 | 0 | if (reader->dict != reader->ctxt->dict) { |
4828 | 0 | xmlDictFree(reader->dict); |
4829 | 0 | reader->dict = reader->ctxt->dict; |
4830 | 0 | } |
4831 | 0 | } else { |
4832 | 0 | reader->ctxt->dict = reader->dict; |
4833 | 0 | } |
4834 | 0 | } else { |
4835 | 0 | if (reader->ctxt->dict == NULL) |
4836 | 0 | reader->ctxt->dict = xmlDictCreate(); |
4837 | 0 | reader->dict = reader->ctxt->dict; |
4838 | 0 | } |
4839 | 0 | reader->ctxt->_private = reader; |
4840 | 0 | reader->ctxt->dictNames = 1; |
4841 | | /* |
4842 | | * use the parser dictionary to allocate all elements and attributes names |
4843 | | */ |
4844 | 0 | reader->ctxt->parseMode = XML_PARSE_READER; |
4845 | |
|
4846 | 0 | #ifdef LIBXML_XINCLUDE_ENABLED |
4847 | 0 | if (reader->xincctxt != NULL) { |
4848 | 0 | xmlXIncludeFreeContext(reader->xincctxt); |
4849 | 0 | reader->xincctxt = NULL; |
4850 | 0 | } |
4851 | 0 | if (options & XML_PARSE_XINCLUDE) { |
4852 | 0 | reader->xinclude = 1; |
4853 | 0 | reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1); |
4854 | 0 | if (reader->xinclude_name == NULL) |
4855 | 0 | return(-1); |
4856 | 0 | options -= XML_PARSE_XINCLUDE; |
4857 | 0 | } else |
4858 | 0 | reader->xinclude = 0; |
4859 | 0 | reader->in_xinclude = 0; |
4860 | 0 | #endif |
4861 | 0 | #ifdef LIBXML_PATTERN_ENABLED |
4862 | 0 | if (reader->patternTab == NULL) { |
4863 | 0 | reader->patternNr = 0; |
4864 | 0 | reader->patternMax = 0; |
4865 | 0 | } |
4866 | 0 | while (reader->patternNr > 0) { |
4867 | 0 | reader->patternNr--; |
4868 | 0 | if (reader->patternTab[reader->patternNr] != NULL) { |
4869 | 0 | xmlFreePattern(reader->patternTab[reader->patternNr]); |
4870 | 0 | reader->patternTab[reader->patternNr] = NULL; |
4871 | 0 | } |
4872 | 0 | } |
4873 | 0 | #endif |
4874 | |
|
4875 | 0 | if (options & XML_PARSE_DTDVALID) |
4876 | 0 | reader->validate = XML_TEXTREADER_VALIDATE_DTD; |
4877 | |
|
4878 | 0 | xmlCtxtUseOptions(reader->ctxt, options); |
4879 | 0 | if (encoding != NULL) |
4880 | 0 | xmlSwitchEncodingName(reader->ctxt, encoding); |
4881 | 0 | if ((URL != NULL) && (reader->ctxt->input != NULL) && |
4882 | 0 | (reader->ctxt->input->filename == NULL)) { |
4883 | 0 | reader->ctxt->input->filename = (char *) |
4884 | 0 | xmlStrdup((const xmlChar *) URL); |
4885 | 0 | if (reader->ctxt->input->filename == NULL) |
4886 | 0 | return(-1); |
4887 | 0 | } |
4888 | | |
4889 | 0 | reader->doc = NULL; |
4890 | |
|
4891 | 0 | return (0); |
4892 | 0 | } |
4893 | | |
4894 | | /** |
4895 | | * Set the maximum amplification factor. See #xmlCtxtSetMaxAmplification. |
4896 | | * |
4897 | | * @param reader an XML reader |
4898 | | * @param maxAmpl maximum amplification factor |
4899 | | */ |
4900 | | void |
4901 | | xmlTextReaderSetMaxAmplification(xmlTextReader *reader, unsigned maxAmpl) |
4902 | 0 | { |
4903 | 0 | if (reader == NULL) |
4904 | 0 | return; |
4905 | 0 | xmlCtxtSetMaxAmplification(reader->ctxt, maxAmpl); |
4906 | 0 | } |
4907 | | |
4908 | | /** |
4909 | | * @since 2.13.0 |
4910 | | * |
4911 | | * @param reader an XML reader |
4912 | | * @returns the last error. |
4913 | | */ |
4914 | | const xmlError * |
4915 | | xmlTextReaderGetLastError(xmlTextReader *reader) |
4916 | 0 | { |
4917 | 0 | if ((reader == NULL) || (reader->ctxt == NULL)) |
4918 | 0 | return(NULL); |
4919 | 0 | return(&reader->ctxt->lastError); |
4920 | 0 | } |
4921 | | |
4922 | | /** |
4923 | | * This function provides the current index of the parser used |
4924 | | * by the reader, relative to the start of the current entity. |
4925 | | * This function actually just wraps a call to #xmlByteConsumed |
4926 | | * for the parser context associated with the reader. |
4927 | | * See #xmlByteConsumed for more information. |
4928 | | * |
4929 | | * @deprecated The returned value is mostly random and useless. |
4930 | | * It reflects the parser reading ahead and is in no way related to |
4931 | | * the current node. |
4932 | | * |
4933 | | * @param reader an XML reader |
4934 | | * @returns the index in bytes from the beginning of the entity or -1 |
4935 | | * in case the index could not be computed. |
4936 | | */ |
4937 | | long |
4938 | 0 | xmlTextReaderByteConsumed(xmlTextReader *reader) { |
4939 | 0 | xmlParserInputPtr in; |
4940 | |
|
4941 | 0 | if ((reader == NULL) || (reader->ctxt == NULL)) |
4942 | 0 | return(-1); |
4943 | 0 | in = reader->ctxt->input; |
4944 | 0 | if (in == NULL) |
4945 | 0 | return(-1); |
4946 | 0 | return(in->consumed + (in->cur - in->base)); |
4947 | 0 | } |
4948 | | |
4949 | | |
4950 | | /** |
4951 | | * Create an xmltextReader for a preparsed document. |
4952 | | * |
4953 | | * @param doc a preparsed document |
4954 | | * @returns the new reader or NULL in case of error. |
4955 | | */ |
4956 | | xmlTextReader * |
4957 | | xmlReaderWalker(xmlDoc *doc) |
4958 | 0 | { |
4959 | 0 | xmlTextReaderPtr ret; |
4960 | |
|
4961 | 0 | if (doc == NULL) |
4962 | 0 | return(NULL); |
4963 | | |
4964 | 0 | ret = xmlMalloc(sizeof(xmlTextReader)); |
4965 | 0 | if (ret == NULL) { |
4966 | 0 | return(NULL); |
4967 | 0 | } |
4968 | 0 | memset(ret, 0, sizeof(xmlTextReader)); |
4969 | 0 | ret->entNr = 0; |
4970 | 0 | ret->input = NULL; |
4971 | 0 | ret->mode = XML_TEXTREADER_MODE_INITIAL; |
4972 | 0 | ret->node = NULL; |
4973 | 0 | ret->curnode = NULL; |
4974 | 0 | ret->base = 0; |
4975 | 0 | ret->cur = 0; |
4976 | 0 | ret->allocs = XML_TEXTREADER_CTXT; |
4977 | 0 | ret->doc = doc; |
4978 | 0 | ret->state = XML_TEXTREADER_START; |
4979 | 0 | ret->dict = xmlDictCreate(); |
4980 | 0 | return(ret); |
4981 | 0 | } |
4982 | | |
4983 | | /** |
4984 | | * Create an xmltextReader for an XML in-memory document. |
4985 | | * The parsing flags `options` are a combination of xmlParserOption. |
4986 | | * |
4987 | | * @param cur a pointer to a zero terminated string |
4988 | | * @param URL the base URL to use for the document |
4989 | | * @param encoding the document encoding, or NULL |
4990 | | * @param options a combination of xmlParserOption |
4991 | | * @returns the new reader or NULL in case of error. |
4992 | | */ |
4993 | | xmlTextReader * |
4994 | | xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding, |
4995 | | int options) |
4996 | 0 | { |
4997 | 0 | int len; |
4998 | |
|
4999 | 0 | if (cur == NULL) |
5000 | 0 | return (NULL); |
5001 | 0 | len = xmlStrlen(cur); |
5002 | |
|
5003 | 0 | return (xmlReaderForMemory |
5004 | 0 | ((const char *) cur, len, URL, encoding, options)); |
5005 | 0 | } |
5006 | | |
5007 | | /** |
5008 | | * parse an XML file from the filesystem or the network. |
5009 | | * The parsing flags `options` are a combination of xmlParserOption. |
5010 | | * |
5011 | | * @param filename a file or URL |
5012 | | * @param encoding the document encoding, or NULL |
5013 | | * @param options a combination of xmlParserOption |
5014 | | * @returns the new reader or NULL in case of error. |
5015 | | */ |
5016 | | xmlTextReader * |
5017 | | xmlReaderForFile(const char *filename, const char *encoding, int options) |
5018 | 0 | { |
5019 | 0 | xmlTextReaderPtr reader; |
5020 | |
|
5021 | 0 | reader = xmlNewTextReaderFilename(filename); |
5022 | 0 | if (reader == NULL) |
5023 | 0 | return (NULL); |
5024 | 0 | if (xmlTextReaderSetup(reader, NULL, NULL, encoding, options) < 0) { |
5025 | 0 | xmlFreeTextReader(reader); |
5026 | 0 | return (NULL); |
5027 | 0 | } |
5028 | 0 | return (reader); |
5029 | 0 | } |
5030 | | |
5031 | | /** |
5032 | | * Create an xmltextReader for an XML in-memory document. |
5033 | | * The parsing flags `options` are a combination of xmlParserOption. |
5034 | | * |
5035 | | * @param buffer a pointer to a char array |
5036 | | * @param size the size of the array |
5037 | | * @param URL the base URL to use for the document |
5038 | | * @param encoding the document encoding, or NULL |
5039 | | * @param options a combination of xmlParserOption |
5040 | | * @returns the new reader or NULL in case of error. |
5041 | | */ |
5042 | | xmlTextReader * |
5043 | | xmlReaderForMemory(const char *buffer, int size, const char *URL, |
5044 | | const char *encoding, int options) |
5045 | 0 | { |
5046 | 0 | xmlTextReaderPtr reader; |
5047 | 0 | xmlParserInputBufferPtr buf; |
5048 | |
|
5049 | 0 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
5050 | 0 | if (buf == NULL) { |
5051 | 0 | return (NULL); |
5052 | 0 | } |
5053 | 0 | reader = xmlNewTextReader(buf, URL); |
5054 | 0 | if (reader == NULL) { |
5055 | 0 | xmlFreeParserInputBuffer(buf); |
5056 | 0 | return (NULL); |
5057 | 0 | } |
5058 | 0 | reader->allocs |= XML_TEXTREADER_INPUT; |
5059 | 0 | if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) { |
5060 | 0 | xmlFreeTextReader(reader); |
5061 | 0 | return (NULL); |
5062 | 0 | } |
5063 | 0 | return (reader); |
5064 | 0 | } |
5065 | | |
5066 | | /** |
5067 | | * Create an xmltextReader for an XML from a file descriptor. |
5068 | | * The parsing flags `options` are a combination of xmlParserOption. |
5069 | | * NOTE that the file descriptor will not be closed when the |
5070 | | * reader is closed or reset. |
5071 | | * |
5072 | | * @param fd an open file descriptor |
5073 | | * @param URL the base URL to use for the document |
5074 | | * @param encoding the document encoding, or NULL |
5075 | | * @param options a combination of xmlParserOption |
5076 | | * @returns the new reader or NULL in case of error. |
5077 | | */ |
5078 | | xmlTextReader * |
5079 | | xmlReaderForFd(int fd, const char *URL, const char *encoding, int options) |
5080 | 0 | { |
5081 | 0 | xmlTextReaderPtr reader; |
5082 | 0 | xmlParserInputBufferPtr input; |
5083 | 0 | xmlParserErrors code; |
5084 | |
|
5085 | 0 | if (fd < 0) { |
5086 | 0 | xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument"); |
5087 | 0 | return(NULL); |
5088 | 0 | } |
5089 | | |
5090 | 0 | input = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
5091 | 0 | if (input == NULL) { |
5092 | 0 | xmlTextReaderErrMemory(NULL); |
5093 | 0 | return(NULL); |
5094 | 0 | } |
5095 | | /* |
5096 | | * TODO: Remove XML_INPUT_UNZIP |
5097 | | */ |
5098 | 0 | code = xmlInputFromFd(input, fd, XML_INPUT_UNZIP); |
5099 | 0 | if (code != XML_ERR_OK) { |
5100 | 0 | xmlTextReaderErr(code, "failed to open fd"); |
5101 | 0 | return(NULL); |
5102 | 0 | } |
5103 | 0 | input->closecallback = NULL; |
5104 | |
|
5105 | 0 | reader = xmlNewTextReader(input, URL); |
5106 | 0 | if (reader == NULL) { |
5107 | 0 | xmlTextReaderErrMemory(NULL); |
5108 | 0 | xmlFreeParserInputBuffer(input); |
5109 | 0 | return(NULL); |
5110 | 0 | } |
5111 | 0 | reader->allocs |= XML_TEXTREADER_INPUT; |
5112 | 0 | if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) { |
5113 | 0 | xmlTextReaderErrMemory(NULL); |
5114 | 0 | xmlFreeTextReader(reader); |
5115 | 0 | return(NULL); |
5116 | 0 | } |
5117 | 0 | return (reader); |
5118 | 0 | } |
5119 | | |
5120 | | /** |
5121 | | * Create an xmltextReader for an XML document from I/O functions and source. |
5122 | | * The parsing flags `options` are a combination of xmlParserOption. |
5123 | | * |
5124 | | * @param ioread an I/O read function |
5125 | | * @param ioclose an I/O close function |
5126 | | * @param ioctx an I/O handler |
5127 | | * @param URL the base URL to use for the document |
5128 | | * @param encoding the document encoding, or NULL |
5129 | | * @param options a combination of xmlParserOption |
5130 | | * @returns the new reader or NULL in case of error. |
5131 | | */ |
5132 | | xmlTextReader * |
5133 | | xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
5134 | | void *ioctx, const char *URL, const char *encoding, |
5135 | | int options) |
5136 | 0 | { |
5137 | 0 | xmlTextReaderPtr reader; |
5138 | 0 | xmlParserInputBufferPtr input; |
5139 | |
|
5140 | 0 | if (ioread == NULL) |
5141 | 0 | return (NULL); |
5142 | | |
5143 | 0 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
5144 | 0 | XML_CHAR_ENCODING_NONE); |
5145 | 0 | if (input == NULL) { |
5146 | 0 | if (ioclose != NULL) |
5147 | 0 | ioclose(ioctx); |
5148 | 0 | return (NULL); |
5149 | 0 | } |
5150 | 0 | reader = xmlNewTextReader(input, URL); |
5151 | 0 | if (reader == NULL) { |
5152 | 0 | xmlFreeParserInputBuffer(input); |
5153 | 0 | return (NULL); |
5154 | 0 | } |
5155 | 0 | reader->allocs |= XML_TEXTREADER_INPUT; |
5156 | 0 | if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) { |
5157 | 0 | xmlFreeTextReader(reader); |
5158 | 0 | return (NULL); |
5159 | 0 | } |
5160 | 0 | return (reader); |
5161 | 0 | } |
5162 | | |
5163 | | /** |
5164 | | * Setup an xmltextReader to parse a preparsed XML document. |
5165 | | * This reuses the existing `reader` xmlTextReader. |
5166 | | * |
5167 | | * @param reader an XML reader |
5168 | | * @param doc a preparsed document |
5169 | | * @returns 0 in case of success and -1 in case of error |
5170 | | */ |
5171 | | int |
5172 | | xmlReaderNewWalker(xmlTextReader *reader, xmlDoc *doc) |
5173 | 0 | { |
5174 | 0 | if (doc == NULL) |
5175 | 0 | return (-1); |
5176 | 0 | if (reader == NULL) |
5177 | 0 | return (-1); |
5178 | | |
5179 | 0 | if (reader->input != NULL) { |
5180 | 0 | xmlFreeParserInputBuffer(reader->input); |
5181 | 0 | } |
5182 | 0 | if (reader->ctxt != NULL) { |
5183 | 0 | xmlCtxtReset(reader->ctxt); |
5184 | 0 | } |
5185 | |
|
5186 | 0 | reader->entNr = 0; |
5187 | 0 | reader->input = NULL; |
5188 | 0 | reader->mode = XML_TEXTREADER_MODE_INITIAL; |
5189 | 0 | reader->node = NULL; |
5190 | 0 | reader->curnode = NULL; |
5191 | 0 | reader->base = 0; |
5192 | 0 | reader->cur = 0; |
5193 | 0 | reader->allocs = XML_TEXTREADER_CTXT; |
5194 | 0 | reader->doc = doc; |
5195 | 0 | reader->state = XML_TEXTREADER_START; |
5196 | 0 | if (reader->dict == NULL) { |
5197 | 0 | if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL)) |
5198 | 0 | reader->dict = reader->ctxt->dict; |
5199 | 0 | else |
5200 | 0 | reader->dict = xmlDictCreate(); |
5201 | 0 | } |
5202 | 0 | return(0); |
5203 | 0 | } |
5204 | | |
5205 | | /** |
5206 | | * Setup an xmltextReader to parse an XML in-memory document. |
5207 | | * The parsing flags `options` are a combination of xmlParserOption. |
5208 | | * This reuses the existing `reader` xmlTextReader. |
5209 | | * |
5210 | | * @param reader an XML reader |
5211 | | * @param cur a pointer to a zero terminated string |
5212 | | * @param URL the base URL to use for the document |
5213 | | * @param encoding the document encoding, or NULL |
5214 | | * @param options a combination of xmlParserOption |
5215 | | * @returns 0 in case of success and -1 in case of error |
5216 | | */ |
5217 | | int |
5218 | | xmlReaderNewDoc(xmlTextReader *reader, const xmlChar * cur, |
5219 | | const char *URL, const char *encoding, int options) |
5220 | 0 | { |
5221 | |
|
5222 | 0 | int len; |
5223 | |
|
5224 | 0 | if (cur == NULL) |
5225 | 0 | return (-1); |
5226 | 0 | if (reader == NULL) |
5227 | 0 | return (-1); |
5228 | | |
5229 | 0 | len = xmlStrlen(cur); |
5230 | 0 | return (xmlReaderNewMemory(reader, (const char *)cur, len, |
5231 | 0 | URL, encoding, options)); |
5232 | 0 | } |
5233 | | |
5234 | | /** |
5235 | | * parse an XML file from the filesystem or the network. |
5236 | | * The parsing flags `options` are a combination of xmlParserOption. |
5237 | | * This reuses the existing `reader` xmlTextReader. |
5238 | | * |
5239 | | * @param reader an XML reader |
5240 | | * @param filename a file or URL |
5241 | | * @param encoding the document encoding, or NULL |
5242 | | * @param options a combination of xmlParserOption |
5243 | | * @returns 0 in case of success and -1 in case of error |
5244 | | */ |
5245 | | int |
5246 | | xmlReaderNewFile(xmlTextReader *reader, const char *filename, |
5247 | | const char *encoding, int options) |
5248 | 0 | { |
5249 | 0 | xmlParserInputBufferPtr input; |
5250 | |
|
5251 | 0 | if ((filename == NULL) || (reader == NULL)) { |
5252 | 0 | xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument"); |
5253 | 0 | return(-1); |
5254 | 0 | } |
5255 | | |
5256 | 0 | if (xmlParserInputBufferCreateFilenameValue != NULL) { |
5257 | 0 | input = xmlParserInputBufferCreateFilenameValue(filename, |
5258 | 0 | XML_CHAR_ENCODING_NONE); |
5259 | 0 | if (input == NULL) { |
5260 | 0 | xmlTextReaderErr(XML_IO_ENOENT, "failed to open %s", filename); |
5261 | 0 | return(-1); |
5262 | 0 | } |
5263 | 0 | } else { |
5264 | | /* |
5265 | | * TODO: Remove XML_INPUT_UNZIP |
5266 | | */ |
5267 | 0 | xmlParserInputFlags flags = XML_INPUT_UNZIP; |
5268 | 0 | xmlParserErrors code; |
5269 | |
|
5270 | 0 | if ((options & XML_PARSE_NONET) == 0) |
5271 | 0 | flags |= XML_INPUT_NETWORK; |
5272 | |
|
5273 | 0 | code = xmlParserInputBufferCreateUrl(filename, XML_CHAR_ENCODING_NONE, |
5274 | 0 | flags, &input); |
5275 | 0 | if (code != XML_ERR_OK) { |
5276 | 0 | xmlTextReaderErr(code, "failed to open %s", filename); |
5277 | 0 | return(-1); |
5278 | 0 | } |
5279 | 0 | } |
5280 | | |
5281 | 0 | if (xmlTextReaderSetup(reader, input, filename, encoding, options) < 0) { |
5282 | 0 | xmlTextReaderErrMemory(NULL); |
5283 | 0 | return(-1); |
5284 | 0 | } |
5285 | | |
5286 | 0 | return(0); |
5287 | 0 | } |
5288 | | |
5289 | | /** |
5290 | | * Setup an xmltextReader to parse an XML in-memory document. |
5291 | | * The parsing flags `options` are a combination of xmlParserOption. |
5292 | | * This reuses the existing `reader` xmlTextReader. |
5293 | | * |
5294 | | * @param reader an XML reader |
5295 | | * @param buffer a pointer to a char array |
5296 | | * @param size the size of the array |
5297 | | * @param URL the base URL to use for the document |
5298 | | * @param encoding the document encoding, or NULL |
5299 | | * @param options a combination of xmlParserOption |
5300 | | * @returns 0 in case of success and -1 in case of error |
5301 | | */ |
5302 | | int |
5303 | | xmlReaderNewMemory(xmlTextReader *reader, const char *buffer, int size, |
5304 | | const char *URL, const char *encoding, int options) |
5305 | 0 | { |
5306 | 0 | xmlParserInputBufferPtr input; |
5307 | |
|
5308 | 0 | if (reader == NULL) |
5309 | 0 | return (-1); |
5310 | 0 | if (buffer == NULL) |
5311 | 0 | return (-1); |
5312 | | |
5313 | 0 | input = xmlParserInputBufferCreateMem(buffer, size, |
5314 | 0 | XML_CHAR_ENCODING_NONE); |
5315 | 0 | if (input == NULL) { |
5316 | 0 | return (-1); |
5317 | 0 | } |
5318 | 0 | return (xmlTextReaderSetup(reader, input, URL, encoding, options)); |
5319 | 0 | } |
5320 | | |
5321 | | /** |
5322 | | * Setup an xmltextReader to parse an XML from a file descriptor. |
5323 | | * NOTE that the file descriptor will not be closed when the |
5324 | | * reader is closed or reset. |
5325 | | * The parsing flags `options` are a combination of xmlParserOption. |
5326 | | * This reuses the existing `reader` xmlTextReader. |
5327 | | * |
5328 | | * @param reader an XML reader |
5329 | | * @param fd an open file descriptor |
5330 | | * @param URL the base URL to use for the document |
5331 | | * @param encoding the document encoding, or NULL |
5332 | | * @param options a combination of xmlParserOption |
5333 | | * @returns 0 in case of success and -1 in case of error |
5334 | | */ |
5335 | | int |
5336 | | xmlReaderNewFd(xmlTextReader *reader, int fd, |
5337 | | const char *URL, const char *encoding, int options) |
5338 | 0 | { |
5339 | 0 | xmlParserInputBufferPtr input; |
5340 | 0 | xmlParserErrors code; |
5341 | |
|
5342 | 0 | if ((fd < 0) || (reader == NULL)) { |
5343 | 0 | xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument"); |
5344 | 0 | return(-1); |
5345 | 0 | } |
5346 | | |
5347 | 0 | input = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
5348 | 0 | if (input == NULL) { |
5349 | 0 | xmlTextReaderErrMemory(NULL); |
5350 | 0 | return(-1); |
5351 | 0 | } |
5352 | | /* |
5353 | | * TODO: Remove XML_INPUT_UNZIP |
5354 | | */ |
5355 | 0 | code = xmlInputFromFd(input, fd, XML_INPUT_UNZIP); |
5356 | 0 | if (code != XML_ERR_OK) { |
5357 | 0 | xmlTextReaderErr(code, "failed to open fd"); |
5358 | 0 | return(-1); |
5359 | 0 | } |
5360 | 0 | input->closecallback = NULL; |
5361 | |
|
5362 | 0 | if (xmlTextReaderSetup(reader, input, URL, encoding, options) < 0) { |
5363 | 0 | xmlTextReaderErrMemory(NULL); |
5364 | 0 | return(-1); |
5365 | 0 | } |
5366 | | |
5367 | 0 | return(0); |
5368 | 0 | } |
5369 | | |
5370 | | /** |
5371 | | * Setup an xmltextReader to parse an XML document from I/O functions |
5372 | | * and source. |
5373 | | * The parsing flags `options` are a combination of xmlParserOption. |
5374 | | * This reuses the existing `reader` xmlTextReader. |
5375 | | * |
5376 | | * @param reader an XML reader |
5377 | | * @param ioread an I/O read function |
5378 | | * @param ioclose an I/O close function |
5379 | | * @param ioctx an I/O handler |
5380 | | * @param URL the base URL to use for the document |
5381 | | * @param encoding the document encoding, or NULL |
5382 | | * @param options a combination of xmlParserOption |
5383 | | * @returns 0 in case of success and -1 in case of error |
5384 | | */ |
5385 | | int |
5386 | | xmlReaderNewIO(xmlTextReader *reader, xmlInputReadCallback ioread, |
5387 | | xmlInputCloseCallback ioclose, void *ioctx, |
5388 | | const char *URL, const char *encoding, int options) |
5389 | 0 | { |
5390 | 0 | xmlParserInputBufferPtr input; |
5391 | |
|
5392 | 0 | if (ioread == NULL) |
5393 | 0 | return (-1); |
5394 | 0 | if (reader == NULL) |
5395 | 0 | return (-1); |
5396 | | |
5397 | 0 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
5398 | 0 | XML_CHAR_ENCODING_NONE); |
5399 | 0 | if (input == NULL) { |
5400 | 0 | if (ioclose != NULL) |
5401 | 0 | ioclose(ioctx); |
5402 | 0 | return (-1); |
5403 | 0 | } |
5404 | 0 | return (xmlTextReaderSetup(reader, input, URL, encoding, options)); |
5405 | 0 | } |
5406 | | |
5407 | | #endif /* LIBXML_READER_ENABLED */ |