blob: 58fbe2e7e8e61e0bf2838ca2ff4e25e0ef3f1e1c [file] [log] [blame]
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001/*
2 * xmlreader.c: implements the xmlTextReader streaming node API
3 *
Daniel Veillard67df8092002-12-16 22:04:11 +00004 * NOTE:
5 * XmlTextReader.Normalization Property won't be supported, since
6 * it makes the parser non compliant to the XML recommendation
7 *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00008 * See Copyright for the status of this software.
9 *
10 * daniel@veillard.com
11 */
12
Daniel Veillard7704fb12003-01-03 16:19:51 +000013/*
14 * TODOs:
Daniel Veillard067bae52003-01-05 01:27:54 +000015 * - XML Schemas validation
Daniel Veillard7704fb12003-01-03 16:19:51 +000016 */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000017#define IN_LIBXML
18#include "libxml.h"
19
Daniel Veillard81273902003-09-30 00:43:48 +000020#ifdef LIBXML_READER_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +000021#include <string.h> /* for memset() only ! */
Daniel Veillard26f70262003-01-16 22:45:08 +000022#include <stdarg.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000023
24#ifdef HAVE_CTYPE_H
25#include <ctype.h>
26#endif
27#ifdef HAVE_STDLIB_H
28#include <stdlib.h>
29#endif
30
31#include <libxml/xmlmemory.h>
32#include <libxml/xmlIO.h>
33#include <libxml/xmlreader.h>
Daniel Veillardfc8dc352003-10-18 09:07:46 +000034#include <libxml/parserInternals.h>
Daniel Veillardf4e55762003-04-15 23:32:22 +000035#include <libxml/relaxng.h>
Daniel Veillard198c1bf2003-10-20 17:07:41 +000036#include <libxml/uri.h>
Daniel Veillard7899c5c2003-11-03 12:31:38 +000037#ifdef LIBXML_XINCLUDE_ENABLED
38#include <libxml/xinclude.h>
39#endif
Daniel Veillard1e906612003-12-05 14:57:46 +000040#ifdef LIBXML_PATTERN_ENABLED
41#include <libxml/pattern.h>
42#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +000043
44/* #define DEBUG_CALLBACKS */
45/* #define DEBUG_READER */
46
47/**
48 * TODO:
49 *
50 * macro to flag unimplemented blocks
51 */
52#define TODO \
53 xmlGenericError(xmlGenericErrorContext, \
54 "Unimplemented block at %s:%d\n", \
55 __FILE__, __LINE__);
56
57#ifdef DEBUG_READER
58#define DUMP_READER xmlTextReaderDebug(reader);
59#else
60#define DUMP_READER
61#endif
62
Daniel Veillarda880b122003-04-21 21:36:41 +000063#define CHUNK_SIZE 512
Daniel Veillarde1ca5032002-12-09 14:13:43 +000064/************************************************************************
65 * *
66 * The parser: maps the Text Reader API on top of the existing *
67 * parsing routines building a tree *
68 * *
69 ************************************************************************/
70
71#define XML_TEXTREADER_INPUT 1
72#define XML_TEXTREADER_CTXT 2
73
74typedef enum {
Daniel Veillarde1ca5032002-12-09 14:13:43 +000075 XML_TEXTREADER_NONE = -1,
76 XML_TEXTREADER_START= 0,
77 XML_TEXTREADER_ELEMENT= 1,
78 XML_TEXTREADER_END= 2,
79 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000080 XML_TEXTREADER_BACKTRACK= 4,
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000081 XML_TEXTREADER_DONE= 5,
82 XML_TEXTREADER_ERROR= 6
Daniel Veillarde1ca5032002-12-09 14:13:43 +000083} xmlTextReaderState;
84
Daniel Veillardf4e55762003-04-15 23:32:22 +000085typedef enum {
86 XML_TEXTREADER_NOT_VALIDATE = 0,
87 XML_TEXTREADER_VALIDATE_DTD = 1,
88 XML_TEXTREADER_VALIDATE_RNG = 2
89} xmlTextReaderValidate;
90
Daniel Veillarde1ca5032002-12-09 14:13:43 +000091struct _xmlTextReader {
92 int mode; /* the parsing mode */
Daniel Veillard198c1bf2003-10-20 17:07:41 +000093 xmlDocPtr doc; /* when walking an existing doc */
Daniel Veillardf4e55762003-04-15 23:32:22 +000094 xmlTextReaderValidate validate;/* is there any validation */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000095 int allocs; /* what structure were deallocated */
96 xmlTextReaderState state;
97 xmlParserCtxtPtr ctxt; /* the parser context */
98 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
99 xmlParserInputBufferPtr input; /* the input */
100 startElementSAXFunc startElement;/* initial SAX callbacks */
101 endElementSAXFunc endElement; /* idem */
Daniel Veillard07cb8222003-09-10 10:51:05 +0000102 startElementNsSAX2Func startElementNs;/* idem */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000103 endElementNsSAX2Func endElementNs; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000104 charactersSAXFunc characters;
105 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000106 unsigned int base; /* base of the segment in the input */
107 unsigned int cur; /* current position in the input */
108 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000109 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000110 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000111 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000112 int preserve;/* preserve the resulting document */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000113 xmlBufferPtr buffer; /* used to return const xmlChar * */
114 xmlDictPtr dict; /* the context dictionnary */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000115
116 /* entity stack when traversing entities content */
117 xmlNodePtr ent; /* Current Entity Ref Node */
118 int entNr; /* Depth of the entities stack */
119 int entMax; /* Max depth of the entities stack */
120 xmlNodePtr *entTab; /* array of entities */
Daniel Veillard26f70262003-01-16 22:45:08 +0000121
122 /* error handling */
123 xmlTextReaderErrorFunc errorFunc; /* callback function */
124 void *errorFuncArg; /* callback function user argument */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000125
126#ifdef LIBXML_SCHEMAS_ENABLED
127 /* Handling of RelaxNG validation */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000128 xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
129 xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
130 int rngValidErrors;/* The number of errors detected */
131 xmlNodePtr rngFullNode; /* the node if RNG not progressive */
132#endif
133#ifdef LIBXML_XINCLUDE_ENABLED
134 /* Handling of XInclude processing */
135 int xinclude; /* is xinclude asked for */
136 const xmlChar * xinclude_name; /* the xinclude name from dict */
137 xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */
138 int in_xinclude; /* counts for xinclude */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000139#endif
Daniel Veillard1e906612003-12-05 14:57:46 +0000140#ifdef LIBXML_PATTERN_ENABLED
141 int patternNr; /* number of preserve patterns */
142 int patternMax; /* max preserve patterns */
143 xmlPatternPtr *patternTab; /* array of preserve patterns */
144#endif
145 int preserves; /* level of preserves */
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000146 int parserFlags; /* the set of options set */
William M. Brack93d004f2004-02-03 00:14:10 +0000147 /* Structured error handling */
148 xmlStructuredErrorFunc sErrorFunc; /* callback function */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000149};
150
Daniel Veillarde8039df2003-10-27 11:25:13 +0000151#define NODE_IS_EMPTY 0x1
152#define NODE_IS_PRESERVED 0x2
Daniel Veillard1e906612003-12-05 14:57:46 +0000153#define NODE_IS_SPRESERVED 0x4
Daniel Veillard067bae52003-01-05 01:27:54 +0000154
Daniel Veillarde72c5082003-09-19 12:44:05 +0000155/**
156 * CONSTSTR:
157 *
158 * Macro used to return an interned string
159 */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000160#define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
161#define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
162
163static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
164static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
Daniel Veillarde72c5082003-09-19 12:44:05 +0000165
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000166/************************************************************************
167 * *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000168 * Our own version of the freeing routines as we recycle nodes *
169 * *
170 ************************************************************************/
171/**
172 * DICT_FREE:
173 * @str: a string
174 *
175 * Free a string if it is not owned by the "dict" dictionnary in the
176 * current scope
177 */
178#define DICT_FREE(str) \
179 if ((str) && ((!dict) || \
180 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
181 xmlFree((char *)(str));
182
183static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
184static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
185
186/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000187 * xmlFreeID:
188 * @not: A id
189 *
190 * Deallocate the memory used by an id definition
191 */
192static void
193xmlFreeID(xmlIDPtr id) {
194 xmlDictPtr dict = NULL;
195
196 if (id == NULL) return;
197
198 if (id->doc != NULL)
199 dict = id->doc->dict;
200
201 if (id->value != NULL)
202 DICT_FREE(id->value)
203 xmlFree(id);
204}
205
206/**
207 * xmlTextReaderRemoveID:
208 * @doc: the document
209 * @attr: the attribute
210 *
211 * Remove the given attribute from the ID table maintained internally.
212 *
213 * Returns -1 if the lookup failed and 0 otherwise
214 */
215static int
216xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
217 xmlIDTablePtr table;
218 xmlIDPtr id;
219 xmlChar *ID;
220
221 if (doc == NULL) return(-1);
222 if (attr == NULL) return(-1);
223 table = (xmlIDTablePtr) doc->ids;
224 if (table == NULL)
225 return(-1);
226
227 if (attr == NULL)
228 return(-1);
229 ID = xmlNodeListGetString(doc, attr->children, 1);
230 if (ID == NULL)
231 return(-1);
232 id = xmlHashLookup(table, ID);
233 xmlFree(ID);
234 if (id == NULL || id->attr != attr) {
235 return(-1);
236 }
237 id->name = attr->name;
238 id->attr = NULL;
239 return(0);
240}
241
242/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000243 * xmlTextReaderFreeProp:
244 * @reader: the xmlTextReaderPtr used
245 * @cur: the node
246 *
247 * Free a node.
248 */
249static void
250xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
251 xmlDictPtr dict;
252
253 dict = reader->ctxt->dict;
254 if (cur == NULL) return;
255
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000256 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
257 xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
258
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000259 /* Check for ID removal -> leading to invalid references ! */
260 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
261 ((cur->parent->doc->intSubset != NULL) ||
262 (cur->parent->doc->extSubset != NULL))) {
263 if (xmlIsID(cur->parent->doc, cur->parent, cur))
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000264 xmlTextReaderRemoveID(cur->parent->doc, cur);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000265 }
266 if (cur->children != NULL)
267 xmlTextReaderFreeNodeList(reader, cur->children);
268
269 DICT_FREE(cur->name);
270 if ((reader != NULL) && (reader->ctxt != NULL) &&
271 (reader->ctxt->freeAttrsNr < 100)) {
272 cur->next = reader->ctxt->freeAttrs;
273 reader->ctxt->freeAttrs = cur;
274 reader->ctxt->freeAttrsNr++;
275 } else {
276 xmlFree(cur);
277 }
278}
279
280/**
281 * xmlTextReaderFreePropList:
282 * @reader: the xmlTextReaderPtr used
283 * @cur: the first property in the list
284 *
285 * Free a property and all its siblings, all the children are freed too.
286 */
287static void
288xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
289 xmlAttrPtr next;
290 if (cur == NULL) return;
291 while (cur != NULL) {
292 next = cur->next;
293 xmlTextReaderFreeProp(reader, cur);
294 cur = next;
295 }
296}
297
298/**
299 * xmlTextReaderFreeNodeList:
300 * @reader: the xmlTextReaderPtr used
301 * @cur: the first node in the list
302 *
303 * Free a node and all its siblings, this is a recursive behaviour, all
304 * the children are freed too.
305 */
306static void
307xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
308 xmlNodePtr next;
309 xmlDictPtr dict;
310
311 dict = reader->ctxt->dict;
312 if (cur == NULL) return;
313 if (cur->type == XML_NAMESPACE_DECL) {
314 xmlFreeNsList((xmlNsPtr) cur);
315 return;
316 }
317 if ((cur->type == XML_DOCUMENT_NODE) ||
318 (cur->type == XML_HTML_DOCUMENT_NODE)) {
319 xmlFreeDoc((xmlDocPtr) cur);
320 return;
321 }
322 while (cur != NULL) {
323 next = cur->next;
324 /* unroll to speed up freeing the document */
325 if (cur->type != XML_DTD_NODE) {
326
327 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000328 (cur->type != XML_ENTITY_REF_NODE)) {
329 if (cur->children->parent == cur)
330 xmlTextReaderFreeNodeList(reader, cur->children);
331 cur->children = NULL;
332 }
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000333
334 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
335 xmlDeregisterNodeDefaultValue(cur);
336
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000337 if (((cur->type == XML_ELEMENT_NODE) ||
338 (cur->type == XML_XINCLUDE_START) ||
339 (cur->type == XML_XINCLUDE_END)) &&
340 (cur->properties != NULL))
341 xmlTextReaderFreePropList(reader, cur->properties);
342 if ((cur->type != XML_ELEMENT_NODE) &&
343 (cur->type != XML_XINCLUDE_START) &&
344 (cur->type != XML_XINCLUDE_END) &&
345 (cur->type != XML_ENTITY_REF_NODE)) {
346 DICT_FREE(cur->content);
347 }
348 if (((cur->type == XML_ELEMENT_NODE) ||
349 (cur->type == XML_XINCLUDE_START) ||
350 (cur->type == XML_XINCLUDE_END)) &&
351 (cur->nsDef != NULL))
352 xmlFreeNsList(cur->nsDef);
353
354 /*
355 * we don't free element names here they are interned now
356 */
357 if ((cur->type != XML_TEXT_NODE) &&
358 (cur->type != XML_COMMENT_NODE))
359 DICT_FREE(cur->name);
360 if (((cur->type == XML_ELEMENT_NODE) ||
361 (cur->type == XML_TEXT_NODE)) &&
362 (reader != NULL) && (reader->ctxt != NULL) &&
363 (reader->ctxt->freeElemsNr < 100)) {
364 cur->next = reader->ctxt->freeElems;
365 reader->ctxt->freeElems = cur;
366 reader->ctxt->freeElemsNr++;
367 } else {
368 xmlFree(cur);
369 }
370 }
371 cur = next;
372 }
373}
374
375/**
376 * xmlTextReaderFreeNode:
377 * @reader: the xmlTextReaderPtr used
378 * @cur: the node
379 *
380 * Free a node, this is a recursive behaviour, all the children are freed too.
381 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
382 */
383static void
384xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
385 xmlDictPtr dict;
386
387 dict = reader->ctxt->dict;
388 if (cur->type == XML_DTD_NODE) {
389 xmlFreeDtd((xmlDtdPtr) cur);
390 return;
391 }
392 if (cur->type == XML_NAMESPACE_DECL) {
393 xmlFreeNs((xmlNsPtr) cur);
394 return;
395 }
396 if (cur->type == XML_ATTRIBUTE_NODE) {
397 xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
398 return;
399 }
400
401 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000402 (cur->type != XML_ENTITY_REF_NODE)) {
403 if (cur->children->parent == cur)
404 xmlTextReaderFreeNodeList(reader, cur->children);
405 cur->children = NULL;
406 }
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000407
408 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
409 xmlDeregisterNodeDefaultValue(cur);
410
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000411 if (((cur->type == XML_ELEMENT_NODE) ||
412 (cur->type == XML_XINCLUDE_START) ||
413 (cur->type == XML_XINCLUDE_END)) &&
414 (cur->properties != NULL))
415 xmlTextReaderFreePropList(reader, cur->properties);
416 if ((cur->type != XML_ELEMENT_NODE) &&
417 (cur->type != XML_XINCLUDE_START) &&
418 (cur->type != XML_XINCLUDE_END) &&
419 (cur->type != XML_ENTITY_REF_NODE)) {
420 DICT_FREE(cur->content);
421 }
422 if (((cur->type == XML_ELEMENT_NODE) ||
423 (cur->type == XML_XINCLUDE_START) ||
424 (cur->type == XML_XINCLUDE_END)) &&
425 (cur->nsDef != NULL))
426 xmlFreeNsList(cur->nsDef);
427
428 /*
429 * we don't free names here they are interned now
430 */
431 if ((cur->type != XML_TEXT_NODE) &&
432 (cur->type != XML_COMMENT_NODE))
433 DICT_FREE(cur->name);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000434
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000435 if (((cur->type == XML_ELEMENT_NODE) ||
436 (cur->type == XML_TEXT_NODE)) &&
437 (reader != NULL) && (reader->ctxt != NULL) &&
438 (reader->ctxt->freeElemsNr < 100)) {
439 cur->next = reader->ctxt->freeElems;
440 reader->ctxt->freeElems = cur;
441 reader->ctxt->freeElemsNr++;
442 } else {
443 xmlFree(cur);
444 }
445}
446
447/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000448 * xmlTextReaderFreeIDTable:
449 * @table: An id table
450 *
451 * Deallocate the memory used by an ID hash table.
452 */
William M. Brack60f394e2003-11-16 06:25:42 +0000453static void
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000454xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
455 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
456}
457
458/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000459 * xmlTextReaderFreeDoc:
460 * @reader: the xmlTextReaderPtr used
461 * @cur: pointer to the document
462 *
463 * Free up all the structures used by a document, tree included.
464 */
465static void
466xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
467 xmlDtdPtr extSubset, intSubset;
468
469 if (cur == NULL) return;
470
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000471 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
472 xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
473
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000474 /*
475 * Do this before freeing the children list to avoid ID lookups
476 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000477 if (cur->ids != NULL) xmlTextReaderFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000478 cur->ids = NULL;
479 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
480 cur->refs = NULL;
481 extSubset = cur->extSubset;
482 intSubset = cur->intSubset;
483 if (intSubset == extSubset)
484 extSubset = NULL;
485 if (extSubset != NULL) {
486 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
487 cur->extSubset = NULL;
488 xmlFreeDtd(extSubset);
489 }
490 if (intSubset != NULL) {
491 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
492 cur->intSubset = NULL;
493 xmlFreeDtd(intSubset);
494 }
495
496 if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
497
498 if (cur->version != NULL) xmlFree((char *) cur->version);
499 if (cur->name != NULL) xmlFree((char *) cur->name);
500 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
501 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
502 if (cur->URL != NULL) xmlFree((char *) cur->URL);
Daniel Veillard500a1de2004-03-22 15:22:58 +0000503 if (cur->dict != NULL) xmlDictFree(cur->dict);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000504
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000505 xmlFree(cur);
506}
507
508/************************************************************************
509 * *
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000510 * The reader core parser *
511 * *
512 ************************************************************************/
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000513#ifdef DEBUG_READER
514static void
515xmlTextReaderDebug(xmlTextReaderPtr reader) {
516 if ((reader == NULL) || (reader->ctxt == NULL)) {
517 fprintf(stderr, "xmlTextReader NULL\n");
518 return;
519 }
520 fprintf(stderr, "xmlTextReader: state %d depth %d ",
521 reader->state, reader->depth);
522 if (reader->node == NULL) {
523 fprintf(stderr, "node = NULL\n");
524 } else {
525 fprintf(stderr, "node %s\n", reader->node->name);
526 }
527 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
528 reader->base, reader->cur, reader->ctxt->nodeNr);
529 if (reader->input->buffer == NULL) {
530 fprintf(stderr, "buffer is NULL\n");
531 } else {
532#ifdef LIBXML_DEBUG_ENABLED
533 xmlDebugDumpString(stderr,
534 &reader->input->buffer->content[reader->cur]);
535#endif
536 fprintf(stderr, "\n");
537 }
538}
539#endif
540
541/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000542 * xmlTextReaderEntPush:
543 * @reader: the xmlTextReaderPtr used
544 * @value: the entity reference node
545 *
546 * Pushes a new entity reference node on top of the entities stack
547 *
548 * Returns 0 in case of error, the index in the stack otherwise
549 */
550static int
551xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
552{
553 if (reader->entMax <= 0) {
554 reader->entMax = 10;
555 reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
556 sizeof(reader->entTab[0]));
557 if (reader->entTab == NULL) {
558 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
559 return (0);
560 }
561 }
562 if (reader->entNr >= reader->entMax) {
563 reader->entMax *= 2;
564 reader->entTab =
565 (xmlNodePtr *) xmlRealloc(reader->entTab,
566 reader->entMax *
567 sizeof(reader->entTab[0]));
568 if (reader->entTab == NULL) {
569 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
570 return (0);
571 }
572 }
573 reader->entTab[reader->entNr] = value;
574 reader->ent = value;
575 return (reader->entNr++);
576}
577
578/**
579 * xmlTextReaderEntPop:
580 * @reader: the xmlTextReaderPtr used
581 *
582 * Pops the top element entity from the entities stack
583 *
584 * Returns the entity just removed
585 */
586static xmlNodePtr
587xmlTextReaderEntPop(xmlTextReaderPtr reader)
588{
589 xmlNodePtr ret;
590
591 if (reader->entNr <= 0)
592 return (0);
593 reader->entNr--;
594 if (reader->entNr > 0)
595 reader->ent = reader->entTab[reader->entNr - 1];
596 else
597 reader->ent = NULL;
598 ret = reader->entTab[reader->entNr];
599 reader->entTab[reader->entNr] = 0;
600 return (ret);
601}
602
603/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000604 * xmlTextReaderStartElement:
605 * @ctx: the user data (XML parser context)
606 * @fullname: The element name, including namespace prefix
607 * @atts: An array of name/value attributes pairs, NULL terminated
608 *
609 * called when an opening tag has been processed.
610 */
611static void
612xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
613 const xmlChar **atts) {
614 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
615 xmlTextReaderPtr reader = ctxt->_private;
616
617#ifdef DEBUG_CALLBACKS
618 printf("xmlTextReaderStartElement(%s)\n", fullname);
619#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000620 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000621 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000622 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
623 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
624 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000625 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000626 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000627 if (reader != NULL)
628 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000629}
630
631/**
632 * xmlTextReaderEndElement:
633 * @ctx: the user data (XML parser context)
634 * @fullname: The element name, including namespace prefix
635 *
636 * called when an ending tag has been processed.
637 */
638static void
639xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
640 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
641 xmlTextReaderPtr reader = ctxt->_private;
642
643#ifdef DEBUG_CALLBACKS
644 printf("xmlTextReaderEndElement(%s)\n", fullname);
645#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000646 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000647 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000648 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000649}
650
651/**
Daniel Veillard07cb8222003-09-10 10:51:05 +0000652 * xmlTextReaderStartElementNs:
653 * @ctx: the user data (XML parser context)
654 * @localname: the local name of the element
655 * @prefix: the element namespace prefix if available
656 * @URI: the element namespace name if available
657 * @nb_namespaces: number of namespace definitions on that node
658 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
659 * @nb_attributes: the number of attributes on that node
660 * nb_defaulted: the number of defaulted attributes.
661 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
662 * attribute values.
663 *
664 * called when an opening tag has been processed.
665 */
666static void
667xmlTextReaderStartElementNs(void *ctx,
668 const xmlChar *localname,
669 const xmlChar *prefix,
670 const xmlChar *URI,
671 int nb_namespaces,
672 const xmlChar **namespaces,
673 int nb_attributes,
674 int nb_defaulted,
675 const xmlChar **attributes)
676{
677 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
678 xmlTextReaderPtr reader = ctxt->_private;
679
680#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000681 printf("xmlTextReaderStartElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000682#endif
683 if ((reader != NULL) && (reader->startElementNs != NULL)) {
684 reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
685 namespaces, nb_attributes, nb_defaulted,
686 attributes);
687 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
688 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
689 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000690 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillard07cb8222003-09-10 10:51:05 +0000691 }
692 if (reader != NULL)
693 reader->state = XML_TEXTREADER_ELEMENT;
694}
695
696/**
697 * xmlTextReaderEndElementNs:
698 * @ctx: the user data (XML parser context)
699 * @localname: the local name of the element
700 * @prefix: the element namespace prefix if available
701 * @URI: the element namespace name if available
702 *
703 * called when an ending tag has been processed.
704 */
705static void
706xmlTextReaderEndElementNs(void *ctx,
707 const xmlChar * localname,
708 const xmlChar * prefix,
709 const xmlChar * URI)
710{
711 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
712 xmlTextReaderPtr reader = ctxt->_private;
713
714#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000715 printf("xmlTextReaderEndElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000716#endif
717 if ((reader != NULL) && (reader->endElementNs != NULL)) {
718 reader->endElementNs(ctx, localname, prefix, URI);
719 }
720}
721
722
723/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000724 * xmlTextReaderCharacters:
725 * @ctx: the user data (XML parser context)
726 * @ch: a xmlChar string
727 * @len: the number of xmlChar
728 *
729 * receiving some chars from the parser.
730 */
731static void
732xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
733{
734 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
735 xmlTextReaderPtr reader = ctxt->_private;
736
737#ifdef DEBUG_CALLBACKS
738 printf("xmlTextReaderCharacters()\n");
739#endif
740 if ((reader != NULL) && (reader->characters != NULL)) {
741 reader->characters(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000742 }
743}
744
745/**
746 * xmlTextReaderCDataBlock:
747 * @ctx: the user data (XML parser context)
748 * @value: The pcdata content
749 * @len: the block length
750 *
751 * called when a pcdata block has been parsed
752 */
753static void
754xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
755{
756 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
757 xmlTextReaderPtr reader = ctxt->_private;
758
759#ifdef DEBUG_CALLBACKS
760 printf("xmlTextReaderCDataBlock()\n");
761#endif
762 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
763 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000764 }
765}
766
767/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000768 * xmlTextReaderPushData:
769 * @reader: the xmlTextReaderPtr used
770 *
771 * Push data down the progressive parser until a significant callback
772 * got raised.
773 *
774 * Returns -1 in case of failure, 0 otherwise
775 */
776static int
777xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000778 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000779 int val, s;
William M. Brack779af002003-08-01 15:55:39 +0000780 xmlTextReaderState oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000781
782 if ((reader->input == NULL) || (reader->input->buffer == NULL))
783 return(-1);
784
Daniel Veillardea7751d2002-12-20 00:16:24 +0000785 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000786 reader->state = XML_TEXTREADER_NONE;
787 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000788
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000789 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000790 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000791 /*
792 * Refill the buffer unless we are at the end of the stream
793 */
794 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
795 val = xmlParserInputBufferRead(reader->input, 4096);
Daniel Veillard53350552003-09-18 13:35:51 +0000796 if ((val == 0) &&
797 (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
798 if (inbuf->use == reader->cur) {
799 reader->mode = XML_TEXTREADER_MODE_EOF;
800 reader->state = oldstate;
Daniel Veillard53350552003-09-18 13:35:51 +0000801 }
802 } else if (val < 0) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000803 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000804 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000805 if ((oldstate != XML_TEXTREADER_START) ||
806 (reader->ctxt->myDoc != NULL))
807 return(val);
Daniel Veillard53350552003-09-18 13:35:51 +0000808 } else if (val == 0) {
809 /* mark the end of the stream and process the remains */
810 reader->mode = XML_TEXTREADER_MODE_EOF;
811 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000812 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000813
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000814 } else
815 break;
816 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000817 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000818 * parse by block of CHUNK_SIZE bytes, various tests show that
819 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000820 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000821 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000822 val = xmlParseChunk(reader->ctxt,
823 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000824 CHUNK_SIZE, 0);
825 reader->cur += CHUNK_SIZE;
William M. Brack9f797ab2004-07-28 07:40:12 +0000826 if ((val != 0) || (reader->ctxt->wellFormed == 0))
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000827 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000828 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000829 s = inbuf->use - reader->cur;
830 val = xmlParseChunk(reader->ctxt,
831 (const char *) &inbuf->content[reader->cur],
832 s, 0);
833 reader->cur += s;
William M. Brack9f797ab2004-07-28 07:40:12 +0000834 if ((val != 0) || (reader->ctxt->wellFormed == 0))
Daniel Veillarda880b122003-04-21 21:36:41 +0000835 return(-1);
836 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000837 }
838 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000839
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000840 /*
841 * Discard the consumed input when needed and possible
842 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000843 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillard21924522004-02-19 16:37:07 +0000844 if (inbuf->alloc != XML_BUFFER_ALLOC_IMMUTABLE) {
845 if ((reader->cur >= 4096) &&
846 (inbuf->use - reader->cur <= CHUNK_SIZE)) {
847 val = xmlBufferShrink(inbuf, reader->cur);
848 if (val >= 0) {
849 reader->cur -= val;
850 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000851 }
852 }
853 }
854
855 /*
856 * At the end of the stream signal that the work is done to the Push
857 * parser.
858 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000859 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000860 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000861 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000862 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000863 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000864 s, 1);
865 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000866 reader->mode = XML_TEXTREADER_DONE;
William M. Brack9f797ab2004-07-28 07:40:12 +0000867 if ((val != 0) || (reader->ctxt->wellFormed == 0))
Daniel Veillard16ed5972003-11-20 18:22:31 +0000868 return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000869 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000870 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000871 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000872 return(0);
873}
874
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000875#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000876/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000877 * xmlTextReaderValidatePush:
878 * @reader: the xmlTextReaderPtr used
879 *
880 * Push the current node for validation
881 */
882static void
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000883xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000884 xmlNodePtr node = reader->node;
885
Daniel Veillardf54cd532004-02-25 11:52:31 +0000886#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000887 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
888 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
889 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
890 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
891 reader->ctxt->myDoc, node, node->name);
892 } else {
893 /* TODO use the BuildQName interface */
894 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000895
Daniel Veillardf4e55762003-04-15 23:32:22 +0000896 qname = xmlStrdup(node->ns->prefix);
897 qname = xmlStrcat(qname, BAD_CAST ":");
898 qname = xmlStrcat(qname, node->name);
899 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
900 reader->ctxt->myDoc, node, qname);
901 if (qname != NULL)
902 xmlFree(qname);
903 }
Daniel Veillardf54cd532004-02-25 11:52:31 +0000904 }
905#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000906#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000907 if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +0000908 (reader->rngValidCtxt != NULL)) {
909 int ret;
910
911 if (reader->rngFullNode != NULL) return;
912 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
913 reader->ctxt->myDoc,
914 node);
915 if (ret == 0) {
916 /*
917 * this element requires a full tree
918 */
919 node = xmlTextReaderExpand(reader);
920 if (node == NULL) {
921printf("Expand failed !\n");
922 ret = -1;
923 } else {
924 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
925 reader->ctxt->myDoc,
926 node);
927 reader->rngFullNode = node;
928 }
929 }
930 if (ret != 1)
931 reader->rngValidErrors++;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000932 }
Daniel Veillardf54cd532004-02-25 11:52:31 +0000933#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000934}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000935
936/**
937 * xmlTextReaderValidateCData:
938 * @reader: the xmlTextReaderPtr used
939 * @data: pointer to the CData
940 * @len: lenght of the CData block in bytes.
941 *
942 * Push some CData for validation
943 */
944static void
945xmlTextReaderValidateCData(xmlTextReaderPtr reader,
946 const xmlChar *data, int len) {
Daniel Veillardf54cd532004-02-25 11:52:31 +0000947#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000948 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
949 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
950 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
951 data, len);
Daniel Veillardf54cd532004-02-25 11:52:31 +0000952 }
953#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000954#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000955 if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +0000956 (reader->rngValidCtxt != NULL)) {
957 int ret;
958
959 if (reader->rngFullNode != NULL) return;
960 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
961 if (ret != 1)
962 reader->rngValidErrors++;
Daniel Veillardf4e55762003-04-15 23:32:22 +0000963 }
Daniel Veillardf54cd532004-02-25 11:52:31 +0000964#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +0000965}
966
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000967/**
968 * xmlTextReaderValidatePop:
969 * @reader: the xmlTextReaderPtr used
970 *
971 * Pop the current node from validation
972 */
973static void
974xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
975 xmlNodePtr node = reader->node;
976
Daniel Veillardf54cd532004-02-25 11:52:31 +0000977#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000978 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
979 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
980 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
981 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
982 reader->ctxt->myDoc, node, node->name);
983 } else {
984 /* TODO use the BuildQName interface */
985 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000986
Daniel Veillardf4e55762003-04-15 23:32:22 +0000987 qname = xmlStrdup(node->ns->prefix);
988 qname = xmlStrcat(qname, BAD_CAST ":");
989 qname = xmlStrcat(qname, node->name);
990 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
991 reader->ctxt->myDoc, node, qname);
992 if (qname != NULL)
993 xmlFree(qname);
994 }
Daniel Veillardf54cd532004-02-25 11:52:31 +0000995 }
996#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000997#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000998 if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +0000999 (reader->rngValidCtxt != NULL)) {
1000 int ret;
1001
1002 if (reader->rngFullNode != NULL) {
1003 if (node == reader->rngFullNode)
1004 reader->rngFullNode = NULL;
1005 return;
1006 }
1007 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
1008 reader->ctxt->myDoc,
1009 node);
1010 if (ret != 1)
1011 reader->rngValidErrors++;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001012 }
Daniel Veillardf54cd532004-02-25 11:52:31 +00001013#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001014}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001015
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001016/**
1017 * xmlTextReaderValidateEntity:
1018 * @reader: the xmlTextReaderPtr used
1019 *
1020 * Handle the validation when an entity reference is encountered and
1021 * entity substitution is not activated. As a result the parser interface
1022 * must walk through the entity and do the validation calls
1023 */
1024static void
1025xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
1026 xmlNodePtr oldnode = reader->node;
1027 xmlNodePtr node = reader->node;
1028 xmlParserCtxtPtr ctxt = reader->ctxt;
1029
1030 do {
1031 if (node->type == XML_ENTITY_REF_NODE) {
1032 /*
1033 * Case where the underlying tree is not availble, lookup the entity
1034 * and walk it.
1035 */
1036 if ((node->children == NULL) && (ctxt->sax != NULL) &&
1037 (ctxt->sax->getEntity != NULL)) {
1038 node->children = (xmlNodePtr)
1039 ctxt->sax->getEntity(ctxt, node->name);
1040 }
1041
1042 if ((node->children != NULL) &&
1043 (node->children->type == XML_ENTITY_DECL) &&
1044 (node->children->children != NULL)) {
1045 xmlTextReaderEntPush(reader, node);
1046 node = node->children->children;
1047 continue;
1048 } else {
1049 /*
1050 * The error has probably be raised already.
1051 */
1052 if (node == oldnode)
1053 break;
1054 node = node->next;
1055 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001056#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001057 } else if (node->type == XML_ELEMENT_NODE) {
1058 reader->node = node;
1059 xmlTextReaderValidatePush(reader);
1060 } else if ((node->type == XML_TEXT_NODE) ||
1061 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001062 xmlTextReaderValidateCData(reader, node->content,
1063 xmlStrlen(node->content));
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001064#endif
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001065 }
1066
1067 /*
1068 * go to next node
1069 */
1070 if (node->children != NULL) {
1071 node = node->children;
1072 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00001073 } else if (node->type == XML_ELEMENT_NODE) {
1074 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001075 }
1076 if (node->next != NULL) {
1077 node = node->next;
1078 continue;
1079 }
1080 do {
1081 node = node->parent;
1082 if (node->type == XML_ELEMENT_NODE) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001083 xmlNodePtr tmp;
Daniel Veillard1d211e22003-10-20 22:32:39 +00001084 if (reader->entNr == 0) {
1085 while ((tmp = node->last) != NULL) {
Daniel Veillarde8039df2003-10-27 11:25:13 +00001086 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard1d211e22003-10-20 22:32:39 +00001087 xmlUnlinkNode(tmp);
1088 xmlTextReaderFreeNode(reader, tmp);
1089 } else
1090 break;
1091 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001092 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001093 reader->node = node;
1094 xmlTextReaderValidatePop(reader);
1095 }
1096 if ((node->type == XML_ENTITY_DECL) &&
1097 (reader->ent != NULL) && (reader->ent->children == node)) {
1098 node = xmlTextReaderEntPop(reader);
1099 }
1100 if (node == oldnode)
1101 break;
1102 if (node->next != NULL) {
1103 node = node->next;
1104 break;
1105 }
1106 } while ((node != NULL) && (node != oldnode));
1107 } while ((node != NULL) && (node != oldnode));
1108 reader->node = oldnode;
1109}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001110#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001111
1112
1113/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001114 * xmlTextReaderGetSuccessor:
1115 * @cur: the current node
1116 *
1117 * Get the successor of a node if available.
1118 *
1119 * Returns the successor node or NULL
1120 */
1121static xmlNodePtr
1122xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1123 if (cur == NULL) return(NULL) ; /* ERROR */
1124 if (cur->next != NULL) return(cur->next) ;
1125 do {
1126 cur = cur->parent;
1127 if (cur == NULL) return(NULL);
1128 if (cur->next != NULL) return(cur->next);
1129 } while (cur != NULL);
1130 return(cur);
1131}
1132
1133/**
1134 * xmlTextReaderDoExpand:
1135 * @reader: the xmlTextReaderPtr used
1136 *
1137 * Makes sure that the current node is fully read as well as all its
1138 * descendant. It means the full DOM subtree must be available at the
1139 * end of the call.
1140 *
1141 * Returns 1 if the node was expanded successfully, 0 if there is no more
1142 * nodes to read, or -1 in case of error
1143 */
1144static int
1145xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1146 int val;
1147
1148 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1149 return(-1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001150 do {
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001151 if (reader->ctxt->instate == XML_PARSER_EOF) return(1);
1152
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001153 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1154 return(1);
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001155 if (reader->ctxt->nodeNr < reader->depth)
Daniel Veillarda37aab82003-06-09 09:10:36 +00001156 return(1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001157 if (reader->mode == XML_TEXTREADER_MODE_EOF)
1158 return(1);
1159 val = xmlTextReaderPushData(reader);
1160 if (val < 0)
1161 return(-1);
1162 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1163 return(1);
1164}
1165
1166/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001167 * xmlTextReaderRead:
1168 * @reader: the xmlTextReaderPtr used
1169 *
1170 * Moves the position of the current instance to the next node in
1171 * the stream, exposing its properties.
1172 *
1173 * Returns 1 if the node was read successfully, 0 if there is no more
1174 * nodes to read, or -1 in case of error
1175 */
1176int
1177xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +00001178 int val, olddepth = 0;
William M. Brack899e64a2003-09-26 18:03:42 +00001179 xmlTextReaderState oldstate = XML_TEXTREADER_START;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001180 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001181
William M. Brack1af55582004-07-22 17:18:00 +00001182
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001183 if (reader == NULL)
1184 return(-1);
William M. Brack1af55582004-07-22 17:18:00 +00001185 reader->curnode = NULL;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001186 if (reader->doc != NULL)
1187 return(xmlTextReaderReadTree(reader));
1188 if (reader->ctxt == NULL)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001189 return(-1);
1190 if (reader->ctxt->wellFormed != 1)
1191 return(-1);
1192
1193#ifdef DEBUG_READER
1194 fprintf(stderr, "\nREAD ");
1195 DUMP_READER
1196#endif
Daniel Veillard67df8092002-12-16 22:04:11 +00001197 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1198 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001199 /*
1200 * Initial state
1201 */
1202 do {
1203 val = xmlTextReaderPushData(reader);
1204 if (val < 0)
1205 return(-1);
1206 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001207 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1208 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001209 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001210 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001211 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001212 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001213 if (reader->node == NULL)
1214 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001215 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001216 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +00001217 if (reader->ctxt->myDoc != NULL) {
1218 reader->node = reader->ctxt->myDoc->children;
1219 }
1220 if (reader->node == NULL)
1221 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +00001222 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001223 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001224 reader->depth = 0;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00001225 reader->ctxt->parseMode = XML_PARSE_READER;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001226 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001227 }
1228 oldstate = reader->state;
1229 olddepth = reader->ctxt->nodeNr;
1230 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001231
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001232get_next_node:
Daniel Veillard6f379a72004-04-29 18:45:42 +00001233 if (reader->node == NULL) {
1234 if (reader->mode == XML_TEXTREADER_DONE)
1235 return(0);
1236 else
1237 return(-1);
1238 }
Daniel Veillarde2161a62004-04-29 17:14:25 +00001239
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001240 /*
1241 * If we are not backtracking on ancestors or examined nodes,
1242 * that the parser didn't finished or that we arent at the end
1243 * of stream, continue processing.
1244 */
Daniel Veillarde2161a62004-04-29 17:14:25 +00001245 while ((reader->node != NULL) && (reader->node->next == NULL) &&
Daniel Veillarda880b122003-04-21 21:36:41 +00001246 (reader->ctxt->nodeNr == olddepth) &&
1247 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +00001248 (reader->node->children == NULL) ||
1249 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard409a8142003-07-18 15:16:57 +00001250 ((reader->node->children != NULL) &&
1251 (reader->node->children->type == XML_TEXT_NODE) &&
1252 (reader->node->children->next == NULL)) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001253 (reader->node->type == XML_DTD_NODE) ||
1254 (reader->node->type == XML_DOCUMENT_NODE) ||
1255 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001256 ((reader->ctxt->node == NULL) ||
1257 (reader->ctxt->node == reader->node) ||
1258 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +00001259 (reader->ctxt->instate != XML_PARSER_EOF)) {
1260 val = xmlTextReaderPushData(reader);
1261 if (val < 0)
1262 return(-1);
1263 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001264 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001265 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001266 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001267 if ((reader->node->children != NULL) &&
1268 (reader->node->type != XML_ENTITY_REF_NODE) &&
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001269 (reader->node->type != XML_XINCLUDE_START) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001270 (reader->node->type != XML_DTD_NODE)) {
1271 reader->node = reader->node->children;
1272 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001273 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001274 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001275 }
1276 }
1277 if (reader->node->next != NULL) {
1278 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +00001279 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001280 (reader->node->children == NULL) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001281 ((reader->node->extra & NODE_IS_EMPTY) == 0)
1282#ifdef LIBXML_XINCLUDE_ENABLED
1283 && (reader->in_xinclude <= 0)
1284#endif
1285 ) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001286 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001287 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001288 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001289#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001290 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001291 (reader->node->type == XML_ELEMENT_NODE))
1292 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001293#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001294 if ((reader->preserves > 0) &&
1295 (reader->node->extra & NODE_IS_SPRESERVED))
1296 reader->preserves--;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001297 reader->node = reader->node->next;
1298 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001299
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001300 /*
1301 * Cleanup of the old node
1302 */
Daniel Veillard1e906612003-12-05 14:57:46 +00001303 if ((reader->preserves == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001304#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard1e906612003-12-05 14:57:46 +00001305 (reader->in_xinclude == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001306#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001307 (reader->entNr == 0) &&
1308 (reader->node->prev != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001309 (reader->node->prev->type != XML_DTD_NODE) &&
1310 (reader->entNr == 0)) {
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001311 xmlNodePtr tmp = reader->node->prev;
Daniel Veillarde8039df2003-10-27 11:25:13 +00001312 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001313 xmlUnlinkNode(tmp);
1314 xmlTextReaderFreeNode(reader, tmp);
1315 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001316 }
1317
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001318 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001319 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001320 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +00001321 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001322 (reader->node->children == NULL) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001323 ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001324 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001325 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001326 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001327#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001328 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001329 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001330#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001331 if ((reader->preserves > 0) &&
1332 (reader->node->extra & NODE_IS_SPRESERVED))
1333 reader->preserves--;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001334 reader->node = reader->node->parent;
1335 if ((reader->node == NULL) ||
1336 (reader->node->type == XML_DOCUMENT_NODE) ||
1337#ifdef LIBXML_DOCB_ENABLED
1338 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
1339#endif
1340 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001341 if (reader->mode != XML_TEXTREADER_DONE) {
1342 val = xmlParseChunk(reader->ctxt, "", 0, 1);
1343 reader->mode = XML_TEXTREADER_DONE;
William M. Brack9f797ab2004-07-28 07:40:12 +00001344 if (val != 0)
1345 return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001346 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001347 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001348 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001349
1350 /*
1351 * Cleanup of the old node
1352 */
Daniel Veillard1e906612003-12-05 14:57:46 +00001353 if ((reader->preserves == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001354#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard1e906612003-12-05 14:57:46 +00001355 (reader->in_xinclude == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001356#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001357 (reader->entNr == 0) &&
1358 (oldnode->type != XML_DTD_NODE) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001359 ((oldnode->extra & NODE_IS_PRESERVED) == 0) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001360 (reader->entNr == 0)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001361 xmlUnlinkNode(oldnode);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001362 xmlTextReaderFreeNode(reader, oldnode);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001363 }
1364
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001365 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001366 }
Daniel Veillard1e906612003-12-05 14:57:46 +00001367 if ((reader->preserves == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001368#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard1e906612003-12-05 14:57:46 +00001369 (reader->in_xinclude == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001370#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001371 (reader->entNr == 0) &&
1372 (reader->node->last != NULL) &&
1373 ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
1374 xmlNodePtr tmp = reader->node->last;
1375 xmlUnlinkNode(tmp);
1376 xmlTextReaderFreeNode(reader, tmp);
1377 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001378 reader->depth--;
1379 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001380
1381node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001382 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001383
1384 /*
Daniel Veillarda880b122003-04-21 21:36:41 +00001385 * If we are in the middle of a piece of CDATA make sure it's finished
1386 */
1387 if ((reader->node != NULL) &&
Daniel Veillardccc4d2b2003-09-17 21:27:31 +00001388 (reader->node->next == NULL) &&
Daniel Veillarda880b122003-04-21 21:36:41 +00001389 ((reader->node->type == XML_TEXT_NODE) ||
1390 (reader->node->type == XML_CDATA_SECTION_NODE))) {
William M. Brack42331a92004-07-29 07:07:16 +00001391 if (xmlTextReaderExpand(reader) == NULL)
1392 return -1;
Daniel Veillarda880b122003-04-21 21:36:41 +00001393 }
1394
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001395#ifdef LIBXML_XINCLUDE_ENABLED
1396 /*
1397 * Handle XInclude if asked for
1398 */
1399 if ((reader->xinclude) && (reader->node != NULL) &&
1400 (reader->node->type == XML_ELEMENT_NODE) &&
1401 (reader->node->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001402 ((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) ||
1403 (xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001404 if (reader->xincctxt == NULL) {
1405 reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
Daniel Veillardc14c3892004-08-16 12:34:50 +00001406 xmlXIncludeSetFlags(reader->xincctxt,
1407 reader->parserFlags & (~XML_PARSE_NOXINCNODE));
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001408 }
1409 /*
1410 * expand that node and process it
1411 */
William M. Brack42331a92004-07-29 07:07:16 +00001412 if (xmlTextReaderExpand(reader) == NULL)
1413 return -1;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001414 xmlXIncludeProcessNode(reader->xincctxt, reader->node);
1415 }
1416 if (reader->node->type == XML_XINCLUDE_START) {
1417 reader->in_xinclude++;
1418 goto get_next_node;
1419 }
1420 if (reader->node->type == XML_XINCLUDE_END) {
1421 reader->in_xinclude--;
1422 goto get_next_node;
1423 }
1424#endif
Daniel Veillarda880b122003-04-21 21:36:41 +00001425 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001426 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001427 */
1428 if ((reader->node != NULL) &&
1429 (reader->node->type == XML_ENTITY_REF_NODE) &&
1430 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1431 /*
1432 * Case where the underlying tree is not availble, lookup the entity
1433 * and walk it.
1434 */
1435 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
1436 (reader->ctxt->sax->getEntity != NULL)) {
1437 reader->node->children = (xmlNodePtr)
1438 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
1439 }
1440
1441 if ((reader->node->children != NULL) &&
1442 (reader->node->children->type == XML_ENTITY_DECL) &&
1443 (reader->node->children->children != NULL)) {
1444 xmlTextReaderEntPush(reader, reader->node);
1445 reader->node = reader->node->children->children;
1446 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001447#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001448 } else if ((reader->node != NULL) &&
1449 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +00001450 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001451 xmlTextReaderValidateEntity(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001452#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001453 }
1454 if ((reader->node != NULL) &&
1455 (reader->node->type == XML_ENTITY_DECL) &&
1456 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1457 reader->node = xmlTextReaderEntPop(reader);
1458 reader->depth++;
1459 goto get_next_node;
1460 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001461#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001462 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001463 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001464
1465 if ((node->type == XML_ELEMENT_NODE) &&
1466 ((reader->state != XML_TEXTREADER_END) &&
1467 (reader->state != XML_TEXTREADER_BACKTRACK))) {
1468 xmlTextReaderValidatePush(reader);
1469 } else if ((node->type == XML_TEXT_NODE) ||
1470 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001471 xmlTextReaderValidateCData(reader, node->content,
1472 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001473 }
1474 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001475#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001476#ifdef LIBXML_PATTERN_ENABLED
1477 if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
1478 (reader->state != XML_TEXTREADER_BACKTRACK)) {
1479 int i;
1480 for (i = 0;i < reader->patternNr;i++) {
1481 if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
1482 xmlTextReaderPreserve(reader);
1483 break;
1484 }
1485 }
1486 }
1487#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001488 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001489node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001490 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001491 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001492}
1493
Daniel Veillard67df8092002-12-16 22:04:11 +00001494/**
1495 * xmlTextReaderReadState:
1496 * @reader: the xmlTextReaderPtr used
1497 *
1498 * Gets the read state of the reader.
1499 *
1500 * Returns the state value, or -1 in case of error
1501 */
1502int
1503xmlTextReaderReadState(xmlTextReaderPtr reader) {
1504 if (reader == NULL)
1505 return(-1);
1506 return(reader->mode);
1507}
1508
1509/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001510 * xmlTextReaderExpand:
1511 * @reader: the xmlTextReaderPtr used
1512 *
1513 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +00001514 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001515 *
1516 * Returns a node pointer valid until the next xmlTextReaderRead() call
1517 * or NULL in case of error.
1518 */
1519xmlNodePtr
1520xmlTextReaderExpand(xmlTextReaderPtr reader) {
Daniel Veillardf40c1ce2003-12-19 17:26:28 +00001521 if ((reader == NULL) || (reader->node == NULL))
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001522 return(NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001523 if (reader->doc != NULL)
1524 return(reader->node);
Daniel Veillardf40c1ce2003-12-19 17:26:28 +00001525 if (reader->ctxt == NULL)
1526 return(NULL);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001527 if (xmlTextReaderDoExpand(reader) < 0)
1528 return(NULL);
1529 return(reader->node);
1530}
1531
1532/**
1533 * xmlTextReaderNext:
1534 * @reader: the xmlTextReaderPtr used
1535 *
1536 * Skip to the node following the current one in document order while
1537 * avoiding the subtree if any.
1538 *
1539 * Returns 1 if the node was read successfully, 0 if there is no more
1540 * nodes to read, or -1 in case of error
1541 */
1542int
1543xmlTextReaderNext(xmlTextReaderPtr reader) {
1544 int ret;
1545 xmlNodePtr cur;
1546
1547 if (reader == NULL)
1548 return(-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001549 if (reader->doc != NULL)
1550 return(xmlTextReaderNextTree(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001551 cur = reader->node;
1552 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1553 return(xmlTextReaderRead(reader));
1554 if (reader->state == XML_TEXTREADER_END)
1555 return(xmlTextReaderRead(reader));
Daniel Veillarde8039df2003-10-27 11:25:13 +00001556 if (cur->extra & NODE_IS_EMPTY)
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001557 return(xmlTextReaderRead(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001558 do {
1559 ret = xmlTextReaderRead(reader);
1560 if (ret != 1)
1561 return(ret);
1562 } while (reader->node != cur);
1563 return(xmlTextReaderRead(reader));
1564}
1565
1566/**
Daniel Veillard67df8092002-12-16 22:04:11 +00001567 * xmlTextReaderReadInnerXml:
1568 * @reader: the xmlTextReaderPtr used
1569 *
1570 * Reads the contents of the current node, including child nodes and markup.
1571 *
1572 * Returns a string containing the XML content, or NULL if the current node
1573 * is neither an element nor attribute, or has no child nodes. The
1574 * string must be deallocated by the caller.
1575 */
1576xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001577xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001578 TODO
1579 return(NULL);
1580}
1581
1582/**
1583 * xmlTextReaderReadOuterXml:
1584 * @reader: the xmlTextReaderPtr used
1585 *
1586 * Reads the contents of the current node, including child nodes and markup.
1587 *
1588 * Returns a string containing the XML content, or NULL if the current node
1589 * is neither an element nor attribute, or has no child nodes. The
1590 * string must be deallocated by the caller.
1591 */
1592xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001593xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001594 TODO
1595 return(NULL);
1596}
1597
1598/**
1599 * xmlTextReaderReadString:
1600 * @reader: the xmlTextReaderPtr used
1601 *
1602 * Reads the contents of an element or a text node as a string.
1603 *
1604 * Returns a string containing the contents of the Element or Text node,
1605 * or NULL if the reader is positioned on any other type of node.
1606 * The string must be deallocated by the caller.
1607 */
1608xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001609xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001610 TODO
1611 return(NULL);
1612}
1613
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001614#if 0
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001615/**
1616 * xmlTextReaderReadBase64:
1617 * @reader: the xmlTextReaderPtr used
1618 * @array: a byte array to store the content.
1619 * @offset: the zero-based index into array where the method should
1620 * begin to write.
1621 * @len: the number of bytes to write.
1622 *
1623 * Reads and decodes the Base64 encoded contents of an element and
1624 * stores the result in a byte buffer.
1625 *
1626 * Returns the number of bytes written to array, or zero if the current
1627 * instance is not positioned on an element or -1 in case of error.
1628 */
1629int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001630xmlTextReaderReadBase64(xmlTextReaderPtr reader,
1631 unsigned char *array ATTRIBUTE_UNUSED,
1632 int offset ATTRIBUTE_UNUSED,
1633 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001634 if ((reader == NULL) || (reader->ctxt == NULL))
1635 return(-1);
1636 if (reader->ctxt->wellFormed != 1)
1637 return(-1);
1638
1639 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1640 return(0);
1641 TODO
1642 return(0);
1643}
1644
1645/**
1646 * xmlTextReaderReadBinHex:
1647 * @reader: the xmlTextReaderPtr used
1648 * @array: a byte array to store the content.
1649 * @offset: the zero-based index into array where the method should
1650 * begin to write.
1651 * @len: the number of bytes to write.
1652 *
1653 * Reads and decodes the BinHex encoded contents of an element and
1654 * stores the result in a byte buffer.
1655 *
1656 * Returns the number of bytes written to array, or zero if the current
1657 * instance is not positioned on an element or -1 in case of error.
1658 */
1659int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001660xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
1661 unsigned char *array ATTRIBUTE_UNUSED,
1662 int offset ATTRIBUTE_UNUSED,
1663 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001664 if ((reader == NULL) || (reader->ctxt == NULL))
1665 return(-1);
1666 if (reader->ctxt->wellFormed != 1)
1667 return(-1);
1668
1669 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1670 return(0);
1671 TODO
1672 return(0);
1673}
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001674#endif
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001675
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001676/************************************************************************
1677 * *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001678 * Operating on a preparsed tree *
1679 * *
1680 ************************************************************************/
1681static int
1682xmlTextReaderNextTree(xmlTextReaderPtr reader)
1683{
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001684 if (reader == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001685 return(-1);
1686
1687 if (reader->state == XML_TEXTREADER_END)
1688 return(0);
1689
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001690 if (reader->node == NULL) {
1691 if (reader->doc->children == NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001692 reader->state = XML_TEXTREADER_END;
1693 return(0);
1694 }
1695
1696 reader->node = reader->doc->children;
1697 reader->state = XML_TEXTREADER_START;
1698 return(1);
1699 }
1700
1701 if (reader->state != XML_TEXTREADER_BACKTRACK) {
1702 if (reader->node->children != 0) {
1703 reader->node = reader->node->children;
1704 reader->depth++;
1705 reader->state = XML_TEXTREADER_START;
1706 return(1);
1707 }
1708
1709 if ((reader->node->type == XML_ELEMENT_NODE) ||
1710 (reader->node->type == XML_ATTRIBUTE_NODE)) {
1711 reader->state = XML_TEXTREADER_BACKTRACK;
1712 return(1);
1713 }
1714 }
1715
1716 if (reader->node->next != 0) {
1717 reader->node = reader->node->next;
1718 reader->state = XML_TEXTREADER_START;
1719 return(1);
1720 }
1721
1722 if (reader->node->parent != 0) {
1723 if (reader->node->parent->type == XML_DOCUMENT_NODE) {
1724 reader->state = XML_TEXTREADER_END;
1725 return(0);
1726 }
1727
1728 reader->node = reader->node->parent;
1729 reader->depth--;
1730 reader->state = XML_TEXTREADER_BACKTRACK;
1731 return(1);
1732 }
1733
1734 reader->state = XML_TEXTREADER_END;
1735
1736 return(1);
1737}
1738
1739/**
1740 * xmlTextReaderReadTree:
1741 * @reader: the xmlTextReaderPtr used
1742 *
1743 * Moves the position of the current instance to the next node in
1744 * the stream, exposing its properties.
1745 *
1746 * Returns 1 if the node was read successfully, 0 if there is no more
1747 * nodes to read, or -1 in case of error
1748 */
1749static int
1750xmlTextReaderReadTree(xmlTextReaderPtr reader) {
1751 if (reader->state == XML_TEXTREADER_END)
1752 return(0);
1753
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001754next_node:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001755 if (reader->node == NULL) {
1756 if (reader->doc->children == NULL) {
1757 reader->state = XML_TEXTREADER_END;
1758 return(0);
1759 }
1760
1761 reader->node = reader->doc->children;
1762 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001763 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001764 }
1765
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001766 if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
1767 (reader->node->type != XML_DTD_NODE) &&
1768 (reader->node->type != XML_XINCLUDE_START) &&
1769 (reader->node->type != XML_ENTITY_REF_NODE)) {
1770 if (reader->node->children != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001771 reader->node = reader->node->children;
1772 reader->depth++;
1773 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001774 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001775 }
1776
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001777 if (reader->node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001778 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001779 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001780 }
1781 }
1782
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001783 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001784 reader->node = reader->node->next;
1785 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001786 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001787 }
1788
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001789 if (reader->node->parent != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001790 if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
1791 (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
1792 reader->state = XML_TEXTREADER_END;
1793 return(0);
1794 }
1795
1796 reader->node = reader->node->parent;
1797 reader->depth--;
1798 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001799 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001800 }
1801
1802 reader->state = XML_TEXTREADER_END;
1803
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001804found_node:
1805 if ((reader->node->type == XML_XINCLUDE_START) ||
1806 (reader->node->type == XML_XINCLUDE_END))
1807 goto next_node;
1808
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001809 return(1);
1810}
1811
1812/**
William M. Brackb1d53162003-11-18 06:54:40 +00001813 * xmlTextReaderNextSibling:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001814 * @reader: the xmlTextReaderPtr used
1815 *
1816 * Skip to the node following the current one in document order while
1817 * avoiding the subtree if any.
1818 * Currently implemented only for Readers built on a document
1819 *
1820 * Returns 1 if the node was read successfully, 0 if there is no more
1821 * nodes to read, or -1 in case of error
1822 */
1823int
1824xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
1825 if (reader == NULL)
1826 return(-1);
1827 if (reader->doc == NULL) {
1828 TODO
1829 return(-1);
1830 }
1831
1832 if (reader->state == XML_TEXTREADER_END)
1833 return(0);
1834
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001835 if (reader->node == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001836 return(xmlTextReaderNextTree(reader));
1837
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001838 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001839 reader->node = reader->node->next;
1840 reader->state = XML_TEXTREADER_START;
1841 return(1);
1842 }
1843
1844 return(0);
1845}
1846
1847/************************************************************************
1848 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001849 * Constructor and destructors *
1850 * *
1851 ************************************************************************/
1852/**
1853 * xmlNewTextReader:
1854 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001855 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001856 *
1857 * Create an xmlTextReader structure fed with @input
1858 *
1859 * Returns the new xmlTextReaderPtr or NULL in case of error
1860 */
1861xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001862xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001863 xmlTextReaderPtr ret;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001864
1865 if (input == NULL)
1866 return(NULL);
1867 ret = xmlMalloc(sizeof(xmlTextReader));
1868 if (ret == NULL) {
1869 xmlGenericError(xmlGenericErrorContext,
1870 "xmlNewTextReader : malloc failed\n");
1871 return(NULL);
1872 }
1873 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001874 ret->doc = NULL;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001875 ret->entTab = NULL;
1876 ret->entMax = 0;
1877 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001878 ret->input = input;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001879 ret->buffer = xmlBufferCreateSize(100);
William M. Bracka3215c72004-07-31 16:24:01 +00001880 if (ret->buffer == NULL) {
1881 xmlFree(ret);
1882 xmlGenericError(xmlGenericErrorContext,
1883 "xmlNewTextReader : malloc failed\n");
1884 return(NULL);
1885 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001886 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1887 if (ret->sax == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001888 xmlBufferFree(ret->buffer);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001889 xmlFree(ret);
1890 xmlGenericError(xmlGenericErrorContext,
1891 "xmlNewTextReader : malloc failed\n");
1892 return(NULL);
1893 }
Daniel Veillard81273902003-09-30 00:43:48 +00001894 xmlSAXVersion(ret->sax, 2);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001895 ret->startElement = ret->sax->startElement;
1896 ret->sax->startElement = xmlTextReaderStartElement;
1897 ret->endElement = ret->sax->endElement;
1898 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillard81273902003-09-30 00:43:48 +00001899#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001900 if (ret->sax->initialized == XML_SAX2_MAGIC) {
Daniel Veillard81273902003-09-30 00:43:48 +00001901#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001902 ret->startElementNs = ret->sax->startElementNs;
1903 ret->sax->startElementNs = xmlTextReaderStartElementNs;
1904 ret->endElementNs = ret->sax->endElementNs;
1905 ret->sax->endElementNs = xmlTextReaderEndElementNs;
Daniel Veillard81273902003-09-30 00:43:48 +00001906#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001907 } else {
1908 ret->startElementNs = NULL;
1909 ret->endElementNs = NULL;
1910 }
Daniel Veillard81273902003-09-30 00:43:48 +00001911#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001912 ret->characters = ret->sax->characters;
1913 ret->sax->characters = xmlTextReaderCharacters;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001914 ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001915 ret->cdataBlock = ret->sax->cdataBlock;
1916 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001917
Daniel Veillard67df8092002-12-16 22:04:11 +00001918 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001919 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001920 ret->curnode = NULL;
Daniel Veillard53350552003-09-18 13:35:51 +00001921 if (ret->input->buffer->use < 4) {
William M. Brack899e64a2003-09-26 18:03:42 +00001922 xmlParserInputBufferRead(input, 4);
Daniel Veillard53350552003-09-18 13:35:51 +00001923 }
1924 if (ret->input->buffer->use >= 4) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001925 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001926 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001927 ret->base = 0;
1928 ret->cur = 4;
1929 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001930 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001931 ret->base = 0;
1932 ret->cur = 0;
1933 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00001934
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001935 if (ret->ctxt == NULL) {
1936 xmlGenericError(xmlGenericErrorContext,
1937 "xmlNewTextReader : malloc failed\n");
William M. Brack42331a92004-07-29 07:07:16 +00001938 xmlBufferFree(ret->buffer);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001939 xmlFree(ret->sax);
1940 xmlFree(ret);
1941 return(NULL);
1942 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00001943 ret->ctxt->parseMode = XML_PARSE_READER;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001944 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001945 ret->ctxt->linenumbers = 1;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001946 ret->ctxt->dictNames = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001947 ret->allocs = XML_TEXTREADER_CTXT;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001948 /*
1949 * use the parser dictionnary to allocate all elements and attributes names
1950 */
1951 ret->ctxt->docdict = 1;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001952 ret->dict = ret->ctxt->dict;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001953#ifdef LIBXML_XINCLUDE_ENABLED
1954 ret->xinclude = 0;
1955#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001956#ifdef LIBXML_PATTERN_ENABLED
1957 ret->patternMax = 0;
1958 ret->patternTab = NULL;
1959#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001960 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001961}
1962
1963/**
1964 * xmlNewTextReaderFilename:
1965 * @URI: the URI of the resource to process
1966 *
1967 * Create an xmlTextReader structure fed with the resource at @URI
1968 *
1969 * Returns the new xmlTextReaderPtr or NULL in case of error
1970 */
1971xmlTextReaderPtr
1972xmlNewTextReaderFilename(const char *URI) {
1973 xmlParserInputBufferPtr input;
1974 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001975 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001976
1977 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1978 if (input == NULL)
1979 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001980 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001981 if (ret == NULL) {
1982 xmlFreeParserInputBuffer(input);
1983 return(NULL);
1984 }
1985 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001986 if (ret->ctxt->directory == NULL)
1987 directory = xmlParserGetDirectory(URI);
1988 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1989 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1990 if (directory != NULL)
1991 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001992 return(ret);
1993}
1994
1995/**
1996 * xmlFreeTextReader:
1997 * @reader: the xmlTextReaderPtr
1998 *
1999 * Deallocate all the resources associated to the reader
2000 */
2001void
2002xmlFreeTextReader(xmlTextReaderPtr reader) {
2003 if (reader == NULL)
2004 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00002005#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00002006 if (reader->rngSchemas != NULL) {
2007 xmlRelaxNGFree(reader->rngSchemas);
2008 reader->rngSchemas = NULL;
2009 }
2010 if (reader->rngValidCtxt != NULL) {
2011 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2012 reader->rngValidCtxt = NULL;
2013 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00002014#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002015#ifdef LIBXML_XINCLUDE_ENABLED
2016 if (reader->xincctxt != NULL)
2017 xmlXIncludeFreeContext(reader->xincctxt);
2018#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00002019#ifdef LIBXML_PATTERN_ENABLED
2020 if (reader->patternTab != NULL) {
2021 int i;
2022 for (i = 0;i < reader->patternNr;i++) {
2023 if (reader->patternTab[i] != NULL)
2024 xmlFreePattern(reader->patternTab[i]);
2025 }
2026 xmlFree(reader->patternTab);
2027 }
2028#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002029 if (reader->ctxt != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002030 if (reader->dict == reader->ctxt->dict)
2031 reader->dict = NULL;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002032 if (reader->ctxt->myDoc != NULL) {
2033 if (reader->preserve == 0)
2034 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
2035 reader->ctxt->myDoc = NULL;
2036 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00002037 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
2038 (reader->ctxt->vctxt.vstateMax > 0)){
2039 xmlFree(reader->ctxt->vctxt.vstateTab);
2040 reader->ctxt->vctxt.vstateTab = 0;
2041 reader->ctxt->vctxt.vstateMax = 0;
2042 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002043 if (reader->allocs & XML_TEXTREADER_CTXT)
2044 xmlFreeParserCtxt(reader->ctxt);
2045 }
2046 if (reader->sax != NULL)
2047 xmlFree(reader->sax);
2048 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
2049 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002050 if (reader->faketext != NULL) {
2051 xmlFreeNode(reader->faketext);
2052 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002053 if (reader->buffer != NULL)
2054 xmlBufferFree(reader->buffer);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00002055 if (reader->entTab != NULL)
2056 xmlFree(reader->entTab);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002057 if (reader->dict != NULL)
2058 xmlDictFree(reader->dict);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002059 xmlFree(reader);
2060}
2061
2062/************************************************************************
2063 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002064 * Methods for XmlTextReader *
2065 * *
2066 ************************************************************************/
2067/**
2068 * xmlTextReaderClose:
2069 * @reader: the xmlTextReaderPtr used
2070 *
2071 * This method releases any resources allocated by the current instance
2072 * changes the state to Closed and close any underlying input.
2073 *
2074 * Returns 0 or -1 in case of error
2075 */
2076int
2077xmlTextReaderClose(xmlTextReaderPtr reader) {
2078 if (reader == NULL)
2079 return(-1);
2080 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002081 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002082 reader->mode = XML_TEXTREADER_MODE_CLOSED;
2083 if (reader->ctxt != NULL) {
Daniel Veillardee1d6922004-04-18 14:58:57 +00002084 xmlStopParser(reader->ctxt);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002085 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002086 if (reader->preserve == 0)
2087 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002088 reader->ctxt->myDoc = NULL;
2089 }
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002090 }
2091 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
2092 xmlFreeParserInputBuffer(reader->input);
2093 reader->allocs -= XML_TEXTREADER_INPUT;
2094 }
2095 return(0);
2096}
2097
2098/**
2099 * xmlTextReaderGetAttributeNo:
2100 * @reader: the xmlTextReaderPtr used
2101 * @no: the zero-based index of the attribute relative to the containing element
2102 *
2103 * Provides the value of the attribute with the specified index relative
2104 * to the containing element.
2105 *
2106 * Returns a string containing the value of the specified attribute, or NULL
2107 * in case of error. The string must be deallocated by the caller.
2108 */
2109xmlChar *
2110xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
2111 xmlChar *ret;
2112 int i;
2113 xmlAttrPtr cur;
2114 xmlNsPtr ns;
2115
2116 if (reader == NULL)
2117 return(NULL);
2118 if (reader->node == NULL)
2119 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002120 if (reader->curnode != NULL)
2121 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002122 /* TODO: handle the xmlDecl */
2123 if (reader->node->type != XML_ELEMENT_NODE)
2124 return(NULL);
2125
2126 ns = reader->node->nsDef;
2127 for (i = 0;(i < no) && (ns != NULL);i++) {
2128 ns = ns->next;
2129 }
2130 if (ns != NULL)
2131 return(xmlStrdup(ns->href));
2132
2133 cur = reader->node->properties;
2134 if (cur == NULL)
2135 return(NULL);
2136 for (;i < no;i++) {
2137 cur = cur->next;
2138 if (cur == NULL)
2139 return(NULL);
2140 }
2141 /* TODO walk the DTD if present */
2142
2143 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
2144 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
2145 return(ret);
2146}
2147
2148/**
2149 * xmlTextReaderGetAttribute:
2150 * @reader: the xmlTextReaderPtr used
2151 * @name: the qualified name of the attribute.
2152 *
2153 * Provides the value of the attribute with the specified qualified name.
2154 *
2155 * Returns a string containing the value of the specified attribute, or NULL
2156 * in case of error. The string must be deallocated by the caller.
2157 */
2158xmlChar *
2159xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2160 xmlChar *prefix = NULL;
2161 xmlChar *localname;
2162 xmlNsPtr ns;
2163 xmlChar *ret = NULL;
2164
2165 if ((reader == NULL) || (name == NULL))
2166 return(NULL);
2167 if (reader->node == NULL)
2168 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002169 if (reader->curnode != NULL)
2170 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002171
2172 /* TODO: handle the xmlDecl */
2173 if (reader->node->type != XML_ELEMENT_NODE)
2174 return(NULL);
2175
2176 localname = xmlSplitQName2(name, &prefix);
2177 if (localname == NULL)
2178 return(xmlGetProp(reader->node, name));
2179
2180 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2181 if (ns != NULL)
2182 ret = xmlGetNsProp(reader->node, localname, ns->href);
2183
2184 if (localname != NULL)
2185 xmlFree(localname);
2186 if (prefix != NULL)
2187 xmlFree(prefix);
2188 return(ret);
2189}
2190
2191
2192/**
2193 * xmlTextReaderGetAttributeNs:
2194 * @reader: the xmlTextReaderPtr used
2195 * @localName: the local name of the attribute.
2196 * @namespaceURI: the namespace URI of the attribute.
2197 *
2198 * Provides the value of the specified attribute
2199 *
2200 * Returns a string containing the value of the specified attribute, or NULL
2201 * in case of error. The string must be deallocated by the caller.
2202 */
2203xmlChar *
2204xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
2205 const xmlChar *namespaceURI) {
2206 if ((reader == NULL) || (localName == NULL))
2207 return(NULL);
2208 if (reader->node == NULL)
2209 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002210 if (reader->curnode != NULL)
2211 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002212
2213 /* TODO: handle the xmlDecl */
2214 if (reader->node->type != XML_ELEMENT_NODE)
2215 return(NULL);
2216
2217 return(xmlGetNsProp(reader->node, localName, namespaceURI));
2218}
2219
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002220/**
2221 * xmlTextReaderGetRemainder:
2222 * @reader: the xmlTextReaderPtr used
2223 *
2224 * Method to get the remainder of the buffered XML. this method stops the
2225 * parser, set its state to End Of File and return the input stream with
2226 * what is left that the parser did not use.
2227 *
Daniel Veillardee1d6922004-04-18 14:58:57 +00002228 * The implementation is not good, the parser certainly procgressed past
2229 * what's left in reader->input, and there is an allocation problem. Best
2230 * would be to rewrite it differently.
2231 *
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002232 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
2233 * in case of error.
2234 */
2235xmlParserInputBufferPtr
2236xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
2237 xmlParserInputBufferPtr ret = NULL;
2238
2239 if (reader == NULL)
2240 return(NULL);
2241 if (reader->node == NULL)
2242 return(NULL);
2243
2244 reader->node = NULL;
2245 reader->curnode = NULL;
2246 reader->mode = XML_TEXTREADER_MODE_EOF;
2247 if (reader->ctxt != NULL) {
Daniel Veillardee1d6922004-04-18 14:58:57 +00002248 xmlStopParser(reader->ctxt);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002249 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002250 if (reader->preserve == 0)
2251 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002252 reader->ctxt->myDoc = NULL;
2253 }
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002254 }
2255 if (reader->allocs & XML_TEXTREADER_INPUT) {
2256 ret = reader->input;
Daniel Veillardee1d6922004-04-18 14:58:57 +00002257 reader->input = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002258 reader->allocs -= XML_TEXTREADER_INPUT;
2259 } else {
2260 /*
2261 * Hum, one may need to duplicate the data structure because
2262 * without reference counting the input may be freed twice:
2263 * - by the layer which allocated it.
2264 * - by the layer to which would have been returned to.
2265 */
2266 TODO
2267 return(NULL);
2268 }
2269 return(ret);
2270}
2271
2272/**
2273 * xmlTextReaderLookupNamespace:
2274 * @reader: the xmlTextReaderPtr used
2275 * @prefix: the prefix whose namespace URI is to be resolved. To return
2276 * the default namespace, specify NULL
2277 *
2278 * Resolves a namespace prefix in the scope of the current element.
2279 *
2280 * Returns a string containing the namespace URI to which the prefix maps
2281 * or NULL in case of error. The string must be deallocated by the caller.
2282 */
2283xmlChar *
2284xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
2285 xmlNsPtr ns;
2286
2287 if (reader == NULL)
2288 return(NULL);
2289 if (reader->node == NULL)
2290 return(NULL);
2291
2292 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2293 if (ns == NULL)
2294 return(NULL);
2295 return(xmlStrdup(ns->href));
2296}
2297
2298/**
2299 * xmlTextReaderMoveToAttributeNo:
2300 * @reader: the xmlTextReaderPtr used
2301 * @no: the zero-based index of the attribute relative to the containing
2302 * element.
2303 *
2304 * Moves the position of the current instance to the attribute with
2305 * the specified index relative to the containing element.
2306 *
2307 * Returns 1 in case of success, -1 in case of error, 0 if not found
2308 */
2309int
2310xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
2311 int i;
2312 xmlAttrPtr cur;
2313 xmlNsPtr ns;
2314
2315 if (reader == NULL)
2316 return(-1);
2317 if (reader->node == NULL)
2318 return(-1);
2319 /* TODO: handle the xmlDecl */
2320 if (reader->node->type != XML_ELEMENT_NODE)
2321 return(-1);
2322
2323 reader->curnode = NULL;
2324
2325 ns = reader->node->nsDef;
2326 for (i = 0;(i < no) && (ns != NULL);i++) {
2327 ns = ns->next;
2328 }
2329 if (ns != NULL) {
2330 reader->curnode = (xmlNodePtr) ns;
2331 return(1);
2332 }
2333
2334 cur = reader->node->properties;
2335 if (cur == NULL)
2336 return(0);
2337 for (;i < no;i++) {
2338 cur = cur->next;
2339 if (cur == NULL)
2340 return(0);
2341 }
2342 /* TODO walk the DTD if present */
2343
2344 reader->curnode = (xmlNodePtr) cur;
2345 return(1);
2346}
2347
2348/**
2349 * xmlTextReaderMoveToAttribute:
2350 * @reader: the xmlTextReaderPtr used
2351 * @name: the qualified name of the attribute.
2352 *
2353 * Moves the position of the current instance to the attribute with
2354 * the specified qualified name.
2355 *
2356 * Returns 1 in case of success, -1 in case of error, 0 if not found
2357 */
2358int
2359xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2360 xmlChar *prefix = NULL;
2361 xmlChar *localname;
2362 xmlNsPtr ns;
2363 xmlAttrPtr prop;
2364
2365 if ((reader == NULL) || (name == NULL))
2366 return(-1);
2367 if (reader->node == NULL)
2368 return(-1);
2369
2370 /* TODO: handle the xmlDecl */
2371 if (reader->node->type != XML_ELEMENT_NODE)
2372 return(0);
2373
2374 localname = xmlSplitQName2(name, &prefix);
2375 if (localname == NULL) {
2376 /*
2377 * Namespace default decl
2378 */
2379 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2380 ns = reader->node->nsDef;
2381 while (ns != NULL) {
2382 if (ns->prefix == NULL) {
2383 reader->curnode = (xmlNodePtr) ns;
2384 return(1);
2385 }
2386 ns = ns->next;
2387 }
2388 return(0);
2389 }
2390
2391 prop = reader->node->properties;
2392 while (prop != NULL) {
2393 /*
2394 * One need to have
2395 * - same attribute names
2396 * - and the attribute carrying that namespace
2397 */
2398 if ((xmlStrEqual(prop->name, name)) &&
2399 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
2400 reader->curnode = (xmlNodePtr) prop;
2401 return(1);
2402 }
2403 prop = prop->next;
2404 }
2405 return(0);
2406 }
2407
2408 /*
2409 * Namespace default decl
2410 */
2411 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2412 ns = reader->node->nsDef;
2413 while (ns != NULL) {
2414 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2415 reader->curnode = (xmlNodePtr) ns;
2416 goto found;
2417 }
2418 ns = ns->next;
2419 }
2420 goto not_found;
2421 }
2422 prop = reader->node->properties;
2423 while (prop != NULL) {
2424 /*
2425 * One need to have
2426 * - same attribute names
2427 * - and the attribute carrying that namespace
2428 */
2429 if ((xmlStrEqual(prop->name, localname)) &&
2430 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2431 reader->curnode = (xmlNodePtr) prop;
2432 goto found;
2433 }
2434 prop = prop->next;
2435 }
2436not_found:
2437 if (localname != NULL)
2438 xmlFree(localname);
2439 if (prefix != NULL)
2440 xmlFree(prefix);
2441 return(0);
2442
2443found:
2444 if (localname != NULL)
2445 xmlFree(localname);
2446 if (prefix != NULL)
2447 xmlFree(prefix);
2448 return(1);
2449}
2450
2451/**
2452 * xmlTextReaderMoveToAttributeNs:
2453 * @reader: the xmlTextReaderPtr used
2454 * @localName: the local name of the attribute.
2455 * @namespaceURI: the namespace URI of the attribute.
2456 *
2457 * Moves the position of the current instance to the attribute with the
2458 * specified local name and namespace URI.
2459 *
2460 * Returns 1 in case of success, -1 in case of error, 0 if not found
2461 */
2462int
2463xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
2464 const xmlChar *localName, const xmlChar *namespaceURI) {
2465 xmlAttrPtr prop;
2466 xmlNodePtr node;
2467
2468 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2469 return(-1);
2470 if (reader->node == NULL)
2471 return(-1);
2472 if (reader->node->type != XML_ELEMENT_NODE)
2473 return(0);
2474 node = reader->node;
2475
2476 /*
2477 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
2478 * namespace name associated to "xmlns"
2479 */
2480 prop = node->properties;
2481 while (prop != NULL) {
2482 /*
2483 * One need to have
2484 * - same attribute names
2485 * - and the attribute carrying that namespace
2486 */
2487 if (xmlStrEqual(prop->name, localName) &&
2488 ((prop->ns != NULL) &&
2489 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2490 reader->curnode = (xmlNodePtr) prop;
2491 return(1);
2492 }
2493 prop = prop->next;
2494 }
2495 return(0);
2496}
2497
2498/**
2499 * xmlTextReaderMoveToFirstAttribute:
2500 * @reader: the xmlTextReaderPtr used
2501 *
2502 * Moves the position of the current instance to the first attribute
2503 * associated with the current node.
2504 *
2505 * Returns 1 in case of success, -1 in case of error, 0 if not found
2506 */
2507int
2508xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
2509 if (reader == NULL)
2510 return(-1);
2511 if (reader->node == NULL)
2512 return(-1);
2513 if (reader->node->type != XML_ELEMENT_NODE)
2514 return(0);
2515
2516 if (reader->node->nsDef != NULL) {
2517 reader->curnode = (xmlNodePtr) reader->node->nsDef;
2518 return(1);
2519 }
2520 if (reader->node->properties != NULL) {
2521 reader->curnode = (xmlNodePtr) reader->node->properties;
2522 return(1);
2523 }
2524 return(0);
2525}
2526
2527/**
2528 * xmlTextReaderMoveToNextAttribute:
2529 * @reader: the xmlTextReaderPtr used
2530 *
2531 * Moves the position of the current instance to the next attribute
2532 * associated with the current node.
2533 *
2534 * Returns 1 in case of success, -1 in case of error, 0 if not found
2535 */
2536int
2537xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
2538 if (reader == NULL)
2539 return(-1);
2540 if (reader->node == NULL)
2541 return(-1);
2542 if (reader->node->type != XML_ELEMENT_NODE)
2543 return(0);
2544 if (reader->curnode == NULL)
2545 return(xmlTextReaderMoveToFirstAttribute(reader));
2546
2547 if (reader->curnode->type == XML_NAMESPACE_DECL) {
2548 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2549 if (ns->next != NULL) {
2550 reader->curnode = (xmlNodePtr) ns->next;
2551 return(1);
2552 }
2553 if (reader->node->properties != NULL) {
2554 reader->curnode = (xmlNodePtr) reader->node->properties;
2555 return(1);
2556 }
2557 return(0);
2558 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2559 (reader->curnode->next != NULL)) {
2560 reader->curnode = reader->curnode->next;
2561 return(1);
2562 }
2563 return(0);
2564}
2565
2566/**
2567 * xmlTextReaderMoveToElement:
2568 * @reader: the xmlTextReaderPtr used
2569 *
2570 * Moves the position of the current instance to the node that
2571 * contains the current Attribute node.
2572 *
2573 * Returns 1 in case of success, -1 in case of error, 0 if not moved
2574 */
2575int
2576xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
2577 if (reader == NULL)
2578 return(-1);
2579 if (reader->node == NULL)
2580 return(-1);
2581 if (reader->node->type != XML_ELEMENT_NODE)
2582 return(0);
2583 if (reader->curnode != NULL) {
2584 reader->curnode = NULL;
2585 return(1);
2586 }
2587 return(0);
2588}
2589
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002590/**
2591 * xmlTextReaderReadAttributeValue:
2592 * @reader: the xmlTextReaderPtr used
2593 *
2594 * Parses an attribute value into one or more Text and EntityReference nodes.
2595 *
2596 * Returns 1 in case of success, 0 if the reader was not positionned on an
2597 * ttribute node or all the attribute values have been read, or -1
2598 * in case of error.
2599 */
2600int
2601xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
2602 if (reader == NULL)
2603 return(-1);
2604 if (reader->node == NULL)
2605 return(-1);
2606 if (reader->curnode == NULL)
2607 return(0);
2608 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2609 if (reader->curnode->children == NULL)
2610 return(0);
2611 reader->curnode = reader->curnode->children;
2612 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2613 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2614
2615 if (reader->faketext == NULL) {
2616 reader->faketext = xmlNewDocText(reader->node->doc,
2617 ns->href);
2618 } else {
2619 if (reader->faketext->content != NULL)
2620 xmlFree(reader->faketext->content);
2621 reader->faketext->content = xmlStrdup(ns->href);
2622 }
2623 reader->curnode = reader->faketext;
2624 } else {
2625 if (reader->curnode->next == NULL)
2626 return(0);
2627 reader->curnode = reader->curnode->next;
2628 }
2629 return(1);
2630}
2631
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002632/************************************************************************
2633 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002634 * Acces API to the current node *
2635 * *
2636 ************************************************************************/
2637/**
2638 * xmlTextReaderAttributeCount:
2639 * @reader: the xmlTextReaderPtr used
2640 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002641 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002642 *
2643 * Returns 0 i no attributes, -1 in case of error or the attribute count
2644 */
2645int
2646xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
2647 int ret;
2648 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00002649 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002650 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002651
2652 if (reader == NULL)
2653 return(-1);
2654 if (reader->node == NULL)
2655 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002656
2657 if (reader->curnode != NULL)
2658 node = reader->curnode;
2659 else
2660 node = reader->node;
2661
2662 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002663 return(0);
2664 if ((reader->state == XML_TEXTREADER_END) ||
2665 (reader->state == XML_TEXTREADER_BACKTRACK))
2666 return(0);
2667 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002668 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002669 while (attr != NULL) {
2670 ret++;
2671 attr = attr->next;
2672 }
Daniel Veillard67df8092002-12-16 22:04:11 +00002673 ns = node->nsDef;
2674 while (ns != NULL) {
2675 ret++;
2676 ns = ns->next;
2677 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002678 return(ret);
2679}
2680
2681/**
2682 * xmlTextReaderNodeType:
2683 * @reader: the xmlTextReaderPtr used
2684 *
2685 * Get the node type of the current node
2686 * Reference:
2687 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
2688 *
2689 * Returns the xmlNodeType of the current node or -1 in case of error
2690 */
2691int
2692xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002693 xmlNodePtr node;
Daniel Veillardd6038e02003-07-30 16:37:18 +00002694
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002695 if (reader == NULL)
2696 return(-1);
2697 if (reader->node == NULL)
Daniel Veillardd6038e02003-07-30 16:37:18 +00002698 return(XML_READER_TYPE_NONE);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002699 if (reader->curnode != NULL)
2700 node = reader->curnode;
2701 else
2702 node = reader->node;
2703 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002704 case XML_ELEMENT_NODE:
2705 if ((reader->state == XML_TEXTREADER_END) ||
2706 (reader->state == XML_TEXTREADER_BACKTRACK))
Daniel Veillardd6038e02003-07-30 16:37:18 +00002707 return(XML_READER_TYPE_END_ELEMENT);
2708 return(XML_READER_TYPE_ELEMENT);
Daniel Veillardecaba492002-12-30 10:55:29 +00002709 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002710 case XML_ATTRIBUTE_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002711 return(XML_READER_TYPE_ATTRIBUTE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002712 case XML_TEXT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002713 if (xmlIsBlankNode(reader->node)) {
2714 if (xmlNodeGetSpacePreserve(reader->node))
2715 return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2716 else
2717 return(XML_READER_TYPE_WHITESPACE);
2718 } else {
2719 return(XML_READER_TYPE_TEXT);
2720 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002721 case XML_CDATA_SECTION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002722 return(XML_READER_TYPE_CDATA);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002723 case XML_ENTITY_REF_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002724 return(XML_READER_TYPE_ENTITY_REFERENCE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002725 case XML_ENTITY_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002726 return(XML_READER_TYPE_ENTITY);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002727 case XML_PI_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002728 return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002729 case XML_COMMENT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002730 return(XML_READER_TYPE_COMMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002731 case XML_DOCUMENT_NODE:
2732 case XML_HTML_DOCUMENT_NODE:
2733#ifdef LIBXML_DOCB_ENABLED
2734 case XML_DOCB_DOCUMENT_NODE:
2735#endif
Daniel Veillardd6038e02003-07-30 16:37:18 +00002736 return(XML_READER_TYPE_DOCUMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002737 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002738 return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002739 case XML_NOTATION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002740 return(XML_READER_TYPE_NOTATION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002741 case XML_DOCUMENT_TYPE_NODE:
2742 case XML_DTD_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002743 return(XML_READER_TYPE_DOCUMENT_TYPE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002744
2745 case XML_ELEMENT_DECL:
2746 case XML_ATTRIBUTE_DECL:
2747 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002748 case XML_XINCLUDE_START:
2749 case XML_XINCLUDE_END:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002750 return(XML_READER_TYPE_NONE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002751 }
2752 return(-1);
2753}
2754
2755/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002756 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002757 * @reader: the xmlTextReaderPtr used
2758 *
2759 * Check if the current node is empty
2760 *
2761 * Returns 1 if empty, 0 if not and -1 in case of error
2762 */
2763int
2764xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
2765 if ((reader == NULL) || (reader->node == NULL))
2766 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00002767 if (reader->node->type != XML_ELEMENT_NODE)
2768 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00002769 if (reader->curnode != NULL)
2770 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002771 if (reader->node->children != NULL)
2772 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00002773 if (reader->state == XML_TEXTREADER_END)
2774 return(0);
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002775 if (reader->doc != NULL)
2776 return(1);
Daniel Veillard96b6cd22004-01-08 16:49:50 +00002777#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002778 if (reader->in_xinclude > 0)
2779 return(1);
Daniel Veillard96b6cd22004-01-08 16:49:50 +00002780#endif
Daniel Veillarde8039df2003-10-27 11:25:13 +00002781 return((reader->node->extra & NODE_IS_EMPTY) != 0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002782}
2783
2784/**
2785 * xmlTextReaderLocalName:
2786 * @reader: the xmlTextReaderPtr used
2787 *
2788 * The local name of the node.
2789 *
2790 * Returns the local name or NULL if not available
2791 */
2792xmlChar *
2793xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002794 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002795 if ((reader == NULL) || (reader->node == NULL))
2796 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002797 if (reader->curnode != NULL)
2798 node = reader->curnode;
2799 else
2800 node = reader->node;
2801 if (node->type == XML_NAMESPACE_DECL) {
2802 xmlNsPtr ns = (xmlNsPtr) node;
2803 if (ns->prefix == NULL)
2804 return(xmlStrdup(BAD_CAST "xmlns"));
2805 else
2806 return(xmlStrdup(ns->prefix));
2807 }
2808 if ((node->type != XML_ELEMENT_NODE) &&
2809 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002810 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002811 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002812}
2813
2814/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002815 * xmlTextReaderConstLocalName:
2816 * @reader: the xmlTextReaderPtr used
2817 *
2818 * The local name of the node.
2819 *
2820 * Returns the local name or NULL if not available, the
2821 * string will be deallocated with the reader.
2822 */
2823const xmlChar *
2824xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
2825 xmlNodePtr node;
2826 if ((reader == NULL) || (reader->node == NULL))
2827 return(NULL);
2828 if (reader->curnode != NULL)
2829 node = reader->curnode;
2830 else
2831 node = reader->node;
2832 if (node->type == XML_NAMESPACE_DECL) {
2833 xmlNsPtr ns = (xmlNsPtr) node;
2834 if (ns->prefix == NULL)
2835 return(CONSTSTR(BAD_CAST "xmlns"));
2836 else
2837 return(ns->prefix);
2838 }
2839 if ((node->type != XML_ELEMENT_NODE) &&
2840 (node->type != XML_ATTRIBUTE_NODE))
2841 return(xmlTextReaderConstName(reader));
2842 return(node->name);
2843}
2844
2845/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002846 * xmlTextReaderName:
2847 * @reader: the xmlTextReaderPtr used
2848 *
2849 * The qualified name of the node, equal to Prefix :LocalName.
2850 *
2851 * Returns the local name or NULL if not available
2852 */
2853xmlChar *
2854xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002855 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002856 xmlChar *ret;
2857
2858 if ((reader == NULL) || (reader->node == NULL))
2859 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002860 if (reader->curnode != NULL)
2861 node = reader->curnode;
2862 else
2863 node = reader->node;
2864 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002865 case XML_ELEMENT_NODE:
2866 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002867 if ((node->ns == NULL) ||
2868 (node->ns->prefix == NULL))
2869 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002870
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002871 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002872 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002873 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002874 return(ret);
2875 case XML_TEXT_NODE:
2876 return(xmlStrdup(BAD_CAST "#text"));
2877 case XML_CDATA_SECTION_NODE:
2878 return(xmlStrdup(BAD_CAST "#cdata-section"));
2879 case XML_ENTITY_NODE:
2880 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002881 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002882 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002883 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002884 case XML_COMMENT_NODE:
2885 return(xmlStrdup(BAD_CAST "#comment"));
2886 case XML_DOCUMENT_NODE:
2887 case XML_HTML_DOCUMENT_NODE:
2888#ifdef LIBXML_DOCB_ENABLED
2889 case XML_DOCB_DOCUMENT_NODE:
2890#endif
2891 return(xmlStrdup(BAD_CAST "#document"));
2892 case XML_DOCUMENT_FRAG_NODE:
2893 return(xmlStrdup(BAD_CAST "#document-fragment"));
2894 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002895 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002896 case XML_DOCUMENT_TYPE_NODE:
2897 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002898 return(xmlStrdup(node->name));
2899 case XML_NAMESPACE_DECL: {
2900 xmlNsPtr ns = (xmlNsPtr) node;
2901
2902 ret = xmlStrdup(BAD_CAST "xmlns");
2903 if (ns->prefix == NULL)
2904 return(ret);
2905 ret = xmlStrcat(ret, BAD_CAST ":");
2906 ret = xmlStrcat(ret, ns->prefix);
2907 return(ret);
2908 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002909
2910 case XML_ELEMENT_DECL:
2911 case XML_ATTRIBUTE_DECL:
2912 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002913 case XML_XINCLUDE_START:
2914 case XML_XINCLUDE_END:
2915 return(NULL);
2916 }
2917 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002918}
2919
2920/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002921 * xmlTextReaderConstName:
2922 * @reader: the xmlTextReaderPtr used
2923 *
2924 * The qualified name of the node, equal to Prefix :LocalName.
2925 *
2926 * Returns the local name or NULL if not available, the string is
2927 * deallocated with the reader.
2928 */
2929const xmlChar *
2930xmlTextReaderConstName(xmlTextReaderPtr reader) {
2931 xmlNodePtr node;
2932
2933 if ((reader == NULL) || (reader->node == NULL))
2934 return(NULL);
2935 if (reader->curnode != NULL)
2936 node = reader->curnode;
2937 else
2938 node = reader->node;
2939 switch (node->type) {
2940 case XML_ELEMENT_NODE:
2941 case XML_ATTRIBUTE_NODE:
2942 if ((node->ns == NULL) ||
2943 (node->ns->prefix == NULL))
2944 return(node->name);
2945 return(CONSTQSTR(node->ns->prefix, node->name));
2946 case XML_TEXT_NODE:
2947 return(CONSTSTR(BAD_CAST "#text"));
2948 case XML_CDATA_SECTION_NODE:
2949 return(CONSTSTR(BAD_CAST "#cdata-section"));
2950 case XML_ENTITY_NODE:
2951 case XML_ENTITY_REF_NODE:
2952 return(CONSTSTR(node->name));
2953 case XML_PI_NODE:
2954 return(CONSTSTR(node->name));
2955 case XML_COMMENT_NODE:
2956 return(CONSTSTR(BAD_CAST "#comment"));
2957 case XML_DOCUMENT_NODE:
2958 case XML_HTML_DOCUMENT_NODE:
2959#ifdef LIBXML_DOCB_ENABLED
2960 case XML_DOCB_DOCUMENT_NODE:
2961#endif
2962 return(CONSTSTR(BAD_CAST "#document"));
2963 case XML_DOCUMENT_FRAG_NODE:
2964 return(CONSTSTR(BAD_CAST "#document-fragment"));
2965 case XML_NOTATION_NODE:
2966 return(CONSTSTR(node->name));
2967 case XML_DOCUMENT_TYPE_NODE:
2968 case XML_DTD_NODE:
2969 return(CONSTSTR(node->name));
2970 case XML_NAMESPACE_DECL: {
2971 xmlNsPtr ns = (xmlNsPtr) node;
2972
2973 if (ns->prefix == NULL)
2974 return(CONSTSTR(BAD_CAST "xmlns"));
2975 return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
2976 }
2977
2978 case XML_ELEMENT_DECL:
2979 case XML_ATTRIBUTE_DECL:
2980 case XML_ENTITY_DECL:
2981 case XML_XINCLUDE_START:
2982 case XML_XINCLUDE_END:
2983 return(NULL);
2984 }
2985 return(NULL);
2986}
2987
2988/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002989 * xmlTextReaderPrefix:
2990 * @reader: the xmlTextReaderPtr used
2991 *
2992 * A shorthand reference to the namespace associated with the node.
2993 *
2994 * Returns the prefix or NULL if not available
2995 */
2996xmlChar *
2997xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002998 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002999 if ((reader == NULL) || (reader->node == NULL))
3000 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003001 if (reader->curnode != NULL)
3002 node = reader->curnode;
3003 else
3004 node = reader->node;
3005 if (node->type == XML_NAMESPACE_DECL) {
3006 xmlNsPtr ns = (xmlNsPtr) node;
3007 if (ns->prefix == NULL)
3008 return(NULL);
3009 return(xmlStrdup(BAD_CAST "xmlns"));
3010 }
3011 if ((node->type != XML_ELEMENT_NODE) &&
3012 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003013 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00003014 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003015 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003016 return(NULL);
3017}
3018
3019/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003020 * xmlTextReaderConstPrefix:
3021 * @reader: the xmlTextReaderPtr used
3022 *
3023 * A shorthand reference to the namespace associated with the node.
3024 *
3025 * Returns the prefix or NULL if not available, the string is deallocated
3026 * with the reader.
3027 */
3028const xmlChar *
3029xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
3030 xmlNodePtr node;
3031 if ((reader == NULL) || (reader->node == NULL))
3032 return(NULL);
3033 if (reader->curnode != NULL)
3034 node = reader->curnode;
3035 else
3036 node = reader->node;
3037 if (node->type == XML_NAMESPACE_DECL) {
3038 xmlNsPtr ns = (xmlNsPtr) node;
3039 if (ns->prefix == NULL)
3040 return(NULL);
3041 return(CONSTSTR(BAD_CAST "xmlns"));
3042 }
3043 if ((node->type != XML_ELEMENT_NODE) &&
3044 (node->type != XML_ATTRIBUTE_NODE))
3045 return(NULL);
3046 if ((node->ns != NULL) && (node->ns->prefix != NULL))
3047 return(CONSTSTR(node->ns->prefix));
3048 return(NULL);
3049}
3050
3051/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003052 * xmlTextReaderNamespaceUri:
3053 * @reader: the xmlTextReaderPtr used
3054 *
3055 * The URI defining the namespace associated with the node.
3056 *
3057 * Returns the namespace URI or NULL if not available
3058 */
3059xmlChar *
3060xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003061 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003062 if ((reader == NULL) || (reader->node == NULL))
3063 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003064 if (reader->curnode != NULL)
3065 node = reader->curnode;
3066 else
3067 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00003068 if (node->type == XML_NAMESPACE_DECL)
3069 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003070 if ((node->type != XML_ELEMENT_NODE) &&
3071 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003072 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003073 if (node->ns != NULL)
3074 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003075 return(NULL);
3076}
3077
3078/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003079 * xmlTextReaderConstNamespaceUri:
3080 * @reader: the xmlTextReaderPtr used
3081 *
3082 * The URI defining the namespace associated with the node.
3083 *
3084 * Returns the namespace URI or NULL if not available, the string
3085 * will be deallocated with the reader
3086 */
3087const xmlChar *
3088xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
3089 xmlNodePtr node;
3090 if ((reader == NULL) || (reader->node == NULL))
3091 return(NULL);
3092 if (reader->curnode != NULL)
3093 node = reader->curnode;
3094 else
3095 node = reader->node;
3096 if (node->type == XML_NAMESPACE_DECL)
3097 return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
3098 if ((node->type != XML_ELEMENT_NODE) &&
3099 (node->type != XML_ATTRIBUTE_NODE))
3100 return(NULL);
3101 if (node->ns != NULL)
3102 return(CONSTSTR(node->ns->href));
3103 return(NULL);
3104}
3105
3106/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003107 * xmlTextReaderBaseUri:
3108 * @reader: the xmlTextReaderPtr used
3109 *
3110 * The base URI of the node.
3111 *
3112 * Returns the base URI or NULL if not available
3113 */
3114xmlChar *
3115xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
3116 if ((reader == NULL) || (reader->node == NULL))
3117 return(NULL);
3118 return(xmlNodeGetBase(NULL, reader->node));
3119}
3120
3121/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003122 * xmlTextReaderConstBaseUri:
3123 * @reader: the xmlTextReaderPtr used
3124 *
3125 * The base URI of the node.
3126 *
3127 * Returns the base URI or NULL if not available, the string
3128 * will be deallocated with the reader
3129 */
3130const xmlChar *
3131xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
3132 xmlChar *tmp;
3133 const xmlChar *ret;
3134
3135 if ((reader == NULL) || (reader->node == NULL))
3136 return(NULL);
3137 tmp = xmlNodeGetBase(NULL, reader->node);
3138 if (tmp == NULL)
3139 return(NULL);
3140 ret = CONSTSTR(tmp);
3141 xmlFree(tmp);
3142 return(ret);
3143}
3144
3145/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003146 * xmlTextReaderDepth:
3147 * @reader: the xmlTextReaderPtr used
3148 *
3149 * The depth of the node in the tree.
3150 *
3151 * Returns the depth or -1 in case of error
3152 */
3153int
3154xmlTextReaderDepth(xmlTextReaderPtr reader) {
3155 if (reader == NULL)
3156 return(-1);
3157 if (reader->node == NULL)
3158 return(0);
3159
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003160 if (reader->curnode != NULL) {
3161 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
3162 (reader->curnode->type == XML_NAMESPACE_DECL))
3163 return(reader->depth + 1);
3164 return(reader->depth + 2);
3165 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003166 return(reader->depth);
3167}
3168
3169/**
3170 * xmlTextReaderHasAttributes:
3171 * @reader: the xmlTextReaderPtr used
3172 *
3173 * Whether the node has attributes.
3174 *
3175 * Returns 1 if true, 0 if false, and -1 in case or error
3176 */
3177int
3178xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003179 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003180 if (reader == NULL)
3181 return(-1);
3182 if (reader->node == NULL)
3183 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003184 if (reader->curnode != NULL)
3185 node = reader->curnode;
3186 else
3187 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003188
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003189 if ((node->type == XML_ELEMENT_NODE) &&
3190 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003191 return(1);
3192 /* TODO: handle the xmlDecl */
3193 return(0);
3194}
3195
3196/**
3197 * xmlTextReaderHasValue:
3198 * @reader: the xmlTextReaderPtr used
3199 *
3200 * Whether the node can have a text value.
3201 *
3202 * Returns 1 if true, 0 if false, and -1 in case or error
3203 */
3204int
3205xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003206 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003207 if (reader == NULL)
3208 return(-1);
3209 if (reader->node == NULL)
3210 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003211 if (reader->curnode != NULL)
3212 node = reader->curnode;
3213 else
3214 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003215
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003216 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003217 case XML_ATTRIBUTE_NODE:
3218 case XML_TEXT_NODE:
3219 case XML_CDATA_SECTION_NODE:
3220 case XML_PI_NODE:
3221 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00003222 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003223 return(1);
3224 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003225 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003226 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003227 return(0);
3228}
3229
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003230/**
3231 * xmlTextReaderValue:
3232 * @reader: the xmlTextReaderPtr used
3233 *
3234 * Provides the text value of the node if present
3235 *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003236 * Returns the string or NULL if not available. The result must be deallocated
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003237 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003238 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003239xmlChar *
3240xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003241 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003242 if (reader == NULL)
3243 return(NULL);
3244 if (reader->node == NULL)
3245 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003246 if (reader->curnode != NULL)
3247 node = reader->curnode;
3248 else
3249 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003250
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003251 switch (node->type) {
3252 case XML_NAMESPACE_DECL:
3253 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003254 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003255 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003256
3257 if (attr->parent != NULL)
3258 return (xmlNodeListGetString
3259 (attr->parent->doc, attr->children, 1));
3260 else
3261 return (xmlNodeListGetString(NULL, attr->children, 1));
3262 break;
3263 }
3264 case XML_TEXT_NODE:
3265 case XML_CDATA_SECTION_NODE:
3266 case XML_PI_NODE:
3267 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003268 if (node->content != NULL)
3269 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003270 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003271 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003272 }
3273 return(NULL);
3274}
3275
3276/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003277 * xmlTextReaderConstValue:
3278 * @reader: the xmlTextReaderPtr used
3279 *
3280 * Provides the text value of the node if present
3281 *
3282 * Returns the string or NULL if not available. The result will be
3283 * deallocated on the next Read() operation.
3284 */
3285const xmlChar *
3286xmlTextReaderConstValue(xmlTextReaderPtr reader) {
3287 xmlNodePtr node;
3288 if (reader == NULL)
3289 return(NULL);
3290 if (reader->node == NULL)
3291 return(NULL);
3292 if (reader->curnode != NULL)
3293 node = reader->curnode;
3294 else
3295 node = reader->node;
3296
3297 switch (node->type) {
3298 case XML_NAMESPACE_DECL:
3299 return(((xmlNsPtr) node)->href);
3300 case XML_ATTRIBUTE_NODE:{
3301 xmlAttrPtr attr = (xmlAttrPtr) node;
3302
3303 if ((attr->children != NULL) &&
3304 (attr->children->type == XML_TEXT_NODE) &&
3305 (attr->children->next == NULL))
3306 return(attr->children->content);
3307 else {
Daniel Veillard8165a6b2004-07-01 11:20:33 +00003308 if (reader->buffer == NULL)
3309 reader->buffer = xmlBufferCreateSize(100);
3310 if (reader->buffer == NULL) {
3311 xmlGenericError(xmlGenericErrorContext,
3312 "xmlTextReaderSetup : malloc failed\n");
3313 return (NULL);
3314 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003315 reader->buffer->use = 0;
3316 xmlNodeBufGetContent(reader->buffer, node);
3317 return(reader->buffer->content);
3318 }
3319 break;
3320 }
3321 case XML_TEXT_NODE:
3322 case XML_CDATA_SECTION_NODE:
3323 case XML_PI_NODE:
3324 case XML_COMMENT_NODE:
3325 return(node->content);
3326 default:
3327 break;
3328 }
3329 return(NULL);
3330}
3331
3332/**
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003333 * xmlTextReaderIsDefault:
3334 * @reader: the xmlTextReaderPtr used
3335 *
3336 * Whether an Attribute node was generated from the default value
3337 * defined in the DTD or schema.
3338 *
3339 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
3340 */
3341int
3342xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
3343 if (reader == NULL)
3344 return(-1);
3345 return(0);
3346}
3347
3348/**
3349 * xmlTextReaderQuoteChar:
3350 * @reader: the xmlTextReaderPtr used
3351 *
3352 * The quotation mark character used to enclose the value of an attribute.
3353 *
3354 * Returns " or ' and -1 in case of error
3355 */
3356int
3357xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
3358 if (reader == NULL)
3359 return(-1);
3360 /* TODO maybe lookup the attribute value for " first */
3361 return((int) '"');
3362}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003363
3364/**
3365 * xmlTextReaderXmlLang:
3366 * @reader: the xmlTextReaderPtr used
3367 *
3368 * The xml:lang scope within which the node resides.
3369 *
3370 * Returns the xml:lang value or NULL if none exists.
3371 */
3372xmlChar *
3373xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
3374 if (reader == NULL)
3375 return(NULL);
3376 if (reader->node == NULL)
3377 return(NULL);
3378 return(xmlNodeGetLang(reader->node));
3379}
3380
Daniel Veillard67df8092002-12-16 22:04:11 +00003381/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00003382 * xmlTextReaderConstXmlLang:
Daniel Veillarde72c5082003-09-19 12:44:05 +00003383 * @reader: the xmlTextReaderPtr used
3384 *
3385 * The xml:lang scope within which the node resides.
3386 *
3387 * Returns the xml:lang value or NULL if none exists.
3388 */
3389const xmlChar *
3390xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
3391 xmlChar *tmp;
3392 const xmlChar *ret;
3393
3394 if (reader == NULL)
3395 return(NULL);
3396 if (reader->node == NULL)
3397 return(NULL);
3398 tmp = xmlNodeGetLang(reader->node);
3399 if (tmp == NULL)
3400 return(NULL);
3401 ret = CONSTSTR(tmp);
3402 xmlFree(tmp);
3403 return(ret);
3404}
3405
3406/**
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00003407 * xmlTextReaderConstString:
3408 * @reader: the xmlTextReaderPtr used
3409 * @str: the string to intern.
3410 *
3411 * Get an interned string from the reader, allows for example to
3412 * speedup string name comparisons
3413 *
3414 * Returns an interned copy of the string or NULL in case of error. The
3415 * string will be deallocated with the reader.
3416 */
3417const xmlChar *
3418xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
3419 if (reader == NULL)
3420 return(NULL);
3421 return(CONSTSTR(str));
3422}
3423
3424/**
Daniel Veillard67df8092002-12-16 22:04:11 +00003425 * xmlTextReaderNormalization:
3426 * @reader: the xmlTextReaderPtr used
3427 *
3428 * The value indicating whether to normalize white space and attribute values.
3429 * Since attribute value and end of line normalizations are a MUST in the XML
3430 * specification only the value true is accepted. The broken bahaviour of
3431 * accepting out of range character entities like &#0; is of course not
3432 * supported either.
3433 *
3434 * Returns 1 or -1 in case of error.
3435 */
3436int
3437xmlTextReaderNormalization(xmlTextReaderPtr reader) {
3438 if (reader == NULL)
3439 return(-1);
3440 return(1);
3441}
3442
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003443/************************************************************************
3444 * *
3445 * Extensions to the base APIs *
3446 * *
3447 ************************************************************************/
3448
3449/**
3450 * xmlTextReaderSetParserProp:
3451 * @reader: the xmlTextReaderPtr used
3452 * @prop: the xmlParserProperties to set
3453 * @value: usually 0 or 1 to (de)activate it
3454 *
3455 * Change the parser processing behaviour by changing some of its internal
3456 * properties. Note that some properties can only be changed before any
3457 * read has been done.
3458 *
3459 * Returns 0 if the call was successful, or -1 in case of error
3460 */
3461int
3462xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
3463 xmlParserProperties p = (xmlParserProperties) prop;
3464 xmlParserCtxtPtr ctxt;
3465
3466 if ((reader == NULL) || (reader->ctxt == NULL))
3467 return(-1);
3468 ctxt = reader->ctxt;
3469
3470 switch (p) {
3471 case XML_PARSER_LOADDTD:
3472 if (value != 0) {
3473 if (ctxt->loadsubset == 0) {
3474 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3475 return(-1);
3476 ctxt->loadsubset = XML_DETECT_IDS;
3477 }
3478 } else {
3479 ctxt->loadsubset = 0;
3480 }
3481 return(0);
3482 case XML_PARSER_DEFAULTATTRS:
3483 if (value != 0) {
3484 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
3485 } else {
3486 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3487 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
3488 }
3489 return(0);
3490 case XML_PARSER_VALIDATE:
3491 if (value != 0) {
3492 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00003493 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003494 } else {
3495 ctxt->validate = 0;
3496 }
3497 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003498 case XML_PARSER_SUBST_ENTITIES:
3499 if (value != 0) {
3500 ctxt->replaceEntities = 1;
3501 } else {
3502 ctxt->replaceEntities = 0;
3503 }
3504 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003505 }
3506 return(-1);
3507}
3508
3509/**
3510 * xmlTextReaderGetParserProp:
3511 * @reader: the xmlTextReaderPtr used
3512 * @prop: the xmlParserProperties to get
3513 *
3514 * Read the parser internal property.
3515 *
3516 * Returns the value, usually 0 or 1, or -1 in case of error.
3517 */
3518int
3519xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
3520 xmlParserProperties p = (xmlParserProperties) prop;
3521 xmlParserCtxtPtr ctxt;
3522
3523 if ((reader == NULL) || (reader->ctxt == NULL))
3524 return(-1);
3525 ctxt = reader->ctxt;
3526
3527 switch (p) {
3528 case XML_PARSER_LOADDTD:
3529 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
3530 return(1);
3531 return(0);
3532 case XML_PARSER_DEFAULTATTRS:
3533 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3534 return(1);
3535 return(0);
3536 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00003537 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003538 case XML_PARSER_SUBST_ENTITIES:
3539 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003540 }
3541 return(-1);
3542}
3543
Daniel Veillarde18fc182002-12-28 22:56:33 +00003544/**
3545 * xmlTextReaderCurrentNode:
3546 * @reader: the xmlTextReaderPtr used
3547 *
3548 * Hacking interface allowing to get the xmlNodePtr correponding to the
3549 * current node being accessed by the xmlTextReader. This is dangerous
3550 * because the underlying node may be destroyed on the next Reads.
3551 *
3552 * Returns the xmlNodePtr or NULL in case of error.
3553 */
3554xmlNodePtr
3555xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
3556 if (reader == NULL)
3557 return(NULL);
3558
3559 if (reader->curnode != NULL)
3560 return(reader->curnode);
3561 return(reader->node);
3562}
3563
3564/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003565 * xmlTextReaderPreserve:
3566 * @reader: the xmlTextReaderPtr used
3567 *
Daniel Veillard4a14fb82004-06-14 19:58:20 +00003568 * This tells the XML Reader to preserve the current node.
3569 * The caller must also use xmlTextReaderCurrentDoc() to
3570 * keep an handle on the resulting document once parsing has finished
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003571 *
3572 * Returns the xmlNodePtr or NULL in case of error.
3573 */
3574xmlNodePtr
3575xmlTextReaderPreserve(xmlTextReaderPtr reader) {
3576 xmlNodePtr cur, parent;
3577
3578 if (reader == NULL)
3579 return(NULL);
3580
3581 if (reader->curnode != NULL)
3582 cur = reader->curnode;
3583 else
3584 cur = reader->node;
3585 if (cur == NULL)
3586 return(NULL);
Daniel Veillard1e906612003-12-05 14:57:46 +00003587
Daniel Veillard4a14fb82004-06-14 19:58:20 +00003588 if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) {
Daniel Veillard1e906612003-12-05 14:57:46 +00003589 cur->extra |= NODE_IS_PRESERVED;
3590 cur->extra |= NODE_IS_SPRESERVED;
3591 }
3592 reader->preserves++;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003593
3594 parent = cur->parent;;
3595 while (parent != NULL) {
Daniel Veillard1e906612003-12-05 14:57:46 +00003596 if (parent->type == XML_ELEMENT_NODE)
3597 parent->extra |= NODE_IS_PRESERVED;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003598 parent = parent->parent;
3599 }
3600 return(cur);
3601}
3602
Daniel Veillard1e906612003-12-05 14:57:46 +00003603#ifdef LIBXML_PATTERN_ENABLED
3604/**
3605 * xmlTextReaderPreservePattern:
3606 * @reader: the xmlTextReaderPtr used
3607 * @pattern: an XPath subset pattern
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003608 * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
Daniel Veillard1e906612003-12-05 14:57:46 +00003609 *
3610 * This tells the XML Reader to preserve all nodes matched by the
3611 * pattern. The caller must also use xmlTextReaderCurrentDoc() to
3612 * keep an handle on the resulting document once parsing has finished
3613 *
3614 * Returns a positive number in case of success and -1 in case of error
3615 */
3616int
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003617xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern,
3618 const xmlChar **namespaces)
3619{
Daniel Veillard1e906612003-12-05 14:57:46 +00003620 xmlPatternPtr comp;
3621
3622 if ((reader == NULL) || (pattern == NULL))
3623 return(-1);
3624
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003625 comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
Daniel Veillard1e906612003-12-05 14:57:46 +00003626 if (comp == NULL)
3627 return(-1);
3628
3629 if (reader->patternMax <= 0) {
3630 reader->patternMax = 4;
3631 reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax *
3632 sizeof(reader->patternTab[0]));
3633 if (reader->patternTab == NULL) {
3634 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3635 return (-1);
3636 }
3637 }
3638 if (reader->patternNr >= reader->patternMax) {
3639 xmlPatternPtr *tmp;
3640 reader->patternMax *= 2;
3641 tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab,
3642 reader->patternMax *
3643 sizeof(reader->patternTab[0]));
3644 if (tmp == NULL) {
3645 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3646 reader->patternMax /= 2;
3647 return (-1);
3648 }
3649 reader->patternTab = tmp;
3650 }
3651 reader->patternTab[reader->patternNr] = comp;
3652 return(reader->patternNr++);
3653}
3654#endif
3655
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003656/**
Daniel Veillarde18fc182002-12-28 22:56:33 +00003657 * xmlTextReaderCurrentDoc:
3658 * @reader: the xmlTextReaderPtr used
3659 *
3660 * Hacking interface allowing to get the xmlDocPtr correponding to the
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003661 * current document being accessed by the xmlTextReader.
3662 * NOTE: as a result of this call, the reader will not destroy the
3663 * associated XML document and calling xmlFreeDoc() on the result
3664 * is needed once the reader parsing has finished.
Daniel Veillarde18fc182002-12-28 22:56:33 +00003665 *
3666 * Returns the xmlDocPtr or NULL in case of error.
3667 */
3668xmlDocPtr
3669xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003670 if (reader == NULL)
3671 return(NULL);
3672 if (reader->doc != NULL)
3673 return(reader->doc);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003674 if ((reader == NULL) || (reader->ctxt == NULL) ||
3675 (reader->ctxt->myDoc == NULL))
Daniel Veillarde18fc182002-12-28 22:56:33 +00003676 return(NULL);
3677
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003678 reader->preserve = 1;
Daniel Veillarde18fc182002-12-28 22:56:33 +00003679 return(reader->ctxt->myDoc);
3680}
3681
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003682#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00003683/**
Daniel Veillard33300b42003-04-17 09:09:19 +00003684 * xmlTextReaderRelaxNGSetSchema:
3685 * @reader: the xmlTextReaderPtr used
3686 * @schema: a precompiled RelaxNG schema
3687 *
3688 * Use RelaxNG to validate the document as it is processed.
3689 * Activation is only possible before the first Read().
3690 * if @schema is NULL, then RelaxNG validation is desactivated.
3691 @ The @schema should not be freed until the reader is deallocated
3692 * or its use has been deactivated.
3693 *
3694 * Returns 0 in case the RelaxNG validation could be (des)activated and
3695 * -1 in case of error.
3696 */
3697int
3698xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
3699 if (schema == NULL) {
3700 if (reader->rngSchemas != NULL) {
3701 xmlRelaxNGFree(reader->rngSchemas);
3702 reader->rngSchemas = NULL;
3703 }
3704 if (reader->rngValidCtxt != NULL) {
3705 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3706 reader->rngValidCtxt = NULL;
3707 }
3708 return(0);
3709 }
3710 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3711 return(-1);
3712 if (reader->rngSchemas != NULL) {
3713 xmlRelaxNGFree(reader->rngSchemas);
3714 reader->rngSchemas = NULL;
3715 }
3716 if (reader->rngValidCtxt != NULL) {
3717 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3718 reader->rngValidCtxt = NULL;
3719 }
3720 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
3721 if (reader->rngValidCtxt == NULL)
3722 return(-1);
3723 if (reader->errorFunc != NULL) {
3724 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3725 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3726 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3727 reader->errorFuncArg);
3728 }
3729 reader->rngValidErrors = 0;
3730 reader->rngFullNode = NULL;
3731 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3732 return(0);
3733}
3734
3735/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00003736 * xmlTextReaderRelaxNGValidate:
3737 * @reader: the xmlTextReaderPtr used
3738 * @rng: the path to a RelaxNG schema or NULL
3739 *
3740 * Use RelaxNG to validate the document as it is processed.
3741 * Activation is only possible before the first Read().
3742 * if @rng is NULL, then RelaxNG validation is desactivated.
3743 *
3744 * Returns 0 in case the RelaxNG validation could be (des)activated and
3745 * -1 in case of error.
3746 */
3747int
3748xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
3749 xmlRelaxNGParserCtxtPtr ctxt;
3750
3751 if (reader == NULL)
3752 return(-1);
3753
3754 if (rng == NULL) {
3755 if (reader->rngSchemas != NULL) {
3756 xmlRelaxNGFree(reader->rngSchemas);
3757 reader->rngSchemas = NULL;
3758 }
3759 if (reader->rngValidCtxt != NULL) {
3760 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3761 reader->rngValidCtxt = NULL;
3762 }
3763 return(0);
3764 }
3765 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3766 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00003767 if (reader->rngSchemas != NULL) {
3768 xmlRelaxNGFree(reader->rngSchemas);
3769 reader->rngSchemas = NULL;
3770 }
3771 if (reader->rngValidCtxt != NULL) {
3772 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3773 reader->rngValidCtxt = NULL;
3774 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00003775 ctxt = xmlRelaxNGNewParserCtxt(rng);
3776 if (reader->errorFunc != NULL) {
3777 xmlRelaxNGSetParserErrors(ctxt,
3778 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
3779 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3780 reader->errorFuncArg);
3781 }
3782 reader->rngSchemas = xmlRelaxNGParse(ctxt);
3783 xmlRelaxNGFreeParserCtxt(ctxt);
3784 if (reader->rngSchemas == NULL)
3785 return(-1);
3786 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
3787 if (reader->rngValidCtxt == NULL)
3788 return(-1);
3789 if (reader->errorFunc != NULL) {
3790 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3791 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3792 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3793 reader->errorFuncArg);
3794 }
3795 reader->rngValidErrors = 0;
3796 reader->rngFullNode = NULL;
3797 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3798 return(0);
3799}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003800#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00003801
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003802/************************************************************************
3803 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00003804 * Error Handling Extensions *
3805 * *
3806 ************************************************************************/
3807
3808/* helper to build a xmlMalloc'ed string from a format and va_list */
3809static char *
3810xmlTextReaderBuildMessage(const char *msg, va_list ap) {
3811 int size;
3812 int chars;
3813 char *larger;
3814 char *str;
3815
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003816 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00003817 if (str == NULL) {
3818 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3819 return NULL;
3820 }
3821
3822 size = 150;
3823
3824 while (1) {
3825 chars = vsnprintf(str, size, msg, ap);
3826 if ((chars > -1) && (chars < size))
3827 break;
3828 if (chars > -1)
3829 size += chars + 1;
3830 else
3831 size += 100;
3832 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
3833 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3834 xmlFree(str);
3835 return NULL;
3836 }
3837 str = larger;
3838 }
3839
3840 return str;
3841}
3842
Daniel Veillard417be3a2003-01-20 21:26:34 +00003843/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003844 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003845 * @locator: the xmlTextReaderLocatorPtr used
3846 *
3847 * Obtain the line number for the given locator.
3848 *
3849 * Returns the line number or -1 in case of error.
3850 */
3851int
3852xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
3853 /* we know that locator is a xmlParserCtxtPtr */
3854 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3855 int ret = -1;
3856
3857 if (ctx->node != NULL) {
3858 ret = xmlGetLineNo(ctx->node);
3859 }
3860 else {
3861 /* inspired from error.c */
3862 xmlParserInputPtr input;
3863 input = ctx->input;
3864 if ((input->filename == NULL) && (ctx->inputNr > 1))
3865 input = ctx->inputTab[ctx->inputNr - 2];
3866 if (input != NULL) {
3867 ret = input->line;
3868 }
3869 else {
3870 ret = -1;
3871 }
3872 }
3873
3874 return ret;
3875}
3876
3877/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003878 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003879 * @locator: the xmlTextReaderLocatorPtr used
3880 *
3881 * Obtain the base URI for the given locator.
3882 *
3883 * Returns the base URI or NULL in case of error.
3884 */
3885xmlChar *
3886xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
3887 /* we know that locator is a xmlParserCtxtPtr */
3888 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3889 xmlChar *ret = NULL;
3890
3891 if (ctx->node != NULL) {
3892 ret = xmlNodeGetBase(NULL,ctx->node);
3893 }
3894 else {
3895 /* inspired from error.c */
3896 xmlParserInputPtr input;
3897 input = ctx->input;
3898 if ((input->filename == NULL) && (ctx->inputNr > 1))
3899 input = ctx->inputTab[ctx->inputNr - 2];
3900 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00003901 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00003902 }
3903 else {
3904 ret = NULL;
3905 }
3906 }
3907
3908 return ret;
3909}
3910
Daniel Veillard26f70262003-01-16 22:45:08 +00003911static void
William M. Brack899e64a2003-09-26 18:03:42 +00003912xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity, char *str) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003913 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
3914 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
3915
William M. Bracka3215c72004-07-31 16:24:01 +00003916 if (str != NULL) {
3917 if (reader->errorFunc)
Daniel Veillard26f70262003-01-16 22:45:08 +00003918 reader->errorFunc(reader->errorFuncArg,
3919 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003920 severity,
3921 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00003922 xmlFree(str);
3923 }
3924}
3925
3926static void
William M. Brack93d004f2004-02-03 00:14:10 +00003927xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error) {
3928 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt;
3929 xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private;
3930
3931 if (error && reader->sErrorFunc) {
3932 reader->sErrorFunc(reader->errorFuncArg,
3933 (xmlErrorPtr) error);
3934 }
3935}
3936
3937static void
Daniel Veillard26f70262003-01-16 22:45:08 +00003938xmlTextReaderError(void *ctxt, const char *msg, ...) {
3939 va_list ap;
3940
3941 va_start(ap,msg);
3942 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003943 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00003944 xmlTextReaderBuildMessage(msg,ap));
3945 va_end(ap);
3946
3947}
3948
3949static void
3950xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
3951 va_list ap;
3952
3953 va_start(ap,msg);
3954 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003955 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00003956 xmlTextReaderBuildMessage(msg,ap));
3957 va_end(ap);
3958}
3959
3960static void
3961xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
3962 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003963 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003964
Daniel Veillard417be3a2003-01-20 21:26:34 +00003965 if ((len > 1) && (msg[len - 2] != ':')) {
3966 /*
3967 * some callbacks only report locator information:
3968 * skip them (mimicking behaviour in error.c)
3969 */
3970 va_start(ap,msg);
3971 xmlTextReaderGenericError(ctxt,
3972 XML_PARSER_SEVERITY_VALIDITY_ERROR,
3973 xmlTextReaderBuildMessage(msg,ap));
3974 va_end(ap);
3975 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003976}
3977
3978static void
3979xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
3980 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003981 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003982
Daniel Veillard417be3a2003-01-20 21:26:34 +00003983 if ((len != 0) && (msg[len - 1] != ':')) {
3984 /*
3985 * some callbacks only report locator information:
3986 * skip them (mimicking behaviour in error.c)
3987 */
3988 va_start(ap,msg);
3989 xmlTextReaderGenericError(ctxt,
3990 XML_PARSER_SEVERITY_VALIDITY_WARNING,
3991 xmlTextReaderBuildMessage(msg,ap));
3992 va_end(ap);
3993 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003994}
3995
3996/**
3997 * xmlTextReaderSetErrorHandler:
3998 * @reader: the xmlTextReaderPtr used
3999 * @f: the callback function to call on error and warnings
4000 * @arg: a user argument to pass to the callback function
4001 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00004002 * Register a callback function that will be called on error and warnings.
4003 *
Daniel Veillard26f70262003-01-16 22:45:08 +00004004 * If @f is NULL, the default error and warning handlers are restored.
4005 */
4006void
4007xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
4008 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00004009 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00004010 if (f != NULL) {
4011 reader->ctxt->sax->error = xmlTextReaderError;
William M. Brack93d004f2004-02-03 00:14:10 +00004012 reader->ctxt->sax->serror = NULL;
Daniel Veillard26f70262003-01-16 22:45:08 +00004013 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
4014 reader->ctxt->sax->warning = xmlTextReaderWarning;
4015 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
4016 reader->errorFunc = f;
William M. Brack9f797ab2004-07-28 07:40:12 +00004017 reader->sErrorFunc = NULL;
Daniel Veillard26f70262003-01-16 22:45:08 +00004018 reader->errorFuncArg = arg;
4019 }
4020 else {
4021 /* restore defaults */
4022 reader->ctxt->sax->error = xmlParserError;
4023 reader->ctxt->vctxt.error = xmlParserValidityError;
4024 reader->ctxt->sax->warning = xmlParserWarning;
4025 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
4026 reader->errorFunc = NULL;
William M. Brack93d004f2004-02-03 00:14:10 +00004027 reader->sErrorFunc = NULL;
Daniel Veillard26f70262003-01-16 22:45:08 +00004028 reader->errorFuncArg = NULL;
4029 }
4030}
4031
Daniel Veillard417be3a2003-01-20 21:26:34 +00004032/**
William M. Brack93d004f2004-02-03 00:14:10 +00004033* xmlTextReaderSetStructuredErrorHandler:
4034 * @reader: the xmlTextReaderPtr used
4035 * @f: the callback function to call on error and warnings
4036 * @arg: a user argument to pass to the callback function
4037 *
4038 * Register a callback function that will be called on error and warnings.
4039 *
4040 * If @f is NULL, the default error and warning handlers are restored.
4041 */
4042void
4043xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader,
4044 xmlStructuredErrorFunc f,
4045 void *arg) {
4046 if (f != NULL) {
William M. Brack9f797ab2004-07-28 07:40:12 +00004047 reader->ctxt->sax->error = NULL;
William M. Brack93d004f2004-02-03 00:14:10 +00004048 reader->ctxt->sax->serror = xmlTextReaderStructuredError;
4049 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
4050 reader->ctxt->sax->warning = xmlTextReaderWarning;
4051 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
4052 reader->sErrorFunc = f;
4053 reader->errorFunc = NULL;
4054 reader->errorFuncArg = arg;
4055 }
4056 else {
4057 /* restore defaults */
4058 reader->ctxt->sax->error = xmlParserError;
4059 reader->ctxt->sax->serror = NULL;
4060 reader->ctxt->vctxt.error = xmlParserValidityError;
4061 reader->ctxt->sax->warning = xmlParserWarning;
4062 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
4063 reader->errorFunc = NULL;
4064 reader->sErrorFunc = NULL;
4065 reader->errorFuncArg = NULL;
4066 }
4067}
4068
4069/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00004070 * xmlTextReaderIsValid:
4071 * @reader: the xmlTextReaderPtr used
4072 *
4073 * Retrieve the validity status from the parser context
4074 *
4075 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
4076 */
4077int
4078xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00004079 if (reader == NULL) return(-1);
4080#ifdef LIBXML_SCHEMAS_ENABLED
4081 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
4082 return(reader->rngValidErrors == 0);
4083#endif
Daniel Veillard16ed5972003-11-20 18:22:31 +00004084 if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
Daniel Veillardf4e55762003-04-15 23:32:22 +00004085 return(reader->ctxt->valid);
4086 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00004087}
4088
4089/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00004090 * xmlTextReaderGetErrorHandler:
4091 * @reader: the xmlTextReaderPtr used
4092 * @f: the callback function or NULL is no callback has been registered
4093 * @arg: a user argument
4094 *
4095 * Retrieve the error callback function and user argument.
4096 */
Daniel Veillard26f70262003-01-16 22:45:08 +00004097void
4098xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
4099 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00004100 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00004101 *f = reader->errorFunc;
4102 *arg = reader->errorFuncArg;
4103}
4104
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004105
4106/************************************************************************
4107 * *
4108 * New set (2.6.0) of simpler and more flexible APIs *
4109 * *
4110 ************************************************************************/
4111
4112/**
4113 * xmlTextReaderSetup:
4114 * @reader: an XML reader
4115 * @URL: the base URL to use for the document
4116 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004117 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004118 * @reuse: keep the context for reuse
4119 *
4120 * Setup an XML reader with new options
4121 *
4122 * Returns 0 in case of success and -1 in case of error.
4123 */
4124static int
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004125xmlTextReaderSetup(xmlTextReaderPtr reader,
4126 xmlParserInputBufferPtr input, const char *URL,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004127 const char *encoding, int options)
4128{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004129 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004130 return (-1);
4131
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004132 reader->doc = NULL;
4133 reader->entNr = 0;
Daniel Veillarde74d2e12003-12-09 11:35:37 +00004134 reader->parserFlags = options;
Daniel Veillardc36965d2003-12-02 10:28:48 +00004135 reader->validate = XML_TEXTREADER_NOT_VALIDATE;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004136 if ((input != NULL) && (reader->input != NULL) &&
4137 (reader->allocs & XML_TEXTREADER_INPUT)) {
4138 xmlFreeParserInputBuffer(reader->input);
4139 reader->input = NULL;
4140 reader->allocs -= XML_TEXTREADER_INPUT;
4141 }
4142 if (input != NULL) {
4143 reader->input = input;
4144 reader->allocs |= XML_TEXTREADER_INPUT;
4145 }
4146 if (reader->buffer == NULL)
4147 reader->buffer = xmlBufferCreateSize(100);
4148 if (reader->buffer == NULL) {
4149 xmlGenericError(xmlGenericErrorContext,
4150 "xmlTextReaderSetup : malloc failed\n");
4151 return (-1);
4152 }
4153 if (reader->sax == NULL)
4154 reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
4155 if (reader->sax == NULL) {
4156 xmlGenericError(xmlGenericErrorContext,
4157 "xmlTextReaderSetup : malloc failed\n");
4158 return (-1);
4159 }
4160 xmlSAXVersion(reader->sax, 2);
4161 reader->startElement = reader->sax->startElement;
4162 reader->sax->startElement = xmlTextReaderStartElement;
4163 reader->endElement = reader->sax->endElement;
4164 reader->sax->endElement = xmlTextReaderEndElement;
4165#ifdef LIBXML_SAX1_ENABLED
4166 if (reader->sax->initialized == XML_SAX2_MAGIC) {
4167#endif /* LIBXML_SAX1_ENABLED */
4168 reader->startElementNs = reader->sax->startElementNs;
4169 reader->sax->startElementNs = xmlTextReaderStartElementNs;
4170 reader->endElementNs = reader->sax->endElementNs;
4171 reader->sax->endElementNs = xmlTextReaderEndElementNs;
4172#ifdef LIBXML_SAX1_ENABLED
4173 } else {
4174 reader->startElementNs = NULL;
4175 reader->endElementNs = NULL;
4176 }
4177#endif /* LIBXML_SAX1_ENABLED */
4178 reader->characters = reader->sax->characters;
4179 reader->sax->characters = xmlTextReaderCharacters;
4180 reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
4181 reader->cdataBlock = reader->sax->cdataBlock;
4182 reader->sax->cdataBlock = xmlTextReaderCDataBlock;
4183
4184 reader->mode = XML_TEXTREADER_MODE_INITIAL;
4185 reader->node = NULL;
4186 reader->curnode = NULL;
4187 if (input != NULL) {
4188 if (reader->input->buffer->use < 4) {
4189 xmlParserInputBufferRead(input, 4);
4190 }
4191 if (reader->ctxt == NULL) {
4192 if (reader->input->buffer->use >= 4) {
4193 reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
4194 (const char *) reader->input->buffer->content, 4, URL);
4195 reader->base = 0;
4196 reader->cur = 4;
4197 } else {
4198 reader->ctxt =
4199 xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
4200 reader->base = 0;
4201 reader->cur = 0;
4202 }
4203 } else {
4204 xmlParserInputPtr inputStream;
4205 xmlParserInputBufferPtr buf;
4206 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
4207
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004208 xmlCtxtReset(reader->ctxt);
4209 buf = xmlAllocParserInputBuffer(enc);
4210 if (buf == NULL) return(-1);
4211 inputStream = xmlNewInputStream(reader->ctxt);
4212 if (inputStream == NULL) {
4213 xmlFreeParserInputBuffer(buf);
4214 return(-1);
4215 }
4216
4217 if (URL == NULL)
4218 inputStream->filename = NULL;
4219 else
4220 inputStream->filename = (char *)
4221 xmlCanonicPath((const xmlChar *) URL);
4222 inputStream->buf = buf;
4223 inputStream->base = inputStream->buf->buffer->content;
4224 inputStream->cur = inputStream->buf->buffer->content;
4225 inputStream->end =
4226 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
4227
4228 inputPush(reader->ctxt, inputStream);
4229 reader->cur = 0;
4230 }
4231 if (reader->ctxt == NULL) {
4232 xmlGenericError(xmlGenericErrorContext,
4233 "xmlTextReaderSetup : malloc failed\n");
4234 return (-1);
4235 }
4236 }
4237 if (reader->dict != NULL) {
4238 if (reader->ctxt->dict != NULL) {
4239 if (reader->dict != reader->ctxt->dict) {
4240 xmlDictFree(reader->dict);
4241 reader->dict = reader->ctxt->dict;
4242 }
4243 } else {
4244 reader->ctxt->dict = reader->dict;
4245 }
4246 } else {
4247 if (reader->ctxt->dict == NULL)
4248 reader->ctxt->dict = xmlDictCreate();
4249 reader->dict = reader->ctxt->dict;
4250 }
4251 reader->ctxt->_private = reader;
4252 reader->ctxt->linenumbers = 1;
4253 reader->ctxt->dictNames = 1;
4254 /*
4255 * use the parser dictionnary to allocate all elements and attributes names
4256 */
4257 reader->ctxt->docdict = 1;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00004258 reader->ctxt->parseMode = XML_PARSE_READER;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004259
Daniel Veillard7899c5c2003-11-03 12:31:38 +00004260#ifdef LIBXML_XINCLUDE_ENABLED
4261 if (reader->xincctxt != NULL) {
4262 xmlXIncludeFreeContext(reader->xincctxt);
4263 reader->xincctxt = NULL;
4264 }
4265 if (options & XML_PARSE_XINCLUDE) {
4266 reader->xinclude = 1;
4267 reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
4268 options -= XML_PARSE_XINCLUDE;
4269 } else
4270 reader->xinclude = 0;
4271 reader->in_xinclude = 0;
4272#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00004273#ifdef LIBXML_PATTERN_ENABLED
4274 if (reader->patternTab == NULL) {
4275 reader->patternNr = 0;
4276 reader->patternMax = 0;
4277 }
4278 while (reader->patternNr > 0) {
4279 reader->patternNr--;
4280 if (reader->patternTab[reader->patternNr] != NULL) {
4281 xmlFreePattern(reader->patternTab[reader->patternNr]);
4282 reader->patternTab[reader->patternNr] = NULL;
4283 }
4284 }
4285#endif
4286
Daniel Veillardc36965d2003-12-02 10:28:48 +00004287 if (options & XML_PARSE_DTDVALID)
4288 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
4289
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004290 xmlCtxtUseOptions(reader->ctxt, options);
4291 if (encoding != NULL) {
4292 xmlCharEncodingHandlerPtr hdlr;
4293
4294 hdlr = xmlFindCharEncodingHandler(encoding);
4295 if (hdlr != NULL)
4296 xmlSwitchToEncoding(reader->ctxt, hdlr);
4297 }
4298 if ((URL != NULL) && (reader->ctxt->input != NULL) &&
4299 (reader->ctxt->input->filename == NULL))
4300 reader->ctxt->input->filename = (char *)
4301 xmlStrdup((const xmlChar *) URL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004302
4303 reader->doc = NULL;
4304
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004305 return (0);
4306}
4307
4308/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004309 * xmlReaderWalker:
4310 * @doc: a preparsed document
4311 *
4312 * Create an xmltextReader for a preparsed document.
4313 *
4314 * Returns the new reader or NULL in case of error.
4315 */
4316xmlTextReaderPtr
4317xmlReaderWalker(xmlDocPtr doc)
4318{
4319 xmlTextReaderPtr ret;
4320
4321 if (doc == NULL)
4322 return(NULL);
4323
4324 ret = xmlMalloc(sizeof(xmlTextReader));
4325 if (ret == NULL) {
4326 xmlGenericError(xmlGenericErrorContext,
4327 "xmlNewTextReader : malloc failed\n");
4328 return(NULL);
4329 }
4330 memset(ret, 0, sizeof(xmlTextReader));
4331 ret->entNr = 0;
4332 ret->input = NULL;
4333 ret->mode = XML_TEXTREADER_MODE_INITIAL;
4334 ret->node = NULL;
4335 ret->curnode = NULL;
4336 ret->base = 0;
4337 ret->cur = 0;
4338 ret->allocs = XML_TEXTREADER_CTXT;
4339 ret->doc = doc;
4340 ret->state = XML_TEXTREADER_START;
4341 ret->dict = xmlDictCreate();
4342 return(ret);
4343}
4344
4345/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004346 * xmlReaderForDoc:
4347 * @cur: a pointer to a zero terminated string
4348 * @URL: the base URL to use for the document
4349 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004350 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004351 *
4352 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004353 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004354 *
4355 * Returns the new reader or NULL in case of error.
4356 */
4357xmlTextReaderPtr
4358xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
4359 int options)
4360{
4361 int len;
4362
4363 if (cur == NULL)
4364 return (NULL);
4365 len = xmlStrlen(cur);
4366
4367 return (xmlReaderForMemory
4368 ((const char *) cur, len, URL, encoding, options));
4369}
4370
4371/**
4372 * xmlReaderForFile:
4373 * @filename: a file or URL
4374 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004375 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004376 *
4377 * parse an XML file from the filesystem or the network.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004378 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004379 *
4380 * Returns the new reader or NULL in case of error.
4381 */
4382xmlTextReaderPtr
4383xmlReaderForFile(const char *filename, const char *encoding, int options)
4384{
4385 xmlTextReaderPtr reader;
4386
4387 reader = xmlNewTextReaderFilename(filename);
4388 if (reader == NULL)
4389 return (NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004390 xmlTextReaderSetup(reader, NULL, NULL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004391 return (reader);
4392}
4393
4394/**
4395 * xmlReaderForMemory:
4396 * @buffer: a pointer to a char array
4397 * @size: the size of the array
4398 * @URL: the base URL to use for the document
4399 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004400 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004401 *
4402 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004403 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004404 *
4405 * Returns the new reader or NULL in case of error.
4406 */
4407xmlTextReaderPtr
4408xmlReaderForMemory(const char *buffer, int size, const char *URL,
4409 const char *encoding, int options)
4410{
4411 xmlTextReaderPtr reader;
4412 xmlParserInputBufferPtr buf;
4413
Daniel Veillard21924522004-02-19 16:37:07 +00004414 buf = xmlParserInputBufferCreateStatic(buffer, size,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004415 XML_CHAR_ENCODING_NONE);
4416 if (buf == NULL) {
4417 return (NULL);
4418 }
4419 reader = xmlNewTextReader(buf, URL);
4420 if (reader == NULL) {
4421 xmlFreeParserInputBuffer(buf);
4422 return (NULL);
4423 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004424 reader->allocs |= XML_TEXTREADER_INPUT;
4425 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004426 return (reader);
4427}
4428
4429/**
4430 * xmlReaderForFd:
4431 * @fd: an open file descriptor
4432 * @URL: the base URL to use for the document
4433 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004434 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004435 *
4436 * Create an xmltextReader for an XML from a file descriptor.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004437 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004438 * NOTE that the file descriptor will not be closed when the
4439 * reader is closed or reset.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004440 *
4441 * Returns the new reader or NULL in case of error.
4442 */
4443xmlTextReaderPtr
4444xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
4445{
4446 xmlTextReaderPtr reader;
4447 xmlParserInputBufferPtr input;
4448
4449 if (fd < 0)
4450 return (NULL);
4451
4452 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4453 if (input == NULL)
4454 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004455 input->closecallback = NULL;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004456 reader = xmlNewTextReader(input, URL);
4457 if (reader == NULL) {
4458 xmlFreeParserInputBuffer(input);
4459 return (NULL);
4460 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004461 reader->allocs |= XML_TEXTREADER_INPUT;
4462 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004463 return (reader);
4464}
4465
4466/**
4467 * xmlReaderForIO:
4468 * @ioread: an I/O read function
4469 * @ioclose: an I/O close function
4470 * @ioctx: an I/O handler
4471 * @URL: the base URL to use for the document
4472 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004473 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004474 *
4475 * Create an xmltextReader for an XML document from I/O functions and source.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004476 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004477 *
4478 * Returns the new reader or NULL in case of error.
4479 */
4480xmlTextReaderPtr
4481xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
4482 void *ioctx, const char *URL, const char *encoding,
4483 int options)
4484{
4485 xmlTextReaderPtr reader;
4486 xmlParserInputBufferPtr input;
4487
4488 if (ioread == NULL)
4489 return (NULL);
4490
4491 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4492 XML_CHAR_ENCODING_NONE);
4493 if (input == NULL)
4494 return (NULL);
4495 reader = xmlNewTextReader(input, URL);
4496 if (reader == NULL) {
4497 xmlFreeParserInputBuffer(input);
4498 return (NULL);
4499 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004500 reader->allocs |= XML_TEXTREADER_INPUT;
4501 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004502 return (reader);
4503}
4504
4505/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004506 * xmlReaderNewWalker:
4507 * @reader: an XML reader
4508 * @doc: a preparsed document
4509 *
4510 * Setup an xmltextReader to parse a preparsed XML document.
4511 * This reuses the existing @reader xmlTextReader.
4512 *
4513 * Returns 0 in case of success and -1 in case of error
4514 */
4515int
4516xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
4517{
4518 if (doc == NULL)
4519 return (-1);
4520 if (reader == NULL)
4521 return (-1);
4522
4523 if (reader->ctxt != NULL) {
4524 xmlCtxtReset(reader->ctxt);
4525 }
4526
4527 reader->entNr = 0;
4528 reader->input = NULL;
4529 reader->mode = XML_TEXTREADER_MODE_INITIAL;
4530 reader->node = NULL;
4531 reader->curnode = NULL;
4532 reader->base = 0;
4533 reader->cur = 0;
4534 reader->allocs = XML_TEXTREADER_CTXT;
4535 reader->doc = doc;
4536 reader->state = XML_TEXTREADER_START;
4537 if (reader->dict == NULL) {
4538 if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
4539 reader->dict = reader->ctxt->dict;
4540 else
4541 reader->dict = xmlDictCreate();
4542 }
4543 return(0);
4544}
4545
4546/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004547 * xmlReaderNewDoc:
4548 * @reader: an XML reader
4549 * @cur: a pointer to a zero terminated string
4550 * @URL: the base URL to use for the document
4551 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004552 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004553 *
4554 * Setup an xmltextReader to parse an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004555 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004556 * This reuses the existing @reader xmlTextReader.
4557 *
4558 * Returns 0 in case of success and -1 in case of error
4559 */
4560int
4561xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
4562 const char *URL, const char *encoding, int options)
4563{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004564
4565 int len;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004566
4567 if (cur == NULL)
4568 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004569 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004570 return (-1);
4571
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004572 len = xmlStrlen(cur);
4573 return (xmlReaderNewMemory(reader, (const char *)cur, len,
4574 URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004575}
4576
4577/**
4578 * xmlReaderNewFile:
4579 * @reader: an XML reader
4580 * @filename: a file or URL
4581 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004582 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004583 *
4584 * parse an XML file from the filesystem or the network.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004585 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004586 * This reuses the existing @reader xmlTextReader.
4587 *
4588 * Returns 0 in case of success and -1 in case of error
4589 */
4590int
4591xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
4592 const char *encoding, int options)
4593{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004594 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004595
4596 if (filename == NULL)
4597 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004598 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004599 return (-1);
4600
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004601 input =
4602 xmlParserInputBufferCreateFilename(filename,
4603 XML_CHAR_ENCODING_NONE);
4604 if (input == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004605 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004606 return (xmlTextReaderSetup(reader, input, filename, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004607}
4608
4609/**
4610 * xmlReaderNewMemory:
4611 * @reader: an XML reader
4612 * @buffer: a pointer to a char array
4613 * @size: the size of the array
4614 * @URL: the base URL to use for the document
4615 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004616 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004617 *
4618 * Setup an xmltextReader to parse an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004619 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004620 * This reuses the existing @reader xmlTextReader.
4621 *
4622 * Returns 0 in case of success and -1 in case of error
4623 */
4624int
4625xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
4626 const char *URL, const char *encoding, int options)
4627{
4628 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004629
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004630 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004631 return (-1);
4632 if (buffer == NULL)
4633 return (-1);
4634
Daniel Veillard21924522004-02-19 16:37:07 +00004635 input = xmlParserInputBufferCreateStatic(buffer, size,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004636 XML_CHAR_ENCODING_NONE);
4637 if (input == NULL) {
4638 return (-1);
4639 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004640 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004641}
4642
4643/**
4644 * xmlReaderNewFd:
4645 * @reader: an XML reader
4646 * @fd: an open file descriptor
4647 * @URL: the base URL to use for the document
4648 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004649 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004650 *
4651 * Setup an xmltextReader to parse an XML from a file descriptor.
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004652 * NOTE that the file descriptor will not be closed when the
4653 * reader is closed or reset.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004654 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004655 * This reuses the existing @reader xmlTextReader.
4656 *
4657 * Returns 0 in case of success and -1 in case of error
4658 */
4659int
4660xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
4661 const char *URL, const char *encoding, int options)
4662{
4663 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004664
4665 if (fd < 0)
4666 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004667 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004668 return (-1);
4669
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004670 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4671 if (input == NULL)
4672 return (-1);
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004673 input->closecallback = NULL;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004674 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004675}
4676
4677/**
4678 * xmlReaderNewIO:
4679 * @reader: an XML reader
4680 * @ioread: an I/O read function
4681 * @ioclose: an I/O close function
4682 * @ioctx: an I/O handler
4683 * @URL: the base URL to use for the document
4684 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004685 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004686 *
4687 * Setup an xmltextReader to parse an XML document from I/O functions
4688 * and source.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004689 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004690 * This reuses the existing @reader xmlTextReader.
4691 *
4692 * Returns 0 in case of success and -1 in case of error
4693 */
4694int
4695xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
4696 xmlInputCloseCallback ioclose, void *ioctx,
4697 const char *URL, const char *encoding, int options)
4698{
4699 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004700
4701 if (ioread == NULL)
4702 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004703 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004704 return (-1);
4705
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004706 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4707 XML_CHAR_ENCODING_NONE);
4708 if (input == NULL)
4709 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004710 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004711}
Daniel Veillard26f70262003-01-16 22:45:08 +00004712/************************************************************************
4713 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004714 * Utilities *
4715 * *
4716 ************************************************************************/
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004717#ifdef NOT_USED_YET
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004718/**
4719 * xmlBase64Decode:
4720 * @in: the input buffer
4721 * @inlen: the size of the input (in), the size read from it (out)
4722 * @to: the output buffer
4723 * @tolen: the size of the output (in), the size written to (out)
4724 *
4725 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00004726 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004727 *
4728 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
4729 * 2 if there wasn't enough space on the output or -1 in case of error.
4730 */
4731static int
4732xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
4733 unsigned char *to, unsigned long *tolen) {
4734 unsigned long incur; /* current index in in[] */
4735 unsigned long inblk; /* last block index in in[] */
4736 unsigned long outcur; /* current index in out[] */
4737 unsigned long inmax; /* size of in[] */
4738 unsigned long outmax; /* size of out[] */
4739 unsigned char cur; /* the current value read from in[] */
Daniel Veillardc127adc2003-07-23 15:07:08 +00004740 unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004741 int nbintmp; /* number of byte in intmp[] */
4742 int is_ignore; /* cur should be ignored */
4743 int is_end = 0; /* the end of the base64 was found */
4744 int retval = 1;
4745 int i;
4746
4747 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
4748 return(-1);
4749
4750 incur = 0;
4751 inblk = 0;
4752 outcur = 0;
4753 inmax = *inlen;
4754 outmax = *tolen;
4755 nbintmp = 0;
4756
4757 while (1) {
4758 if (incur >= inmax)
4759 break;
4760 cur = in[incur++];
4761 is_ignore = 0;
4762 if ((cur >= 'A') && (cur <= 'Z'))
4763 cur = cur - 'A';
4764 else if ((cur >= 'a') && (cur <= 'z'))
4765 cur = cur - 'a' + 26;
4766 else if ((cur >= '0') && (cur <= '9'))
4767 cur = cur - '0' + 52;
4768 else if (cur == '+')
4769 cur = 62;
4770 else if (cur == '/')
4771 cur = 63;
4772 else if (cur == '.')
4773 cur = 0;
4774 else if (cur == '=') /*no op , end of the base64 stream */
4775 is_end = 1;
4776 else {
4777 is_ignore = 1;
4778 if (nbintmp == 0)
4779 inblk = incur;
4780 }
4781
4782 if (!is_ignore) {
4783 int nbouttmp = 3;
4784 int is_break = 0;
4785
4786 if (is_end) {
4787 if (nbintmp == 0)
4788 break;
4789 if ((nbintmp == 1) || (nbintmp == 2))
4790 nbouttmp = 1;
4791 else
4792 nbouttmp = 2;
4793 nbintmp = 3;
4794 is_break = 1;
4795 }
4796 intmp[nbintmp++] = cur;
4797 /*
4798 * if intmp is full, push the 4byte sequence as a 3 byte
4799 * sequence out
4800 */
4801 if (nbintmp == 4) {
4802 nbintmp = 0;
4803 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
4804 outtmp[1] =
4805 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
4806 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
4807 if (outcur + 3 >= outmax) {
4808 retval = 2;
4809 break;
4810 }
4811
4812 for (i = 0; i < nbouttmp; i++)
4813 to[outcur++] = outtmp[i];
4814 inblk = incur;
4815 }
4816
4817 if (is_break) {
4818 retval = 0;
4819 break;
4820 }
4821 }
4822 }
4823
4824 *tolen = outcur;
4825 *inlen = inblk;
4826 return (retval);
4827}
4828
4829/*
4830 * Test routine for the xmlBase64Decode function
4831 */
4832#if 0
4833int main(int argc, char **argv) {
4834 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
4835 char output[100];
4836 char output2[100];
4837 char output3[100];
4838 unsigned long inlen = strlen(input);
4839 unsigned long outlen = 100;
4840 int ret;
4841 unsigned long cons, tmp, tmp2, prod;
4842
4843 /*
4844 * Direct
4845 */
4846 ret = xmlBase64Decode(input, &inlen, output, &outlen);
4847
4848 output[outlen] = 0;
4849 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
4850
4851 /*
4852 * output chunking
4853 */
4854 cons = 0;
4855 prod = 0;
4856 while (cons < inlen) {
4857 tmp = 5;
4858 tmp2 = inlen - cons;
4859
4860 printf("%ld %ld\n", cons, prod);
4861 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
4862 cons += tmp2;
4863 prod += tmp;
4864 printf("%ld %ld\n", cons, prod);
4865 }
4866 output2[outlen] = 0;
4867 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
4868
4869 /*
4870 * input chunking
4871 */
4872 cons = 0;
4873 prod = 0;
4874 while (cons < inlen) {
4875 tmp = 100 - prod;
4876 tmp2 = inlen - cons;
4877 if (tmp2 > 5)
4878 tmp2 = 5;
4879
4880 printf("%ld %ld\n", cons, prod);
4881 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
4882 cons += tmp2;
4883 prod += tmp;
4884 printf("%ld %ld\n", cons, prod);
4885 }
4886 output3[outlen] = 0;
4887 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
4888 return(0);
4889
4890}
4891#endif
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004892#endif /* NOT_USED_YET */
Daniel Veillard81273902003-09-30 00:43:48 +00004893#endif /* LIBXML_READER_ENABLED */