blob: 749cb31b2fb5628c3cfae4fbe3c7b60adc4126f0 [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 Veillard7704fb12003-01-03 16:19:51 +000015 * - provide an API to preserve part of the tree
16 * - Streaming XInclude support
Daniel Veillard067bae52003-01-05 01:27:54 +000017 * - XML Schemas validation
Daniel Veillard7704fb12003-01-03 16:19:51 +000018 * - setting(s) for NoBlanks
19 * - performances and tuning ...
20 */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000021#define IN_LIBXML
22#include "libxml.h"
23
Daniel Veillard81273902003-09-30 00:43:48 +000024#ifdef LIBXML_READER_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +000025#include <string.h> /* for memset() only ! */
Daniel Veillard26f70262003-01-16 22:45:08 +000026#include <stdarg.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000027
28#ifdef HAVE_CTYPE_H
29#include <ctype.h>
30#endif
31#ifdef HAVE_STDLIB_H
32#include <stdlib.h>
33#endif
34
35#include <libxml/xmlmemory.h>
36#include <libxml/xmlIO.h>
37#include <libxml/xmlreader.h>
Daniel Veillardfc8dc352003-10-18 09:07:46 +000038#include <libxml/parserInternals.h>
Daniel Veillardf4e55762003-04-15 23:32:22 +000039#include <libxml/relaxng.h>
Daniel Veillard198c1bf2003-10-20 17:07:41 +000040#include <libxml/uri.h>
Daniel Veillard7899c5c2003-11-03 12:31:38 +000041#ifdef LIBXML_XINCLUDE_ENABLED
42#include <libxml/xinclude.h>
43#endif
Daniel Veillard1e906612003-12-05 14:57:46 +000044#ifdef LIBXML_PATTERN_ENABLED
45#include <libxml/pattern.h>
46#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +000047
48/* #define DEBUG_CALLBACKS */
49/* #define DEBUG_READER */
50
51/**
52 * TODO:
53 *
54 * macro to flag unimplemented blocks
55 */
56#define TODO \
57 xmlGenericError(xmlGenericErrorContext, \
58 "Unimplemented block at %s:%d\n", \
59 __FILE__, __LINE__);
60
61#ifdef DEBUG_READER
62#define DUMP_READER xmlTextReaderDebug(reader);
63#else
64#define DUMP_READER
65#endif
66
Daniel Veillarda880b122003-04-21 21:36:41 +000067#define CHUNK_SIZE 512
Daniel Veillarde1ca5032002-12-09 14:13:43 +000068/************************************************************************
69 * *
70 * The parser: maps the Text Reader API on top of the existing *
71 * parsing routines building a tree *
72 * *
73 ************************************************************************/
74
75#define XML_TEXTREADER_INPUT 1
76#define XML_TEXTREADER_CTXT 2
77
78typedef enum {
Daniel Veillard67df8092002-12-16 22:04:11 +000079 XML_TEXTREADER_MODE_INITIAL = 0,
80 XML_TEXTREADER_MODE_INTERACTIVE = 1,
81 XML_TEXTREADER_MODE_ERROR = 2,
82 XML_TEXTREADER_MODE_EOF =3,
83 XML_TEXTREADER_MODE_CLOSED = 4,
84 XML_TEXTREADER_MODE_READING = 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000085} xmlTextReaderMode;
86
87typedef enum {
88 XML_TEXTREADER_NONE = -1,
89 XML_TEXTREADER_START= 0,
90 XML_TEXTREADER_ELEMENT= 1,
91 XML_TEXTREADER_END= 2,
92 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000093 XML_TEXTREADER_BACKTRACK= 4,
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000094 XML_TEXTREADER_DONE= 5,
95 XML_TEXTREADER_ERROR= 6
Daniel Veillarde1ca5032002-12-09 14:13:43 +000096} xmlTextReaderState;
97
Daniel Veillardf4e55762003-04-15 23:32:22 +000098typedef enum {
99 XML_TEXTREADER_NOT_VALIDATE = 0,
100 XML_TEXTREADER_VALIDATE_DTD = 1,
101 XML_TEXTREADER_VALIDATE_RNG = 2
102} xmlTextReaderValidate;
103
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000104struct _xmlTextReader {
105 int mode; /* the parsing mode */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000106 xmlDocPtr doc; /* when walking an existing doc */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000107 xmlTextReaderValidate validate;/* is there any validation */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000108 int allocs; /* what structure were deallocated */
109 xmlTextReaderState state;
110 xmlParserCtxtPtr ctxt; /* the parser context */
111 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
112 xmlParserInputBufferPtr input; /* the input */
113 startElementSAXFunc startElement;/* initial SAX callbacks */
114 endElementSAXFunc endElement; /* idem */
Daniel Veillard07cb8222003-09-10 10:51:05 +0000115 startElementNsSAX2Func startElementNs;/* idem */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000116 endElementNsSAX2Func endElementNs; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000117 charactersSAXFunc characters;
118 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000119 unsigned int base; /* base of the segment in the input */
120 unsigned int cur; /* current position in the input */
121 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000122 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000123 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000124 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000125 int preserve;/* preserve the resulting document */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000126 xmlBufferPtr buffer; /* used to return const xmlChar * */
127 xmlDictPtr dict; /* the context dictionnary */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000128
129 /* entity stack when traversing entities content */
130 xmlNodePtr ent; /* Current Entity Ref Node */
131 int entNr; /* Depth of the entities stack */
132 int entMax; /* Max depth of the entities stack */
133 xmlNodePtr *entTab; /* array of entities */
Daniel Veillard26f70262003-01-16 22:45:08 +0000134
135 /* error handling */
136 xmlTextReaderErrorFunc errorFunc; /* callback function */
137 void *errorFuncArg; /* callback function user argument */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000138
139#ifdef LIBXML_SCHEMAS_ENABLED
140 /* Handling of RelaxNG validation */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000141 xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
142 xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
143 int rngValidErrors;/* The number of errors detected */
144 xmlNodePtr rngFullNode; /* the node if RNG not progressive */
145#endif
146#ifdef LIBXML_XINCLUDE_ENABLED
147 /* Handling of XInclude processing */
148 int xinclude; /* is xinclude asked for */
149 const xmlChar * xinclude_name; /* the xinclude name from dict */
150 xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */
151 int in_xinclude; /* counts for xinclude */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000152#endif
Daniel Veillard1e906612003-12-05 14:57:46 +0000153#ifdef LIBXML_PATTERN_ENABLED
154 int patternNr; /* number of preserve patterns */
155 int patternMax; /* max preserve patterns */
156 xmlPatternPtr *patternTab; /* array of preserve patterns */
157#endif
158 int preserves; /* level of preserves */
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000159 int parserFlags; /* the set of options set */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000160};
161
Daniel Veillarde8039df2003-10-27 11:25:13 +0000162#define NODE_IS_EMPTY 0x1
163#define NODE_IS_PRESERVED 0x2
Daniel Veillard1e906612003-12-05 14:57:46 +0000164#define NODE_IS_SPRESERVED 0x4
Daniel Veillard067bae52003-01-05 01:27:54 +0000165
Daniel Veillarde72c5082003-09-19 12:44:05 +0000166/**
167 * CONSTSTR:
168 *
169 * Macro used to return an interned string
170 */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000171#define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
172#define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
173
174static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
175static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
Daniel Veillarde72c5082003-09-19 12:44:05 +0000176
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000177/************************************************************************
178 * *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000179 * Our own version of the freeing routines as we recycle nodes *
180 * *
181 ************************************************************************/
182/**
183 * DICT_FREE:
184 * @str: a string
185 *
186 * Free a string if it is not owned by the "dict" dictionnary in the
187 * current scope
188 */
189#define DICT_FREE(str) \
190 if ((str) && ((!dict) || \
191 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
192 xmlFree((char *)(str));
193
194static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
195static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
196
197/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000198 * xmlFreeID:
199 * @not: A id
200 *
201 * Deallocate the memory used by an id definition
202 */
203static void
204xmlFreeID(xmlIDPtr id) {
205 xmlDictPtr dict = NULL;
206
207 if (id == NULL) return;
208
209 if (id->doc != NULL)
210 dict = id->doc->dict;
211
212 if (id->value != NULL)
213 DICT_FREE(id->value)
214 xmlFree(id);
215}
216
217/**
218 * xmlTextReaderRemoveID:
219 * @doc: the document
220 * @attr: the attribute
221 *
222 * Remove the given attribute from the ID table maintained internally.
223 *
224 * Returns -1 if the lookup failed and 0 otherwise
225 */
226static int
227xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
228 xmlIDTablePtr table;
229 xmlIDPtr id;
230 xmlChar *ID;
231
232 if (doc == NULL) return(-1);
233 if (attr == NULL) return(-1);
234 table = (xmlIDTablePtr) doc->ids;
235 if (table == NULL)
236 return(-1);
237
238 if (attr == NULL)
239 return(-1);
240 ID = xmlNodeListGetString(doc, attr->children, 1);
241 if (ID == NULL)
242 return(-1);
243 id = xmlHashLookup(table, ID);
244 xmlFree(ID);
245 if (id == NULL || id->attr != attr) {
246 return(-1);
247 }
248 id->name = attr->name;
249 id->attr = NULL;
250 return(0);
251}
252
253/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000254 * xmlTextReaderFreeProp:
255 * @reader: the xmlTextReaderPtr used
256 * @cur: the node
257 *
258 * Free a node.
259 */
260static void
261xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
262 xmlDictPtr dict;
263
264 dict = reader->ctxt->dict;
265 if (cur == NULL) return;
266
267 /* Check for ID removal -> leading to invalid references ! */
268 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
269 ((cur->parent->doc->intSubset != NULL) ||
270 (cur->parent->doc->extSubset != NULL))) {
271 if (xmlIsID(cur->parent->doc, cur->parent, cur))
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000272 xmlTextReaderRemoveID(cur->parent->doc, cur);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000273 }
274 if (cur->children != NULL)
275 xmlTextReaderFreeNodeList(reader, cur->children);
276
277 DICT_FREE(cur->name);
278 if ((reader != NULL) && (reader->ctxt != NULL) &&
279 (reader->ctxt->freeAttrsNr < 100)) {
280 cur->next = reader->ctxt->freeAttrs;
281 reader->ctxt->freeAttrs = cur;
282 reader->ctxt->freeAttrsNr++;
283 } else {
284 xmlFree(cur);
285 }
286}
287
288/**
289 * xmlTextReaderFreePropList:
290 * @reader: the xmlTextReaderPtr used
291 * @cur: the first property in the list
292 *
293 * Free a property and all its siblings, all the children are freed too.
294 */
295static void
296xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
297 xmlAttrPtr next;
298 if (cur == NULL) return;
299 while (cur != NULL) {
300 next = cur->next;
301 xmlTextReaderFreeProp(reader, cur);
302 cur = next;
303 }
304}
305
306/**
307 * xmlTextReaderFreeNodeList:
308 * @reader: the xmlTextReaderPtr used
309 * @cur: the first node in the list
310 *
311 * Free a node and all its siblings, this is a recursive behaviour, all
312 * the children are freed too.
313 */
314static void
315xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
316 xmlNodePtr next;
317 xmlDictPtr dict;
318
319 dict = reader->ctxt->dict;
320 if (cur == NULL) return;
321 if (cur->type == XML_NAMESPACE_DECL) {
322 xmlFreeNsList((xmlNsPtr) cur);
323 return;
324 }
325 if ((cur->type == XML_DOCUMENT_NODE) ||
326 (cur->type == XML_HTML_DOCUMENT_NODE)) {
327 xmlFreeDoc((xmlDocPtr) cur);
328 return;
329 }
330 while (cur != NULL) {
331 next = cur->next;
332 /* unroll to speed up freeing the document */
333 if (cur->type != XML_DTD_NODE) {
334
335 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000336 (cur->type != XML_ENTITY_REF_NODE)) {
337 if (cur->children->parent == cur)
338 xmlTextReaderFreeNodeList(reader, cur->children);
339 cur->children = NULL;
340 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000341 if (((cur->type == XML_ELEMENT_NODE) ||
342 (cur->type == XML_XINCLUDE_START) ||
343 (cur->type == XML_XINCLUDE_END)) &&
344 (cur->properties != NULL))
345 xmlTextReaderFreePropList(reader, cur->properties);
346 if ((cur->type != XML_ELEMENT_NODE) &&
347 (cur->type != XML_XINCLUDE_START) &&
348 (cur->type != XML_XINCLUDE_END) &&
349 (cur->type != XML_ENTITY_REF_NODE)) {
350 DICT_FREE(cur->content);
351 }
352 if (((cur->type == XML_ELEMENT_NODE) ||
353 (cur->type == XML_XINCLUDE_START) ||
354 (cur->type == XML_XINCLUDE_END)) &&
355 (cur->nsDef != NULL))
356 xmlFreeNsList(cur->nsDef);
357
358 /*
359 * we don't free element names here they are interned now
360 */
361 if ((cur->type != XML_TEXT_NODE) &&
362 (cur->type != XML_COMMENT_NODE))
363 DICT_FREE(cur->name);
364 if (((cur->type == XML_ELEMENT_NODE) ||
365 (cur->type == XML_TEXT_NODE)) &&
366 (reader != NULL) && (reader->ctxt != NULL) &&
367 (reader->ctxt->freeElemsNr < 100)) {
368 cur->next = reader->ctxt->freeElems;
369 reader->ctxt->freeElems = cur;
370 reader->ctxt->freeElemsNr++;
371 } else {
372 xmlFree(cur);
373 }
374 }
375 cur = next;
376 }
377}
378
379/**
380 * xmlTextReaderFreeNode:
381 * @reader: the xmlTextReaderPtr used
382 * @cur: the node
383 *
384 * Free a node, this is a recursive behaviour, all the children are freed too.
385 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
386 */
387static void
388xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
389 xmlDictPtr dict;
390
391 dict = reader->ctxt->dict;
392 if (cur->type == XML_DTD_NODE) {
393 xmlFreeDtd((xmlDtdPtr) cur);
394 return;
395 }
396 if (cur->type == XML_NAMESPACE_DECL) {
397 xmlFreeNs((xmlNsPtr) cur);
398 return;
399 }
400 if (cur->type == XML_ATTRIBUTE_NODE) {
401 xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
402 return;
403 }
404
405 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000406 (cur->type != XML_ENTITY_REF_NODE)) {
407 if (cur->children->parent == cur)
408 xmlTextReaderFreeNodeList(reader, cur->children);
409 cur->children = NULL;
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);
434 if (((cur->type == XML_ELEMENT_NODE) ||
435 (cur->type == XML_TEXT_NODE)) &&
436 (reader != NULL) && (reader->ctxt != NULL) &&
437 (reader->ctxt->freeElemsNr < 100)) {
438 cur->next = reader->ctxt->freeElems;
439 reader->ctxt->freeElems = cur;
440 reader->ctxt->freeElemsNr++;
441 } else {
442 xmlFree(cur);
443 }
444}
445
446/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000447 * xmlTextReaderFreeIDTable:
448 * @table: An id table
449 *
450 * Deallocate the memory used by an ID hash table.
451 */
William M. Brack60f394e2003-11-16 06:25:42 +0000452static void
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000453xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
454 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
455}
456
457/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000458 * xmlTextReaderFreeDoc:
459 * @reader: the xmlTextReaderPtr used
460 * @cur: pointer to the document
461 *
462 * Free up all the structures used by a document, tree included.
463 */
464static void
465xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
466 xmlDtdPtr extSubset, intSubset;
467
468 if (cur == NULL) return;
469
470 /*
471 * Do this before freeing the children list to avoid ID lookups
472 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000473 if (cur->ids != NULL) xmlTextReaderFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000474 cur->ids = NULL;
475 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
476 cur->refs = NULL;
477 extSubset = cur->extSubset;
478 intSubset = cur->intSubset;
479 if (intSubset == extSubset)
480 extSubset = NULL;
481 if (extSubset != NULL) {
482 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
483 cur->extSubset = NULL;
484 xmlFreeDtd(extSubset);
485 }
486 if (intSubset != NULL) {
487 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
488 cur->intSubset = NULL;
489 xmlFreeDtd(intSubset);
490 }
491
492 if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
493
494 if (cur->version != NULL) xmlFree((char *) cur->version);
495 if (cur->name != NULL) xmlFree((char *) cur->name);
496 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
497 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
498 if (cur->URL != NULL) xmlFree((char *) cur->URL);
499 xmlFree(cur);
500}
501
502/************************************************************************
503 * *
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000504 * The reader core parser *
505 * *
506 ************************************************************************/
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000507#ifdef DEBUG_READER
508static void
509xmlTextReaderDebug(xmlTextReaderPtr reader) {
510 if ((reader == NULL) || (reader->ctxt == NULL)) {
511 fprintf(stderr, "xmlTextReader NULL\n");
512 return;
513 }
514 fprintf(stderr, "xmlTextReader: state %d depth %d ",
515 reader->state, reader->depth);
516 if (reader->node == NULL) {
517 fprintf(stderr, "node = NULL\n");
518 } else {
519 fprintf(stderr, "node %s\n", reader->node->name);
520 }
521 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
522 reader->base, reader->cur, reader->ctxt->nodeNr);
523 if (reader->input->buffer == NULL) {
524 fprintf(stderr, "buffer is NULL\n");
525 } else {
526#ifdef LIBXML_DEBUG_ENABLED
527 xmlDebugDumpString(stderr,
528 &reader->input->buffer->content[reader->cur]);
529#endif
530 fprintf(stderr, "\n");
531 }
532}
533#endif
534
535/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000536 * xmlTextReaderEntPush:
537 * @reader: the xmlTextReaderPtr used
538 * @value: the entity reference node
539 *
540 * Pushes a new entity reference node on top of the entities stack
541 *
542 * Returns 0 in case of error, the index in the stack otherwise
543 */
544static int
545xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
546{
547 if (reader->entMax <= 0) {
548 reader->entMax = 10;
549 reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
550 sizeof(reader->entTab[0]));
551 if (reader->entTab == NULL) {
552 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
553 return (0);
554 }
555 }
556 if (reader->entNr >= reader->entMax) {
557 reader->entMax *= 2;
558 reader->entTab =
559 (xmlNodePtr *) xmlRealloc(reader->entTab,
560 reader->entMax *
561 sizeof(reader->entTab[0]));
562 if (reader->entTab == NULL) {
563 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
564 return (0);
565 }
566 }
567 reader->entTab[reader->entNr] = value;
568 reader->ent = value;
569 return (reader->entNr++);
570}
571
572/**
573 * xmlTextReaderEntPop:
574 * @reader: the xmlTextReaderPtr used
575 *
576 * Pops the top element entity from the entities stack
577 *
578 * Returns the entity just removed
579 */
580static xmlNodePtr
581xmlTextReaderEntPop(xmlTextReaderPtr reader)
582{
583 xmlNodePtr ret;
584
585 if (reader->entNr <= 0)
586 return (0);
587 reader->entNr--;
588 if (reader->entNr > 0)
589 reader->ent = reader->entTab[reader->entNr - 1];
590 else
591 reader->ent = NULL;
592 ret = reader->entTab[reader->entNr];
593 reader->entTab[reader->entNr] = 0;
594 return (ret);
595}
596
597/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000598 * xmlTextReaderStartElement:
599 * @ctx: the user data (XML parser context)
600 * @fullname: The element name, including namespace prefix
601 * @atts: An array of name/value attributes pairs, NULL terminated
602 *
603 * called when an opening tag has been processed.
604 */
605static void
606xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
607 const xmlChar **atts) {
608 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
609 xmlTextReaderPtr reader = ctxt->_private;
610
611#ifdef DEBUG_CALLBACKS
612 printf("xmlTextReaderStartElement(%s)\n", fullname);
613#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000614 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000615 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000616 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
617 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
618 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000619 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000620 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000621 if (reader != NULL)
622 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000623}
624
625/**
626 * xmlTextReaderEndElement:
627 * @ctx: the user data (XML parser context)
628 * @fullname: The element name, including namespace prefix
629 *
630 * called when an ending tag has been processed.
631 */
632static void
633xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
634 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
635 xmlTextReaderPtr reader = ctxt->_private;
636
637#ifdef DEBUG_CALLBACKS
638 printf("xmlTextReaderEndElement(%s)\n", fullname);
639#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000640 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000641 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000642 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000643}
644
645/**
Daniel Veillard07cb8222003-09-10 10:51:05 +0000646 * xmlTextReaderStartElementNs:
647 * @ctx: the user data (XML parser context)
648 * @localname: the local name of the element
649 * @prefix: the element namespace prefix if available
650 * @URI: the element namespace name if available
651 * @nb_namespaces: number of namespace definitions on that node
652 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
653 * @nb_attributes: the number of attributes on that node
654 * nb_defaulted: the number of defaulted attributes.
655 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
656 * attribute values.
657 *
658 * called when an opening tag has been processed.
659 */
660static void
661xmlTextReaderStartElementNs(void *ctx,
662 const xmlChar *localname,
663 const xmlChar *prefix,
664 const xmlChar *URI,
665 int nb_namespaces,
666 const xmlChar **namespaces,
667 int nb_attributes,
668 int nb_defaulted,
669 const xmlChar **attributes)
670{
671 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
672 xmlTextReaderPtr reader = ctxt->_private;
673
674#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000675 printf("xmlTextReaderStartElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000676#endif
677 if ((reader != NULL) && (reader->startElementNs != NULL)) {
678 reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
679 namespaces, nb_attributes, nb_defaulted,
680 attributes);
681 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
682 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
683 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000684 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillard07cb8222003-09-10 10:51:05 +0000685 }
686 if (reader != NULL)
687 reader->state = XML_TEXTREADER_ELEMENT;
688}
689
690/**
691 * xmlTextReaderEndElementNs:
692 * @ctx: the user data (XML parser context)
693 * @localname: the local name of the element
694 * @prefix: the element namespace prefix if available
695 * @URI: the element namespace name if available
696 *
697 * called when an ending tag has been processed.
698 */
699static void
700xmlTextReaderEndElementNs(void *ctx,
701 const xmlChar * localname,
702 const xmlChar * prefix,
703 const xmlChar * URI)
704{
705 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
706 xmlTextReaderPtr reader = ctxt->_private;
707
708#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000709 printf("xmlTextReaderEndElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000710#endif
711 if ((reader != NULL) && (reader->endElementNs != NULL)) {
712 reader->endElementNs(ctx, localname, prefix, URI);
713 }
714}
715
716
717/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000718 * xmlTextReaderCharacters:
719 * @ctx: the user data (XML parser context)
720 * @ch: a xmlChar string
721 * @len: the number of xmlChar
722 *
723 * receiving some chars from the parser.
724 */
725static void
726xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
727{
728 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
729 xmlTextReaderPtr reader = ctxt->_private;
730
731#ifdef DEBUG_CALLBACKS
732 printf("xmlTextReaderCharacters()\n");
733#endif
734 if ((reader != NULL) && (reader->characters != NULL)) {
735 reader->characters(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000736 }
737}
738
739/**
740 * xmlTextReaderCDataBlock:
741 * @ctx: the user data (XML parser context)
742 * @value: The pcdata content
743 * @len: the block length
744 *
745 * called when a pcdata block has been parsed
746 */
747static void
748xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
749{
750 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
751 xmlTextReaderPtr reader = ctxt->_private;
752
753#ifdef DEBUG_CALLBACKS
754 printf("xmlTextReaderCDataBlock()\n");
755#endif
756 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
757 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000758 }
759}
760
761/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000762 * xmlTextReaderPushData:
763 * @reader: the xmlTextReaderPtr used
764 *
765 * Push data down the progressive parser until a significant callback
766 * got raised.
767 *
768 * Returns -1 in case of failure, 0 otherwise
769 */
770static int
771xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000772 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000773 int val, s;
William M. Brack779af002003-08-01 15:55:39 +0000774 xmlTextReaderState oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000775
776 if ((reader->input == NULL) || (reader->input->buffer == NULL))
777 return(-1);
778
Daniel Veillardea7751d2002-12-20 00:16:24 +0000779 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000780 reader->state = XML_TEXTREADER_NONE;
781 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000782
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000783 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000784 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000785 /*
786 * Refill the buffer unless we are at the end of the stream
787 */
788 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
789 val = xmlParserInputBufferRead(reader->input, 4096);
Daniel Veillard53350552003-09-18 13:35:51 +0000790 if ((val == 0) &&
791 (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
792 if (inbuf->use == reader->cur) {
793 reader->mode = XML_TEXTREADER_MODE_EOF;
794 reader->state = oldstate;
795 if ((oldstate != XML_TEXTREADER_START) ||
796 (reader->ctxt->myDoc != NULL))
797 return(val);
798 }
799 } else if (val < 0) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000800 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000801 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000802 if ((oldstate != XML_TEXTREADER_START) ||
803 (reader->ctxt->myDoc != NULL))
804 return(val);
Daniel Veillard53350552003-09-18 13:35:51 +0000805 } else if (val == 0) {
806 /* mark the end of the stream and process the remains */
807 reader->mode = XML_TEXTREADER_MODE_EOF;
808 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000809 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000810
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000811 } else
812 break;
813 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000814 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000815 * parse by block of CHUNK_SIZE bytes, various tests show that
816 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000817 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000818 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000819 val = xmlParseChunk(reader->ctxt,
820 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000821 CHUNK_SIZE, 0);
822 reader->cur += CHUNK_SIZE;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000823 if ((val != 0) && (reader->ctxt->wellFormed == 0))
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000824 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000825 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000826 s = inbuf->use - reader->cur;
827 val = xmlParseChunk(reader->ctxt,
828 (const char *) &inbuf->content[reader->cur],
829 s, 0);
830 reader->cur += s;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000831 if ((val != 0) && (reader->ctxt->wellFormed == 0))
Daniel Veillarda880b122003-04-21 21:36:41 +0000832 return(-1);
833 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000834 }
835 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000836
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000837 /*
838 * Discard the consumed input when needed and possible
839 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000840 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillardf6bc7c22003-09-17 22:33:22 +0000841 if ((reader->cur >= 4096) &&
842 (inbuf->use - reader->cur <= CHUNK_SIZE)) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000843 val = xmlBufferShrink(inbuf, reader->cur);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000844 if (val >= 0) {
845 reader->cur -= val;
846 }
847 }
848 }
849
850 /*
851 * At the end of the stream signal that the work is done to the Push
852 * parser.
853 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000854 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000855 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000856 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000857 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000858 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000859 s, 1);
860 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000861 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000862 if ((val != 0) && (reader->ctxt->wellFormed == 0))
863 return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000864 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000865 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000866 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000867 return(0);
868}
869
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000870#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000871/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000872 * xmlTextReaderValidatePush:
873 * @reader: the xmlTextReaderPtr used
874 *
875 * Push the current node for validation
876 */
877static void
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000878xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000879 xmlNodePtr node = reader->node;
880
Daniel Veillardf4e55762003-04-15 23:32:22 +0000881 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
882 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
883 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
884 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
885 reader->ctxt->myDoc, node, node->name);
886 } else {
887 /* TODO use the BuildQName interface */
888 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000889
Daniel Veillardf4e55762003-04-15 23:32:22 +0000890 qname = xmlStrdup(node->ns->prefix);
891 qname = xmlStrcat(qname, BAD_CAST ":");
892 qname = xmlStrcat(qname, node->name);
893 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
894 reader->ctxt->myDoc, node, qname);
895 if (qname != NULL)
896 xmlFree(qname);
897 }
898#ifdef LIBXML_SCHEMAS_ENABLED
899 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
900 (reader->rngValidCtxt != NULL)) {
901 int ret;
902
903 if (reader->rngFullNode != NULL) return;
904 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
905 reader->ctxt->myDoc,
906 node);
907 if (ret == 0) {
908 /*
909 * this element requires a full tree
910 */
911 node = xmlTextReaderExpand(reader);
912 if (node == NULL) {
913printf("Expand failed !\n");
914 ret = -1;
915 } else {
916 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
917 reader->ctxt->myDoc,
918 node);
919 reader->rngFullNode = node;
920 }
921 }
922 if (ret != 1)
923 reader->rngValidErrors++;
924#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000925 }
926}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000927
928/**
929 * xmlTextReaderValidateCData:
930 * @reader: the xmlTextReaderPtr used
931 * @data: pointer to the CData
932 * @len: lenght of the CData block in bytes.
933 *
934 * Push some CData for validation
935 */
936static void
937xmlTextReaderValidateCData(xmlTextReaderPtr reader,
938 const xmlChar *data, int len) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000939 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
940 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
941 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
942 data, len);
943#ifdef LIBXML_SCHEMAS_ENABLED
944 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
945 (reader->rngValidCtxt != NULL)) {
946 int ret;
947
948 if (reader->rngFullNode != NULL) return;
949 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
950 if (ret != 1)
951 reader->rngValidErrors++;
952#endif
953 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000954}
955
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000956/**
957 * xmlTextReaderValidatePop:
958 * @reader: the xmlTextReaderPtr used
959 *
960 * Pop the current node from validation
961 */
962static void
963xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
964 xmlNodePtr node = reader->node;
965
Daniel Veillardf4e55762003-04-15 23:32:22 +0000966 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
967 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
968 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
969 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
970 reader->ctxt->myDoc, node, node->name);
971 } else {
972 /* TODO use the BuildQName interface */
973 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000974
Daniel Veillardf4e55762003-04-15 23:32:22 +0000975 qname = xmlStrdup(node->ns->prefix);
976 qname = xmlStrcat(qname, BAD_CAST ":");
977 qname = xmlStrcat(qname, node->name);
978 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
979 reader->ctxt->myDoc, node, qname);
980 if (qname != NULL)
981 xmlFree(qname);
982 }
983#ifdef LIBXML_SCHEMAS_ENABLED
984 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
985 (reader->rngValidCtxt != NULL)) {
986 int ret;
987
988 if (reader->rngFullNode != NULL) {
989 if (node == reader->rngFullNode)
990 reader->rngFullNode = NULL;
991 return;
992 }
993 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
994 reader->ctxt->myDoc,
995 node);
996 if (ret != 1)
997 reader->rngValidErrors++;
998#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000999 }
1000}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001001
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001002/**
1003 * xmlTextReaderValidateEntity:
1004 * @reader: the xmlTextReaderPtr used
1005 *
1006 * Handle the validation when an entity reference is encountered and
1007 * entity substitution is not activated. As a result the parser interface
1008 * must walk through the entity and do the validation calls
1009 */
1010static void
1011xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
1012 xmlNodePtr oldnode = reader->node;
1013 xmlNodePtr node = reader->node;
1014 xmlParserCtxtPtr ctxt = reader->ctxt;
1015
1016 do {
1017 if (node->type == XML_ENTITY_REF_NODE) {
1018 /*
1019 * Case where the underlying tree is not availble, lookup the entity
1020 * and walk it.
1021 */
1022 if ((node->children == NULL) && (ctxt->sax != NULL) &&
1023 (ctxt->sax->getEntity != NULL)) {
1024 node->children = (xmlNodePtr)
1025 ctxt->sax->getEntity(ctxt, node->name);
1026 }
1027
1028 if ((node->children != NULL) &&
1029 (node->children->type == XML_ENTITY_DECL) &&
1030 (node->children->children != NULL)) {
1031 xmlTextReaderEntPush(reader, node);
1032 node = node->children->children;
1033 continue;
1034 } else {
1035 /*
1036 * The error has probably be raised already.
1037 */
1038 if (node == oldnode)
1039 break;
1040 node = node->next;
1041 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001042#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001043 } else if (node->type == XML_ELEMENT_NODE) {
1044 reader->node = node;
1045 xmlTextReaderValidatePush(reader);
1046 } else if ((node->type == XML_TEXT_NODE) ||
1047 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001048 xmlTextReaderValidateCData(reader, node->content,
1049 xmlStrlen(node->content));
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001050#endif
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001051 }
1052
1053 /*
1054 * go to next node
1055 */
1056 if (node->children != NULL) {
1057 node = node->children;
1058 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00001059 } else if (node->type == XML_ELEMENT_NODE) {
1060 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001061 }
1062 if (node->next != NULL) {
1063 node = node->next;
1064 continue;
1065 }
1066 do {
1067 node = node->parent;
1068 if (node->type == XML_ELEMENT_NODE) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001069 xmlNodePtr tmp;
Daniel Veillard1d211e22003-10-20 22:32:39 +00001070 if (reader->entNr == 0) {
1071 while ((tmp = node->last) != NULL) {
Daniel Veillarde8039df2003-10-27 11:25:13 +00001072 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard1d211e22003-10-20 22:32:39 +00001073 xmlUnlinkNode(tmp);
1074 xmlTextReaderFreeNode(reader, tmp);
1075 } else
1076 break;
1077 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001078 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001079 reader->node = node;
1080 xmlTextReaderValidatePop(reader);
1081 }
1082 if ((node->type == XML_ENTITY_DECL) &&
1083 (reader->ent != NULL) && (reader->ent->children == node)) {
1084 node = xmlTextReaderEntPop(reader);
1085 }
1086 if (node == oldnode)
1087 break;
1088 if (node->next != NULL) {
1089 node = node->next;
1090 break;
1091 }
1092 } while ((node != NULL) && (node != oldnode));
1093 } while ((node != NULL) && (node != oldnode));
1094 reader->node = oldnode;
1095}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001096#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001097
1098
1099/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001100 * xmlTextReaderGetSuccessor:
1101 * @cur: the current node
1102 *
1103 * Get the successor of a node if available.
1104 *
1105 * Returns the successor node or NULL
1106 */
1107static xmlNodePtr
1108xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1109 if (cur == NULL) return(NULL) ; /* ERROR */
1110 if (cur->next != NULL) return(cur->next) ;
1111 do {
1112 cur = cur->parent;
1113 if (cur == NULL) return(NULL);
1114 if (cur->next != NULL) return(cur->next);
1115 } while (cur != NULL);
1116 return(cur);
1117}
1118
1119/**
1120 * xmlTextReaderDoExpand:
1121 * @reader: the xmlTextReaderPtr used
1122 *
1123 * Makes sure that the current node is fully read as well as all its
1124 * descendant. It means the full DOM subtree must be available at the
1125 * end of the call.
1126 *
1127 * Returns 1 if the node was expanded successfully, 0 if there is no more
1128 * nodes to read, or -1 in case of error
1129 */
1130static int
1131xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1132 int val;
1133
1134 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1135 return(-1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001136 do {
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001137 if (reader->ctxt->instate == XML_PARSER_EOF) return(1);
1138
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001139 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1140 return(1);
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001141 if (reader->ctxt->nodeNr < reader->depth)
Daniel Veillarda37aab82003-06-09 09:10:36 +00001142 return(1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001143 if (reader->mode == XML_TEXTREADER_MODE_EOF)
1144 return(1);
1145 val = xmlTextReaderPushData(reader);
1146 if (val < 0)
1147 return(-1);
1148 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1149 return(1);
1150}
1151
1152/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001153 * xmlTextReaderRead:
1154 * @reader: the xmlTextReaderPtr used
1155 *
1156 * Moves the position of the current instance to the next node in
1157 * the stream, exposing its properties.
1158 *
1159 * Returns 1 if the node was read successfully, 0 if there is no more
1160 * nodes to read, or -1 in case of error
1161 */
1162int
1163xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +00001164 int val, olddepth = 0;
William M. Brack899e64a2003-09-26 18:03:42 +00001165 xmlTextReaderState oldstate = XML_TEXTREADER_START;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001166 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001167
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001168
1169 if (reader == NULL)
1170 return(-1);
1171 if (reader->doc != NULL)
1172 return(xmlTextReaderReadTree(reader));
1173 if (reader->ctxt == NULL)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001174 return(-1);
1175 if (reader->ctxt->wellFormed != 1)
1176 return(-1);
1177
1178#ifdef DEBUG_READER
1179 fprintf(stderr, "\nREAD ");
1180 DUMP_READER
1181#endif
Daniel Veillard29b3e282002-12-29 11:14:41 +00001182 reader->curnode = NULL;
Daniel Veillard67df8092002-12-16 22:04:11 +00001183 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1184 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001185 /*
1186 * Initial state
1187 */
1188 do {
1189 val = xmlTextReaderPushData(reader);
1190 if (val < 0)
1191 return(-1);
1192 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001193 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1194 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001195 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001196 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001197 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001198 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001199 if (reader->node == NULL)
1200 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001201 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001202 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +00001203 if (reader->ctxt->myDoc != NULL) {
1204 reader->node = reader->ctxt->myDoc->children;
1205 }
1206 if (reader->node == NULL)
1207 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +00001208 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001209 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001210 reader->depth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001211 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001212 }
1213 oldstate = reader->state;
1214 olddepth = reader->ctxt->nodeNr;
1215 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001216
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001217get_next_node:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001218 /*
1219 * If we are not backtracking on ancestors or examined nodes,
1220 * that the parser didn't finished or that we arent at the end
1221 * of stream, continue processing.
1222 */
Daniel Veillarda880b122003-04-21 21:36:41 +00001223 while ((reader->node->next == NULL) &&
1224 (reader->ctxt->nodeNr == olddepth) &&
1225 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +00001226 (reader->node->children == NULL) ||
1227 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard409a8142003-07-18 15:16:57 +00001228 ((reader->node->children != NULL) &&
1229 (reader->node->children->type == XML_TEXT_NODE) &&
1230 (reader->node->children->next == NULL)) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001231 (reader->node->type == XML_DTD_NODE) ||
1232 (reader->node->type == XML_DOCUMENT_NODE) ||
1233 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001234 ((reader->ctxt->node == NULL) ||
1235 (reader->ctxt->node == reader->node) ||
1236 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +00001237 (reader->ctxt->instate != XML_PARSER_EOF)) {
1238 val = xmlTextReaderPushData(reader);
1239 if (val < 0)
1240 return(-1);
1241 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001242 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001243 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001244 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001245 if ((reader->node->children != NULL) &&
1246 (reader->node->type != XML_ENTITY_REF_NODE) &&
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001247 (reader->node->type != XML_XINCLUDE_START) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001248 (reader->node->type != XML_DTD_NODE)) {
1249 reader->node = reader->node->children;
1250 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001251 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001252 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001253 }
1254 }
1255 if (reader->node->next != NULL) {
1256 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +00001257 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001258 (reader->node->children == NULL) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001259 ((reader->node->extra & NODE_IS_EMPTY) == 0)
1260#ifdef LIBXML_XINCLUDE_ENABLED
1261 && (reader->in_xinclude <= 0)
1262#endif
1263 ) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001264 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001265 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001266 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001267#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001268 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001269 (reader->node->type == XML_ELEMENT_NODE))
1270 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001271#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001272 if ((reader->preserves > 0) &&
1273 (reader->node->extra & NODE_IS_SPRESERVED))
1274 reader->preserves--;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001275 reader->node = reader->node->next;
1276 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001277
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001278 /*
1279 * Cleanup of the old node
1280 */
Daniel Veillard1e906612003-12-05 14:57:46 +00001281 if ((reader->preserves == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001282#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard1e906612003-12-05 14:57:46 +00001283 (reader->in_xinclude == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001284#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001285 (reader->entNr == 0) &&
1286 (reader->node->prev != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001287 (reader->node->prev->type != XML_DTD_NODE) &&
1288 (reader->entNr == 0)) {
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001289 xmlNodePtr tmp = reader->node->prev;
Daniel Veillarde8039df2003-10-27 11:25:13 +00001290 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001291 xmlUnlinkNode(tmp);
1292 xmlTextReaderFreeNode(reader, tmp);
1293 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001294 }
1295
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001296 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001297 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001298 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +00001299 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001300 (reader->node->children == NULL) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001301 ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001302 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001303 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001304 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001305#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001306 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001307 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001308#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001309 if ((reader->preserves > 0) &&
1310 (reader->node->extra & NODE_IS_SPRESERVED))
1311 reader->preserves--;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001312 reader->node = reader->node->parent;
1313 if ((reader->node == NULL) ||
1314 (reader->node->type == XML_DOCUMENT_NODE) ||
1315#ifdef LIBXML_DOCB_ENABLED
1316 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
1317#endif
1318 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001319 if (reader->mode != XML_TEXTREADER_DONE) {
1320 val = xmlParseChunk(reader->ctxt, "", 0, 1);
1321 reader->mode = XML_TEXTREADER_DONE;
1322 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001323 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001324 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001325
1326 /*
1327 * Cleanup of the old node
1328 */
Daniel Veillard1e906612003-12-05 14:57:46 +00001329 if ((reader->preserves == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001330#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard1e906612003-12-05 14:57:46 +00001331 (reader->in_xinclude == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001332#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001333 (reader->entNr == 0) &&
1334 (oldnode->type != XML_DTD_NODE) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001335 ((oldnode->extra & NODE_IS_PRESERVED) == 0) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001336 (reader->entNr == 0)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001337 xmlUnlinkNode(oldnode);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001338 xmlTextReaderFreeNode(reader, oldnode);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001339 }
1340
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001341 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001342 }
Daniel Veillard1e906612003-12-05 14:57:46 +00001343 if ((reader->preserves == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001344#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard1e906612003-12-05 14:57:46 +00001345 (reader->in_xinclude == 0) &&
Daniel Veillard96b6cd22004-01-08 16:49:50 +00001346#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001347 (reader->entNr == 0) &&
1348 (reader->node->last != NULL) &&
1349 ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
1350 xmlNodePtr tmp = reader->node->last;
1351 xmlUnlinkNode(tmp);
1352 xmlTextReaderFreeNode(reader, tmp);
1353 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001354 reader->depth--;
1355 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001356
1357node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001358 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001359
1360 /*
Daniel Veillarda880b122003-04-21 21:36:41 +00001361 * If we are in the middle of a piece of CDATA make sure it's finished
1362 */
1363 if ((reader->node != NULL) &&
Daniel Veillardccc4d2b2003-09-17 21:27:31 +00001364 (reader->node->next == NULL) &&
Daniel Veillarda880b122003-04-21 21:36:41 +00001365 ((reader->node->type == XML_TEXT_NODE) ||
1366 (reader->node->type == XML_CDATA_SECTION_NODE))) {
1367 xmlTextReaderExpand(reader);
1368 }
1369
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001370#ifdef LIBXML_XINCLUDE_ENABLED
1371 /*
1372 * Handle XInclude if asked for
1373 */
1374 if ((reader->xinclude) && (reader->node != NULL) &&
1375 (reader->node->type == XML_ELEMENT_NODE) &&
1376 (reader->node->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00001377 ((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) ||
1378 (xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001379 if (reader->xincctxt == NULL) {
1380 reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00001381 xmlXIncludeSetFlags(reader->xincctxt, reader->parserFlags);
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001382 }
1383 /*
1384 * expand that node and process it
1385 */
1386 xmlTextReaderExpand(reader);
1387 xmlXIncludeProcessNode(reader->xincctxt, reader->node);
1388 }
1389 if (reader->node->type == XML_XINCLUDE_START) {
1390 reader->in_xinclude++;
1391 goto get_next_node;
1392 }
1393 if (reader->node->type == XML_XINCLUDE_END) {
1394 reader->in_xinclude--;
1395 goto get_next_node;
1396 }
1397#endif
Daniel Veillarda880b122003-04-21 21:36:41 +00001398 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001399 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001400 */
1401 if ((reader->node != NULL) &&
1402 (reader->node->type == XML_ENTITY_REF_NODE) &&
1403 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1404 /*
1405 * Case where the underlying tree is not availble, lookup the entity
1406 * and walk it.
1407 */
1408 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
1409 (reader->ctxt->sax->getEntity != NULL)) {
1410 reader->node->children = (xmlNodePtr)
1411 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
1412 }
1413
1414 if ((reader->node->children != NULL) &&
1415 (reader->node->children->type == XML_ENTITY_DECL) &&
1416 (reader->node->children->children != NULL)) {
1417 xmlTextReaderEntPush(reader, reader->node);
1418 reader->node = reader->node->children->children;
1419 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001420#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001421 } else if ((reader->node != NULL) &&
1422 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +00001423 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001424 xmlTextReaderValidateEntity(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001425#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001426 }
1427 if ((reader->node != NULL) &&
1428 (reader->node->type == XML_ENTITY_DECL) &&
1429 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1430 reader->node = xmlTextReaderEntPop(reader);
1431 reader->depth++;
1432 goto get_next_node;
1433 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001434#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001435 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001436 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001437
1438 if ((node->type == XML_ELEMENT_NODE) &&
1439 ((reader->state != XML_TEXTREADER_END) &&
1440 (reader->state != XML_TEXTREADER_BACKTRACK))) {
1441 xmlTextReaderValidatePush(reader);
1442 } else if ((node->type == XML_TEXT_NODE) ||
1443 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001444 xmlTextReaderValidateCData(reader, node->content,
1445 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001446 }
1447 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001448#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001449#ifdef LIBXML_PATTERN_ENABLED
1450 if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
1451 (reader->state != XML_TEXTREADER_BACKTRACK)) {
1452 int i;
1453 for (i = 0;i < reader->patternNr;i++) {
1454 if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
1455 xmlTextReaderPreserve(reader);
1456 break;
1457 }
1458 }
1459 }
1460#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001461 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001462node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001463 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001464 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001465}
1466
Daniel Veillard67df8092002-12-16 22:04:11 +00001467/**
1468 * xmlTextReaderReadState:
1469 * @reader: the xmlTextReaderPtr used
1470 *
1471 * Gets the read state of the reader.
1472 *
1473 * Returns the state value, or -1 in case of error
1474 */
1475int
1476xmlTextReaderReadState(xmlTextReaderPtr reader) {
1477 if (reader == NULL)
1478 return(-1);
1479 return(reader->mode);
1480}
1481
1482/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001483 * xmlTextReaderExpand:
1484 * @reader: the xmlTextReaderPtr used
1485 *
1486 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +00001487 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001488 *
1489 * Returns a node pointer valid until the next xmlTextReaderRead() call
1490 * or NULL in case of error.
1491 */
1492xmlNodePtr
1493xmlTextReaderExpand(xmlTextReaderPtr reader) {
Daniel Veillardf40c1ce2003-12-19 17:26:28 +00001494 if ((reader == NULL) || (reader->node == NULL))
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001495 return(NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001496 if (reader->doc != NULL)
1497 return(reader->node);
Daniel Veillardf40c1ce2003-12-19 17:26:28 +00001498 if (reader->ctxt == NULL)
1499 return(NULL);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001500 if (xmlTextReaderDoExpand(reader) < 0)
1501 return(NULL);
1502 return(reader->node);
1503}
1504
1505/**
1506 * xmlTextReaderNext:
1507 * @reader: the xmlTextReaderPtr used
1508 *
1509 * Skip to the node following the current one in document order while
1510 * avoiding the subtree if any.
1511 *
1512 * Returns 1 if the node was read successfully, 0 if there is no more
1513 * nodes to read, or -1 in case of error
1514 */
1515int
1516xmlTextReaderNext(xmlTextReaderPtr reader) {
1517 int ret;
1518 xmlNodePtr cur;
1519
1520 if (reader == NULL)
1521 return(-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001522 if (reader->doc != NULL)
1523 return(xmlTextReaderNextTree(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001524 cur = reader->node;
1525 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1526 return(xmlTextReaderRead(reader));
1527 if (reader->state == XML_TEXTREADER_END)
1528 return(xmlTextReaderRead(reader));
Daniel Veillarde8039df2003-10-27 11:25:13 +00001529 if (cur->extra & NODE_IS_EMPTY)
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001530 return(xmlTextReaderRead(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001531 do {
1532 ret = xmlTextReaderRead(reader);
1533 if (ret != 1)
1534 return(ret);
1535 } while (reader->node != cur);
1536 return(xmlTextReaderRead(reader));
1537}
1538
1539/**
Daniel Veillard67df8092002-12-16 22:04:11 +00001540 * xmlTextReaderReadInnerXml:
1541 * @reader: the xmlTextReaderPtr used
1542 *
1543 * Reads the contents of the current node, including child nodes and markup.
1544 *
1545 * Returns a string containing the XML content, or NULL if the current node
1546 * is neither an element nor attribute, or has no child nodes. The
1547 * string must be deallocated by the caller.
1548 */
1549xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001550xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001551 TODO
1552 return(NULL);
1553}
1554
1555/**
1556 * xmlTextReaderReadOuterXml:
1557 * @reader: the xmlTextReaderPtr used
1558 *
1559 * Reads the contents of the current node, including child nodes and markup.
1560 *
1561 * Returns a string containing the XML content, or NULL if the current node
1562 * is neither an element nor attribute, or has no child nodes. The
1563 * string must be deallocated by the caller.
1564 */
1565xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001566xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001567 TODO
1568 return(NULL);
1569}
1570
1571/**
1572 * xmlTextReaderReadString:
1573 * @reader: the xmlTextReaderPtr used
1574 *
1575 * Reads the contents of an element or a text node as a string.
1576 *
1577 * Returns a string containing the contents of the Element or Text node,
1578 * or NULL if the reader is positioned on any other type of node.
1579 * The string must be deallocated by the caller.
1580 */
1581xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001582xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001583 TODO
1584 return(NULL);
1585}
1586
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001587#if 0
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001588/**
1589 * xmlTextReaderReadBase64:
1590 * @reader: the xmlTextReaderPtr used
1591 * @array: a byte array to store the content.
1592 * @offset: the zero-based index into array where the method should
1593 * begin to write.
1594 * @len: the number of bytes to write.
1595 *
1596 * Reads and decodes the Base64 encoded contents of an element and
1597 * stores the result in a byte buffer.
1598 *
1599 * Returns the number of bytes written to array, or zero if the current
1600 * instance is not positioned on an element or -1 in case of error.
1601 */
1602int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001603xmlTextReaderReadBase64(xmlTextReaderPtr reader,
1604 unsigned char *array ATTRIBUTE_UNUSED,
1605 int offset ATTRIBUTE_UNUSED,
1606 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001607 if ((reader == NULL) || (reader->ctxt == NULL))
1608 return(-1);
1609 if (reader->ctxt->wellFormed != 1)
1610 return(-1);
1611
1612 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1613 return(0);
1614 TODO
1615 return(0);
1616}
1617
1618/**
1619 * xmlTextReaderReadBinHex:
1620 * @reader: the xmlTextReaderPtr used
1621 * @array: a byte array to store the content.
1622 * @offset: the zero-based index into array where the method should
1623 * begin to write.
1624 * @len: the number of bytes to write.
1625 *
1626 * Reads and decodes the BinHex encoded contents of an element and
1627 * stores the result in a byte buffer.
1628 *
1629 * Returns the number of bytes written to array, or zero if the current
1630 * instance is not positioned on an element or -1 in case of error.
1631 */
1632int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001633xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
1634 unsigned char *array ATTRIBUTE_UNUSED,
1635 int offset ATTRIBUTE_UNUSED,
1636 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001637 if ((reader == NULL) || (reader->ctxt == NULL))
1638 return(-1);
1639 if (reader->ctxt->wellFormed != 1)
1640 return(-1);
1641
1642 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1643 return(0);
1644 TODO
1645 return(0);
1646}
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001647#endif
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001648
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001649/************************************************************************
1650 * *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001651 * Operating on a preparsed tree *
1652 * *
1653 ************************************************************************/
1654static int
1655xmlTextReaderNextTree(xmlTextReaderPtr reader)
1656{
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001657 if (reader == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001658 return(-1);
1659
1660 if (reader->state == XML_TEXTREADER_END)
1661 return(0);
1662
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001663 if (reader->node == NULL) {
1664 if (reader->doc->children == NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001665 reader->state = XML_TEXTREADER_END;
1666 return(0);
1667 }
1668
1669 reader->node = reader->doc->children;
1670 reader->state = XML_TEXTREADER_START;
1671 return(1);
1672 }
1673
1674 if (reader->state != XML_TEXTREADER_BACKTRACK) {
1675 if (reader->node->children != 0) {
1676 reader->node = reader->node->children;
1677 reader->depth++;
1678 reader->state = XML_TEXTREADER_START;
1679 return(1);
1680 }
1681
1682 if ((reader->node->type == XML_ELEMENT_NODE) ||
1683 (reader->node->type == XML_ATTRIBUTE_NODE)) {
1684 reader->state = XML_TEXTREADER_BACKTRACK;
1685 return(1);
1686 }
1687 }
1688
1689 if (reader->node->next != 0) {
1690 reader->node = reader->node->next;
1691 reader->state = XML_TEXTREADER_START;
1692 return(1);
1693 }
1694
1695 if (reader->node->parent != 0) {
1696 if (reader->node->parent->type == XML_DOCUMENT_NODE) {
1697 reader->state = XML_TEXTREADER_END;
1698 return(0);
1699 }
1700
1701 reader->node = reader->node->parent;
1702 reader->depth--;
1703 reader->state = XML_TEXTREADER_BACKTRACK;
1704 return(1);
1705 }
1706
1707 reader->state = XML_TEXTREADER_END;
1708
1709 return(1);
1710}
1711
1712/**
1713 * xmlTextReaderReadTree:
1714 * @reader: the xmlTextReaderPtr used
1715 *
1716 * Moves the position of the current instance to the next node in
1717 * the stream, exposing its properties.
1718 *
1719 * Returns 1 if the node was read successfully, 0 if there is no more
1720 * nodes to read, or -1 in case of error
1721 */
1722static int
1723xmlTextReaderReadTree(xmlTextReaderPtr reader) {
1724 if (reader->state == XML_TEXTREADER_END)
1725 return(0);
1726
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001727next_node:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001728 if (reader->node == NULL) {
1729 if (reader->doc->children == NULL) {
1730 reader->state = XML_TEXTREADER_END;
1731 return(0);
1732 }
1733
1734 reader->node = reader->doc->children;
1735 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001736 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001737 }
1738
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001739 if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
1740 (reader->node->type != XML_DTD_NODE) &&
1741 (reader->node->type != XML_XINCLUDE_START) &&
1742 (reader->node->type != XML_ENTITY_REF_NODE)) {
1743 if (reader->node->children != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001744 reader->node = reader->node->children;
1745 reader->depth++;
1746 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001747 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001748 }
1749
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001750 if (reader->node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001751 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001752 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001753 }
1754 }
1755
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001756 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001757 reader->node = reader->node->next;
1758 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001759 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001760 }
1761
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001762 if (reader->node->parent != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001763 if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
1764 (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
1765 reader->state = XML_TEXTREADER_END;
1766 return(0);
1767 }
1768
1769 reader->node = reader->node->parent;
1770 reader->depth--;
1771 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001772 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001773 }
1774
1775 reader->state = XML_TEXTREADER_END;
1776
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001777found_node:
1778 if ((reader->node->type == XML_XINCLUDE_START) ||
1779 (reader->node->type == XML_XINCLUDE_END))
1780 goto next_node;
1781
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001782 return(1);
1783}
1784
1785/**
William M. Brackb1d53162003-11-18 06:54:40 +00001786 * xmlTextReaderNextSibling:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001787 * @reader: the xmlTextReaderPtr used
1788 *
1789 * Skip to the node following the current one in document order while
1790 * avoiding the subtree if any.
1791 * Currently implemented only for Readers built on a document
1792 *
1793 * Returns 1 if the node was read successfully, 0 if there is no more
1794 * nodes to read, or -1 in case of error
1795 */
1796int
1797xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
1798 if (reader == NULL)
1799 return(-1);
1800 if (reader->doc == NULL) {
1801 TODO
1802 return(-1);
1803 }
1804
1805 if (reader->state == XML_TEXTREADER_END)
1806 return(0);
1807
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001808 if (reader->node == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001809 return(xmlTextReaderNextTree(reader));
1810
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001811 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001812 reader->node = reader->node->next;
1813 reader->state = XML_TEXTREADER_START;
1814 return(1);
1815 }
1816
1817 return(0);
1818}
1819
1820/************************************************************************
1821 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001822 * Constructor and destructors *
1823 * *
1824 ************************************************************************/
1825/**
1826 * xmlNewTextReader:
1827 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001828 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001829 *
1830 * Create an xmlTextReader structure fed with @input
1831 *
1832 * Returns the new xmlTextReaderPtr or NULL in case of error
1833 */
1834xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001835xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001836 xmlTextReaderPtr ret;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001837
1838 if (input == NULL)
1839 return(NULL);
1840 ret = xmlMalloc(sizeof(xmlTextReader));
1841 if (ret == NULL) {
1842 xmlGenericError(xmlGenericErrorContext,
1843 "xmlNewTextReader : malloc failed\n");
1844 return(NULL);
1845 }
1846 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001847 ret->doc = NULL;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001848 ret->entTab = NULL;
1849 ret->entMax = 0;
1850 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001851 ret->input = input;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001852 ret->buffer = xmlBufferCreateSize(100);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001853 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1854 if (ret->sax == NULL) {
1855 xmlFree(ret);
1856 xmlGenericError(xmlGenericErrorContext,
1857 "xmlNewTextReader : malloc failed\n");
1858 return(NULL);
1859 }
Daniel Veillard81273902003-09-30 00:43:48 +00001860 xmlSAXVersion(ret->sax, 2);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001861 ret->startElement = ret->sax->startElement;
1862 ret->sax->startElement = xmlTextReaderStartElement;
1863 ret->endElement = ret->sax->endElement;
1864 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillard81273902003-09-30 00:43:48 +00001865#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001866 if (ret->sax->initialized == XML_SAX2_MAGIC) {
Daniel Veillard81273902003-09-30 00:43:48 +00001867#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001868 ret->startElementNs = ret->sax->startElementNs;
1869 ret->sax->startElementNs = xmlTextReaderStartElementNs;
1870 ret->endElementNs = ret->sax->endElementNs;
1871 ret->sax->endElementNs = xmlTextReaderEndElementNs;
Daniel Veillard81273902003-09-30 00:43:48 +00001872#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001873 } else {
1874 ret->startElementNs = NULL;
1875 ret->endElementNs = NULL;
1876 }
Daniel Veillard81273902003-09-30 00:43:48 +00001877#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001878 ret->characters = ret->sax->characters;
1879 ret->sax->characters = xmlTextReaderCharacters;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001880 ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001881 ret->cdataBlock = ret->sax->cdataBlock;
1882 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001883
Daniel Veillard67df8092002-12-16 22:04:11 +00001884 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001885 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001886 ret->curnode = NULL;
Daniel Veillard53350552003-09-18 13:35:51 +00001887 if (ret->input->buffer->use < 4) {
William M. Brack899e64a2003-09-26 18:03:42 +00001888 xmlParserInputBufferRead(input, 4);
Daniel Veillard53350552003-09-18 13:35:51 +00001889 }
1890 if (ret->input->buffer->use >= 4) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001891 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001892 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001893 ret->base = 0;
1894 ret->cur = 4;
1895 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001896 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001897 ret->base = 0;
1898 ret->cur = 0;
1899 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001900 if (ret->ctxt == NULL) {
1901 xmlGenericError(xmlGenericErrorContext,
1902 "xmlNewTextReader : malloc failed\n");
1903 xmlFree(ret->sax);
1904 xmlFree(ret);
1905 return(NULL);
1906 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001907 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001908 ret->ctxt->linenumbers = 1;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001909 ret->ctxt->dictNames = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001910 ret->allocs = XML_TEXTREADER_CTXT;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001911 /*
1912 * use the parser dictionnary to allocate all elements and attributes names
1913 */
1914 ret->ctxt->docdict = 1;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001915 ret->dict = ret->ctxt->dict;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001916#ifdef LIBXML_XINCLUDE_ENABLED
1917 ret->xinclude = 0;
1918#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001919#ifdef LIBXML_PATTERN_ENABLED
1920 ret->patternMax = 0;
1921 ret->patternTab = NULL;
1922#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001923 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001924}
1925
1926/**
1927 * xmlNewTextReaderFilename:
1928 * @URI: the URI of the resource to process
1929 *
1930 * Create an xmlTextReader structure fed with the resource at @URI
1931 *
1932 * Returns the new xmlTextReaderPtr or NULL in case of error
1933 */
1934xmlTextReaderPtr
1935xmlNewTextReaderFilename(const char *URI) {
1936 xmlParserInputBufferPtr input;
1937 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001938 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001939
1940 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1941 if (input == NULL)
1942 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001943 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001944 if (ret == NULL) {
1945 xmlFreeParserInputBuffer(input);
1946 return(NULL);
1947 }
1948 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001949 if (ret->ctxt->directory == NULL)
1950 directory = xmlParserGetDirectory(URI);
1951 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1952 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1953 if (directory != NULL)
1954 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001955 return(ret);
1956}
1957
1958/**
1959 * xmlFreeTextReader:
1960 * @reader: the xmlTextReaderPtr
1961 *
1962 * Deallocate all the resources associated to the reader
1963 */
1964void
1965xmlFreeTextReader(xmlTextReaderPtr reader) {
1966 if (reader == NULL)
1967 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001968#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001969 if (reader->rngSchemas != NULL) {
1970 xmlRelaxNGFree(reader->rngSchemas);
1971 reader->rngSchemas = NULL;
1972 }
1973 if (reader->rngValidCtxt != NULL) {
1974 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
1975 reader->rngValidCtxt = NULL;
1976 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001977#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001978#ifdef LIBXML_XINCLUDE_ENABLED
1979 if (reader->xincctxt != NULL)
1980 xmlXIncludeFreeContext(reader->xincctxt);
1981#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001982#ifdef LIBXML_PATTERN_ENABLED
1983 if (reader->patternTab != NULL) {
1984 int i;
1985 for (i = 0;i < reader->patternNr;i++) {
1986 if (reader->patternTab[i] != NULL)
1987 xmlFreePattern(reader->patternTab[i]);
1988 }
1989 xmlFree(reader->patternTab);
1990 }
1991#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001992 if (reader->ctxt != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001993 if (reader->dict == reader->ctxt->dict)
1994 reader->dict = NULL;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001995 if (reader->ctxt->myDoc != NULL) {
1996 if (reader->preserve == 0)
1997 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
1998 reader->ctxt->myDoc = NULL;
1999 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00002000 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
2001 (reader->ctxt->vctxt.vstateMax > 0)){
2002 xmlFree(reader->ctxt->vctxt.vstateTab);
2003 reader->ctxt->vctxt.vstateTab = 0;
2004 reader->ctxt->vctxt.vstateMax = 0;
2005 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002006 if (reader->allocs & XML_TEXTREADER_CTXT)
2007 xmlFreeParserCtxt(reader->ctxt);
2008 }
2009 if (reader->sax != NULL)
2010 xmlFree(reader->sax);
2011 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
2012 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002013 if (reader->faketext != NULL) {
2014 xmlFreeNode(reader->faketext);
2015 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002016 if (reader->buffer != NULL)
2017 xmlBufferFree(reader->buffer);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00002018 if (reader->entTab != NULL)
2019 xmlFree(reader->entTab);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002020 if (reader->dict != NULL)
2021 xmlDictFree(reader->dict);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002022 xmlFree(reader);
2023}
2024
2025/************************************************************************
2026 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002027 * Methods for XmlTextReader *
2028 * *
2029 ************************************************************************/
2030/**
2031 * xmlTextReaderClose:
2032 * @reader: the xmlTextReaderPtr used
2033 *
2034 * This method releases any resources allocated by the current instance
2035 * changes the state to Closed and close any underlying input.
2036 *
2037 * Returns 0 or -1 in case of error
2038 */
2039int
2040xmlTextReaderClose(xmlTextReaderPtr reader) {
2041 if (reader == NULL)
2042 return(-1);
2043 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002044 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002045 reader->mode = XML_TEXTREADER_MODE_CLOSED;
2046 if (reader->ctxt != NULL) {
2047 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002048 if (reader->preserve == 0)
2049 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002050 reader->ctxt->myDoc = NULL;
2051 }
2052 if (reader->allocs & XML_TEXTREADER_CTXT) {
2053 xmlFreeParserCtxt(reader->ctxt);
2054 reader->allocs -= XML_TEXTREADER_CTXT;
2055 }
2056 }
2057 if (reader->sax != NULL) {
2058 xmlFree(reader->sax);
2059 reader->sax = NULL;
2060 }
2061 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
2062 xmlFreeParserInputBuffer(reader->input);
2063 reader->allocs -= XML_TEXTREADER_INPUT;
2064 }
2065 return(0);
2066}
2067
2068/**
2069 * xmlTextReaderGetAttributeNo:
2070 * @reader: the xmlTextReaderPtr used
2071 * @no: the zero-based index of the attribute relative to the containing element
2072 *
2073 * Provides the value of the attribute with the specified index relative
2074 * to the containing element.
2075 *
2076 * Returns a string containing the value of the specified attribute, or NULL
2077 * in case of error. The string must be deallocated by the caller.
2078 */
2079xmlChar *
2080xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
2081 xmlChar *ret;
2082 int i;
2083 xmlAttrPtr cur;
2084 xmlNsPtr ns;
2085
2086 if (reader == NULL)
2087 return(NULL);
2088 if (reader->node == NULL)
2089 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002090 if (reader->curnode != NULL)
2091 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002092 /* TODO: handle the xmlDecl */
2093 if (reader->node->type != XML_ELEMENT_NODE)
2094 return(NULL);
2095
2096 ns = reader->node->nsDef;
2097 for (i = 0;(i < no) && (ns != NULL);i++) {
2098 ns = ns->next;
2099 }
2100 if (ns != NULL)
2101 return(xmlStrdup(ns->href));
2102
2103 cur = reader->node->properties;
2104 if (cur == NULL)
2105 return(NULL);
2106 for (;i < no;i++) {
2107 cur = cur->next;
2108 if (cur == NULL)
2109 return(NULL);
2110 }
2111 /* TODO walk the DTD if present */
2112
2113 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
2114 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
2115 return(ret);
2116}
2117
2118/**
2119 * xmlTextReaderGetAttribute:
2120 * @reader: the xmlTextReaderPtr used
2121 * @name: the qualified name of the attribute.
2122 *
2123 * Provides the value of the attribute with the specified qualified name.
2124 *
2125 * Returns a string containing the value of the specified attribute, or NULL
2126 * in case of error. The string must be deallocated by the caller.
2127 */
2128xmlChar *
2129xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2130 xmlChar *prefix = NULL;
2131 xmlChar *localname;
2132 xmlNsPtr ns;
2133 xmlChar *ret = NULL;
2134
2135 if ((reader == NULL) || (name == NULL))
2136 return(NULL);
2137 if (reader->node == NULL)
2138 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002139 if (reader->curnode != NULL)
2140 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002141
2142 /* TODO: handle the xmlDecl */
2143 if (reader->node->type != XML_ELEMENT_NODE)
2144 return(NULL);
2145
2146 localname = xmlSplitQName2(name, &prefix);
2147 if (localname == NULL)
2148 return(xmlGetProp(reader->node, name));
2149
2150 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2151 if (ns != NULL)
2152 ret = xmlGetNsProp(reader->node, localname, ns->href);
2153
2154 if (localname != NULL)
2155 xmlFree(localname);
2156 if (prefix != NULL)
2157 xmlFree(prefix);
2158 return(ret);
2159}
2160
2161
2162/**
2163 * xmlTextReaderGetAttributeNs:
2164 * @reader: the xmlTextReaderPtr used
2165 * @localName: the local name of the attribute.
2166 * @namespaceURI: the namespace URI of the attribute.
2167 *
2168 * Provides the value of the specified attribute
2169 *
2170 * Returns a string containing the value of the specified attribute, or NULL
2171 * in case of error. The string must be deallocated by the caller.
2172 */
2173xmlChar *
2174xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
2175 const xmlChar *namespaceURI) {
2176 if ((reader == NULL) || (localName == NULL))
2177 return(NULL);
2178 if (reader->node == NULL)
2179 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002180 if (reader->curnode != NULL)
2181 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002182
2183 /* TODO: handle the xmlDecl */
2184 if (reader->node->type != XML_ELEMENT_NODE)
2185 return(NULL);
2186
2187 return(xmlGetNsProp(reader->node, localName, namespaceURI));
2188}
2189
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002190/**
2191 * xmlTextReaderGetRemainder:
2192 * @reader: the xmlTextReaderPtr used
2193 *
2194 * Method to get the remainder of the buffered XML. this method stops the
2195 * parser, set its state to End Of File and return the input stream with
2196 * what is left that the parser did not use.
2197 *
2198 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
2199 * in case of error.
2200 */
2201xmlParserInputBufferPtr
2202xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
2203 xmlParserInputBufferPtr ret = NULL;
2204
2205 if (reader == NULL)
2206 return(NULL);
2207 if (reader->node == NULL)
2208 return(NULL);
2209
2210 reader->node = NULL;
2211 reader->curnode = NULL;
2212 reader->mode = XML_TEXTREADER_MODE_EOF;
2213 if (reader->ctxt != NULL) {
2214 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002215 if (reader->preserve == 0)
2216 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002217 reader->ctxt->myDoc = NULL;
2218 }
2219 if (reader->allocs & XML_TEXTREADER_CTXT) {
2220 xmlFreeParserCtxt(reader->ctxt);
2221 reader->allocs -= XML_TEXTREADER_CTXT;
2222 }
2223 }
2224 if (reader->sax != NULL) {
2225 xmlFree(reader->sax);
2226 reader->sax = NULL;
2227 }
2228 if (reader->allocs & XML_TEXTREADER_INPUT) {
2229 ret = reader->input;
2230 reader->allocs -= XML_TEXTREADER_INPUT;
2231 } else {
2232 /*
2233 * Hum, one may need to duplicate the data structure because
2234 * without reference counting the input may be freed twice:
2235 * - by the layer which allocated it.
2236 * - by the layer to which would have been returned to.
2237 */
2238 TODO
2239 return(NULL);
2240 }
2241 return(ret);
2242}
2243
2244/**
2245 * xmlTextReaderLookupNamespace:
2246 * @reader: the xmlTextReaderPtr used
2247 * @prefix: the prefix whose namespace URI is to be resolved. To return
2248 * the default namespace, specify NULL
2249 *
2250 * Resolves a namespace prefix in the scope of the current element.
2251 *
2252 * Returns a string containing the namespace URI to which the prefix maps
2253 * or NULL in case of error. The string must be deallocated by the caller.
2254 */
2255xmlChar *
2256xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
2257 xmlNsPtr ns;
2258
2259 if (reader == NULL)
2260 return(NULL);
2261 if (reader->node == NULL)
2262 return(NULL);
2263
2264 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2265 if (ns == NULL)
2266 return(NULL);
2267 return(xmlStrdup(ns->href));
2268}
2269
2270/**
2271 * xmlTextReaderMoveToAttributeNo:
2272 * @reader: the xmlTextReaderPtr used
2273 * @no: the zero-based index of the attribute relative to the containing
2274 * element.
2275 *
2276 * Moves the position of the current instance to the attribute with
2277 * the specified index relative to the containing element.
2278 *
2279 * Returns 1 in case of success, -1 in case of error, 0 if not found
2280 */
2281int
2282xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
2283 int i;
2284 xmlAttrPtr cur;
2285 xmlNsPtr ns;
2286
2287 if (reader == NULL)
2288 return(-1);
2289 if (reader->node == NULL)
2290 return(-1);
2291 /* TODO: handle the xmlDecl */
2292 if (reader->node->type != XML_ELEMENT_NODE)
2293 return(-1);
2294
2295 reader->curnode = NULL;
2296
2297 ns = reader->node->nsDef;
2298 for (i = 0;(i < no) && (ns != NULL);i++) {
2299 ns = ns->next;
2300 }
2301 if (ns != NULL) {
2302 reader->curnode = (xmlNodePtr) ns;
2303 return(1);
2304 }
2305
2306 cur = reader->node->properties;
2307 if (cur == NULL)
2308 return(0);
2309 for (;i < no;i++) {
2310 cur = cur->next;
2311 if (cur == NULL)
2312 return(0);
2313 }
2314 /* TODO walk the DTD if present */
2315
2316 reader->curnode = (xmlNodePtr) cur;
2317 return(1);
2318}
2319
2320/**
2321 * xmlTextReaderMoveToAttribute:
2322 * @reader: the xmlTextReaderPtr used
2323 * @name: the qualified name of the attribute.
2324 *
2325 * Moves the position of the current instance to the attribute with
2326 * the specified qualified name.
2327 *
2328 * Returns 1 in case of success, -1 in case of error, 0 if not found
2329 */
2330int
2331xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2332 xmlChar *prefix = NULL;
2333 xmlChar *localname;
2334 xmlNsPtr ns;
2335 xmlAttrPtr prop;
2336
2337 if ((reader == NULL) || (name == NULL))
2338 return(-1);
2339 if (reader->node == NULL)
2340 return(-1);
2341
2342 /* TODO: handle the xmlDecl */
2343 if (reader->node->type != XML_ELEMENT_NODE)
2344 return(0);
2345
2346 localname = xmlSplitQName2(name, &prefix);
2347 if (localname == NULL) {
2348 /*
2349 * Namespace default decl
2350 */
2351 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2352 ns = reader->node->nsDef;
2353 while (ns != NULL) {
2354 if (ns->prefix == NULL) {
2355 reader->curnode = (xmlNodePtr) ns;
2356 return(1);
2357 }
2358 ns = ns->next;
2359 }
2360 return(0);
2361 }
2362
2363 prop = reader->node->properties;
2364 while (prop != NULL) {
2365 /*
2366 * One need to have
2367 * - same attribute names
2368 * - and the attribute carrying that namespace
2369 */
2370 if ((xmlStrEqual(prop->name, name)) &&
2371 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
2372 reader->curnode = (xmlNodePtr) prop;
2373 return(1);
2374 }
2375 prop = prop->next;
2376 }
2377 return(0);
2378 }
2379
2380 /*
2381 * Namespace default decl
2382 */
2383 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2384 ns = reader->node->nsDef;
2385 while (ns != NULL) {
2386 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2387 reader->curnode = (xmlNodePtr) ns;
2388 goto found;
2389 }
2390 ns = ns->next;
2391 }
2392 goto not_found;
2393 }
2394 prop = reader->node->properties;
2395 while (prop != NULL) {
2396 /*
2397 * One need to have
2398 * - same attribute names
2399 * - and the attribute carrying that namespace
2400 */
2401 if ((xmlStrEqual(prop->name, localname)) &&
2402 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2403 reader->curnode = (xmlNodePtr) prop;
2404 goto found;
2405 }
2406 prop = prop->next;
2407 }
2408not_found:
2409 if (localname != NULL)
2410 xmlFree(localname);
2411 if (prefix != NULL)
2412 xmlFree(prefix);
2413 return(0);
2414
2415found:
2416 if (localname != NULL)
2417 xmlFree(localname);
2418 if (prefix != NULL)
2419 xmlFree(prefix);
2420 return(1);
2421}
2422
2423/**
2424 * xmlTextReaderMoveToAttributeNs:
2425 * @reader: the xmlTextReaderPtr used
2426 * @localName: the local name of the attribute.
2427 * @namespaceURI: the namespace URI of the attribute.
2428 *
2429 * Moves the position of the current instance to the attribute with the
2430 * specified local name and namespace URI.
2431 *
2432 * Returns 1 in case of success, -1 in case of error, 0 if not found
2433 */
2434int
2435xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
2436 const xmlChar *localName, const xmlChar *namespaceURI) {
2437 xmlAttrPtr prop;
2438 xmlNodePtr node;
2439
2440 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2441 return(-1);
2442 if (reader->node == NULL)
2443 return(-1);
2444 if (reader->node->type != XML_ELEMENT_NODE)
2445 return(0);
2446 node = reader->node;
2447
2448 /*
2449 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
2450 * namespace name associated to "xmlns"
2451 */
2452 prop = node->properties;
2453 while (prop != NULL) {
2454 /*
2455 * One need to have
2456 * - same attribute names
2457 * - and the attribute carrying that namespace
2458 */
2459 if (xmlStrEqual(prop->name, localName) &&
2460 ((prop->ns != NULL) &&
2461 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2462 reader->curnode = (xmlNodePtr) prop;
2463 return(1);
2464 }
2465 prop = prop->next;
2466 }
2467 return(0);
2468}
2469
2470/**
2471 * xmlTextReaderMoveToFirstAttribute:
2472 * @reader: the xmlTextReaderPtr used
2473 *
2474 * Moves the position of the current instance to the first attribute
2475 * associated with the current node.
2476 *
2477 * Returns 1 in case of success, -1 in case of error, 0 if not found
2478 */
2479int
2480xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
2481 if (reader == NULL)
2482 return(-1);
2483 if (reader->node == NULL)
2484 return(-1);
2485 if (reader->node->type != XML_ELEMENT_NODE)
2486 return(0);
2487
2488 if (reader->node->nsDef != NULL) {
2489 reader->curnode = (xmlNodePtr) reader->node->nsDef;
2490 return(1);
2491 }
2492 if (reader->node->properties != NULL) {
2493 reader->curnode = (xmlNodePtr) reader->node->properties;
2494 return(1);
2495 }
2496 return(0);
2497}
2498
2499/**
2500 * xmlTextReaderMoveToNextAttribute:
2501 * @reader: the xmlTextReaderPtr used
2502 *
2503 * Moves the position of the current instance to the next attribute
2504 * associated with the current node.
2505 *
2506 * Returns 1 in case of success, -1 in case of error, 0 if not found
2507 */
2508int
2509xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
2510 if (reader == NULL)
2511 return(-1);
2512 if (reader->node == NULL)
2513 return(-1);
2514 if (reader->node->type != XML_ELEMENT_NODE)
2515 return(0);
2516 if (reader->curnode == NULL)
2517 return(xmlTextReaderMoveToFirstAttribute(reader));
2518
2519 if (reader->curnode->type == XML_NAMESPACE_DECL) {
2520 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2521 if (ns->next != NULL) {
2522 reader->curnode = (xmlNodePtr) ns->next;
2523 return(1);
2524 }
2525 if (reader->node->properties != NULL) {
2526 reader->curnode = (xmlNodePtr) reader->node->properties;
2527 return(1);
2528 }
2529 return(0);
2530 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2531 (reader->curnode->next != NULL)) {
2532 reader->curnode = reader->curnode->next;
2533 return(1);
2534 }
2535 return(0);
2536}
2537
2538/**
2539 * xmlTextReaderMoveToElement:
2540 * @reader: the xmlTextReaderPtr used
2541 *
2542 * Moves the position of the current instance to the node that
2543 * contains the current Attribute node.
2544 *
2545 * Returns 1 in case of success, -1 in case of error, 0 if not moved
2546 */
2547int
2548xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
2549 if (reader == NULL)
2550 return(-1);
2551 if (reader->node == NULL)
2552 return(-1);
2553 if (reader->node->type != XML_ELEMENT_NODE)
2554 return(0);
2555 if (reader->curnode != NULL) {
2556 reader->curnode = NULL;
2557 return(1);
2558 }
2559 return(0);
2560}
2561
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002562/**
2563 * xmlTextReaderReadAttributeValue:
2564 * @reader: the xmlTextReaderPtr used
2565 *
2566 * Parses an attribute value into one or more Text and EntityReference nodes.
2567 *
2568 * Returns 1 in case of success, 0 if the reader was not positionned on an
2569 * ttribute node or all the attribute values have been read, or -1
2570 * in case of error.
2571 */
2572int
2573xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
2574 if (reader == NULL)
2575 return(-1);
2576 if (reader->node == NULL)
2577 return(-1);
2578 if (reader->curnode == NULL)
2579 return(0);
2580 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2581 if (reader->curnode->children == NULL)
2582 return(0);
2583 reader->curnode = reader->curnode->children;
2584 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2585 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2586
2587 if (reader->faketext == NULL) {
2588 reader->faketext = xmlNewDocText(reader->node->doc,
2589 ns->href);
2590 } else {
2591 if (reader->faketext->content != NULL)
2592 xmlFree(reader->faketext->content);
2593 reader->faketext->content = xmlStrdup(ns->href);
2594 }
2595 reader->curnode = reader->faketext;
2596 } else {
2597 if (reader->curnode->next == NULL)
2598 return(0);
2599 reader->curnode = reader->curnode->next;
2600 }
2601 return(1);
2602}
2603
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002604/************************************************************************
2605 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002606 * Acces API to the current node *
2607 * *
2608 ************************************************************************/
2609/**
2610 * xmlTextReaderAttributeCount:
2611 * @reader: the xmlTextReaderPtr used
2612 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002613 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002614 *
2615 * Returns 0 i no attributes, -1 in case of error or the attribute count
2616 */
2617int
2618xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
2619 int ret;
2620 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00002621 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002622 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002623
2624 if (reader == NULL)
2625 return(-1);
2626 if (reader->node == NULL)
2627 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002628
2629 if (reader->curnode != NULL)
2630 node = reader->curnode;
2631 else
2632 node = reader->node;
2633
2634 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002635 return(0);
2636 if ((reader->state == XML_TEXTREADER_END) ||
2637 (reader->state == XML_TEXTREADER_BACKTRACK))
2638 return(0);
2639 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002640 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002641 while (attr != NULL) {
2642 ret++;
2643 attr = attr->next;
2644 }
Daniel Veillard67df8092002-12-16 22:04:11 +00002645 ns = node->nsDef;
2646 while (ns != NULL) {
2647 ret++;
2648 ns = ns->next;
2649 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002650 return(ret);
2651}
2652
2653/**
2654 * xmlTextReaderNodeType:
2655 * @reader: the xmlTextReaderPtr used
2656 *
2657 * Get the node type of the current node
2658 * Reference:
2659 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
2660 *
2661 * Returns the xmlNodeType of the current node or -1 in case of error
2662 */
2663int
2664xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002665 xmlNodePtr node;
Daniel Veillardd6038e02003-07-30 16:37:18 +00002666
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002667 if (reader == NULL)
2668 return(-1);
2669 if (reader->node == NULL)
Daniel Veillardd6038e02003-07-30 16:37:18 +00002670 return(XML_READER_TYPE_NONE);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002671 if (reader->curnode != NULL)
2672 node = reader->curnode;
2673 else
2674 node = reader->node;
2675 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002676 case XML_ELEMENT_NODE:
2677 if ((reader->state == XML_TEXTREADER_END) ||
2678 (reader->state == XML_TEXTREADER_BACKTRACK))
Daniel Veillardd6038e02003-07-30 16:37:18 +00002679 return(XML_READER_TYPE_END_ELEMENT);
2680 return(XML_READER_TYPE_ELEMENT);
Daniel Veillardecaba492002-12-30 10:55:29 +00002681 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002682 case XML_ATTRIBUTE_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002683 return(XML_READER_TYPE_ATTRIBUTE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002684 case XML_TEXT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002685 if (xmlIsBlankNode(reader->node)) {
2686 if (xmlNodeGetSpacePreserve(reader->node))
2687 return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2688 else
2689 return(XML_READER_TYPE_WHITESPACE);
2690 } else {
2691 return(XML_READER_TYPE_TEXT);
2692 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002693 case XML_CDATA_SECTION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002694 return(XML_READER_TYPE_CDATA);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002695 case XML_ENTITY_REF_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002696 return(XML_READER_TYPE_ENTITY_REFERENCE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002697 case XML_ENTITY_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002698 return(XML_READER_TYPE_ENTITY);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002699 case XML_PI_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002700 return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002701 case XML_COMMENT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002702 return(XML_READER_TYPE_COMMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002703 case XML_DOCUMENT_NODE:
2704 case XML_HTML_DOCUMENT_NODE:
2705#ifdef LIBXML_DOCB_ENABLED
2706 case XML_DOCB_DOCUMENT_NODE:
2707#endif
Daniel Veillardd6038e02003-07-30 16:37:18 +00002708 return(XML_READER_TYPE_DOCUMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002709 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002710 return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002711 case XML_NOTATION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002712 return(XML_READER_TYPE_NOTATION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002713 case XML_DOCUMENT_TYPE_NODE:
2714 case XML_DTD_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002715 return(XML_READER_TYPE_DOCUMENT_TYPE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002716
2717 case XML_ELEMENT_DECL:
2718 case XML_ATTRIBUTE_DECL:
2719 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002720 case XML_XINCLUDE_START:
2721 case XML_XINCLUDE_END:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002722 return(XML_READER_TYPE_NONE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002723 }
2724 return(-1);
2725}
2726
2727/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002728 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002729 * @reader: the xmlTextReaderPtr used
2730 *
2731 * Check if the current node is empty
2732 *
2733 * Returns 1 if empty, 0 if not and -1 in case of error
2734 */
2735int
2736xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
2737 if ((reader == NULL) || (reader->node == NULL))
2738 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00002739 if (reader->node->type != XML_ELEMENT_NODE)
2740 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00002741 if (reader->curnode != NULL)
2742 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002743 if (reader->node->children != NULL)
2744 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00002745 if (reader->state == XML_TEXTREADER_END)
2746 return(0);
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002747 if (reader->doc != NULL)
2748 return(1);
Daniel Veillard96b6cd22004-01-08 16:49:50 +00002749#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002750 if (reader->in_xinclude > 0)
2751 return(1);
Daniel Veillard96b6cd22004-01-08 16:49:50 +00002752#endif
Daniel Veillarde8039df2003-10-27 11:25:13 +00002753 return((reader->node->extra & NODE_IS_EMPTY) != 0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002754}
2755
2756/**
2757 * xmlTextReaderLocalName:
2758 * @reader: the xmlTextReaderPtr used
2759 *
2760 * The local name of the node.
2761 *
2762 * Returns the local name or NULL if not available
2763 */
2764xmlChar *
2765xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002766 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002767 if ((reader == NULL) || (reader->node == NULL))
2768 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002769 if (reader->curnode != NULL)
2770 node = reader->curnode;
2771 else
2772 node = reader->node;
2773 if (node->type == XML_NAMESPACE_DECL) {
2774 xmlNsPtr ns = (xmlNsPtr) node;
2775 if (ns->prefix == NULL)
2776 return(xmlStrdup(BAD_CAST "xmlns"));
2777 else
2778 return(xmlStrdup(ns->prefix));
2779 }
2780 if ((node->type != XML_ELEMENT_NODE) &&
2781 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002782 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002783 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002784}
2785
2786/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002787 * xmlTextReaderConstLocalName:
2788 * @reader: the xmlTextReaderPtr used
2789 *
2790 * The local name of the node.
2791 *
2792 * Returns the local name or NULL if not available, the
2793 * string will be deallocated with the reader.
2794 */
2795const xmlChar *
2796xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
2797 xmlNodePtr node;
2798 if ((reader == NULL) || (reader->node == NULL))
2799 return(NULL);
2800 if (reader->curnode != NULL)
2801 node = reader->curnode;
2802 else
2803 node = reader->node;
2804 if (node->type == XML_NAMESPACE_DECL) {
2805 xmlNsPtr ns = (xmlNsPtr) node;
2806 if (ns->prefix == NULL)
2807 return(CONSTSTR(BAD_CAST "xmlns"));
2808 else
2809 return(ns->prefix);
2810 }
2811 if ((node->type != XML_ELEMENT_NODE) &&
2812 (node->type != XML_ATTRIBUTE_NODE))
2813 return(xmlTextReaderConstName(reader));
2814 return(node->name);
2815}
2816
2817/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002818 * xmlTextReaderName:
2819 * @reader: the xmlTextReaderPtr used
2820 *
2821 * The qualified name of the node, equal to Prefix :LocalName.
2822 *
2823 * Returns the local name or NULL if not available
2824 */
2825xmlChar *
2826xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002827 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002828 xmlChar *ret;
2829
2830 if ((reader == NULL) || (reader->node == NULL))
2831 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002832 if (reader->curnode != NULL)
2833 node = reader->curnode;
2834 else
2835 node = reader->node;
2836 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002837 case XML_ELEMENT_NODE:
2838 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002839 if ((node->ns == NULL) ||
2840 (node->ns->prefix == NULL))
2841 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002842
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002843 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002844 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002845 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002846 return(ret);
2847 case XML_TEXT_NODE:
2848 return(xmlStrdup(BAD_CAST "#text"));
2849 case XML_CDATA_SECTION_NODE:
2850 return(xmlStrdup(BAD_CAST "#cdata-section"));
2851 case XML_ENTITY_NODE:
2852 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002853 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002854 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002855 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002856 case XML_COMMENT_NODE:
2857 return(xmlStrdup(BAD_CAST "#comment"));
2858 case XML_DOCUMENT_NODE:
2859 case XML_HTML_DOCUMENT_NODE:
2860#ifdef LIBXML_DOCB_ENABLED
2861 case XML_DOCB_DOCUMENT_NODE:
2862#endif
2863 return(xmlStrdup(BAD_CAST "#document"));
2864 case XML_DOCUMENT_FRAG_NODE:
2865 return(xmlStrdup(BAD_CAST "#document-fragment"));
2866 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002867 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002868 case XML_DOCUMENT_TYPE_NODE:
2869 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002870 return(xmlStrdup(node->name));
2871 case XML_NAMESPACE_DECL: {
2872 xmlNsPtr ns = (xmlNsPtr) node;
2873
2874 ret = xmlStrdup(BAD_CAST "xmlns");
2875 if (ns->prefix == NULL)
2876 return(ret);
2877 ret = xmlStrcat(ret, BAD_CAST ":");
2878 ret = xmlStrcat(ret, ns->prefix);
2879 return(ret);
2880 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002881
2882 case XML_ELEMENT_DECL:
2883 case XML_ATTRIBUTE_DECL:
2884 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002885 case XML_XINCLUDE_START:
2886 case XML_XINCLUDE_END:
2887 return(NULL);
2888 }
2889 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002890}
2891
2892/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002893 * xmlTextReaderConstName:
2894 * @reader: the xmlTextReaderPtr used
2895 *
2896 * The qualified name of the node, equal to Prefix :LocalName.
2897 *
2898 * Returns the local name or NULL if not available, the string is
2899 * deallocated with the reader.
2900 */
2901const xmlChar *
2902xmlTextReaderConstName(xmlTextReaderPtr reader) {
2903 xmlNodePtr node;
2904
2905 if ((reader == NULL) || (reader->node == NULL))
2906 return(NULL);
2907 if (reader->curnode != NULL)
2908 node = reader->curnode;
2909 else
2910 node = reader->node;
2911 switch (node->type) {
2912 case XML_ELEMENT_NODE:
2913 case XML_ATTRIBUTE_NODE:
2914 if ((node->ns == NULL) ||
2915 (node->ns->prefix == NULL))
2916 return(node->name);
2917 return(CONSTQSTR(node->ns->prefix, node->name));
2918 case XML_TEXT_NODE:
2919 return(CONSTSTR(BAD_CAST "#text"));
2920 case XML_CDATA_SECTION_NODE:
2921 return(CONSTSTR(BAD_CAST "#cdata-section"));
2922 case XML_ENTITY_NODE:
2923 case XML_ENTITY_REF_NODE:
2924 return(CONSTSTR(node->name));
2925 case XML_PI_NODE:
2926 return(CONSTSTR(node->name));
2927 case XML_COMMENT_NODE:
2928 return(CONSTSTR(BAD_CAST "#comment"));
2929 case XML_DOCUMENT_NODE:
2930 case XML_HTML_DOCUMENT_NODE:
2931#ifdef LIBXML_DOCB_ENABLED
2932 case XML_DOCB_DOCUMENT_NODE:
2933#endif
2934 return(CONSTSTR(BAD_CAST "#document"));
2935 case XML_DOCUMENT_FRAG_NODE:
2936 return(CONSTSTR(BAD_CAST "#document-fragment"));
2937 case XML_NOTATION_NODE:
2938 return(CONSTSTR(node->name));
2939 case XML_DOCUMENT_TYPE_NODE:
2940 case XML_DTD_NODE:
2941 return(CONSTSTR(node->name));
2942 case XML_NAMESPACE_DECL: {
2943 xmlNsPtr ns = (xmlNsPtr) node;
2944
2945 if (ns->prefix == NULL)
2946 return(CONSTSTR(BAD_CAST "xmlns"));
2947 return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
2948 }
2949
2950 case XML_ELEMENT_DECL:
2951 case XML_ATTRIBUTE_DECL:
2952 case XML_ENTITY_DECL:
2953 case XML_XINCLUDE_START:
2954 case XML_XINCLUDE_END:
2955 return(NULL);
2956 }
2957 return(NULL);
2958}
2959
2960/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002961 * xmlTextReaderPrefix:
2962 * @reader: the xmlTextReaderPtr used
2963 *
2964 * A shorthand reference to the namespace associated with the node.
2965 *
2966 * Returns the prefix or NULL if not available
2967 */
2968xmlChar *
2969xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002970 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002971 if ((reader == NULL) || (reader->node == NULL))
2972 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002973 if (reader->curnode != NULL)
2974 node = reader->curnode;
2975 else
2976 node = reader->node;
2977 if (node->type == XML_NAMESPACE_DECL) {
2978 xmlNsPtr ns = (xmlNsPtr) node;
2979 if (ns->prefix == NULL)
2980 return(NULL);
2981 return(xmlStrdup(BAD_CAST "xmlns"));
2982 }
2983 if ((node->type != XML_ELEMENT_NODE) &&
2984 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002985 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00002986 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002987 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002988 return(NULL);
2989}
2990
2991/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002992 * xmlTextReaderConstPrefix:
2993 * @reader: the xmlTextReaderPtr used
2994 *
2995 * A shorthand reference to the namespace associated with the node.
2996 *
2997 * Returns the prefix or NULL if not available, the string is deallocated
2998 * with the reader.
2999 */
3000const xmlChar *
3001xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
3002 xmlNodePtr node;
3003 if ((reader == NULL) || (reader->node == NULL))
3004 return(NULL);
3005 if (reader->curnode != NULL)
3006 node = reader->curnode;
3007 else
3008 node = reader->node;
3009 if (node->type == XML_NAMESPACE_DECL) {
3010 xmlNsPtr ns = (xmlNsPtr) node;
3011 if (ns->prefix == NULL)
3012 return(NULL);
3013 return(CONSTSTR(BAD_CAST "xmlns"));
3014 }
3015 if ((node->type != XML_ELEMENT_NODE) &&
3016 (node->type != XML_ATTRIBUTE_NODE))
3017 return(NULL);
3018 if ((node->ns != NULL) && (node->ns->prefix != NULL))
3019 return(CONSTSTR(node->ns->prefix));
3020 return(NULL);
3021}
3022
3023/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003024 * xmlTextReaderNamespaceUri:
3025 * @reader: the xmlTextReaderPtr used
3026 *
3027 * The URI defining the namespace associated with the node.
3028 *
3029 * Returns the namespace URI or NULL if not available
3030 */
3031xmlChar *
3032xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003033 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003034 if ((reader == NULL) || (reader->node == NULL))
3035 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003036 if (reader->curnode != NULL)
3037 node = reader->curnode;
3038 else
3039 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00003040 if (node->type == XML_NAMESPACE_DECL)
3041 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003042 if ((node->type != XML_ELEMENT_NODE) &&
3043 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003044 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003045 if (node->ns != NULL)
3046 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003047 return(NULL);
3048}
3049
3050/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003051 * xmlTextReaderConstNamespaceUri:
3052 * @reader: the xmlTextReaderPtr used
3053 *
3054 * The URI defining the namespace associated with the node.
3055 *
3056 * Returns the namespace URI or NULL if not available, the string
3057 * will be deallocated with the reader
3058 */
3059const xmlChar *
3060xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
3061 xmlNodePtr node;
3062 if ((reader == NULL) || (reader->node == NULL))
3063 return(NULL);
3064 if (reader->curnode != NULL)
3065 node = reader->curnode;
3066 else
3067 node = reader->node;
3068 if (node->type == XML_NAMESPACE_DECL)
3069 return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
3070 if ((node->type != XML_ELEMENT_NODE) &&
3071 (node->type != XML_ATTRIBUTE_NODE))
3072 return(NULL);
3073 if (node->ns != NULL)
3074 return(CONSTSTR(node->ns->href));
3075 return(NULL);
3076}
3077
3078/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003079 * xmlTextReaderBaseUri:
3080 * @reader: the xmlTextReaderPtr used
3081 *
3082 * The base URI of the node.
3083 *
3084 * Returns the base URI or NULL if not available
3085 */
3086xmlChar *
3087xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
3088 if ((reader == NULL) || (reader->node == NULL))
3089 return(NULL);
3090 return(xmlNodeGetBase(NULL, reader->node));
3091}
3092
3093/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003094 * xmlTextReaderConstBaseUri:
3095 * @reader: the xmlTextReaderPtr used
3096 *
3097 * The base URI of the node.
3098 *
3099 * Returns the base URI or NULL if not available, the string
3100 * will be deallocated with the reader
3101 */
3102const xmlChar *
3103xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
3104 xmlChar *tmp;
3105 const xmlChar *ret;
3106
3107 if ((reader == NULL) || (reader->node == NULL))
3108 return(NULL);
3109 tmp = xmlNodeGetBase(NULL, reader->node);
3110 if (tmp == NULL)
3111 return(NULL);
3112 ret = CONSTSTR(tmp);
3113 xmlFree(tmp);
3114 return(ret);
3115}
3116
3117/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003118 * xmlTextReaderDepth:
3119 * @reader: the xmlTextReaderPtr used
3120 *
3121 * The depth of the node in the tree.
3122 *
3123 * Returns the depth or -1 in case of error
3124 */
3125int
3126xmlTextReaderDepth(xmlTextReaderPtr reader) {
3127 if (reader == NULL)
3128 return(-1);
3129 if (reader->node == NULL)
3130 return(0);
3131
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003132 if (reader->curnode != NULL) {
3133 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
3134 (reader->curnode->type == XML_NAMESPACE_DECL))
3135 return(reader->depth + 1);
3136 return(reader->depth + 2);
3137 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003138 return(reader->depth);
3139}
3140
3141/**
3142 * xmlTextReaderHasAttributes:
3143 * @reader: the xmlTextReaderPtr used
3144 *
3145 * Whether the node has attributes.
3146 *
3147 * Returns 1 if true, 0 if false, and -1 in case or error
3148 */
3149int
3150xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003151 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003152 if (reader == NULL)
3153 return(-1);
3154 if (reader->node == NULL)
3155 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003156 if (reader->curnode != NULL)
3157 node = reader->curnode;
3158 else
3159 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003160
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003161 if ((node->type == XML_ELEMENT_NODE) &&
3162 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003163 return(1);
3164 /* TODO: handle the xmlDecl */
3165 return(0);
3166}
3167
3168/**
3169 * xmlTextReaderHasValue:
3170 * @reader: the xmlTextReaderPtr used
3171 *
3172 * Whether the node can have a text value.
3173 *
3174 * Returns 1 if true, 0 if false, and -1 in case or error
3175 */
3176int
3177xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003178 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003179 if (reader == NULL)
3180 return(-1);
3181 if (reader->node == NULL)
3182 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003183 if (reader->curnode != NULL)
3184 node = reader->curnode;
3185 else
3186 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003187
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003188 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003189 case XML_ATTRIBUTE_NODE:
3190 case XML_TEXT_NODE:
3191 case XML_CDATA_SECTION_NODE:
3192 case XML_PI_NODE:
3193 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00003194 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003195 return(1);
3196 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003197 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003198 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003199 return(0);
3200}
3201
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003202/**
3203 * xmlTextReaderValue:
3204 * @reader: the xmlTextReaderPtr used
3205 *
3206 * Provides the text value of the node if present
3207 *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003208 * Returns the string or NULL if not available. The result must be deallocated
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003209 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003210 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003211xmlChar *
3212xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003213 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003214 if (reader == NULL)
3215 return(NULL);
3216 if (reader->node == NULL)
3217 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003218 if (reader->curnode != NULL)
3219 node = reader->curnode;
3220 else
3221 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003222
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003223 switch (node->type) {
3224 case XML_NAMESPACE_DECL:
3225 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003226 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003227 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003228
3229 if (attr->parent != NULL)
3230 return (xmlNodeListGetString
3231 (attr->parent->doc, attr->children, 1));
3232 else
3233 return (xmlNodeListGetString(NULL, attr->children, 1));
3234 break;
3235 }
3236 case XML_TEXT_NODE:
3237 case XML_CDATA_SECTION_NODE:
3238 case XML_PI_NODE:
3239 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003240 if (node->content != NULL)
3241 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003242 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003243 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003244 }
3245 return(NULL);
3246}
3247
3248/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003249 * xmlTextReaderConstValue:
3250 * @reader: the xmlTextReaderPtr used
3251 *
3252 * Provides the text value of the node if present
3253 *
3254 * Returns the string or NULL if not available. The result will be
3255 * deallocated on the next Read() operation.
3256 */
3257const xmlChar *
3258xmlTextReaderConstValue(xmlTextReaderPtr reader) {
3259 xmlNodePtr node;
3260 if (reader == NULL)
3261 return(NULL);
3262 if (reader->node == NULL)
3263 return(NULL);
3264 if (reader->curnode != NULL)
3265 node = reader->curnode;
3266 else
3267 node = reader->node;
3268
3269 switch (node->type) {
3270 case XML_NAMESPACE_DECL:
3271 return(((xmlNsPtr) node)->href);
3272 case XML_ATTRIBUTE_NODE:{
3273 xmlAttrPtr attr = (xmlAttrPtr) node;
3274
3275 if ((attr->children != NULL) &&
3276 (attr->children->type == XML_TEXT_NODE) &&
3277 (attr->children->next == NULL))
3278 return(attr->children->content);
3279 else {
3280 reader->buffer->use = 0;
3281 xmlNodeBufGetContent(reader->buffer, node);
3282 return(reader->buffer->content);
3283 }
3284 break;
3285 }
3286 case XML_TEXT_NODE:
3287 case XML_CDATA_SECTION_NODE:
3288 case XML_PI_NODE:
3289 case XML_COMMENT_NODE:
3290 return(node->content);
3291 default:
3292 break;
3293 }
3294 return(NULL);
3295}
3296
3297/**
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003298 * xmlTextReaderIsDefault:
3299 * @reader: the xmlTextReaderPtr used
3300 *
3301 * Whether an Attribute node was generated from the default value
3302 * defined in the DTD or schema.
3303 *
3304 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
3305 */
3306int
3307xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
3308 if (reader == NULL)
3309 return(-1);
3310 return(0);
3311}
3312
3313/**
3314 * xmlTextReaderQuoteChar:
3315 * @reader: the xmlTextReaderPtr used
3316 *
3317 * The quotation mark character used to enclose the value of an attribute.
3318 *
3319 * Returns " or ' and -1 in case of error
3320 */
3321int
3322xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
3323 if (reader == NULL)
3324 return(-1);
3325 /* TODO maybe lookup the attribute value for " first */
3326 return((int) '"');
3327}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003328
3329/**
3330 * xmlTextReaderXmlLang:
3331 * @reader: the xmlTextReaderPtr used
3332 *
3333 * The xml:lang scope within which the node resides.
3334 *
3335 * Returns the xml:lang value or NULL if none exists.
3336 */
3337xmlChar *
3338xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
3339 if (reader == NULL)
3340 return(NULL);
3341 if (reader->node == NULL)
3342 return(NULL);
3343 return(xmlNodeGetLang(reader->node));
3344}
3345
Daniel Veillard67df8092002-12-16 22:04:11 +00003346/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00003347 * xmlTextReaderConstXmlLang:
Daniel Veillarde72c5082003-09-19 12:44:05 +00003348 * @reader: the xmlTextReaderPtr used
3349 *
3350 * The xml:lang scope within which the node resides.
3351 *
3352 * Returns the xml:lang value or NULL if none exists.
3353 */
3354const xmlChar *
3355xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
3356 xmlChar *tmp;
3357 const xmlChar *ret;
3358
3359 if (reader == NULL)
3360 return(NULL);
3361 if (reader->node == NULL)
3362 return(NULL);
3363 tmp = xmlNodeGetLang(reader->node);
3364 if (tmp == NULL)
3365 return(NULL);
3366 ret = CONSTSTR(tmp);
3367 xmlFree(tmp);
3368 return(ret);
3369}
3370
3371/**
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00003372 * xmlTextReaderConstString:
3373 * @reader: the xmlTextReaderPtr used
3374 * @str: the string to intern.
3375 *
3376 * Get an interned string from the reader, allows for example to
3377 * speedup string name comparisons
3378 *
3379 * Returns an interned copy of the string or NULL in case of error. The
3380 * string will be deallocated with the reader.
3381 */
3382const xmlChar *
3383xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
3384 if (reader == NULL)
3385 return(NULL);
3386 return(CONSTSTR(str));
3387}
3388
3389/**
Daniel Veillard67df8092002-12-16 22:04:11 +00003390 * xmlTextReaderNormalization:
3391 * @reader: the xmlTextReaderPtr used
3392 *
3393 * The value indicating whether to normalize white space and attribute values.
3394 * Since attribute value and end of line normalizations are a MUST in the XML
3395 * specification only the value true is accepted. The broken bahaviour of
3396 * accepting out of range character entities like &#0; is of course not
3397 * supported either.
3398 *
3399 * Returns 1 or -1 in case of error.
3400 */
3401int
3402xmlTextReaderNormalization(xmlTextReaderPtr reader) {
3403 if (reader == NULL)
3404 return(-1);
3405 return(1);
3406}
3407
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003408/************************************************************************
3409 * *
3410 * Extensions to the base APIs *
3411 * *
3412 ************************************************************************/
3413
3414/**
3415 * xmlTextReaderSetParserProp:
3416 * @reader: the xmlTextReaderPtr used
3417 * @prop: the xmlParserProperties to set
3418 * @value: usually 0 or 1 to (de)activate it
3419 *
3420 * Change the parser processing behaviour by changing some of its internal
3421 * properties. Note that some properties can only be changed before any
3422 * read has been done.
3423 *
3424 * Returns 0 if the call was successful, or -1 in case of error
3425 */
3426int
3427xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
3428 xmlParserProperties p = (xmlParserProperties) prop;
3429 xmlParserCtxtPtr ctxt;
3430
3431 if ((reader == NULL) || (reader->ctxt == NULL))
3432 return(-1);
3433 ctxt = reader->ctxt;
3434
3435 switch (p) {
3436 case XML_PARSER_LOADDTD:
3437 if (value != 0) {
3438 if (ctxt->loadsubset == 0) {
3439 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3440 return(-1);
3441 ctxt->loadsubset = XML_DETECT_IDS;
3442 }
3443 } else {
3444 ctxt->loadsubset = 0;
3445 }
3446 return(0);
3447 case XML_PARSER_DEFAULTATTRS:
3448 if (value != 0) {
3449 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
3450 } else {
3451 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3452 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
3453 }
3454 return(0);
3455 case XML_PARSER_VALIDATE:
3456 if (value != 0) {
3457 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00003458 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003459 } else {
3460 ctxt->validate = 0;
3461 }
3462 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003463 case XML_PARSER_SUBST_ENTITIES:
3464 if (value != 0) {
3465 ctxt->replaceEntities = 1;
3466 } else {
3467 ctxt->replaceEntities = 0;
3468 }
3469 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003470 }
3471 return(-1);
3472}
3473
3474/**
3475 * xmlTextReaderGetParserProp:
3476 * @reader: the xmlTextReaderPtr used
3477 * @prop: the xmlParserProperties to get
3478 *
3479 * Read the parser internal property.
3480 *
3481 * Returns the value, usually 0 or 1, or -1 in case of error.
3482 */
3483int
3484xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
3485 xmlParserProperties p = (xmlParserProperties) prop;
3486 xmlParserCtxtPtr ctxt;
3487
3488 if ((reader == NULL) || (reader->ctxt == NULL))
3489 return(-1);
3490 ctxt = reader->ctxt;
3491
3492 switch (p) {
3493 case XML_PARSER_LOADDTD:
3494 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
3495 return(1);
3496 return(0);
3497 case XML_PARSER_DEFAULTATTRS:
3498 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3499 return(1);
3500 return(0);
3501 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00003502 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003503 case XML_PARSER_SUBST_ENTITIES:
3504 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003505 }
3506 return(-1);
3507}
3508
Daniel Veillarde18fc182002-12-28 22:56:33 +00003509/**
3510 * xmlTextReaderCurrentNode:
3511 * @reader: the xmlTextReaderPtr used
3512 *
3513 * Hacking interface allowing to get the xmlNodePtr correponding to the
3514 * current node being accessed by the xmlTextReader. This is dangerous
3515 * because the underlying node may be destroyed on the next Reads.
3516 *
3517 * Returns the xmlNodePtr or NULL in case of error.
3518 */
3519xmlNodePtr
3520xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
3521 if (reader == NULL)
3522 return(NULL);
3523
3524 if (reader->curnode != NULL)
3525 return(reader->curnode);
3526 return(reader->node);
3527}
3528
3529/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003530 * xmlTextReaderPreserve:
3531 * @reader: the xmlTextReaderPtr used
3532 *
3533 *
3534 * current node being accessed by the xmlTextReader. This is dangerous
3535 * because the underlying node may be destroyed on the next Reads.
3536 *
3537 * Returns the xmlNodePtr or NULL in case of error.
3538 */
3539xmlNodePtr
3540xmlTextReaderPreserve(xmlTextReaderPtr reader) {
3541 xmlNodePtr cur, parent;
3542
3543 if (reader == NULL)
3544 return(NULL);
3545
3546 if (reader->curnode != NULL)
3547 cur = reader->curnode;
3548 else
3549 cur = reader->node;
3550 if (cur == NULL)
3551 return(NULL);
Daniel Veillard1e906612003-12-05 14:57:46 +00003552
3553 if (cur->type != XML_DOCUMENT_NODE) {
3554 cur->extra |= NODE_IS_PRESERVED;
3555 cur->extra |= NODE_IS_SPRESERVED;
3556 }
3557 reader->preserves++;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003558
3559 parent = cur->parent;;
3560 while (parent != NULL) {
Daniel Veillard1e906612003-12-05 14:57:46 +00003561 if (parent->type == XML_ELEMENT_NODE)
3562 parent->extra |= NODE_IS_PRESERVED;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003563 parent = parent->parent;
3564 }
3565 return(cur);
3566}
3567
Daniel Veillard1e906612003-12-05 14:57:46 +00003568#ifdef LIBXML_PATTERN_ENABLED
3569/**
3570 * xmlTextReaderPreservePattern:
3571 * @reader: the xmlTextReaderPtr used
3572 * @pattern: an XPath subset pattern
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003573 * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
Daniel Veillard1e906612003-12-05 14:57:46 +00003574 *
3575 * This tells the XML Reader to preserve all nodes matched by the
3576 * pattern. The caller must also use xmlTextReaderCurrentDoc() to
3577 * keep an handle on the resulting document once parsing has finished
3578 *
3579 * Returns a positive number in case of success and -1 in case of error
3580 */
3581int
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003582xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern,
3583 const xmlChar **namespaces)
3584{
Daniel Veillard1e906612003-12-05 14:57:46 +00003585 xmlPatternPtr comp;
3586
3587 if ((reader == NULL) || (pattern == NULL))
3588 return(-1);
3589
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003590 comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
Daniel Veillard1e906612003-12-05 14:57:46 +00003591 if (comp == NULL)
3592 return(-1);
3593
3594 if (reader->patternMax <= 0) {
3595 reader->patternMax = 4;
3596 reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax *
3597 sizeof(reader->patternTab[0]));
3598 if (reader->patternTab == NULL) {
3599 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3600 return (-1);
3601 }
3602 }
3603 if (reader->patternNr >= reader->patternMax) {
3604 xmlPatternPtr *tmp;
3605 reader->patternMax *= 2;
3606 tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab,
3607 reader->patternMax *
3608 sizeof(reader->patternTab[0]));
3609 if (tmp == NULL) {
3610 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3611 reader->patternMax /= 2;
3612 return (-1);
3613 }
3614 reader->patternTab = tmp;
3615 }
3616 reader->patternTab[reader->patternNr] = comp;
3617 return(reader->patternNr++);
3618}
3619#endif
3620
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003621/**
Daniel Veillarde18fc182002-12-28 22:56:33 +00003622 * xmlTextReaderCurrentDoc:
3623 * @reader: the xmlTextReaderPtr used
3624 *
3625 * Hacking interface allowing to get the xmlDocPtr correponding to the
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003626 * current document being accessed by the xmlTextReader.
3627 * NOTE: as a result of this call, the reader will not destroy the
3628 * associated XML document and calling xmlFreeDoc() on the result
3629 * is needed once the reader parsing has finished.
Daniel Veillarde18fc182002-12-28 22:56:33 +00003630 *
3631 * Returns the xmlDocPtr or NULL in case of error.
3632 */
3633xmlDocPtr
3634xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003635 if (reader == NULL)
3636 return(NULL);
3637 if (reader->doc != NULL)
3638 return(reader->doc);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003639 if ((reader == NULL) || (reader->ctxt == NULL) ||
3640 (reader->ctxt->myDoc == NULL))
Daniel Veillarde18fc182002-12-28 22:56:33 +00003641 return(NULL);
3642
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003643 reader->preserve = 1;
3644 if ((reader->ctxt->myDoc->dict != NULL) &&
3645 (reader->ctxt->myDoc->dict == reader->ctxt->dict))
3646 xmlDictReference(reader->ctxt->dict);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003647 return(reader->ctxt->myDoc);
3648}
3649
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003650#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00003651/**
Daniel Veillard33300b42003-04-17 09:09:19 +00003652 * xmlTextReaderRelaxNGSetSchema:
3653 * @reader: the xmlTextReaderPtr used
3654 * @schema: a precompiled RelaxNG schema
3655 *
3656 * Use RelaxNG to validate the document as it is processed.
3657 * Activation is only possible before the first Read().
3658 * if @schema is NULL, then RelaxNG validation is desactivated.
3659 @ The @schema should not be freed until the reader is deallocated
3660 * or its use has been deactivated.
3661 *
3662 * Returns 0 in case the RelaxNG validation could be (des)activated and
3663 * -1 in case of error.
3664 */
3665int
3666xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
3667 if (schema == NULL) {
3668 if (reader->rngSchemas != NULL) {
3669 xmlRelaxNGFree(reader->rngSchemas);
3670 reader->rngSchemas = NULL;
3671 }
3672 if (reader->rngValidCtxt != NULL) {
3673 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3674 reader->rngValidCtxt = NULL;
3675 }
3676 return(0);
3677 }
3678 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3679 return(-1);
3680 if (reader->rngSchemas != NULL) {
3681 xmlRelaxNGFree(reader->rngSchemas);
3682 reader->rngSchemas = NULL;
3683 }
3684 if (reader->rngValidCtxt != NULL) {
3685 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3686 reader->rngValidCtxt = NULL;
3687 }
3688 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
3689 if (reader->rngValidCtxt == NULL)
3690 return(-1);
3691 if (reader->errorFunc != NULL) {
3692 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3693 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3694 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3695 reader->errorFuncArg);
3696 }
3697 reader->rngValidErrors = 0;
3698 reader->rngFullNode = NULL;
3699 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3700 return(0);
3701}
3702
3703/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00003704 * xmlTextReaderRelaxNGValidate:
3705 * @reader: the xmlTextReaderPtr used
3706 * @rng: the path to a RelaxNG schema or NULL
3707 *
3708 * Use RelaxNG to validate the document as it is processed.
3709 * Activation is only possible before the first Read().
3710 * if @rng is NULL, then RelaxNG validation is desactivated.
3711 *
3712 * Returns 0 in case the RelaxNG validation could be (des)activated and
3713 * -1 in case of error.
3714 */
3715int
3716xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
3717 xmlRelaxNGParserCtxtPtr ctxt;
3718
3719 if (reader == NULL)
3720 return(-1);
3721
3722 if (rng == NULL) {
3723 if (reader->rngSchemas != NULL) {
3724 xmlRelaxNGFree(reader->rngSchemas);
3725 reader->rngSchemas = NULL;
3726 }
3727 if (reader->rngValidCtxt != NULL) {
3728 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3729 reader->rngValidCtxt = NULL;
3730 }
3731 return(0);
3732 }
3733 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3734 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00003735 if (reader->rngSchemas != NULL) {
3736 xmlRelaxNGFree(reader->rngSchemas);
3737 reader->rngSchemas = NULL;
3738 }
3739 if (reader->rngValidCtxt != NULL) {
3740 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3741 reader->rngValidCtxt = NULL;
3742 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00003743 ctxt = xmlRelaxNGNewParserCtxt(rng);
3744 if (reader->errorFunc != NULL) {
3745 xmlRelaxNGSetParserErrors(ctxt,
3746 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
3747 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3748 reader->errorFuncArg);
3749 }
3750 reader->rngSchemas = xmlRelaxNGParse(ctxt);
3751 xmlRelaxNGFreeParserCtxt(ctxt);
3752 if (reader->rngSchemas == NULL)
3753 return(-1);
3754 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
3755 if (reader->rngValidCtxt == NULL)
3756 return(-1);
3757 if (reader->errorFunc != NULL) {
3758 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3759 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3760 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3761 reader->errorFuncArg);
3762 }
3763 reader->rngValidErrors = 0;
3764 reader->rngFullNode = NULL;
3765 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3766 return(0);
3767}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003768#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00003769
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003770/************************************************************************
3771 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00003772 * Error Handling Extensions *
3773 * *
3774 ************************************************************************/
3775
3776/* helper to build a xmlMalloc'ed string from a format and va_list */
3777static char *
3778xmlTextReaderBuildMessage(const char *msg, va_list ap) {
3779 int size;
3780 int chars;
3781 char *larger;
3782 char *str;
3783
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003784 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00003785 if (str == NULL) {
3786 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3787 return NULL;
3788 }
3789
3790 size = 150;
3791
3792 while (1) {
3793 chars = vsnprintf(str, size, msg, ap);
3794 if ((chars > -1) && (chars < size))
3795 break;
3796 if (chars > -1)
3797 size += chars + 1;
3798 else
3799 size += 100;
3800 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
3801 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3802 xmlFree(str);
3803 return NULL;
3804 }
3805 str = larger;
3806 }
3807
3808 return str;
3809}
3810
Daniel Veillard417be3a2003-01-20 21:26:34 +00003811/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003812 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003813 * @locator: the xmlTextReaderLocatorPtr used
3814 *
3815 * Obtain the line number for the given locator.
3816 *
3817 * Returns the line number or -1 in case of error.
3818 */
3819int
3820xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
3821 /* we know that locator is a xmlParserCtxtPtr */
3822 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3823 int ret = -1;
3824
3825 if (ctx->node != NULL) {
3826 ret = xmlGetLineNo(ctx->node);
3827 }
3828 else {
3829 /* inspired from error.c */
3830 xmlParserInputPtr input;
3831 input = ctx->input;
3832 if ((input->filename == NULL) && (ctx->inputNr > 1))
3833 input = ctx->inputTab[ctx->inputNr - 2];
3834 if (input != NULL) {
3835 ret = input->line;
3836 }
3837 else {
3838 ret = -1;
3839 }
3840 }
3841
3842 return ret;
3843}
3844
3845/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003846 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003847 * @locator: the xmlTextReaderLocatorPtr used
3848 *
3849 * Obtain the base URI for the given locator.
3850 *
3851 * Returns the base URI or NULL in case of error.
3852 */
3853xmlChar *
3854xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
3855 /* we know that locator is a xmlParserCtxtPtr */
3856 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3857 xmlChar *ret = NULL;
3858
3859 if (ctx->node != NULL) {
3860 ret = xmlNodeGetBase(NULL,ctx->node);
3861 }
3862 else {
3863 /* inspired from error.c */
3864 xmlParserInputPtr input;
3865 input = ctx->input;
3866 if ((input->filename == NULL) && (ctx->inputNr > 1))
3867 input = ctx->inputTab[ctx->inputNr - 2];
3868 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00003869 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00003870 }
3871 else {
3872 ret = NULL;
3873 }
3874 }
3875
3876 return ret;
3877}
3878
Daniel Veillard26f70262003-01-16 22:45:08 +00003879static void
William M. Brack899e64a2003-09-26 18:03:42 +00003880xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity, char *str) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003881 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
3882 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
3883
3884 if (str != NULL) {
3885 reader->errorFunc(reader->errorFuncArg,
3886 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003887 severity,
3888 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00003889 xmlFree(str);
3890 }
3891}
3892
3893static void
3894xmlTextReaderError(void *ctxt, const char *msg, ...) {
3895 va_list ap;
3896
3897 va_start(ap,msg);
3898 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003899 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00003900 xmlTextReaderBuildMessage(msg,ap));
3901 va_end(ap);
3902
3903}
3904
3905static void
3906xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
3907 va_list ap;
3908
3909 va_start(ap,msg);
3910 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003911 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00003912 xmlTextReaderBuildMessage(msg,ap));
3913 va_end(ap);
3914}
3915
3916static void
3917xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
3918 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003919 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003920
Daniel Veillard417be3a2003-01-20 21:26:34 +00003921 if ((len > 1) && (msg[len - 2] != ':')) {
3922 /*
3923 * some callbacks only report locator information:
3924 * skip them (mimicking behaviour in error.c)
3925 */
3926 va_start(ap,msg);
3927 xmlTextReaderGenericError(ctxt,
3928 XML_PARSER_SEVERITY_VALIDITY_ERROR,
3929 xmlTextReaderBuildMessage(msg,ap));
3930 va_end(ap);
3931 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003932}
3933
3934static void
3935xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
3936 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003937 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003938
Daniel Veillard417be3a2003-01-20 21:26:34 +00003939 if ((len != 0) && (msg[len - 1] != ':')) {
3940 /*
3941 * some callbacks only report locator information:
3942 * skip them (mimicking behaviour in error.c)
3943 */
3944 va_start(ap,msg);
3945 xmlTextReaderGenericError(ctxt,
3946 XML_PARSER_SEVERITY_VALIDITY_WARNING,
3947 xmlTextReaderBuildMessage(msg,ap));
3948 va_end(ap);
3949 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003950}
3951
3952/**
3953 * xmlTextReaderSetErrorHandler:
3954 * @reader: the xmlTextReaderPtr used
3955 * @f: the callback function to call on error and warnings
3956 * @arg: a user argument to pass to the callback function
3957 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00003958 * Register a callback function that will be called on error and warnings.
3959 *
Daniel Veillard26f70262003-01-16 22:45:08 +00003960 * If @f is NULL, the default error and warning handlers are restored.
3961 */
3962void
3963xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
3964 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003965 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003966 if (f != NULL) {
3967 reader->ctxt->sax->error = xmlTextReaderError;
3968 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
3969 reader->ctxt->sax->warning = xmlTextReaderWarning;
3970 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
3971 reader->errorFunc = f;
3972 reader->errorFuncArg = arg;
3973 }
3974 else {
3975 /* restore defaults */
3976 reader->ctxt->sax->error = xmlParserError;
3977 reader->ctxt->vctxt.error = xmlParserValidityError;
3978 reader->ctxt->sax->warning = xmlParserWarning;
3979 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
3980 reader->errorFunc = NULL;
3981 reader->errorFuncArg = NULL;
3982 }
3983}
3984
Daniel Veillard417be3a2003-01-20 21:26:34 +00003985/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00003986 * xmlTextReaderIsValid:
3987 * @reader: the xmlTextReaderPtr used
3988 *
3989 * Retrieve the validity status from the parser context
3990 *
3991 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
3992 */
3993int
3994xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00003995 if (reader == NULL) return(-1);
3996#ifdef LIBXML_SCHEMAS_ENABLED
3997 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
3998 return(reader->rngValidErrors == 0);
3999#endif
Daniel Veillard16ed5972003-11-20 18:22:31 +00004000 if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
Daniel Veillardf4e55762003-04-15 23:32:22 +00004001 return(reader->ctxt->valid);
4002 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00004003}
4004
4005/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00004006 * xmlTextReaderGetErrorHandler:
4007 * @reader: the xmlTextReaderPtr used
4008 * @f: the callback function or NULL is no callback has been registered
4009 * @arg: a user argument
4010 *
4011 * Retrieve the error callback function and user argument.
4012 */
Daniel Veillard26f70262003-01-16 22:45:08 +00004013void
4014xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
4015 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00004016 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00004017 *f = reader->errorFunc;
4018 *arg = reader->errorFuncArg;
4019}
4020
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004021
4022/************************************************************************
4023 * *
4024 * New set (2.6.0) of simpler and more flexible APIs *
4025 * *
4026 ************************************************************************/
4027
4028/**
4029 * xmlTextReaderSetup:
4030 * @reader: an XML reader
4031 * @URL: the base URL to use for the document
4032 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004033 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004034 * @reuse: keep the context for reuse
4035 *
4036 * Setup an XML reader with new options
4037 *
4038 * Returns 0 in case of success and -1 in case of error.
4039 */
4040static int
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004041xmlTextReaderSetup(xmlTextReaderPtr reader,
4042 xmlParserInputBufferPtr input, const char *URL,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004043 const char *encoding, int options)
4044{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004045 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004046 return (-1);
4047
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004048 reader->doc = NULL;
4049 reader->entNr = 0;
Daniel Veillarde74d2e12003-12-09 11:35:37 +00004050 reader->parserFlags = options;
Daniel Veillardc36965d2003-12-02 10:28:48 +00004051 reader->validate = XML_TEXTREADER_NOT_VALIDATE;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004052 if ((input != NULL) && (reader->input != NULL) &&
4053 (reader->allocs & XML_TEXTREADER_INPUT)) {
4054 xmlFreeParserInputBuffer(reader->input);
4055 reader->input = NULL;
4056 reader->allocs -= XML_TEXTREADER_INPUT;
4057 }
4058 if (input != NULL) {
4059 reader->input = input;
4060 reader->allocs |= XML_TEXTREADER_INPUT;
4061 }
4062 if (reader->buffer == NULL)
4063 reader->buffer = xmlBufferCreateSize(100);
4064 if (reader->buffer == NULL) {
4065 xmlGenericError(xmlGenericErrorContext,
4066 "xmlTextReaderSetup : malloc failed\n");
4067 return (-1);
4068 }
4069 if (reader->sax == NULL)
4070 reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
4071 if (reader->sax == NULL) {
4072 xmlGenericError(xmlGenericErrorContext,
4073 "xmlTextReaderSetup : malloc failed\n");
4074 return (-1);
4075 }
4076 xmlSAXVersion(reader->sax, 2);
4077 reader->startElement = reader->sax->startElement;
4078 reader->sax->startElement = xmlTextReaderStartElement;
4079 reader->endElement = reader->sax->endElement;
4080 reader->sax->endElement = xmlTextReaderEndElement;
4081#ifdef LIBXML_SAX1_ENABLED
4082 if (reader->sax->initialized == XML_SAX2_MAGIC) {
4083#endif /* LIBXML_SAX1_ENABLED */
4084 reader->startElementNs = reader->sax->startElementNs;
4085 reader->sax->startElementNs = xmlTextReaderStartElementNs;
4086 reader->endElementNs = reader->sax->endElementNs;
4087 reader->sax->endElementNs = xmlTextReaderEndElementNs;
4088#ifdef LIBXML_SAX1_ENABLED
4089 } else {
4090 reader->startElementNs = NULL;
4091 reader->endElementNs = NULL;
4092 }
4093#endif /* LIBXML_SAX1_ENABLED */
4094 reader->characters = reader->sax->characters;
4095 reader->sax->characters = xmlTextReaderCharacters;
4096 reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
4097 reader->cdataBlock = reader->sax->cdataBlock;
4098 reader->sax->cdataBlock = xmlTextReaderCDataBlock;
4099
4100 reader->mode = XML_TEXTREADER_MODE_INITIAL;
4101 reader->node = NULL;
4102 reader->curnode = NULL;
4103 if (input != NULL) {
4104 if (reader->input->buffer->use < 4) {
4105 xmlParserInputBufferRead(input, 4);
4106 }
4107 if (reader->ctxt == NULL) {
4108 if (reader->input->buffer->use >= 4) {
4109 reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
4110 (const char *) reader->input->buffer->content, 4, URL);
4111 reader->base = 0;
4112 reader->cur = 4;
4113 } else {
4114 reader->ctxt =
4115 xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
4116 reader->base = 0;
4117 reader->cur = 0;
4118 }
4119 } else {
4120 xmlParserInputPtr inputStream;
4121 xmlParserInputBufferPtr buf;
4122 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
4123
4124 if (reader->ctxt->myDoc != NULL)
4125 xmlDictReference(reader->ctxt->myDoc->dict);
4126 xmlCtxtReset(reader->ctxt);
4127 buf = xmlAllocParserInputBuffer(enc);
4128 if (buf == NULL) return(-1);
4129 inputStream = xmlNewInputStream(reader->ctxt);
4130 if (inputStream == NULL) {
4131 xmlFreeParserInputBuffer(buf);
4132 return(-1);
4133 }
4134
4135 if (URL == NULL)
4136 inputStream->filename = NULL;
4137 else
4138 inputStream->filename = (char *)
4139 xmlCanonicPath((const xmlChar *) URL);
4140 inputStream->buf = buf;
4141 inputStream->base = inputStream->buf->buffer->content;
4142 inputStream->cur = inputStream->buf->buffer->content;
4143 inputStream->end =
4144 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
4145
4146 inputPush(reader->ctxt, inputStream);
4147 reader->cur = 0;
4148 }
4149 if (reader->ctxt == NULL) {
4150 xmlGenericError(xmlGenericErrorContext,
4151 "xmlTextReaderSetup : malloc failed\n");
4152 return (-1);
4153 }
4154 }
4155 if (reader->dict != NULL) {
4156 if (reader->ctxt->dict != NULL) {
4157 if (reader->dict != reader->ctxt->dict) {
4158 xmlDictFree(reader->dict);
4159 reader->dict = reader->ctxt->dict;
4160 }
4161 } else {
4162 reader->ctxt->dict = reader->dict;
4163 }
4164 } else {
4165 if (reader->ctxt->dict == NULL)
4166 reader->ctxt->dict = xmlDictCreate();
4167 reader->dict = reader->ctxt->dict;
4168 }
4169 reader->ctxt->_private = reader;
4170 reader->ctxt->linenumbers = 1;
4171 reader->ctxt->dictNames = 1;
4172 /*
4173 * use the parser dictionnary to allocate all elements and attributes names
4174 */
4175 reader->ctxt->docdict = 1;
4176
Daniel Veillard7899c5c2003-11-03 12:31:38 +00004177#ifdef LIBXML_XINCLUDE_ENABLED
4178 if (reader->xincctxt != NULL) {
4179 xmlXIncludeFreeContext(reader->xincctxt);
4180 reader->xincctxt = NULL;
4181 }
4182 if (options & XML_PARSE_XINCLUDE) {
4183 reader->xinclude = 1;
4184 reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
4185 options -= XML_PARSE_XINCLUDE;
4186 } else
4187 reader->xinclude = 0;
4188 reader->in_xinclude = 0;
4189#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00004190#ifdef LIBXML_PATTERN_ENABLED
4191 if (reader->patternTab == NULL) {
4192 reader->patternNr = 0;
4193 reader->patternMax = 0;
4194 }
4195 while (reader->patternNr > 0) {
4196 reader->patternNr--;
4197 if (reader->patternTab[reader->patternNr] != NULL) {
4198 xmlFreePattern(reader->patternTab[reader->patternNr]);
4199 reader->patternTab[reader->patternNr] = NULL;
4200 }
4201 }
4202#endif
4203
Daniel Veillardc36965d2003-12-02 10:28:48 +00004204 if (options & XML_PARSE_DTDVALID)
4205 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
4206
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004207 xmlCtxtUseOptions(reader->ctxt, options);
4208 if (encoding != NULL) {
4209 xmlCharEncodingHandlerPtr hdlr;
4210
4211 hdlr = xmlFindCharEncodingHandler(encoding);
4212 if (hdlr != NULL)
4213 xmlSwitchToEncoding(reader->ctxt, hdlr);
4214 }
4215 if ((URL != NULL) && (reader->ctxt->input != NULL) &&
4216 (reader->ctxt->input->filename == NULL))
4217 reader->ctxt->input->filename = (char *)
4218 xmlStrdup((const xmlChar *) URL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004219
4220 reader->doc = NULL;
4221
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004222 return (0);
4223}
4224
4225/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004226 * xmlReaderWalker:
4227 * @doc: a preparsed document
4228 *
4229 * Create an xmltextReader for a preparsed document.
4230 *
4231 * Returns the new reader or NULL in case of error.
4232 */
4233xmlTextReaderPtr
4234xmlReaderWalker(xmlDocPtr doc)
4235{
4236 xmlTextReaderPtr ret;
4237
4238 if (doc == NULL)
4239 return(NULL);
4240
4241 ret = xmlMalloc(sizeof(xmlTextReader));
4242 if (ret == NULL) {
4243 xmlGenericError(xmlGenericErrorContext,
4244 "xmlNewTextReader : malloc failed\n");
4245 return(NULL);
4246 }
4247 memset(ret, 0, sizeof(xmlTextReader));
4248 ret->entNr = 0;
4249 ret->input = NULL;
4250 ret->mode = XML_TEXTREADER_MODE_INITIAL;
4251 ret->node = NULL;
4252 ret->curnode = NULL;
4253 ret->base = 0;
4254 ret->cur = 0;
4255 ret->allocs = XML_TEXTREADER_CTXT;
4256 ret->doc = doc;
4257 ret->state = XML_TEXTREADER_START;
4258 ret->dict = xmlDictCreate();
4259 return(ret);
4260}
4261
4262/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004263 * xmlReaderForDoc:
4264 * @cur: a pointer to a zero terminated string
4265 * @URL: the base URL to use for the document
4266 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004267 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004268 *
4269 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004270 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004271 *
4272 * Returns the new reader or NULL in case of error.
4273 */
4274xmlTextReaderPtr
4275xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
4276 int options)
4277{
4278 int len;
4279
4280 if (cur == NULL)
4281 return (NULL);
4282 len = xmlStrlen(cur);
4283
4284 return (xmlReaderForMemory
4285 ((const char *) cur, len, URL, encoding, options));
4286}
4287
4288/**
4289 * xmlReaderForFile:
4290 * @filename: a file or URL
4291 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004292 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004293 *
4294 * parse an XML file from the filesystem or the network.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004295 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004296 *
4297 * Returns the new reader or NULL in case of error.
4298 */
4299xmlTextReaderPtr
4300xmlReaderForFile(const char *filename, const char *encoding, int options)
4301{
4302 xmlTextReaderPtr reader;
4303
4304 reader = xmlNewTextReaderFilename(filename);
4305 if (reader == NULL)
4306 return (NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004307 xmlTextReaderSetup(reader, NULL, NULL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004308 return (reader);
4309}
4310
4311/**
4312 * xmlReaderForMemory:
4313 * @buffer: a pointer to a char array
4314 * @size: the size of the array
4315 * @URL: the base URL to use for the document
4316 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004317 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004318 *
4319 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004320 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004321 *
4322 * Returns the new reader or NULL in case of error.
4323 */
4324xmlTextReaderPtr
4325xmlReaderForMemory(const char *buffer, int size, const char *URL,
4326 const char *encoding, int options)
4327{
4328 xmlTextReaderPtr reader;
4329 xmlParserInputBufferPtr buf;
4330
4331 buf =
4332 xmlParserInputBufferCreateMem(buffer, size,
4333 XML_CHAR_ENCODING_NONE);
4334 if (buf == NULL) {
4335 return (NULL);
4336 }
4337 reader = xmlNewTextReader(buf, URL);
4338 if (reader == NULL) {
4339 xmlFreeParserInputBuffer(buf);
4340 return (NULL);
4341 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004342 reader->allocs |= XML_TEXTREADER_INPUT;
4343 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004344 return (reader);
4345}
4346
4347/**
4348 * xmlReaderForFd:
4349 * @fd: an open file descriptor
4350 * @URL: the base URL to use for the document
4351 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004352 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004353 *
4354 * Create an xmltextReader for an XML from a file descriptor.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004355 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004356 * NOTE that the file descriptor will not be closed when the
4357 * reader is closed or reset.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004358 *
4359 * Returns the new reader or NULL in case of error.
4360 */
4361xmlTextReaderPtr
4362xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
4363{
4364 xmlTextReaderPtr reader;
4365 xmlParserInputBufferPtr input;
4366
4367 if (fd < 0)
4368 return (NULL);
4369
4370 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4371 if (input == NULL)
4372 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004373 input->closecallback = NULL;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004374 reader = xmlNewTextReader(input, URL);
4375 if (reader == NULL) {
4376 xmlFreeParserInputBuffer(input);
4377 return (NULL);
4378 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004379 reader->allocs |= XML_TEXTREADER_INPUT;
4380 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004381 return (reader);
4382}
4383
4384/**
4385 * xmlReaderForIO:
4386 * @ioread: an I/O read function
4387 * @ioclose: an I/O close function
4388 * @ioctx: an I/O handler
4389 * @URL: the base URL to use for the document
4390 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004391 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004392 *
4393 * Create an xmltextReader for an XML document from I/O functions and source.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004394 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004395 *
4396 * Returns the new reader or NULL in case of error.
4397 */
4398xmlTextReaderPtr
4399xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
4400 void *ioctx, const char *URL, const char *encoding,
4401 int options)
4402{
4403 xmlTextReaderPtr reader;
4404 xmlParserInputBufferPtr input;
4405
4406 if (ioread == NULL)
4407 return (NULL);
4408
4409 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4410 XML_CHAR_ENCODING_NONE);
4411 if (input == NULL)
4412 return (NULL);
4413 reader = xmlNewTextReader(input, URL);
4414 if (reader == NULL) {
4415 xmlFreeParserInputBuffer(input);
4416 return (NULL);
4417 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004418 reader->allocs |= XML_TEXTREADER_INPUT;
4419 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004420 return (reader);
4421}
4422
4423/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004424 * xmlReaderNewWalker:
4425 * @reader: an XML reader
4426 * @doc: a preparsed document
4427 *
4428 * Setup an xmltextReader to parse a preparsed XML document.
4429 * This reuses the existing @reader xmlTextReader.
4430 *
4431 * Returns 0 in case of success and -1 in case of error
4432 */
4433int
4434xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
4435{
4436 if (doc == NULL)
4437 return (-1);
4438 if (reader == NULL)
4439 return (-1);
4440
4441 if (reader->ctxt != NULL) {
4442 xmlCtxtReset(reader->ctxt);
4443 }
4444
4445 reader->entNr = 0;
4446 reader->input = NULL;
4447 reader->mode = XML_TEXTREADER_MODE_INITIAL;
4448 reader->node = NULL;
4449 reader->curnode = NULL;
4450 reader->base = 0;
4451 reader->cur = 0;
4452 reader->allocs = XML_TEXTREADER_CTXT;
4453 reader->doc = doc;
4454 reader->state = XML_TEXTREADER_START;
4455 if (reader->dict == NULL) {
4456 if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
4457 reader->dict = reader->ctxt->dict;
4458 else
4459 reader->dict = xmlDictCreate();
4460 }
4461 return(0);
4462}
4463
4464/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004465 * xmlReaderNewDoc:
4466 * @reader: an XML reader
4467 * @cur: a pointer to a zero terminated string
4468 * @URL: the base URL to use for the document
4469 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004470 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004471 *
4472 * Setup an xmltextReader to parse an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004473 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004474 * This reuses the existing @reader xmlTextReader.
4475 *
4476 * Returns 0 in case of success and -1 in case of error
4477 */
4478int
4479xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
4480 const char *URL, const char *encoding, int options)
4481{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004482
4483 int len;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004484
4485 if (cur == NULL)
4486 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004487 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004488 return (-1);
4489
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004490 len = xmlStrlen(cur);
4491 return (xmlReaderNewMemory(reader, (const char *)cur, len,
4492 URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004493}
4494
4495/**
4496 * xmlReaderNewFile:
4497 * @reader: an XML reader
4498 * @filename: a file or URL
4499 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004500 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004501 *
4502 * parse an XML file from the filesystem or the network.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004503 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004504 * This reuses the existing @reader xmlTextReader.
4505 *
4506 * Returns 0 in case of success and -1 in case of error
4507 */
4508int
4509xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
4510 const char *encoding, int options)
4511{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004512 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004513
4514 if (filename == NULL)
4515 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004516 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004517 return (-1);
4518
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004519 input =
4520 xmlParserInputBufferCreateFilename(filename,
4521 XML_CHAR_ENCODING_NONE);
4522 if (input == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004523 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004524 return (xmlTextReaderSetup(reader, input, filename, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004525}
4526
4527/**
4528 * xmlReaderNewMemory:
4529 * @reader: an XML reader
4530 * @buffer: a pointer to a char array
4531 * @size: the size of the array
4532 * @URL: the base URL to use for the document
4533 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004534 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004535 *
4536 * Setup an xmltextReader to parse an XML in-memory document.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004537 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004538 * This reuses the existing @reader xmlTextReader.
4539 *
4540 * Returns 0 in case of success and -1 in case of error
4541 */
4542int
4543xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
4544 const char *URL, const char *encoding, int options)
4545{
4546 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004547
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004548 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004549 return (-1);
4550 if (buffer == NULL)
4551 return (-1);
4552
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004553 input = xmlParserInputBufferCreateMem(buffer, size,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004554 XML_CHAR_ENCODING_NONE);
4555 if (input == NULL) {
4556 return (-1);
4557 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004558 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004559}
4560
4561/**
4562 * xmlReaderNewFd:
4563 * @reader: an XML reader
4564 * @fd: an open file descriptor
4565 * @URL: the base URL to use for the document
4566 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004567 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004568 *
4569 * Setup an xmltextReader to parse an XML from a file descriptor.
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004570 * NOTE that the file descriptor will not be closed when the
4571 * reader is closed or reset.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004572 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004573 * This reuses the existing @reader xmlTextReader.
4574 *
4575 * Returns 0 in case of success and -1 in case of error
4576 */
4577int
4578xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
4579 const char *URL, const char *encoding, int options)
4580{
4581 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004582
4583 if (fd < 0)
4584 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004585 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004586 return (-1);
4587
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004588 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4589 if (input == NULL)
4590 return (-1);
Daniel Veillard4bc5f432003-12-22 18:13:12 +00004591 input->closecallback = NULL;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004592 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004593}
4594
4595/**
4596 * xmlReaderNewIO:
4597 * @reader: an XML reader
4598 * @ioread: an I/O read function
4599 * @ioclose: an I/O close function
4600 * @ioctx: an I/O handler
4601 * @URL: the base URL to use for the document
4602 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004603 * @options: a combination of xmlParserOption
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004604 *
4605 * Setup an xmltextReader to parse an XML document from I/O functions
4606 * and source.
Daniel Veillard87ab1c12003-12-21 13:01:56 +00004607 * The parsing flags @options are a combination of xmlParserOption.
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004608 * This reuses the existing @reader xmlTextReader.
4609 *
4610 * Returns 0 in case of success and -1 in case of error
4611 */
4612int
4613xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
4614 xmlInputCloseCallback ioclose, void *ioctx,
4615 const char *URL, const char *encoding, int options)
4616{
4617 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004618
4619 if (ioread == NULL)
4620 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004621 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004622 return (-1);
4623
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004624 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4625 XML_CHAR_ENCODING_NONE);
4626 if (input == NULL)
4627 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004628 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004629}
Daniel Veillard26f70262003-01-16 22:45:08 +00004630/************************************************************************
4631 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004632 * Utilities *
4633 * *
4634 ************************************************************************/
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004635#ifdef NOT_USED_YET
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004636/**
4637 * xmlBase64Decode:
4638 * @in: the input buffer
4639 * @inlen: the size of the input (in), the size read from it (out)
4640 * @to: the output buffer
4641 * @tolen: the size of the output (in), the size written to (out)
4642 *
4643 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00004644 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004645 *
4646 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
4647 * 2 if there wasn't enough space on the output or -1 in case of error.
4648 */
4649static int
4650xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
4651 unsigned char *to, unsigned long *tolen) {
4652 unsigned long incur; /* current index in in[] */
4653 unsigned long inblk; /* last block index in in[] */
4654 unsigned long outcur; /* current index in out[] */
4655 unsigned long inmax; /* size of in[] */
4656 unsigned long outmax; /* size of out[] */
4657 unsigned char cur; /* the current value read from in[] */
Daniel Veillardc127adc2003-07-23 15:07:08 +00004658 unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004659 int nbintmp; /* number of byte in intmp[] */
4660 int is_ignore; /* cur should be ignored */
4661 int is_end = 0; /* the end of the base64 was found */
4662 int retval = 1;
4663 int i;
4664
4665 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
4666 return(-1);
4667
4668 incur = 0;
4669 inblk = 0;
4670 outcur = 0;
4671 inmax = *inlen;
4672 outmax = *tolen;
4673 nbintmp = 0;
4674
4675 while (1) {
4676 if (incur >= inmax)
4677 break;
4678 cur = in[incur++];
4679 is_ignore = 0;
4680 if ((cur >= 'A') && (cur <= 'Z'))
4681 cur = cur - 'A';
4682 else if ((cur >= 'a') && (cur <= 'z'))
4683 cur = cur - 'a' + 26;
4684 else if ((cur >= '0') && (cur <= '9'))
4685 cur = cur - '0' + 52;
4686 else if (cur == '+')
4687 cur = 62;
4688 else if (cur == '/')
4689 cur = 63;
4690 else if (cur == '.')
4691 cur = 0;
4692 else if (cur == '=') /*no op , end of the base64 stream */
4693 is_end = 1;
4694 else {
4695 is_ignore = 1;
4696 if (nbintmp == 0)
4697 inblk = incur;
4698 }
4699
4700 if (!is_ignore) {
4701 int nbouttmp = 3;
4702 int is_break = 0;
4703
4704 if (is_end) {
4705 if (nbintmp == 0)
4706 break;
4707 if ((nbintmp == 1) || (nbintmp == 2))
4708 nbouttmp = 1;
4709 else
4710 nbouttmp = 2;
4711 nbintmp = 3;
4712 is_break = 1;
4713 }
4714 intmp[nbintmp++] = cur;
4715 /*
4716 * if intmp is full, push the 4byte sequence as a 3 byte
4717 * sequence out
4718 */
4719 if (nbintmp == 4) {
4720 nbintmp = 0;
4721 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
4722 outtmp[1] =
4723 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
4724 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
4725 if (outcur + 3 >= outmax) {
4726 retval = 2;
4727 break;
4728 }
4729
4730 for (i = 0; i < nbouttmp; i++)
4731 to[outcur++] = outtmp[i];
4732 inblk = incur;
4733 }
4734
4735 if (is_break) {
4736 retval = 0;
4737 break;
4738 }
4739 }
4740 }
4741
4742 *tolen = outcur;
4743 *inlen = inblk;
4744 return (retval);
4745}
4746
4747/*
4748 * Test routine for the xmlBase64Decode function
4749 */
4750#if 0
4751int main(int argc, char **argv) {
4752 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
4753 char output[100];
4754 char output2[100];
4755 char output3[100];
4756 unsigned long inlen = strlen(input);
4757 unsigned long outlen = 100;
4758 int ret;
4759 unsigned long cons, tmp, tmp2, prod;
4760
4761 /*
4762 * Direct
4763 */
4764 ret = xmlBase64Decode(input, &inlen, output, &outlen);
4765
4766 output[outlen] = 0;
4767 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
4768
4769 /*
4770 * output chunking
4771 */
4772 cons = 0;
4773 prod = 0;
4774 while (cons < inlen) {
4775 tmp = 5;
4776 tmp2 = inlen - cons;
4777
4778 printf("%ld %ld\n", cons, prod);
4779 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
4780 cons += tmp2;
4781 prod += tmp;
4782 printf("%ld %ld\n", cons, prod);
4783 }
4784 output2[outlen] = 0;
4785 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
4786
4787 /*
4788 * input chunking
4789 */
4790 cons = 0;
4791 prod = 0;
4792 while (cons < inlen) {
4793 tmp = 100 - prod;
4794 tmp2 = inlen - cons;
4795 if (tmp2 > 5)
4796 tmp2 = 5;
4797
4798 printf("%ld %ld\n", cons, prod);
4799 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
4800 cons += tmp2;
4801 prod += tmp;
4802 printf("%ld %ld\n", cons, prod);
4803 }
4804 output3[outlen] = 0;
4805 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
4806 return(0);
4807
4808}
4809#endif
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004810#endif /* NOT_USED_YET */
Daniel Veillard81273902003-09-30 00:43:48 +00004811#endif /* LIBXML_READER_ENABLED */