blob: efdba3d05e7a5313eaa555d90b1b9927796c29fb [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 Veillarde1ca5032002-12-09 14:13:43 +000044
45/* #define DEBUG_CALLBACKS */
46/* #define DEBUG_READER */
47
48/**
49 * TODO:
50 *
51 * macro to flag unimplemented blocks
52 */
53#define TODO \
54 xmlGenericError(xmlGenericErrorContext, \
55 "Unimplemented block at %s:%d\n", \
56 __FILE__, __LINE__);
57
58#ifdef DEBUG_READER
59#define DUMP_READER xmlTextReaderDebug(reader);
60#else
61#define DUMP_READER
62#endif
63
Daniel Veillarda880b122003-04-21 21:36:41 +000064#define CHUNK_SIZE 512
Daniel Veillarde1ca5032002-12-09 14:13:43 +000065/************************************************************************
66 * *
67 * The parser: maps the Text Reader API on top of the existing *
68 * parsing routines building a tree *
69 * *
70 ************************************************************************/
71
72#define XML_TEXTREADER_INPUT 1
73#define XML_TEXTREADER_CTXT 2
74
75typedef enum {
Daniel Veillard67df8092002-12-16 22:04:11 +000076 XML_TEXTREADER_MODE_INITIAL = 0,
77 XML_TEXTREADER_MODE_INTERACTIVE = 1,
78 XML_TEXTREADER_MODE_ERROR = 2,
79 XML_TEXTREADER_MODE_EOF =3,
80 XML_TEXTREADER_MODE_CLOSED = 4,
81 XML_TEXTREADER_MODE_READING = 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000082} xmlTextReaderMode;
83
84typedef enum {
85 XML_TEXTREADER_NONE = -1,
86 XML_TEXTREADER_START= 0,
87 XML_TEXTREADER_ELEMENT= 1,
88 XML_TEXTREADER_END= 2,
89 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000090 XML_TEXTREADER_BACKTRACK= 4,
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000091 XML_TEXTREADER_DONE= 5,
92 XML_TEXTREADER_ERROR= 6
Daniel Veillarde1ca5032002-12-09 14:13:43 +000093} xmlTextReaderState;
94
Daniel Veillardf4e55762003-04-15 23:32:22 +000095typedef enum {
96 XML_TEXTREADER_NOT_VALIDATE = 0,
97 XML_TEXTREADER_VALIDATE_DTD = 1,
98 XML_TEXTREADER_VALIDATE_RNG = 2
99} xmlTextReaderValidate;
100
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000101struct _xmlTextReader {
102 int mode; /* the parsing mode */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000103 xmlDocPtr doc; /* when walking an existing doc */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000104 xmlTextReaderValidate validate;/* is there any validation */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000105 int allocs; /* what structure were deallocated */
106 xmlTextReaderState state;
107 xmlParserCtxtPtr ctxt; /* the parser context */
108 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
109 xmlParserInputBufferPtr input; /* the input */
110 startElementSAXFunc startElement;/* initial SAX callbacks */
111 endElementSAXFunc endElement; /* idem */
Daniel Veillard07cb8222003-09-10 10:51:05 +0000112 startElementNsSAX2Func startElementNs;/* idem */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000113 endElementNsSAX2Func endElementNs; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000114 charactersSAXFunc characters;
115 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000116 unsigned int base; /* base of the segment in the input */
117 unsigned int cur; /* current position in the input */
118 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000119 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000120 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000121 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000122 int preserve;/* preserve the resulting document */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000123 xmlBufferPtr buffer; /* used to return const xmlChar * */
124 xmlDictPtr dict; /* the context dictionnary */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000125
126 /* entity stack when traversing entities content */
127 xmlNodePtr ent; /* Current Entity Ref Node */
128 int entNr; /* Depth of the entities stack */
129 int entMax; /* Max depth of the entities stack */
130 xmlNodePtr *entTab; /* array of entities */
Daniel Veillard26f70262003-01-16 22:45:08 +0000131
132 /* error handling */
133 xmlTextReaderErrorFunc errorFunc; /* callback function */
134 void *errorFuncArg; /* callback function user argument */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000135
136#ifdef LIBXML_SCHEMAS_ENABLED
137 /* Handling of RelaxNG validation */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000138 xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
139 xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
140 int rngValidErrors;/* The number of errors detected */
141 xmlNodePtr rngFullNode; /* the node if RNG not progressive */
142#endif
143#ifdef LIBXML_XINCLUDE_ENABLED
144 /* Handling of XInclude processing */
145 int xinclude; /* is xinclude asked for */
146 const xmlChar * xinclude_name; /* the xinclude name from dict */
147 xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */
148 int in_xinclude; /* counts for xinclude */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000149#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000150};
151
Daniel Veillarde8039df2003-10-27 11:25:13 +0000152#define NODE_IS_EMPTY 0x1
153#define NODE_IS_PRESERVED 0x2
Daniel Veillard067bae52003-01-05 01:27:54 +0000154
Daniel Veillarde72c5082003-09-19 12:44:05 +0000155/**
156 * CONSTSTR:
157 *
158 * Macro used to return an interned string
159 */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000160#define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
161#define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
162
163static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
164static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
Daniel Veillarde72c5082003-09-19 12:44:05 +0000165
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000166/************************************************************************
167 * *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000168 * Our own version of the freeing routines as we recycle nodes *
169 * *
170 ************************************************************************/
171/**
172 * DICT_FREE:
173 * @str: a string
174 *
175 * Free a string if it is not owned by the "dict" dictionnary in the
176 * current scope
177 */
178#define DICT_FREE(str) \
179 if ((str) && ((!dict) || \
180 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
181 xmlFree((char *)(str));
182
183static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
184static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
185
186/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000187 * xmlFreeID:
188 * @not: A id
189 *
190 * Deallocate the memory used by an id definition
191 */
192static void
193xmlFreeID(xmlIDPtr id) {
194 xmlDictPtr dict = NULL;
195
196 if (id == NULL) return;
197
198 if (id->doc != NULL)
199 dict = id->doc->dict;
200
201 if (id->value != NULL)
202 DICT_FREE(id->value)
203 xmlFree(id);
204}
205
206/**
207 * xmlTextReaderRemoveID:
208 * @doc: the document
209 * @attr: the attribute
210 *
211 * Remove the given attribute from the ID table maintained internally.
212 *
213 * Returns -1 if the lookup failed and 0 otherwise
214 */
215static int
216xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
217 xmlIDTablePtr table;
218 xmlIDPtr id;
219 xmlChar *ID;
220
221 if (doc == NULL) return(-1);
222 if (attr == NULL) return(-1);
223 table = (xmlIDTablePtr) doc->ids;
224 if (table == NULL)
225 return(-1);
226
227 if (attr == NULL)
228 return(-1);
229 ID = xmlNodeListGetString(doc, attr->children, 1);
230 if (ID == NULL)
231 return(-1);
232 id = xmlHashLookup(table, ID);
233 xmlFree(ID);
234 if (id == NULL || id->attr != attr) {
235 return(-1);
236 }
237 id->name = attr->name;
238 id->attr = NULL;
239 return(0);
240}
241
242/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000243 * xmlTextReaderFreeProp:
244 * @reader: the xmlTextReaderPtr used
245 * @cur: the node
246 *
247 * Free a node.
248 */
249static void
250xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
251 xmlDictPtr dict;
252
253 dict = reader->ctxt->dict;
254 if (cur == NULL) return;
255
256 /* Check for ID removal -> leading to invalid references ! */
257 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
258 ((cur->parent->doc->intSubset != NULL) ||
259 (cur->parent->doc->extSubset != NULL))) {
260 if (xmlIsID(cur->parent->doc, cur->parent, cur))
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000261 xmlTextReaderRemoveID(cur->parent->doc, cur);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000262 }
263 if (cur->children != NULL)
264 xmlTextReaderFreeNodeList(reader, cur->children);
265
266 DICT_FREE(cur->name);
267 if ((reader != NULL) && (reader->ctxt != NULL) &&
268 (reader->ctxt->freeAttrsNr < 100)) {
269 cur->next = reader->ctxt->freeAttrs;
270 reader->ctxt->freeAttrs = cur;
271 reader->ctxt->freeAttrsNr++;
272 } else {
273 xmlFree(cur);
274 }
275}
276
277/**
278 * xmlTextReaderFreePropList:
279 * @reader: the xmlTextReaderPtr used
280 * @cur: the first property in the list
281 *
282 * Free a property and all its siblings, all the children are freed too.
283 */
284static void
285xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
286 xmlAttrPtr next;
287 if (cur == NULL) return;
288 while (cur != NULL) {
289 next = cur->next;
290 xmlTextReaderFreeProp(reader, cur);
291 cur = next;
292 }
293}
294
295/**
296 * xmlTextReaderFreeNodeList:
297 * @reader: the xmlTextReaderPtr used
298 * @cur: the first node in the list
299 *
300 * Free a node and all its siblings, this is a recursive behaviour, all
301 * the children are freed too.
302 */
303static void
304xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
305 xmlNodePtr next;
306 xmlDictPtr dict;
307
308 dict = reader->ctxt->dict;
309 if (cur == NULL) return;
310 if (cur->type == XML_NAMESPACE_DECL) {
311 xmlFreeNsList((xmlNsPtr) cur);
312 return;
313 }
314 if ((cur->type == XML_DOCUMENT_NODE) ||
315 (cur->type == XML_HTML_DOCUMENT_NODE)) {
316 xmlFreeDoc((xmlDocPtr) cur);
317 return;
318 }
319 while (cur != NULL) {
320 next = cur->next;
321 /* unroll to speed up freeing the document */
322 if (cur->type != XML_DTD_NODE) {
323
324 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000325 (cur->type != XML_ENTITY_REF_NODE)) {
326 if (cur->children->parent == cur)
327 xmlTextReaderFreeNodeList(reader, cur->children);
328 cur->children = NULL;
329 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000330 if (((cur->type == XML_ELEMENT_NODE) ||
331 (cur->type == XML_XINCLUDE_START) ||
332 (cur->type == XML_XINCLUDE_END)) &&
333 (cur->properties != NULL))
334 xmlTextReaderFreePropList(reader, cur->properties);
335 if ((cur->type != XML_ELEMENT_NODE) &&
336 (cur->type != XML_XINCLUDE_START) &&
337 (cur->type != XML_XINCLUDE_END) &&
338 (cur->type != XML_ENTITY_REF_NODE)) {
339 DICT_FREE(cur->content);
340 }
341 if (((cur->type == XML_ELEMENT_NODE) ||
342 (cur->type == XML_XINCLUDE_START) ||
343 (cur->type == XML_XINCLUDE_END)) &&
344 (cur->nsDef != NULL))
345 xmlFreeNsList(cur->nsDef);
346
347 /*
348 * we don't free element names here they are interned now
349 */
350 if ((cur->type != XML_TEXT_NODE) &&
351 (cur->type != XML_COMMENT_NODE))
352 DICT_FREE(cur->name);
353 if (((cur->type == XML_ELEMENT_NODE) ||
354 (cur->type == XML_TEXT_NODE)) &&
355 (reader != NULL) && (reader->ctxt != NULL) &&
356 (reader->ctxt->freeElemsNr < 100)) {
357 cur->next = reader->ctxt->freeElems;
358 reader->ctxt->freeElems = cur;
359 reader->ctxt->freeElemsNr++;
360 } else {
361 xmlFree(cur);
362 }
363 }
364 cur = next;
365 }
366}
367
368/**
369 * xmlTextReaderFreeNode:
370 * @reader: the xmlTextReaderPtr used
371 * @cur: the node
372 *
373 * Free a node, this is a recursive behaviour, all the children are freed too.
374 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
375 */
376static void
377xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
378 xmlDictPtr dict;
379
380 dict = reader->ctxt->dict;
381 if (cur->type == XML_DTD_NODE) {
382 xmlFreeDtd((xmlDtdPtr) cur);
383 return;
384 }
385 if (cur->type == XML_NAMESPACE_DECL) {
386 xmlFreeNs((xmlNsPtr) cur);
387 return;
388 }
389 if (cur->type == XML_ATTRIBUTE_NODE) {
390 xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
391 return;
392 }
393
394 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000395 (cur->type != XML_ENTITY_REF_NODE)) {
396 if (cur->children->parent == cur)
397 xmlTextReaderFreeNodeList(reader, cur->children);
398 cur->children = NULL;
399 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000400 if (((cur->type == XML_ELEMENT_NODE) ||
401 (cur->type == XML_XINCLUDE_START) ||
402 (cur->type == XML_XINCLUDE_END)) &&
403 (cur->properties != NULL))
404 xmlTextReaderFreePropList(reader, cur->properties);
405 if ((cur->type != XML_ELEMENT_NODE) &&
406 (cur->type != XML_XINCLUDE_START) &&
407 (cur->type != XML_XINCLUDE_END) &&
408 (cur->type != XML_ENTITY_REF_NODE)) {
409 DICT_FREE(cur->content);
410 }
411 if (((cur->type == XML_ELEMENT_NODE) ||
412 (cur->type == XML_XINCLUDE_START) ||
413 (cur->type == XML_XINCLUDE_END)) &&
414 (cur->nsDef != NULL))
415 xmlFreeNsList(cur->nsDef);
416
417 /*
418 * we don't free names here they are interned now
419 */
420 if ((cur->type != XML_TEXT_NODE) &&
421 (cur->type != XML_COMMENT_NODE))
422 DICT_FREE(cur->name);
423 if (((cur->type == XML_ELEMENT_NODE) ||
424 (cur->type == XML_TEXT_NODE)) &&
425 (reader != NULL) && (reader->ctxt != NULL) &&
426 (reader->ctxt->freeElemsNr < 100)) {
427 cur->next = reader->ctxt->freeElems;
428 reader->ctxt->freeElems = cur;
429 reader->ctxt->freeElemsNr++;
430 } else {
431 xmlFree(cur);
432 }
433}
434
435/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000436 * xmlTextReaderFreeIDTable:
437 * @table: An id table
438 *
439 * Deallocate the memory used by an ID hash table.
440 */
William M. Brack60f394e2003-11-16 06:25:42 +0000441static void
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000442xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
443 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
444}
445
446/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000447 * xmlTextReaderFreeDoc:
448 * @reader: the xmlTextReaderPtr used
449 * @cur: pointer to the document
450 *
451 * Free up all the structures used by a document, tree included.
452 */
453static void
454xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
455 xmlDtdPtr extSubset, intSubset;
456
457 if (cur == NULL) return;
458
459 /*
460 * Do this before freeing the children list to avoid ID lookups
461 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000462 if (cur->ids != NULL) xmlTextReaderFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000463 cur->ids = NULL;
464 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
465 cur->refs = NULL;
466 extSubset = cur->extSubset;
467 intSubset = cur->intSubset;
468 if (intSubset == extSubset)
469 extSubset = NULL;
470 if (extSubset != NULL) {
471 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
472 cur->extSubset = NULL;
473 xmlFreeDtd(extSubset);
474 }
475 if (intSubset != NULL) {
476 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
477 cur->intSubset = NULL;
478 xmlFreeDtd(intSubset);
479 }
480
481 if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
482
483 if (cur->version != NULL) xmlFree((char *) cur->version);
484 if (cur->name != NULL) xmlFree((char *) cur->name);
485 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
486 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
487 if (cur->URL != NULL) xmlFree((char *) cur->URL);
488 xmlFree(cur);
489}
490
491/************************************************************************
492 * *
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000493 * The reader core parser *
494 * *
495 ************************************************************************/
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000496#ifdef DEBUG_READER
497static void
498xmlTextReaderDebug(xmlTextReaderPtr reader) {
499 if ((reader == NULL) || (reader->ctxt == NULL)) {
500 fprintf(stderr, "xmlTextReader NULL\n");
501 return;
502 }
503 fprintf(stderr, "xmlTextReader: state %d depth %d ",
504 reader->state, reader->depth);
505 if (reader->node == NULL) {
506 fprintf(stderr, "node = NULL\n");
507 } else {
508 fprintf(stderr, "node %s\n", reader->node->name);
509 }
510 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
511 reader->base, reader->cur, reader->ctxt->nodeNr);
512 if (reader->input->buffer == NULL) {
513 fprintf(stderr, "buffer is NULL\n");
514 } else {
515#ifdef LIBXML_DEBUG_ENABLED
516 xmlDebugDumpString(stderr,
517 &reader->input->buffer->content[reader->cur]);
518#endif
519 fprintf(stderr, "\n");
520 }
521}
522#endif
523
524/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000525 * xmlTextReaderEntPush:
526 * @reader: the xmlTextReaderPtr used
527 * @value: the entity reference node
528 *
529 * Pushes a new entity reference node on top of the entities stack
530 *
531 * Returns 0 in case of error, the index in the stack otherwise
532 */
533static int
534xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
535{
536 if (reader->entMax <= 0) {
537 reader->entMax = 10;
538 reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
539 sizeof(reader->entTab[0]));
540 if (reader->entTab == NULL) {
541 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
542 return (0);
543 }
544 }
545 if (reader->entNr >= reader->entMax) {
546 reader->entMax *= 2;
547 reader->entTab =
548 (xmlNodePtr *) xmlRealloc(reader->entTab,
549 reader->entMax *
550 sizeof(reader->entTab[0]));
551 if (reader->entTab == NULL) {
552 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
553 return (0);
554 }
555 }
556 reader->entTab[reader->entNr] = value;
557 reader->ent = value;
558 return (reader->entNr++);
559}
560
561/**
562 * xmlTextReaderEntPop:
563 * @reader: the xmlTextReaderPtr used
564 *
565 * Pops the top element entity from the entities stack
566 *
567 * Returns the entity just removed
568 */
569static xmlNodePtr
570xmlTextReaderEntPop(xmlTextReaderPtr reader)
571{
572 xmlNodePtr ret;
573
574 if (reader->entNr <= 0)
575 return (0);
576 reader->entNr--;
577 if (reader->entNr > 0)
578 reader->ent = reader->entTab[reader->entNr - 1];
579 else
580 reader->ent = NULL;
581 ret = reader->entTab[reader->entNr];
582 reader->entTab[reader->entNr] = 0;
583 return (ret);
584}
585
586/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000587 * xmlTextReaderStartElement:
588 * @ctx: the user data (XML parser context)
589 * @fullname: The element name, including namespace prefix
590 * @atts: An array of name/value attributes pairs, NULL terminated
591 *
592 * called when an opening tag has been processed.
593 */
594static void
595xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
596 const xmlChar **atts) {
597 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
598 xmlTextReaderPtr reader = ctxt->_private;
599
600#ifdef DEBUG_CALLBACKS
601 printf("xmlTextReaderStartElement(%s)\n", fullname);
602#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000603 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000604 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000605 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
606 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
607 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000608 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000609 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000610 if (reader != NULL)
611 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000612}
613
614/**
615 * xmlTextReaderEndElement:
616 * @ctx: the user data (XML parser context)
617 * @fullname: The element name, including namespace prefix
618 *
619 * called when an ending tag has been processed.
620 */
621static void
622xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
623 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
624 xmlTextReaderPtr reader = ctxt->_private;
625
626#ifdef DEBUG_CALLBACKS
627 printf("xmlTextReaderEndElement(%s)\n", fullname);
628#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000629 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000630 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000631 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000632}
633
634/**
Daniel Veillard07cb8222003-09-10 10:51:05 +0000635 * xmlTextReaderStartElementNs:
636 * @ctx: the user data (XML parser context)
637 * @localname: the local name of the element
638 * @prefix: the element namespace prefix if available
639 * @URI: the element namespace name if available
640 * @nb_namespaces: number of namespace definitions on that node
641 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
642 * @nb_attributes: the number of attributes on that node
643 * nb_defaulted: the number of defaulted attributes.
644 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
645 * attribute values.
646 *
647 * called when an opening tag has been processed.
648 */
649static void
650xmlTextReaderStartElementNs(void *ctx,
651 const xmlChar *localname,
652 const xmlChar *prefix,
653 const xmlChar *URI,
654 int nb_namespaces,
655 const xmlChar **namespaces,
656 int nb_attributes,
657 int nb_defaulted,
658 const xmlChar **attributes)
659{
660 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
661 xmlTextReaderPtr reader = ctxt->_private;
662
663#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000664 printf("xmlTextReaderStartElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000665#endif
666 if ((reader != NULL) && (reader->startElementNs != NULL)) {
667 reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
668 namespaces, nb_attributes, nb_defaulted,
669 attributes);
670 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
671 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
672 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000673 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillard07cb8222003-09-10 10:51:05 +0000674 }
675 if (reader != NULL)
676 reader->state = XML_TEXTREADER_ELEMENT;
677}
678
679/**
680 * xmlTextReaderEndElementNs:
681 * @ctx: the user data (XML parser context)
682 * @localname: the local name of the element
683 * @prefix: the element namespace prefix if available
684 * @URI: the element namespace name if available
685 *
686 * called when an ending tag has been processed.
687 */
688static void
689xmlTextReaderEndElementNs(void *ctx,
690 const xmlChar * localname,
691 const xmlChar * prefix,
692 const xmlChar * URI)
693{
694 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
695 xmlTextReaderPtr reader = ctxt->_private;
696
697#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000698 printf("xmlTextReaderEndElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000699#endif
700 if ((reader != NULL) && (reader->endElementNs != NULL)) {
701 reader->endElementNs(ctx, localname, prefix, URI);
702 }
703}
704
705
706/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000707 * xmlTextReaderCharacters:
708 * @ctx: the user data (XML parser context)
709 * @ch: a xmlChar string
710 * @len: the number of xmlChar
711 *
712 * receiving some chars from the parser.
713 */
714static void
715xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
716{
717 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
718 xmlTextReaderPtr reader = ctxt->_private;
719
720#ifdef DEBUG_CALLBACKS
721 printf("xmlTextReaderCharacters()\n");
722#endif
723 if ((reader != NULL) && (reader->characters != NULL)) {
724 reader->characters(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000725 }
726}
727
728/**
729 * xmlTextReaderCDataBlock:
730 * @ctx: the user data (XML parser context)
731 * @value: The pcdata content
732 * @len: the block length
733 *
734 * called when a pcdata block has been parsed
735 */
736static void
737xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
738{
739 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
740 xmlTextReaderPtr reader = ctxt->_private;
741
742#ifdef DEBUG_CALLBACKS
743 printf("xmlTextReaderCDataBlock()\n");
744#endif
745 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
746 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000747 }
748}
749
750/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000751 * xmlTextReaderPushData:
752 * @reader: the xmlTextReaderPtr used
753 *
754 * Push data down the progressive parser until a significant callback
755 * got raised.
756 *
757 * Returns -1 in case of failure, 0 otherwise
758 */
759static int
760xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000761 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000762 int val, s;
William M. Brack779af002003-08-01 15:55:39 +0000763 xmlTextReaderState oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000764
765 if ((reader->input == NULL) || (reader->input->buffer == NULL))
766 return(-1);
767
Daniel Veillardea7751d2002-12-20 00:16:24 +0000768 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000769 reader->state = XML_TEXTREADER_NONE;
770 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000771
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000772 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000773 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000774 /*
775 * Refill the buffer unless we are at the end of the stream
776 */
777 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
778 val = xmlParserInputBufferRead(reader->input, 4096);
Daniel Veillard53350552003-09-18 13:35:51 +0000779 if ((val == 0) &&
780 (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
781 if (inbuf->use == reader->cur) {
782 reader->mode = XML_TEXTREADER_MODE_EOF;
783 reader->state = oldstate;
784 if ((oldstate != XML_TEXTREADER_START) ||
785 (reader->ctxt->myDoc != NULL))
786 return(val);
787 }
788 } else if (val < 0) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000789 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000790 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000791 if ((oldstate != XML_TEXTREADER_START) ||
792 (reader->ctxt->myDoc != NULL))
793 return(val);
Daniel Veillard53350552003-09-18 13:35:51 +0000794 } else if (val == 0) {
795 /* mark the end of the stream and process the remains */
796 reader->mode = XML_TEXTREADER_MODE_EOF;
797 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000798 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000799
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000800 } else
801 break;
802 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000803 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000804 * parse by block of CHUNK_SIZE bytes, various tests show that
805 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000806 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000807 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000808 val = xmlParseChunk(reader->ctxt,
809 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000810 CHUNK_SIZE, 0);
811 reader->cur += CHUNK_SIZE;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000812 if ((val != 0) && (reader->ctxt->wellFormed == 0))
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000813 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000814 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000815 s = inbuf->use - reader->cur;
816 val = xmlParseChunk(reader->ctxt,
817 (const char *) &inbuf->content[reader->cur],
818 s, 0);
819 reader->cur += s;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000820 if ((val != 0) && (reader->ctxt->wellFormed == 0))
Daniel Veillarda880b122003-04-21 21:36:41 +0000821 return(-1);
822 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000823 }
824 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000825
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000826 /*
827 * Discard the consumed input when needed and possible
828 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000829 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillardf6bc7c22003-09-17 22:33:22 +0000830 if ((reader->cur >= 4096) &&
831 (inbuf->use - reader->cur <= CHUNK_SIZE)) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000832 val = xmlBufferShrink(inbuf, reader->cur);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000833 if (val >= 0) {
834 reader->cur -= val;
835 }
836 }
837 }
838
839 /*
840 * At the end of the stream signal that the work is done to the Push
841 * parser.
842 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000843 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000844 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000845 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000846 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000847 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000848 s, 1);
849 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000850 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000851 if ((val != 0) && (reader->ctxt->wellFormed == 0))
852 return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000853 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000854 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000855 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000856 return(0);
857}
858
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000859#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000860/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000861 * xmlTextReaderValidatePush:
862 * @reader: the xmlTextReaderPtr used
863 *
864 * Push the current node for validation
865 */
866static void
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000867xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000868 xmlNodePtr node = reader->node;
869
Daniel Veillardf4e55762003-04-15 23:32:22 +0000870 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
871 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
872 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
873 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
874 reader->ctxt->myDoc, node, node->name);
875 } else {
876 /* TODO use the BuildQName interface */
877 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000878
Daniel Veillardf4e55762003-04-15 23:32:22 +0000879 qname = xmlStrdup(node->ns->prefix);
880 qname = xmlStrcat(qname, BAD_CAST ":");
881 qname = xmlStrcat(qname, node->name);
882 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
883 reader->ctxt->myDoc, node, qname);
884 if (qname != NULL)
885 xmlFree(qname);
886 }
887#ifdef LIBXML_SCHEMAS_ENABLED
888 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
889 (reader->rngValidCtxt != NULL)) {
890 int ret;
891
892 if (reader->rngFullNode != NULL) return;
893 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
894 reader->ctxt->myDoc,
895 node);
896 if (ret == 0) {
897 /*
898 * this element requires a full tree
899 */
900 node = xmlTextReaderExpand(reader);
901 if (node == NULL) {
902printf("Expand failed !\n");
903 ret = -1;
904 } else {
905 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
906 reader->ctxt->myDoc,
907 node);
908 reader->rngFullNode = node;
909 }
910 }
911 if (ret != 1)
912 reader->rngValidErrors++;
913#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000914 }
915}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000916
917/**
918 * xmlTextReaderValidateCData:
919 * @reader: the xmlTextReaderPtr used
920 * @data: pointer to the CData
921 * @len: lenght of the CData block in bytes.
922 *
923 * Push some CData for validation
924 */
925static void
926xmlTextReaderValidateCData(xmlTextReaderPtr reader,
927 const xmlChar *data, int len) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000928 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
929 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
930 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
931 data, len);
932#ifdef LIBXML_SCHEMAS_ENABLED
933 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
934 (reader->rngValidCtxt != NULL)) {
935 int ret;
936
937 if (reader->rngFullNode != NULL) return;
938 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
939 if (ret != 1)
940 reader->rngValidErrors++;
941#endif
942 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000943}
944
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000945/**
946 * xmlTextReaderValidatePop:
947 * @reader: the xmlTextReaderPtr used
948 *
949 * Pop the current node from validation
950 */
951static void
952xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
953 xmlNodePtr node = reader->node;
954
Daniel Veillardf4e55762003-04-15 23:32:22 +0000955 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
956 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
957 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
958 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
959 reader->ctxt->myDoc, node, node->name);
960 } else {
961 /* TODO use the BuildQName interface */
962 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000963
Daniel Veillardf4e55762003-04-15 23:32:22 +0000964 qname = xmlStrdup(node->ns->prefix);
965 qname = xmlStrcat(qname, BAD_CAST ":");
966 qname = xmlStrcat(qname, node->name);
967 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
968 reader->ctxt->myDoc, node, qname);
969 if (qname != NULL)
970 xmlFree(qname);
971 }
972#ifdef LIBXML_SCHEMAS_ENABLED
973 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
974 (reader->rngValidCtxt != NULL)) {
975 int ret;
976
977 if (reader->rngFullNode != NULL) {
978 if (node == reader->rngFullNode)
979 reader->rngFullNode = NULL;
980 return;
981 }
982 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
983 reader->ctxt->myDoc,
984 node);
985 if (ret != 1)
986 reader->rngValidErrors++;
987#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000988 }
989}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000990
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000991/**
992 * xmlTextReaderValidateEntity:
993 * @reader: the xmlTextReaderPtr used
994 *
995 * Handle the validation when an entity reference is encountered and
996 * entity substitution is not activated. As a result the parser interface
997 * must walk through the entity and do the validation calls
998 */
999static void
1000xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
1001 xmlNodePtr oldnode = reader->node;
1002 xmlNodePtr node = reader->node;
1003 xmlParserCtxtPtr ctxt = reader->ctxt;
1004
1005 do {
1006 if (node->type == XML_ENTITY_REF_NODE) {
1007 /*
1008 * Case where the underlying tree is not availble, lookup the entity
1009 * and walk it.
1010 */
1011 if ((node->children == NULL) && (ctxt->sax != NULL) &&
1012 (ctxt->sax->getEntity != NULL)) {
1013 node->children = (xmlNodePtr)
1014 ctxt->sax->getEntity(ctxt, node->name);
1015 }
1016
1017 if ((node->children != NULL) &&
1018 (node->children->type == XML_ENTITY_DECL) &&
1019 (node->children->children != NULL)) {
1020 xmlTextReaderEntPush(reader, node);
1021 node = node->children->children;
1022 continue;
1023 } else {
1024 /*
1025 * The error has probably be raised already.
1026 */
1027 if (node == oldnode)
1028 break;
1029 node = node->next;
1030 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001031#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001032 } else if (node->type == XML_ELEMENT_NODE) {
1033 reader->node = node;
1034 xmlTextReaderValidatePush(reader);
1035 } else if ((node->type == XML_TEXT_NODE) ||
1036 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001037 xmlTextReaderValidateCData(reader, node->content,
1038 xmlStrlen(node->content));
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001039#endif
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001040 }
1041
1042 /*
1043 * go to next node
1044 */
1045 if (node->children != NULL) {
1046 node = node->children;
1047 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00001048 } else if (node->type == XML_ELEMENT_NODE) {
1049 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001050 }
1051 if (node->next != NULL) {
1052 node = node->next;
1053 continue;
1054 }
1055 do {
1056 node = node->parent;
1057 if (node->type == XML_ELEMENT_NODE) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001058 xmlNodePtr tmp;
Daniel Veillard1d211e22003-10-20 22:32:39 +00001059 if (reader->entNr == 0) {
1060 while ((tmp = node->last) != NULL) {
Daniel Veillarde8039df2003-10-27 11:25:13 +00001061 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard1d211e22003-10-20 22:32:39 +00001062 xmlUnlinkNode(tmp);
1063 xmlTextReaderFreeNode(reader, tmp);
1064 } else
1065 break;
1066 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001067 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001068 reader->node = node;
1069 xmlTextReaderValidatePop(reader);
1070 }
1071 if ((node->type == XML_ENTITY_DECL) &&
1072 (reader->ent != NULL) && (reader->ent->children == node)) {
1073 node = xmlTextReaderEntPop(reader);
1074 }
1075 if (node == oldnode)
1076 break;
1077 if (node->next != NULL) {
1078 node = node->next;
1079 break;
1080 }
1081 } while ((node != NULL) && (node != oldnode));
1082 } while ((node != NULL) && (node != oldnode));
1083 reader->node = oldnode;
1084}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001085#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001086
1087
1088/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001089 * xmlTextReaderGetSuccessor:
1090 * @cur: the current node
1091 *
1092 * Get the successor of a node if available.
1093 *
1094 * Returns the successor node or NULL
1095 */
1096static xmlNodePtr
1097xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1098 if (cur == NULL) return(NULL) ; /* ERROR */
1099 if (cur->next != NULL) return(cur->next) ;
1100 do {
1101 cur = cur->parent;
1102 if (cur == NULL) return(NULL);
1103 if (cur->next != NULL) return(cur->next);
1104 } while (cur != NULL);
1105 return(cur);
1106}
1107
1108/**
1109 * xmlTextReaderDoExpand:
1110 * @reader: the xmlTextReaderPtr used
1111 *
1112 * Makes sure that the current node is fully read as well as all its
1113 * descendant. It means the full DOM subtree must be available at the
1114 * end of the call.
1115 *
1116 * Returns 1 if the node was expanded successfully, 0 if there is no more
1117 * nodes to read, or -1 in case of error
1118 */
1119static int
1120xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1121 int val;
1122
1123 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1124 return(-1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001125 do {
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001126 if (reader->ctxt->instate == XML_PARSER_EOF) return(1);
1127
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001128 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1129 return(1);
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001130 if (reader->ctxt->nodeNr < reader->depth)
Daniel Veillarda37aab82003-06-09 09:10:36 +00001131 return(1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001132 if (reader->mode == XML_TEXTREADER_MODE_EOF)
1133 return(1);
1134 val = xmlTextReaderPushData(reader);
1135 if (val < 0)
1136 return(-1);
1137 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1138 return(1);
1139}
1140
1141/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001142 * xmlTextReaderRead:
1143 * @reader: the xmlTextReaderPtr used
1144 *
1145 * Moves the position of the current instance to the next node in
1146 * the stream, exposing its properties.
1147 *
1148 * Returns 1 if the node was read successfully, 0 if there is no more
1149 * nodes to read, or -1 in case of error
1150 */
1151int
1152xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +00001153 int val, olddepth = 0;
William M. Brack899e64a2003-09-26 18:03:42 +00001154 xmlTextReaderState oldstate = XML_TEXTREADER_START;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001155 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001156
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001157
1158 if (reader == NULL)
1159 return(-1);
1160 if (reader->doc != NULL)
1161 return(xmlTextReaderReadTree(reader));
1162 if (reader->ctxt == NULL)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001163 return(-1);
1164 if (reader->ctxt->wellFormed != 1)
1165 return(-1);
1166
1167#ifdef DEBUG_READER
1168 fprintf(stderr, "\nREAD ");
1169 DUMP_READER
1170#endif
Daniel Veillard29b3e282002-12-29 11:14:41 +00001171 reader->curnode = NULL;
Daniel Veillard67df8092002-12-16 22:04:11 +00001172 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1173 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001174 /*
1175 * Initial state
1176 */
1177 do {
1178 val = xmlTextReaderPushData(reader);
1179 if (val < 0)
1180 return(-1);
1181 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001182 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1183 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001184 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001185 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001186 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001187 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001188 if (reader->node == NULL)
1189 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001190 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001191 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +00001192 if (reader->ctxt->myDoc != NULL) {
1193 reader->node = reader->ctxt->myDoc->children;
1194 }
1195 if (reader->node == NULL)
1196 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +00001197 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001198 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001199 reader->depth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001200 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001201 }
1202 oldstate = reader->state;
1203 olddepth = reader->ctxt->nodeNr;
1204 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001205
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001206get_next_node:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001207 /*
1208 * If we are not backtracking on ancestors or examined nodes,
1209 * that the parser didn't finished or that we arent at the end
1210 * of stream, continue processing.
1211 */
Daniel Veillarda880b122003-04-21 21:36:41 +00001212 while ((reader->node->next == NULL) &&
1213 (reader->ctxt->nodeNr == olddepth) &&
1214 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +00001215 (reader->node->children == NULL) ||
1216 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard409a8142003-07-18 15:16:57 +00001217 ((reader->node->children != NULL) &&
1218 (reader->node->children->type == XML_TEXT_NODE) &&
1219 (reader->node->children->next == NULL)) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001220 (reader->node->type == XML_DTD_NODE) ||
1221 (reader->node->type == XML_DOCUMENT_NODE) ||
1222 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001223 ((reader->ctxt->node == NULL) ||
1224 (reader->ctxt->node == reader->node) ||
1225 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +00001226 (reader->ctxt->instate != XML_PARSER_EOF)) {
1227 val = xmlTextReaderPushData(reader);
1228 if (val < 0)
1229 return(-1);
1230 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001231 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001232 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001233 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001234 if ((reader->node->children != NULL) &&
1235 (reader->node->type != XML_ENTITY_REF_NODE) &&
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001236 (reader->node->type != XML_XINCLUDE_START) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001237 (reader->node->type != XML_DTD_NODE)) {
1238 reader->node = reader->node->children;
1239 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001240 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001241 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001242 }
1243 }
1244 if (reader->node->next != NULL) {
1245 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +00001246 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001247 (reader->node->children == NULL) &&
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001248 ((reader->node->extra & NODE_IS_EMPTY) == 0) &&
1249 (reader->in_xinclude <= 0)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001250 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001251 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001252 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001253#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001254 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001255 (reader->node->type == XML_ELEMENT_NODE))
1256 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001257#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001258 reader->node = reader->node->next;
1259 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001260
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001261 /*
1262 * Cleanup of the old node
1263 */
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001264 if ((reader->node->prev != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001265 (reader->node->prev->type != XML_DTD_NODE) &&
1266 (reader->entNr == 0)) {
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001267 xmlNodePtr tmp = reader->node->prev;
Daniel Veillarde8039df2003-10-27 11:25:13 +00001268 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001269 xmlUnlinkNode(tmp);
1270 xmlTextReaderFreeNode(reader, tmp);
1271 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001272 }
1273
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001274 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001275 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001276 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +00001277 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001278 (reader->node->children == NULL) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001279 ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001280 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001281 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001282 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001283#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001284 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001285 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001286#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001287 reader->node = reader->node->parent;
1288 if ((reader->node == NULL) ||
1289 (reader->node->type == XML_DOCUMENT_NODE) ||
1290#ifdef LIBXML_DOCB_ENABLED
1291 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
1292#endif
1293 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001294 if (reader->mode != XML_TEXTREADER_DONE) {
1295 val = xmlParseChunk(reader->ctxt, "", 0, 1);
1296 reader->mode = XML_TEXTREADER_DONE;
1297 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001298 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001299 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001300
1301 /*
1302 * Cleanup of the old node
1303 */
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001304 if ((oldnode->type != XML_DTD_NODE) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001305 ((oldnode->extra & NODE_IS_PRESERVED) == 0) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001306 (reader->entNr == 0)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001307 xmlUnlinkNode(oldnode);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001308 xmlTextReaderFreeNode(reader, oldnode);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001309 }
1310
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001311 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001312 }
1313 reader->depth--;
1314 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001315
1316node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001317 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001318
1319 /*
Daniel Veillarda880b122003-04-21 21:36:41 +00001320 * If we are in the middle of a piece of CDATA make sure it's finished
1321 */
1322 if ((reader->node != NULL) &&
Daniel Veillardccc4d2b2003-09-17 21:27:31 +00001323 (reader->node->next == NULL) &&
Daniel Veillarda880b122003-04-21 21:36:41 +00001324 ((reader->node->type == XML_TEXT_NODE) ||
1325 (reader->node->type == XML_CDATA_SECTION_NODE))) {
1326 xmlTextReaderExpand(reader);
1327 }
1328
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001329#ifdef LIBXML_XINCLUDE_ENABLED
1330 /*
1331 * Handle XInclude if asked for
1332 */
1333 if ((reader->xinclude) && (reader->node != NULL) &&
1334 (reader->node->type == XML_ELEMENT_NODE) &&
1335 (reader->node->ns != NULL) &&
1336 (xmlStrEqual(reader->node->ns->href, XINCLUDE_NS))) {
1337 if (reader->xincctxt == NULL) {
1338 reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
1339 }
1340 /*
1341 * expand that node and process it
1342 */
1343 xmlTextReaderExpand(reader);
1344 xmlXIncludeProcessNode(reader->xincctxt, reader->node);
1345 }
1346 if (reader->node->type == XML_XINCLUDE_START) {
1347 reader->in_xinclude++;
1348 goto get_next_node;
1349 }
1350 if (reader->node->type == XML_XINCLUDE_END) {
1351 reader->in_xinclude--;
1352 goto get_next_node;
1353 }
1354#endif
Daniel Veillarda880b122003-04-21 21:36:41 +00001355 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001356 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001357 */
1358 if ((reader->node != NULL) &&
1359 (reader->node->type == XML_ENTITY_REF_NODE) &&
1360 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1361 /*
1362 * Case where the underlying tree is not availble, lookup the entity
1363 * and walk it.
1364 */
1365 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
1366 (reader->ctxt->sax->getEntity != NULL)) {
1367 reader->node->children = (xmlNodePtr)
1368 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
1369 }
1370
1371 if ((reader->node->children != NULL) &&
1372 (reader->node->children->type == XML_ENTITY_DECL) &&
1373 (reader->node->children->children != NULL)) {
1374 xmlTextReaderEntPush(reader, reader->node);
1375 reader->node = reader->node->children->children;
1376 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001377#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001378 } else if ((reader->node != NULL) &&
1379 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +00001380 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001381 xmlTextReaderValidateEntity(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001382#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001383 }
1384 if ((reader->node != NULL) &&
1385 (reader->node->type == XML_ENTITY_DECL) &&
1386 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1387 reader->node = xmlTextReaderEntPop(reader);
1388 reader->depth++;
1389 goto get_next_node;
1390 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001391#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001392 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001393 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001394
1395 if ((node->type == XML_ELEMENT_NODE) &&
1396 ((reader->state != XML_TEXTREADER_END) &&
1397 (reader->state != XML_TEXTREADER_BACKTRACK))) {
1398 xmlTextReaderValidatePush(reader);
1399 } else if ((node->type == XML_TEXT_NODE) ||
1400 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001401 xmlTextReaderValidateCData(reader, node->content,
1402 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001403 }
1404 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001405#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001406 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001407node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001408 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001409 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001410}
1411
Daniel Veillard67df8092002-12-16 22:04:11 +00001412/**
1413 * xmlTextReaderReadState:
1414 * @reader: the xmlTextReaderPtr used
1415 *
1416 * Gets the read state of the reader.
1417 *
1418 * Returns the state value, or -1 in case of error
1419 */
1420int
1421xmlTextReaderReadState(xmlTextReaderPtr reader) {
1422 if (reader == NULL)
1423 return(-1);
1424 return(reader->mode);
1425}
1426
1427/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001428 * xmlTextReaderExpand:
1429 * @reader: the xmlTextReaderPtr used
1430 *
1431 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +00001432 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001433 *
1434 * Returns a node pointer valid until the next xmlTextReaderRead() call
1435 * or NULL in case of error.
1436 */
1437xmlNodePtr
1438xmlTextReaderExpand(xmlTextReaderPtr reader) {
1439 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1440 return(NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001441 if (reader->doc != NULL)
1442 return(reader->node);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001443 if (xmlTextReaderDoExpand(reader) < 0)
1444 return(NULL);
1445 return(reader->node);
1446}
1447
1448/**
1449 * xmlTextReaderNext:
1450 * @reader: the xmlTextReaderPtr used
1451 *
1452 * Skip to the node following the current one in document order while
1453 * avoiding the subtree if any.
1454 *
1455 * Returns 1 if the node was read successfully, 0 if there is no more
1456 * nodes to read, or -1 in case of error
1457 */
1458int
1459xmlTextReaderNext(xmlTextReaderPtr reader) {
1460 int ret;
1461 xmlNodePtr cur;
1462
1463 if (reader == NULL)
1464 return(-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001465 if (reader->doc != NULL)
1466 return(xmlTextReaderNextTree(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001467 cur = reader->node;
1468 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1469 return(xmlTextReaderRead(reader));
1470 if (reader->state == XML_TEXTREADER_END)
1471 return(xmlTextReaderRead(reader));
Daniel Veillarde8039df2003-10-27 11:25:13 +00001472 if (cur->extra & NODE_IS_EMPTY)
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001473 return(xmlTextReaderRead(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001474 do {
1475 ret = xmlTextReaderRead(reader);
1476 if (ret != 1)
1477 return(ret);
1478 } while (reader->node != cur);
1479 return(xmlTextReaderRead(reader));
1480}
1481
1482/**
Daniel Veillard67df8092002-12-16 22:04:11 +00001483 * xmlTextReaderReadInnerXml:
1484 * @reader: the xmlTextReaderPtr used
1485 *
1486 * Reads the contents of the current node, including child nodes and markup.
1487 *
1488 * Returns a string containing the XML content, or NULL if the current node
1489 * is neither an element nor attribute, or has no child nodes. The
1490 * string must be deallocated by the caller.
1491 */
1492xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001493xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001494 TODO
1495 return(NULL);
1496}
1497
1498/**
1499 * xmlTextReaderReadOuterXml:
1500 * @reader: the xmlTextReaderPtr used
1501 *
1502 * Reads the contents of the current node, including child nodes and markup.
1503 *
1504 * Returns a string containing the XML content, or NULL if the current node
1505 * is neither an element nor attribute, or has no child nodes. The
1506 * string must be deallocated by the caller.
1507 */
1508xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001509xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001510 TODO
1511 return(NULL);
1512}
1513
1514/**
1515 * xmlTextReaderReadString:
1516 * @reader: the xmlTextReaderPtr used
1517 *
1518 * Reads the contents of an element or a text node as a string.
1519 *
1520 * Returns a string containing the contents of the Element or Text node,
1521 * or NULL if the reader is positioned on any other type of node.
1522 * The string must be deallocated by the caller.
1523 */
1524xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001525xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001526 TODO
1527 return(NULL);
1528}
1529
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001530#if 0
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001531/**
1532 * xmlTextReaderReadBase64:
1533 * @reader: the xmlTextReaderPtr used
1534 * @array: a byte array to store the content.
1535 * @offset: the zero-based index into array where the method should
1536 * begin to write.
1537 * @len: the number of bytes to write.
1538 *
1539 * Reads and decodes the Base64 encoded contents of an element and
1540 * stores the result in a byte buffer.
1541 *
1542 * Returns the number of bytes written to array, or zero if the current
1543 * instance is not positioned on an element or -1 in case of error.
1544 */
1545int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001546xmlTextReaderReadBase64(xmlTextReaderPtr reader,
1547 unsigned char *array ATTRIBUTE_UNUSED,
1548 int offset ATTRIBUTE_UNUSED,
1549 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001550 if ((reader == NULL) || (reader->ctxt == NULL))
1551 return(-1);
1552 if (reader->ctxt->wellFormed != 1)
1553 return(-1);
1554
1555 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1556 return(0);
1557 TODO
1558 return(0);
1559}
1560
1561/**
1562 * xmlTextReaderReadBinHex:
1563 * @reader: the xmlTextReaderPtr used
1564 * @array: a byte array to store the content.
1565 * @offset: the zero-based index into array where the method should
1566 * begin to write.
1567 * @len: the number of bytes to write.
1568 *
1569 * Reads and decodes the BinHex encoded contents of an element and
1570 * stores the result in a byte buffer.
1571 *
1572 * Returns the number of bytes written to array, or zero if the current
1573 * instance is not positioned on an element or -1 in case of error.
1574 */
1575int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001576xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
1577 unsigned char *array ATTRIBUTE_UNUSED,
1578 int offset ATTRIBUTE_UNUSED,
1579 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001580 if ((reader == NULL) || (reader->ctxt == NULL))
1581 return(-1);
1582 if (reader->ctxt->wellFormed != 1)
1583 return(-1);
1584
1585 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1586 return(0);
1587 TODO
1588 return(0);
1589}
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001590#endif
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001591
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001592/************************************************************************
1593 * *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001594 * Operating on a preparsed tree *
1595 * *
1596 ************************************************************************/
1597static int
1598xmlTextReaderNextTree(xmlTextReaderPtr reader)
1599{
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001600 if (reader == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001601 return(-1);
1602
1603 if (reader->state == XML_TEXTREADER_END)
1604 return(0);
1605
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001606 if (reader->node == NULL) {
1607 if (reader->doc->children == NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001608 reader->state = XML_TEXTREADER_END;
1609 return(0);
1610 }
1611
1612 reader->node = reader->doc->children;
1613 reader->state = XML_TEXTREADER_START;
1614 return(1);
1615 }
1616
1617 if (reader->state != XML_TEXTREADER_BACKTRACK) {
1618 if (reader->node->children != 0) {
1619 reader->node = reader->node->children;
1620 reader->depth++;
1621 reader->state = XML_TEXTREADER_START;
1622 return(1);
1623 }
1624
1625 if ((reader->node->type == XML_ELEMENT_NODE) ||
1626 (reader->node->type == XML_ATTRIBUTE_NODE)) {
1627 reader->state = XML_TEXTREADER_BACKTRACK;
1628 return(1);
1629 }
1630 }
1631
1632 if (reader->node->next != 0) {
1633 reader->node = reader->node->next;
1634 reader->state = XML_TEXTREADER_START;
1635 return(1);
1636 }
1637
1638 if (reader->node->parent != 0) {
1639 if (reader->node->parent->type == XML_DOCUMENT_NODE) {
1640 reader->state = XML_TEXTREADER_END;
1641 return(0);
1642 }
1643
1644 reader->node = reader->node->parent;
1645 reader->depth--;
1646 reader->state = XML_TEXTREADER_BACKTRACK;
1647 return(1);
1648 }
1649
1650 reader->state = XML_TEXTREADER_END;
1651
1652 return(1);
1653}
1654
1655/**
1656 * xmlTextReaderReadTree:
1657 * @reader: the xmlTextReaderPtr used
1658 *
1659 * Moves the position of the current instance to the next node in
1660 * the stream, exposing its properties.
1661 *
1662 * Returns 1 if the node was read successfully, 0 if there is no more
1663 * nodes to read, or -1 in case of error
1664 */
1665static int
1666xmlTextReaderReadTree(xmlTextReaderPtr reader) {
1667 if (reader->state == XML_TEXTREADER_END)
1668 return(0);
1669
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001670next_node:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001671 if (reader->node == NULL) {
1672 if (reader->doc->children == NULL) {
1673 reader->state = XML_TEXTREADER_END;
1674 return(0);
1675 }
1676
1677 reader->node = reader->doc->children;
1678 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001679 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001680 }
1681
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001682 if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
1683 (reader->node->type != XML_DTD_NODE) &&
1684 (reader->node->type != XML_XINCLUDE_START) &&
1685 (reader->node->type != XML_ENTITY_REF_NODE)) {
1686 if (reader->node->children != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001687 reader->node = reader->node->children;
1688 reader->depth++;
1689 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001690 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001691 }
1692
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001693 if (reader->node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001694 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001695 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001696 }
1697 }
1698
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001699 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001700 reader->node = reader->node->next;
1701 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001702 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001703 }
1704
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001705 if (reader->node->parent != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001706 if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
1707 (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
1708 reader->state = XML_TEXTREADER_END;
1709 return(0);
1710 }
1711
1712 reader->node = reader->node->parent;
1713 reader->depth--;
1714 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001715 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001716 }
1717
1718 reader->state = XML_TEXTREADER_END;
1719
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001720found_node:
1721 if ((reader->node->type == XML_XINCLUDE_START) ||
1722 (reader->node->type == XML_XINCLUDE_END))
1723 goto next_node;
1724
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001725 return(1);
1726}
1727
1728/**
William M. Brackb1d53162003-11-18 06:54:40 +00001729 * xmlTextReaderNextSibling:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001730 * @reader: the xmlTextReaderPtr used
1731 *
1732 * Skip to the node following the current one in document order while
1733 * avoiding the subtree if any.
1734 * Currently implemented only for Readers built on a document
1735 *
1736 * Returns 1 if the node was read successfully, 0 if there is no more
1737 * nodes to read, or -1 in case of error
1738 */
1739int
1740xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
1741 if (reader == NULL)
1742 return(-1);
1743 if (reader->doc == NULL) {
1744 TODO
1745 return(-1);
1746 }
1747
1748 if (reader->state == XML_TEXTREADER_END)
1749 return(0);
1750
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001751 if (reader->node == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001752 return(xmlTextReaderNextTree(reader));
1753
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001754 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001755 reader->node = reader->node->next;
1756 reader->state = XML_TEXTREADER_START;
1757 return(1);
1758 }
1759
1760 return(0);
1761}
1762
1763/************************************************************************
1764 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001765 * Constructor and destructors *
1766 * *
1767 ************************************************************************/
1768/**
1769 * xmlNewTextReader:
1770 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001771 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001772 *
1773 * Create an xmlTextReader structure fed with @input
1774 *
1775 * Returns the new xmlTextReaderPtr or NULL in case of error
1776 */
1777xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001778xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001779 xmlTextReaderPtr ret;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001780
1781 if (input == NULL)
1782 return(NULL);
1783 ret = xmlMalloc(sizeof(xmlTextReader));
1784 if (ret == NULL) {
1785 xmlGenericError(xmlGenericErrorContext,
1786 "xmlNewTextReader : malloc failed\n");
1787 return(NULL);
1788 }
1789 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001790 ret->doc = NULL;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001791 ret->entTab = NULL;
1792 ret->entMax = 0;
1793 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001794 ret->input = input;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001795 ret->buffer = xmlBufferCreateSize(100);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001796 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1797 if (ret->sax == NULL) {
1798 xmlFree(ret);
1799 xmlGenericError(xmlGenericErrorContext,
1800 "xmlNewTextReader : malloc failed\n");
1801 return(NULL);
1802 }
Daniel Veillard81273902003-09-30 00:43:48 +00001803 xmlSAXVersion(ret->sax, 2);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001804 ret->startElement = ret->sax->startElement;
1805 ret->sax->startElement = xmlTextReaderStartElement;
1806 ret->endElement = ret->sax->endElement;
1807 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillard81273902003-09-30 00:43:48 +00001808#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001809 if (ret->sax->initialized == XML_SAX2_MAGIC) {
Daniel Veillard81273902003-09-30 00:43:48 +00001810#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001811 ret->startElementNs = ret->sax->startElementNs;
1812 ret->sax->startElementNs = xmlTextReaderStartElementNs;
1813 ret->endElementNs = ret->sax->endElementNs;
1814 ret->sax->endElementNs = xmlTextReaderEndElementNs;
Daniel Veillard81273902003-09-30 00:43:48 +00001815#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001816 } else {
1817 ret->startElementNs = NULL;
1818 ret->endElementNs = NULL;
1819 }
Daniel Veillard81273902003-09-30 00:43:48 +00001820#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001821 ret->characters = ret->sax->characters;
1822 ret->sax->characters = xmlTextReaderCharacters;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001823 ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001824 ret->cdataBlock = ret->sax->cdataBlock;
1825 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001826
Daniel Veillard67df8092002-12-16 22:04:11 +00001827 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001828 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001829 ret->curnode = NULL;
Daniel Veillard53350552003-09-18 13:35:51 +00001830 if (ret->input->buffer->use < 4) {
William M. Brack899e64a2003-09-26 18:03:42 +00001831 xmlParserInputBufferRead(input, 4);
Daniel Veillard53350552003-09-18 13:35:51 +00001832 }
1833 if (ret->input->buffer->use >= 4) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001834 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001835 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001836 ret->base = 0;
1837 ret->cur = 4;
1838 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001839 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001840 ret->base = 0;
1841 ret->cur = 0;
1842 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001843 if (ret->ctxt == NULL) {
1844 xmlGenericError(xmlGenericErrorContext,
1845 "xmlNewTextReader : malloc failed\n");
1846 xmlFree(ret->sax);
1847 xmlFree(ret);
1848 return(NULL);
1849 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001850 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001851 ret->ctxt->linenumbers = 1;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001852 ret->ctxt->dictNames = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001853 ret->allocs = XML_TEXTREADER_CTXT;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001854 /*
1855 * use the parser dictionnary to allocate all elements and attributes names
1856 */
1857 ret->ctxt->docdict = 1;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001858 ret->dict = ret->ctxt->dict;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001859#ifdef LIBXML_XINCLUDE_ENABLED
1860 ret->xinclude = 0;
1861#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001862 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001863}
1864
1865/**
1866 * xmlNewTextReaderFilename:
1867 * @URI: the URI of the resource to process
1868 *
1869 * Create an xmlTextReader structure fed with the resource at @URI
1870 *
1871 * Returns the new xmlTextReaderPtr or NULL in case of error
1872 */
1873xmlTextReaderPtr
1874xmlNewTextReaderFilename(const char *URI) {
1875 xmlParserInputBufferPtr input;
1876 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001877 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001878
1879 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1880 if (input == NULL)
1881 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001882 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001883 if (ret == NULL) {
1884 xmlFreeParserInputBuffer(input);
1885 return(NULL);
1886 }
1887 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001888 if (ret->ctxt->directory == NULL)
1889 directory = xmlParserGetDirectory(URI);
1890 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1891 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1892 if (directory != NULL)
1893 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001894 return(ret);
1895}
1896
1897/**
1898 * xmlFreeTextReader:
1899 * @reader: the xmlTextReaderPtr
1900 *
1901 * Deallocate all the resources associated to the reader
1902 */
1903void
1904xmlFreeTextReader(xmlTextReaderPtr reader) {
1905 if (reader == NULL)
1906 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001907#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001908 if (reader->rngSchemas != NULL) {
1909 xmlRelaxNGFree(reader->rngSchemas);
1910 reader->rngSchemas = NULL;
1911 }
1912 if (reader->rngValidCtxt != NULL) {
1913 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
1914 reader->rngValidCtxt = NULL;
1915 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001916#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001917#ifdef LIBXML_XINCLUDE_ENABLED
1918 if (reader->xincctxt != NULL)
1919 xmlXIncludeFreeContext(reader->xincctxt);
1920#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001921 if (reader->ctxt != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001922 if (reader->dict == reader->ctxt->dict)
1923 reader->dict = NULL;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001924 if (reader->ctxt->myDoc != NULL) {
1925 if (reader->preserve == 0)
1926 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
1927 reader->ctxt->myDoc = NULL;
1928 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00001929 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
1930 (reader->ctxt->vctxt.vstateMax > 0)){
1931 xmlFree(reader->ctxt->vctxt.vstateTab);
1932 reader->ctxt->vctxt.vstateTab = 0;
1933 reader->ctxt->vctxt.vstateMax = 0;
1934 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001935 if (reader->allocs & XML_TEXTREADER_CTXT)
1936 xmlFreeParserCtxt(reader->ctxt);
1937 }
1938 if (reader->sax != NULL)
1939 xmlFree(reader->sax);
1940 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
1941 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001942 if (reader->faketext != NULL) {
1943 xmlFreeNode(reader->faketext);
1944 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001945 if (reader->buffer != NULL)
1946 xmlBufferFree(reader->buffer);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001947 if (reader->entTab != NULL)
1948 xmlFree(reader->entTab);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001949 if (reader->dict != NULL)
1950 xmlDictFree(reader->dict);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001951 xmlFree(reader);
1952}
1953
1954/************************************************************************
1955 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001956 * Methods for XmlTextReader *
1957 * *
1958 ************************************************************************/
1959/**
1960 * xmlTextReaderClose:
1961 * @reader: the xmlTextReaderPtr used
1962 *
1963 * This method releases any resources allocated by the current instance
1964 * changes the state to Closed and close any underlying input.
1965 *
1966 * Returns 0 or -1 in case of error
1967 */
1968int
1969xmlTextReaderClose(xmlTextReaderPtr reader) {
1970 if (reader == NULL)
1971 return(-1);
1972 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001973 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001974 reader->mode = XML_TEXTREADER_MODE_CLOSED;
1975 if (reader->ctxt != NULL) {
1976 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001977 if (reader->preserve == 0)
1978 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001979 reader->ctxt->myDoc = NULL;
1980 }
1981 if (reader->allocs & XML_TEXTREADER_CTXT) {
1982 xmlFreeParserCtxt(reader->ctxt);
1983 reader->allocs -= XML_TEXTREADER_CTXT;
1984 }
1985 }
1986 if (reader->sax != NULL) {
1987 xmlFree(reader->sax);
1988 reader->sax = NULL;
1989 }
1990 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
1991 xmlFreeParserInputBuffer(reader->input);
1992 reader->allocs -= XML_TEXTREADER_INPUT;
1993 }
1994 return(0);
1995}
1996
1997/**
1998 * xmlTextReaderGetAttributeNo:
1999 * @reader: the xmlTextReaderPtr used
2000 * @no: the zero-based index of the attribute relative to the containing element
2001 *
2002 * Provides the value of the attribute with the specified index relative
2003 * to the containing element.
2004 *
2005 * Returns a string containing the value of the specified attribute, or NULL
2006 * in case of error. The string must be deallocated by the caller.
2007 */
2008xmlChar *
2009xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
2010 xmlChar *ret;
2011 int i;
2012 xmlAttrPtr cur;
2013 xmlNsPtr ns;
2014
2015 if (reader == NULL)
2016 return(NULL);
2017 if (reader->node == NULL)
2018 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002019 if (reader->curnode != NULL)
2020 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002021 /* TODO: handle the xmlDecl */
2022 if (reader->node->type != XML_ELEMENT_NODE)
2023 return(NULL);
2024
2025 ns = reader->node->nsDef;
2026 for (i = 0;(i < no) && (ns != NULL);i++) {
2027 ns = ns->next;
2028 }
2029 if (ns != NULL)
2030 return(xmlStrdup(ns->href));
2031
2032 cur = reader->node->properties;
2033 if (cur == NULL)
2034 return(NULL);
2035 for (;i < no;i++) {
2036 cur = cur->next;
2037 if (cur == NULL)
2038 return(NULL);
2039 }
2040 /* TODO walk the DTD if present */
2041
2042 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
2043 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
2044 return(ret);
2045}
2046
2047/**
2048 * xmlTextReaderGetAttribute:
2049 * @reader: the xmlTextReaderPtr used
2050 * @name: the qualified name of the attribute.
2051 *
2052 * Provides the value of the attribute with the specified qualified name.
2053 *
2054 * Returns a string containing the value of the specified attribute, or NULL
2055 * in case of error. The string must be deallocated by the caller.
2056 */
2057xmlChar *
2058xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2059 xmlChar *prefix = NULL;
2060 xmlChar *localname;
2061 xmlNsPtr ns;
2062 xmlChar *ret = NULL;
2063
2064 if ((reader == NULL) || (name == NULL))
2065 return(NULL);
2066 if (reader->node == NULL)
2067 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002068 if (reader->curnode != NULL)
2069 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002070
2071 /* TODO: handle the xmlDecl */
2072 if (reader->node->type != XML_ELEMENT_NODE)
2073 return(NULL);
2074
2075 localname = xmlSplitQName2(name, &prefix);
2076 if (localname == NULL)
2077 return(xmlGetProp(reader->node, name));
2078
2079 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2080 if (ns != NULL)
2081 ret = xmlGetNsProp(reader->node, localname, ns->href);
2082
2083 if (localname != NULL)
2084 xmlFree(localname);
2085 if (prefix != NULL)
2086 xmlFree(prefix);
2087 return(ret);
2088}
2089
2090
2091/**
2092 * xmlTextReaderGetAttributeNs:
2093 * @reader: the xmlTextReaderPtr used
2094 * @localName: the local name of the attribute.
2095 * @namespaceURI: the namespace URI of the attribute.
2096 *
2097 * Provides the value of the specified attribute
2098 *
2099 * Returns a string containing the value of the specified attribute, or NULL
2100 * in case of error. The string must be deallocated by the caller.
2101 */
2102xmlChar *
2103xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
2104 const xmlChar *namespaceURI) {
2105 if ((reader == NULL) || (localName == NULL))
2106 return(NULL);
2107 if (reader->node == NULL)
2108 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002109 if (reader->curnode != NULL)
2110 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002111
2112 /* TODO: handle the xmlDecl */
2113 if (reader->node->type != XML_ELEMENT_NODE)
2114 return(NULL);
2115
2116 return(xmlGetNsProp(reader->node, localName, namespaceURI));
2117}
2118
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002119/**
2120 * xmlTextReaderGetRemainder:
2121 * @reader: the xmlTextReaderPtr used
2122 *
2123 * Method to get the remainder of the buffered XML. this method stops the
2124 * parser, set its state to End Of File and return the input stream with
2125 * what is left that the parser did not use.
2126 *
2127 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
2128 * in case of error.
2129 */
2130xmlParserInputBufferPtr
2131xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
2132 xmlParserInputBufferPtr ret = NULL;
2133
2134 if (reader == NULL)
2135 return(NULL);
2136 if (reader->node == NULL)
2137 return(NULL);
2138
2139 reader->node = NULL;
2140 reader->curnode = NULL;
2141 reader->mode = XML_TEXTREADER_MODE_EOF;
2142 if (reader->ctxt != NULL) {
2143 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002144 if (reader->preserve == 0)
2145 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002146 reader->ctxt->myDoc = NULL;
2147 }
2148 if (reader->allocs & XML_TEXTREADER_CTXT) {
2149 xmlFreeParserCtxt(reader->ctxt);
2150 reader->allocs -= XML_TEXTREADER_CTXT;
2151 }
2152 }
2153 if (reader->sax != NULL) {
2154 xmlFree(reader->sax);
2155 reader->sax = NULL;
2156 }
2157 if (reader->allocs & XML_TEXTREADER_INPUT) {
2158 ret = reader->input;
2159 reader->allocs -= XML_TEXTREADER_INPUT;
2160 } else {
2161 /*
2162 * Hum, one may need to duplicate the data structure because
2163 * without reference counting the input may be freed twice:
2164 * - by the layer which allocated it.
2165 * - by the layer to which would have been returned to.
2166 */
2167 TODO
2168 return(NULL);
2169 }
2170 return(ret);
2171}
2172
2173/**
2174 * xmlTextReaderLookupNamespace:
2175 * @reader: the xmlTextReaderPtr used
2176 * @prefix: the prefix whose namespace URI is to be resolved. To return
2177 * the default namespace, specify NULL
2178 *
2179 * Resolves a namespace prefix in the scope of the current element.
2180 *
2181 * Returns a string containing the namespace URI to which the prefix maps
2182 * or NULL in case of error. The string must be deallocated by the caller.
2183 */
2184xmlChar *
2185xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
2186 xmlNsPtr ns;
2187
2188 if (reader == NULL)
2189 return(NULL);
2190 if (reader->node == NULL)
2191 return(NULL);
2192
2193 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2194 if (ns == NULL)
2195 return(NULL);
2196 return(xmlStrdup(ns->href));
2197}
2198
2199/**
2200 * xmlTextReaderMoveToAttributeNo:
2201 * @reader: the xmlTextReaderPtr used
2202 * @no: the zero-based index of the attribute relative to the containing
2203 * element.
2204 *
2205 * Moves the position of the current instance to the attribute with
2206 * the specified index relative to the containing element.
2207 *
2208 * Returns 1 in case of success, -1 in case of error, 0 if not found
2209 */
2210int
2211xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
2212 int i;
2213 xmlAttrPtr cur;
2214 xmlNsPtr ns;
2215
2216 if (reader == NULL)
2217 return(-1);
2218 if (reader->node == NULL)
2219 return(-1);
2220 /* TODO: handle the xmlDecl */
2221 if (reader->node->type != XML_ELEMENT_NODE)
2222 return(-1);
2223
2224 reader->curnode = NULL;
2225
2226 ns = reader->node->nsDef;
2227 for (i = 0;(i < no) && (ns != NULL);i++) {
2228 ns = ns->next;
2229 }
2230 if (ns != NULL) {
2231 reader->curnode = (xmlNodePtr) ns;
2232 return(1);
2233 }
2234
2235 cur = reader->node->properties;
2236 if (cur == NULL)
2237 return(0);
2238 for (;i < no;i++) {
2239 cur = cur->next;
2240 if (cur == NULL)
2241 return(0);
2242 }
2243 /* TODO walk the DTD if present */
2244
2245 reader->curnode = (xmlNodePtr) cur;
2246 return(1);
2247}
2248
2249/**
2250 * xmlTextReaderMoveToAttribute:
2251 * @reader: the xmlTextReaderPtr used
2252 * @name: the qualified name of the attribute.
2253 *
2254 * Moves the position of the current instance to the attribute with
2255 * the specified qualified name.
2256 *
2257 * Returns 1 in case of success, -1 in case of error, 0 if not found
2258 */
2259int
2260xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2261 xmlChar *prefix = NULL;
2262 xmlChar *localname;
2263 xmlNsPtr ns;
2264 xmlAttrPtr prop;
2265
2266 if ((reader == NULL) || (name == NULL))
2267 return(-1);
2268 if (reader->node == NULL)
2269 return(-1);
2270
2271 /* TODO: handle the xmlDecl */
2272 if (reader->node->type != XML_ELEMENT_NODE)
2273 return(0);
2274
2275 localname = xmlSplitQName2(name, &prefix);
2276 if (localname == NULL) {
2277 /*
2278 * Namespace default decl
2279 */
2280 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2281 ns = reader->node->nsDef;
2282 while (ns != NULL) {
2283 if (ns->prefix == NULL) {
2284 reader->curnode = (xmlNodePtr) ns;
2285 return(1);
2286 }
2287 ns = ns->next;
2288 }
2289 return(0);
2290 }
2291
2292 prop = reader->node->properties;
2293 while (prop != NULL) {
2294 /*
2295 * One need to have
2296 * - same attribute names
2297 * - and the attribute carrying that namespace
2298 */
2299 if ((xmlStrEqual(prop->name, name)) &&
2300 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
2301 reader->curnode = (xmlNodePtr) prop;
2302 return(1);
2303 }
2304 prop = prop->next;
2305 }
2306 return(0);
2307 }
2308
2309 /*
2310 * Namespace default decl
2311 */
2312 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2313 ns = reader->node->nsDef;
2314 while (ns != NULL) {
2315 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2316 reader->curnode = (xmlNodePtr) ns;
2317 goto found;
2318 }
2319 ns = ns->next;
2320 }
2321 goto not_found;
2322 }
2323 prop = reader->node->properties;
2324 while (prop != NULL) {
2325 /*
2326 * One need to have
2327 * - same attribute names
2328 * - and the attribute carrying that namespace
2329 */
2330 if ((xmlStrEqual(prop->name, localname)) &&
2331 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2332 reader->curnode = (xmlNodePtr) prop;
2333 goto found;
2334 }
2335 prop = prop->next;
2336 }
2337not_found:
2338 if (localname != NULL)
2339 xmlFree(localname);
2340 if (prefix != NULL)
2341 xmlFree(prefix);
2342 return(0);
2343
2344found:
2345 if (localname != NULL)
2346 xmlFree(localname);
2347 if (prefix != NULL)
2348 xmlFree(prefix);
2349 return(1);
2350}
2351
2352/**
2353 * xmlTextReaderMoveToAttributeNs:
2354 * @reader: the xmlTextReaderPtr used
2355 * @localName: the local name of the attribute.
2356 * @namespaceURI: the namespace URI of the attribute.
2357 *
2358 * Moves the position of the current instance to the attribute with the
2359 * specified local name and namespace URI.
2360 *
2361 * Returns 1 in case of success, -1 in case of error, 0 if not found
2362 */
2363int
2364xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
2365 const xmlChar *localName, const xmlChar *namespaceURI) {
2366 xmlAttrPtr prop;
2367 xmlNodePtr node;
2368
2369 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2370 return(-1);
2371 if (reader->node == NULL)
2372 return(-1);
2373 if (reader->node->type != XML_ELEMENT_NODE)
2374 return(0);
2375 node = reader->node;
2376
2377 /*
2378 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
2379 * namespace name associated to "xmlns"
2380 */
2381 prop = node->properties;
2382 while (prop != NULL) {
2383 /*
2384 * One need to have
2385 * - same attribute names
2386 * - and the attribute carrying that namespace
2387 */
2388 if (xmlStrEqual(prop->name, localName) &&
2389 ((prop->ns != NULL) &&
2390 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2391 reader->curnode = (xmlNodePtr) prop;
2392 return(1);
2393 }
2394 prop = prop->next;
2395 }
2396 return(0);
2397}
2398
2399/**
2400 * xmlTextReaderMoveToFirstAttribute:
2401 * @reader: the xmlTextReaderPtr used
2402 *
2403 * Moves the position of the current instance to the first attribute
2404 * associated with the current node.
2405 *
2406 * Returns 1 in case of success, -1 in case of error, 0 if not found
2407 */
2408int
2409xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
2410 if (reader == NULL)
2411 return(-1);
2412 if (reader->node == NULL)
2413 return(-1);
2414 if (reader->node->type != XML_ELEMENT_NODE)
2415 return(0);
2416
2417 if (reader->node->nsDef != NULL) {
2418 reader->curnode = (xmlNodePtr) reader->node->nsDef;
2419 return(1);
2420 }
2421 if (reader->node->properties != NULL) {
2422 reader->curnode = (xmlNodePtr) reader->node->properties;
2423 return(1);
2424 }
2425 return(0);
2426}
2427
2428/**
2429 * xmlTextReaderMoveToNextAttribute:
2430 * @reader: the xmlTextReaderPtr used
2431 *
2432 * Moves the position of the current instance to the next attribute
2433 * associated with the current node.
2434 *
2435 * Returns 1 in case of success, -1 in case of error, 0 if not found
2436 */
2437int
2438xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
2439 if (reader == NULL)
2440 return(-1);
2441 if (reader->node == NULL)
2442 return(-1);
2443 if (reader->node->type != XML_ELEMENT_NODE)
2444 return(0);
2445 if (reader->curnode == NULL)
2446 return(xmlTextReaderMoveToFirstAttribute(reader));
2447
2448 if (reader->curnode->type == XML_NAMESPACE_DECL) {
2449 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2450 if (ns->next != NULL) {
2451 reader->curnode = (xmlNodePtr) ns->next;
2452 return(1);
2453 }
2454 if (reader->node->properties != NULL) {
2455 reader->curnode = (xmlNodePtr) reader->node->properties;
2456 return(1);
2457 }
2458 return(0);
2459 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2460 (reader->curnode->next != NULL)) {
2461 reader->curnode = reader->curnode->next;
2462 return(1);
2463 }
2464 return(0);
2465}
2466
2467/**
2468 * xmlTextReaderMoveToElement:
2469 * @reader: the xmlTextReaderPtr used
2470 *
2471 * Moves the position of the current instance to the node that
2472 * contains the current Attribute node.
2473 *
2474 * Returns 1 in case of success, -1 in case of error, 0 if not moved
2475 */
2476int
2477xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
2478 if (reader == NULL)
2479 return(-1);
2480 if (reader->node == NULL)
2481 return(-1);
2482 if (reader->node->type != XML_ELEMENT_NODE)
2483 return(0);
2484 if (reader->curnode != NULL) {
2485 reader->curnode = NULL;
2486 return(1);
2487 }
2488 return(0);
2489}
2490
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002491/**
2492 * xmlTextReaderReadAttributeValue:
2493 * @reader: the xmlTextReaderPtr used
2494 *
2495 * Parses an attribute value into one or more Text and EntityReference nodes.
2496 *
2497 * Returns 1 in case of success, 0 if the reader was not positionned on an
2498 * ttribute node or all the attribute values have been read, or -1
2499 * in case of error.
2500 */
2501int
2502xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
2503 if (reader == NULL)
2504 return(-1);
2505 if (reader->node == NULL)
2506 return(-1);
2507 if (reader->curnode == NULL)
2508 return(0);
2509 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2510 if (reader->curnode->children == NULL)
2511 return(0);
2512 reader->curnode = reader->curnode->children;
2513 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2514 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2515
2516 if (reader->faketext == NULL) {
2517 reader->faketext = xmlNewDocText(reader->node->doc,
2518 ns->href);
2519 } else {
2520 if (reader->faketext->content != NULL)
2521 xmlFree(reader->faketext->content);
2522 reader->faketext->content = xmlStrdup(ns->href);
2523 }
2524 reader->curnode = reader->faketext;
2525 } else {
2526 if (reader->curnode->next == NULL)
2527 return(0);
2528 reader->curnode = reader->curnode->next;
2529 }
2530 return(1);
2531}
2532
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002533/************************************************************************
2534 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002535 * Acces API to the current node *
2536 * *
2537 ************************************************************************/
2538/**
2539 * xmlTextReaderAttributeCount:
2540 * @reader: the xmlTextReaderPtr used
2541 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002542 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002543 *
2544 * Returns 0 i no attributes, -1 in case of error or the attribute count
2545 */
2546int
2547xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
2548 int ret;
2549 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00002550 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002551 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002552
2553 if (reader == NULL)
2554 return(-1);
2555 if (reader->node == NULL)
2556 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002557
2558 if (reader->curnode != NULL)
2559 node = reader->curnode;
2560 else
2561 node = reader->node;
2562
2563 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002564 return(0);
2565 if ((reader->state == XML_TEXTREADER_END) ||
2566 (reader->state == XML_TEXTREADER_BACKTRACK))
2567 return(0);
2568 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002569 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002570 while (attr != NULL) {
2571 ret++;
2572 attr = attr->next;
2573 }
Daniel Veillard67df8092002-12-16 22:04:11 +00002574 ns = node->nsDef;
2575 while (ns != NULL) {
2576 ret++;
2577 ns = ns->next;
2578 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002579 return(ret);
2580}
2581
2582/**
2583 * xmlTextReaderNodeType:
2584 * @reader: the xmlTextReaderPtr used
2585 *
2586 * Get the node type of the current node
2587 * Reference:
2588 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
2589 *
2590 * Returns the xmlNodeType of the current node or -1 in case of error
2591 */
2592int
2593xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002594 xmlNodePtr node;
Daniel Veillardd6038e02003-07-30 16:37:18 +00002595
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002596 if (reader == NULL)
2597 return(-1);
2598 if (reader->node == NULL)
Daniel Veillardd6038e02003-07-30 16:37:18 +00002599 return(XML_READER_TYPE_NONE);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002600 if (reader->curnode != NULL)
2601 node = reader->curnode;
2602 else
2603 node = reader->node;
2604 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002605 case XML_ELEMENT_NODE:
2606 if ((reader->state == XML_TEXTREADER_END) ||
2607 (reader->state == XML_TEXTREADER_BACKTRACK))
Daniel Veillardd6038e02003-07-30 16:37:18 +00002608 return(XML_READER_TYPE_END_ELEMENT);
2609 return(XML_READER_TYPE_ELEMENT);
Daniel Veillardecaba492002-12-30 10:55:29 +00002610 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002611 case XML_ATTRIBUTE_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002612 return(XML_READER_TYPE_ATTRIBUTE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002613 case XML_TEXT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002614 if (xmlIsBlankNode(reader->node)) {
2615 if (xmlNodeGetSpacePreserve(reader->node))
2616 return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2617 else
2618 return(XML_READER_TYPE_WHITESPACE);
2619 } else {
2620 return(XML_READER_TYPE_TEXT);
2621 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002622 case XML_CDATA_SECTION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002623 return(XML_READER_TYPE_CDATA);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002624 case XML_ENTITY_REF_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002625 return(XML_READER_TYPE_ENTITY_REFERENCE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002626 case XML_ENTITY_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002627 return(XML_READER_TYPE_ENTITY);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002628 case XML_PI_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002629 return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002630 case XML_COMMENT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002631 return(XML_READER_TYPE_COMMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002632 case XML_DOCUMENT_NODE:
2633 case XML_HTML_DOCUMENT_NODE:
2634#ifdef LIBXML_DOCB_ENABLED
2635 case XML_DOCB_DOCUMENT_NODE:
2636#endif
Daniel Veillardd6038e02003-07-30 16:37:18 +00002637 return(XML_READER_TYPE_DOCUMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002638 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002639 return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002640 case XML_NOTATION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002641 return(XML_READER_TYPE_NOTATION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002642 case XML_DOCUMENT_TYPE_NODE:
2643 case XML_DTD_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002644 return(XML_READER_TYPE_DOCUMENT_TYPE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002645
2646 case XML_ELEMENT_DECL:
2647 case XML_ATTRIBUTE_DECL:
2648 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002649 case XML_XINCLUDE_START:
2650 case XML_XINCLUDE_END:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002651 return(XML_READER_TYPE_NONE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002652 }
2653 return(-1);
2654}
2655
2656/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002657 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002658 * @reader: the xmlTextReaderPtr used
2659 *
2660 * Check if the current node is empty
2661 *
2662 * Returns 1 if empty, 0 if not and -1 in case of error
2663 */
2664int
2665xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
2666 if ((reader == NULL) || (reader->node == NULL))
2667 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00002668 if (reader->node->type != XML_ELEMENT_NODE)
2669 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00002670 if (reader->curnode != NULL)
2671 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002672 if (reader->node->children != NULL)
2673 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00002674 if (reader->state == XML_TEXTREADER_END)
2675 return(0);
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002676 if (reader->doc != NULL)
2677 return(1);
2678 if (reader->in_xinclude > 0)
2679 return(1);
Daniel Veillarde8039df2003-10-27 11:25:13 +00002680 return((reader->node->extra & NODE_IS_EMPTY) != 0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002681}
2682
2683/**
2684 * xmlTextReaderLocalName:
2685 * @reader: the xmlTextReaderPtr used
2686 *
2687 * The local name of the node.
2688 *
2689 * Returns the local name or NULL if not available
2690 */
2691xmlChar *
2692xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002693 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002694 if ((reader == NULL) || (reader->node == NULL))
2695 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002696 if (reader->curnode != NULL)
2697 node = reader->curnode;
2698 else
2699 node = reader->node;
2700 if (node->type == XML_NAMESPACE_DECL) {
2701 xmlNsPtr ns = (xmlNsPtr) node;
2702 if (ns->prefix == NULL)
2703 return(xmlStrdup(BAD_CAST "xmlns"));
2704 else
2705 return(xmlStrdup(ns->prefix));
2706 }
2707 if ((node->type != XML_ELEMENT_NODE) &&
2708 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002709 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002710 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002711}
2712
2713/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002714 * xmlTextReaderConstLocalName:
2715 * @reader: the xmlTextReaderPtr used
2716 *
2717 * The local name of the node.
2718 *
2719 * Returns the local name or NULL if not available, the
2720 * string will be deallocated with the reader.
2721 */
2722const xmlChar *
2723xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
2724 xmlNodePtr node;
2725 if ((reader == NULL) || (reader->node == NULL))
2726 return(NULL);
2727 if (reader->curnode != NULL)
2728 node = reader->curnode;
2729 else
2730 node = reader->node;
2731 if (node->type == XML_NAMESPACE_DECL) {
2732 xmlNsPtr ns = (xmlNsPtr) node;
2733 if (ns->prefix == NULL)
2734 return(CONSTSTR(BAD_CAST "xmlns"));
2735 else
2736 return(ns->prefix);
2737 }
2738 if ((node->type != XML_ELEMENT_NODE) &&
2739 (node->type != XML_ATTRIBUTE_NODE))
2740 return(xmlTextReaderConstName(reader));
2741 return(node->name);
2742}
2743
2744/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002745 * xmlTextReaderName:
2746 * @reader: the xmlTextReaderPtr used
2747 *
2748 * The qualified name of the node, equal to Prefix :LocalName.
2749 *
2750 * Returns the local name or NULL if not available
2751 */
2752xmlChar *
2753xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002754 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002755 xmlChar *ret;
2756
2757 if ((reader == NULL) || (reader->node == NULL))
2758 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002759 if (reader->curnode != NULL)
2760 node = reader->curnode;
2761 else
2762 node = reader->node;
2763 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002764 case XML_ELEMENT_NODE:
2765 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002766 if ((node->ns == NULL) ||
2767 (node->ns->prefix == NULL))
2768 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002769
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002770 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002771 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002772 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002773 return(ret);
2774 case XML_TEXT_NODE:
2775 return(xmlStrdup(BAD_CAST "#text"));
2776 case XML_CDATA_SECTION_NODE:
2777 return(xmlStrdup(BAD_CAST "#cdata-section"));
2778 case XML_ENTITY_NODE:
2779 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002780 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002781 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002782 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002783 case XML_COMMENT_NODE:
2784 return(xmlStrdup(BAD_CAST "#comment"));
2785 case XML_DOCUMENT_NODE:
2786 case XML_HTML_DOCUMENT_NODE:
2787#ifdef LIBXML_DOCB_ENABLED
2788 case XML_DOCB_DOCUMENT_NODE:
2789#endif
2790 return(xmlStrdup(BAD_CAST "#document"));
2791 case XML_DOCUMENT_FRAG_NODE:
2792 return(xmlStrdup(BAD_CAST "#document-fragment"));
2793 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002794 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002795 case XML_DOCUMENT_TYPE_NODE:
2796 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002797 return(xmlStrdup(node->name));
2798 case XML_NAMESPACE_DECL: {
2799 xmlNsPtr ns = (xmlNsPtr) node;
2800
2801 ret = xmlStrdup(BAD_CAST "xmlns");
2802 if (ns->prefix == NULL)
2803 return(ret);
2804 ret = xmlStrcat(ret, BAD_CAST ":");
2805 ret = xmlStrcat(ret, ns->prefix);
2806 return(ret);
2807 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002808
2809 case XML_ELEMENT_DECL:
2810 case XML_ATTRIBUTE_DECL:
2811 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002812 case XML_XINCLUDE_START:
2813 case XML_XINCLUDE_END:
2814 return(NULL);
2815 }
2816 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002817}
2818
2819/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002820 * xmlTextReaderConstName:
2821 * @reader: the xmlTextReaderPtr used
2822 *
2823 * The qualified name of the node, equal to Prefix :LocalName.
2824 *
2825 * Returns the local name or NULL if not available, the string is
2826 * deallocated with the reader.
2827 */
2828const xmlChar *
2829xmlTextReaderConstName(xmlTextReaderPtr reader) {
2830 xmlNodePtr node;
2831
2832 if ((reader == NULL) || (reader->node == NULL))
2833 return(NULL);
2834 if (reader->curnode != NULL)
2835 node = reader->curnode;
2836 else
2837 node = reader->node;
2838 switch (node->type) {
2839 case XML_ELEMENT_NODE:
2840 case XML_ATTRIBUTE_NODE:
2841 if ((node->ns == NULL) ||
2842 (node->ns->prefix == NULL))
2843 return(node->name);
2844 return(CONSTQSTR(node->ns->prefix, node->name));
2845 case XML_TEXT_NODE:
2846 return(CONSTSTR(BAD_CAST "#text"));
2847 case XML_CDATA_SECTION_NODE:
2848 return(CONSTSTR(BAD_CAST "#cdata-section"));
2849 case XML_ENTITY_NODE:
2850 case XML_ENTITY_REF_NODE:
2851 return(CONSTSTR(node->name));
2852 case XML_PI_NODE:
2853 return(CONSTSTR(node->name));
2854 case XML_COMMENT_NODE:
2855 return(CONSTSTR(BAD_CAST "#comment"));
2856 case XML_DOCUMENT_NODE:
2857 case XML_HTML_DOCUMENT_NODE:
2858#ifdef LIBXML_DOCB_ENABLED
2859 case XML_DOCB_DOCUMENT_NODE:
2860#endif
2861 return(CONSTSTR(BAD_CAST "#document"));
2862 case XML_DOCUMENT_FRAG_NODE:
2863 return(CONSTSTR(BAD_CAST "#document-fragment"));
2864 case XML_NOTATION_NODE:
2865 return(CONSTSTR(node->name));
2866 case XML_DOCUMENT_TYPE_NODE:
2867 case XML_DTD_NODE:
2868 return(CONSTSTR(node->name));
2869 case XML_NAMESPACE_DECL: {
2870 xmlNsPtr ns = (xmlNsPtr) node;
2871
2872 if (ns->prefix == NULL)
2873 return(CONSTSTR(BAD_CAST "xmlns"));
2874 return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
2875 }
2876
2877 case XML_ELEMENT_DECL:
2878 case XML_ATTRIBUTE_DECL:
2879 case XML_ENTITY_DECL:
2880 case XML_XINCLUDE_START:
2881 case XML_XINCLUDE_END:
2882 return(NULL);
2883 }
2884 return(NULL);
2885}
2886
2887/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002888 * xmlTextReaderPrefix:
2889 * @reader: the xmlTextReaderPtr used
2890 *
2891 * A shorthand reference to the namespace associated with the node.
2892 *
2893 * Returns the prefix or NULL if not available
2894 */
2895xmlChar *
2896xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002897 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002898 if ((reader == NULL) || (reader->node == NULL))
2899 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002900 if (reader->curnode != NULL)
2901 node = reader->curnode;
2902 else
2903 node = reader->node;
2904 if (node->type == XML_NAMESPACE_DECL) {
2905 xmlNsPtr ns = (xmlNsPtr) node;
2906 if (ns->prefix == NULL)
2907 return(NULL);
2908 return(xmlStrdup(BAD_CAST "xmlns"));
2909 }
2910 if ((node->type != XML_ELEMENT_NODE) &&
2911 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002912 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00002913 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002914 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002915 return(NULL);
2916}
2917
2918/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002919 * xmlTextReaderConstPrefix:
2920 * @reader: the xmlTextReaderPtr used
2921 *
2922 * A shorthand reference to the namespace associated with the node.
2923 *
2924 * Returns the prefix or NULL if not available, the string is deallocated
2925 * with the reader.
2926 */
2927const xmlChar *
2928xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
2929 xmlNodePtr node;
2930 if ((reader == NULL) || (reader->node == NULL))
2931 return(NULL);
2932 if (reader->curnode != NULL)
2933 node = reader->curnode;
2934 else
2935 node = reader->node;
2936 if (node->type == XML_NAMESPACE_DECL) {
2937 xmlNsPtr ns = (xmlNsPtr) node;
2938 if (ns->prefix == NULL)
2939 return(NULL);
2940 return(CONSTSTR(BAD_CAST "xmlns"));
2941 }
2942 if ((node->type != XML_ELEMENT_NODE) &&
2943 (node->type != XML_ATTRIBUTE_NODE))
2944 return(NULL);
2945 if ((node->ns != NULL) && (node->ns->prefix != NULL))
2946 return(CONSTSTR(node->ns->prefix));
2947 return(NULL);
2948}
2949
2950/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002951 * xmlTextReaderNamespaceUri:
2952 * @reader: the xmlTextReaderPtr used
2953 *
2954 * The URI defining the namespace associated with the node.
2955 *
2956 * Returns the namespace URI or NULL if not available
2957 */
2958xmlChar *
2959xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002960 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002961 if ((reader == NULL) || (reader->node == NULL))
2962 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002963 if (reader->curnode != NULL)
2964 node = reader->curnode;
2965 else
2966 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00002967 if (node->type == XML_NAMESPACE_DECL)
2968 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002969 if ((node->type != XML_ELEMENT_NODE) &&
2970 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002971 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002972 if (node->ns != NULL)
2973 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002974 return(NULL);
2975}
2976
2977/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002978 * xmlTextReaderConstNamespaceUri:
2979 * @reader: the xmlTextReaderPtr used
2980 *
2981 * The URI defining the namespace associated with the node.
2982 *
2983 * Returns the namespace URI or NULL if not available, the string
2984 * will be deallocated with the reader
2985 */
2986const xmlChar *
2987xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
2988 xmlNodePtr node;
2989 if ((reader == NULL) || (reader->node == NULL))
2990 return(NULL);
2991 if (reader->curnode != NULL)
2992 node = reader->curnode;
2993 else
2994 node = reader->node;
2995 if (node->type == XML_NAMESPACE_DECL)
2996 return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
2997 if ((node->type != XML_ELEMENT_NODE) &&
2998 (node->type != XML_ATTRIBUTE_NODE))
2999 return(NULL);
3000 if (node->ns != NULL)
3001 return(CONSTSTR(node->ns->href));
3002 return(NULL);
3003}
3004
3005/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003006 * xmlTextReaderBaseUri:
3007 * @reader: the xmlTextReaderPtr used
3008 *
3009 * The base URI of the node.
3010 *
3011 * Returns the base URI or NULL if not available
3012 */
3013xmlChar *
3014xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
3015 if ((reader == NULL) || (reader->node == NULL))
3016 return(NULL);
3017 return(xmlNodeGetBase(NULL, reader->node));
3018}
3019
3020/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003021 * xmlTextReaderConstBaseUri:
3022 * @reader: the xmlTextReaderPtr used
3023 *
3024 * The base URI of the node.
3025 *
3026 * Returns the base URI or NULL if not available, the string
3027 * will be deallocated with the reader
3028 */
3029const xmlChar *
3030xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
3031 xmlChar *tmp;
3032 const xmlChar *ret;
3033
3034 if ((reader == NULL) || (reader->node == NULL))
3035 return(NULL);
3036 tmp = xmlNodeGetBase(NULL, reader->node);
3037 if (tmp == NULL)
3038 return(NULL);
3039 ret = CONSTSTR(tmp);
3040 xmlFree(tmp);
3041 return(ret);
3042}
3043
3044/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003045 * xmlTextReaderDepth:
3046 * @reader: the xmlTextReaderPtr used
3047 *
3048 * The depth of the node in the tree.
3049 *
3050 * Returns the depth or -1 in case of error
3051 */
3052int
3053xmlTextReaderDepth(xmlTextReaderPtr reader) {
3054 if (reader == NULL)
3055 return(-1);
3056 if (reader->node == NULL)
3057 return(0);
3058
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003059 if (reader->curnode != NULL) {
3060 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
3061 (reader->curnode->type == XML_NAMESPACE_DECL))
3062 return(reader->depth + 1);
3063 return(reader->depth + 2);
3064 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003065 return(reader->depth);
3066}
3067
3068/**
3069 * xmlTextReaderHasAttributes:
3070 * @reader: the xmlTextReaderPtr used
3071 *
3072 * Whether the node has attributes.
3073 *
3074 * Returns 1 if true, 0 if false, and -1 in case or error
3075 */
3076int
3077xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003078 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003079 if (reader == NULL)
3080 return(-1);
3081 if (reader->node == NULL)
3082 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003083 if (reader->curnode != NULL)
3084 node = reader->curnode;
3085 else
3086 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003087
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003088 if ((node->type == XML_ELEMENT_NODE) &&
3089 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003090 return(1);
3091 /* TODO: handle the xmlDecl */
3092 return(0);
3093}
3094
3095/**
3096 * xmlTextReaderHasValue:
3097 * @reader: the xmlTextReaderPtr used
3098 *
3099 * Whether the node can have a text value.
3100 *
3101 * Returns 1 if true, 0 if false, and -1 in case or error
3102 */
3103int
3104xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003105 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003106 if (reader == NULL)
3107 return(-1);
3108 if (reader->node == NULL)
3109 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003110 if (reader->curnode != NULL)
3111 node = reader->curnode;
3112 else
3113 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003114
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003115 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003116 case XML_ATTRIBUTE_NODE:
3117 case XML_TEXT_NODE:
3118 case XML_CDATA_SECTION_NODE:
3119 case XML_PI_NODE:
3120 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00003121 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003122 return(1);
3123 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003124 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003125 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003126 return(0);
3127}
3128
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003129/**
3130 * xmlTextReaderValue:
3131 * @reader: the xmlTextReaderPtr used
3132 *
3133 * Provides the text value of the node if present
3134 *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003135 * Returns the string or NULL if not available. The result must be deallocated
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003136 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003137 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003138xmlChar *
3139xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003140 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003141 if (reader == NULL)
3142 return(NULL);
3143 if (reader->node == NULL)
3144 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003145 if (reader->curnode != NULL)
3146 node = reader->curnode;
3147 else
3148 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003149
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003150 switch (node->type) {
3151 case XML_NAMESPACE_DECL:
3152 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003153 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003154 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003155
3156 if (attr->parent != NULL)
3157 return (xmlNodeListGetString
3158 (attr->parent->doc, attr->children, 1));
3159 else
3160 return (xmlNodeListGetString(NULL, attr->children, 1));
3161 break;
3162 }
3163 case XML_TEXT_NODE:
3164 case XML_CDATA_SECTION_NODE:
3165 case XML_PI_NODE:
3166 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003167 if (node->content != NULL)
3168 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003169 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003170 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003171 }
3172 return(NULL);
3173}
3174
3175/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003176 * xmlTextReaderConstValue:
3177 * @reader: the xmlTextReaderPtr used
3178 *
3179 * Provides the text value of the node if present
3180 *
3181 * Returns the string or NULL if not available. The result will be
3182 * deallocated on the next Read() operation.
3183 */
3184const xmlChar *
3185xmlTextReaderConstValue(xmlTextReaderPtr reader) {
3186 xmlNodePtr node;
3187 if (reader == NULL)
3188 return(NULL);
3189 if (reader->node == NULL)
3190 return(NULL);
3191 if (reader->curnode != NULL)
3192 node = reader->curnode;
3193 else
3194 node = reader->node;
3195
3196 switch (node->type) {
3197 case XML_NAMESPACE_DECL:
3198 return(((xmlNsPtr) node)->href);
3199 case XML_ATTRIBUTE_NODE:{
3200 xmlAttrPtr attr = (xmlAttrPtr) node;
3201
3202 if ((attr->children != NULL) &&
3203 (attr->children->type == XML_TEXT_NODE) &&
3204 (attr->children->next == NULL))
3205 return(attr->children->content);
3206 else {
3207 reader->buffer->use = 0;
3208 xmlNodeBufGetContent(reader->buffer, node);
3209 return(reader->buffer->content);
3210 }
3211 break;
3212 }
3213 case XML_TEXT_NODE:
3214 case XML_CDATA_SECTION_NODE:
3215 case XML_PI_NODE:
3216 case XML_COMMENT_NODE:
3217 return(node->content);
3218 default:
3219 break;
3220 }
3221 return(NULL);
3222}
3223
3224/**
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003225 * xmlTextReaderIsDefault:
3226 * @reader: the xmlTextReaderPtr used
3227 *
3228 * Whether an Attribute node was generated from the default value
3229 * defined in the DTD or schema.
3230 *
3231 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
3232 */
3233int
3234xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
3235 if (reader == NULL)
3236 return(-1);
3237 return(0);
3238}
3239
3240/**
3241 * xmlTextReaderQuoteChar:
3242 * @reader: the xmlTextReaderPtr used
3243 *
3244 * The quotation mark character used to enclose the value of an attribute.
3245 *
3246 * Returns " or ' and -1 in case of error
3247 */
3248int
3249xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
3250 if (reader == NULL)
3251 return(-1);
3252 /* TODO maybe lookup the attribute value for " first */
3253 return((int) '"');
3254}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003255
3256/**
3257 * xmlTextReaderXmlLang:
3258 * @reader: the xmlTextReaderPtr used
3259 *
3260 * The xml:lang scope within which the node resides.
3261 *
3262 * Returns the xml:lang value or NULL if none exists.
3263 */
3264xmlChar *
3265xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
3266 if (reader == NULL)
3267 return(NULL);
3268 if (reader->node == NULL)
3269 return(NULL);
3270 return(xmlNodeGetLang(reader->node));
3271}
3272
Daniel Veillard67df8092002-12-16 22:04:11 +00003273/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00003274 * xmlTextReaderConstXmlLang:
Daniel Veillarde72c5082003-09-19 12:44:05 +00003275 * @reader: the xmlTextReaderPtr used
3276 *
3277 * The xml:lang scope within which the node resides.
3278 *
3279 * Returns the xml:lang value or NULL if none exists.
3280 */
3281const xmlChar *
3282xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
3283 xmlChar *tmp;
3284 const xmlChar *ret;
3285
3286 if (reader == NULL)
3287 return(NULL);
3288 if (reader->node == NULL)
3289 return(NULL);
3290 tmp = xmlNodeGetLang(reader->node);
3291 if (tmp == NULL)
3292 return(NULL);
3293 ret = CONSTSTR(tmp);
3294 xmlFree(tmp);
3295 return(ret);
3296}
3297
3298/**
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00003299 * xmlTextReaderConstString:
3300 * @reader: the xmlTextReaderPtr used
3301 * @str: the string to intern.
3302 *
3303 * Get an interned string from the reader, allows for example to
3304 * speedup string name comparisons
3305 *
3306 * Returns an interned copy of the string or NULL in case of error. The
3307 * string will be deallocated with the reader.
3308 */
3309const xmlChar *
3310xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
3311 if (reader == NULL)
3312 return(NULL);
3313 return(CONSTSTR(str));
3314}
3315
3316/**
Daniel Veillard67df8092002-12-16 22:04:11 +00003317 * xmlTextReaderNormalization:
3318 * @reader: the xmlTextReaderPtr used
3319 *
3320 * The value indicating whether to normalize white space and attribute values.
3321 * Since attribute value and end of line normalizations are a MUST in the XML
3322 * specification only the value true is accepted. The broken bahaviour of
3323 * accepting out of range character entities like &#0; is of course not
3324 * supported either.
3325 *
3326 * Returns 1 or -1 in case of error.
3327 */
3328int
3329xmlTextReaderNormalization(xmlTextReaderPtr reader) {
3330 if (reader == NULL)
3331 return(-1);
3332 return(1);
3333}
3334
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003335/************************************************************************
3336 * *
3337 * Extensions to the base APIs *
3338 * *
3339 ************************************************************************/
3340
3341/**
3342 * xmlTextReaderSetParserProp:
3343 * @reader: the xmlTextReaderPtr used
3344 * @prop: the xmlParserProperties to set
3345 * @value: usually 0 or 1 to (de)activate it
3346 *
3347 * Change the parser processing behaviour by changing some of its internal
3348 * properties. Note that some properties can only be changed before any
3349 * read has been done.
3350 *
3351 * Returns 0 if the call was successful, or -1 in case of error
3352 */
3353int
3354xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
3355 xmlParserProperties p = (xmlParserProperties) prop;
3356 xmlParserCtxtPtr ctxt;
3357
3358 if ((reader == NULL) || (reader->ctxt == NULL))
3359 return(-1);
3360 ctxt = reader->ctxt;
3361
3362 switch (p) {
3363 case XML_PARSER_LOADDTD:
3364 if (value != 0) {
3365 if (ctxt->loadsubset == 0) {
3366 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3367 return(-1);
3368 ctxt->loadsubset = XML_DETECT_IDS;
3369 }
3370 } else {
3371 ctxt->loadsubset = 0;
3372 }
3373 return(0);
3374 case XML_PARSER_DEFAULTATTRS:
3375 if (value != 0) {
3376 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
3377 } else {
3378 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3379 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
3380 }
3381 return(0);
3382 case XML_PARSER_VALIDATE:
3383 if (value != 0) {
3384 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00003385 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003386 } else {
3387 ctxt->validate = 0;
3388 }
3389 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003390 case XML_PARSER_SUBST_ENTITIES:
3391 if (value != 0) {
3392 ctxt->replaceEntities = 1;
3393 } else {
3394 ctxt->replaceEntities = 0;
3395 }
3396 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003397 }
3398 return(-1);
3399}
3400
3401/**
3402 * xmlTextReaderGetParserProp:
3403 * @reader: the xmlTextReaderPtr used
3404 * @prop: the xmlParserProperties to get
3405 *
3406 * Read the parser internal property.
3407 *
3408 * Returns the value, usually 0 or 1, or -1 in case of error.
3409 */
3410int
3411xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
3412 xmlParserProperties p = (xmlParserProperties) prop;
3413 xmlParserCtxtPtr ctxt;
3414
3415 if ((reader == NULL) || (reader->ctxt == NULL))
3416 return(-1);
3417 ctxt = reader->ctxt;
3418
3419 switch (p) {
3420 case XML_PARSER_LOADDTD:
3421 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
3422 return(1);
3423 return(0);
3424 case XML_PARSER_DEFAULTATTRS:
3425 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3426 return(1);
3427 return(0);
3428 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00003429 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003430 case XML_PARSER_SUBST_ENTITIES:
3431 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003432 }
3433 return(-1);
3434}
3435
Daniel Veillarde18fc182002-12-28 22:56:33 +00003436/**
3437 * xmlTextReaderCurrentNode:
3438 * @reader: the xmlTextReaderPtr used
3439 *
3440 * Hacking interface allowing to get the xmlNodePtr correponding to the
3441 * current node being accessed by the xmlTextReader. This is dangerous
3442 * because the underlying node may be destroyed on the next Reads.
3443 *
3444 * Returns the xmlNodePtr or NULL in case of error.
3445 */
3446xmlNodePtr
3447xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
3448 if (reader == NULL)
3449 return(NULL);
3450
3451 if (reader->curnode != NULL)
3452 return(reader->curnode);
3453 return(reader->node);
3454}
3455
3456/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003457 * xmlTextReaderPreserve:
3458 * @reader: the xmlTextReaderPtr used
3459 *
3460 *
3461 * current node being accessed by the xmlTextReader. This is dangerous
3462 * because the underlying node may be destroyed on the next Reads.
3463 *
3464 * Returns the xmlNodePtr or NULL in case of error.
3465 */
3466xmlNodePtr
3467xmlTextReaderPreserve(xmlTextReaderPtr reader) {
3468 xmlNodePtr cur, parent;
3469
3470 if (reader == NULL)
3471 return(NULL);
3472
3473 if (reader->curnode != NULL)
3474 cur = reader->curnode;
3475 else
3476 cur = reader->node;
3477 if (cur == NULL)
3478 return(NULL);
Daniel Veillarde8039df2003-10-27 11:25:13 +00003479 cur->extra |= NODE_IS_PRESERVED;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003480
3481 parent = cur->parent;;
3482 while (parent != NULL) {
Daniel Veillarde8039df2003-10-27 11:25:13 +00003483 parent->extra |= NODE_IS_PRESERVED;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003484 parent = parent->parent;
3485 }
3486 return(cur);
3487}
3488
3489/**
Daniel Veillarde18fc182002-12-28 22:56:33 +00003490 * xmlTextReaderCurrentDoc:
3491 * @reader: the xmlTextReaderPtr used
3492 *
3493 * Hacking interface allowing to get the xmlDocPtr correponding to the
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003494 * current document being accessed by the xmlTextReader.
3495 * NOTE: as a result of this call, the reader will not destroy the
3496 * associated XML document and calling xmlFreeDoc() on the result
3497 * is needed once the reader parsing has finished.
Daniel Veillarde18fc182002-12-28 22:56:33 +00003498 *
3499 * Returns the xmlDocPtr or NULL in case of error.
3500 */
3501xmlDocPtr
3502xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003503 if (reader == NULL)
3504 return(NULL);
3505 if (reader->doc != NULL)
3506 return(reader->doc);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003507 if ((reader == NULL) || (reader->ctxt == NULL) ||
3508 (reader->ctxt->myDoc == NULL))
Daniel Veillarde18fc182002-12-28 22:56:33 +00003509 return(NULL);
3510
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003511 reader->preserve = 1;
3512 if ((reader->ctxt->myDoc->dict != NULL) &&
3513 (reader->ctxt->myDoc->dict == reader->ctxt->dict))
3514 xmlDictReference(reader->ctxt->dict);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003515 return(reader->ctxt->myDoc);
3516}
3517
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003518#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00003519/**
Daniel Veillard33300b42003-04-17 09:09:19 +00003520 * xmlTextReaderRelaxNGSetSchema:
3521 * @reader: the xmlTextReaderPtr used
3522 * @schema: a precompiled RelaxNG schema
3523 *
3524 * Use RelaxNG to validate the document as it is processed.
3525 * Activation is only possible before the first Read().
3526 * if @schema is NULL, then RelaxNG validation is desactivated.
3527 @ The @schema should not be freed until the reader is deallocated
3528 * or its use has been deactivated.
3529 *
3530 * Returns 0 in case the RelaxNG validation could be (des)activated and
3531 * -1 in case of error.
3532 */
3533int
3534xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
3535 if (schema == NULL) {
3536 if (reader->rngSchemas != NULL) {
3537 xmlRelaxNGFree(reader->rngSchemas);
3538 reader->rngSchemas = NULL;
3539 }
3540 if (reader->rngValidCtxt != NULL) {
3541 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3542 reader->rngValidCtxt = NULL;
3543 }
3544 return(0);
3545 }
3546 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3547 return(-1);
3548 if (reader->rngSchemas != NULL) {
3549 xmlRelaxNGFree(reader->rngSchemas);
3550 reader->rngSchemas = NULL;
3551 }
3552 if (reader->rngValidCtxt != NULL) {
3553 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3554 reader->rngValidCtxt = NULL;
3555 }
3556 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
3557 if (reader->rngValidCtxt == NULL)
3558 return(-1);
3559 if (reader->errorFunc != NULL) {
3560 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3561 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3562 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3563 reader->errorFuncArg);
3564 }
3565 reader->rngValidErrors = 0;
3566 reader->rngFullNode = NULL;
3567 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3568 return(0);
3569}
3570
3571/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00003572 * xmlTextReaderRelaxNGValidate:
3573 * @reader: the xmlTextReaderPtr used
3574 * @rng: the path to a RelaxNG schema or NULL
3575 *
3576 * Use RelaxNG to validate the document as it is processed.
3577 * Activation is only possible before the first Read().
3578 * if @rng is NULL, then RelaxNG validation is desactivated.
3579 *
3580 * Returns 0 in case the RelaxNG validation could be (des)activated and
3581 * -1 in case of error.
3582 */
3583int
3584xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
3585 xmlRelaxNGParserCtxtPtr ctxt;
3586
3587 if (reader == NULL)
3588 return(-1);
3589
3590 if (rng == NULL) {
3591 if (reader->rngSchemas != NULL) {
3592 xmlRelaxNGFree(reader->rngSchemas);
3593 reader->rngSchemas = NULL;
3594 }
3595 if (reader->rngValidCtxt != NULL) {
3596 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3597 reader->rngValidCtxt = NULL;
3598 }
3599 return(0);
3600 }
3601 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3602 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00003603 if (reader->rngSchemas != NULL) {
3604 xmlRelaxNGFree(reader->rngSchemas);
3605 reader->rngSchemas = NULL;
3606 }
3607 if (reader->rngValidCtxt != NULL) {
3608 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3609 reader->rngValidCtxt = NULL;
3610 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00003611 ctxt = xmlRelaxNGNewParserCtxt(rng);
3612 if (reader->errorFunc != NULL) {
3613 xmlRelaxNGSetParserErrors(ctxt,
3614 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
3615 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3616 reader->errorFuncArg);
3617 }
3618 reader->rngSchemas = xmlRelaxNGParse(ctxt);
3619 xmlRelaxNGFreeParserCtxt(ctxt);
3620 if (reader->rngSchemas == NULL)
3621 return(-1);
3622 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
3623 if (reader->rngValidCtxt == NULL)
3624 return(-1);
3625 if (reader->errorFunc != NULL) {
3626 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3627 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3628 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3629 reader->errorFuncArg);
3630 }
3631 reader->rngValidErrors = 0;
3632 reader->rngFullNode = NULL;
3633 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3634 return(0);
3635}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003636#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00003637
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003638/************************************************************************
3639 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00003640 * Error Handling Extensions *
3641 * *
3642 ************************************************************************/
3643
3644/* helper to build a xmlMalloc'ed string from a format and va_list */
3645static char *
3646xmlTextReaderBuildMessage(const char *msg, va_list ap) {
3647 int size;
3648 int chars;
3649 char *larger;
3650 char *str;
3651
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003652 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00003653 if (str == NULL) {
3654 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3655 return NULL;
3656 }
3657
3658 size = 150;
3659
3660 while (1) {
3661 chars = vsnprintf(str, size, msg, ap);
3662 if ((chars > -1) && (chars < size))
3663 break;
3664 if (chars > -1)
3665 size += chars + 1;
3666 else
3667 size += 100;
3668 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
3669 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3670 xmlFree(str);
3671 return NULL;
3672 }
3673 str = larger;
3674 }
3675
3676 return str;
3677}
3678
Daniel Veillard417be3a2003-01-20 21:26:34 +00003679/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003680 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003681 * @locator: the xmlTextReaderLocatorPtr used
3682 *
3683 * Obtain the line number for the given locator.
3684 *
3685 * Returns the line number or -1 in case of error.
3686 */
3687int
3688xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
3689 /* we know that locator is a xmlParserCtxtPtr */
3690 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3691 int ret = -1;
3692
3693 if (ctx->node != NULL) {
3694 ret = xmlGetLineNo(ctx->node);
3695 }
3696 else {
3697 /* inspired from error.c */
3698 xmlParserInputPtr input;
3699 input = ctx->input;
3700 if ((input->filename == NULL) && (ctx->inputNr > 1))
3701 input = ctx->inputTab[ctx->inputNr - 2];
3702 if (input != NULL) {
3703 ret = input->line;
3704 }
3705 else {
3706 ret = -1;
3707 }
3708 }
3709
3710 return ret;
3711}
3712
3713/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003714 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003715 * @locator: the xmlTextReaderLocatorPtr used
3716 *
3717 * Obtain the base URI for the given locator.
3718 *
3719 * Returns the base URI or NULL in case of error.
3720 */
3721xmlChar *
3722xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
3723 /* we know that locator is a xmlParserCtxtPtr */
3724 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3725 xmlChar *ret = NULL;
3726
3727 if (ctx->node != NULL) {
3728 ret = xmlNodeGetBase(NULL,ctx->node);
3729 }
3730 else {
3731 /* inspired from error.c */
3732 xmlParserInputPtr input;
3733 input = ctx->input;
3734 if ((input->filename == NULL) && (ctx->inputNr > 1))
3735 input = ctx->inputTab[ctx->inputNr - 2];
3736 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00003737 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00003738 }
3739 else {
3740 ret = NULL;
3741 }
3742 }
3743
3744 return ret;
3745}
3746
Daniel Veillard26f70262003-01-16 22:45:08 +00003747static void
William M. Brack899e64a2003-09-26 18:03:42 +00003748xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity, char *str) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003749 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
3750 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
3751
3752 if (str != NULL) {
3753 reader->errorFunc(reader->errorFuncArg,
3754 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003755 severity,
3756 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00003757 xmlFree(str);
3758 }
3759}
3760
3761static void
3762xmlTextReaderError(void *ctxt, const char *msg, ...) {
3763 va_list ap;
3764
3765 va_start(ap,msg);
3766 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003767 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00003768 xmlTextReaderBuildMessage(msg,ap));
3769 va_end(ap);
3770
3771}
3772
3773static void
3774xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
3775 va_list ap;
3776
3777 va_start(ap,msg);
3778 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003779 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00003780 xmlTextReaderBuildMessage(msg,ap));
3781 va_end(ap);
3782}
3783
3784static void
3785xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
3786 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003787 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003788
Daniel Veillard417be3a2003-01-20 21:26:34 +00003789 if ((len > 1) && (msg[len - 2] != ':')) {
3790 /*
3791 * some callbacks only report locator information:
3792 * skip them (mimicking behaviour in error.c)
3793 */
3794 va_start(ap,msg);
3795 xmlTextReaderGenericError(ctxt,
3796 XML_PARSER_SEVERITY_VALIDITY_ERROR,
3797 xmlTextReaderBuildMessage(msg,ap));
3798 va_end(ap);
3799 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003800}
3801
3802static void
3803xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
3804 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003805 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003806
Daniel Veillard417be3a2003-01-20 21:26:34 +00003807 if ((len != 0) && (msg[len - 1] != ':')) {
3808 /*
3809 * some callbacks only report locator information:
3810 * skip them (mimicking behaviour in error.c)
3811 */
3812 va_start(ap,msg);
3813 xmlTextReaderGenericError(ctxt,
3814 XML_PARSER_SEVERITY_VALIDITY_WARNING,
3815 xmlTextReaderBuildMessage(msg,ap));
3816 va_end(ap);
3817 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003818}
3819
3820/**
3821 * xmlTextReaderSetErrorHandler:
3822 * @reader: the xmlTextReaderPtr used
3823 * @f: the callback function to call on error and warnings
3824 * @arg: a user argument to pass to the callback function
3825 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00003826 * Register a callback function that will be called on error and warnings.
3827 *
Daniel Veillard26f70262003-01-16 22:45:08 +00003828 * If @f is NULL, the default error and warning handlers are restored.
3829 */
3830void
3831xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
3832 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003833 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003834 if (f != NULL) {
3835 reader->ctxt->sax->error = xmlTextReaderError;
3836 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
3837 reader->ctxt->sax->warning = xmlTextReaderWarning;
3838 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
3839 reader->errorFunc = f;
3840 reader->errorFuncArg = arg;
3841 }
3842 else {
3843 /* restore defaults */
3844 reader->ctxt->sax->error = xmlParserError;
3845 reader->ctxt->vctxt.error = xmlParserValidityError;
3846 reader->ctxt->sax->warning = xmlParserWarning;
3847 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
3848 reader->errorFunc = NULL;
3849 reader->errorFuncArg = NULL;
3850 }
3851}
3852
Daniel Veillard417be3a2003-01-20 21:26:34 +00003853/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00003854 * xmlTextReaderIsValid:
3855 * @reader: the xmlTextReaderPtr used
3856 *
3857 * Retrieve the validity status from the parser context
3858 *
3859 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
3860 */
3861int
3862xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00003863 if (reader == NULL) return(-1);
3864#ifdef LIBXML_SCHEMAS_ENABLED
3865 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
3866 return(reader->rngValidErrors == 0);
3867#endif
Daniel Veillard16ed5972003-11-20 18:22:31 +00003868 if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
Daniel Veillardf4e55762003-04-15 23:32:22 +00003869 return(reader->ctxt->valid);
3870 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00003871}
3872
3873/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00003874 * xmlTextReaderGetErrorHandler:
3875 * @reader: the xmlTextReaderPtr used
3876 * @f: the callback function or NULL is no callback has been registered
3877 * @arg: a user argument
3878 *
3879 * Retrieve the error callback function and user argument.
3880 */
Daniel Veillard26f70262003-01-16 22:45:08 +00003881void
3882xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
3883 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003884 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003885 *f = reader->errorFunc;
3886 *arg = reader->errorFuncArg;
3887}
3888
Daniel Veillardfc8dc352003-10-18 09:07:46 +00003889
3890/************************************************************************
3891 * *
3892 * New set (2.6.0) of simpler and more flexible APIs *
3893 * *
3894 ************************************************************************/
3895
3896/**
3897 * xmlTextReaderSetup:
3898 * @reader: an XML reader
3899 * @URL: the base URL to use for the document
3900 * @encoding: the document encoding, or NULL
3901 * @options: a combination of xmlParserOption(s)
3902 * @reuse: keep the context for reuse
3903 *
3904 * Setup an XML reader with new options
3905 *
3906 * Returns 0 in case of success and -1 in case of error.
3907 */
3908static int
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003909xmlTextReaderSetup(xmlTextReaderPtr reader,
3910 xmlParserInputBufferPtr input, const char *URL,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00003911 const char *encoding, int options)
3912{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003913 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00003914 return (-1);
3915
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003916 reader->doc = NULL;
3917 reader->entNr = 0;
Daniel Veillardc36965d2003-12-02 10:28:48 +00003918 reader->validate = XML_TEXTREADER_NOT_VALIDATE;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003919 if ((input != NULL) && (reader->input != NULL) &&
3920 (reader->allocs & XML_TEXTREADER_INPUT)) {
3921 xmlFreeParserInputBuffer(reader->input);
3922 reader->input = NULL;
3923 reader->allocs -= XML_TEXTREADER_INPUT;
3924 }
3925 if (input != NULL) {
3926 reader->input = input;
3927 reader->allocs |= XML_TEXTREADER_INPUT;
3928 }
3929 if (reader->buffer == NULL)
3930 reader->buffer = xmlBufferCreateSize(100);
3931 if (reader->buffer == NULL) {
3932 xmlGenericError(xmlGenericErrorContext,
3933 "xmlTextReaderSetup : malloc failed\n");
3934 return (-1);
3935 }
3936 if (reader->sax == NULL)
3937 reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
3938 if (reader->sax == NULL) {
3939 xmlGenericError(xmlGenericErrorContext,
3940 "xmlTextReaderSetup : malloc failed\n");
3941 return (-1);
3942 }
3943 xmlSAXVersion(reader->sax, 2);
3944 reader->startElement = reader->sax->startElement;
3945 reader->sax->startElement = xmlTextReaderStartElement;
3946 reader->endElement = reader->sax->endElement;
3947 reader->sax->endElement = xmlTextReaderEndElement;
3948#ifdef LIBXML_SAX1_ENABLED
3949 if (reader->sax->initialized == XML_SAX2_MAGIC) {
3950#endif /* LIBXML_SAX1_ENABLED */
3951 reader->startElementNs = reader->sax->startElementNs;
3952 reader->sax->startElementNs = xmlTextReaderStartElementNs;
3953 reader->endElementNs = reader->sax->endElementNs;
3954 reader->sax->endElementNs = xmlTextReaderEndElementNs;
3955#ifdef LIBXML_SAX1_ENABLED
3956 } else {
3957 reader->startElementNs = NULL;
3958 reader->endElementNs = NULL;
3959 }
3960#endif /* LIBXML_SAX1_ENABLED */
3961 reader->characters = reader->sax->characters;
3962 reader->sax->characters = xmlTextReaderCharacters;
3963 reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
3964 reader->cdataBlock = reader->sax->cdataBlock;
3965 reader->sax->cdataBlock = xmlTextReaderCDataBlock;
3966
3967 reader->mode = XML_TEXTREADER_MODE_INITIAL;
3968 reader->node = NULL;
3969 reader->curnode = NULL;
3970 if (input != NULL) {
3971 if (reader->input->buffer->use < 4) {
3972 xmlParserInputBufferRead(input, 4);
3973 }
3974 if (reader->ctxt == NULL) {
3975 if (reader->input->buffer->use >= 4) {
3976 reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
3977 (const char *) reader->input->buffer->content, 4, URL);
3978 reader->base = 0;
3979 reader->cur = 4;
3980 } else {
3981 reader->ctxt =
3982 xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
3983 reader->base = 0;
3984 reader->cur = 0;
3985 }
3986 } else {
3987 xmlParserInputPtr inputStream;
3988 xmlParserInputBufferPtr buf;
3989 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
3990
3991 if (reader->ctxt->myDoc != NULL)
3992 xmlDictReference(reader->ctxt->myDoc->dict);
3993 xmlCtxtReset(reader->ctxt);
3994 buf = xmlAllocParserInputBuffer(enc);
3995 if (buf == NULL) return(-1);
3996 inputStream = xmlNewInputStream(reader->ctxt);
3997 if (inputStream == NULL) {
3998 xmlFreeParserInputBuffer(buf);
3999 return(-1);
4000 }
4001
4002 if (URL == NULL)
4003 inputStream->filename = NULL;
4004 else
4005 inputStream->filename = (char *)
4006 xmlCanonicPath((const xmlChar *) URL);
4007 inputStream->buf = buf;
4008 inputStream->base = inputStream->buf->buffer->content;
4009 inputStream->cur = inputStream->buf->buffer->content;
4010 inputStream->end =
4011 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
4012
4013 inputPush(reader->ctxt, inputStream);
4014 reader->cur = 0;
4015 }
4016 if (reader->ctxt == NULL) {
4017 xmlGenericError(xmlGenericErrorContext,
4018 "xmlTextReaderSetup : malloc failed\n");
4019 return (-1);
4020 }
4021 }
4022 if (reader->dict != NULL) {
4023 if (reader->ctxt->dict != NULL) {
4024 if (reader->dict != reader->ctxt->dict) {
4025 xmlDictFree(reader->dict);
4026 reader->dict = reader->ctxt->dict;
4027 }
4028 } else {
4029 reader->ctxt->dict = reader->dict;
4030 }
4031 } else {
4032 if (reader->ctxt->dict == NULL)
4033 reader->ctxt->dict = xmlDictCreate();
4034 reader->dict = reader->ctxt->dict;
4035 }
4036 reader->ctxt->_private = reader;
4037 reader->ctxt->linenumbers = 1;
4038 reader->ctxt->dictNames = 1;
4039 /*
4040 * use the parser dictionnary to allocate all elements and attributes names
4041 */
4042 reader->ctxt->docdict = 1;
4043
Daniel Veillard7899c5c2003-11-03 12:31:38 +00004044#ifdef LIBXML_XINCLUDE_ENABLED
4045 if (reader->xincctxt != NULL) {
4046 xmlXIncludeFreeContext(reader->xincctxt);
4047 reader->xincctxt = NULL;
4048 }
4049 if (options & XML_PARSE_XINCLUDE) {
4050 reader->xinclude = 1;
4051 reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
4052 options -= XML_PARSE_XINCLUDE;
4053 } else
4054 reader->xinclude = 0;
4055 reader->in_xinclude = 0;
4056#endif
Daniel Veillardc36965d2003-12-02 10:28:48 +00004057 if (options & XML_PARSE_DTDVALID)
4058 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
4059
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004060 xmlCtxtUseOptions(reader->ctxt, options);
4061 if (encoding != NULL) {
4062 xmlCharEncodingHandlerPtr hdlr;
4063
4064 hdlr = xmlFindCharEncodingHandler(encoding);
4065 if (hdlr != NULL)
4066 xmlSwitchToEncoding(reader->ctxt, hdlr);
4067 }
4068 if ((URL != NULL) && (reader->ctxt->input != NULL) &&
4069 (reader->ctxt->input->filename == NULL))
4070 reader->ctxt->input->filename = (char *)
4071 xmlStrdup((const xmlChar *) URL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004072
4073 reader->doc = NULL;
4074
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004075 return (0);
4076}
4077
4078/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004079 * xmlReaderWalker:
4080 * @doc: a preparsed document
4081 *
4082 * Create an xmltextReader for a preparsed document.
4083 *
4084 * Returns the new reader or NULL in case of error.
4085 */
4086xmlTextReaderPtr
4087xmlReaderWalker(xmlDocPtr doc)
4088{
4089 xmlTextReaderPtr ret;
4090
4091 if (doc == NULL)
4092 return(NULL);
4093
4094 ret = xmlMalloc(sizeof(xmlTextReader));
4095 if (ret == NULL) {
4096 xmlGenericError(xmlGenericErrorContext,
4097 "xmlNewTextReader : malloc failed\n");
4098 return(NULL);
4099 }
4100 memset(ret, 0, sizeof(xmlTextReader));
4101 ret->entNr = 0;
4102 ret->input = NULL;
4103 ret->mode = XML_TEXTREADER_MODE_INITIAL;
4104 ret->node = NULL;
4105 ret->curnode = NULL;
4106 ret->base = 0;
4107 ret->cur = 0;
4108 ret->allocs = XML_TEXTREADER_CTXT;
4109 ret->doc = doc;
4110 ret->state = XML_TEXTREADER_START;
4111 ret->dict = xmlDictCreate();
4112 return(ret);
4113}
4114
4115/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004116 * xmlReaderForDoc:
4117 * @cur: a pointer to a zero terminated string
4118 * @URL: the base URL to use for the document
4119 * @encoding: the document encoding, or NULL
4120 * @options: a combination of xmlParserOption(s)
4121 *
4122 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004123 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004124 *
4125 * Returns the new reader or NULL in case of error.
4126 */
4127xmlTextReaderPtr
4128xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
4129 int options)
4130{
4131 int len;
4132
4133 if (cur == NULL)
4134 return (NULL);
4135 len = xmlStrlen(cur);
4136
4137 return (xmlReaderForMemory
4138 ((const char *) cur, len, URL, encoding, options));
4139}
4140
4141/**
4142 * xmlReaderForFile:
4143 * @filename: a file or URL
4144 * @encoding: the document encoding, or NULL
4145 * @options: a combination of xmlParserOption(s)
4146 *
4147 * parse an XML file from the filesystem or the network.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004148 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004149 *
4150 * Returns the new reader or NULL in case of error.
4151 */
4152xmlTextReaderPtr
4153xmlReaderForFile(const char *filename, const char *encoding, int options)
4154{
4155 xmlTextReaderPtr reader;
4156
4157 reader = xmlNewTextReaderFilename(filename);
4158 if (reader == NULL)
4159 return (NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004160 xmlTextReaderSetup(reader, NULL, NULL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004161 return (reader);
4162}
4163
4164/**
4165 * xmlReaderForMemory:
4166 * @buffer: a pointer to a char array
4167 * @size: the size of the array
4168 * @URL: the base URL to use for the document
4169 * @encoding: the document encoding, or NULL
4170 * @options: a combination of xmlParserOption(s)
4171 *
4172 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004173 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004174 *
4175 * Returns the new reader or NULL in case of error.
4176 */
4177xmlTextReaderPtr
4178xmlReaderForMemory(const char *buffer, int size, const char *URL,
4179 const char *encoding, int options)
4180{
4181 xmlTextReaderPtr reader;
4182 xmlParserInputBufferPtr buf;
4183
4184 buf =
4185 xmlParserInputBufferCreateMem(buffer, size,
4186 XML_CHAR_ENCODING_NONE);
4187 if (buf == NULL) {
4188 return (NULL);
4189 }
4190 reader = xmlNewTextReader(buf, URL);
4191 if (reader == NULL) {
4192 xmlFreeParserInputBuffer(buf);
4193 return (NULL);
4194 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004195 reader->allocs |= XML_TEXTREADER_INPUT;
4196 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004197 return (reader);
4198}
4199
4200/**
4201 * xmlReaderForFd:
4202 * @fd: an open file descriptor
4203 * @URL: the base URL to use for the document
4204 * @encoding: the document encoding, or NULL
4205 * @options: a combination of xmlParserOption(s)
4206 *
4207 * Create an xmltextReader for an XML from a file descriptor.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004208 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004209 *
4210 * Returns the new reader or NULL in case of error.
4211 */
4212xmlTextReaderPtr
4213xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
4214{
4215 xmlTextReaderPtr reader;
4216 xmlParserInputBufferPtr input;
4217
4218 if (fd < 0)
4219 return (NULL);
4220
4221 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4222 if (input == NULL)
4223 return (NULL);
4224 reader = xmlNewTextReader(input, URL);
4225 if (reader == NULL) {
4226 xmlFreeParserInputBuffer(input);
4227 return (NULL);
4228 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004229 reader->allocs |= XML_TEXTREADER_INPUT;
4230 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004231 return (reader);
4232}
4233
4234/**
4235 * xmlReaderForIO:
4236 * @ioread: an I/O read function
4237 * @ioclose: an I/O close function
4238 * @ioctx: an I/O handler
4239 * @URL: the base URL to use for the document
4240 * @encoding: the document encoding, or NULL
4241 * @options: a combination of xmlParserOption(s)
4242 *
4243 * Create an xmltextReader for an XML document from I/O functions and source.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004244 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004245 *
4246 * Returns the new reader or NULL in case of error.
4247 */
4248xmlTextReaderPtr
4249xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
4250 void *ioctx, const char *URL, const char *encoding,
4251 int options)
4252{
4253 xmlTextReaderPtr reader;
4254 xmlParserInputBufferPtr input;
4255
4256 if (ioread == NULL)
4257 return (NULL);
4258
4259 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4260 XML_CHAR_ENCODING_NONE);
4261 if (input == NULL)
4262 return (NULL);
4263 reader = xmlNewTextReader(input, URL);
4264 if (reader == NULL) {
4265 xmlFreeParserInputBuffer(input);
4266 return (NULL);
4267 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004268 reader->allocs |= XML_TEXTREADER_INPUT;
4269 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004270 return (reader);
4271}
4272
4273/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004274 * xmlReaderNewWalker:
4275 * @reader: an XML reader
4276 * @doc: a preparsed document
4277 *
4278 * Setup an xmltextReader to parse a preparsed XML document.
4279 * This reuses the existing @reader xmlTextReader.
4280 *
4281 * Returns 0 in case of success and -1 in case of error
4282 */
4283int
4284xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
4285{
4286 if (doc == NULL)
4287 return (-1);
4288 if (reader == NULL)
4289 return (-1);
4290
4291 if (reader->ctxt != NULL) {
4292 xmlCtxtReset(reader->ctxt);
4293 }
4294
4295 reader->entNr = 0;
4296 reader->input = NULL;
4297 reader->mode = XML_TEXTREADER_MODE_INITIAL;
4298 reader->node = NULL;
4299 reader->curnode = NULL;
4300 reader->base = 0;
4301 reader->cur = 0;
4302 reader->allocs = XML_TEXTREADER_CTXT;
4303 reader->doc = doc;
4304 reader->state = XML_TEXTREADER_START;
4305 if (reader->dict == NULL) {
4306 if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
4307 reader->dict = reader->ctxt->dict;
4308 else
4309 reader->dict = xmlDictCreate();
4310 }
4311 return(0);
4312}
4313
4314/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004315 * xmlReaderNewDoc:
4316 * @reader: an XML reader
4317 * @cur: a pointer to a zero terminated string
4318 * @URL: the base URL to use for the document
4319 * @encoding: the document encoding, or NULL
4320 * @options: a combination of xmlParserOption(s)
4321 *
4322 * Setup an xmltextReader to parse an XML in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004323 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004324 * This reuses the existing @reader xmlTextReader.
4325 *
4326 * Returns 0 in case of success and -1 in case of error
4327 */
4328int
4329xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
4330 const char *URL, const char *encoding, int options)
4331{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004332
4333 int len;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004334
4335 if (cur == NULL)
4336 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004337 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004338 return (-1);
4339
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004340 len = xmlStrlen(cur);
4341 return (xmlReaderNewMemory(reader, (const char *)cur, len,
4342 URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004343}
4344
4345/**
4346 * xmlReaderNewFile:
4347 * @reader: an XML reader
4348 * @filename: a file or URL
4349 * @encoding: the document encoding, or NULL
4350 * @options: a combination of xmlParserOption(s)
4351 *
4352 * parse an XML file from the filesystem or the network.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004353 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004354 * This reuses the existing @reader xmlTextReader.
4355 *
4356 * Returns 0 in case of success and -1 in case of error
4357 */
4358int
4359xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
4360 const char *encoding, int options)
4361{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004362 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004363
4364 if (filename == NULL)
4365 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004366 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004367 return (-1);
4368
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004369 input =
4370 xmlParserInputBufferCreateFilename(filename,
4371 XML_CHAR_ENCODING_NONE);
4372 if (input == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004373 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004374 return (xmlTextReaderSetup(reader, input, filename, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004375}
4376
4377/**
4378 * xmlReaderNewMemory:
4379 * @reader: an XML reader
4380 * @buffer: a pointer to a char array
4381 * @size: the size of the array
4382 * @URL: the base URL to use for the document
4383 * @encoding: the document encoding, or NULL
4384 * @options: a combination of xmlParserOption(s)
4385 *
4386 * Setup an xmltextReader to parse an XML in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004387 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004388 * This reuses the existing @reader xmlTextReader.
4389 *
4390 * Returns 0 in case of success and -1 in case of error
4391 */
4392int
4393xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
4394 const char *URL, const char *encoding, int options)
4395{
4396 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004397
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004398 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004399 return (-1);
4400 if (buffer == NULL)
4401 return (-1);
4402
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004403 input = xmlParserInputBufferCreateMem(buffer, size,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004404 XML_CHAR_ENCODING_NONE);
4405 if (input == NULL) {
4406 return (-1);
4407 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004408 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004409}
4410
4411/**
4412 * xmlReaderNewFd:
4413 * @reader: an XML reader
4414 * @fd: an open file descriptor
4415 * @URL: the base URL to use for the document
4416 * @encoding: the document encoding, or NULL
4417 * @options: a combination of xmlParserOption(s)
4418 *
4419 * Setup an xmltextReader to parse an XML from a file descriptor.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004420 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004421 * This reuses the existing @reader xmlTextReader.
4422 *
4423 * Returns 0 in case of success and -1 in case of error
4424 */
4425int
4426xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
4427 const char *URL, const char *encoding, int options)
4428{
4429 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004430
4431 if (fd < 0)
4432 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004433 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004434 return (-1);
4435
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004436 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4437 if (input == NULL)
4438 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004439 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004440}
4441
4442/**
4443 * xmlReaderNewIO:
4444 * @reader: an XML reader
4445 * @ioread: an I/O read function
4446 * @ioclose: an I/O close function
4447 * @ioctx: an I/O handler
4448 * @URL: the base URL to use for the document
4449 * @encoding: the document encoding, or NULL
4450 * @options: a combination of xmlParserOption(s)
4451 *
4452 * Setup an xmltextReader to parse an XML document from I/O functions
4453 * and source.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004454 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004455 * This reuses the existing @reader xmlTextReader.
4456 *
4457 * Returns 0 in case of success and -1 in case of error
4458 */
4459int
4460xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
4461 xmlInputCloseCallback ioclose, void *ioctx,
4462 const char *URL, const char *encoding, int options)
4463{
4464 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004465
4466 if (ioread == NULL)
4467 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004468 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004469 return (-1);
4470
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004471 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4472 XML_CHAR_ENCODING_NONE);
4473 if (input == NULL)
4474 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004475 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004476}
Daniel Veillard26f70262003-01-16 22:45:08 +00004477/************************************************************************
4478 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004479 * Utilities *
4480 * *
4481 ************************************************************************/
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004482#ifdef NOT_USED_YET
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004483/**
4484 * xmlBase64Decode:
4485 * @in: the input buffer
4486 * @inlen: the size of the input (in), the size read from it (out)
4487 * @to: the output buffer
4488 * @tolen: the size of the output (in), the size written to (out)
4489 *
4490 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00004491 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004492 *
4493 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
4494 * 2 if there wasn't enough space on the output or -1 in case of error.
4495 */
4496static int
4497xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
4498 unsigned char *to, unsigned long *tolen) {
4499 unsigned long incur; /* current index in in[] */
4500 unsigned long inblk; /* last block index in in[] */
4501 unsigned long outcur; /* current index in out[] */
4502 unsigned long inmax; /* size of in[] */
4503 unsigned long outmax; /* size of out[] */
4504 unsigned char cur; /* the current value read from in[] */
Daniel Veillardc127adc2003-07-23 15:07:08 +00004505 unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004506 int nbintmp; /* number of byte in intmp[] */
4507 int is_ignore; /* cur should be ignored */
4508 int is_end = 0; /* the end of the base64 was found */
4509 int retval = 1;
4510 int i;
4511
4512 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
4513 return(-1);
4514
4515 incur = 0;
4516 inblk = 0;
4517 outcur = 0;
4518 inmax = *inlen;
4519 outmax = *tolen;
4520 nbintmp = 0;
4521
4522 while (1) {
4523 if (incur >= inmax)
4524 break;
4525 cur = in[incur++];
4526 is_ignore = 0;
4527 if ((cur >= 'A') && (cur <= 'Z'))
4528 cur = cur - 'A';
4529 else if ((cur >= 'a') && (cur <= 'z'))
4530 cur = cur - 'a' + 26;
4531 else if ((cur >= '0') && (cur <= '9'))
4532 cur = cur - '0' + 52;
4533 else if (cur == '+')
4534 cur = 62;
4535 else if (cur == '/')
4536 cur = 63;
4537 else if (cur == '.')
4538 cur = 0;
4539 else if (cur == '=') /*no op , end of the base64 stream */
4540 is_end = 1;
4541 else {
4542 is_ignore = 1;
4543 if (nbintmp == 0)
4544 inblk = incur;
4545 }
4546
4547 if (!is_ignore) {
4548 int nbouttmp = 3;
4549 int is_break = 0;
4550
4551 if (is_end) {
4552 if (nbintmp == 0)
4553 break;
4554 if ((nbintmp == 1) || (nbintmp == 2))
4555 nbouttmp = 1;
4556 else
4557 nbouttmp = 2;
4558 nbintmp = 3;
4559 is_break = 1;
4560 }
4561 intmp[nbintmp++] = cur;
4562 /*
4563 * if intmp is full, push the 4byte sequence as a 3 byte
4564 * sequence out
4565 */
4566 if (nbintmp == 4) {
4567 nbintmp = 0;
4568 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
4569 outtmp[1] =
4570 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
4571 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
4572 if (outcur + 3 >= outmax) {
4573 retval = 2;
4574 break;
4575 }
4576
4577 for (i = 0; i < nbouttmp; i++)
4578 to[outcur++] = outtmp[i];
4579 inblk = incur;
4580 }
4581
4582 if (is_break) {
4583 retval = 0;
4584 break;
4585 }
4586 }
4587 }
4588
4589 *tolen = outcur;
4590 *inlen = inblk;
4591 return (retval);
4592}
4593
4594/*
4595 * Test routine for the xmlBase64Decode function
4596 */
4597#if 0
4598int main(int argc, char **argv) {
4599 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
4600 char output[100];
4601 char output2[100];
4602 char output3[100];
4603 unsigned long inlen = strlen(input);
4604 unsigned long outlen = 100;
4605 int ret;
4606 unsigned long cons, tmp, tmp2, prod;
4607
4608 /*
4609 * Direct
4610 */
4611 ret = xmlBase64Decode(input, &inlen, output, &outlen);
4612
4613 output[outlen] = 0;
4614 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
4615
4616 /*
4617 * output chunking
4618 */
4619 cons = 0;
4620 prod = 0;
4621 while (cons < inlen) {
4622 tmp = 5;
4623 tmp2 = inlen - cons;
4624
4625 printf("%ld %ld\n", cons, prod);
4626 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
4627 cons += tmp2;
4628 prod += tmp;
4629 printf("%ld %ld\n", cons, prod);
4630 }
4631 output2[outlen] = 0;
4632 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
4633
4634 /*
4635 * input chunking
4636 */
4637 cons = 0;
4638 prod = 0;
4639 while (cons < inlen) {
4640 tmp = 100 - prod;
4641 tmp2 = inlen - cons;
4642 if (tmp2 > 5)
4643 tmp2 = 5;
4644
4645 printf("%ld %ld\n", cons, prod);
4646 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
4647 cons += tmp2;
4648 prod += tmp;
4649 printf("%ld %ld\n", cons, prod);
4650 }
4651 output3[outlen] = 0;
4652 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
4653 return(0);
4654
4655}
4656#endif
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004657#endif /* NOT_USED_YET */
Daniel Veillard81273902003-09-30 00:43:48 +00004658#endif /* LIBXML_READER_ENABLED */