blob: 0ce19db73799e306101487396e05a36f27ae9566 [file] [log] [blame]
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001/*
2 * xmlreader.c: implements the xmlTextReader streaming node API
3 *
Daniel Veillard67df8092002-12-16 22:04:11 +00004 * NOTE:
5 * XmlTextReader.Normalization Property won't be supported, since
6 * it makes the parser non compliant to the XML recommendation
7 *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00008 * See Copyright for the status of this software.
9 *
10 * daniel@veillard.com
11 */
12
Daniel Veillard7704fb12003-01-03 16:19:51 +000013/*
14 * TODOs:
Daniel Veillard7704fb12003-01-03 16:19:51 +000015 * - provide an API to preserve part of the tree
16 * - Streaming XInclude support
Daniel Veillard067bae52003-01-05 01:27:54 +000017 * - XML Schemas validation
Daniel Veillard7704fb12003-01-03 16:19:51 +000018 * - setting(s) for NoBlanks
19 * - performances and tuning ...
20 */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000021#define IN_LIBXML
22#include "libxml.h"
23
Daniel Veillard81273902003-09-30 00:43:48 +000024#ifdef LIBXML_READER_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +000025#include <string.h> /* for memset() only ! */
Daniel Veillard26f70262003-01-16 22:45:08 +000026#include <stdarg.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000027
28#ifdef HAVE_CTYPE_H
29#include <ctype.h>
30#endif
31#ifdef HAVE_STDLIB_H
32#include <stdlib.h>
33#endif
34
35#include <libxml/xmlmemory.h>
36#include <libxml/xmlIO.h>
37#include <libxml/xmlreader.h>
Daniel Veillardfc8dc352003-10-18 09:07:46 +000038#include <libxml/parserInternals.h>
Daniel Veillardf4e55762003-04-15 23:32:22 +000039#include <libxml/relaxng.h>
Daniel Veillard198c1bf2003-10-20 17:07:41 +000040#include <libxml/uri.h>
Daniel Veillard7899c5c2003-11-03 12:31:38 +000041#ifdef LIBXML_XINCLUDE_ENABLED
42#include <libxml/xinclude.h>
43#endif
Daniel Veillard1e906612003-12-05 14:57:46 +000044#ifdef LIBXML_PATTERN_ENABLED
45#include <libxml/pattern.h>
46#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +000047
48/* #define DEBUG_CALLBACKS */
49/* #define DEBUG_READER */
50
51/**
52 * TODO:
53 *
54 * macro to flag unimplemented blocks
55 */
56#define TODO \
57 xmlGenericError(xmlGenericErrorContext, \
58 "Unimplemented block at %s:%d\n", \
59 __FILE__, __LINE__);
60
61#ifdef DEBUG_READER
62#define DUMP_READER xmlTextReaderDebug(reader);
63#else
64#define DUMP_READER
65#endif
66
Daniel Veillarda880b122003-04-21 21:36:41 +000067#define CHUNK_SIZE 512
Daniel Veillarde1ca5032002-12-09 14:13:43 +000068/************************************************************************
69 * *
70 * The parser: maps the Text Reader API on top of the existing *
71 * parsing routines building a tree *
72 * *
73 ************************************************************************/
74
75#define XML_TEXTREADER_INPUT 1
76#define XML_TEXTREADER_CTXT 2
77
78typedef enum {
Daniel Veillard67df8092002-12-16 22:04:11 +000079 XML_TEXTREADER_MODE_INITIAL = 0,
80 XML_TEXTREADER_MODE_INTERACTIVE = 1,
81 XML_TEXTREADER_MODE_ERROR = 2,
82 XML_TEXTREADER_MODE_EOF =3,
83 XML_TEXTREADER_MODE_CLOSED = 4,
84 XML_TEXTREADER_MODE_READING = 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000085} xmlTextReaderMode;
86
87typedef enum {
88 XML_TEXTREADER_NONE = -1,
89 XML_TEXTREADER_START= 0,
90 XML_TEXTREADER_ELEMENT= 1,
91 XML_TEXTREADER_END= 2,
92 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000093 XML_TEXTREADER_BACKTRACK= 4,
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000094 XML_TEXTREADER_DONE= 5,
95 XML_TEXTREADER_ERROR= 6
Daniel Veillarde1ca5032002-12-09 14:13:43 +000096} xmlTextReaderState;
97
Daniel Veillardf4e55762003-04-15 23:32:22 +000098typedef enum {
99 XML_TEXTREADER_NOT_VALIDATE = 0,
100 XML_TEXTREADER_VALIDATE_DTD = 1,
101 XML_TEXTREADER_VALIDATE_RNG = 2
102} xmlTextReaderValidate;
103
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000104struct _xmlTextReader {
105 int mode; /* the parsing mode */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000106 xmlDocPtr doc; /* when walking an existing doc */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000107 xmlTextReaderValidate validate;/* is there any validation */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000108 int allocs; /* what structure were deallocated */
109 xmlTextReaderState state;
110 xmlParserCtxtPtr ctxt; /* the parser context */
111 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
112 xmlParserInputBufferPtr input; /* the input */
113 startElementSAXFunc startElement;/* initial SAX callbacks */
114 endElementSAXFunc endElement; /* idem */
Daniel Veillard07cb8222003-09-10 10:51:05 +0000115 startElementNsSAX2Func startElementNs;/* idem */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000116 endElementNsSAX2Func endElementNs; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000117 charactersSAXFunc characters;
118 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000119 unsigned int base; /* base of the segment in the input */
120 unsigned int cur; /* current position in the input */
121 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000122 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000123 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000124 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000125 int preserve;/* preserve the resulting document */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000126 xmlBufferPtr buffer; /* used to return const xmlChar * */
127 xmlDictPtr dict; /* the context dictionnary */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000128
129 /* entity stack when traversing entities content */
130 xmlNodePtr ent; /* Current Entity Ref Node */
131 int entNr; /* Depth of the entities stack */
132 int entMax; /* Max depth of the entities stack */
133 xmlNodePtr *entTab; /* array of entities */
Daniel Veillard26f70262003-01-16 22:45:08 +0000134
135 /* error handling */
136 xmlTextReaderErrorFunc errorFunc; /* callback function */
137 void *errorFuncArg; /* callback function user argument */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000138
139#ifdef LIBXML_SCHEMAS_ENABLED
140 /* Handling of RelaxNG validation */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000141 xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
142 xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
143 int rngValidErrors;/* The number of errors detected */
144 xmlNodePtr rngFullNode; /* the node if RNG not progressive */
145#endif
146#ifdef LIBXML_XINCLUDE_ENABLED
147 /* Handling of XInclude processing */
148 int xinclude; /* is xinclude asked for */
149 const xmlChar * xinclude_name; /* the xinclude name from dict */
150 xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */
151 int in_xinclude; /* counts for xinclude */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000152#endif
Daniel Veillard1e906612003-12-05 14:57:46 +0000153#ifdef LIBXML_PATTERN_ENABLED
154 int patternNr; /* number of preserve patterns */
155 int patternMax; /* max preserve patterns */
156 xmlPatternPtr *patternTab; /* array of preserve patterns */
157#endif
158 int preserves; /* level of preserves */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000159};
160
Daniel Veillarde8039df2003-10-27 11:25:13 +0000161#define NODE_IS_EMPTY 0x1
162#define NODE_IS_PRESERVED 0x2
Daniel Veillard1e906612003-12-05 14:57:46 +0000163#define NODE_IS_SPRESERVED 0x4
Daniel Veillard067bae52003-01-05 01:27:54 +0000164
Daniel Veillarde72c5082003-09-19 12:44:05 +0000165/**
166 * CONSTSTR:
167 *
168 * Macro used to return an interned string
169 */
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000170#define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
171#define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
172
173static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
174static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
Daniel Veillarde72c5082003-09-19 12:44:05 +0000175
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000176/************************************************************************
177 * *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000178 * Our own version of the freeing routines as we recycle nodes *
179 * *
180 ************************************************************************/
181/**
182 * DICT_FREE:
183 * @str: a string
184 *
185 * Free a string if it is not owned by the "dict" dictionnary in the
186 * current scope
187 */
188#define DICT_FREE(str) \
189 if ((str) && ((!dict) || \
190 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
191 xmlFree((char *)(str));
192
193static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
194static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
195
196/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000197 * xmlFreeID:
198 * @not: A id
199 *
200 * Deallocate the memory used by an id definition
201 */
202static void
203xmlFreeID(xmlIDPtr id) {
204 xmlDictPtr dict = NULL;
205
206 if (id == NULL) return;
207
208 if (id->doc != NULL)
209 dict = id->doc->dict;
210
211 if (id->value != NULL)
212 DICT_FREE(id->value)
213 xmlFree(id);
214}
215
216/**
217 * xmlTextReaderRemoveID:
218 * @doc: the document
219 * @attr: the attribute
220 *
221 * Remove the given attribute from the ID table maintained internally.
222 *
223 * Returns -1 if the lookup failed and 0 otherwise
224 */
225static int
226xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
227 xmlIDTablePtr table;
228 xmlIDPtr id;
229 xmlChar *ID;
230
231 if (doc == NULL) return(-1);
232 if (attr == NULL) return(-1);
233 table = (xmlIDTablePtr) doc->ids;
234 if (table == NULL)
235 return(-1);
236
237 if (attr == NULL)
238 return(-1);
239 ID = xmlNodeListGetString(doc, attr->children, 1);
240 if (ID == NULL)
241 return(-1);
242 id = xmlHashLookup(table, ID);
243 xmlFree(ID);
244 if (id == NULL || id->attr != attr) {
245 return(-1);
246 }
247 id->name = attr->name;
248 id->attr = NULL;
249 return(0);
250}
251
252/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000253 * xmlTextReaderFreeProp:
254 * @reader: the xmlTextReaderPtr used
255 * @cur: the node
256 *
257 * Free a node.
258 */
259static void
260xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
261 xmlDictPtr dict;
262
263 dict = reader->ctxt->dict;
264 if (cur == NULL) return;
265
266 /* Check for ID removal -> leading to invalid references ! */
267 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
268 ((cur->parent->doc->intSubset != NULL) ||
269 (cur->parent->doc->extSubset != NULL))) {
270 if (xmlIsID(cur->parent->doc, cur->parent, cur))
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000271 xmlTextReaderRemoveID(cur->parent->doc, cur);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000272 }
273 if (cur->children != NULL)
274 xmlTextReaderFreeNodeList(reader, cur->children);
275
276 DICT_FREE(cur->name);
277 if ((reader != NULL) && (reader->ctxt != NULL) &&
278 (reader->ctxt->freeAttrsNr < 100)) {
279 cur->next = reader->ctxt->freeAttrs;
280 reader->ctxt->freeAttrs = cur;
281 reader->ctxt->freeAttrsNr++;
282 } else {
283 xmlFree(cur);
284 }
285}
286
287/**
288 * xmlTextReaderFreePropList:
289 * @reader: the xmlTextReaderPtr used
290 * @cur: the first property in the list
291 *
292 * Free a property and all its siblings, all the children are freed too.
293 */
294static void
295xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
296 xmlAttrPtr next;
297 if (cur == NULL) return;
298 while (cur != NULL) {
299 next = cur->next;
300 xmlTextReaderFreeProp(reader, cur);
301 cur = next;
302 }
303}
304
305/**
306 * xmlTextReaderFreeNodeList:
307 * @reader: the xmlTextReaderPtr used
308 * @cur: the first node in the list
309 *
310 * Free a node and all its siblings, this is a recursive behaviour, all
311 * the children are freed too.
312 */
313static void
314xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
315 xmlNodePtr next;
316 xmlDictPtr dict;
317
318 dict = reader->ctxt->dict;
319 if (cur == NULL) return;
320 if (cur->type == XML_NAMESPACE_DECL) {
321 xmlFreeNsList((xmlNsPtr) cur);
322 return;
323 }
324 if ((cur->type == XML_DOCUMENT_NODE) ||
325 (cur->type == XML_HTML_DOCUMENT_NODE)) {
326 xmlFreeDoc((xmlDocPtr) cur);
327 return;
328 }
329 while (cur != NULL) {
330 next = cur->next;
331 /* unroll to speed up freeing the document */
332 if (cur->type != XML_DTD_NODE) {
333
334 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000335 (cur->type != XML_ENTITY_REF_NODE)) {
336 if (cur->children->parent == cur)
337 xmlTextReaderFreeNodeList(reader, cur->children);
338 cur->children = NULL;
339 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000340 if (((cur->type == XML_ELEMENT_NODE) ||
341 (cur->type == XML_XINCLUDE_START) ||
342 (cur->type == XML_XINCLUDE_END)) &&
343 (cur->properties != NULL))
344 xmlTextReaderFreePropList(reader, cur->properties);
345 if ((cur->type != XML_ELEMENT_NODE) &&
346 (cur->type != XML_XINCLUDE_START) &&
347 (cur->type != XML_XINCLUDE_END) &&
348 (cur->type != XML_ENTITY_REF_NODE)) {
349 DICT_FREE(cur->content);
350 }
351 if (((cur->type == XML_ELEMENT_NODE) ||
352 (cur->type == XML_XINCLUDE_START) ||
353 (cur->type == XML_XINCLUDE_END)) &&
354 (cur->nsDef != NULL))
355 xmlFreeNsList(cur->nsDef);
356
357 /*
358 * we don't free element names here they are interned now
359 */
360 if ((cur->type != XML_TEXT_NODE) &&
361 (cur->type != XML_COMMENT_NODE))
362 DICT_FREE(cur->name);
363 if (((cur->type == XML_ELEMENT_NODE) ||
364 (cur->type == XML_TEXT_NODE)) &&
365 (reader != NULL) && (reader->ctxt != NULL) &&
366 (reader->ctxt->freeElemsNr < 100)) {
367 cur->next = reader->ctxt->freeElems;
368 reader->ctxt->freeElems = cur;
369 reader->ctxt->freeElemsNr++;
370 } else {
371 xmlFree(cur);
372 }
373 }
374 cur = next;
375 }
376}
377
378/**
379 * xmlTextReaderFreeNode:
380 * @reader: the xmlTextReaderPtr used
381 * @cur: the node
382 *
383 * Free a node, this is a recursive behaviour, all the children are freed too.
384 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
385 */
386static void
387xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
388 xmlDictPtr dict;
389
390 dict = reader->ctxt->dict;
391 if (cur->type == XML_DTD_NODE) {
392 xmlFreeDtd((xmlDtdPtr) cur);
393 return;
394 }
395 if (cur->type == XML_NAMESPACE_DECL) {
396 xmlFreeNs((xmlNsPtr) cur);
397 return;
398 }
399 if (cur->type == XML_ATTRIBUTE_NODE) {
400 xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
401 return;
402 }
403
404 if ((cur->children != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +0000405 (cur->type != XML_ENTITY_REF_NODE)) {
406 if (cur->children->parent == cur)
407 xmlTextReaderFreeNodeList(reader, cur->children);
408 cur->children = NULL;
409 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000410 if (((cur->type == XML_ELEMENT_NODE) ||
411 (cur->type == XML_XINCLUDE_START) ||
412 (cur->type == XML_XINCLUDE_END)) &&
413 (cur->properties != NULL))
414 xmlTextReaderFreePropList(reader, cur->properties);
415 if ((cur->type != XML_ELEMENT_NODE) &&
416 (cur->type != XML_XINCLUDE_START) &&
417 (cur->type != XML_XINCLUDE_END) &&
418 (cur->type != XML_ENTITY_REF_NODE)) {
419 DICT_FREE(cur->content);
420 }
421 if (((cur->type == XML_ELEMENT_NODE) ||
422 (cur->type == XML_XINCLUDE_START) ||
423 (cur->type == XML_XINCLUDE_END)) &&
424 (cur->nsDef != NULL))
425 xmlFreeNsList(cur->nsDef);
426
427 /*
428 * we don't free names here they are interned now
429 */
430 if ((cur->type != XML_TEXT_NODE) &&
431 (cur->type != XML_COMMENT_NODE))
432 DICT_FREE(cur->name);
433 if (((cur->type == XML_ELEMENT_NODE) ||
434 (cur->type == XML_TEXT_NODE)) &&
435 (reader != NULL) && (reader->ctxt != NULL) &&
436 (reader->ctxt->freeElemsNr < 100)) {
437 cur->next = reader->ctxt->freeElems;
438 reader->ctxt->freeElems = cur;
439 reader->ctxt->freeElemsNr++;
440 } else {
441 xmlFree(cur);
442 }
443}
444
445/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000446 * xmlTextReaderFreeIDTable:
447 * @table: An id table
448 *
449 * Deallocate the memory used by an ID hash table.
450 */
William M. Brack60f394e2003-11-16 06:25:42 +0000451static void
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000452xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
453 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
454}
455
456/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000457 * xmlTextReaderFreeDoc:
458 * @reader: the xmlTextReaderPtr used
459 * @cur: pointer to the document
460 *
461 * Free up all the structures used by a document, tree included.
462 */
463static void
464xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
465 xmlDtdPtr extSubset, intSubset;
466
467 if (cur == NULL) return;
468
469 /*
470 * Do this before freeing the children list to avoid ID lookups
471 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +0000472 if (cur->ids != NULL) xmlTextReaderFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000473 cur->ids = NULL;
474 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
475 cur->refs = NULL;
476 extSubset = cur->extSubset;
477 intSubset = cur->intSubset;
478 if (intSubset == extSubset)
479 extSubset = NULL;
480 if (extSubset != NULL) {
481 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
482 cur->extSubset = NULL;
483 xmlFreeDtd(extSubset);
484 }
485 if (intSubset != NULL) {
486 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
487 cur->intSubset = NULL;
488 xmlFreeDtd(intSubset);
489 }
490
491 if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
492
493 if (cur->version != NULL) xmlFree((char *) cur->version);
494 if (cur->name != NULL) xmlFree((char *) cur->name);
495 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
496 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
497 if (cur->URL != NULL) xmlFree((char *) cur->URL);
498 xmlFree(cur);
499}
500
501/************************************************************************
502 * *
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000503 * The reader core parser *
504 * *
505 ************************************************************************/
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000506#ifdef DEBUG_READER
507static void
508xmlTextReaderDebug(xmlTextReaderPtr reader) {
509 if ((reader == NULL) || (reader->ctxt == NULL)) {
510 fprintf(stderr, "xmlTextReader NULL\n");
511 return;
512 }
513 fprintf(stderr, "xmlTextReader: state %d depth %d ",
514 reader->state, reader->depth);
515 if (reader->node == NULL) {
516 fprintf(stderr, "node = NULL\n");
517 } else {
518 fprintf(stderr, "node %s\n", reader->node->name);
519 }
520 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
521 reader->base, reader->cur, reader->ctxt->nodeNr);
522 if (reader->input->buffer == NULL) {
523 fprintf(stderr, "buffer is NULL\n");
524 } else {
525#ifdef LIBXML_DEBUG_ENABLED
526 xmlDebugDumpString(stderr,
527 &reader->input->buffer->content[reader->cur]);
528#endif
529 fprintf(stderr, "\n");
530 }
531}
532#endif
533
534/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000535 * xmlTextReaderEntPush:
536 * @reader: the xmlTextReaderPtr used
537 * @value: the entity reference node
538 *
539 * Pushes a new entity reference node on top of the entities stack
540 *
541 * Returns 0 in case of error, the index in the stack otherwise
542 */
543static int
544xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
545{
546 if (reader->entMax <= 0) {
547 reader->entMax = 10;
548 reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
549 sizeof(reader->entTab[0]));
550 if (reader->entTab == NULL) {
551 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
552 return (0);
553 }
554 }
555 if (reader->entNr >= reader->entMax) {
556 reader->entMax *= 2;
557 reader->entTab =
558 (xmlNodePtr *) xmlRealloc(reader->entTab,
559 reader->entMax *
560 sizeof(reader->entTab[0]));
561 if (reader->entTab == NULL) {
562 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
563 return (0);
564 }
565 }
566 reader->entTab[reader->entNr] = value;
567 reader->ent = value;
568 return (reader->entNr++);
569}
570
571/**
572 * xmlTextReaderEntPop:
573 * @reader: the xmlTextReaderPtr used
574 *
575 * Pops the top element entity from the entities stack
576 *
577 * Returns the entity just removed
578 */
579static xmlNodePtr
580xmlTextReaderEntPop(xmlTextReaderPtr reader)
581{
582 xmlNodePtr ret;
583
584 if (reader->entNr <= 0)
585 return (0);
586 reader->entNr--;
587 if (reader->entNr > 0)
588 reader->ent = reader->entTab[reader->entNr - 1];
589 else
590 reader->ent = NULL;
591 ret = reader->entTab[reader->entNr];
592 reader->entTab[reader->entNr] = 0;
593 return (ret);
594}
595
596/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000597 * xmlTextReaderStartElement:
598 * @ctx: the user data (XML parser context)
599 * @fullname: The element name, including namespace prefix
600 * @atts: An array of name/value attributes pairs, NULL terminated
601 *
602 * called when an opening tag has been processed.
603 */
604static void
605xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
606 const xmlChar **atts) {
607 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
608 xmlTextReaderPtr reader = ctxt->_private;
609
610#ifdef DEBUG_CALLBACKS
611 printf("xmlTextReaderStartElement(%s)\n", fullname);
612#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000613 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000614 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000615 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
616 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
617 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000618 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000619 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000620 if (reader != NULL)
621 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000622}
623
624/**
625 * xmlTextReaderEndElement:
626 * @ctx: the user data (XML parser context)
627 * @fullname: The element name, including namespace prefix
628 *
629 * called when an ending tag has been processed.
630 */
631static void
632xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
633 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
634 xmlTextReaderPtr reader = ctxt->_private;
635
636#ifdef DEBUG_CALLBACKS
637 printf("xmlTextReaderEndElement(%s)\n", fullname);
638#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000639 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000640 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000641 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000642}
643
644/**
Daniel Veillard07cb8222003-09-10 10:51:05 +0000645 * xmlTextReaderStartElementNs:
646 * @ctx: the user data (XML parser context)
647 * @localname: the local name of the element
648 * @prefix: the element namespace prefix if available
649 * @URI: the element namespace name if available
650 * @nb_namespaces: number of namespace definitions on that node
651 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
652 * @nb_attributes: the number of attributes on that node
653 * nb_defaulted: the number of defaulted attributes.
654 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
655 * attribute values.
656 *
657 * called when an opening tag has been processed.
658 */
659static void
660xmlTextReaderStartElementNs(void *ctx,
661 const xmlChar *localname,
662 const xmlChar *prefix,
663 const xmlChar *URI,
664 int nb_namespaces,
665 const xmlChar **namespaces,
666 int nb_attributes,
667 int nb_defaulted,
668 const xmlChar **attributes)
669{
670 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
671 xmlTextReaderPtr reader = ctxt->_private;
672
673#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000674 printf("xmlTextReaderStartElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000675#endif
676 if ((reader != NULL) && (reader->startElementNs != NULL)) {
677 reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
678 namespaces, nb_attributes, nb_defaulted,
679 attributes);
680 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
681 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
682 (ctxt->input->cur[1] == '>'))
Daniel Veillarde8039df2003-10-27 11:25:13 +0000683 ctxt->node->extra = NODE_IS_EMPTY;
Daniel Veillard07cb8222003-09-10 10:51:05 +0000684 }
685 if (reader != NULL)
686 reader->state = XML_TEXTREADER_ELEMENT;
687}
688
689/**
690 * xmlTextReaderEndElementNs:
691 * @ctx: the user data (XML parser context)
692 * @localname: the local name of the element
693 * @prefix: the element namespace prefix if available
694 * @URI: the element namespace name if available
695 *
696 * called when an ending tag has been processed.
697 */
698static void
699xmlTextReaderEndElementNs(void *ctx,
700 const xmlChar * localname,
701 const xmlChar * prefix,
702 const xmlChar * URI)
703{
704 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
705 xmlTextReaderPtr reader = ctxt->_private;
706
707#ifdef DEBUG_CALLBACKS
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000708 printf("xmlTextReaderEndElementNs(%s)\n", localname);
Daniel Veillard07cb8222003-09-10 10:51:05 +0000709#endif
710 if ((reader != NULL) && (reader->endElementNs != NULL)) {
711 reader->endElementNs(ctx, localname, prefix, URI);
712 }
713}
714
715
716/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000717 * xmlTextReaderCharacters:
718 * @ctx: the user data (XML parser context)
719 * @ch: a xmlChar string
720 * @len: the number of xmlChar
721 *
722 * receiving some chars from the parser.
723 */
724static void
725xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
726{
727 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
728 xmlTextReaderPtr reader = ctxt->_private;
729
730#ifdef DEBUG_CALLBACKS
731 printf("xmlTextReaderCharacters()\n");
732#endif
733 if ((reader != NULL) && (reader->characters != NULL)) {
734 reader->characters(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000735 }
736}
737
738/**
739 * xmlTextReaderCDataBlock:
740 * @ctx: the user data (XML parser context)
741 * @value: The pcdata content
742 * @len: the block length
743 *
744 * called when a pcdata block has been parsed
745 */
746static void
747xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
748{
749 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
750 xmlTextReaderPtr reader = ctxt->_private;
751
752#ifdef DEBUG_CALLBACKS
753 printf("xmlTextReaderCDataBlock()\n");
754#endif
755 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
756 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000757 }
758}
759
760/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000761 * xmlTextReaderPushData:
762 * @reader: the xmlTextReaderPtr used
763 *
764 * Push data down the progressive parser until a significant callback
765 * got raised.
766 *
767 * Returns -1 in case of failure, 0 otherwise
768 */
769static int
770xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000771 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000772 int val, s;
William M. Brack779af002003-08-01 15:55:39 +0000773 xmlTextReaderState oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000774
775 if ((reader->input == NULL) || (reader->input->buffer == NULL))
776 return(-1);
777
Daniel Veillardea7751d2002-12-20 00:16:24 +0000778 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000779 reader->state = XML_TEXTREADER_NONE;
780 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000781
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000782 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000783 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000784 /*
785 * Refill the buffer unless we are at the end of the stream
786 */
787 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
788 val = xmlParserInputBufferRead(reader->input, 4096);
Daniel Veillard53350552003-09-18 13:35:51 +0000789 if ((val == 0) &&
790 (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
791 if (inbuf->use == reader->cur) {
792 reader->mode = XML_TEXTREADER_MODE_EOF;
793 reader->state = oldstate;
794 if ((oldstate != XML_TEXTREADER_START) ||
795 (reader->ctxt->myDoc != NULL))
796 return(val);
797 }
798 } else if (val < 0) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000799 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000800 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000801 if ((oldstate != XML_TEXTREADER_START) ||
802 (reader->ctxt->myDoc != NULL))
803 return(val);
Daniel Veillard53350552003-09-18 13:35:51 +0000804 } else if (val == 0) {
805 /* mark the end of the stream and process the remains */
806 reader->mode = XML_TEXTREADER_MODE_EOF;
807 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000808 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000809
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000810 } else
811 break;
812 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000813 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000814 * parse by block of CHUNK_SIZE bytes, various tests show that
815 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000816 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000817 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000818 val = xmlParseChunk(reader->ctxt,
819 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000820 CHUNK_SIZE, 0);
821 reader->cur += CHUNK_SIZE;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000822 if ((val != 0) && (reader->ctxt->wellFormed == 0))
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000823 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000824 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000825 s = inbuf->use - reader->cur;
826 val = xmlParseChunk(reader->ctxt,
827 (const char *) &inbuf->content[reader->cur],
828 s, 0);
829 reader->cur += s;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000830 if ((val != 0) && (reader->ctxt->wellFormed == 0))
Daniel Veillarda880b122003-04-21 21:36:41 +0000831 return(-1);
832 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000833 }
834 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000835
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000836 /*
837 * Discard the consumed input when needed and possible
838 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000839 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillardf6bc7c22003-09-17 22:33:22 +0000840 if ((reader->cur >= 4096) &&
841 (inbuf->use - reader->cur <= CHUNK_SIZE)) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000842 val = xmlBufferShrink(inbuf, reader->cur);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000843 if (val >= 0) {
844 reader->cur -= val;
845 }
846 }
847 }
848
849 /*
850 * At the end of the stream signal that the work is done to the Push
851 * parser.
852 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000853 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000854 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000855 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000856 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000857 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000858 s, 1);
859 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000860 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard16ed5972003-11-20 18:22:31 +0000861 if ((val != 0) && (reader->ctxt->wellFormed == 0))
862 return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000863 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000864 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000865 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000866 return(0);
867}
868
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000869#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000870/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000871 * xmlTextReaderValidatePush:
872 * @reader: the xmlTextReaderPtr used
873 *
874 * Push the current node for validation
875 */
876static void
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000877xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000878 xmlNodePtr node = reader->node;
879
Daniel Veillardf4e55762003-04-15 23:32:22 +0000880 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
881 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
882 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
883 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
884 reader->ctxt->myDoc, node, node->name);
885 } else {
886 /* TODO use the BuildQName interface */
887 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000888
Daniel Veillardf4e55762003-04-15 23:32:22 +0000889 qname = xmlStrdup(node->ns->prefix);
890 qname = xmlStrcat(qname, BAD_CAST ":");
891 qname = xmlStrcat(qname, node->name);
892 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
893 reader->ctxt->myDoc, node, qname);
894 if (qname != NULL)
895 xmlFree(qname);
896 }
897#ifdef LIBXML_SCHEMAS_ENABLED
898 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
899 (reader->rngValidCtxt != NULL)) {
900 int ret;
901
902 if (reader->rngFullNode != NULL) return;
903 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
904 reader->ctxt->myDoc,
905 node);
906 if (ret == 0) {
907 /*
908 * this element requires a full tree
909 */
910 node = xmlTextReaderExpand(reader);
911 if (node == NULL) {
912printf("Expand failed !\n");
913 ret = -1;
914 } else {
915 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
916 reader->ctxt->myDoc,
917 node);
918 reader->rngFullNode = node;
919 }
920 }
921 if (ret != 1)
922 reader->rngValidErrors++;
923#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000924 }
925}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000926
927/**
928 * xmlTextReaderValidateCData:
929 * @reader: the xmlTextReaderPtr used
930 * @data: pointer to the CData
931 * @len: lenght of the CData block in bytes.
932 *
933 * Push some CData for validation
934 */
935static void
936xmlTextReaderValidateCData(xmlTextReaderPtr reader,
937 const xmlChar *data, int len) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000938 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
939 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
940 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
941 data, len);
942#ifdef LIBXML_SCHEMAS_ENABLED
943 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
944 (reader->rngValidCtxt != NULL)) {
945 int ret;
946
947 if (reader->rngFullNode != NULL) return;
948 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
949 if (ret != 1)
950 reader->rngValidErrors++;
951#endif
952 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000953}
954
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000955/**
956 * xmlTextReaderValidatePop:
957 * @reader: the xmlTextReaderPtr used
958 *
959 * Pop the current node from validation
960 */
961static void
962xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
963 xmlNodePtr node = reader->node;
964
Daniel Veillardf4e55762003-04-15 23:32:22 +0000965 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
966 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
967 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
968 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
969 reader->ctxt->myDoc, node, node->name);
970 } else {
971 /* TODO use the BuildQName interface */
972 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000973
Daniel Veillardf4e55762003-04-15 23:32:22 +0000974 qname = xmlStrdup(node->ns->prefix);
975 qname = xmlStrcat(qname, BAD_CAST ":");
976 qname = xmlStrcat(qname, node->name);
977 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
978 reader->ctxt->myDoc, node, qname);
979 if (qname != NULL)
980 xmlFree(qname);
981 }
982#ifdef LIBXML_SCHEMAS_ENABLED
983 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
984 (reader->rngValidCtxt != NULL)) {
985 int ret;
986
987 if (reader->rngFullNode != NULL) {
988 if (node == reader->rngFullNode)
989 reader->rngFullNode = NULL;
990 return;
991 }
992 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
993 reader->ctxt->myDoc,
994 node);
995 if (ret != 1)
996 reader->rngValidErrors++;
997#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000998 }
999}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001000
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001001/**
1002 * xmlTextReaderValidateEntity:
1003 * @reader: the xmlTextReaderPtr used
1004 *
1005 * Handle the validation when an entity reference is encountered and
1006 * entity substitution is not activated. As a result the parser interface
1007 * must walk through the entity and do the validation calls
1008 */
1009static void
1010xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
1011 xmlNodePtr oldnode = reader->node;
1012 xmlNodePtr node = reader->node;
1013 xmlParserCtxtPtr ctxt = reader->ctxt;
1014
1015 do {
1016 if (node->type == XML_ENTITY_REF_NODE) {
1017 /*
1018 * Case where the underlying tree is not availble, lookup the entity
1019 * and walk it.
1020 */
1021 if ((node->children == NULL) && (ctxt->sax != NULL) &&
1022 (ctxt->sax->getEntity != NULL)) {
1023 node->children = (xmlNodePtr)
1024 ctxt->sax->getEntity(ctxt, node->name);
1025 }
1026
1027 if ((node->children != NULL) &&
1028 (node->children->type == XML_ENTITY_DECL) &&
1029 (node->children->children != NULL)) {
1030 xmlTextReaderEntPush(reader, node);
1031 node = node->children->children;
1032 continue;
1033 } else {
1034 /*
1035 * The error has probably be raised already.
1036 */
1037 if (node == oldnode)
1038 break;
1039 node = node->next;
1040 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001041#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001042 } else if (node->type == XML_ELEMENT_NODE) {
1043 reader->node = node;
1044 xmlTextReaderValidatePush(reader);
1045 } else if ((node->type == XML_TEXT_NODE) ||
1046 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001047 xmlTextReaderValidateCData(reader, node->content,
1048 xmlStrlen(node->content));
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001049#endif
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001050 }
1051
1052 /*
1053 * go to next node
1054 */
1055 if (node->children != NULL) {
1056 node = node->children;
1057 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00001058 } else if (node->type == XML_ELEMENT_NODE) {
1059 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001060 }
1061 if (node->next != NULL) {
1062 node = node->next;
1063 continue;
1064 }
1065 do {
1066 node = node->parent;
1067 if (node->type == XML_ELEMENT_NODE) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001068 xmlNodePtr tmp;
Daniel Veillard1d211e22003-10-20 22:32:39 +00001069 if (reader->entNr == 0) {
1070 while ((tmp = node->last) != NULL) {
Daniel Veillarde8039df2003-10-27 11:25:13 +00001071 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard1d211e22003-10-20 22:32:39 +00001072 xmlUnlinkNode(tmp);
1073 xmlTextReaderFreeNode(reader, tmp);
1074 } else
1075 break;
1076 }
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001077 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001078 reader->node = node;
1079 xmlTextReaderValidatePop(reader);
1080 }
1081 if ((node->type == XML_ENTITY_DECL) &&
1082 (reader->ent != NULL) && (reader->ent->children == node)) {
1083 node = xmlTextReaderEntPop(reader);
1084 }
1085 if (node == oldnode)
1086 break;
1087 if (node->next != NULL) {
1088 node = node->next;
1089 break;
1090 }
1091 } while ((node != NULL) && (node != oldnode));
1092 } while ((node != NULL) && (node != oldnode));
1093 reader->node = oldnode;
1094}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001095#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001096
1097
1098/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001099 * xmlTextReaderGetSuccessor:
1100 * @cur: the current node
1101 *
1102 * Get the successor of a node if available.
1103 *
1104 * Returns the successor node or NULL
1105 */
1106static xmlNodePtr
1107xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1108 if (cur == NULL) return(NULL) ; /* ERROR */
1109 if (cur->next != NULL) return(cur->next) ;
1110 do {
1111 cur = cur->parent;
1112 if (cur == NULL) return(NULL);
1113 if (cur->next != NULL) return(cur->next);
1114 } while (cur != NULL);
1115 return(cur);
1116}
1117
1118/**
1119 * xmlTextReaderDoExpand:
1120 * @reader: the xmlTextReaderPtr used
1121 *
1122 * Makes sure that the current node is fully read as well as all its
1123 * descendant. It means the full DOM subtree must be available at the
1124 * end of the call.
1125 *
1126 * Returns 1 if the node was expanded successfully, 0 if there is no more
1127 * nodes to read, or -1 in case of error
1128 */
1129static int
1130xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1131 int val;
1132
1133 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1134 return(-1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001135 do {
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001136 if (reader->ctxt->instate == XML_PARSER_EOF) return(1);
1137
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001138 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1139 return(1);
Daniel Veillardf96cf4e2003-10-01 09:05:25 +00001140 if (reader->ctxt->nodeNr < reader->depth)
Daniel Veillarda37aab82003-06-09 09:10:36 +00001141 return(1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001142 if (reader->mode == XML_TEXTREADER_MODE_EOF)
1143 return(1);
1144 val = xmlTextReaderPushData(reader);
1145 if (val < 0)
1146 return(-1);
1147 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1148 return(1);
1149}
1150
1151/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001152 * xmlTextReaderRead:
1153 * @reader: the xmlTextReaderPtr used
1154 *
1155 * Moves the position of the current instance to the next node in
1156 * the stream, exposing its properties.
1157 *
1158 * Returns 1 if the node was read successfully, 0 if there is no more
1159 * nodes to read, or -1 in case of error
1160 */
1161int
1162xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +00001163 int val, olddepth = 0;
William M. Brack899e64a2003-09-26 18:03:42 +00001164 xmlTextReaderState oldstate = XML_TEXTREADER_START;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001165 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001166
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001167
1168 if (reader == NULL)
1169 return(-1);
1170 if (reader->doc != NULL)
1171 return(xmlTextReaderReadTree(reader));
1172 if (reader->ctxt == NULL)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001173 return(-1);
1174 if (reader->ctxt->wellFormed != 1)
1175 return(-1);
1176
1177#ifdef DEBUG_READER
1178 fprintf(stderr, "\nREAD ");
1179 DUMP_READER
1180#endif
Daniel Veillard29b3e282002-12-29 11:14:41 +00001181 reader->curnode = NULL;
Daniel Veillard67df8092002-12-16 22:04:11 +00001182 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1183 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001184 /*
1185 * Initial state
1186 */
1187 do {
1188 val = xmlTextReaderPushData(reader);
1189 if (val < 0)
1190 return(-1);
1191 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001192 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1193 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001194 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001195 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001196 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001197 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001198 if (reader->node == NULL)
1199 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001200 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001201 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +00001202 if (reader->ctxt->myDoc != NULL) {
1203 reader->node = reader->ctxt->myDoc->children;
1204 }
1205 if (reader->node == NULL)
1206 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +00001207 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001208 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001209 reader->depth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001210 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001211 }
1212 oldstate = reader->state;
1213 olddepth = reader->ctxt->nodeNr;
1214 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001215
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001216get_next_node:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001217 /*
1218 * If we are not backtracking on ancestors or examined nodes,
1219 * that the parser didn't finished or that we arent at the end
1220 * of stream, continue processing.
1221 */
Daniel Veillarda880b122003-04-21 21:36:41 +00001222 while ((reader->node->next == NULL) &&
1223 (reader->ctxt->nodeNr == olddepth) &&
1224 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +00001225 (reader->node->children == NULL) ||
1226 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard409a8142003-07-18 15:16:57 +00001227 ((reader->node->children != NULL) &&
1228 (reader->node->children->type == XML_TEXT_NODE) &&
1229 (reader->node->children->next == NULL)) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001230 (reader->node->type == XML_DTD_NODE) ||
1231 (reader->node->type == XML_DOCUMENT_NODE) ||
1232 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001233 ((reader->ctxt->node == NULL) ||
1234 (reader->ctxt->node == reader->node) ||
1235 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +00001236 (reader->ctxt->instate != XML_PARSER_EOF)) {
1237 val = xmlTextReaderPushData(reader);
1238 if (val < 0)
1239 return(-1);
1240 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001241 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001242 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001243 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001244 if ((reader->node->children != NULL) &&
1245 (reader->node->type != XML_ENTITY_REF_NODE) &&
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001246 (reader->node->type != XML_XINCLUDE_START) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001247 (reader->node->type != XML_DTD_NODE)) {
1248 reader->node = reader->node->children;
1249 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001250 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001251 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001252 }
1253 }
1254 if (reader->node->next != NULL) {
1255 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +00001256 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001257 (reader->node->children == NULL) &&
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001258 ((reader->node->extra & NODE_IS_EMPTY) == 0) &&
1259 (reader->in_xinclude <= 0)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001260 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001261 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001262 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001263#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001264 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001265 (reader->node->type == XML_ELEMENT_NODE))
1266 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001267#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001268 if ((reader->preserves > 0) &&
1269 (reader->node->extra & NODE_IS_SPRESERVED))
1270 reader->preserves--;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001271 reader->node = reader->node->next;
1272 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001273
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001274 /*
1275 * Cleanup of the old node
1276 */
Daniel Veillard1e906612003-12-05 14:57:46 +00001277 if ((reader->preserves == 0) &&
1278 (reader->in_xinclude == 0) &&
1279 (reader->entNr == 0) &&
1280 (reader->node->prev != NULL) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001281 (reader->node->prev->type != XML_DTD_NODE) &&
1282 (reader->entNr == 0)) {
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001283 xmlNodePtr tmp = reader->node->prev;
Daniel Veillarde8039df2003-10-27 11:25:13 +00001284 if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001285 xmlUnlinkNode(tmp);
1286 xmlTextReaderFreeNode(reader, tmp);
1287 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001288 }
1289
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001290 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001291 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001292 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +00001293 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001294 (reader->node->children == NULL) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001295 ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001296 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001297 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001298 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001299#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001300 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001301 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001302#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001303 if ((reader->preserves > 0) &&
1304 (reader->node->extra & NODE_IS_SPRESERVED))
1305 reader->preserves--;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001306 reader->node = reader->node->parent;
1307 if ((reader->node == NULL) ||
1308 (reader->node->type == XML_DOCUMENT_NODE) ||
1309#ifdef LIBXML_DOCB_ENABLED
1310 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
1311#endif
1312 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001313 if (reader->mode != XML_TEXTREADER_DONE) {
1314 val = xmlParseChunk(reader->ctxt, "", 0, 1);
1315 reader->mode = XML_TEXTREADER_DONE;
1316 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001317 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001318 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001319
1320 /*
1321 * Cleanup of the old node
1322 */
Daniel Veillard1e906612003-12-05 14:57:46 +00001323 if ((reader->preserves == 0) &&
1324 (reader->in_xinclude == 0) &&
1325 (reader->entNr == 0) &&
1326 (oldnode->type != XML_DTD_NODE) &&
Daniel Veillarde8039df2003-10-27 11:25:13 +00001327 ((oldnode->extra & NODE_IS_PRESERVED) == 0) &&
Daniel Veillard1d211e22003-10-20 22:32:39 +00001328 (reader->entNr == 0)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001329 xmlUnlinkNode(oldnode);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001330 xmlTextReaderFreeNode(reader, oldnode);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001331 }
1332
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001333 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001334 }
Daniel Veillard1e906612003-12-05 14:57:46 +00001335 if ((reader->preserves == 0) &&
1336 (reader->in_xinclude == 0) &&
1337 (reader->entNr == 0) &&
1338 (reader->node->last != NULL) &&
1339 ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
1340 xmlNodePtr tmp = reader->node->last;
1341 xmlUnlinkNode(tmp);
1342 xmlTextReaderFreeNode(reader, tmp);
1343 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001344 reader->depth--;
1345 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001346
1347node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001348 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001349
1350 /*
Daniel Veillarda880b122003-04-21 21:36:41 +00001351 * If we are in the middle of a piece of CDATA make sure it's finished
1352 */
1353 if ((reader->node != NULL) &&
Daniel Veillardccc4d2b2003-09-17 21:27:31 +00001354 (reader->node->next == NULL) &&
Daniel Veillarda880b122003-04-21 21:36:41 +00001355 ((reader->node->type == XML_TEXT_NODE) ||
1356 (reader->node->type == XML_CDATA_SECTION_NODE))) {
1357 xmlTextReaderExpand(reader);
1358 }
1359
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001360#ifdef LIBXML_XINCLUDE_ENABLED
1361 /*
1362 * Handle XInclude if asked for
1363 */
1364 if ((reader->xinclude) && (reader->node != NULL) &&
1365 (reader->node->type == XML_ELEMENT_NODE) &&
1366 (reader->node->ns != NULL) &&
1367 (xmlStrEqual(reader->node->ns->href, XINCLUDE_NS))) {
1368 if (reader->xincctxt == NULL) {
1369 reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
1370 }
1371 /*
1372 * expand that node and process it
1373 */
1374 xmlTextReaderExpand(reader);
1375 xmlXIncludeProcessNode(reader->xincctxt, reader->node);
1376 }
1377 if (reader->node->type == XML_XINCLUDE_START) {
1378 reader->in_xinclude++;
1379 goto get_next_node;
1380 }
1381 if (reader->node->type == XML_XINCLUDE_END) {
1382 reader->in_xinclude--;
1383 goto get_next_node;
1384 }
1385#endif
Daniel Veillarda880b122003-04-21 21:36:41 +00001386 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001387 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001388 */
1389 if ((reader->node != NULL) &&
1390 (reader->node->type == XML_ENTITY_REF_NODE) &&
1391 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1392 /*
1393 * Case where the underlying tree is not availble, lookup the entity
1394 * and walk it.
1395 */
1396 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
1397 (reader->ctxt->sax->getEntity != NULL)) {
1398 reader->node->children = (xmlNodePtr)
1399 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
1400 }
1401
1402 if ((reader->node->children != NULL) &&
1403 (reader->node->children->type == XML_ENTITY_DECL) &&
1404 (reader->node->children->children != NULL)) {
1405 xmlTextReaderEntPush(reader, reader->node);
1406 reader->node = reader->node->children->children;
1407 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001408#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001409 } else if ((reader->node != NULL) &&
1410 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +00001411 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001412 xmlTextReaderValidateEntity(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001413#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001414 }
1415 if ((reader->node != NULL) &&
1416 (reader->node->type == XML_ENTITY_DECL) &&
1417 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1418 reader->node = xmlTextReaderEntPop(reader);
1419 reader->depth++;
1420 goto get_next_node;
1421 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001422#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001423 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001424 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001425
1426 if ((node->type == XML_ELEMENT_NODE) &&
1427 ((reader->state != XML_TEXTREADER_END) &&
1428 (reader->state != XML_TEXTREADER_BACKTRACK))) {
1429 xmlTextReaderValidatePush(reader);
1430 } else if ((node->type == XML_TEXT_NODE) ||
1431 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001432 xmlTextReaderValidateCData(reader, node->content,
1433 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001434 }
1435 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001436#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1e906612003-12-05 14:57:46 +00001437#ifdef LIBXML_PATTERN_ENABLED
1438 if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
1439 (reader->state != XML_TEXTREADER_BACKTRACK)) {
1440 int i;
1441 for (i = 0;i < reader->patternNr;i++) {
1442 if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
1443 xmlTextReaderPreserve(reader);
1444 break;
1445 }
1446 }
1447 }
1448#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001449 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001450node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001451 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001452 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001453}
1454
Daniel Veillard67df8092002-12-16 22:04:11 +00001455/**
1456 * xmlTextReaderReadState:
1457 * @reader: the xmlTextReaderPtr used
1458 *
1459 * Gets the read state of the reader.
1460 *
1461 * Returns the state value, or -1 in case of error
1462 */
1463int
1464xmlTextReaderReadState(xmlTextReaderPtr reader) {
1465 if (reader == NULL)
1466 return(-1);
1467 return(reader->mode);
1468}
1469
1470/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001471 * xmlTextReaderExpand:
1472 * @reader: the xmlTextReaderPtr used
1473 *
1474 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +00001475 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001476 *
1477 * Returns a node pointer valid until the next xmlTextReaderRead() call
1478 * or NULL in case of error.
1479 */
1480xmlNodePtr
1481xmlTextReaderExpand(xmlTextReaderPtr reader) {
1482 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1483 return(NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001484 if (reader->doc != NULL)
1485 return(reader->node);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001486 if (xmlTextReaderDoExpand(reader) < 0)
1487 return(NULL);
1488 return(reader->node);
1489}
1490
1491/**
1492 * xmlTextReaderNext:
1493 * @reader: the xmlTextReaderPtr used
1494 *
1495 * Skip to the node following the current one in document order while
1496 * avoiding the subtree if any.
1497 *
1498 * Returns 1 if the node was read successfully, 0 if there is no more
1499 * nodes to read, or -1 in case of error
1500 */
1501int
1502xmlTextReaderNext(xmlTextReaderPtr reader) {
1503 int ret;
1504 xmlNodePtr cur;
1505
1506 if (reader == NULL)
1507 return(-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001508 if (reader->doc != NULL)
1509 return(xmlTextReaderNextTree(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001510 cur = reader->node;
1511 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1512 return(xmlTextReaderRead(reader));
1513 if (reader->state == XML_TEXTREADER_END)
1514 return(xmlTextReaderRead(reader));
Daniel Veillarde8039df2003-10-27 11:25:13 +00001515 if (cur->extra & NODE_IS_EMPTY)
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001516 return(xmlTextReaderRead(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001517 do {
1518 ret = xmlTextReaderRead(reader);
1519 if (ret != 1)
1520 return(ret);
1521 } while (reader->node != cur);
1522 return(xmlTextReaderRead(reader));
1523}
1524
1525/**
Daniel Veillard67df8092002-12-16 22:04:11 +00001526 * xmlTextReaderReadInnerXml:
1527 * @reader: the xmlTextReaderPtr used
1528 *
1529 * Reads the contents of the current node, including child nodes and markup.
1530 *
1531 * Returns a string containing the XML content, or NULL if the current node
1532 * is neither an element nor attribute, or has no child nodes. The
1533 * string must be deallocated by the caller.
1534 */
1535xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001536xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001537 TODO
1538 return(NULL);
1539}
1540
1541/**
1542 * xmlTextReaderReadOuterXml:
1543 * @reader: the xmlTextReaderPtr used
1544 *
1545 * Reads the contents of the current node, including child nodes and markup.
1546 *
1547 * Returns a string containing the XML content, or NULL if the current node
1548 * is neither an element nor attribute, or has no child nodes. The
1549 * string must be deallocated by the caller.
1550 */
1551xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001552xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001553 TODO
1554 return(NULL);
1555}
1556
1557/**
1558 * xmlTextReaderReadString:
1559 * @reader: the xmlTextReaderPtr used
1560 *
1561 * Reads the contents of an element or a text node as a string.
1562 *
1563 * Returns a string containing the contents of the Element or Text node,
1564 * or NULL if the reader is positioned on any other type of node.
1565 * The string must be deallocated by the caller.
1566 */
1567xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001568xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001569 TODO
1570 return(NULL);
1571}
1572
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001573#if 0
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001574/**
1575 * xmlTextReaderReadBase64:
1576 * @reader: the xmlTextReaderPtr used
1577 * @array: a byte array to store the content.
1578 * @offset: the zero-based index into array where the method should
1579 * begin to write.
1580 * @len: the number of bytes to write.
1581 *
1582 * Reads and decodes the Base64 encoded contents of an element and
1583 * stores the result in a byte buffer.
1584 *
1585 * Returns the number of bytes written to array, or zero if the current
1586 * instance is not positioned on an element or -1 in case of error.
1587 */
1588int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001589xmlTextReaderReadBase64(xmlTextReaderPtr reader,
1590 unsigned char *array ATTRIBUTE_UNUSED,
1591 int offset ATTRIBUTE_UNUSED,
1592 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001593 if ((reader == NULL) || (reader->ctxt == NULL))
1594 return(-1);
1595 if (reader->ctxt->wellFormed != 1)
1596 return(-1);
1597
1598 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1599 return(0);
1600 TODO
1601 return(0);
1602}
1603
1604/**
1605 * xmlTextReaderReadBinHex:
1606 * @reader: the xmlTextReaderPtr used
1607 * @array: a byte array to store the content.
1608 * @offset: the zero-based index into array where the method should
1609 * begin to write.
1610 * @len: the number of bytes to write.
1611 *
1612 * Reads and decodes the BinHex encoded contents of an element and
1613 * stores the result in a byte buffer.
1614 *
1615 * Returns the number of bytes written to array, or zero if the current
1616 * instance is not positioned on an element or -1 in case of error.
1617 */
1618int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001619xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
1620 unsigned char *array ATTRIBUTE_UNUSED,
1621 int offset ATTRIBUTE_UNUSED,
1622 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001623 if ((reader == NULL) || (reader->ctxt == NULL))
1624 return(-1);
1625 if (reader->ctxt->wellFormed != 1)
1626 return(-1);
1627
1628 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1629 return(0);
1630 TODO
1631 return(0);
1632}
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001633#endif
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001634
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001635/************************************************************************
1636 * *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001637 * Operating on a preparsed tree *
1638 * *
1639 ************************************************************************/
1640static int
1641xmlTextReaderNextTree(xmlTextReaderPtr reader)
1642{
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001643 if (reader == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001644 return(-1);
1645
1646 if (reader->state == XML_TEXTREADER_END)
1647 return(0);
1648
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001649 if (reader->node == NULL) {
1650 if (reader->doc->children == NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001651 reader->state = XML_TEXTREADER_END;
1652 return(0);
1653 }
1654
1655 reader->node = reader->doc->children;
1656 reader->state = XML_TEXTREADER_START;
1657 return(1);
1658 }
1659
1660 if (reader->state != XML_TEXTREADER_BACKTRACK) {
1661 if (reader->node->children != 0) {
1662 reader->node = reader->node->children;
1663 reader->depth++;
1664 reader->state = XML_TEXTREADER_START;
1665 return(1);
1666 }
1667
1668 if ((reader->node->type == XML_ELEMENT_NODE) ||
1669 (reader->node->type == XML_ATTRIBUTE_NODE)) {
1670 reader->state = XML_TEXTREADER_BACKTRACK;
1671 return(1);
1672 }
1673 }
1674
1675 if (reader->node->next != 0) {
1676 reader->node = reader->node->next;
1677 reader->state = XML_TEXTREADER_START;
1678 return(1);
1679 }
1680
1681 if (reader->node->parent != 0) {
1682 if (reader->node->parent->type == XML_DOCUMENT_NODE) {
1683 reader->state = XML_TEXTREADER_END;
1684 return(0);
1685 }
1686
1687 reader->node = reader->node->parent;
1688 reader->depth--;
1689 reader->state = XML_TEXTREADER_BACKTRACK;
1690 return(1);
1691 }
1692
1693 reader->state = XML_TEXTREADER_END;
1694
1695 return(1);
1696}
1697
1698/**
1699 * xmlTextReaderReadTree:
1700 * @reader: the xmlTextReaderPtr used
1701 *
1702 * Moves the position of the current instance to the next node in
1703 * the stream, exposing its properties.
1704 *
1705 * Returns 1 if the node was read successfully, 0 if there is no more
1706 * nodes to read, or -1 in case of error
1707 */
1708static int
1709xmlTextReaderReadTree(xmlTextReaderPtr reader) {
1710 if (reader->state == XML_TEXTREADER_END)
1711 return(0);
1712
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001713next_node:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001714 if (reader->node == NULL) {
1715 if (reader->doc->children == NULL) {
1716 reader->state = XML_TEXTREADER_END;
1717 return(0);
1718 }
1719
1720 reader->node = reader->doc->children;
1721 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001722 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001723 }
1724
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001725 if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
1726 (reader->node->type != XML_DTD_NODE) &&
1727 (reader->node->type != XML_XINCLUDE_START) &&
1728 (reader->node->type != XML_ENTITY_REF_NODE)) {
1729 if (reader->node->children != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001730 reader->node = reader->node->children;
1731 reader->depth++;
1732 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001733 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001734 }
1735
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001736 if (reader->node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001737 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001738 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001739 }
1740 }
1741
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001742 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001743 reader->node = reader->node->next;
1744 reader->state = XML_TEXTREADER_START;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001745 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001746 }
1747
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001748 if (reader->node->parent != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001749 if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
1750 (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
1751 reader->state = XML_TEXTREADER_END;
1752 return(0);
1753 }
1754
1755 reader->node = reader->node->parent;
1756 reader->depth--;
1757 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001758 goto found_node;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001759 }
1760
1761 reader->state = XML_TEXTREADER_END;
1762
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001763found_node:
1764 if ((reader->node->type == XML_XINCLUDE_START) ||
1765 (reader->node->type == XML_XINCLUDE_END))
1766 goto next_node;
1767
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001768 return(1);
1769}
1770
1771/**
William M. Brackb1d53162003-11-18 06:54:40 +00001772 * xmlTextReaderNextSibling:
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001773 * @reader: the xmlTextReaderPtr used
1774 *
1775 * Skip to the node following the current one in document order while
1776 * avoiding the subtree if any.
1777 * Currently implemented only for Readers built on a document
1778 *
1779 * Returns 1 if the node was read successfully, 0 if there is no more
1780 * nodes to read, or -1 in case of error
1781 */
1782int
1783xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
1784 if (reader == NULL)
1785 return(-1);
1786 if (reader->doc == NULL) {
1787 TODO
1788 return(-1);
1789 }
1790
1791 if (reader->state == XML_TEXTREADER_END)
1792 return(0);
1793
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001794 if (reader->node == NULL)
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001795 return(xmlTextReaderNextTree(reader));
1796
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001797 if (reader->node->next != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001798 reader->node = reader->node->next;
1799 reader->state = XML_TEXTREADER_START;
1800 return(1);
1801 }
1802
1803 return(0);
1804}
1805
1806/************************************************************************
1807 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001808 * Constructor and destructors *
1809 * *
1810 ************************************************************************/
1811/**
1812 * xmlNewTextReader:
1813 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001814 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001815 *
1816 * Create an xmlTextReader structure fed with @input
1817 *
1818 * Returns the new xmlTextReaderPtr or NULL in case of error
1819 */
1820xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001821xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001822 xmlTextReaderPtr ret;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001823
1824 if (input == NULL)
1825 return(NULL);
1826 ret = xmlMalloc(sizeof(xmlTextReader));
1827 if (ret == NULL) {
1828 xmlGenericError(xmlGenericErrorContext,
1829 "xmlNewTextReader : malloc failed\n");
1830 return(NULL);
1831 }
1832 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001833 ret->doc = NULL;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001834 ret->entTab = NULL;
1835 ret->entMax = 0;
1836 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001837 ret->input = input;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001838 ret->buffer = xmlBufferCreateSize(100);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001839 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1840 if (ret->sax == NULL) {
1841 xmlFree(ret);
1842 xmlGenericError(xmlGenericErrorContext,
1843 "xmlNewTextReader : malloc failed\n");
1844 return(NULL);
1845 }
Daniel Veillard81273902003-09-30 00:43:48 +00001846 xmlSAXVersion(ret->sax, 2);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001847 ret->startElement = ret->sax->startElement;
1848 ret->sax->startElement = xmlTextReaderStartElement;
1849 ret->endElement = ret->sax->endElement;
1850 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillard81273902003-09-30 00:43:48 +00001851#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001852 if (ret->sax->initialized == XML_SAX2_MAGIC) {
Daniel Veillard81273902003-09-30 00:43:48 +00001853#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001854 ret->startElementNs = ret->sax->startElementNs;
1855 ret->sax->startElementNs = xmlTextReaderStartElementNs;
1856 ret->endElementNs = ret->sax->endElementNs;
1857 ret->sax->endElementNs = xmlTextReaderEndElementNs;
Daniel Veillard81273902003-09-30 00:43:48 +00001858#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001859 } else {
1860 ret->startElementNs = NULL;
1861 ret->endElementNs = NULL;
1862 }
Daniel Veillard81273902003-09-30 00:43:48 +00001863#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001864 ret->characters = ret->sax->characters;
1865 ret->sax->characters = xmlTextReaderCharacters;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001866 ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001867 ret->cdataBlock = ret->sax->cdataBlock;
1868 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001869
Daniel Veillard67df8092002-12-16 22:04:11 +00001870 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001871 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001872 ret->curnode = NULL;
Daniel Veillard53350552003-09-18 13:35:51 +00001873 if (ret->input->buffer->use < 4) {
William M. Brack899e64a2003-09-26 18:03:42 +00001874 xmlParserInputBufferRead(input, 4);
Daniel Veillard53350552003-09-18 13:35:51 +00001875 }
1876 if (ret->input->buffer->use >= 4) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001877 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001878 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001879 ret->base = 0;
1880 ret->cur = 4;
1881 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001882 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001883 ret->base = 0;
1884 ret->cur = 0;
1885 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001886 if (ret->ctxt == NULL) {
1887 xmlGenericError(xmlGenericErrorContext,
1888 "xmlNewTextReader : malloc failed\n");
1889 xmlFree(ret->sax);
1890 xmlFree(ret);
1891 return(NULL);
1892 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001893 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001894 ret->ctxt->linenumbers = 1;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001895 ret->ctxt->dictNames = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001896 ret->allocs = XML_TEXTREADER_CTXT;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001897 /*
1898 * use the parser dictionnary to allocate all elements and attributes names
1899 */
1900 ret->ctxt->docdict = 1;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001901 ret->dict = ret->ctxt->dict;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001902#ifdef LIBXML_XINCLUDE_ENABLED
1903 ret->xinclude = 0;
1904#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001905#ifdef LIBXML_PATTERN_ENABLED
1906 ret->patternMax = 0;
1907 ret->patternTab = NULL;
1908#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001909 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001910}
1911
1912/**
1913 * xmlNewTextReaderFilename:
1914 * @URI: the URI of the resource to process
1915 *
1916 * Create an xmlTextReader structure fed with the resource at @URI
1917 *
1918 * Returns the new xmlTextReaderPtr or NULL in case of error
1919 */
1920xmlTextReaderPtr
1921xmlNewTextReaderFilename(const char *URI) {
1922 xmlParserInputBufferPtr input;
1923 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001924 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001925
1926 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1927 if (input == NULL)
1928 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001929 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001930 if (ret == NULL) {
1931 xmlFreeParserInputBuffer(input);
1932 return(NULL);
1933 }
1934 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001935 if (ret->ctxt->directory == NULL)
1936 directory = xmlParserGetDirectory(URI);
1937 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1938 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1939 if (directory != NULL)
1940 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001941 return(ret);
1942}
1943
1944/**
1945 * xmlFreeTextReader:
1946 * @reader: the xmlTextReaderPtr
1947 *
1948 * Deallocate all the resources associated to the reader
1949 */
1950void
1951xmlFreeTextReader(xmlTextReaderPtr reader) {
1952 if (reader == NULL)
1953 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001954#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001955 if (reader->rngSchemas != NULL) {
1956 xmlRelaxNGFree(reader->rngSchemas);
1957 reader->rngSchemas = NULL;
1958 }
1959 if (reader->rngValidCtxt != NULL) {
1960 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
1961 reader->rngValidCtxt = NULL;
1962 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001963#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001964#ifdef LIBXML_XINCLUDE_ENABLED
1965 if (reader->xincctxt != NULL)
1966 xmlXIncludeFreeContext(reader->xincctxt);
1967#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00001968#ifdef LIBXML_PATTERN_ENABLED
1969 if (reader->patternTab != NULL) {
1970 int i;
1971 for (i = 0;i < reader->patternNr;i++) {
1972 if (reader->patternTab[i] != NULL)
1973 xmlFreePattern(reader->patternTab[i]);
1974 }
1975 xmlFree(reader->patternTab);
1976 }
1977#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001978 if (reader->ctxt != NULL) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001979 if (reader->dict == reader->ctxt->dict)
1980 reader->dict = NULL;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001981 if (reader->ctxt->myDoc != NULL) {
1982 if (reader->preserve == 0)
1983 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
1984 reader->ctxt->myDoc = NULL;
1985 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00001986 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
1987 (reader->ctxt->vctxt.vstateMax > 0)){
1988 xmlFree(reader->ctxt->vctxt.vstateTab);
1989 reader->ctxt->vctxt.vstateTab = 0;
1990 reader->ctxt->vctxt.vstateMax = 0;
1991 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001992 if (reader->allocs & XML_TEXTREADER_CTXT)
1993 xmlFreeParserCtxt(reader->ctxt);
1994 }
1995 if (reader->sax != NULL)
1996 xmlFree(reader->sax);
1997 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
1998 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001999 if (reader->faketext != NULL) {
2000 xmlFreeNode(reader->faketext);
2001 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002002 if (reader->buffer != NULL)
2003 xmlBufferFree(reader->buffer);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00002004 if (reader->entTab != NULL)
2005 xmlFree(reader->entTab);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002006 if (reader->dict != NULL)
2007 xmlDictFree(reader->dict);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002008 xmlFree(reader);
2009}
2010
2011/************************************************************************
2012 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002013 * Methods for XmlTextReader *
2014 * *
2015 ************************************************************************/
2016/**
2017 * xmlTextReaderClose:
2018 * @reader: the xmlTextReaderPtr used
2019 *
2020 * This method releases any resources allocated by the current instance
2021 * changes the state to Closed and close any underlying input.
2022 *
2023 * Returns 0 or -1 in case of error
2024 */
2025int
2026xmlTextReaderClose(xmlTextReaderPtr reader) {
2027 if (reader == NULL)
2028 return(-1);
2029 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002030 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002031 reader->mode = XML_TEXTREADER_MODE_CLOSED;
2032 if (reader->ctxt != NULL) {
2033 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002034 if (reader->preserve == 0)
2035 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002036 reader->ctxt->myDoc = NULL;
2037 }
2038 if (reader->allocs & XML_TEXTREADER_CTXT) {
2039 xmlFreeParserCtxt(reader->ctxt);
2040 reader->allocs -= XML_TEXTREADER_CTXT;
2041 }
2042 }
2043 if (reader->sax != NULL) {
2044 xmlFree(reader->sax);
2045 reader->sax = NULL;
2046 }
2047 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
2048 xmlFreeParserInputBuffer(reader->input);
2049 reader->allocs -= XML_TEXTREADER_INPUT;
2050 }
2051 return(0);
2052}
2053
2054/**
2055 * xmlTextReaderGetAttributeNo:
2056 * @reader: the xmlTextReaderPtr used
2057 * @no: the zero-based index of the attribute relative to the containing element
2058 *
2059 * Provides the value of the attribute with the specified index relative
2060 * to the containing element.
2061 *
2062 * Returns a string containing the value of the specified attribute, or NULL
2063 * in case of error. The string must be deallocated by the caller.
2064 */
2065xmlChar *
2066xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
2067 xmlChar *ret;
2068 int i;
2069 xmlAttrPtr cur;
2070 xmlNsPtr ns;
2071
2072 if (reader == NULL)
2073 return(NULL);
2074 if (reader->node == NULL)
2075 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002076 if (reader->curnode != NULL)
2077 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002078 /* TODO: handle the xmlDecl */
2079 if (reader->node->type != XML_ELEMENT_NODE)
2080 return(NULL);
2081
2082 ns = reader->node->nsDef;
2083 for (i = 0;(i < no) && (ns != NULL);i++) {
2084 ns = ns->next;
2085 }
2086 if (ns != NULL)
2087 return(xmlStrdup(ns->href));
2088
2089 cur = reader->node->properties;
2090 if (cur == NULL)
2091 return(NULL);
2092 for (;i < no;i++) {
2093 cur = cur->next;
2094 if (cur == NULL)
2095 return(NULL);
2096 }
2097 /* TODO walk the DTD if present */
2098
2099 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
2100 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
2101 return(ret);
2102}
2103
2104/**
2105 * xmlTextReaderGetAttribute:
2106 * @reader: the xmlTextReaderPtr used
2107 * @name: the qualified name of the attribute.
2108 *
2109 * Provides the value of the attribute with the specified qualified name.
2110 *
2111 * Returns a string containing the value of the specified attribute, or NULL
2112 * in case of error. The string must be deallocated by the caller.
2113 */
2114xmlChar *
2115xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2116 xmlChar *prefix = NULL;
2117 xmlChar *localname;
2118 xmlNsPtr ns;
2119 xmlChar *ret = NULL;
2120
2121 if ((reader == NULL) || (name == NULL))
2122 return(NULL);
2123 if (reader->node == NULL)
2124 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002125 if (reader->curnode != NULL)
2126 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002127
2128 /* TODO: handle the xmlDecl */
2129 if (reader->node->type != XML_ELEMENT_NODE)
2130 return(NULL);
2131
2132 localname = xmlSplitQName2(name, &prefix);
2133 if (localname == NULL)
2134 return(xmlGetProp(reader->node, name));
2135
2136 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2137 if (ns != NULL)
2138 ret = xmlGetNsProp(reader->node, localname, ns->href);
2139
2140 if (localname != NULL)
2141 xmlFree(localname);
2142 if (prefix != NULL)
2143 xmlFree(prefix);
2144 return(ret);
2145}
2146
2147
2148/**
2149 * xmlTextReaderGetAttributeNs:
2150 * @reader: the xmlTextReaderPtr used
2151 * @localName: the local name of the attribute.
2152 * @namespaceURI: the namespace URI of the attribute.
2153 *
2154 * Provides the value of the specified attribute
2155 *
2156 * Returns a string containing the value of the specified attribute, or NULL
2157 * in case of error. The string must be deallocated by the caller.
2158 */
2159xmlChar *
2160xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
2161 const xmlChar *namespaceURI) {
2162 if ((reader == NULL) || (localName == NULL))
2163 return(NULL);
2164 if (reader->node == NULL)
2165 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002166 if (reader->curnode != NULL)
2167 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002168
2169 /* TODO: handle the xmlDecl */
2170 if (reader->node->type != XML_ELEMENT_NODE)
2171 return(NULL);
2172
2173 return(xmlGetNsProp(reader->node, localName, namespaceURI));
2174}
2175
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002176/**
2177 * xmlTextReaderGetRemainder:
2178 * @reader: the xmlTextReaderPtr used
2179 *
2180 * Method to get the remainder of the buffered XML. this method stops the
2181 * parser, set its state to End Of File and return the input stream with
2182 * what is left that the parser did not use.
2183 *
2184 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
2185 * in case of error.
2186 */
2187xmlParserInputBufferPtr
2188xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
2189 xmlParserInputBufferPtr ret = NULL;
2190
2191 if (reader == NULL)
2192 return(NULL);
2193 if (reader->node == NULL)
2194 return(NULL);
2195
2196 reader->node = NULL;
2197 reader->curnode = NULL;
2198 reader->mode = XML_TEXTREADER_MODE_EOF;
2199 if (reader->ctxt != NULL) {
2200 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002201 if (reader->preserve == 0)
2202 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002203 reader->ctxt->myDoc = NULL;
2204 }
2205 if (reader->allocs & XML_TEXTREADER_CTXT) {
2206 xmlFreeParserCtxt(reader->ctxt);
2207 reader->allocs -= XML_TEXTREADER_CTXT;
2208 }
2209 }
2210 if (reader->sax != NULL) {
2211 xmlFree(reader->sax);
2212 reader->sax = NULL;
2213 }
2214 if (reader->allocs & XML_TEXTREADER_INPUT) {
2215 ret = reader->input;
2216 reader->allocs -= XML_TEXTREADER_INPUT;
2217 } else {
2218 /*
2219 * Hum, one may need to duplicate the data structure because
2220 * without reference counting the input may be freed twice:
2221 * - by the layer which allocated it.
2222 * - by the layer to which would have been returned to.
2223 */
2224 TODO
2225 return(NULL);
2226 }
2227 return(ret);
2228}
2229
2230/**
2231 * xmlTextReaderLookupNamespace:
2232 * @reader: the xmlTextReaderPtr used
2233 * @prefix: the prefix whose namespace URI is to be resolved. To return
2234 * the default namespace, specify NULL
2235 *
2236 * Resolves a namespace prefix in the scope of the current element.
2237 *
2238 * Returns a string containing the namespace URI to which the prefix maps
2239 * or NULL in case of error. The string must be deallocated by the caller.
2240 */
2241xmlChar *
2242xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
2243 xmlNsPtr ns;
2244
2245 if (reader == NULL)
2246 return(NULL);
2247 if (reader->node == NULL)
2248 return(NULL);
2249
2250 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
2251 if (ns == NULL)
2252 return(NULL);
2253 return(xmlStrdup(ns->href));
2254}
2255
2256/**
2257 * xmlTextReaderMoveToAttributeNo:
2258 * @reader: the xmlTextReaderPtr used
2259 * @no: the zero-based index of the attribute relative to the containing
2260 * element.
2261 *
2262 * Moves the position of the current instance to the attribute with
2263 * the specified index relative to the containing element.
2264 *
2265 * Returns 1 in case of success, -1 in case of error, 0 if not found
2266 */
2267int
2268xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
2269 int i;
2270 xmlAttrPtr cur;
2271 xmlNsPtr ns;
2272
2273 if (reader == NULL)
2274 return(-1);
2275 if (reader->node == NULL)
2276 return(-1);
2277 /* TODO: handle the xmlDecl */
2278 if (reader->node->type != XML_ELEMENT_NODE)
2279 return(-1);
2280
2281 reader->curnode = NULL;
2282
2283 ns = reader->node->nsDef;
2284 for (i = 0;(i < no) && (ns != NULL);i++) {
2285 ns = ns->next;
2286 }
2287 if (ns != NULL) {
2288 reader->curnode = (xmlNodePtr) ns;
2289 return(1);
2290 }
2291
2292 cur = reader->node->properties;
2293 if (cur == NULL)
2294 return(0);
2295 for (;i < no;i++) {
2296 cur = cur->next;
2297 if (cur == NULL)
2298 return(0);
2299 }
2300 /* TODO walk the DTD if present */
2301
2302 reader->curnode = (xmlNodePtr) cur;
2303 return(1);
2304}
2305
2306/**
2307 * xmlTextReaderMoveToAttribute:
2308 * @reader: the xmlTextReaderPtr used
2309 * @name: the qualified name of the attribute.
2310 *
2311 * Moves the position of the current instance to the attribute with
2312 * the specified qualified name.
2313 *
2314 * Returns 1 in case of success, -1 in case of error, 0 if not found
2315 */
2316int
2317xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2318 xmlChar *prefix = NULL;
2319 xmlChar *localname;
2320 xmlNsPtr ns;
2321 xmlAttrPtr prop;
2322
2323 if ((reader == NULL) || (name == NULL))
2324 return(-1);
2325 if (reader->node == NULL)
2326 return(-1);
2327
2328 /* TODO: handle the xmlDecl */
2329 if (reader->node->type != XML_ELEMENT_NODE)
2330 return(0);
2331
2332 localname = xmlSplitQName2(name, &prefix);
2333 if (localname == NULL) {
2334 /*
2335 * Namespace default decl
2336 */
2337 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2338 ns = reader->node->nsDef;
2339 while (ns != NULL) {
2340 if (ns->prefix == NULL) {
2341 reader->curnode = (xmlNodePtr) ns;
2342 return(1);
2343 }
2344 ns = ns->next;
2345 }
2346 return(0);
2347 }
2348
2349 prop = reader->node->properties;
2350 while (prop != NULL) {
2351 /*
2352 * One need to have
2353 * - same attribute names
2354 * - and the attribute carrying that namespace
2355 */
2356 if ((xmlStrEqual(prop->name, name)) &&
2357 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
2358 reader->curnode = (xmlNodePtr) prop;
2359 return(1);
2360 }
2361 prop = prop->next;
2362 }
2363 return(0);
2364 }
2365
2366 /*
2367 * Namespace default decl
2368 */
2369 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2370 ns = reader->node->nsDef;
2371 while (ns != NULL) {
2372 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2373 reader->curnode = (xmlNodePtr) ns;
2374 goto found;
2375 }
2376 ns = ns->next;
2377 }
2378 goto not_found;
2379 }
2380 prop = reader->node->properties;
2381 while (prop != NULL) {
2382 /*
2383 * One need to have
2384 * - same attribute names
2385 * - and the attribute carrying that namespace
2386 */
2387 if ((xmlStrEqual(prop->name, localname)) &&
2388 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2389 reader->curnode = (xmlNodePtr) prop;
2390 goto found;
2391 }
2392 prop = prop->next;
2393 }
2394not_found:
2395 if (localname != NULL)
2396 xmlFree(localname);
2397 if (prefix != NULL)
2398 xmlFree(prefix);
2399 return(0);
2400
2401found:
2402 if (localname != NULL)
2403 xmlFree(localname);
2404 if (prefix != NULL)
2405 xmlFree(prefix);
2406 return(1);
2407}
2408
2409/**
2410 * xmlTextReaderMoveToAttributeNs:
2411 * @reader: the xmlTextReaderPtr used
2412 * @localName: the local name of the attribute.
2413 * @namespaceURI: the namespace URI of the attribute.
2414 *
2415 * Moves the position of the current instance to the attribute with the
2416 * specified local name and namespace URI.
2417 *
2418 * Returns 1 in case of success, -1 in case of error, 0 if not found
2419 */
2420int
2421xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
2422 const xmlChar *localName, const xmlChar *namespaceURI) {
2423 xmlAttrPtr prop;
2424 xmlNodePtr node;
2425
2426 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2427 return(-1);
2428 if (reader->node == NULL)
2429 return(-1);
2430 if (reader->node->type != XML_ELEMENT_NODE)
2431 return(0);
2432 node = reader->node;
2433
2434 /*
2435 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
2436 * namespace name associated to "xmlns"
2437 */
2438 prop = node->properties;
2439 while (prop != NULL) {
2440 /*
2441 * One need to have
2442 * - same attribute names
2443 * - and the attribute carrying that namespace
2444 */
2445 if (xmlStrEqual(prop->name, localName) &&
2446 ((prop->ns != NULL) &&
2447 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2448 reader->curnode = (xmlNodePtr) prop;
2449 return(1);
2450 }
2451 prop = prop->next;
2452 }
2453 return(0);
2454}
2455
2456/**
2457 * xmlTextReaderMoveToFirstAttribute:
2458 * @reader: the xmlTextReaderPtr used
2459 *
2460 * Moves the position of the current instance to the first attribute
2461 * associated with the current node.
2462 *
2463 * Returns 1 in case of success, -1 in case of error, 0 if not found
2464 */
2465int
2466xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
2467 if (reader == NULL)
2468 return(-1);
2469 if (reader->node == NULL)
2470 return(-1);
2471 if (reader->node->type != XML_ELEMENT_NODE)
2472 return(0);
2473
2474 if (reader->node->nsDef != NULL) {
2475 reader->curnode = (xmlNodePtr) reader->node->nsDef;
2476 return(1);
2477 }
2478 if (reader->node->properties != NULL) {
2479 reader->curnode = (xmlNodePtr) reader->node->properties;
2480 return(1);
2481 }
2482 return(0);
2483}
2484
2485/**
2486 * xmlTextReaderMoveToNextAttribute:
2487 * @reader: the xmlTextReaderPtr used
2488 *
2489 * Moves the position of the current instance to the next attribute
2490 * associated with the current node.
2491 *
2492 * Returns 1 in case of success, -1 in case of error, 0 if not found
2493 */
2494int
2495xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
2496 if (reader == NULL)
2497 return(-1);
2498 if (reader->node == NULL)
2499 return(-1);
2500 if (reader->node->type != XML_ELEMENT_NODE)
2501 return(0);
2502 if (reader->curnode == NULL)
2503 return(xmlTextReaderMoveToFirstAttribute(reader));
2504
2505 if (reader->curnode->type == XML_NAMESPACE_DECL) {
2506 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2507 if (ns->next != NULL) {
2508 reader->curnode = (xmlNodePtr) ns->next;
2509 return(1);
2510 }
2511 if (reader->node->properties != NULL) {
2512 reader->curnode = (xmlNodePtr) reader->node->properties;
2513 return(1);
2514 }
2515 return(0);
2516 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2517 (reader->curnode->next != NULL)) {
2518 reader->curnode = reader->curnode->next;
2519 return(1);
2520 }
2521 return(0);
2522}
2523
2524/**
2525 * xmlTextReaderMoveToElement:
2526 * @reader: the xmlTextReaderPtr used
2527 *
2528 * Moves the position of the current instance to the node that
2529 * contains the current Attribute node.
2530 *
2531 * Returns 1 in case of success, -1 in case of error, 0 if not moved
2532 */
2533int
2534xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
2535 if (reader == NULL)
2536 return(-1);
2537 if (reader->node == NULL)
2538 return(-1);
2539 if (reader->node->type != XML_ELEMENT_NODE)
2540 return(0);
2541 if (reader->curnode != NULL) {
2542 reader->curnode = NULL;
2543 return(1);
2544 }
2545 return(0);
2546}
2547
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002548/**
2549 * xmlTextReaderReadAttributeValue:
2550 * @reader: the xmlTextReaderPtr used
2551 *
2552 * Parses an attribute value into one or more Text and EntityReference nodes.
2553 *
2554 * Returns 1 in case of success, 0 if the reader was not positionned on an
2555 * ttribute node or all the attribute values have been read, or -1
2556 * in case of error.
2557 */
2558int
2559xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
2560 if (reader == NULL)
2561 return(-1);
2562 if (reader->node == NULL)
2563 return(-1);
2564 if (reader->curnode == NULL)
2565 return(0);
2566 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2567 if (reader->curnode->children == NULL)
2568 return(0);
2569 reader->curnode = reader->curnode->children;
2570 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2571 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2572
2573 if (reader->faketext == NULL) {
2574 reader->faketext = xmlNewDocText(reader->node->doc,
2575 ns->href);
2576 } else {
2577 if (reader->faketext->content != NULL)
2578 xmlFree(reader->faketext->content);
2579 reader->faketext->content = xmlStrdup(ns->href);
2580 }
2581 reader->curnode = reader->faketext;
2582 } else {
2583 if (reader->curnode->next == NULL)
2584 return(0);
2585 reader->curnode = reader->curnode->next;
2586 }
2587 return(1);
2588}
2589
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002590/************************************************************************
2591 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002592 * Acces API to the current node *
2593 * *
2594 ************************************************************************/
2595/**
2596 * xmlTextReaderAttributeCount:
2597 * @reader: the xmlTextReaderPtr used
2598 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002599 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002600 *
2601 * Returns 0 i no attributes, -1 in case of error or the attribute count
2602 */
2603int
2604xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
2605 int ret;
2606 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00002607 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002608 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002609
2610 if (reader == NULL)
2611 return(-1);
2612 if (reader->node == NULL)
2613 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002614
2615 if (reader->curnode != NULL)
2616 node = reader->curnode;
2617 else
2618 node = reader->node;
2619
2620 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002621 return(0);
2622 if ((reader->state == XML_TEXTREADER_END) ||
2623 (reader->state == XML_TEXTREADER_BACKTRACK))
2624 return(0);
2625 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002626 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002627 while (attr != NULL) {
2628 ret++;
2629 attr = attr->next;
2630 }
Daniel Veillard67df8092002-12-16 22:04:11 +00002631 ns = node->nsDef;
2632 while (ns != NULL) {
2633 ret++;
2634 ns = ns->next;
2635 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002636 return(ret);
2637}
2638
2639/**
2640 * xmlTextReaderNodeType:
2641 * @reader: the xmlTextReaderPtr used
2642 *
2643 * Get the node type of the current node
2644 * Reference:
2645 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
2646 *
2647 * Returns the xmlNodeType of the current node or -1 in case of error
2648 */
2649int
2650xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002651 xmlNodePtr node;
Daniel Veillardd6038e02003-07-30 16:37:18 +00002652
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002653 if (reader == NULL)
2654 return(-1);
2655 if (reader->node == NULL)
Daniel Veillardd6038e02003-07-30 16:37:18 +00002656 return(XML_READER_TYPE_NONE);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002657 if (reader->curnode != NULL)
2658 node = reader->curnode;
2659 else
2660 node = reader->node;
2661 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002662 case XML_ELEMENT_NODE:
2663 if ((reader->state == XML_TEXTREADER_END) ||
2664 (reader->state == XML_TEXTREADER_BACKTRACK))
Daniel Veillardd6038e02003-07-30 16:37:18 +00002665 return(XML_READER_TYPE_END_ELEMENT);
2666 return(XML_READER_TYPE_ELEMENT);
Daniel Veillardecaba492002-12-30 10:55:29 +00002667 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002668 case XML_ATTRIBUTE_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002669 return(XML_READER_TYPE_ATTRIBUTE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002670 case XML_TEXT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002671 if (xmlIsBlankNode(reader->node)) {
2672 if (xmlNodeGetSpacePreserve(reader->node))
2673 return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2674 else
2675 return(XML_READER_TYPE_WHITESPACE);
2676 } else {
2677 return(XML_READER_TYPE_TEXT);
2678 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002679 case XML_CDATA_SECTION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002680 return(XML_READER_TYPE_CDATA);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002681 case XML_ENTITY_REF_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002682 return(XML_READER_TYPE_ENTITY_REFERENCE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002683 case XML_ENTITY_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002684 return(XML_READER_TYPE_ENTITY);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002685 case XML_PI_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002686 return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002687 case XML_COMMENT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002688 return(XML_READER_TYPE_COMMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002689 case XML_DOCUMENT_NODE:
2690 case XML_HTML_DOCUMENT_NODE:
2691#ifdef LIBXML_DOCB_ENABLED
2692 case XML_DOCB_DOCUMENT_NODE:
2693#endif
Daniel Veillardd6038e02003-07-30 16:37:18 +00002694 return(XML_READER_TYPE_DOCUMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002695 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002696 return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002697 case XML_NOTATION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002698 return(XML_READER_TYPE_NOTATION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002699 case XML_DOCUMENT_TYPE_NODE:
2700 case XML_DTD_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002701 return(XML_READER_TYPE_DOCUMENT_TYPE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002702
2703 case XML_ELEMENT_DECL:
2704 case XML_ATTRIBUTE_DECL:
2705 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002706 case XML_XINCLUDE_START:
2707 case XML_XINCLUDE_END:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002708 return(XML_READER_TYPE_NONE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002709 }
2710 return(-1);
2711}
2712
2713/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002714 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002715 * @reader: the xmlTextReaderPtr used
2716 *
2717 * Check if the current node is empty
2718 *
2719 * Returns 1 if empty, 0 if not and -1 in case of error
2720 */
2721int
2722xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
2723 if ((reader == NULL) || (reader->node == NULL))
2724 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00002725 if (reader->node->type != XML_ELEMENT_NODE)
2726 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00002727 if (reader->curnode != NULL)
2728 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002729 if (reader->node->children != NULL)
2730 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00002731 if (reader->state == XML_TEXTREADER_END)
2732 return(0);
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002733 if (reader->doc != NULL)
2734 return(1);
2735 if (reader->in_xinclude > 0)
2736 return(1);
Daniel Veillarde8039df2003-10-27 11:25:13 +00002737 return((reader->node->extra & NODE_IS_EMPTY) != 0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002738}
2739
2740/**
2741 * xmlTextReaderLocalName:
2742 * @reader: the xmlTextReaderPtr used
2743 *
2744 * The local name of the node.
2745 *
2746 * Returns the local name or NULL if not available
2747 */
2748xmlChar *
2749xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002750 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002751 if ((reader == NULL) || (reader->node == NULL))
2752 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002753 if (reader->curnode != NULL)
2754 node = reader->curnode;
2755 else
2756 node = reader->node;
2757 if (node->type == XML_NAMESPACE_DECL) {
2758 xmlNsPtr ns = (xmlNsPtr) node;
2759 if (ns->prefix == NULL)
2760 return(xmlStrdup(BAD_CAST "xmlns"));
2761 else
2762 return(xmlStrdup(ns->prefix));
2763 }
2764 if ((node->type != XML_ELEMENT_NODE) &&
2765 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002766 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002767 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002768}
2769
2770/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002771 * xmlTextReaderConstLocalName:
2772 * @reader: the xmlTextReaderPtr used
2773 *
2774 * The local name of the node.
2775 *
2776 * Returns the local name or NULL if not available, the
2777 * string will be deallocated with the reader.
2778 */
2779const xmlChar *
2780xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
2781 xmlNodePtr node;
2782 if ((reader == NULL) || (reader->node == NULL))
2783 return(NULL);
2784 if (reader->curnode != NULL)
2785 node = reader->curnode;
2786 else
2787 node = reader->node;
2788 if (node->type == XML_NAMESPACE_DECL) {
2789 xmlNsPtr ns = (xmlNsPtr) node;
2790 if (ns->prefix == NULL)
2791 return(CONSTSTR(BAD_CAST "xmlns"));
2792 else
2793 return(ns->prefix);
2794 }
2795 if ((node->type != XML_ELEMENT_NODE) &&
2796 (node->type != XML_ATTRIBUTE_NODE))
2797 return(xmlTextReaderConstName(reader));
2798 return(node->name);
2799}
2800
2801/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002802 * xmlTextReaderName:
2803 * @reader: the xmlTextReaderPtr used
2804 *
2805 * The qualified name of the node, equal to Prefix :LocalName.
2806 *
2807 * Returns the local name or NULL if not available
2808 */
2809xmlChar *
2810xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002811 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002812 xmlChar *ret;
2813
2814 if ((reader == NULL) || (reader->node == NULL))
2815 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002816 if (reader->curnode != NULL)
2817 node = reader->curnode;
2818 else
2819 node = reader->node;
2820 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002821 case XML_ELEMENT_NODE:
2822 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002823 if ((node->ns == NULL) ||
2824 (node->ns->prefix == NULL))
2825 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002826
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002827 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002828 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002829 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002830 return(ret);
2831 case XML_TEXT_NODE:
2832 return(xmlStrdup(BAD_CAST "#text"));
2833 case XML_CDATA_SECTION_NODE:
2834 return(xmlStrdup(BAD_CAST "#cdata-section"));
2835 case XML_ENTITY_NODE:
2836 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002837 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002838 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002839 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002840 case XML_COMMENT_NODE:
2841 return(xmlStrdup(BAD_CAST "#comment"));
2842 case XML_DOCUMENT_NODE:
2843 case XML_HTML_DOCUMENT_NODE:
2844#ifdef LIBXML_DOCB_ENABLED
2845 case XML_DOCB_DOCUMENT_NODE:
2846#endif
2847 return(xmlStrdup(BAD_CAST "#document"));
2848 case XML_DOCUMENT_FRAG_NODE:
2849 return(xmlStrdup(BAD_CAST "#document-fragment"));
2850 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002851 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002852 case XML_DOCUMENT_TYPE_NODE:
2853 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002854 return(xmlStrdup(node->name));
2855 case XML_NAMESPACE_DECL: {
2856 xmlNsPtr ns = (xmlNsPtr) node;
2857
2858 ret = xmlStrdup(BAD_CAST "xmlns");
2859 if (ns->prefix == NULL)
2860 return(ret);
2861 ret = xmlStrcat(ret, BAD_CAST ":");
2862 ret = xmlStrcat(ret, ns->prefix);
2863 return(ret);
2864 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002865
2866 case XML_ELEMENT_DECL:
2867 case XML_ATTRIBUTE_DECL:
2868 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002869 case XML_XINCLUDE_START:
2870 case XML_XINCLUDE_END:
2871 return(NULL);
2872 }
2873 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002874}
2875
2876/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002877 * xmlTextReaderConstName:
2878 * @reader: the xmlTextReaderPtr used
2879 *
2880 * The qualified name of the node, equal to Prefix :LocalName.
2881 *
2882 * Returns the local name or NULL if not available, the string is
2883 * deallocated with the reader.
2884 */
2885const xmlChar *
2886xmlTextReaderConstName(xmlTextReaderPtr reader) {
2887 xmlNodePtr node;
2888
2889 if ((reader == NULL) || (reader->node == NULL))
2890 return(NULL);
2891 if (reader->curnode != NULL)
2892 node = reader->curnode;
2893 else
2894 node = reader->node;
2895 switch (node->type) {
2896 case XML_ELEMENT_NODE:
2897 case XML_ATTRIBUTE_NODE:
2898 if ((node->ns == NULL) ||
2899 (node->ns->prefix == NULL))
2900 return(node->name);
2901 return(CONSTQSTR(node->ns->prefix, node->name));
2902 case XML_TEXT_NODE:
2903 return(CONSTSTR(BAD_CAST "#text"));
2904 case XML_CDATA_SECTION_NODE:
2905 return(CONSTSTR(BAD_CAST "#cdata-section"));
2906 case XML_ENTITY_NODE:
2907 case XML_ENTITY_REF_NODE:
2908 return(CONSTSTR(node->name));
2909 case XML_PI_NODE:
2910 return(CONSTSTR(node->name));
2911 case XML_COMMENT_NODE:
2912 return(CONSTSTR(BAD_CAST "#comment"));
2913 case XML_DOCUMENT_NODE:
2914 case XML_HTML_DOCUMENT_NODE:
2915#ifdef LIBXML_DOCB_ENABLED
2916 case XML_DOCB_DOCUMENT_NODE:
2917#endif
2918 return(CONSTSTR(BAD_CAST "#document"));
2919 case XML_DOCUMENT_FRAG_NODE:
2920 return(CONSTSTR(BAD_CAST "#document-fragment"));
2921 case XML_NOTATION_NODE:
2922 return(CONSTSTR(node->name));
2923 case XML_DOCUMENT_TYPE_NODE:
2924 case XML_DTD_NODE:
2925 return(CONSTSTR(node->name));
2926 case XML_NAMESPACE_DECL: {
2927 xmlNsPtr ns = (xmlNsPtr) node;
2928
2929 if (ns->prefix == NULL)
2930 return(CONSTSTR(BAD_CAST "xmlns"));
2931 return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
2932 }
2933
2934 case XML_ELEMENT_DECL:
2935 case XML_ATTRIBUTE_DECL:
2936 case XML_ENTITY_DECL:
2937 case XML_XINCLUDE_START:
2938 case XML_XINCLUDE_END:
2939 return(NULL);
2940 }
2941 return(NULL);
2942}
2943
2944/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002945 * xmlTextReaderPrefix:
2946 * @reader: the xmlTextReaderPtr used
2947 *
2948 * A shorthand reference to the namespace associated with the node.
2949 *
2950 * Returns the prefix or NULL if not available
2951 */
2952xmlChar *
2953xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002954 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002955 if ((reader == NULL) || (reader->node == NULL))
2956 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002957 if (reader->curnode != NULL)
2958 node = reader->curnode;
2959 else
2960 node = reader->node;
2961 if (node->type == XML_NAMESPACE_DECL) {
2962 xmlNsPtr ns = (xmlNsPtr) node;
2963 if (ns->prefix == NULL)
2964 return(NULL);
2965 return(xmlStrdup(BAD_CAST "xmlns"));
2966 }
2967 if ((node->type != XML_ELEMENT_NODE) &&
2968 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002969 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00002970 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002971 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002972 return(NULL);
2973}
2974
2975/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002976 * xmlTextReaderConstPrefix:
2977 * @reader: the xmlTextReaderPtr used
2978 *
2979 * A shorthand reference to the namespace associated with the node.
2980 *
2981 * Returns the prefix or NULL if not available, the string is deallocated
2982 * with the reader.
2983 */
2984const xmlChar *
2985xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
2986 xmlNodePtr node;
2987 if ((reader == NULL) || (reader->node == NULL))
2988 return(NULL);
2989 if (reader->curnode != NULL)
2990 node = reader->curnode;
2991 else
2992 node = reader->node;
2993 if (node->type == XML_NAMESPACE_DECL) {
2994 xmlNsPtr ns = (xmlNsPtr) node;
2995 if (ns->prefix == NULL)
2996 return(NULL);
2997 return(CONSTSTR(BAD_CAST "xmlns"));
2998 }
2999 if ((node->type != XML_ELEMENT_NODE) &&
3000 (node->type != XML_ATTRIBUTE_NODE))
3001 return(NULL);
3002 if ((node->ns != NULL) && (node->ns->prefix != NULL))
3003 return(CONSTSTR(node->ns->prefix));
3004 return(NULL);
3005}
3006
3007/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003008 * xmlTextReaderNamespaceUri:
3009 * @reader: the xmlTextReaderPtr used
3010 *
3011 * The URI defining the namespace associated with the node.
3012 *
3013 * Returns the namespace URI or NULL if not available
3014 */
3015xmlChar *
3016xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003017 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003018 if ((reader == NULL) || (reader->node == NULL))
3019 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003020 if (reader->curnode != NULL)
3021 node = reader->curnode;
3022 else
3023 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00003024 if (node->type == XML_NAMESPACE_DECL)
3025 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003026 if ((node->type != XML_ELEMENT_NODE) &&
3027 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003028 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003029 if (node->ns != NULL)
3030 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003031 return(NULL);
3032}
3033
3034/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003035 * xmlTextReaderConstNamespaceUri:
3036 * @reader: the xmlTextReaderPtr used
3037 *
3038 * The URI defining the namespace associated with the node.
3039 *
3040 * Returns the namespace URI or NULL if not available, the string
3041 * will be deallocated with the reader
3042 */
3043const xmlChar *
3044xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
3045 xmlNodePtr node;
3046 if ((reader == NULL) || (reader->node == NULL))
3047 return(NULL);
3048 if (reader->curnode != NULL)
3049 node = reader->curnode;
3050 else
3051 node = reader->node;
3052 if (node->type == XML_NAMESPACE_DECL)
3053 return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
3054 if ((node->type != XML_ELEMENT_NODE) &&
3055 (node->type != XML_ATTRIBUTE_NODE))
3056 return(NULL);
3057 if (node->ns != NULL)
3058 return(CONSTSTR(node->ns->href));
3059 return(NULL);
3060}
3061
3062/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003063 * xmlTextReaderBaseUri:
3064 * @reader: the xmlTextReaderPtr used
3065 *
3066 * The base URI of the node.
3067 *
3068 * Returns the base URI or NULL if not available
3069 */
3070xmlChar *
3071xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
3072 if ((reader == NULL) || (reader->node == NULL))
3073 return(NULL);
3074 return(xmlNodeGetBase(NULL, reader->node));
3075}
3076
3077/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00003078 * xmlTextReaderConstBaseUri:
3079 * @reader: the xmlTextReaderPtr used
3080 *
3081 * The base URI of the node.
3082 *
3083 * Returns the base URI or NULL if not available, the string
3084 * will be deallocated with the reader
3085 */
3086const xmlChar *
3087xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
3088 xmlChar *tmp;
3089 const xmlChar *ret;
3090
3091 if ((reader == NULL) || (reader->node == NULL))
3092 return(NULL);
3093 tmp = xmlNodeGetBase(NULL, reader->node);
3094 if (tmp == NULL)
3095 return(NULL);
3096 ret = CONSTSTR(tmp);
3097 xmlFree(tmp);
3098 return(ret);
3099}
3100
3101/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003102 * xmlTextReaderDepth:
3103 * @reader: the xmlTextReaderPtr used
3104 *
3105 * The depth of the node in the tree.
3106 *
3107 * Returns the depth or -1 in case of error
3108 */
3109int
3110xmlTextReaderDepth(xmlTextReaderPtr reader) {
3111 if (reader == NULL)
3112 return(-1);
3113 if (reader->node == NULL)
3114 return(0);
3115
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003116 if (reader->curnode != NULL) {
3117 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
3118 (reader->curnode->type == XML_NAMESPACE_DECL))
3119 return(reader->depth + 1);
3120 return(reader->depth + 2);
3121 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003122 return(reader->depth);
3123}
3124
3125/**
3126 * xmlTextReaderHasAttributes:
3127 * @reader: the xmlTextReaderPtr used
3128 *
3129 * Whether the node has attributes.
3130 *
3131 * Returns 1 if true, 0 if false, and -1 in case or error
3132 */
3133int
3134xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003135 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003136 if (reader == NULL)
3137 return(-1);
3138 if (reader->node == NULL)
3139 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003140 if (reader->curnode != NULL)
3141 node = reader->curnode;
3142 else
3143 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003144
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003145 if ((node->type == XML_ELEMENT_NODE) &&
3146 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003147 return(1);
3148 /* TODO: handle the xmlDecl */
3149 return(0);
3150}
3151
3152/**
3153 * xmlTextReaderHasValue:
3154 * @reader: the xmlTextReaderPtr used
3155 *
3156 * Whether the node can have a text value.
3157 *
3158 * Returns 1 if true, 0 if false, and -1 in case or error
3159 */
3160int
3161xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003162 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003163 if (reader == NULL)
3164 return(-1);
3165 if (reader->node == NULL)
3166 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003167 if (reader->curnode != NULL)
3168 node = reader->curnode;
3169 else
3170 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003171
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003172 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003173 case XML_ATTRIBUTE_NODE:
3174 case XML_TEXT_NODE:
3175 case XML_CDATA_SECTION_NODE:
3176 case XML_PI_NODE:
3177 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00003178 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003179 return(1);
3180 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003181 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003182 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003183 return(0);
3184}
3185
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003186/**
3187 * xmlTextReaderValue:
3188 * @reader: the xmlTextReaderPtr used
3189 *
3190 * Provides the text value of the node if present
3191 *
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003192 * Returns the string or NULL if not available. The result must be deallocated
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003193 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003194 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003195xmlChar *
3196xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003197 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003198 if (reader == NULL)
3199 return(NULL);
3200 if (reader->node == NULL)
3201 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003202 if (reader->curnode != NULL)
3203 node = reader->curnode;
3204 else
3205 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003206
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003207 switch (node->type) {
3208 case XML_NAMESPACE_DECL:
3209 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003210 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003211 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003212
3213 if (attr->parent != NULL)
3214 return (xmlNodeListGetString
3215 (attr->parent->doc, attr->children, 1));
3216 else
3217 return (xmlNodeListGetString(NULL, attr->children, 1));
3218 break;
3219 }
3220 case XML_TEXT_NODE:
3221 case XML_CDATA_SECTION_NODE:
3222 case XML_PI_NODE:
3223 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00003224 if (node->content != NULL)
3225 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003226 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00003227 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003228 }
3229 return(NULL);
3230}
3231
3232/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003233 * xmlTextReaderConstValue:
3234 * @reader: the xmlTextReaderPtr used
3235 *
3236 * Provides the text value of the node if present
3237 *
3238 * Returns the string or NULL if not available. The result will be
3239 * deallocated on the next Read() operation.
3240 */
3241const xmlChar *
3242xmlTextReaderConstValue(xmlTextReaderPtr reader) {
3243 xmlNodePtr node;
3244 if (reader == NULL)
3245 return(NULL);
3246 if (reader->node == NULL)
3247 return(NULL);
3248 if (reader->curnode != NULL)
3249 node = reader->curnode;
3250 else
3251 node = reader->node;
3252
3253 switch (node->type) {
3254 case XML_NAMESPACE_DECL:
3255 return(((xmlNsPtr) node)->href);
3256 case XML_ATTRIBUTE_NODE:{
3257 xmlAttrPtr attr = (xmlAttrPtr) node;
3258
3259 if ((attr->children != NULL) &&
3260 (attr->children->type == XML_TEXT_NODE) &&
3261 (attr->children->next == NULL))
3262 return(attr->children->content);
3263 else {
3264 reader->buffer->use = 0;
3265 xmlNodeBufGetContent(reader->buffer, node);
3266 return(reader->buffer->content);
3267 }
3268 break;
3269 }
3270 case XML_TEXT_NODE:
3271 case XML_CDATA_SECTION_NODE:
3272 case XML_PI_NODE:
3273 case XML_COMMENT_NODE:
3274 return(node->content);
3275 default:
3276 break;
3277 }
3278 return(NULL);
3279}
3280
3281/**
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00003282 * xmlTextReaderIsDefault:
3283 * @reader: the xmlTextReaderPtr used
3284 *
3285 * Whether an Attribute node was generated from the default value
3286 * defined in the DTD or schema.
3287 *
3288 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
3289 */
3290int
3291xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
3292 if (reader == NULL)
3293 return(-1);
3294 return(0);
3295}
3296
3297/**
3298 * xmlTextReaderQuoteChar:
3299 * @reader: the xmlTextReaderPtr used
3300 *
3301 * The quotation mark character used to enclose the value of an attribute.
3302 *
3303 * Returns " or ' and -1 in case of error
3304 */
3305int
3306xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
3307 if (reader == NULL)
3308 return(-1);
3309 /* TODO maybe lookup the attribute value for " first */
3310 return((int) '"');
3311}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003312
3313/**
3314 * xmlTextReaderXmlLang:
3315 * @reader: the xmlTextReaderPtr used
3316 *
3317 * The xml:lang scope within which the node resides.
3318 *
3319 * Returns the xml:lang value or NULL if none exists.
3320 */
3321xmlChar *
3322xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
3323 if (reader == NULL)
3324 return(NULL);
3325 if (reader->node == NULL)
3326 return(NULL);
3327 return(xmlNodeGetLang(reader->node));
3328}
3329
Daniel Veillard67df8092002-12-16 22:04:11 +00003330/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00003331 * xmlTextReaderConstXmlLang:
Daniel Veillarde72c5082003-09-19 12:44:05 +00003332 * @reader: the xmlTextReaderPtr used
3333 *
3334 * The xml:lang scope within which the node resides.
3335 *
3336 * Returns the xml:lang value or NULL if none exists.
3337 */
3338const xmlChar *
3339xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
3340 xmlChar *tmp;
3341 const xmlChar *ret;
3342
3343 if (reader == NULL)
3344 return(NULL);
3345 if (reader->node == NULL)
3346 return(NULL);
3347 tmp = xmlNodeGetLang(reader->node);
3348 if (tmp == NULL)
3349 return(NULL);
3350 ret = CONSTSTR(tmp);
3351 xmlFree(tmp);
3352 return(ret);
3353}
3354
3355/**
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00003356 * xmlTextReaderConstString:
3357 * @reader: the xmlTextReaderPtr used
3358 * @str: the string to intern.
3359 *
3360 * Get an interned string from the reader, allows for example to
3361 * speedup string name comparisons
3362 *
3363 * Returns an interned copy of the string or NULL in case of error. The
3364 * string will be deallocated with the reader.
3365 */
3366const xmlChar *
3367xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
3368 if (reader == NULL)
3369 return(NULL);
3370 return(CONSTSTR(str));
3371}
3372
3373/**
Daniel Veillard67df8092002-12-16 22:04:11 +00003374 * xmlTextReaderNormalization:
3375 * @reader: the xmlTextReaderPtr used
3376 *
3377 * The value indicating whether to normalize white space and attribute values.
3378 * Since attribute value and end of line normalizations are a MUST in the XML
3379 * specification only the value true is accepted. The broken bahaviour of
3380 * accepting out of range character entities like &#0; is of course not
3381 * supported either.
3382 *
3383 * Returns 1 or -1 in case of error.
3384 */
3385int
3386xmlTextReaderNormalization(xmlTextReaderPtr reader) {
3387 if (reader == NULL)
3388 return(-1);
3389 return(1);
3390}
3391
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003392/************************************************************************
3393 * *
3394 * Extensions to the base APIs *
3395 * *
3396 ************************************************************************/
3397
3398/**
3399 * xmlTextReaderSetParserProp:
3400 * @reader: the xmlTextReaderPtr used
3401 * @prop: the xmlParserProperties to set
3402 * @value: usually 0 or 1 to (de)activate it
3403 *
3404 * Change the parser processing behaviour by changing some of its internal
3405 * properties. Note that some properties can only be changed before any
3406 * read has been done.
3407 *
3408 * Returns 0 if the call was successful, or -1 in case of error
3409 */
3410int
3411xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
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 (value != 0) {
3422 if (ctxt->loadsubset == 0) {
3423 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3424 return(-1);
3425 ctxt->loadsubset = XML_DETECT_IDS;
3426 }
3427 } else {
3428 ctxt->loadsubset = 0;
3429 }
3430 return(0);
3431 case XML_PARSER_DEFAULTATTRS:
3432 if (value != 0) {
3433 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
3434 } else {
3435 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3436 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
3437 }
3438 return(0);
3439 case XML_PARSER_VALIDATE:
3440 if (value != 0) {
3441 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00003442 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003443 } else {
3444 ctxt->validate = 0;
3445 }
3446 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003447 case XML_PARSER_SUBST_ENTITIES:
3448 if (value != 0) {
3449 ctxt->replaceEntities = 1;
3450 } else {
3451 ctxt->replaceEntities = 0;
3452 }
3453 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003454 }
3455 return(-1);
3456}
3457
3458/**
3459 * xmlTextReaderGetParserProp:
3460 * @reader: the xmlTextReaderPtr used
3461 * @prop: the xmlParserProperties to get
3462 *
3463 * Read the parser internal property.
3464 *
3465 * Returns the value, usually 0 or 1, or -1 in case of error.
3466 */
3467int
3468xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
3469 xmlParserProperties p = (xmlParserProperties) prop;
3470 xmlParserCtxtPtr ctxt;
3471
3472 if ((reader == NULL) || (reader->ctxt == NULL))
3473 return(-1);
3474 ctxt = reader->ctxt;
3475
3476 switch (p) {
3477 case XML_PARSER_LOADDTD:
3478 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
3479 return(1);
3480 return(0);
3481 case XML_PARSER_DEFAULTATTRS:
3482 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3483 return(1);
3484 return(0);
3485 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00003486 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003487 case XML_PARSER_SUBST_ENTITIES:
3488 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003489 }
3490 return(-1);
3491}
3492
Daniel Veillarde18fc182002-12-28 22:56:33 +00003493/**
3494 * xmlTextReaderCurrentNode:
3495 * @reader: the xmlTextReaderPtr used
3496 *
3497 * Hacking interface allowing to get the xmlNodePtr correponding to the
3498 * current node being accessed by the xmlTextReader. This is dangerous
3499 * because the underlying node may be destroyed on the next Reads.
3500 *
3501 * Returns the xmlNodePtr or NULL in case of error.
3502 */
3503xmlNodePtr
3504xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
3505 if (reader == NULL)
3506 return(NULL);
3507
3508 if (reader->curnode != NULL)
3509 return(reader->curnode);
3510 return(reader->node);
3511}
3512
3513/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003514 * xmlTextReaderPreserve:
3515 * @reader: the xmlTextReaderPtr used
3516 *
3517 *
3518 * current node being accessed by the xmlTextReader. This is dangerous
3519 * because the underlying node may be destroyed on the next Reads.
3520 *
3521 * Returns the xmlNodePtr or NULL in case of error.
3522 */
3523xmlNodePtr
3524xmlTextReaderPreserve(xmlTextReaderPtr reader) {
3525 xmlNodePtr cur, parent;
3526
3527 if (reader == NULL)
3528 return(NULL);
3529
3530 if (reader->curnode != NULL)
3531 cur = reader->curnode;
3532 else
3533 cur = reader->node;
3534 if (cur == NULL)
3535 return(NULL);
Daniel Veillard1e906612003-12-05 14:57:46 +00003536
3537 if (cur->type != XML_DOCUMENT_NODE) {
3538 cur->extra |= NODE_IS_PRESERVED;
3539 cur->extra |= NODE_IS_SPRESERVED;
3540 }
3541 reader->preserves++;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003542
3543 parent = cur->parent;;
3544 while (parent != NULL) {
Daniel Veillard1e906612003-12-05 14:57:46 +00003545 if (parent->type == XML_ELEMENT_NODE)
3546 parent->extra |= NODE_IS_PRESERVED;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003547 parent = parent->parent;
3548 }
3549 return(cur);
3550}
3551
Daniel Veillard1e906612003-12-05 14:57:46 +00003552#ifdef LIBXML_PATTERN_ENABLED
3553/**
3554 * xmlTextReaderPreservePattern:
3555 * @reader: the xmlTextReaderPtr used
3556 * @pattern: an XPath subset pattern
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003557 * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
Daniel Veillard1e906612003-12-05 14:57:46 +00003558 *
3559 * This tells the XML Reader to preserve all nodes matched by the
3560 * pattern. The caller must also use xmlTextReaderCurrentDoc() to
3561 * keep an handle on the resulting document once parsing has finished
3562 *
3563 * Returns a positive number in case of success and -1 in case of error
3564 */
3565int
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003566xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern,
3567 const xmlChar **namespaces)
3568{
Daniel Veillard1e906612003-12-05 14:57:46 +00003569 xmlPatternPtr comp;
3570
3571 if ((reader == NULL) || (pattern == NULL))
3572 return(-1);
3573
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003574 comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
Daniel Veillard1e906612003-12-05 14:57:46 +00003575 if (comp == NULL)
3576 return(-1);
3577
3578 if (reader->patternMax <= 0) {
3579 reader->patternMax = 4;
3580 reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax *
3581 sizeof(reader->patternTab[0]));
3582 if (reader->patternTab == NULL) {
3583 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3584 return (-1);
3585 }
3586 }
3587 if (reader->patternNr >= reader->patternMax) {
3588 xmlPatternPtr *tmp;
3589 reader->patternMax *= 2;
3590 tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab,
3591 reader->patternMax *
3592 sizeof(reader->patternTab[0]));
3593 if (tmp == NULL) {
3594 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3595 reader->patternMax /= 2;
3596 return (-1);
3597 }
3598 reader->patternTab = tmp;
3599 }
3600 reader->patternTab[reader->patternNr] = comp;
3601 return(reader->patternNr++);
3602}
3603#endif
3604
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003605/**
Daniel Veillarde18fc182002-12-28 22:56:33 +00003606 * xmlTextReaderCurrentDoc:
3607 * @reader: the xmlTextReaderPtr used
3608 *
3609 * Hacking interface allowing to get the xmlDocPtr correponding to the
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003610 * current document being accessed by the xmlTextReader.
3611 * NOTE: as a result of this call, the reader will not destroy the
3612 * associated XML document and calling xmlFreeDoc() on the result
3613 * is needed once the reader parsing has finished.
Daniel Veillarde18fc182002-12-28 22:56:33 +00003614 *
3615 * Returns the xmlDocPtr or NULL in case of error.
3616 */
3617xmlDocPtr
3618xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003619 if (reader == NULL)
3620 return(NULL);
3621 if (reader->doc != NULL)
3622 return(reader->doc);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003623 if ((reader == NULL) || (reader->ctxt == NULL) ||
3624 (reader->ctxt->myDoc == NULL))
Daniel Veillarde18fc182002-12-28 22:56:33 +00003625 return(NULL);
3626
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003627 reader->preserve = 1;
3628 if ((reader->ctxt->myDoc->dict != NULL) &&
3629 (reader->ctxt->myDoc->dict == reader->ctxt->dict))
3630 xmlDictReference(reader->ctxt->dict);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003631 return(reader->ctxt->myDoc);
3632}
3633
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003634#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00003635/**
Daniel Veillard33300b42003-04-17 09:09:19 +00003636 * xmlTextReaderRelaxNGSetSchema:
3637 * @reader: the xmlTextReaderPtr used
3638 * @schema: a precompiled RelaxNG schema
3639 *
3640 * Use RelaxNG to validate the document as it is processed.
3641 * Activation is only possible before the first Read().
3642 * if @schema is NULL, then RelaxNG validation is desactivated.
3643 @ The @schema should not be freed until the reader is deallocated
3644 * or its use has been deactivated.
3645 *
3646 * Returns 0 in case the RelaxNG validation could be (des)activated and
3647 * -1 in case of error.
3648 */
3649int
3650xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
3651 if (schema == NULL) {
3652 if (reader->rngSchemas != NULL) {
3653 xmlRelaxNGFree(reader->rngSchemas);
3654 reader->rngSchemas = NULL;
3655 }
3656 if (reader->rngValidCtxt != NULL) {
3657 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3658 reader->rngValidCtxt = NULL;
3659 }
3660 return(0);
3661 }
3662 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3663 return(-1);
3664 if (reader->rngSchemas != NULL) {
3665 xmlRelaxNGFree(reader->rngSchemas);
3666 reader->rngSchemas = NULL;
3667 }
3668 if (reader->rngValidCtxt != NULL) {
3669 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3670 reader->rngValidCtxt = NULL;
3671 }
3672 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
3673 if (reader->rngValidCtxt == NULL)
3674 return(-1);
3675 if (reader->errorFunc != NULL) {
3676 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3677 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3678 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3679 reader->errorFuncArg);
3680 }
3681 reader->rngValidErrors = 0;
3682 reader->rngFullNode = NULL;
3683 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3684 return(0);
3685}
3686
3687/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00003688 * xmlTextReaderRelaxNGValidate:
3689 * @reader: the xmlTextReaderPtr used
3690 * @rng: the path to a RelaxNG schema or NULL
3691 *
3692 * Use RelaxNG to validate the document as it is processed.
3693 * Activation is only possible before the first Read().
3694 * if @rng is NULL, then RelaxNG validation is desactivated.
3695 *
3696 * Returns 0 in case the RelaxNG validation could be (des)activated and
3697 * -1 in case of error.
3698 */
3699int
3700xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
3701 xmlRelaxNGParserCtxtPtr ctxt;
3702
3703 if (reader == NULL)
3704 return(-1);
3705
3706 if (rng == NULL) {
3707 if (reader->rngSchemas != NULL) {
3708 xmlRelaxNGFree(reader->rngSchemas);
3709 reader->rngSchemas = NULL;
3710 }
3711 if (reader->rngValidCtxt != NULL) {
3712 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3713 reader->rngValidCtxt = NULL;
3714 }
3715 return(0);
3716 }
3717 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3718 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00003719 if (reader->rngSchemas != NULL) {
3720 xmlRelaxNGFree(reader->rngSchemas);
3721 reader->rngSchemas = NULL;
3722 }
3723 if (reader->rngValidCtxt != NULL) {
3724 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3725 reader->rngValidCtxt = NULL;
3726 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00003727 ctxt = xmlRelaxNGNewParserCtxt(rng);
3728 if (reader->errorFunc != NULL) {
3729 xmlRelaxNGSetParserErrors(ctxt,
3730 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
3731 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3732 reader->errorFuncArg);
3733 }
3734 reader->rngSchemas = xmlRelaxNGParse(ctxt);
3735 xmlRelaxNGFreeParserCtxt(ctxt);
3736 if (reader->rngSchemas == NULL)
3737 return(-1);
3738 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
3739 if (reader->rngValidCtxt == NULL)
3740 return(-1);
3741 if (reader->errorFunc != NULL) {
3742 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3743 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3744 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3745 reader->errorFuncArg);
3746 }
3747 reader->rngValidErrors = 0;
3748 reader->rngFullNode = NULL;
3749 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3750 return(0);
3751}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003752#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00003753
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003754/************************************************************************
3755 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00003756 * Error Handling Extensions *
3757 * *
3758 ************************************************************************/
3759
3760/* helper to build a xmlMalloc'ed string from a format and va_list */
3761static char *
3762xmlTextReaderBuildMessage(const char *msg, va_list ap) {
3763 int size;
3764 int chars;
3765 char *larger;
3766 char *str;
3767
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003768 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00003769 if (str == NULL) {
3770 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3771 return NULL;
3772 }
3773
3774 size = 150;
3775
3776 while (1) {
3777 chars = vsnprintf(str, size, msg, ap);
3778 if ((chars > -1) && (chars < size))
3779 break;
3780 if (chars > -1)
3781 size += chars + 1;
3782 else
3783 size += 100;
3784 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
3785 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3786 xmlFree(str);
3787 return NULL;
3788 }
3789 str = larger;
3790 }
3791
3792 return str;
3793}
3794
Daniel Veillard417be3a2003-01-20 21:26:34 +00003795/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003796 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003797 * @locator: the xmlTextReaderLocatorPtr used
3798 *
3799 * Obtain the line number for the given locator.
3800 *
3801 * Returns the line number or -1 in case of error.
3802 */
3803int
3804xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
3805 /* we know that locator is a xmlParserCtxtPtr */
3806 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3807 int ret = -1;
3808
3809 if (ctx->node != NULL) {
3810 ret = xmlGetLineNo(ctx->node);
3811 }
3812 else {
3813 /* inspired from error.c */
3814 xmlParserInputPtr input;
3815 input = ctx->input;
3816 if ((input->filename == NULL) && (ctx->inputNr > 1))
3817 input = ctx->inputTab[ctx->inputNr - 2];
3818 if (input != NULL) {
3819 ret = input->line;
3820 }
3821 else {
3822 ret = -1;
3823 }
3824 }
3825
3826 return ret;
3827}
3828
3829/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003830 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003831 * @locator: the xmlTextReaderLocatorPtr used
3832 *
3833 * Obtain the base URI for the given locator.
3834 *
3835 * Returns the base URI or NULL in case of error.
3836 */
3837xmlChar *
3838xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
3839 /* we know that locator is a xmlParserCtxtPtr */
3840 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3841 xmlChar *ret = NULL;
3842
3843 if (ctx->node != NULL) {
3844 ret = xmlNodeGetBase(NULL,ctx->node);
3845 }
3846 else {
3847 /* inspired from error.c */
3848 xmlParserInputPtr input;
3849 input = ctx->input;
3850 if ((input->filename == NULL) && (ctx->inputNr > 1))
3851 input = ctx->inputTab[ctx->inputNr - 2];
3852 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00003853 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00003854 }
3855 else {
3856 ret = NULL;
3857 }
3858 }
3859
3860 return ret;
3861}
3862
Daniel Veillard26f70262003-01-16 22:45:08 +00003863static void
William M. Brack899e64a2003-09-26 18:03:42 +00003864xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity, char *str) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003865 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
3866 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
3867
3868 if (str != NULL) {
3869 reader->errorFunc(reader->errorFuncArg,
3870 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003871 severity,
3872 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00003873 xmlFree(str);
3874 }
3875}
3876
3877static void
3878xmlTextReaderError(void *ctxt, const char *msg, ...) {
3879 va_list ap;
3880
3881 va_start(ap,msg);
3882 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003883 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00003884 xmlTextReaderBuildMessage(msg,ap));
3885 va_end(ap);
3886
3887}
3888
3889static void
3890xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
3891 va_list ap;
3892
3893 va_start(ap,msg);
3894 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003895 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00003896 xmlTextReaderBuildMessage(msg,ap));
3897 va_end(ap);
3898}
3899
3900static void
3901xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
3902 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003903 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003904
Daniel Veillard417be3a2003-01-20 21:26:34 +00003905 if ((len > 1) && (msg[len - 2] != ':')) {
3906 /*
3907 * some callbacks only report locator information:
3908 * skip them (mimicking behaviour in error.c)
3909 */
3910 va_start(ap,msg);
3911 xmlTextReaderGenericError(ctxt,
3912 XML_PARSER_SEVERITY_VALIDITY_ERROR,
3913 xmlTextReaderBuildMessage(msg,ap));
3914 va_end(ap);
3915 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003916}
3917
3918static void
3919xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
3920 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003921 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003922
Daniel Veillard417be3a2003-01-20 21:26:34 +00003923 if ((len != 0) && (msg[len - 1] != ':')) {
3924 /*
3925 * some callbacks only report locator information:
3926 * skip them (mimicking behaviour in error.c)
3927 */
3928 va_start(ap,msg);
3929 xmlTextReaderGenericError(ctxt,
3930 XML_PARSER_SEVERITY_VALIDITY_WARNING,
3931 xmlTextReaderBuildMessage(msg,ap));
3932 va_end(ap);
3933 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003934}
3935
3936/**
3937 * xmlTextReaderSetErrorHandler:
3938 * @reader: the xmlTextReaderPtr used
3939 * @f: the callback function to call on error and warnings
3940 * @arg: a user argument to pass to the callback function
3941 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00003942 * Register a callback function that will be called on error and warnings.
3943 *
Daniel Veillard26f70262003-01-16 22:45:08 +00003944 * If @f is NULL, the default error and warning handlers are restored.
3945 */
3946void
3947xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
3948 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003949 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003950 if (f != NULL) {
3951 reader->ctxt->sax->error = xmlTextReaderError;
3952 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
3953 reader->ctxt->sax->warning = xmlTextReaderWarning;
3954 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
3955 reader->errorFunc = f;
3956 reader->errorFuncArg = arg;
3957 }
3958 else {
3959 /* restore defaults */
3960 reader->ctxt->sax->error = xmlParserError;
3961 reader->ctxt->vctxt.error = xmlParserValidityError;
3962 reader->ctxt->sax->warning = xmlParserWarning;
3963 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
3964 reader->errorFunc = NULL;
3965 reader->errorFuncArg = NULL;
3966 }
3967}
3968
Daniel Veillard417be3a2003-01-20 21:26:34 +00003969/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00003970 * xmlTextReaderIsValid:
3971 * @reader: the xmlTextReaderPtr used
3972 *
3973 * Retrieve the validity status from the parser context
3974 *
3975 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
3976 */
3977int
3978xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00003979 if (reader == NULL) return(-1);
3980#ifdef LIBXML_SCHEMAS_ENABLED
3981 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
3982 return(reader->rngValidErrors == 0);
3983#endif
Daniel Veillard16ed5972003-11-20 18:22:31 +00003984 if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
Daniel Veillardf4e55762003-04-15 23:32:22 +00003985 return(reader->ctxt->valid);
3986 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00003987}
3988
3989/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00003990 * xmlTextReaderGetErrorHandler:
3991 * @reader: the xmlTextReaderPtr used
3992 * @f: the callback function or NULL is no callback has been registered
3993 * @arg: a user argument
3994 *
3995 * Retrieve the error callback function and user argument.
3996 */
Daniel Veillard26f70262003-01-16 22:45:08 +00003997void
3998xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
3999 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00004000 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00004001 *f = reader->errorFunc;
4002 *arg = reader->errorFuncArg;
4003}
4004
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004005
4006/************************************************************************
4007 * *
4008 * New set (2.6.0) of simpler and more flexible APIs *
4009 * *
4010 ************************************************************************/
4011
4012/**
4013 * xmlTextReaderSetup:
4014 * @reader: an XML reader
4015 * @URL: the base URL to use for the document
4016 * @encoding: the document encoding, or NULL
4017 * @options: a combination of xmlParserOption(s)
4018 * @reuse: keep the context for reuse
4019 *
4020 * Setup an XML reader with new options
4021 *
4022 * Returns 0 in case of success and -1 in case of error.
4023 */
4024static int
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004025xmlTextReaderSetup(xmlTextReaderPtr reader,
4026 xmlParserInputBufferPtr input, const char *URL,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004027 const char *encoding, int options)
4028{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004029 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004030 return (-1);
4031
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004032 reader->doc = NULL;
4033 reader->entNr = 0;
Daniel Veillardc36965d2003-12-02 10:28:48 +00004034 reader->validate = XML_TEXTREADER_NOT_VALIDATE;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004035 if ((input != NULL) && (reader->input != NULL) &&
4036 (reader->allocs & XML_TEXTREADER_INPUT)) {
4037 xmlFreeParserInputBuffer(reader->input);
4038 reader->input = NULL;
4039 reader->allocs -= XML_TEXTREADER_INPUT;
4040 }
4041 if (input != NULL) {
4042 reader->input = input;
4043 reader->allocs |= XML_TEXTREADER_INPUT;
4044 }
4045 if (reader->buffer == NULL)
4046 reader->buffer = xmlBufferCreateSize(100);
4047 if (reader->buffer == NULL) {
4048 xmlGenericError(xmlGenericErrorContext,
4049 "xmlTextReaderSetup : malloc failed\n");
4050 return (-1);
4051 }
4052 if (reader->sax == NULL)
4053 reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
4054 if (reader->sax == NULL) {
4055 xmlGenericError(xmlGenericErrorContext,
4056 "xmlTextReaderSetup : malloc failed\n");
4057 return (-1);
4058 }
4059 xmlSAXVersion(reader->sax, 2);
4060 reader->startElement = reader->sax->startElement;
4061 reader->sax->startElement = xmlTextReaderStartElement;
4062 reader->endElement = reader->sax->endElement;
4063 reader->sax->endElement = xmlTextReaderEndElement;
4064#ifdef LIBXML_SAX1_ENABLED
4065 if (reader->sax->initialized == XML_SAX2_MAGIC) {
4066#endif /* LIBXML_SAX1_ENABLED */
4067 reader->startElementNs = reader->sax->startElementNs;
4068 reader->sax->startElementNs = xmlTextReaderStartElementNs;
4069 reader->endElementNs = reader->sax->endElementNs;
4070 reader->sax->endElementNs = xmlTextReaderEndElementNs;
4071#ifdef LIBXML_SAX1_ENABLED
4072 } else {
4073 reader->startElementNs = NULL;
4074 reader->endElementNs = NULL;
4075 }
4076#endif /* LIBXML_SAX1_ENABLED */
4077 reader->characters = reader->sax->characters;
4078 reader->sax->characters = xmlTextReaderCharacters;
4079 reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
4080 reader->cdataBlock = reader->sax->cdataBlock;
4081 reader->sax->cdataBlock = xmlTextReaderCDataBlock;
4082
4083 reader->mode = XML_TEXTREADER_MODE_INITIAL;
4084 reader->node = NULL;
4085 reader->curnode = NULL;
4086 if (input != NULL) {
4087 if (reader->input->buffer->use < 4) {
4088 xmlParserInputBufferRead(input, 4);
4089 }
4090 if (reader->ctxt == NULL) {
4091 if (reader->input->buffer->use >= 4) {
4092 reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
4093 (const char *) reader->input->buffer->content, 4, URL);
4094 reader->base = 0;
4095 reader->cur = 4;
4096 } else {
4097 reader->ctxt =
4098 xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
4099 reader->base = 0;
4100 reader->cur = 0;
4101 }
4102 } else {
4103 xmlParserInputPtr inputStream;
4104 xmlParserInputBufferPtr buf;
4105 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
4106
4107 if (reader->ctxt->myDoc != NULL)
4108 xmlDictReference(reader->ctxt->myDoc->dict);
4109 xmlCtxtReset(reader->ctxt);
4110 buf = xmlAllocParserInputBuffer(enc);
4111 if (buf == NULL) return(-1);
4112 inputStream = xmlNewInputStream(reader->ctxt);
4113 if (inputStream == NULL) {
4114 xmlFreeParserInputBuffer(buf);
4115 return(-1);
4116 }
4117
4118 if (URL == NULL)
4119 inputStream->filename = NULL;
4120 else
4121 inputStream->filename = (char *)
4122 xmlCanonicPath((const xmlChar *) URL);
4123 inputStream->buf = buf;
4124 inputStream->base = inputStream->buf->buffer->content;
4125 inputStream->cur = inputStream->buf->buffer->content;
4126 inputStream->end =
4127 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
4128
4129 inputPush(reader->ctxt, inputStream);
4130 reader->cur = 0;
4131 }
4132 if (reader->ctxt == NULL) {
4133 xmlGenericError(xmlGenericErrorContext,
4134 "xmlTextReaderSetup : malloc failed\n");
4135 return (-1);
4136 }
4137 }
4138 if (reader->dict != NULL) {
4139 if (reader->ctxt->dict != NULL) {
4140 if (reader->dict != reader->ctxt->dict) {
4141 xmlDictFree(reader->dict);
4142 reader->dict = reader->ctxt->dict;
4143 }
4144 } else {
4145 reader->ctxt->dict = reader->dict;
4146 }
4147 } else {
4148 if (reader->ctxt->dict == NULL)
4149 reader->ctxt->dict = xmlDictCreate();
4150 reader->dict = reader->ctxt->dict;
4151 }
4152 reader->ctxt->_private = reader;
4153 reader->ctxt->linenumbers = 1;
4154 reader->ctxt->dictNames = 1;
4155 /*
4156 * use the parser dictionnary to allocate all elements and attributes names
4157 */
4158 reader->ctxt->docdict = 1;
4159
Daniel Veillard7899c5c2003-11-03 12:31:38 +00004160#ifdef LIBXML_XINCLUDE_ENABLED
4161 if (reader->xincctxt != NULL) {
4162 xmlXIncludeFreeContext(reader->xincctxt);
4163 reader->xincctxt = NULL;
4164 }
4165 if (options & XML_PARSE_XINCLUDE) {
4166 reader->xinclude = 1;
4167 reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
4168 options -= XML_PARSE_XINCLUDE;
4169 } else
4170 reader->xinclude = 0;
4171 reader->in_xinclude = 0;
4172#endif
Daniel Veillard1e906612003-12-05 14:57:46 +00004173#ifdef LIBXML_PATTERN_ENABLED
4174 if (reader->patternTab == NULL) {
4175 reader->patternNr = 0;
4176 reader->patternMax = 0;
4177 }
4178 while (reader->patternNr > 0) {
4179 reader->patternNr--;
4180 if (reader->patternTab[reader->patternNr] != NULL) {
4181 xmlFreePattern(reader->patternTab[reader->patternNr]);
4182 reader->patternTab[reader->patternNr] = NULL;
4183 }
4184 }
4185#endif
4186
Daniel Veillardc36965d2003-12-02 10:28:48 +00004187 if (options & XML_PARSE_DTDVALID)
4188 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
4189
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004190 xmlCtxtUseOptions(reader->ctxt, options);
4191 if (encoding != NULL) {
4192 xmlCharEncodingHandlerPtr hdlr;
4193
4194 hdlr = xmlFindCharEncodingHandler(encoding);
4195 if (hdlr != NULL)
4196 xmlSwitchToEncoding(reader->ctxt, hdlr);
4197 }
4198 if ((URL != NULL) && (reader->ctxt->input != NULL) &&
4199 (reader->ctxt->input->filename == NULL))
4200 reader->ctxt->input->filename = (char *)
4201 xmlStrdup((const xmlChar *) URL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004202
4203 reader->doc = NULL;
4204
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004205 return (0);
4206}
4207
4208/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004209 * xmlReaderWalker:
4210 * @doc: a preparsed document
4211 *
4212 * Create an xmltextReader for a preparsed document.
4213 *
4214 * Returns the new reader or NULL in case of error.
4215 */
4216xmlTextReaderPtr
4217xmlReaderWalker(xmlDocPtr doc)
4218{
4219 xmlTextReaderPtr ret;
4220
4221 if (doc == NULL)
4222 return(NULL);
4223
4224 ret = xmlMalloc(sizeof(xmlTextReader));
4225 if (ret == NULL) {
4226 xmlGenericError(xmlGenericErrorContext,
4227 "xmlNewTextReader : malloc failed\n");
4228 return(NULL);
4229 }
4230 memset(ret, 0, sizeof(xmlTextReader));
4231 ret->entNr = 0;
4232 ret->input = NULL;
4233 ret->mode = XML_TEXTREADER_MODE_INITIAL;
4234 ret->node = NULL;
4235 ret->curnode = NULL;
4236 ret->base = 0;
4237 ret->cur = 0;
4238 ret->allocs = XML_TEXTREADER_CTXT;
4239 ret->doc = doc;
4240 ret->state = XML_TEXTREADER_START;
4241 ret->dict = xmlDictCreate();
4242 return(ret);
4243}
4244
4245/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004246 * xmlReaderForDoc:
4247 * @cur: a pointer to a zero terminated string
4248 * @URL: the base URL to use for the document
4249 * @encoding: the document encoding, or NULL
4250 * @options: a combination of xmlParserOption(s)
4251 *
4252 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004253 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004254 *
4255 * Returns the new reader or NULL in case of error.
4256 */
4257xmlTextReaderPtr
4258xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
4259 int options)
4260{
4261 int len;
4262
4263 if (cur == NULL)
4264 return (NULL);
4265 len = xmlStrlen(cur);
4266
4267 return (xmlReaderForMemory
4268 ((const char *) cur, len, URL, encoding, options));
4269}
4270
4271/**
4272 * xmlReaderForFile:
4273 * @filename: a file or URL
4274 * @encoding: the document encoding, or NULL
4275 * @options: a combination of xmlParserOption(s)
4276 *
4277 * parse an XML file from the filesystem or the network.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004278 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004279 *
4280 * Returns the new reader or NULL in case of error.
4281 */
4282xmlTextReaderPtr
4283xmlReaderForFile(const char *filename, const char *encoding, int options)
4284{
4285 xmlTextReaderPtr reader;
4286
4287 reader = xmlNewTextReaderFilename(filename);
4288 if (reader == NULL)
4289 return (NULL);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004290 xmlTextReaderSetup(reader, NULL, NULL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004291 return (reader);
4292}
4293
4294/**
4295 * xmlReaderForMemory:
4296 * @buffer: a pointer to a char array
4297 * @size: the size of the array
4298 * @URL: the base URL to use for the document
4299 * @encoding: the document encoding, or NULL
4300 * @options: a combination of xmlParserOption(s)
4301 *
4302 * Create an xmltextReader for an XML in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004303 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004304 *
4305 * Returns the new reader or NULL in case of error.
4306 */
4307xmlTextReaderPtr
4308xmlReaderForMemory(const char *buffer, int size, const char *URL,
4309 const char *encoding, int options)
4310{
4311 xmlTextReaderPtr reader;
4312 xmlParserInputBufferPtr buf;
4313
4314 buf =
4315 xmlParserInputBufferCreateMem(buffer, size,
4316 XML_CHAR_ENCODING_NONE);
4317 if (buf == NULL) {
4318 return (NULL);
4319 }
4320 reader = xmlNewTextReader(buf, URL);
4321 if (reader == NULL) {
4322 xmlFreeParserInputBuffer(buf);
4323 return (NULL);
4324 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004325 reader->allocs |= XML_TEXTREADER_INPUT;
4326 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004327 return (reader);
4328}
4329
4330/**
4331 * xmlReaderForFd:
4332 * @fd: an open file descriptor
4333 * @URL: the base URL to use for the document
4334 * @encoding: the document encoding, or NULL
4335 * @options: a combination of xmlParserOption(s)
4336 *
4337 * Create an xmltextReader for an XML from a file descriptor.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004338 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004339 *
4340 * Returns the new reader or NULL in case of error.
4341 */
4342xmlTextReaderPtr
4343xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
4344{
4345 xmlTextReaderPtr reader;
4346 xmlParserInputBufferPtr input;
4347
4348 if (fd < 0)
4349 return (NULL);
4350
4351 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4352 if (input == NULL)
4353 return (NULL);
4354 reader = xmlNewTextReader(input, URL);
4355 if (reader == NULL) {
4356 xmlFreeParserInputBuffer(input);
4357 return (NULL);
4358 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004359 reader->allocs |= XML_TEXTREADER_INPUT;
4360 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004361 return (reader);
4362}
4363
4364/**
4365 * xmlReaderForIO:
4366 * @ioread: an I/O read function
4367 * @ioclose: an I/O close function
4368 * @ioctx: an I/O handler
4369 * @URL: the base URL to use for the document
4370 * @encoding: the document encoding, or NULL
4371 * @options: a combination of xmlParserOption(s)
4372 *
4373 * Create an xmltextReader for an XML document from I/O functions and source.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004374 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004375 *
4376 * Returns the new reader or NULL in case of error.
4377 */
4378xmlTextReaderPtr
4379xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
4380 void *ioctx, const char *URL, const char *encoding,
4381 int options)
4382{
4383 xmlTextReaderPtr reader;
4384 xmlParserInputBufferPtr input;
4385
4386 if (ioread == NULL)
4387 return (NULL);
4388
4389 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4390 XML_CHAR_ENCODING_NONE);
4391 if (input == NULL)
4392 return (NULL);
4393 reader = xmlNewTextReader(input, URL);
4394 if (reader == NULL) {
4395 xmlFreeParserInputBuffer(input);
4396 return (NULL);
4397 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004398 reader->allocs |= XML_TEXTREADER_INPUT;
4399 xmlTextReaderSetup(reader, NULL, URL, encoding, options);
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004400 return (reader);
4401}
4402
4403/**
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004404 * xmlReaderNewWalker:
4405 * @reader: an XML reader
4406 * @doc: a preparsed document
4407 *
4408 * Setup an xmltextReader to parse a preparsed XML document.
4409 * This reuses the existing @reader xmlTextReader.
4410 *
4411 * Returns 0 in case of success and -1 in case of error
4412 */
4413int
4414xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
4415{
4416 if (doc == NULL)
4417 return (-1);
4418 if (reader == NULL)
4419 return (-1);
4420
4421 if (reader->ctxt != NULL) {
4422 xmlCtxtReset(reader->ctxt);
4423 }
4424
4425 reader->entNr = 0;
4426 reader->input = NULL;
4427 reader->mode = XML_TEXTREADER_MODE_INITIAL;
4428 reader->node = NULL;
4429 reader->curnode = NULL;
4430 reader->base = 0;
4431 reader->cur = 0;
4432 reader->allocs = XML_TEXTREADER_CTXT;
4433 reader->doc = doc;
4434 reader->state = XML_TEXTREADER_START;
4435 if (reader->dict == NULL) {
4436 if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
4437 reader->dict = reader->ctxt->dict;
4438 else
4439 reader->dict = xmlDictCreate();
4440 }
4441 return(0);
4442}
4443
4444/**
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004445 * xmlReaderNewDoc:
4446 * @reader: an XML reader
4447 * @cur: a pointer to a zero terminated string
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 in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004453 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004454 * This reuses the existing @reader xmlTextReader.
4455 *
4456 * Returns 0 in case of success and -1 in case of error
4457 */
4458int
4459xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
4460 const char *URL, const char *encoding, int options)
4461{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004462
4463 int len;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004464
4465 if (cur == NULL)
4466 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004467 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004468 return (-1);
4469
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004470 len = xmlStrlen(cur);
4471 return (xmlReaderNewMemory(reader, (const char *)cur, len,
4472 URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004473}
4474
4475/**
4476 * xmlReaderNewFile:
4477 * @reader: an XML reader
4478 * @filename: a file or URL
4479 * @encoding: the document encoding, or NULL
4480 * @options: a combination of xmlParserOption(s)
4481 *
4482 * parse an XML file from the filesystem or the network.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004483 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004484 * This reuses the existing @reader xmlTextReader.
4485 *
4486 * Returns 0 in case of success and -1 in case of error
4487 */
4488int
4489xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
4490 const char *encoding, int options)
4491{
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004492 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004493
4494 if (filename == NULL)
4495 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004496 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004497 return (-1);
4498
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004499 input =
4500 xmlParserInputBufferCreateFilename(filename,
4501 XML_CHAR_ENCODING_NONE);
4502 if (input == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004503 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004504 return (xmlTextReaderSetup(reader, input, filename, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004505}
4506
4507/**
4508 * xmlReaderNewMemory:
4509 * @reader: an XML reader
4510 * @buffer: a pointer to a char array
4511 * @size: the size of the array
4512 * @URL: the base URL to use for the document
4513 * @encoding: the document encoding, or NULL
4514 * @options: a combination of xmlParserOption(s)
4515 *
4516 * Setup an xmltextReader to parse an XML in-memory document.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004517 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004518 * This reuses the existing @reader xmlTextReader.
4519 *
4520 * Returns 0 in case of success and -1 in case of error
4521 */
4522int
4523xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
4524 const char *URL, const char *encoding, int options)
4525{
4526 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004527
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004528 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004529 return (-1);
4530 if (buffer == NULL)
4531 return (-1);
4532
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004533 input = xmlParserInputBufferCreateMem(buffer, size,
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004534 XML_CHAR_ENCODING_NONE);
4535 if (input == NULL) {
4536 return (-1);
4537 }
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004538 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004539}
4540
4541/**
4542 * xmlReaderNewFd:
4543 * @reader: an XML reader
4544 * @fd: an open file descriptor
4545 * @URL: the base URL to use for the document
4546 * @encoding: the document encoding, or NULL
4547 * @options: a combination of xmlParserOption(s)
4548 *
4549 * Setup an xmltextReader to parse an XML from a file descriptor.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004550 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004551 * This reuses the existing @reader xmlTextReader.
4552 *
4553 * Returns 0 in case of success and -1 in case of error
4554 */
4555int
4556xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
4557 const char *URL, const char *encoding, int options)
4558{
4559 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004560
4561 if (fd < 0)
4562 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004563 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004564 return (-1);
4565
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004566 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
4567 if (input == NULL)
4568 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004569 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004570}
4571
4572/**
4573 * xmlReaderNewIO:
4574 * @reader: an XML reader
4575 * @ioread: an I/O read function
4576 * @ioclose: an I/O close function
4577 * @ioctx: an I/O handler
4578 * @URL: the base URL to use for the document
4579 * @encoding: the document encoding, or NULL
4580 * @options: a combination of xmlParserOption(s)
4581 *
4582 * Setup an xmltextReader to parse an XML document from I/O functions
4583 * and source.
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004584 * The parsing flags @options are a combination of xmlParserOption(s).
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004585 * This reuses the existing @reader xmlTextReader.
4586 *
4587 * Returns 0 in case of success and -1 in case of error
4588 */
4589int
4590xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
4591 xmlInputCloseCallback ioclose, void *ioctx,
4592 const char *URL, const char *encoding, int options)
4593{
4594 xmlParserInputBufferPtr input;
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004595
4596 if (ioread == NULL)
4597 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004598 if (reader == NULL)
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004599 return (-1);
4600
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004601 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
4602 XML_CHAR_ENCODING_NONE);
4603 if (input == NULL)
4604 return (-1);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00004605 return (xmlTextReaderSetup(reader, input, URL, encoding, options));
Daniel Veillardfc8dc352003-10-18 09:07:46 +00004606}
Daniel Veillard26f70262003-01-16 22:45:08 +00004607/************************************************************************
4608 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004609 * Utilities *
4610 * *
4611 ************************************************************************/
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004612#ifdef NOT_USED_YET
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004613/**
4614 * xmlBase64Decode:
4615 * @in: the input buffer
4616 * @inlen: the size of the input (in), the size read from it (out)
4617 * @to: the output buffer
4618 * @tolen: the size of the output (in), the size written to (out)
4619 *
4620 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00004621 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004622 *
4623 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
4624 * 2 if there wasn't enough space on the output or -1 in case of error.
4625 */
4626static int
4627xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
4628 unsigned char *to, unsigned long *tolen) {
4629 unsigned long incur; /* current index in in[] */
4630 unsigned long inblk; /* last block index in in[] */
4631 unsigned long outcur; /* current index in out[] */
4632 unsigned long inmax; /* size of in[] */
4633 unsigned long outmax; /* size of out[] */
4634 unsigned char cur; /* the current value read from in[] */
Daniel Veillardc127adc2003-07-23 15:07:08 +00004635 unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00004636 int nbintmp; /* number of byte in intmp[] */
4637 int is_ignore; /* cur should be ignored */
4638 int is_end = 0; /* the end of the base64 was found */
4639 int retval = 1;
4640 int i;
4641
4642 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
4643 return(-1);
4644
4645 incur = 0;
4646 inblk = 0;
4647 outcur = 0;
4648 inmax = *inlen;
4649 outmax = *tolen;
4650 nbintmp = 0;
4651
4652 while (1) {
4653 if (incur >= inmax)
4654 break;
4655 cur = in[incur++];
4656 is_ignore = 0;
4657 if ((cur >= 'A') && (cur <= 'Z'))
4658 cur = cur - 'A';
4659 else if ((cur >= 'a') && (cur <= 'z'))
4660 cur = cur - 'a' + 26;
4661 else if ((cur >= '0') && (cur <= '9'))
4662 cur = cur - '0' + 52;
4663 else if (cur == '+')
4664 cur = 62;
4665 else if (cur == '/')
4666 cur = 63;
4667 else if (cur == '.')
4668 cur = 0;
4669 else if (cur == '=') /*no op , end of the base64 stream */
4670 is_end = 1;
4671 else {
4672 is_ignore = 1;
4673 if (nbintmp == 0)
4674 inblk = incur;
4675 }
4676
4677 if (!is_ignore) {
4678 int nbouttmp = 3;
4679 int is_break = 0;
4680
4681 if (is_end) {
4682 if (nbintmp == 0)
4683 break;
4684 if ((nbintmp == 1) || (nbintmp == 2))
4685 nbouttmp = 1;
4686 else
4687 nbouttmp = 2;
4688 nbintmp = 3;
4689 is_break = 1;
4690 }
4691 intmp[nbintmp++] = cur;
4692 /*
4693 * if intmp is full, push the 4byte sequence as a 3 byte
4694 * sequence out
4695 */
4696 if (nbintmp == 4) {
4697 nbintmp = 0;
4698 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
4699 outtmp[1] =
4700 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
4701 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
4702 if (outcur + 3 >= outmax) {
4703 retval = 2;
4704 break;
4705 }
4706
4707 for (i = 0; i < nbouttmp; i++)
4708 to[outcur++] = outtmp[i];
4709 inblk = incur;
4710 }
4711
4712 if (is_break) {
4713 retval = 0;
4714 break;
4715 }
4716 }
4717 }
4718
4719 *tolen = outcur;
4720 *inlen = inblk;
4721 return (retval);
4722}
4723
4724/*
4725 * Test routine for the xmlBase64Decode function
4726 */
4727#if 0
4728int main(int argc, char **argv) {
4729 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
4730 char output[100];
4731 char output2[100];
4732 char output3[100];
4733 unsigned long inlen = strlen(input);
4734 unsigned long outlen = 100;
4735 int ret;
4736 unsigned long cons, tmp, tmp2, prod;
4737
4738 /*
4739 * Direct
4740 */
4741 ret = xmlBase64Decode(input, &inlen, output, &outlen);
4742
4743 output[outlen] = 0;
4744 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
4745
4746 /*
4747 * output chunking
4748 */
4749 cons = 0;
4750 prod = 0;
4751 while (cons < inlen) {
4752 tmp = 5;
4753 tmp2 = inlen - cons;
4754
4755 printf("%ld %ld\n", cons, prod);
4756 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
4757 cons += tmp2;
4758 prod += tmp;
4759 printf("%ld %ld\n", cons, prod);
4760 }
4761 output2[outlen] = 0;
4762 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
4763
4764 /*
4765 * input chunking
4766 */
4767 cons = 0;
4768 prod = 0;
4769 while (cons < inlen) {
4770 tmp = 100 - prod;
4771 tmp2 = inlen - cons;
4772 if (tmp2 > 5)
4773 tmp2 = 5;
4774
4775 printf("%ld %ld\n", cons, prod);
4776 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
4777 cons += tmp2;
4778 prod += tmp;
4779 printf("%ld %ld\n", cons, prod);
4780 }
4781 output3[outlen] = 0;
4782 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
4783 return(0);
4784
4785}
4786#endif
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00004787#endif /* NOT_USED_YET */
Daniel Veillard81273902003-09-30 00:43:48 +00004788#endif /* LIBXML_READER_ENABLED */