blob: 30451b438046248730ac9eac3a2d619c505132d7 [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 * - validation against a provided DTD
18 * - XML Schemas validation
Daniel Veillard7704fb12003-01-03 16:19:51 +000019 * - setting(s) for NoBlanks
20 * - performances and tuning ...
21 */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000022#define IN_LIBXML
23#include "libxml.h"
24
25#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 Veillardf4e55762003-04-15 23:32:22 +000038#include <libxml/relaxng.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000039
40/* #define DEBUG_CALLBACKS */
41/* #define DEBUG_READER */
42
43/**
44 * TODO:
45 *
46 * macro to flag unimplemented blocks
47 */
48#define TODO \
49 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
51 __FILE__, __LINE__);
52
53#ifdef DEBUG_READER
54#define DUMP_READER xmlTextReaderDebug(reader);
55#else
56#define DUMP_READER
57#endif
58
Daniel Veillarda880b122003-04-21 21:36:41 +000059#define CHUNK_SIZE 512
Daniel Veillarde1ca5032002-12-09 14:13:43 +000060/************************************************************************
61 * *
62 * The parser: maps the Text Reader API on top of the existing *
63 * parsing routines building a tree *
64 * *
65 ************************************************************************/
66
67#define XML_TEXTREADER_INPUT 1
68#define XML_TEXTREADER_CTXT 2
69
70typedef enum {
Daniel Veillard67df8092002-12-16 22:04:11 +000071 XML_TEXTREADER_MODE_INITIAL = 0,
72 XML_TEXTREADER_MODE_INTERACTIVE = 1,
73 XML_TEXTREADER_MODE_ERROR = 2,
74 XML_TEXTREADER_MODE_EOF =3,
75 XML_TEXTREADER_MODE_CLOSED = 4,
76 XML_TEXTREADER_MODE_READING = 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000077} xmlTextReaderMode;
78
79typedef enum {
80 XML_TEXTREADER_NONE = -1,
81 XML_TEXTREADER_START= 0,
82 XML_TEXTREADER_ELEMENT= 1,
83 XML_TEXTREADER_END= 2,
84 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000085 XML_TEXTREADER_BACKTRACK= 4,
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000086 XML_TEXTREADER_DONE= 5,
87 XML_TEXTREADER_ERROR= 6
Daniel Veillarde1ca5032002-12-09 14:13:43 +000088} xmlTextReaderState;
89
Daniel Veillardf4e55762003-04-15 23:32:22 +000090typedef enum {
91 XML_TEXTREADER_NOT_VALIDATE = 0,
92 XML_TEXTREADER_VALIDATE_DTD = 1,
93 XML_TEXTREADER_VALIDATE_RNG = 2
94} xmlTextReaderValidate;
95
Daniel Veillarde1ca5032002-12-09 14:13:43 +000096struct _xmlTextReader {
97 int mode; /* the parsing mode */
Daniel Veillardf4e55762003-04-15 23:32:22 +000098 xmlTextReaderValidate validate;/* is there any validation */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000099 int allocs; /* what structure were deallocated */
100 xmlTextReaderState state;
101 xmlParserCtxtPtr ctxt; /* the parser context */
102 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
103 xmlParserInputBufferPtr input; /* the input */
104 startElementSAXFunc startElement;/* initial SAX callbacks */
105 endElementSAXFunc endElement; /* idem */
Daniel Veillard07cb8222003-09-10 10:51:05 +0000106 startElementNsSAX2Func startElementNs;/* idem */
107 endElementNsSAX2Func endElementNs; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000108 charactersSAXFunc characters;
109 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000110 unsigned int base; /* base of the segment in the input */
111 unsigned int cur; /* current position in the input */
112 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000113 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000114 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000115 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000116
117 /* entity stack when traversing entities content */
118 xmlNodePtr ent; /* Current Entity Ref Node */
119 int entNr; /* Depth of the entities stack */
120 int entMax; /* Max depth of the entities stack */
121 xmlNodePtr *entTab; /* array of entities */
Daniel Veillard26f70262003-01-16 22:45:08 +0000122
123 /* error handling */
124 xmlTextReaderErrorFunc errorFunc; /* callback function */
125 void *errorFuncArg; /* callback function user argument */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000126
127#ifdef LIBXML_SCHEMAS_ENABLED
128 /* Handling of RelaxNG validation */
129 xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
130 xmlRelaxNGValidCtxtPtr rngValidCtxt; /* The Relax NG validation context */
131 int rngValidErrors; /* The number of errors detected */
132 xmlNodePtr rngFullNode; /* the node if RNG not progressive */
133#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000134};
135
Daniel Veillard067bae52003-01-05 01:27:54 +0000136static const char *xmlTextReaderIsEmpty = "This element is empty";
137
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000138/************************************************************************
139 * *
140 * Our own version of the freeing routines as we recycle nodes *
141 * *
142 ************************************************************************/
143
144static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
145static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
146
147/**
148 * xmlTextReaderFreeEntityWrapper:
149 * @entity: An entity
150 * @name: its name
151 *
152 * Deallocate the memory used by an entities in the hash table.
153 */
154static void
155xmlTextReaderFreeEntityWrapper(xmlEntityPtr entity,
156 const xmlChar *name ATTRIBUTE_UNUSED) {
157 if (entity == NULL) return;
158
159 if ((entity->children) && (entity->owner == 1) &&
160 (entity == (xmlEntityPtr) entity->children->parent)) {
161 xmlDocPtr doc;
162 xmlTextReaderPtr reader = NULL;
163 doc = entity->doc;
164 if (doc != NULL)
165 reader = doc->_private;
166 xmlTextReaderFreeNodeList(reader, entity->children);
167 }
168 if (entity->name != NULL)
169 xmlFree((char *) entity->name);
170 if (entity->ExternalID != NULL)
171 xmlFree((char *) entity->ExternalID);
172 if (entity->SystemID != NULL)
173 xmlFree((char *) entity->SystemID);
174 if (entity->URI != NULL)
175 xmlFree((char *) entity->URI);
176 if (entity->content != NULL)
177 xmlFree((char *) entity->content);
178 if (entity->orig != NULL)
179 xmlFree((char *) entity->orig);
180 xmlFree(entity);
181}
182
183/**
184 * xmlTextReaderFreeEntitiesTable:
185 * @table: An entity table
186 *
187 * Deallocate the memory used by an entities hash table.
188 */
189static void
190xmlTextReaderFreeEntitiesTable(xmlEntitiesTablePtr table) {
191 xmlHashFree(table, (xmlHashDeallocator) xmlTextReaderFreeEntityWrapper);
192}
193
194/**
195 * xmlTextReaderFreeDtd:
196 * @cur: the DTD structure to free up
197 *
198 * Free a DTD structure.
199 */
200static void
201xmlTextReaderFreeDtd(xmlTextReaderPtr reader, xmlDtdPtr cur) {
202 if (cur == NULL) return;
203
204 if (cur->children != NULL) {
205 xmlNodePtr next, c = cur->children;
206
207 /*
208 * Cleanup all the DTD comments they are not in the DTD
209 * indexes.
210 */
211 while (c != NULL) {
212 next = c->next;
213 if ((c->type == XML_COMMENT_NODE) || (c->type == XML_PI_NODE)) {
214 xmlUnlinkNode(c);
215 xmlTextReaderFreeNode(reader, c);
216 }
217 c = next;
218 }
219 }
220
221 if (cur->name != NULL) xmlFree((char *) cur->name);
222 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
223 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
224 /* TODO !!! */
225 if (cur->notations != NULL)
226 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
227
228 if (cur->elements != NULL)
229 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
230 if (cur->attributes != NULL)
231 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
232 if (cur->pentities != NULL)
233 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
234
235 if (cur->entities != NULL)
236 xmlTextReaderFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
237
238 xmlFree(cur);
239}
240
241/**
242 * xmlTextReaderFreeProp:
243 * @reader: the xmlTextReaderPtr used
244 * @cur: the node
245 *
246 * Free a node.
247 */
248static void
249xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
250 if (cur == NULL) return;
251
252 /* Check for ID removal -> leading to invalid references ! */
253 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
254 ((cur->parent->doc->intSubset != NULL) ||
255 (cur->parent->doc->extSubset != NULL))) {
256 if (xmlIsID(cur->parent->doc, cur->parent, cur))
257 xmlRemoveID(cur->parent->doc, cur);
258 }
259 if (cur->children != NULL)
260 xmlTextReaderFreeNodeList(reader, cur->children);
261
262 if ((reader != NULL) && (reader->ctxt != NULL) &&
263 (xmlDictOwns(reader->ctxt->dict, cur->name) != 1) &&
264 (cur->name != NULL))
265 xmlFree((xmlChar *)cur->name);
Daniel Veillard19895052003-09-17 13:59:32 +0000266 if ((reader != NULL) && (reader->ctxt != NULL) &&
267 (reader->ctxt->freeAttrsNr < 100)) {
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000268 cur->next = reader->ctxt->freeAttrs;
269 reader->ctxt->freeAttrs = cur;
Daniel Veillard19895052003-09-17 13:59:32 +0000270 reader->ctxt->freeAttrsNr++;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000271 } else {
272 xmlFree(cur);
273 }
274}
275
276/**
277 * xmlTextReaderFreePropList:
278 * @reader: the xmlTextReaderPtr used
279 * @cur: the first property in the list
280 *
281 * Free a property and all its siblings, all the children are freed too.
282 */
283static void
284xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
285 xmlAttrPtr next;
286 if (cur == NULL) return;
287 while (cur != NULL) {
288 next = cur->next;
289 xmlTextReaderFreeProp(reader, cur);
290 cur = next;
291 }
292}
293
294/**
295 * xmlTextReaderFreeNodeList:
296 * @reader: the xmlTextReaderPtr used
297 * @cur: the first node in the list
298 *
299 * Free a node and all its siblings, this is a recursive behaviour, all
300 * the children are freed too.
301 */
302static void
303xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
304 xmlNodePtr next;
305 if (cur == NULL) return;
306 if (cur->type == XML_NAMESPACE_DECL) {
307 xmlFreeNsList((xmlNsPtr) cur);
308 return;
309 }
310 if ((cur->type == XML_DOCUMENT_NODE) ||
311 (cur->type == XML_HTML_DOCUMENT_NODE)) {
312 xmlFreeDoc((xmlDocPtr) cur);
313 return;
314 }
315 while (cur != NULL) {
316 next = cur->next;
317 /* unroll to speed up freeing the document */
318 if (cur->type != XML_DTD_NODE) {
319
320 if ((cur->children != NULL) &&
321 (cur->type != XML_ENTITY_REF_NODE))
322 xmlTextReaderFreeNodeList(reader, cur->children);
323 if (((cur->type == XML_ELEMENT_NODE) ||
324 (cur->type == XML_XINCLUDE_START) ||
325 (cur->type == XML_XINCLUDE_END)) &&
326 (cur->properties != NULL))
327 xmlTextReaderFreePropList(reader, cur->properties);
328 if ((cur->type != XML_ELEMENT_NODE) &&
329 (cur->type != XML_XINCLUDE_START) &&
330 (cur->type != XML_XINCLUDE_END) &&
331 (cur->type != XML_ENTITY_REF_NODE)) {
332 if (cur->content != NULL) xmlFree(cur->content);
333 }
334 if (((cur->type == XML_ELEMENT_NODE) ||
335 (cur->type == XML_XINCLUDE_START) ||
336 (cur->type == XML_XINCLUDE_END)) &&
337 (cur->nsDef != NULL))
338 xmlFreeNsList(cur->nsDef);
339
340 /*
341 * we don't free element names here they are interned now
342 */
343 if (cur->type == XML_ELEMENT_NODE) {
344 if ((reader != NULL) && (reader->ctxt != NULL) &&
345 (xmlDictOwns(reader->ctxt->dict, cur->name) != 1) &&
346 (cur->name != NULL))
347 xmlFree((xmlChar *)cur->name);
348 } else if ((cur->type != XML_TEXT_NODE) &&
349 (cur->type != XML_COMMENT_NODE) &&
350 (cur->name != NULL))
351 xmlFree((xmlChar *)cur->name);
Daniel Veillard19895052003-09-17 13:59:32 +0000352 if (((cur->type == XML_ELEMENT_NODE) ||
353 (cur->type == XML_TEXT_NODE)) &&
354 (reader != NULL) && (reader->ctxt != NULL) &&
355 (reader->ctxt->freeElemsNr < 100)) {
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000356 cur->next = reader->ctxt->freeElems;
357 reader->ctxt->freeElems = cur;
Daniel Veillard19895052003-09-17 13:59:32 +0000358 reader->ctxt->freeElemsNr++;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000359 } else {
360 xmlFree(cur);
361 }
362 }
363 cur = next;
364 }
365}
366
367/**
368 * xmlTextReaderFreeNode:
369 * @reader: the xmlTextReaderPtr used
370 * @cur: the node
371 *
372 * Free a node, this is a recursive behaviour, all the children are freed too.
373 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
374 */
375static void
376xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
377 if (cur->type == XML_DTD_NODE) {
378 xmlTextReaderFreeDtd(reader, (xmlDtdPtr) cur);
379 return;
380 }
381 if (cur->type == XML_NAMESPACE_DECL) {
382 xmlFreeNs((xmlNsPtr) cur);
383 return;
384 }
385 if (cur->type == XML_ATTRIBUTE_NODE) {
386 xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
387 return;
388 }
389
390 if ((cur->children != NULL) &&
391 (cur->type != XML_ENTITY_REF_NODE))
392 xmlTextReaderFreeNodeList(reader, cur->children);
393 if (((cur->type == XML_ELEMENT_NODE) ||
394 (cur->type == XML_XINCLUDE_START) ||
395 (cur->type == XML_XINCLUDE_END)) &&
396 (cur->properties != NULL))
397 xmlTextReaderFreePropList(reader, cur->properties);
398 if ((cur->type != XML_ELEMENT_NODE) &&
399 (cur->type != XML_XINCLUDE_START) &&
400 (cur->type != XML_XINCLUDE_END) &&
401 (cur->type != XML_ENTITY_REF_NODE)) {
402 if (cur->content != NULL) xmlFree(cur->content);
403 }
404 if (((cur->type == XML_ELEMENT_NODE) ||
405 (cur->type == XML_XINCLUDE_START) ||
406 (cur->type == XML_XINCLUDE_END)) &&
407 (cur->nsDef != NULL))
408 xmlFreeNsList(cur->nsDef);
409
410 /*
411 * we don't free names here they are interned now
412 */
413 if (cur->type == XML_ELEMENT_NODE) {
414 if ((reader != NULL) && (reader->ctxt != NULL) &&
415 (xmlDictOwns(reader->ctxt->dict, cur->name) != 1) &&
416 (cur->name != NULL))
417 xmlFree((xmlChar *)cur->name);
418 } else if ((cur->type != XML_TEXT_NODE) &&
419 (cur->type != XML_COMMENT_NODE) &&
420 (cur->name != NULL))
421 xmlFree((xmlChar *)cur->name);
Daniel Veillard19895052003-09-17 13:59:32 +0000422 if (((cur->type == XML_ELEMENT_NODE) ||
423 (cur->type == XML_TEXT_NODE)) &&
424 (reader != NULL) && (reader->ctxt != NULL) &&
425 (reader->ctxt->freeElemsNr < 100)) {
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000426 cur->next = reader->ctxt->freeElems;
427 reader->ctxt->freeElems = cur;
Daniel Veillard19895052003-09-17 13:59:32 +0000428 reader->ctxt->freeElemsNr++;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000429 } else {
430 xmlFree(cur);
431 }
432
433}
434
435/**
436 * xmlTextReaderFreeDoc:
437 * @reader: the xmlTextReaderPtr used
438 * @cur: pointer to the document
439 *
440 * Free up all the structures used by a document, tree included.
441 */
442static void
443xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
444 xmlDtdPtr extSubset, intSubset;
445
446 if (cur == NULL) return;
447
448 /*
449 * Do this before freeing the children list to avoid ID lookups
450 */
451 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
452 cur->ids = NULL;
453 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
454 cur->refs = NULL;
455 extSubset = cur->extSubset;
456 intSubset = cur->intSubset;
457 if (intSubset == extSubset)
458 extSubset = NULL;
459 if (extSubset != NULL) {
460 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
461 cur->extSubset = NULL;
462 xmlTextReaderFreeDtd(reader, extSubset);
463 }
464 if (intSubset != NULL) {
465 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
466 cur->intSubset = NULL;
467 xmlTextReaderFreeDtd(reader, intSubset);
468 }
469
470 if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
471
472 if (cur->version != NULL) xmlFree((char *) cur->version);
473 if (cur->name != NULL) xmlFree((char *) cur->name);
474 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
475 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
476 if (cur->URL != NULL) xmlFree((char *) cur->URL);
477 xmlFree(cur);
478}
479
480/************************************************************************
481 * *
482 * The reader core parser *
483 * *
484 ************************************************************************/
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000485#ifdef DEBUG_READER
486static void
487xmlTextReaderDebug(xmlTextReaderPtr reader) {
488 if ((reader == NULL) || (reader->ctxt == NULL)) {
489 fprintf(stderr, "xmlTextReader NULL\n");
490 return;
491 }
492 fprintf(stderr, "xmlTextReader: state %d depth %d ",
493 reader->state, reader->depth);
494 if (reader->node == NULL) {
495 fprintf(stderr, "node = NULL\n");
496 } else {
497 fprintf(stderr, "node %s\n", reader->node->name);
498 }
499 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
500 reader->base, reader->cur, reader->ctxt->nodeNr);
501 if (reader->input->buffer == NULL) {
502 fprintf(stderr, "buffer is NULL\n");
503 } else {
504#ifdef LIBXML_DEBUG_ENABLED
505 xmlDebugDumpString(stderr,
506 &reader->input->buffer->content[reader->cur]);
507#endif
508 fprintf(stderr, "\n");
509 }
510}
511#endif
512
513/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000514 * xmlTextReaderEntPush:
515 * @reader: the xmlTextReaderPtr used
516 * @value: the entity reference node
517 *
518 * Pushes a new entity reference node on top of the entities stack
519 *
520 * Returns 0 in case of error, the index in the stack otherwise
521 */
522static int
523xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
524{
525 if (reader->entMax <= 0) {
526 reader->entMax = 10;
527 reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
528 sizeof(reader->entTab[0]));
529 if (reader->entTab == NULL) {
530 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
531 return (0);
532 }
533 }
534 if (reader->entNr >= reader->entMax) {
535 reader->entMax *= 2;
536 reader->entTab =
537 (xmlNodePtr *) xmlRealloc(reader->entTab,
538 reader->entMax *
539 sizeof(reader->entTab[0]));
540 if (reader->entTab == NULL) {
541 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
542 return (0);
543 }
544 }
545 reader->entTab[reader->entNr] = value;
546 reader->ent = value;
547 return (reader->entNr++);
548}
549
550/**
551 * xmlTextReaderEntPop:
552 * @reader: the xmlTextReaderPtr used
553 *
554 * Pops the top element entity from the entities stack
555 *
556 * Returns the entity just removed
557 */
558static xmlNodePtr
559xmlTextReaderEntPop(xmlTextReaderPtr reader)
560{
561 xmlNodePtr ret;
562
563 if (reader->entNr <= 0)
564 return (0);
565 reader->entNr--;
566 if (reader->entNr > 0)
567 reader->ent = reader->entTab[reader->entNr - 1];
568 else
569 reader->ent = NULL;
570 ret = reader->entTab[reader->entNr];
571 reader->entTab[reader->entNr] = 0;
572 return (ret);
573}
574
575/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000576 * xmlTextReaderStartElement:
577 * @ctx: the user data (XML parser context)
578 * @fullname: The element name, including namespace prefix
579 * @atts: An array of name/value attributes pairs, NULL terminated
580 *
581 * called when an opening tag has been processed.
582 */
583static void
584xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
585 const xmlChar **atts) {
586 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
587 xmlTextReaderPtr reader = ctxt->_private;
588
589#ifdef DEBUG_CALLBACKS
590 printf("xmlTextReaderStartElement(%s)\n", fullname);
591#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000592 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000593 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000594 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
595 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
596 (ctxt->input->cur[1] == '>'))
597 ctxt->node->_private = (void *) xmlTextReaderIsEmpty;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000598 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000599 if (reader != NULL)
600 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000601}
602
603/**
604 * xmlTextReaderEndElement:
605 * @ctx: the user data (XML parser context)
606 * @fullname: The element name, including namespace prefix
607 *
608 * called when an ending tag has been processed.
609 */
610static void
611xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
612 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
613 xmlTextReaderPtr reader = ctxt->_private;
614
615#ifdef DEBUG_CALLBACKS
616 printf("xmlTextReaderEndElement(%s)\n", fullname);
617#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000618 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000619 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000620 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000621}
622
623/**
Daniel Veillard07cb8222003-09-10 10:51:05 +0000624 * xmlTextReaderStartElementNs:
625 * @ctx: the user data (XML parser context)
626 * @localname: the local name of the element
627 * @prefix: the element namespace prefix if available
628 * @URI: the element namespace name if available
629 * @nb_namespaces: number of namespace definitions on that node
630 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
631 * @nb_attributes: the number of attributes on that node
632 * nb_defaulted: the number of defaulted attributes.
633 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
634 * attribute values.
635 *
636 * called when an opening tag has been processed.
637 */
638static void
639xmlTextReaderStartElementNs(void *ctx,
640 const xmlChar *localname,
641 const xmlChar *prefix,
642 const xmlChar *URI,
643 int nb_namespaces,
644 const xmlChar **namespaces,
645 int nb_attributes,
646 int nb_defaulted,
647 const xmlChar **attributes)
648{
649 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
650 xmlTextReaderPtr reader = ctxt->_private;
651
652#ifdef DEBUG_CALLBACKS
653 printf("xmlTextReaderStartElementNs(%s)\n", fullname);
654#endif
655 if ((reader != NULL) && (reader->startElementNs != NULL)) {
656 reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
657 namespaces, nb_attributes, nb_defaulted,
658 attributes);
659 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
660 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
661 (ctxt->input->cur[1] == '>'))
662 ctxt->node->_private = (void *) xmlTextReaderIsEmpty;
663 }
664 if (reader != NULL)
665 reader->state = XML_TEXTREADER_ELEMENT;
666}
667
668/**
669 * xmlTextReaderEndElementNs:
670 * @ctx: the user data (XML parser context)
671 * @localname: the local name of the element
672 * @prefix: the element namespace prefix if available
673 * @URI: the element namespace name if available
674 *
675 * called when an ending tag has been processed.
676 */
677static void
678xmlTextReaderEndElementNs(void *ctx,
679 const xmlChar * localname,
680 const xmlChar * prefix,
681 const xmlChar * URI)
682{
683 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
684 xmlTextReaderPtr reader = ctxt->_private;
685
686#ifdef DEBUG_CALLBACKS
687 printf("xmlTextReaderEndElementNs(%s)\n", fullname);
688#endif
689 if ((reader != NULL) && (reader->endElementNs != NULL)) {
690 reader->endElementNs(ctx, localname, prefix, URI);
691 }
692}
693
694
695/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000696 * xmlTextReaderCharacters:
697 * @ctx: the user data (XML parser context)
698 * @ch: a xmlChar string
699 * @len: the number of xmlChar
700 *
701 * receiving some chars from the parser.
702 */
703static void
704xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
705{
706 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
707 xmlTextReaderPtr reader = ctxt->_private;
708
709#ifdef DEBUG_CALLBACKS
710 printf("xmlTextReaderCharacters()\n");
711#endif
712 if ((reader != NULL) && (reader->characters != NULL)) {
713 reader->characters(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000714 }
715}
716
717/**
718 * xmlTextReaderCDataBlock:
719 * @ctx: the user data (XML parser context)
720 * @value: The pcdata content
721 * @len: the block length
722 *
723 * called when a pcdata block has been parsed
724 */
725static void
726xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
727{
728 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
729 xmlTextReaderPtr reader = ctxt->_private;
730
731#ifdef DEBUG_CALLBACKS
732 printf("xmlTextReaderCDataBlock()\n");
733#endif
734 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
735 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000736 }
737}
738
739/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000740 * xmlTextReaderPushData:
741 * @reader: the xmlTextReaderPtr used
742 *
743 * Push data down the progressive parser until a significant callback
744 * got raised.
745 *
746 * Returns -1 in case of failure, 0 otherwise
747 */
748static int
749xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000750 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000751 int val, s;
William M. Brack779af002003-08-01 15:55:39 +0000752 xmlTextReaderState oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000753
754 if ((reader->input == NULL) || (reader->input->buffer == NULL))
755 return(-1);
756
Daniel Veillardea7751d2002-12-20 00:16:24 +0000757 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000758 reader->state = XML_TEXTREADER_NONE;
759 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000760
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000761 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000762 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000763 /*
764 * Refill the buffer unless we are at the end of the stream
765 */
766 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
767 val = xmlParserInputBufferRead(reader->input, 4096);
768 if (val <= 0) {
769 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000770 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000771 if ((oldstate != XML_TEXTREADER_START) ||
772 (reader->ctxt->myDoc != NULL))
773 return(val);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000774 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000775
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000776 } else
777 break;
778 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000779 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000780 * parse by block of CHUNK_SIZE bytes, various tests show that
781 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000782 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000783 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000784 val = xmlParseChunk(reader->ctxt,
785 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000786 CHUNK_SIZE, 0);
787 reader->cur += CHUNK_SIZE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000788 if (val != 0)
789 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000790 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000791 s = inbuf->use - reader->cur;
792 val = xmlParseChunk(reader->ctxt,
793 (const char *) &inbuf->content[reader->cur],
794 s, 0);
795 reader->cur += s;
796 if (val != 0)
797 return(-1);
798 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000799 }
800 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000801
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000802 /*
803 * Discard the consumed input when needed and possible
804 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000805 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000806 if (reader->cur >= 4096) {
807 val = xmlBufferShrink(inbuf, reader->cur);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000808 if (val >= 0) {
809 reader->cur -= val;
810 }
811 }
812 }
813
814 /*
815 * At the end of the stream signal that the work is done to the Push
816 * parser.
817 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000818 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000819 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000820 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000821 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000822 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000823 s, 1);
824 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000825 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000826 if (val != 0) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000827 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000828 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000829 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000830 return(0);
831}
832
833/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000834 * xmlTextReaderValidatePush:
835 * @reader: the xmlTextReaderPtr used
836 *
837 * Push the current node for validation
838 */
839static void
840xmlTextReaderValidatePush(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000841#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000842 xmlNodePtr node = reader->node;
843
Daniel Veillardf4e55762003-04-15 23:32:22 +0000844 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
845 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
846 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
847 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
848 reader->ctxt->myDoc, node, node->name);
849 } else {
850 /* TODO use the BuildQName interface */
851 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000852
Daniel Veillardf4e55762003-04-15 23:32:22 +0000853 qname = xmlStrdup(node->ns->prefix);
854 qname = xmlStrcat(qname, BAD_CAST ":");
855 qname = xmlStrcat(qname, node->name);
856 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
857 reader->ctxt->myDoc, node, qname);
858 if (qname != NULL)
859 xmlFree(qname);
860 }
861#ifdef LIBXML_SCHEMAS_ENABLED
862 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
863 (reader->rngValidCtxt != NULL)) {
864 int ret;
865
866 if (reader->rngFullNode != NULL) return;
867 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
868 reader->ctxt->myDoc,
869 node);
870 if (ret == 0) {
871 /*
872 * this element requires a full tree
873 */
874 node = xmlTextReaderExpand(reader);
875 if (node == NULL) {
876printf("Expand failed !\n");
877 ret = -1;
878 } else {
879 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
880 reader->ctxt->myDoc,
881 node);
882 reader->rngFullNode = node;
883 }
884 }
885 if (ret != 1)
886 reader->rngValidErrors++;
887#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000888 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000889#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000890}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000891
892/**
893 * xmlTextReaderValidateCData:
894 * @reader: the xmlTextReaderPtr used
895 * @data: pointer to the CData
896 * @len: lenght of the CData block in bytes.
897 *
898 * Push some CData for validation
899 */
900static void
901xmlTextReaderValidateCData(xmlTextReaderPtr reader,
902 const xmlChar *data, int len) {
903#ifdef LIBXML_REGEXP_ENABLED
904 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
905 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
906 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
907 data, len);
908#ifdef LIBXML_SCHEMAS_ENABLED
909 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
910 (reader->rngValidCtxt != NULL)) {
911 int ret;
912
913 if (reader->rngFullNode != NULL) return;
914 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
915 if (ret != 1)
916 reader->rngValidErrors++;
917#endif
918 }
919#endif /* LIBXML_REGEXP_ENABLED */
920}
921
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000922/**
923 * xmlTextReaderValidatePop:
924 * @reader: the xmlTextReaderPtr used
925 *
926 * Pop the current node from validation
927 */
928static void
929xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000930#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000931 xmlNodePtr node = reader->node;
932
Daniel Veillardf4e55762003-04-15 23:32:22 +0000933 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
934 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
935 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
936 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
937 reader->ctxt->myDoc, node, node->name);
938 } else {
939 /* TODO use the BuildQName interface */
940 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000941
Daniel Veillardf4e55762003-04-15 23:32:22 +0000942 qname = xmlStrdup(node->ns->prefix);
943 qname = xmlStrcat(qname, BAD_CAST ":");
944 qname = xmlStrcat(qname, node->name);
945 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
946 reader->ctxt->myDoc, node, qname);
947 if (qname != NULL)
948 xmlFree(qname);
949 }
950#ifdef LIBXML_SCHEMAS_ENABLED
951 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
952 (reader->rngValidCtxt != NULL)) {
953 int ret;
954
955 if (reader->rngFullNode != NULL) {
956 if (node == reader->rngFullNode)
957 reader->rngFullNode = NULL;
958 return;
959 }
960 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
961 reader->ctxt->myDoc,
962 node);
963 if (ret != 1)
964 reader->rngValidErrors++;
965#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000966 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000967#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000968}
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000969/**
970 * xmlTextReaderValidateEntity:
971 * @reader: the xmlTextReaderPtr used
972 *
973 * Handle the validation when an entity reference is encountered and
974 * entity substitution is not activated. As a result the parser interface
975 * must walk through the entity and do the validation calls
976 */
977static void
978xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000979#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000980 xmlNodePtr oldnode = reader->node;
981 xmlNodePtr node = reader->node;
982 xmlParserCtxtPtr ctxt = reader->ctxt;
983
984 do {
985 if (node->type == XML_ENTITY_REF_NODE) {
986 /*
987 * Case where the underlying tree is not availble, lookup the entity
988 * and walk it.
989 */
990 if ((node->children == NULL) && (ctxt->sax != NULL) &&
991 (ctxt->sax->getEntity != NULL)) {
992 node->children = (xmlNodePtr)
993 ctxt->sax->getEntity(ctxt, node->name);
994 }
995
996 if ((node->children != NULL) &&
997 (node->children->type == XML_ENTITY_DECL) &&
998 (node->children->children != NULL)) {
999 xmlTextReaderEntPush(reader, node);
1000 node = node->children->children;
1001 continue;
1002 } else {
1003 /*
1004 * The error has probably be raised already.
1005 */
1006 if (node == oldnode)
1007 break;
1008 node = node->next;
1009 }
1010 } else if (node->type == XML_ELEMENT_NODE) {
1011 reader->node = node;
1012 xmlTextReaderValidatePush(reader);
1013 } else if ((node->type == XML_TEXT_NODE) ||
1014 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001015 xmlTextReaderValidateCData(reader, node->content,
1016 xmlStrlen(node->content));
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001017 }
1018
1019 /*
1020 * go to next node
1021 */
1022 if (node->children != NULL) {
1023 node = node->children;
1024 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00001025 } else if (node->type == XML_ELEMENT_NODE) {
1026 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001027 }
1028 if (node->next != NULL) {
1029 node = node->next;
1030 continue;
1031 }
1032 do {
1033 node = node->parent;
1034 if (node->type == XML_ELEMENT_NODE) {
1035 reader->node = node;
1036 xmlTextReaderValidatePop(reader);
1037 }
1038 if ((node->type == XML_ENTITY_DECL) &&
1039 (reader->ent != NULL) && (reader->ent->children == node)) {
1040 node = xmlTextReaderEntPop(reader);
1041 }
1042 if (node == oldnode)
1043 break;
1044 if (node->next != NULL) {
1045 node = node->next;
1046 break;
1047 }
1048 } while ((node != NULL) && (node != oldnode));
1049 } while ((node != NULL) && (node != oldnode));
1050 reader->node = oldnode;
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001051#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001052}
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001053
1054
1055/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001056 * xmlTextReaderGetSuccessor:
1057 * @cur: the current node
1058 *
1059 * Get the successor of a node if available.
1060 *
1061 * Returns the successor node or NULL
1062 */
1063static xmlNodePtr
1064xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1065 if (cur == NULL) return(NULL) ; /* ERROR */
1066 if (cur->next != NULL) return(cur->next) ;
1067 do {
1068 cur = cur->parent;
1069 if (cur == NULL) return(NULL);
1070 if (cur->next != NULL) return(cur->next);
1071 } while (cur != NULL);
1072 return(cur);
1073}
1074
1075/**
1076 * xmlTextReaderDoExpand:
1077 * @reader: the xmlTextReaderPtr used
1078 *
1079 * Makes sure that the current node is fully read as well as all its
1080 * descendant. It means the full DOM subtree must be available at the
1081 * end of the call.
1082 *
1083 * Returns 1 if the node was expanded successfully, 0 if there is no more
1084 * nodes to read, or -1 in case of error
1085 */
1086static int
1087xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1088 int val;
1089
1090 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1091 return(-1);
1092
1093 do {
1094 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1095 return(1);
Daniel Veillarda37aab82003-06-09 09:10:36 +00001096 if (reader->ctxt->nodeNr <= reader->depth)
1097 return(1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001098 if (reader->mode == XML_TEXTREADER_MODE_EOF)
1099 return(1);
1100 val = xmlTextReaderPushData(reader);
1101 if (val < 0)
1102 return(-1);
1103 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1104 return(1);
1105}
1106
1107/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001108 * xmlTextReaderRead:
1109 * @reader: the xmlTextReaderPtr used
1110 *
1111 * Moves the position of the current instance to the next node in
1112 * the stream, exposing its properties.
1113 *
1114 * Returns 1 if the node was read successfully, 0 if there is no more
1115 * nodes to read, or -1 in case of error
1116 */
1117int
1118xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +00001119 int val, olddepth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001120 xmlTextReaderState oldstate = 0;
1121 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001122
1123 if ((reader == NULL) || (reader->ctxt == NULL))
1124 return(-1);
1125 if (reader->ctxt->wellFormed != 1)
1126 return(-1);
1127
1128#ifdef DEBUG_READER
1129 fprintf(stderr, "\nREAD ");
1130 DUMP_READER
1131#endif
Daniel Veillard29b3e282002-12-29 11:14:41 +00001132 reader->curnode = NULL;
Daniel Veillard67df8092002-12-16 22:04:11 +00001133 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1134 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001135 /*
1136 * Initial state
1137 */
1138 do {
1139 val = xmlTextReaderPushData(reader);
1140 if (val < 0)
1141 return(-1);
1142 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001143 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1144 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001145 if (reader->ctxt->myDoc != NULL)
1146 reader->ctxt->myDoc->_private = reader;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001147 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001148 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001149 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001150 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001151 if (reader->node == NULL)
1152 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001153 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001154 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +00001155 if (reader->ctxt->myDoc != NULL) {
1156 reader->node = reader->ctxt->myDoc->children;
1157 }
1158 if (reader->node == NULL)
1159 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +00001160 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001161 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001162 reader->depth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001163 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001164 }
1165 oldstate = reader->state;
1166 olddepth = reader->ctxt->nodeNr;
1167 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001168
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001169get_next_node:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001170 /*
1171 * If we are not backtracking on ancestors or examined nodes,
1172 * that the parser didn't finished or that we arent at the end
1173 * of stream, continue processing.
1174 */
Daniel Veillarda880b122003-04-21 21:36:41 +00001175 while ((reader->node->next == NULL) &&
1176 (reader->ctxt->nodeNr == olddepth) &&
1177 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +00001178 (reader->node->children == NULL) ||
1179 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard409a8142003-07-18 15:16:57 +00001180 ((reader->node->children != NULL) &&
1181 (reader->node->children->type == XML_TEXT_NODE) &&
1182 (reader->node->children->next == NULL)) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001183 (reader->node->type == XML_DTD_NODE) ||
1184 (reader->node->type == XML_DOCUMENT_NODE) ||
1185 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001186 ((reader->ctxt->node == NULL) ||
1187 (reader->ctxt->node == reader->node) ||
1188 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +00001189 (reader->ctxt->instate != XML_PARSER_EOF)) {
1190 val = xmlTextReaderPushData(reader);
1191 if (val < 0)
1192 return(-1);
1193 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001194 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001195 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001196 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001197 if ((reader->node->children != NULL) &&
1198 (reader->node->type != XML_ENTITY_REF_NODE) &&
1199 (reader->node->type != XML_DTD_NODE)) {
1200 reader->node = reader->node->children;
1201 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001202 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001203 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001204 }
1205 }
1206 if (reader->node->next != NULL) {
1207 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +00001208 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001209 (reader->node->children == NULL) &&
1210 (reader->node->_private != (void *)xmlTextReaderIsEmpty)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001211 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001212 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001213 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00001214 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001215 (reader->node->type == XML_ELEMENT_NODE))
1216 xmlTextReaderValidatePop(reader);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001217 reader->node = reader->node->next;
1218 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001219
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001220 /*
1221 * Cleanup of the old node
1222 */
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001223 if ((reader->node->prev != NULL) &&
1224 (reader->node->prev->type != XML_DTD_NODE)) {
1225 xmlNodePtr tmp = reader->node->prev;
1226 xmlUnlinkNode(tmp);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001227 xmlTextReaderFreeNode(reader, tmp);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001228 }
1229
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001230 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001231 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001232 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +00001233 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001234 (reader->node->children == NULL) &&
1235 (reader->node->_private != (void *)xmlTextReaderIsEmpty)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001236 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001237 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001238 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00001239 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001240 xmlTextReaderValidatePop(reader);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001241 reader->node = reader->node->parent;
1242 if ((reader->node == NULL) ||
1243 (reader->node->type == XML_DOCUMENT_NODE) ||
1244#ifdef LIBXML_DOCB_ENABLED
1245 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
1246#endif
1247 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001248 if (reader->mode != XML_TEXTREADER_DONE) {
1249 val = xmlParseChunk(reader->ctxt, "", 0, 1);
1250 reader->mode = XML_TEXTREADER_DONE;
1251 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001252 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001253 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001254
1255 /*
1256 * Cleanup of the old node
1257 */
1258 if (oldnode->type != XML_DTD_NODE) {
1259 xmlUnlinkNode(oldnode);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001260 xmlTextReaderFreeNode(reader, oldnode);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001261 }
1262
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001263 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001264 }
1265 reader->depth--;
1266 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001267
1268node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001269 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001270
1271 /*
Daniel Veillarda880b122003-04-21 21:36:41 +00001272 * If we are in the middle of a piece of CDATA make sure it's finished
1273 */
1274 if ((reader->node != NULL) &&
Daniel Veillardccc4d2b2003-09-17 21:27:31 +00001275 (reader->node->next == NULL) &&
Daniel Veillarda880b122003-04-21 21:36:41 +00001276 ((reader->node->type == XML_TEXT_NODE) ||
1277 (reader->node->type == XML_CDATA_SECTION_NODE))) {
1278 xmlTextReaderExpand(reader);
1279 }
1280
1281 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001282 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001283 */
1284 if ((reader->node != NULL) &&
1285 (reader->node->type == XML_ENTITY_REF_NODE) &&
1286 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1287 /*
1288 * Case where the underlying tree is not availble, lookup the entity
1289 * and walk it.
1290 */
1291 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
1292 (reader->ctxt->sax->getEntity != NULL)) {
1293 reader->node->children = (xmlNodePtr)
1294 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
1295 }
1296
1297 if ((reader->node->children != NULL) &&
1298 (reader->node->children->type == XML_ENTITY_DECL) &&
1299 (reader->node->children->children != NULL)) {
1300 xmlTextReaderEntPush(reader, reader->node);
1301 reader->node = reader->node->children->children;
1302 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001303 } else if ((reader->node != NULL) &&
1304 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +00001305 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001306 xmlTextReaderValidateEntity(reader);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001307 }
1308 if ((reader->node != NULL) &&
1309 (reader->node->type == XML_ENTITY_DECL) &&
1310 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1311 reader->node = xmlTextReaderEntPop(reader);
1312 reader->depth++;
1313 goto get_next_node;
1314 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001315#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001316 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001317 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001318
1319 if ((node->type == XML_ELEMENT_NODE) &&
1320 ((reader->state != XML_TEXTREADER_END) &&
1321 (reader->state != XML_TEXTREADER_BACKTRACK))) {
1322 xmlTextReaderValidatePush(reader);
1323 } else if ((node->type == XML_TEXT_NODE) ||
1324 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001325 xmlTextReaderValidateCData(reader, node->content,
1326 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001327 }
1328 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001329#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001330 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001331node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001332 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001333 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001334}
1335
Daniel Veillard67df8092002-12-16 22:04:11 +00001336/**
1337 * xmlTextReaderReadState:
1338 * @reader: the xmlTextReaderPtr used
1339 *
1340 * Gets the read state of the reader.
1341 *
1342 * Returns the state value, or -1 in case of error
1343 */
1344int
1345xmlTextReaderReadState(xmlTextReaderPtr reader) {
1346 if (reader == NULL)
1347 return(-1);
1348 return(reader->mode);
1349}
1350
1351/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001352 * xmlTextReaderExpand:
1353 * @reader: the xmlTextReaderPtr used
1354 *
1355 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +00001356 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001357 *
1358 * Returns a node pointer valid until the next xmlTextReaderRead() call
1359 * or NULL in case of error.
1360 */
1361xmlNodePtr
1362xmlTextReaderExpand(xmlTextReaderPtr reader) {
1363 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1364 return(NULL);
1365 if (xmlTextReaderDoExpand(reader) < 0)
1366 return(NULL);
1367 return(reader->node);
1368}
1369
1370/**
1371 * xmlTextReaderNext:
1372 * @reader: the xmlTextReaderPtr used
1373 *
1374 * Skip to the node following the current one in document order while
1375 * avoiding the subtree if any.
1376 *
1377 * Returns 1 if the node was read successfully, 0 if there is no more
1378 * nodes to read, or -1 in case of error
1379 */
1380int
1381xmlTextReaderNext(xmlTextReaderPtr reader) {
1382 int ret;
1383 xmlNodePtr cur;
1384
1385 if (reader == NULL)
1386 return(-1);
1387 cur = reader->node;
1388 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1389 return(xmlTextReaderRead(reader));
1390 if (reader->state == XML_TEXTREADER_END)
1391 return(xmlTextReaderRead(reader));
1392 if (cur->_private == (void *)xmlTextReaderIsEmpty)
1393 return(xmlTextReaderRead(reader));
1394 do {
1395 ret = xmlTextReaderRead(reader);
1396 if (ret != 1)
1397 return(ret);
1398 } while (reader->node != cur);
1399 return(xmlTextReaderRead(reader));
1400}
1401
1402/**
Daniel Veillard67df8092002-12-16 22:04:11 +00001403 * xmlTextReaderReadInnerXml:
1404 * @reader: the xmlTextReaderPtr used
1405 *
1406 * Reads the contents of the current node, including child nodes and markup.
1407 *
1408 * Returns a string containing the XML content, or NULL if the current node
1409 * is neither an element nor attribute, or has no child nodes. The
1410 * string must be deallocated by the caller.
1411 */
1412xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001413xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001414 TODO
1415 return(NULL);
1416}
1417
1418/**
1419 * xmlTextReaderReadOuterXml:
1420 * @reader: the xmlTextReaderPtr used
1421 *
1422 * Reads the contents of the current node, including child nodes and markup.
1423 *
1424 * Returns a string containing the XML content, or NULL if the current node
1425 * is neither an element nor attribute, or has no child nodes. The
1426 * string must be deallocated by the caller.
1427 */
1428xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001429xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001430 TODO
1431 return(NULL);
1432}
1433
1434/**
1435 * xmlTextReaderReadString:
1436 * @reader: the xmlTextReaderPtr used
1437 *
1438 * Reads the contents of an element or a text node as a string.
1439 *
1440 * Returns a string containing the contents of the Element or Text node,
1441 * or NULL if the reader is positioned on any other type of node.
1442 * The string must be deallocated by the caller.
1443 */
1444xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001445xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001446 TODO
1447 return(NULL);
1448}
1449
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001450#if 0
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001451/**
1452 * xmlTextReaderReadBase64:
1453 * @reader: the xmlTextReaderPtr used
1454 * @array: a byte array to store the content.
1455 * @offset: the zero-based index into array where the method should
1456 * begin to write.
1457 * @len: the number of bytes to write.
1458 *
1459 * Reads and decodes the Base64 encoded contents of an element and
1460 * stores the result in a byte buffer.
1461 *
1462 * Returns the number of bytes written to array, or zero if the current
1463 * instance is not positioned on an element or -1 in case of error.
1464 */
1465int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001466xmlTextReaderReadBase64(xmlTextReaderPtr reader,
1467 unsigned char *array ATTRIBUTE_UNUSED,
1468 int offset ATTRIBUTE_UNUSED,
1469 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001470 if ((reader == NULL) || (reader->ctxt == NULL))
1471 return(-1);
1472 if (reader->ctxt->wellFormed != 1)
1473 return(-1);
1474
1475 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1476 return(0);
1477 TODO
1478 return(0);
1479}
1480
1481/**
1482 * xmlTextReaderReadBinHex:
1483 * @reader: the xmlTextReaderPtr used
1484 * @array: a byte array to store the content.
1485 * @offset: the zero-based index into array where the method should
1486 * begin to write.
1487 * @len: the number of bytes to write.
1488 *
1489 * Reads and decodes the BinHex encoded contents of an element and
1490 * stores the result in a byte buffer.
1491 *
1492 * Returns the number of bytes written to array, or zero if the current
1493 * instance is not positioned on an element or -1 in case of error.
1494 */
1495int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001496xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
1497 unsigned char *array ATTRIBUTE_UNUSED,
1498 int offset ATTRIBUTE_UNUSED,
1499 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001500 if ((reader == NULL) || (reader->ctxt == NULL))
1501 return(-1);
1502 if (reader->ctxt->wellFormed != 1)
1503 return(-1);
1504
1505 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1506 return(0);
1507 TODO
1508 return(0);
1509}
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001510#endif
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001511
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001512/************************************************************************
1513 * *
1514 * Constructor and destructors *
1515 * *
1516 ************************************************************************/
1517/**
1518 * xmlNewTextReader:
1519 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001520 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001521 *
1522 * Create an xmlTextReader structure fed with @input
1523 *
1524 * Returns the new xmlTextReaderPtr or NULL in case of error
1525 */
1526xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001527xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001528 xmlTextReaderPtr ret;
1529 int val;
1530
1531 if (input == NULL)
1532 return(NULL);
1533 ret = xmlMalloc(sizeof(xmlTextReader));
1534 if (ret == NULL) {
1535 xmlGenericError(xmlGenericErrorContext,
1536 "xmlNewTextReader : malloc failed\n");
1537 return(NULL);
1538 }
1539 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001540 ret->entTab = NULL;
1541 ret->entMax = 0;
1542 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001543 ret->input = input;
1544 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1545 if (ret->sax == NULL) {
1546 xmlFree(ret);
1547 xmlGenericError(xmlGenericErrorContext,
1548 "xmlNewTextReader : malloc failed\n");
1549 return(NULL);
1550 }
1551 memcpy(ret->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler));
1552 ret->startElement = ret->sax->startElement;
1553 ret->sax->startElement = xmlTextReaderStartElement;
1554 ret->endElement = ret->sax->endElement;
1555 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001556 if (ret->sax->initialized == XML_SAX2_MAGIC) {
1557 ret->startElementNs = ret->sax->startElementNs;
1558 ret->sax->startElementNs = xmlTextReaderStartElementNs;
1559 ret->endElementNs = ret->sax->endElementNs;
1560 ret->sax->endElementNs = xmlTextReaderEndElementNs;
1561 } else {
1562 ret->startElementNs = NULL;
1563 ret->endElementNs = NULL;
1564 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001565 ret->characters = ret->sax->characters;
1566 ret->sax->characters = xmlTextReaderCharacters;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001567 ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001568 ret->cdataBlock = ret->sax->cdataBlock;
1569 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001570
Daniel Veillard67df8092002-12-16 22:04:11 +00001571 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001572 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001573 ret->curnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001574 val = xmlParserInputBufferRead(input, 4);
1575 if (val >= 4) {
1576 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001577 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001578 ret->base = 0;
1579 ret->cur = 4;
1580 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001581 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001582 ret->base = 0;
1583 ret->cur = 0;
1584 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001585 if (ret->ctxt == NULL) {
1586 xmlGenericError(xmlGenericErrorContext,
1587 "xmlNewTextReader : malloc failed\n");
1588 xmlFree(ret->sax);
1589 xmlFree(ret);
1590 return(NULL);
1591 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001592 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001593 ret->ctxt->linenumbers = 1;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001594 ret->ctxt->dictNames = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001595 ret->allocs = XML_TEXTREADER_CTXT;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001596 /*
1597 * use the parser dictionnary to allocate all elements and attributes names
1598 */
1599 ret->ctxt->docdict = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001600 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001601}
1602
1603/**
1604 * xmlNewTextReaderFilename:
1605 * @URI: the URI of the resource to process
1606 *
1607 * Create an xmlTextReader structure fed with the resource at @URI
1608 *
1609 * Returns the new xmlTextReaderPtr or NULL in case of error
1610 */
1611xmlTextReaderPtr
1612xmlNewTextReaderFilename(const char *URI) {
1613 xmlParserInputBufferPtr input;
1614 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001615 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001616
1617 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1618 if (input == NULL)
1619 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001620 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001621 if (ret == NULL) {
1622 xmlFreeParserInputBuffer(input);
1623 return(NULL);
1624 }
1625 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001626 if (ret->ctxt->directory == NULL)
1627 directory = xmlParserGetDirectory(URI);
1628 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1629 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1630 if (directory != NULL)
1631 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001632 return(ret);
1633}
1634
1635/**
1636 * xmlFreeTextReader:
1637 * @reader: the xmlTextReaderPtr
1638 *
1639 * Deallocate all the resources associated to the reader
1640 */
1641void
1642xmlFreeTextReader(xmlTextReaderPtr reader) {
1643 if (reader == NULL)
1644 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001645#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001646 if (reader->rngSchemas != NULL) {
1647 xmlRelaxNGFree(reader->rngSchemas);
1648 reader->rngSchemas = NULL;
1649 }
1650 if (reader->rngValidCtxt != NULL) {
1651 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
1652 reader->rngValidCtxt = NULL;
1653 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001654#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001655 if (reader->ctxt != NULL) {
1656 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001657 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001658 reader->ctxt->myDoc = NULL;
1659 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00001660 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
1661 (reader->ctxt->vctxt.vstateMax > 0)){
1662 xmlFree(reader->ctxt->vctxt.vstateTab);
1663 reader->ctxt->vctxt.vstateTab = 0;
1664 reader->ctxt->vctxt.vstateMax = 0;
1665 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001666 if (reader->allocs & XML_TEXTREADER_CTXT)
1667 xmlFreeParserCtxt(reader->ctxt);
1668 }
1669 if (reader->sax != NULL)
1670 xmlFree(reader->sax);
1671 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
1672 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001673 if (reader->faketext != NULL) {
1674 xmlFreeNode(reader->faketext);
1675 }
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001676 if (reader->entTab != NULL)
1677 xmlFree(reader->entTab);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001678 xmlFree(reader);
1679}
1680
1681/************************************************************************
1682 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001683 * Methods for XmlTextReader *
1684 * *
1685 ************************************************************************/
1686/**
1687 * xmlTextReaderClose:
1688 * @reader: the xmlTextReaderPtr used
1689 *
1690 * This method releases any resources allocated by the current instance
1691 * changes the state to Closed and close any underlying input.
1692 *
1693 * Returns 0 or -1 in case of error
1694 */
1695int
1696xmlTextReaderClose(xmlTextReaderPtr reader) {
1697 if (reader == NULL)
1698 return(-1);
1699 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001700 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001701 reader->mode = XML_TEXTREADER_MODE_CLOSED;
1702 if (reader->ctxt != NULL) {
1703 if (reader->ctxt->myDoc != NULL) {
1704 xmlFreeDoc(reader->ctxt->myDoc);
1705 reader->ctxt->myDoc = NULL;
1706 }
1707 if (reader->allocs & XML_TEXTREADER_CTXT) {
1708 xmlFreeParserCtxt(reader->ctxt);
1709 reader->allocs -= XML_TEXTREADER_CTXT;
1710 }
1711 }
1712 if (reader->sax != NULL) {
1713 xmlFree(reader->sax);
1714 reader->sax = NULL;
1715 }
1716 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
1717 xmlFreeParserInputBuffer(reader->input);
1718 reader->allocs -= XML_TEXTREADER_INPUT;
1719 }
1720 return(0);
1721}
1722
1723/**
1724 * xmlTextReaderGetAttributeNo:
1725 * @reader: the xmlTextReaderPtr used
1726 * @no: the zero-based index of the attribute relative to the containing element
1727 *
1728 * Provides the value of the attribute with the specified index relative
1729 * to the containing element.
1730 *
1731 * Returns a string containing the value of the specified attribute, or NULL
1732 * in case of error. The string must be deallocated by the caller.
1733 */
1734xmlChar *
1735xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
1736 xmlChar *ret;
1737 int i;
1738 xmlAttrPtr cur;
1739 xmlNsPtr ns;
1740
1741 if (reader == NULL)
1742 return(NULL);
1743 if (reader->node == NULL)
1744 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001745 if (reader->curnode != NULL)
1746 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001747 /* TODO: handle the xmlDecl */
1748 if (reader->node->type != XML_ELEMENT_NODE)
1749 return(NULL);
1750
1751 ns = reader->node->nsDef;
1752 for (i = 0;(i < no) && (ns != NULL);i++) {
1753 ns = ns->next;
1754 }
1755 if (ns != NULL)
1756 return(xmlStrdup(ns->href));
1757
1758 cur = reader->node->properties;
1759 if (cur == NULL)
1760 return(NULL);
1761 for (;i < no;i++) {
1762 cur = cur->next;
1763 if (cur == NULL)
1764 return(NULL);
1765 }
1766 /* TODO walk the DTD if present */
1767
1768 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
1769 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
1770 return(ret);
1771}
1772
1773/**
1774 * xmlTextReaderGetAttribute:
1775 * @reader: the xmlTextReaderPtr used
1776 * @name: the qualified name of the attribute.
1777 *
1778 * Provides the value of the attribute with the specified qualified name.
1779 *
1780 * Returns a string containing the value of the specified attribute, or NULL
1781 * in case of error. The string must be deallocated by the caller.
1782 */
1783xmlChar *
1784xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1785 xmlChar *prefix = NULL;
1786 xmlChar *localname;
1787 xmlNsPtr ns;
1788 xmlChar *ret = NULL;
1789
1790 if ((reader == NULL) || (name == NULL))
1791 return(NULL);
1792 if (reader->node == NULL)
1793 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001794 if (reader->curnode != NULL)
1795 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001796
1797 /* TODO: handle the xmlDecl */
1798 if (reader->node->type != XML_ELEMENT_NODE)
1799 return(NULL);
1800
1801 localname = xmlSplitQName2(name, &prefix);
1802 if (localname == NULL)
1803 return(xmlGetProp(reader->node, name));
1804
1805 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1806 if (ns != NULL)
1807 ret = xmlGetNsProp(reader->node, localname, ns->href);
1808
1809 if (localname != NULL)
1810 xmlFree(localname);
1811 if (prefix != NULL)
1812 xmlFree(prefix);
1813 return(ret);
1814}
1815
1816
1817/**
1818 * xmlTextReaderGetAttributeNs:
1819 * @reader: the xmlTextReaderPtr used
1820 * @localName: the local name of the attribute.
1821 * @namespaceURI: the namespace URI of the attribute.
1822 *
1823 * Provides the value of the specified attribute
1824 *
1825 * Returns a string containing the value of the specified attribute, or NULL
1826 * in case of error. The string must be deallocated by the caller.
1827 */
1828xmlChar *
1829xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
1830 const xmlChar *namespaceURI) {
1831 if ((reader == NULL) || (localName == NULL))
1832 return(NULL);
1833 if (reader->node == NULL)
1834 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001835 if (reader->curnode != NULL)
1836 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001837
1838 /* TODO: handle the xmlDecl */
1839 if (reader->node->type != XML_ELEMENT_NODE)
1840 return(NULL);
1841
1842 return(xmlGetNsProp(reader->node, localName, namespaceURI));
1843}
1844
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001845/**
1846 * xmlTextReaderGetRemainder:
1847 * @reader: the xmlTextReaderPtr used
1848 *
1849 * Method to get the remainder of the buffered XML. this method stops the
1850 * parser, set its state to End Of File and return the input stream with
1851 * what is left that the parser did not use.
1852 *
1853 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
1854 * in case of error.
1855 */
1856xmlParserInputBufferPtr
1857xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
1858 xmlParserInputBufferPtr ret = NULL;
1859
1860 if (reader == NULL)
1861 return(NULL);
1862 if (reader->node == NULL)
1863 return(NULL);
1864
1865 reader->node = NULL;
1866 reader->curnode = NULL;
1867 reader->mode = XML_TEXTREADER_MODE_EOF;
1868 if (reader->ctxt != NULL) {
1869 if (reader->ctxt->myDoc != NULL) {
1870 xmlFreeDoc(reader->ctxt->myDoc);
1871 reader->ctxt->myDoc = NULL;
1872 }
1873 if (reader->allocs & XML_TEXTREADER_CTXT) {
1874 xmlFreeParserCtxt(reader->ctxt);
1875 reader->allocs -= XML_TEXTREADER_CTXT;
1876 }
1877 }
1878 if (reader->sax != NULL) {
1879 xmlFree(reader->sax);
1880 reader->sax = NULL;
1881 }
1882 if (reader->allocs & XML_TEXTREADER_INPUT) {
1883 ret = reader->input;
1884 reader->allocs -= XML_TEXTREADER_INPUT;
1885 } else {
1886 /*
1887 * Hum, one may need to duplicate the data structure because
1888 * without reference counting the input may be freed twice:
1889 * - by the layer which allocated it.
1890 * - by the layer to which would have been returned to.
1891 */
1892 TODO
1893 return(NULL);
1894 }
1895 return(ret);
1896}
1897
1898/**
1899 * xmlTextReaderLookupNamespace:
1900 * @reader: the xmlTextReaderPtr used
1901 * @prefix: the prefix whose namespace URI is to be resolved. To return
1902 * the default namespace, specify NULL
1903 *
1904 * Resolves a namespace prefix in the scope of the current element.
1905 *
1906 * Returns a string containing the namespace URI to which the prefix maps
1907 * or NULL in case of error. The string must be deallocated by the caller.
1908 */
1909xmlChar *
1910xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
1911 xmlNsPtr ns;
1912
1913 if (reader == NULL)
1914 return(NULL);
1915 if (reader->node == NULL)
1916 return(NULL);
1917
1918 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1919 if (ns == NULL)
1920 return(NULL);
1921 return(xmlStrdup(ns->href));
1922}
1923
1924/**
1925 * xmlTextReaderMoveToAttributeNo:
1926 * @reader: the xmlTextReaderPtr used
1927 * @no: the zero-based index of the attribute relative to the containing
1928 * element.
1929 *
1930 * Moves the position of the current instance to the attribute with
1931 * the specified index relative to the containing element.
1932 *
1933 * Returns 1 in case of success, -1 in case of error, 0 if not found
1934 */
1935int
1936xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
1937 int i;
1938 xmlAttrPtr cur;
1939 xmlNsPtr ns;
1940
1941 if (reader == NULL)
1942 return(-1);
1943 if (reader->node == NULL)
1944 return(-1);
1945 /* TODO: handle the xmlDecl */
1946 if (reader->node->type != XML_ELEMENT_NODE)
1947 return(-1);
1948
1949 reader->curnode = NULL;
1950
1951 ns = reader->node->nsDef;
1952 for (i = 0;(i < no) && (ns != NULL);i++) {
1953 ns = ns->next;
1954 }
1955 if (ns != NULL) {
1956 reader->curnode = (xmlNodePtr) ns;
1957 return(1);
1958 }
1959
1960 cur = reader->node->properties;
1961 if (cur == NULL)
1962 return(0);
1963 for (;i < no;i++) {
1964 cur = cur->next;
1965 if (cur == NULL)
1966 return(0);
1967 }
1968 /* TODO walk the DTD if present */
1969
1970 reader->curnode = (xmlNodePtr) cur;
1971 return(1);
1972}
1973
1974/**
1975 * xmlTextReaderMoveToAttribute:
1976 * @reader: the xmlTextReaderPtr used
1977 * @name: the qualified name of the attribute.
1978 *
1979 * Moves the position of the current instance to the attribute with
1980 * the specified qualified name.
1981 *
1982 * Returns 1 in case of success, -1 in case of error, 0 if not found
1983 */
1984int
1985xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1986 xmlChar *prefix = NULL;
1987 xmlChar *localname;
1988 xmlNsPtr ns;
1989 xmlAttrPtr prop;
1990
1991 if ((reader == NULL) || (name == NULL))
1992 return(-1);
1993 if (reader->node == NULL)
1994 return(-1);
1995
1996 /* TODO: handle the xmlDecl */
1997 if (reader->node->type != XML_ELEMENT_NODE)
1998 return(0);
1999
2000 localname = xmlSplitQName2(name, &prefix);
2001 if (localname == NULL) {
2002 /*
2003 * Namespace default decl
2004 */
2005 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2006 ns = reader->node->nsDef;
2007 while (ns != NULL) {
2008 if (ns->prefix == NULL) {
2009 reader->curnode = (xmlNodePtr) ns;
2010 return(1);
2011 }
2012 ns = ns->next;
2013 }
2014 return(0);
2015 }
2016
2017 prop = reader->node->properties;
2018 while (prop != NULL) {
2019 /*
2020 * One need to have
2021 * - same attribute names
2022 * - and the attribute carrying that namespace
2023 */
2024 if ((xmlStrEqual(prop->name, name)) &&
2025 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
2026 reader->curnode = (xmlNodePtr) prop;
2027 return(1);
2028 }
2029 prop = prop->next;
2030 }
2031 return(0);
2032 }
2033
2034 /*
2035 * Namespace default decl
2036 */
2037 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2038 ns = reader->node->nsDef;
2039 while (ns != NULL) {
2040 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2041 reader->curnode = (xmlNodePtr) ns;
2042 goto found;
2043 }
2044 ns = ns->next;
2045 }
2046 goto not_found;
2047 }
2048 prop = reader->node->properties;
2049 while (prop != NULL) {
2050 /*
2051 * One need to have
2052 * - same attribute names
2053 * - and the attribute carrying that namespace
2054 */
2055 if ((xmlStrEqual(prop->name, localname)) &&
2056 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2057 reader->curnode = (xmlNodePtr) prop;
2058 goto found;
2059 }
2060 prop = prop->next;
2061 }
2062not_found:
2063 if (localname != NULL)
2064 xmlFree(localname);
2065 if (prefix != NULL)
2066 xmlFree(prefix);
2067 return(0);
2068
2069found:
2070 if (localname != NULL)
2071 xmlFree(localname);
2072 if (prefix != NULL)
2073 xmlFree(prefix);
2074 return(1);
2075}
2076
2077/**
2078 * xmlTextReaderMoveToAttributeNs:
2079 * @reader: the xmlTextReaderPtr used
2080 * @localName: the local name of the attribute.
2081 * @namespaceURI: the namespace URI of the attribute.
2082 *
2083 * Moves the position of the current instance to the attribute with the
2084 * specified local name and namespace URI.
2085 *
2086 * Returns 1 in case of success, -1 in case of error, 0 if not found
2087 */
2088int
2089xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
2090 const xmlChar *localName, const xmlChar *namespaceURI) {
2091 xmlAttrPtr prop;
2092 xmlNodePtr node;
2093
2094 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2095 return(-1);
2096 if (reader->node == NULL)
2097 return(-1);
2098 if (reader->node->type != XML_ELEMENT_NODE)
2099 return(0);
2100 node = reader->node;
2101
2102 /*
2103 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
2104 * namespace name associated to "xmlns"
2105 */
2106 prop = node->properties;
2107 while (prop != NULL) {
2108 /*
2109 * One need to have
2110 * - same attribute names
2111 * - and the attribute carrying that namespace
2112 */
2113 if (xmlStrEqual(prop->name, localName) &&
2114 ((prop->ns != NULL) &&
2115 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2116 reader->curnode = (xmlNodePtr) prop;
2117 return(1);
2118 }
2119 prop = prop->next;
2120 }
2121 return(0);
2122}
2123
2124/**
2125 * xmlTextReaderMoveToFirstAttribute:
2126 * @reader: the xmlTextReaderPtr used
2127 *
2128 * Moves the position of the current instance to the first attribute
2129 * associated with the current node.
2130 *
2131 * Returns 1 in case of success, -1 in case of error, 0 if not found
2132 */
2133int
2134xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
2135 if (reader == NULL)
2136 return(-1);
2137 if (reader->node == NULL)
2138 return(-1);
2139 if (reader->node->type != XML_ELEMENT_NODE)
2140 return(0);
2141
2142 if (reader->node->nsDef != NULL) {
2143 reader->curnode = (xmlNodePtr) reader->node->nsDef;
2144 return(1);
2145 }
2146 if (reader->node->properties != NULL) {
2147 reader->curnode = (xmlNodePtr) reader->node->properties;
2148 return(1);
2149 }
2150 return(0);
2151}
2152
2153/**
2154 * xmlTextReaderMoveToNextAttribute:
2155 * @reader: the xmlTextReaderPtr used
2156 *
2157 * Moves the position of the current instance to the next attribute
2158 * associated with the current node.
2159 *
2160 * Returns 1 in case of success, -1 in case of error, 0 if not found
2161 */
2162int
2163xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
2164 if (reader == NULL)
2165 return(-1);
2166 if (reader->node == NULL)
2167 return(-1);
2168 if (reader->node->type != XML_ELEMENT_NODE)
2169 return(0);
2170 if (reader->curnode == NULL)
2171 return(xmlTextReaderMoveToFirstAttribute(reader));
2172
2173 if (reader->curnode->type == XML_NAMESPACE_DECL) {
2174 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2175 if (ns->next != NULL) {
2176 reader->curnode = (xmlNodePtr) ns->next;
2177 return(1);
2178 }
2179 if (reader->node->properties != NULL) {
2180 reader->curnode = (xmlNodePtr) reader->node->properties;
2181 return(1);
2182 }
2183 return(0);
2184 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2185 (reader->curnode->next != NULL)) {
2186 reader->curnode = reader->curnode->next;
2187 return(1);
2188 }
2189 return(0);
2190}
2191
2192/**
2193 * xmlTextReaderMoveToElement:
2194 * @reader: the xmlTextReaderPtr used
2195 *
2196 * Moves the position of the current instance to the node that
2197 * contains the current Attribute node.
2198 *
2199 * Returns 1 in case of success, -1 in case of error, 0 if not moved
2200 */
2201int
2202xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
2203 if (reader == NULL)
2204 return(-1);
2205 if (reader->node == NULL)
2206 return(-1);
2207 if (reader->node->type != XML_ELEMENT_NODE)
2208 return(0);
2209 if (reader->curnode != NULL) {
2210 reader->curnode = NULL;
2211 return(1);
2212 }
2213 return(0);
2214}
2215
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002216/**
2217 * xmlTextReaderReadAttributeValue:
2218 * @reader: the xmlTextReaderPtr used
2219 *
2220 * Parses an attribute value into one or more Text and EntityReference nodes.
2221 *
2222 * Returns 1 in case of success, 0 if the reader was not positionned on an
2223 * ttribute node or all the attribute values have been read, or -1
2224 * in case of error.
2225 */
2226int
2227xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
2228 if (reader == NULL)
2229 return(-1);
2230 if (reader->node == NULL)
2231 return(-1);
2232 if (reader->curnode == NULL)
2233 return(0);
2234 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2235 if (reader->curnode->children == NULL)
2236 return(0);
2237 reader->curnode = reader->curnode->children;
2238 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2239 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2240
2241 if (reader->faketext == NULL) {
2242 reader->faketext = xmlNewDocText(reader->node->doc,
2243 ns->href);
2244 } else {
2245 if (reader->faketext->content != NULL)
2246 xmlFree(reader->faketext->content);
2247 reader->faketext->content = xmlStrdup(ns->href);
2248 }
2249 reader->curnode = reader->faketext;
2250 } else {
2251 if (reader->curnode->next == NULL)
2252 return(0);
2253 reader->curnode = reader->curnode->next;
2254 }
2255 return(1);
2256}
2257
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002258/************************************************************************
2259 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002260 * Acces API to the current node *
2261 * *
2262 ************************************************************************/
2263/**
2264 * xmlTextReaderAttributeCount:
2265 * @reader: the xmlTextReaderPtr used
2266 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002267 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002268 *
2269 * Returns 0 i no attributes, -1 in case of error or the attribute count
2270 */
2271int
2272xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
2273 int ret;
2274 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00002275 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002276 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002277
2278 if (reader == NULL)
2279 return(-1);
2280 if (reader->node == NULL)
2281 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002282
2283 if (reader->curnode != NULL)
2284 node = reader->curnode;
2285 else
2286 node = reader->node;
2287
2288 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002289 return(0);
2290 if ((reader->state == XML_TEXTREADER_END) ||
2291 (reader->state == XML_TEXTREADER_BACKTRACK))
2292 return(0);
2293 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002294 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002295 while (attr != NULL) {
2296 ret++;
2297 attr = attr->next;
2298 }
Daniel Veillard67df8092002-12-16 22:04:11 +00002299 ns = node->nsDef;
2300 while (ns != NULL) {
2301 ret++;
2302 ns = ns->next;
2303 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002304 return(ret);
2305}
2306
2307/**
2308 * xmlTextReaderNodeType:
2309 * @reader: the xmlTextReaderPtr used
2310 *
2311 * Get the node type of the current node
2312 * Reference:
2313 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
2314 *
2315 * Returns the xmlNodeType of the current node or -1 in case of error
2316 */
2317int
2318xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002319 xmlNodePtr node;
Daniel Veillardd6038e02003-07-30 16:37:18 +00002320
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002321 if (reader == NULL)
2322 return(-1);
2323 if (reader->node == NULL)
Daniel Veillardd6038e02003-07-30 16:37:18 +00002324 return(XML_READER_TYPE_NONE);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002325 if (reader->curnode != NULL)
2326 node = reader->curnode;
2327 else
2328 node = reader->node;
2329 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002330 case XML_ELEMENT_NODE:
2331 if ((reader->state == XML_TEXTREADER_END) ||
2332 (reader->state == XML_TEXTREADER_BACKTRACK))
Daniel Veillardd6038e02003-07-30 16:37:18 +00002333 return(XML_READER_TYPE_END_ELEMENT);
2334 return(XML_READER_TYPE_ELEMENT);
Daniel Veillardecaba492002-12-30 10:55:29 +00002335 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002336 case XML_ATTRIBUTE_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002337 return(XML_READER_TYPE_ATTRIBUTE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002338 case XML_TEXT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002339 if (xmlIsBlankNode(reader->node)) {
2340 if (xmlNodeGetSpacePreserve(reader->node))
2341 return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2342 else
2343 return(XML_READER_TYPE_WHITESPACE);
2344 } else {
2345 return(XML_READER_TYPE_TEXT);
2346 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002347 case XML_CDATA_SECTION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002348 return(XML_READER_TYPE_CDATA);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002349 case XML_ENTITY_REF_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002350 return(XML_READER_TYPE_ENTITY_REFERENCE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002351 case XML_ENTITY_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002352 return(XML_READER_TYPE_ENTITY);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002353 case XML_PI_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002354 return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002355 case XML_COMMENT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002356 return(XML_READER_TYPE_COMMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002357 case XML_DOCUMENT_NODE:
2358 case XML_HTML_DOCUMENT_NODE:
2359#ifdef LIBXML_DOCB_ENABLED
2360 case XML_DOCB_DOCUMENT_NODE:
2361#endif
Daniel Veillardd6038e02003-07-30 16:37:18 +00002362 return(XML_READER_TYPE_DOCUMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002363 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002364 return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002365 case XML_NOTATION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002366 return(XML_READER_TYPE_NOTATION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002367 case XML_DOCUMENT_TYPE_NODE:
2368 case XML_DTD_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002369 return(XML_READER_TYPE_DOCUMENT_TYPE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002370
2371 case XML_ELEMENT_DECL:
2372 case XML_ATTRIBUTE_DECL:
2373 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002374 case XML_XINCLUDE_START:
2375 case XML_XINCLUDE_END:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002376 return(XML_READER_TYPE_NONE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002377 }
2378 return(-1);
2379}
2380
2381/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002382 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002383 * @reader: the xmlTextReaderPtr used
2384 *
2385 * Check if the current node is empty
2386 *
2387 * Returns 1 if empty, 0 if not and -1 in case of error
2388 */
2389int
2390xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
2391 if ((reader == NULL) || (reader->node == NULL))
2392 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00002393 if (reader->node->type != XML_ELEMENT_NODE)
2394 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00002395 if (reader->curnode != NULL)
2396 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002397 if (reader->node->children != NULL)
2398 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00002399 if (reader->state == XML_TEXTREADER_END)
2400 return(0);
Daniel Veillard067bae52003-01-05 01:27:54 +00002401 return(reader->node->_private == (void *)xmlTextReaderIsEmpty);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002402}
2403
2404/**
2405 * xmlTextReaderLocalName:
2406 * @reader: the xmlTextReaderPtr used
2407 *
2408 * The local name of the node.
2409 *
2410 * Returns the local name or NULL if not available
2411 */
2412xmlChar *
2413xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002414 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002415 if ((reader == NULL) || (reader->node == NULL))
2416 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002417 if (reader->curnode != NULL)
2418 node = reader->curnode;
2419 else
2420 node = reader->node;
2421 if (node->type == XML_NAMESPACE_DECL) {
2422 xmlNsPtr ns = (xmlNsPtr) node;
2423 if (ns->prefix == NULL)
2424 return(xmlStrdup(BAD_CAST "xmlns"));
2425 else
2426 return(xmlStrdup(ns->prefix));
2427 }
2428 if ((node->type != XML_ELEMENT_NODE) &&
2429 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002430 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002431 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002432}
2433
2434/**
2435 * xmlTextReaderName:
2436 * @reader: the xmlTextReaderPtr used
2437 *
2438 * The qualified name of the node, equal to Prefix :LocalName.
2439 *
2440 * Returns the local name or NULL if not available
2441 */
2442xmlChar *
2443xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002444 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002445 xmlChar *ret;
2446
2447 if ((reader == NULL) || (reader->node == NULL))
2448 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002449 if (reader->curnode != NULL)
2450 node = reader->curnode;
2451 else
2452 node = reader->node;
2453 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002454 case XML_ELEMENT_NODE:
2455 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002456 if ((node->ns == NULL) ||
2457 (node->ns->prefix == NULL))
2458 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002459
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002460 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002461 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002462 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002463 return(ret);
2464 case XML_TEXT_NODE:
2465 return(xmlStrdup(BAD_CAST "#text"));
2466 case XML_CDATA_SECTION_NODE:
2467 return(xmlStrdup(BAD_CAST "#cdata-section"));
2468 case XML_ENTITY_NODE:
2469 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002470 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002471 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002472 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002473 case XML_COMMENT_NODE:
2474 return(xmlStrdup(BAD_CAST "#comment"));
2475 case XML_DOCUMENT_NODE:
2476 case XML_HTML_DOCUMENT_NODE:
2477#ifdef LIBXML_DOCB_ENABLED
2478 case XML_DOCB_DOCUMENT_NODE:
2479#endif
2480 return(xmlStrdup(BAD_CAST "#document"));
2481 case XML_DOCUMENT_FRAG_NODE:
2482 return(xmlStrdup(BAD_CAST "#document-fragment"));
2483 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002484 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002485 case XML_DOCUMENT_TYPE_NODE:
2486 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002487 return(xmlStrdup(node->name));
2488 case XML_NAMESPACE_DECL: {
2489 xmlNsPtr ns = (xmlNsPtr) node;
2490
2491 ret = xmlStrdup(BAD_CAST "xmlns");
2492 if (ns->prefix == NULL)
2493 return(ret);
2494 ret = xmlStrcat(ret, BAD_CAST ":");
2495 ret = xmlStrcat(ret, ns->prefix);
2496 return(ret);
2497 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002498
2499 case XML_ELEMENT_DECL:
2500 case XML_ATTRIBUTE_DECL:
2501 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002502 case XML_XINCLUDE_START:
2503 case XML_XINCLUDE_END:
2504 return(NULL);
2505 }
2506 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002507}
2508
2509/**
2510 * xmlTextReaderPrefix:
2511 * @reader: the xmlTextReaderPtr used
2512 *
2513 * A shorthand reference to the namespace associated with the node.
2514 *
2515 * Returns the prefix or NULL if not available
2516 */
2517xmlChar *
2518xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002519 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002520 if ((reader == NULL) || (reader->node == NULL))
2521 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002522 if (reader->curnode != NULL)
2523 node = reader->curnode;
2524 else
2525 node = reader->node;
2526 if (node->type == XML_NAMESPACE_DECL) {
2527 xmlNsPtr ns = (xmlNsPtr) node;
2528 if (ns->prefix == NULL)
2529 return(NULL);
2530 return(xmlStrdup(BAD_CAST "xmlns"));
2531 }
2532 if ((node->type != XML_ELEMENT_NODE) &&
2533 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002534 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00002535 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002536 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002537 return(NULL);
2538}
2539
2540/**
2541 * xmlTextReaderNamespaceUri:
2542 * @reader: the xmlTextReaderPtr used
2543 *
2544 * The URI defining the namespace associated with the node.
2545 *
2546 * Returns the namespace URI or NULL if not available
2547 */
2548xmlChar *
2549xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002550 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002551 if ((reader == NULL) || (reader->node == NULL))
2552 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002553 if (reader->curnode != NULL)
2554 node = reader->curnode;
2555 else
2556 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00002557 if (node->type == XML_NAMESPACE_DECL)
2558 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002559 if ((node->type != XML_ELEMENT_NODE) &&
2560 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002561 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002562 if (node->ns != NULL)
2563 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002564 return(NULL);
2565}
2566
2567/**
2568 * xmlTextReaderBaseUri:
2569 * @reader: the xmlTextReaderPtr used
2570 *
2571 * The base URI of the node.
2572 *
2573 * Returns the base URI or NULL if not available
2574 */
2575xmlChar *
2576xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
2577 if ((reader == NULL) || (reader->node == NULL))
2578 return(NULL);
2579 return(xmlNodeGetBase(NULL, reader->node));
2580}
2581
2582/**
2583 * xmlTextReaderDepth:
2584 * @reader: the xmlTextReaderPtr used
2585 *
2586 * The depth of the node in the tree.
2587 *
2588 * Returns the depth or -1 in case of error
2589 */
2590int
2591xmlTextReaderDepth(xmlTextReaderPtr reader) {
2592 if (reader == NULL)
2593 return(-1);
2594 if (reader->node == NULL)
2595 return(0);
2596
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002597 if (reader->curnode != NULL) {
2598 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
2599 (reader->curnode->type == XML_NAMESPACE_DECL))
2600 return(reader->depth + 1);
2601 return(reader->depth + 2);
2602 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002603 return(reader->depth);
2604}
2605
2606/**
2607 * xmlTextReaderHasAttributes:
2608 * @reader: the xmlTextReaderPtr used
2609 *
2610 * Whether the node has attributes.
2611 *
2612 * Returns 1 if true, 0 if false, and -1 in case or error
2613 */
2614int
2615xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002616 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002617 if (reader == NULL)
2618 return(-1);
2619 if (reader->node == NULL)
2620 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002621 if (reader->curnode != NULL)
2622 node = reader->curnode;
2623 else
2624 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002625
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002626 if ((node->type == XML_ELEMENT_NODE) &&
2627 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002628 return(1);
2629 /* TODO: handle the xmlDecl */
2630 return(0);
2631}
2632
2633/**
2634 * xmlTextReaderHasValue:
2635 * @reader: the xmlTextReaderPtr used
2636 *
2637 * Whether the node can have a text value.
2638 *
2639 * Returns 1 if true, 0 if false, and -1 in case or error
2640 */
2641int
2642xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002643 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002644 if (reader == NULL)
2645 return(-1);
2646 if (reader->node == NULL)
2647 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002648 if (reader->curnode != NULL)
2649 node = reader->curnode;
2650 else
2651 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002652
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002653 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002654 case XML_ATTRIBUTE_NODE:
2655 case XML_TEXT_NODE:
2656 case XML_CDATA_SECTION_NODE:
2657 case XML_PI_NODE:
2658 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00002659 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002660 return(1);
2661 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002662 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002663 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002664 return(0);
2665}
2666
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002667/**
2668 * xmlTextReaderValue:
2669 * @reader: the xmlTextReaderPtr used
2670 *
2671 * Provides the text value of the node if present
2672 *
2673 * Returns the string or NULL if not available. The retsult must be deallocated
2674 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002675 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002676xmlChar *
2677xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002678 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002679 if (reader == NULL)
2680 return(NULL);
2681 if (reader->node == NULL)
2682 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002683 if (reader->curnode != NULL)
2684 node = reader->curnode;
2685 else
2686 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002687
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002688 switch (node->type) {
2689 case XML_NAMESPACE_DECL:
2690 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002691 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002692 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002693
2694 if (attr->parent != NULL)
2695 return (xmlNodeListGetString
2696 (attr->parent->doc, attr->children, 1));
2697 else
2698 return (xmlNodeListGetString(NULL, attr->children, 1));
2699 break;
2700 }
2701 case XML_TEXT_NODE:
2702 case XML_CDATA_SECTION_NODE:
2703 case XML_PI_NODE:
2704 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002705 if (node->content != NULL)
2706 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002707 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002708 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002709 }
2710 return(NULL);
2711}
2712
2713/**
2714 * xmlTextReaderIsDefault:
2715 * @reader: the xmlTextReaderPtr used
2716 *
2717 * Whether an Attribute node was generated from the default value
2718 * defined in the DTD or schema.
2719 *
2720 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
2721 */
2722int
2723xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
2724 if (reader == NULL)
2725 return(-1);
2726 return(0);
2727}
2728
2729/**
2730 * xmlTextReaderQuoteChar:
2731 * @reader: the xmlTextReaderPtr used
2732 *
2733 * The quotation mark character used to enclose the value of an attribute.
2734 *
2735 * Returns " or ' and -1 in case of error
2736 */
2737int
2738xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
2739 if (reader == NULL)
2740 return(-1);
2741 /* TODO maybe lookup the attribute value for " first */
2742 return((int) '"');
2743}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002744
2745/**
2746 * xmlTextReaderXmlLang:
2747 * @reader: the xmlTextReaderPtr used
2748 *
2749 * The xml:lang scope within which the node resides.
2750 *
2751 * Returns the xml:lang value or NULL if none exists.
2752 */
2753xmlChar *
2754xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
2755 if (reader == NULL)
2756 return(NULL);
2757 if (reader->node == NULL)
2758 return(NULL);
2759 return(xmlNodeGetLang(reader->node));
2760}
2761
Daniel Veillard67df8092002-12-16 22:04:11 +00002762/**
2763 * xmlTextReaderNormalization:
2764 * @reader: the xmlTextReaderPtr used
2765 *
2766 * The value indicating whether to normalize white space and attribute values.
2767 * Since attribute value and end of line normalizations are a MUST in the XML
2768 * specification only the value true is accepted. The broken bahaviour of
2769 * accepting out of range character entities like &#0; is of course not
2770 * supported either.
2771 *
2772 * Returns 1 or -1 in case of error.
2773 */
2774int
2775xmlTextReaderNormalization(xmlTextReaderPtr reader) {
2776 if (reader == NULL)
2777 return(-1);
2778 return(1);
2779}
2780
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002781/************************************************************************
2782 * *
2783 * Extensions to the base APIs *
2784 * *
2785 ************************************************************************/
2786
2787/**
2788 * xmlTextReaderSetParserProp:
2789 * @reader: the xmlTextReaderPtr used
2790 * @prop: the xmlParserProperties to set
2791 * @value: usually 0 or 1 to (de)activate it
2792 *
2793 * Change the parser processing behaviour by changing some of its internal
2794 * properties. Note that some properties can only be changed before any
2795 * read has been done.
2796 *
2797 * Returns 0 if the call was successful, or -1 in case of error
2798 */
2799int
2800xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
2801 xmlParserProperties p = (xmlParserProperties) prop;
2802 xmlParserCtxtPtr ctxt;
2803
2804 if ((reader == NULL) || (reader->ctxt == NULL))
2805 return(-1);
2806 ctxt = reader->ctxt;
2807
2808 switch (p) {
2809 case XML_PARSER_LOADDTD:
2810 if (value != 0) {
2811 if (ctxt->loadsubset == 0) {
2812 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2813 return(-1);
2814 ctxt->loadsubset = XML_DETECT_IDS;
2815 }
2816 } else {
2817 ctxt->loadsubset = 0;
2818 }
2819 return(0);
2820 case XML_PARSER_DEFAULTATTRS:
2821 if (value != 0) {
2822 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
2823 } else {
2824 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
2825 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
2826 }
2827 return(0);
2828 case XML_PARSER_VALIDATE:
2829 if (value != 0) {
2830 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00002831 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002832 } else {
2833 ctxt->validate = 0;
2834 }
2835 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00002836 case XML_PARSER_SUBST_ENTITIES:
2837 if (value != 0) {
2838 ctxt->replaceEntities = 1;
2839 } else {
2840 ctxt->replaceEntities = 0;
2841 }
2842 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002843 }
2844 return(-1);
2845}
2846
2847/**
2848 * xmlTextReaderGetParserProp:
2849 * @reader: the xmlTextReaderPtr used
2850 * @prop: the xmlParserProperties to get
2851 *
2852 * Read the parser internal property.
2853 *
2854 * Returns the value, usually 0 or 1, or -1 in case of error.
2855 */
2856int
2857xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
2858 xmlParserProperties p = (xmlParserProperties) prop;
2859 xmlParserCtxtPtr ctxt;
2860
2861 if ((reader == NULL) || (reader->ctxt == NULL))
2862 return(-1);
2863 ctxt = reader->ctxt;
2864
2865 switch (p) {
2866 case XML_PARSER_LOADDTD:
2867 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
2868 return(1);
2869 return(0);
2870 case XML_PARSER_DEFAULTATTRS:
2871 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
2872 return(1);
2873 return(0);
2874 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00002875 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00002876 case XML_PARSER_SUBST_ENTITIES:
2877 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002878 }
2879 return(-1);
2880}
2881
Daniel Veillarde18fc182002-12-28 22:56:33 +00002882/**
2883 * xmlTextReaderCurrentNode:
2884 * @reader: the xmlTextReaderPtr used
2885 *
2886 * Hacking interface allowing to get the xmlNodePtr correponding to the
2887 * current node being accessed by the xmlTextReader. This is dangerous
2888 * because the underlying node may be destroyed on the next Reads.
2889 *
2890 * Returns the xmlNodePtr or NULL in case of error.
2891 */
2892xmlNodePtr
2893xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
2894 if (reader == NULL)
2895 return(NULL);
2896
2897 if (reader->curnode != NULL)
2898 return(reader->curnode);
2899 return(reader->node);
2900}
2901
2902/**
2903 * xmlTextReaderCurrentDoc:
2904 * @reader: the xmlTextReaderPtr used
2905 *
2906 * Hacking interface allowing to get the xmlDocPtr correponding to the
2907 * current document being accessed by the xmlTextReader. This is dangerous
2908 * because the associated node may be destroyed on the next Reads.
2909 *
2910 * Returns the xmlDocPtr or NULL in case of error.
2911 */
2912xmlDocPtr
2913xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
2914 if ((reader == NULL) || (reader->ctxt == NULL))
2915 return(NULL);
2916
2917 return(reader->ctxt->myDoc);
2918}
2919
Daniel Veillard37fc84d2003-05-09 19:38:15 +00002920#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00002921/**
Daniel Veillard33300b42003-04-17 09:09:19 +00002922 * xmlTextReaderRelaxNGSetSchema:
2923 * @reader: the xmlTextReaderPtr used
2924 * @schema: a precompiled RelaxNG schema
2925 *
2926 * Use RelaxNG to validate the document as it is processed.
2927 * Activation is only possible before the first Read().
2928 * if @schema is NULL, then RelaxNG validation is desactivated.
2929 @ The @schema should not be freed until the reader is deallocated
2930 * or its use has been deactivated.
2931 *
2932 * Returns 0 in case the RelaxNG validation could be (des)activated and
2933 * -1 in case of error.
2934 */
2935int
2936xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
2937 if (schema == NULL) {
2938 if (reader->rngSchemas != NULL) {
2939 xmlRelaxNGFree(reader->rngSchemas);
2940 reader->rngSchemas = NULL;
2941 }
2942 if (reader->rngValidCtxt != NULL) {
2943 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2944 reader->rngValidCtxt = NULL;
2945 }
2946 return(0);
2947 }
2948 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2949 return(-1);
2950 if (reader->rngSchemas != NULL) {
2951 xmlRelaxNGFree(reader->rngSchemas);
2952 reader->rngSchemas = NULL;
2953 }
2954 if (reader->rngValidCtxt != NULL) {
2955 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2956 reader->rngValidCtxt = NULL;
2957 }
2958 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
2959 if (reader->rngValidCtxt == NULL)
2960 return(-1);
2961 if (reader->errorFunc != NULL) {
2962 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
2963 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
2964 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
2965 reader->errorFuncArg);
2966 }
2967 reader->rngValidErrors = 0;
2968 reader->rngFullNode = NULL;
2969 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
2970 return(0);
2971}
2972
2973/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00002974 * xmlTextReaderRelaxNGValidate:
2975 * @reader: the xmlTextReaderPtr used
2976 * @rng: the path to a RelaxNG schema or NULL
2977 *
2978 * Use RelaxNG to validate the document as it is processed.
2979 * Activation is only possible before the first Read().
2980 * if @rng is NULL, then RelaxNG validation is desactivated.
2981 *
2982 * Returns 0 in case the RelaxNG validation could be (des)activated and
2983 * -1 in case of error.
2984 */
2985int
2986xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
2987 xmlRelaxNGParserCtxtPtr ctxt;
2988
2989 if (reader == NULL)
2990 return(-1);
2991
2992 if (rng == NULL) {
2993 if (reader->rngSchemas != NULL) {
2994 xmlRelaxNGFree(reader->rngSchemas);
2995 reader->rngSchemas = NULL;
2996 }
2997 if (reader->rngValidCtxt != NULL) {
2998 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2999 reader->rngValidCtxt = NULL;
3000 }
3001 return(0);
3002 }
3003 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3004 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00003005 if (reader->rngSchemas != NULL) {
3006 xmlRelaxNGFree(reader->rngSchemas);
3007 reader->rngSchemas = NULL;
3008 }
3009 if (reader->rngValidCtxt != NULL) {
3010 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3011 reader->rngValidCtxt = NULL;
3012 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00003013 ctxt = xmlRelaxNGNewParserCtxt(rng);
3014 if (reader->errorFunc != NULL) {
3015 xmlRelaxNGSetParserErrors(ctxt,
3016 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
3017 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3018 reader->errorFuncArg);
3019 }
3020 reader->rngSchemas = xmlRelaxNGParse(ctxt);
3021 xmlRelaxNGFreeParserCtxt(ctxt);
3022 if (reader->rngSchemas == NULL)
3023 return(-1);
3024 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
3025 if (reader->rngValidCtxt == NULL)
3026 return(-1);
3027 if (reader->errorFunc != NULL) {
3028 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3029 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3030 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3031 reader->errorFuncArg);
3032 }
3033 reader->rngValidErrors = 0;
3034 reader->rngFullNode = NULL;
3035 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3036 return(0);
3037}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003038#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00003039
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003040/************************************************************************
3041 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00003042 * Error Handling Extensions *
3043 * *
3044 ************************************************************************/
3045
3046/* helper to build a xmlMalloc'ed string from a format and va_list */
3047static char *
3048xmlTextReaderBuildMessage(const char *msg, va_list ap) {
3049 int size;
3050 int chars;
3051 char *larger;
3052 char *str;
3053
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003054 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00003055 if (str == NULL) {
3056 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3057 return NULL;
3058 }
3059
3060 size = 150;
3061
3062 while (1) {
3063 chars = vsnprintf(str, size, msg, ap);
3064 if ((chars > -1) && (chars < size))
3065 break;
3066 if (chars > -1)
3067 size += chars + 1;
3068 else
3069 size += 100;
3070 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
3071 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3072 xmlFree(str);
3073 return NULL;
3074 }
3075 str = larger;
3076 }
3077
3078 return str;
3079}
3080
Daniel Veillard417be3a2003-01-20 21:26:34 +00003081/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003082 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003083 * @locator: the xmlTextReaderLocatorPtr used
3084 *
3085 * Obtain the line number for the given locator.
3086 *
3087 * Returns the line number or -1 in case of error.
3088 */
3089int
3090xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
3091 /* we know that locator is a xmlParserCtxtPtr */
3092 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3093 int ret = -1;
3094
3095 if (ctx->node != NULL) {
3096 ret = xmlGetLineNo(ctx->node);
3097 }
3098 else {
3099 /* inspired from error.c */
3100 xmlParserInputPtr input;
3101 input = ctx->input;
3102 if ((input->filename == NULL) && (ctx->inputNr > 1))
3103 input = ctx->inputTab[ctx->inputNr - 2];
3104 if (input != NULL) {
3105 ret = input->line;
3106 }
3107 else {
3108 ret = -1;
3109 }
3110 }
3111
3112 return ret;
3113}
3114
3115/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003116 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003117 * @locator: the xmlTextReaderLocatorPtr used
3118 *
3119 * Obtain the base URI for the given locator.
3120 *
3121 * Returns the base URI or NULL in case of error.
3122 */
3123xmlChar *
3124xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
3125 /* we know that locator is a xmlParserCtxtPtr */
3126 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3127 xmlChar *ret = NULL;
3128
3129 if (ctx->node != NULL) {
3130 ret = xmlNodeGetBase(NULL,ctx->node);
3131 }
3132 else {
3133 /* inspired from error.c */
3134 xmlParserInputPtr input;
3135 input = ctx->input;
3136 if ((input->filename == NULL) && (ctx->inputNr > 1))
3137 input = ctx->inputTab[ctx->inputNr - 2];
3138 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00003139 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00003140 }
3141 else {
3142 ret = NULL;
3143 }
3144 }
3145
3146 return ret;
3147}
3148
Daniel Veillard26f70262003-01-16 22:45:08 +00003149static void
3150xmlTextReaderGenericError(void *ctxt, int severity, char *str) {
3151 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
3152 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
3153
3154 if (str != NULL) {
3155 reader->errorFunc(reader->errorFuncArg,
3156 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003157 severity,
3158 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00003159 xmlFree(str);
3160 }
3161}
3162
3163static void
3164xmlTextReaderError(void *ctxt, const char *msg, ...) {
3165 va_list ap;
3166
3167 va_start(ap,msg);
3168 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003169 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00003170 xmlTextReaderBuildMessage(msg,ap));
3171 va_end(ap);
3172
3173}
3174
3175static void
3176xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
3177 va_list ap;
3178
3179 va_start(ap,msg);
3180 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003181 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00003182 xmlTextReaderBuildMessage(msg,ap));
3183 va_end(ap);
3184}
3185
3186static void
3187xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
3188 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003189 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003190
Daniel Veillard417be3a2003-01-20 21:26:34 +00003191 if ((len > 1) && (msg[len - 2] != ':')) {
3192 /*
3193 * some callbacks only report locator information:
3194 * skip them (mimicking behaviour in error.c)
3195 */
3196 va_start(ap,msg);
3197 xmlTextReaderGenericError(ctxt,
3198 XML_PARSER_SEVERITY_VALIDITY_ERROR,
3199 xmlTextReaderBuildMessage(msg,ap));
3200 va_end(ap);
3201 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003202}
3203
3204static void
3205xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
3206 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003207 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003208
Daniel Veillard417be3a2003-01-20 21:26:34 +00003209 if ((len != 0) && (msg[len - 1] != ':')) {
3210 /*
3211 * some callbacks only report locator information:
3212 * skip them (mimicking behaviour in error.c)
3213 */
3214 va_start(ap,msg);
3215 xmlTextReaderGenericError(ctxt,
3216 XML_PARSER_SEVERITY_VALIDITY_WARNING,
3217 xmlTextReaderBuildMessage(msg,ap));
3218 va_end(ap);
3219 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003220}
3221
3222/**
3223 * xmlTextReaderSetErrorHandler:
3224 * @reader: the xmlTextReaderPtr used
3225 * @f: the callback function to call on error and warnings
3226 * @arg: a user argument to pass to the callback function
3227 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00003228 * Register a callback function that will be called on error and warnings.
3229 *
Daniel Veillard26f70262003-01-16 22:45:08 +00003230 * If @f is NULL, the default error and warning handlers are restored.
3231 */
3232void
3233xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
3234 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003235 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003236 if (f != NULL) {
3237 reader->ctxt->sax->error = xmlTextReaderError;
3238 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
3239 reader->ctxt->sax->warning = xmlTextReaderWarning;
3240 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
3241 reader->errorFunc = f;
3242 reader->errorFuncArg = arg;
3243 }
3244 else {
3245 /* restore defaults */
3246 reader->ctxt->sax->error = xmlParserError;
3247 reader->ctxt->vctxt.error = xmlParserValidityError;
3248 reader->ctxt->sax->warning = xmlParserWarning;
3249 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
3250 reader->errorFunc = NULL;
3251 reader->errorFuncArg = NULL;
3252 }
3253}
3254
Daniel Veillard417be3a2003-01-20 21:26:34 +00003255/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00003256 * xmlTextReaderIsValid:
3257 * @reader: the xmlTextReaderPtr used
3258 *
3259 * Retrieve the validity status from the parser context
3260 *
3261 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
3262 */
3263int
3264xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00003265 if (reader == NULL) return(-1);
3266#ifdef LIBXML_SCHEMAS_ENABLED
3267 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
3268 return(reader->rngValidErrors == 0);
3269#endif
3270 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
3271 (reader->ctxt != NULL))
3272 return(reader->ctxt->valid);
3273 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00003274}
3275
3276/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00003277 * xmlTextReaderGetErrorHandler:
3278 * @reader: the xmlTextReaderPtr used
3279 * @f: the callback function or NULL is no callback has been registered
3280 * @arg: a user argument
3281 *
3282 * Retrieve the error callback function and user argument.
3283 */
Daniel Veillard26f70262003-01-16 22:45:08 +00003284void
3285xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
3286 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003287 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003288 *f = reader->errorFunc;
3289 *arg = reader->errorFuncArg;
3290}
3291
3292/************************************************************************
3293 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003294 * Utilities *
3295 * *
3296 ************************************************************************/
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00003297#ifdef NOT_USED_YET
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003298/**
3299 * xmlBase64Decode:
3300 * @in: the input buffer
3301 * @inlen: the size of the input (in), the size read from it (out)
3302 * @to: the output buffer
3303 * @tolen: the size of the output (in), the size written to (out)
3304 *
3305 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00003306 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003307 *
3308 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
3309 * 2 if there wasn't enough space on the output or -1 in case of error.
3310 */
3311static int
3312xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
3313 unsigned char *to, unsigned long *tolen) {
3314 unsigned long incur; /* current index in in[] */
3315 unsigned long inblk; /* last block index in in[] */
3316 unsigned long outcur; /* current index in out[] */
3317 unsigned long inmax; /* size of in[] */
3318 unsigned long outmax; /* size of out[] */
3319 unsigned char cur; /* the current value read from in[] */
Daniel Veillardc127adc2003-07-23 15:07:08 +00003320 unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003321 int nbintmp; /* number of byte in intmp[] */
3322 int is_ignore; /* cur should be ignored */
3323 int is_end = 0; /* the end of the base64 was found */
3324 int retval = 1;
3325 int i;
3326
3327 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
3328 return(-1);
3329
3330 incur = 0;
3331 inblk = 0;
3332 outcur = 0;
3333 inmax = *inlen;
3334 outmax = *tolen;
3335 nbintmp = 0;
3336
3337 while (1) {
3338 if (incur >= inmax)
3339 break;
3340 cur = in[incur++];
3341 is_ignore = 0;
3342 if ((cur >= 'A') && (cur <= 'Z'))
3343 cur = cur - 'A';
3344 else if ((cur >= 'a') && (cur <= 'z'))
3345 cur = cur - 'a' + 26;
3346 else if ((cur >= '0') && (cur <= '9'))
3347 cur = cur - '0' + 52;
3348 else if (cur == '+')
3349 cur = 62;
3350 else if (cur == '/')
3351 cur = 63;
3352 else if (cur == '.')
3353 cur = 0;
3354 else if (cur == '=') /*no op , end of the base64 stream */
3355 is_end = 1;
3356 else {
3357 is_ignore = 1;
3358 if (nbintmp == 0)
3359 inblk = incur;
3360 }
3361
3362 if (!is_ignore) {
3363 int nbouttmp = 3;
3364 int is_break = 0;
3365
3366 if (is_end) {
3367 if (nbintmp == 0)
3368 break;
3369 if ((nbintmp == 1) || (nbintmp == 2))
3370 nbouttmp = 1;
3371 else
3372 nbouttmp = 2;
3373 nbintmp = 3;
3374 is_break = 1;
3375 }
3376 intmp[nbintmp++] = cur;
3377 /*
3378 * if intmp is full, push the 4byte sequence as a 3 byte
3379 * sequence out
3380 */
3381 if (nbintmp == 4) {
3382 nbintmp = 0;
3383 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
3384 outtmp[1] =
3385 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
3386 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
3387 if (outcur + 3 >= outmax) {
3388 retval = 2;
3389 break;
3390 }
3391
3392 for (i = 0; i < nbouttmp; i++)
3393 to[outcur++] = outtmp[i];
3394 inblk = incur;
3395 }
3396
3397 if (is_break) {
3398 retval = 0;
3399 break;
3400 }
3401 }
3402 }
3403
3404 *tolen = outcur;
3405 *inlen = inblk;
3406 return (retval);
3407}
3408
3409/*
3410 * Test routine for the xmlBase64Decode function
3411 */
3412#if 0
3413int main(int argc, char **argv) {
3414 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
3415 char output[100];
3416 char output2[100];
3417 char output3[100];
3418 unsigned long inlen = strlen(input);
3419 unsigned long outlen = 100;
3420 int ret;
3421 unsigned long cons, tmp, tmp2, prod;
3422
3423 /*
3424 * Direct
3425 */
3426 ret = xmlBase64Decode(input, &inlen, output, &outlen);
3427
3428 output[outlen] = 0;
3429 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
3430
3431 /*
3432 * output chunking
3433 */
3434 cons = 0;
3435 prod = 0;
3436 while (cons < inlen) {
3437 tmp = 5;
3438 tmp2 = inlen - cons;
3439
3440 printf("%ld %ld\n", cons, prod);
3441 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
3442 cons += tmp2;
3443 prod += tmp;
3444 printf("%ld %ld\n", cons, prod);
3445 }
3446 output2[outlen] = 0;
3447 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
3448
3449 /*
3450 * input chunking
3451 */
3452 cons = 0;
3453 prod = 0;
3454 while (cons < inlen) {
3455 tmp = 100 - prod;
3456 tmp2 = inlen - cons;
3457 if (tmp2 > 5)
3458 tmp2 = 5;
3459
3460 printf("%ld %ld\n", cons, prod);
3461 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
3462 cons += tmp2;
3463 prod += tmp;
3464 printf("%ld %ld\n", cons, prod);
3465 }
3466 output3[outlen] = 0;
3467 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
3468 return(0);
3469
3470}
3471#endif
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00003472#endif /* NOT_USED_YET */