blob: 2900a4c82ab7b9d84f5ed120928908265541d537 [file] [log] [blame]
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001/*
2 * xmlreader.c: implements the xmlTextReader streaming node API
3 *
Daniel Veillard67df8092002-12-16 22:04:11 +00004 * NOTE:
5 * XmlTextReader.Normalization Property won't be supported, since
6 * it makes the parser non compliant to the XML recommendation
7 *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00008 * See Copyright for the status of this software.
9 *
10 * daniel@veillard.com
11 */
12
Daniel Veillard7704fb12003-01-03 16:19:51 +000013/*
14 * TODOs:
Daniel Veillard7704fb12003-01-03 16:19:51 +000015 * - provide an API to preserve part of the tree
16 * - Streaming XInclude support
Daniel Veillard067bae52003-01-05 01:27:54 +000017 * - XML Schemas validation
Daniel Veillard7704fb12003-01-03 16:19:51 +000018 * - setting(s) for NoBlanks
19 * - performances and tuning ...
20 */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000021#define IN_LIBXML
22#include "libxml.h"
23
24#include <string.h> /* for memset() only ! */
Daniel Veillard26f70262003-01-16 22:45:08 +000025#include <stdarg.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000026
27#ifdef HAVE_CTYPE_H
28#include <ctype.h>
29#endif
30#ifdef HAVE_STDLIB_H
31#include <stdlib.h>
32#endif
33
34#include <libxml/xmlmemory.h>
35#include <libxml/xmlIO.h>
36#include <libxml/xmlreader.h>
Daniel Veillardf4e55762003-04-15 23:32:22 +000037#include <libxml/relaxng.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000038
39/* #define DEBUG_CALLBACKS */
40/* #define DEBUG_READER */
41
42/**
43 * TODO:
44 *
45 * macro to flag unimplemented blocks
46 */
47#define TODO \
48 xmlGenericError(xmlGenericErrorContext, \
49 "Unimplemented block at %s:%d\n", \
50 __FILE__, __LINE__);
51
52#ifdef DEBUG_READER
53#define DUMP_READER xmlTextReaderDebug(reader);
54#else
55#define DUMP_READER
56#endif
57
Daniel Veillarda880b122003-04-21 21:36:41 +000058#define CHUNK_SIZE 512
Daniel Veillarde1ca5032002-12-09 14:13:43 +000059/************************************************************************
60 * *
61 * The parser: maps the Text Reader API on top of the existing *
62 * parsing routines building a tree *
63 * *
64 ************************************************************************/
65
66#define XML_TEXTREADER_INPUT 1
67#define XML_TEXTREADER_CTXT 2
68
69typedef enum {
Daniel Veillard67df8092002-12-16 22:04:11 +000070 XML_TEXTREADER_MODE_INITIAL = 0,
71 XML_TEXTREADER_MODE_INTERACTIVE = 1,
72 XML_TEXTREADER_MODE_ERROR = 2,
73 XML_TEXTREADER_MODE_EOF =3,
74 XML_TEXTREADER_MODE_CLOSED = 4,
75 XML_TEXTREADER_MODE_READING = 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000076} xmlTextReaderMode;
77
78typedef enum {
79 XML_TEXTREADER_NONE = -1,
80 XML_TEXTREADER_START= 0,
81 XML_TEXTREADER_ELEMENT= 1,
82 XML_TEXTREADER_END= 2,
83 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000084 XML_TEXTREADER_BACKTRACK= 4,
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000085 XML_TEXTREADER_DONE= 5,
86 XML_TEXTREADER_ERROR= 6
Daniel Veillarde1ca5032002-12-09 14:13:43 +000087} xmlTextReaderState;
88
Daniel Veillardf4e55762003-04-15 23:32:22 +000089typedef enum {
90 XML_TEXTREADER_NOT_VALIDATE = 0,
91 XML_TEXTREADER_VALIDATE_DTD = 1,
92 XML_TEXTREADER_VALIDATE_RNG = 2
93} xmlTextReaderValidate;
94
Daniel Veillarde1ca5032002-12-09 14:13:43 +000095struct _xmlTextReader {
96 int mode; /* the parsing mode */
Daniel Veillardf4e55762003-04-15 23:32:22 +000097 xmlTextReaderValidate validate;/* is there any validation */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000098 int allocs; /* what structure were deallocated */
99 xmlTextReaderState state;
100 xmlParserCtxtPtr ctxt; /* the parser context */
101 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
102 xmlParserInputBufferPtr input; /* the input */
103 startElementSAXFunc startElement;/* initial SAX callbacks */
104 endElementSAXFunc endElement; /* idem */
Daniel Veillard07cb8222003-09-10 10:51:05 +0000105 startElementNsSAX2Func startElementNs;/* idem */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000106 endElementNsSAX2Func endElementNs; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000107 charactersSAXFunc characters;
108 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000109 unsigned int base; /* base of the segment in the input */
110 unsigned int cur; /* current position in the input */
111 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000112 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000113 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000114 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000115 int preserve;/* preserve the resulting document */
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";
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000137static const char *xmlTextReaderIsEmptyPreserved = "Preserve this element";
138static const char *xmlTextReaderIsPreserved = "Preserve this element";
Daniel Veillard067bae52003-01-05 01:27:54 +0000139
Daniel Veillarde72c5082003-09-19 12:44:05 +0000140/**
141 * CONSTSTR:
142 *
143 * Macro used to return an interned string
144 */
145#define CONSTSTR(str) xmlDictLookup(reader->ctxt->dict, (str), -1)
146#define CONSTQSTR(p, str) xmlDictQLookup(reader->ctxt->dict, (p), (str))
147
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000148/************************************************************************
149 * *
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000150 * Our own version of the freeing routines as we recycle nodes *
151 * *
152 ************************************************************************/
153/**
154 * DICT_FREE:
155 * @str: a string
156 *
157 * Free a string if it is not owned by the "dict" dictionnary in the
158 * current scope
159 */
160#define DICT_FREE(str) \
161 if ((str) && ((!dict) || \
162 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
163 xmlFree((char *)(str));
164
165static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
166static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
167
168/**
169 * xmlTextReaderFreeProp:
170 * @reader: the xmlTextReaderPtr used
171 * @cur: the node
172 *
173 * Free a node.
174 */
175static void
176xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
177 xmlDictPtr dict;
178
179 dict = reader->ctxt->dict;
180 if (cur == NULL) return;
181
182 /* Check for ID removal -> leading to invalid references ! */
183 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
184 ((cur->parent->doc->intSubset != NULL) ||
185 (cur->parent->doc->extSubset != NULL))) {
186 if (xmlIsID(cur->parent->doc, cur->parent, cur))
187 xmlRemoveID(cur->parent->doc, cur);
188 }
189 if (cur->children != NULL)
190 xmlTextReaderFreeNodeList(reader, cur->children);
191
192 DICT_FREE(cur->name);
193 if ((reader != NULL) && (reader->ctxt != NULL) &&
194 (reader->ctxt->freeAttrsNr < 100)) {
195 cur->next = reader->ctxt->freeAttrs;
196 reader->ctxt->freeAttrs = cur;
197 reader->ctxt->freeAttrsNr++;
198 } else {
199 xmlFree(cur);
200 }
201}
202
203/**
204 * xmlTextReaderFreePropList:
205 * @reader: the xmlTextReaderPtr used
206 * @cur: the first property in the list
207 *
208 * Free a property and all its siblings, all the children are freed too.
209 */
210static void
211xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
212 xmlAttrPtr next;
213 if (cur == NULL) return;
214 while (cur != NULL) {
215 next = cur->next;
216 xmlTextReaderFreeProp(reader, cur);
217 cur = next;
218 }
219}
220
221/**
222 * xmlTextReaderFreeNodeList:
223 * @reader: the xmlTextReaderPtr used
224 * @cur: the first node in the list
225 *
226 * Free a node and all its siblings, this is a recursive behaviour, all
227 * the children are freed too.
228 */
229static void
230xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
231 xmlNodePtr next;
232 xmlDictPtr dict;
233
234 dict = reader->ctxt->dict;
235 if (cur == NULL) return;
236 if (cur->type == XML_NAMESPACE_DECL) {
237 xmlFreeNsList((xmlNsPtr) cur);
238 return;
239 }
240 if ((cur->type == XML_DOCUMENT_NODE) ||
241 (cur->type == XML_HTML_DOCUMENT_NODE)) {
242 xmlFreeDoc((xmlDocPtr) cur);
243 return;
244 }
245 while (cur != NULL) {
246 next = cur->next;
247 /* unroll to speed up freeing the document */
248 if (cur->type != XML_DTD_NODE) {
249
250 if ((cur->children != NULL) &&
251 (cur->type != XML_ENTITY_REF_NODE))
252 xmlTextReaderFreeNodeList(reader, cur->children);
253 if (((cur->type == XML_ELEMENT_NODE) ||
254 (cur->type == XML_XINCLUDE_START) ||
255 (cur->type == XML_XINCLUDE_END)) &&
256 (cur->properties != NULL))
257 xmlTextReaderFreePropList(reader, cur->properties);
258 if ((cur->type != XML_ELEMENT_NODE) &&
259 (cur->type != XML_XINCLUDE_START) &&
260 (cur->type != XML_XINCLUDE_END) &&
261 (cur->type != XML_ENTITY_REF_NODE)) {
262 DICT_FREE(cur->content);
263 }
264 if (((cur->type == XML_ELEMENT_NODE) ||
265 (cur->type == XML_XINCLUDE_START) ||
266 (cur->type == XML_XINCLUDE_END)) &&
267 (cur->nsDef != NULL))
268 xmlFreeNsList(cur->nsDef);
269
270 /*
271 * we don't free element names here they are interned now
272 */
273 if ((cur->type != XML_TEXT_NODE) &&
274 (cur->type != XML_COMMENT_NODE))
275 DICT_FREE(cur->name);
276 if (((cur->type == XML_ELEMENT_NODE) ||
277 (cur->type == XML_TEXT_NODE)) &&
278 (reader != NULL) && (reader->ctxt != NULL) &&
279 (reader->ctxt->freeElemsNr < 100)) {
280 cur->next = reader->ctxt->freeElems;
281 reader->ctxt->freeElems = cur;
282 reader->ctxt->freeElemsNr++;
283 } else {
284 xmlFree(cur);
285 }
286 }
287 cur = next;
288 }
289}
290
291/**
292 * xmlTextReaderFreeNode:
293 * @reader: the xmlTextReaderPtr used
294 * @cur: the node
295 *
296 * Free a node, this is a recursive behaviour, all the children are freed too.
297 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
298 */
299static void
300xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
301 xmlDictPtr dict;
302
303 dict = reader->ctxt->dict;
304 if (cur->type == XML_DTD_NODE) {
305 xmlFreeDtd((xmlDtdPtr) cur);
306 return;
307 }
308 if (cur->type == XML_NAMESPACE_DECL) {
309 xmlFreeNs((xmlNsPtr) cur);
310 return;
311 }
312 if (cur->type == XML_ATTRIBUTE_NODE) {
313 xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
314 return;
315 }
316
317 if ((cur->children != NULL) &&
318 (cur->type != XML_ENTITY_REF_NODE))
319 xmlTextReaderFreeNodeList(reader, cur->children);
320 if (((cur->type == XML_ELEMENT_NODE) ||
321 (cur->type == XML_XINCLUDE_START) ||
322 (cur->type == XML_XINCLUDE_END)) &&
323 (cur->properties != NULL))
324 xmlTextReaderFreePropList(reader, cur->properties);
325 if ((cur->type != XML_ELEMENT_NODE) &&
326 (cur->type != XML_XINCLUDE_START) &&
327 (cur->type != XML_XINCLUDE_END) &&
328 (cur->type != XML_ENTITY_REF_NODE)) {
329 DICT_FREE(cur->content);
330 }
331 if (((cur->type == XML_ELEMENT_NODE) ||
332 (cur->type == XML_XINCLUDE_START) ||
333 (cur->type == XML_XINCLUDE_END)) &&
334 (cur->nsDef != NULL))
335 xmlFreeNsList(cur->nsDef);
336
337 /*
338 * we don't free names here they are interned now
339 */
340 if ((cur->type != XML_TEXT_NODE) &&
341 (cur->type != XML_COMMENT_NODE))
342 DICT_FREE(cur->name);
343 if (((cur->type == XML_ELEMENT_NODE) ||
344 (cur->type == XML_TEXT_NODE)) &&
345 (reader != NULL) && (reader->ctxt != NULL) &&
346 (reader->ctxt->freeElemsNr < 100)) {
347 cur->next = reader->ctxt->freeElems;
348 reader->ctxt->freeElems = cur;
349 reader->ctxt->freeElemsNr++;
350 } else {
351 xmlFree(cur);
352 }
353}
354
355/**
356 * xmlTextReaderFreeDoc:
357 * @reader: the xmlTextReaderPtr used
358 * @cur: pointer to the document
359 *
360 * Free up all the structures used by a document, tree included.
361 */
362static void
363xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
364 xmlDtdPtr extSubset, intSubset;
365
366 if (cur == NULL) return;
367
368 /*
369 * Do this before freeing the children list to avoid ID lookups
370 */
371 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
372 cur->ids = NULL;
373 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
374 cur->refs = NULL;
375 extSubset = cur->extSubset;
376 intSubset = cur->intSubset;
377 if (intSubset == extSubset)
378 extSubset = NULL;
379 if (extSubset != NULL) {
380 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
381 cur->extSubset = NULL;
382 xmlFreeDtd(extSubset);
383 }
384 if (intSubset != NULL) {
385 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
386 cur->intSubset = NULL;
387 xmlFreeDtd(intSubset);
388 }
389
390 if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
391
392 if (cur->version != NULL) xmlFree((char *) cur->version);
393 if (cur->name != NULL) xmlFree((char *) cur->name);
394 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
395 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
396 if (cur->URL != NULL) xmlFree((char *) cur->URL);
397 xmlFree(cur);
398}
399
400/************************************************************************
401 * *
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000402 * The reader core parser *
403 * *
404 ************************************************************************/
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000405#ifdef DEBUG_READER
406static void
407xmlTextReaderDebug(xmlTextReaderPtr reader) {
408 if ((reader == NULL) || (reader->ctxt == NULL)) {
409 fprintf(stderr, "xmlTextReader NULL\n");
410 return;
411 }
412 fprintf(stderr, "xmlTextReader: state %d depth %d ",
413 reader->state, reader->depth);
414 if (reader->node == NULL) {
415 fprintf(stderr, "node = NULL\n");
416 } else {
417 fprintf(stderr, "node %s\n", reader->node->name);
418 }
419 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
420 reader->base, reader->cur, reader->ctxt->nodeNr);
421 if (reader->input->buffer == NULL) {
422 fprintf(stderr, "buffer is NULL\n");
423 } else {
424#ifdef LIBXML_DEBUG_ENABLED
425 xmlDebugDumpString(stderr,
426 &reader->input->buffer->content[reader->cur]);
427#endif
428 fprintf(stderr, "\n");
429 }
430}
431#endif
432
433/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000434 * xmlTextReaderEntPush:
435 * @reader: the xmlTextReaderPtr used
436 * @value: the entity reference node
437 *
438 * Pushes a new entity reference node on top of the entities stack
439 *
440 * Returns 0 in case of error, the index in the stack otherwise
441 */
442static int
443xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
444{
445 if (reader->entMax <= 0) {
446 reader->entMax = 10;
447 reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
448 sizeof(reader->entTab[0]));
449 if (reader->entTab == NULL) {
450 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
451 return (0);
452 }
453 }
454 if (reader->entNr >= reader->entMax) {
455 reader->entMax *= 2;
456 reader->entTab =
457 (xmlNodePtr *) xmlRealloc(reader->entTab,
458 reader->entMax *
459 sizeof(reader->entTab[0]));
460 if (reader->entTab == NULL) {
461 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
462 return (0);
463 }
464 }
465 reader->entTab[reader->entNr] = value;
466 reader->ent = value;
467 return (reader->entNr++);
468}
469
470/**
471 * xmlTextReaderEntPop:
472 * @reader: the xmlTextReaderPtr used
473 *
474 * Pops the top element entity from the entities stack
475 *
476 * Returns the entity just removed
477 */
478static xmlNodePtr
479xmlTextReaderEntPop(xmlTextReaderPtr reader)
480{
481 xmlNodePtr ret;
482
483 if (reader->entNr <= 0)
484 return (0);
485 reader->entNr--;
486 if (reader->entNr > 0)
487 reader->ent = reader->entTab[reader->entNr - 1];
488 else
489 reader->ent = NULL;
490 ret = reader->entTab[reader->entNr];
491 reader->entTab[reader->entNr] = 0;
492 return (ret);
493}
494
495/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000496 * xmlTextReaderStartElement:
497 * @ctx: the user data (XML parser context)
498 * @fullname: The element name, including namespace prefix
499 * @atts: An array of name/value attributes pairs, NULL terminated
500 *
501 * called when an opening tag has been processed.
502 */
503static void
504xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
505 const xmlChar **atts) {
506 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
507 xmlTextReaderPtr reader = ctxt->_private;
508
509#ifdef DEBUG_CALLBACKS
510 printf("xmlTextReaderStartElement(%s)\n", fullname);
511#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000512 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000513 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000514 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
515 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
516 (ctxt->input->cur[1] == '>'))
517 ctxt->node->_private = (void *) xmlTextReaderIsEmpty;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000518 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000519 if (reader != NULL)
520 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000521}
522
523/**
524 * xmlTextReaderEndElement:
525 * @ctx: the user data (XML parser context)
526 * @fullname: The element name, including namespace prefix
527 *
528 * called when an ending tag has been processed.
529 */
530static void
531xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
532 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
533 xmlTextReaderPtr reader = ctxt->_private;
534
535#ifdef DEBUG_CALLBACKS
536 printf("xmlTextReaderEndElement(%s)\n", fullname);
537#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000538 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000539 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000540 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000541}
542
543/**
Daniel Veillard07cb8222003-09-10 10:51:05 +0000544 * xmlTextReaderStartElementNs:
545 * @ctx: the user data (XML parser context)
546 * @localname: the local name of the element
547 * @prefix: the element namespace prefix if available
548 * @URI: the element namespace name if available
549 * @nb_namespaces: number of namespace definitions on that node
550 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
551 * @nb_attributes: the number of attributes on that node
552 * nb_defaulted: the number of defaulted attributes.
553 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
554 * attribute values.
555 *
556 * called when an opening tag has been processed.
557 */
558static void
559xmlTextReaderStartElementNs(void *ctx,
560 const xmlChar *localname,
561 const xmlChar *prefix,
562 const xmlChar *URI,
563 int nb_namespaces,
564 const xmlChar **namespaces,
565 int nb_attributes,
566 int nb_defaulted,
567 const xmlChar **attributes)
568{
569 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
570 xmlTextReaderPtr reader = ctxt->_private;
571
572#ifdef DEBUG_CALLBACKS
573 printf("xmlTextReaderStartElementNs(%s)\n", fullname);
574#endif
575 if ((reader != NULL) && (reader->startElementNs != NULL)) {
576 reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
577 namespaces, nb_attributes, nb_defaulted,
578 attributes);
579 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
580 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
581 (ctxt->input->cur[1] == '>'))
582 ctxt->node->_private = (void *) xmlTextReaderIsEmpty;
583 }
584 if (reader != NULL)
585 reader->state = XML_TEXTREADER_ELEMENT;
586}
587
588/**
589 * xmlTextReaderEndElementNs:
590 * @ctx: the user data (XML parser context)
591 * @localname: the local name of the element
592 * @prefix: the element namespace prefix if available
593 * @URI: the element namespace name if available
594 *
595 * called when an ending tag has been processed.
596 */
597static void
598xmlTextReaderEndElementNs(void *ctx,
599 const xmlChar * localname,
600 const xmlChar * prefix,
601 const xmlChar * URI)
602{
603 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
604 xmlTextReaderPtr reader = ctxt->_private;
605
606#ifdef DEBUG_CALLBACKS
607 printf("xmlTextReaderEndElementNs(%s)\n", fullname);
608#endif
609 if ((reader != NULL) && (reader->endElementNs != NULL)) {
610 reader->endElementNs(ctx, localname, prefix, URI);
611 }
612}
613
614
615/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000616 * xmlTextReaderCharacters:
617 * @ctx: the user data (XML parser context)
618 * @ch: a xmlChar string
619 * @len: the number of xmlChar
620 *
621 * receiving some chars from the parser.
622 */
623static void
624xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
625{
626 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
627 xmlTextReaderPtr reader = ctxt->_private;
628
629#ifdef DEBUG_CALLBACKS
630 printf("xmlTextReaderCharacters()\n");
631#endif
632 if ((reader != NULL) && (reader->characters != NULL)) {
633 reader->characters(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000634 }
635}
636
637/**
638 * xmlTextReaderCDataBlock:
639 * @ctx: the user data (XML parser context)
640 * @value: The pcdata content
641 * @len: the block length
642 *
643 * called when a pcdata block has been parsed
644 */
645static void
646xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
647{
648 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
649 xmlTextReaderPtr reader = ctxt->_private;
650
651#ifdef DEBUG_CALLBACKS
652 printf("xmlTextReaderCDataBlock()\n");
653#endif
654 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
655 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000656 }
657}
658
659/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000660 * xmlTextReaderPushData:
661 * @reader: the xmlTextReaderPtr used
662 *
663 * Push data down the progressive parser until a significant callback
664 * got raised.
665 *
666 * Returns -1 in case of failure, 0 otherwise
667 */
668static int
669xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000670 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000671 int val, s;
William M. Brack779af002003-08-01 15:55:39 +0000672 xmlTextReaderState oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000673
674 if ((reader->input == NULL) || (reader->input->buffer == NULL))
675 return(-1);
676
Daniel Veillardea7751d2002-12-20 00:16:24 +0000677 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000678 reader->state = XML_TEXTREADER_NONE;
679 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000680
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000681 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000682 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000683 /*
684 * Refill the buffer unless we are at the end of the stream
685 */
686 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
687 val = xmlParserInputBufferRead(reader->input, 4096);
Daniel Veillard53350552003-09-18 13:35:51 +0000688 if ((val == 0) &&
689 (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
690 if (inbuf->use == reader->cur) {
691 reader->mode = XML_TEXTREADER_MODE_EOF;
692 reader->state = oldstate;
693 if ((oldstate != XML_TEXTREADER_START) ||
694 (reader->ctxt->myDoc != NULL))
695 return(val);
696 }
697 } else if (val < 0) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000698 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000699 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000700 if ((oldstate != XML_TEXTREADER_START) ||
701 (reader->ctxt->myDoc != NULL))
702 return(val);
Daniel Veillard53350552003-09-18 13:35:51 +0000703 } else if (val == 0) {
704 /* mark the end of the stream and process the remains */
705 reader->mode = XML_TEXTREADER_MODE_EOF;
706 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000707 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000708
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000709 } else
710 break;
711 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000712 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000713 * parse by block of CHUNK_SIZE bytes, various tests show that
714 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000715 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000716 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000717 val = xmlParseChunk(reader->ctxt,
718 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000719 CHUNK_SIZE, 0);
720 reader->cur += CHUNK_SIZE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000721 if (val != 0)
722 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000723 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000724 s = inbuf->use - reader->cur;
725 val = xmlParseChunk(reader->ctxt,
726 (const char *) &inbuf->content[reader->cur],
727 s, 0);
728 reader->cur += s;
729 if (val != 0)
730 return(-1);
731 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000732 }
733 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000734
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000735 /*
736 * Discard the consumed input when needed and possible
737 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000738 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillardf6bc7c22003-09-17 22:33:22 +0000739 if ((reader->cur >= 4096) &&
740 (inbuf->use - reader->cur <= CHUNK_SIZE)) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000741 val = xmlBufferShrink(inbuf, reader->cur);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000742 if (val >= 0) {
743 reader->cur -= val;
744 }
745 }
746 }
747
748 /*
749 * At the end of the stream signal that the work is done to the Push
750 * parser.
751 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000752 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000753 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000754 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000755 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000756 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000757 s, 1);
758 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000759 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000760 if (val != 0) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000761 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000762 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000763 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000764 return(0);
765}
766
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000767#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000768/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000769 * xmlTextReaderValidatePush:
770 * @reader: the xmlTextReaderPtr used
771 *
772 * Push the current node for validation
773 */
774static void
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000775xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000776 xmlNodePtr node = reader->node;
777
Daniel Veillardf4e55762003-04-15 23:32:22 +0000778 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
779 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
780 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
781 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
782 reader->ctxt->myDoc, node, node->name);
783 } else {
784 /* TODO use the BuildQName interface */
785 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000786
Daniel Veillardf4e55762003-04-15 23:32:22 +0000787 qname = xmlStrdup(node->ns->prefix);
788 qname = xmlStrcat(qname, BAD_CAST ":");
789 qname = xmlStrcat(qname, node->name);
790 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
791 reader->ctxt->myDoc, node, qname);
792 if (qname != NULL)
793 xmlFree(qname);
794 }
795#ifdef LIBXML_SCHEMAS_ENABLED
796 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
797 (reader->rngValidCtxt != NULL)) {
798 int ret;
799
800 if (reader->rngFullNode != NULL) return;
801 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
802 reader->ctxt->myDoc,
803 node);
804 if (ret == 0) {
805 /*
806 * this element requires a full tree
807 */
808 node = xmlTextReaderExpand(reader);
809 if (node == NULL) {
810printf("Expand failed !\n");
811 ret = -1;
812 } else {
813 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
814 reader->ctxt->myDoc,
815 node);
816 reader->rngFullNode = node;
817 }
818 }
819 if (ret != 1)
820 reader->rngValidErrors++;
821#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000822 }
823}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000824
825/**
826 * xmlTextReaderValidateCData:
827 * @reader: the xmlTextReaderPtr used
828 * @data: pointer to the CData
829 * @len: lenght of the CData block in bytes.
830 *
831 * Push some CData for validation
832 */
833static void
834xmlTextReaderValidateCData(xmlTextReaderPtr reader,
835 const xmlChar *data, int len) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000836 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
837 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
838 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
839 data, len);
840#ifdef LIBXML_SCHEMAS_ENABLED
841 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
842 (reader->rngValidCtxt != NULL)) {
843 int ret;
844
845 if (reader->rngFullNode != NULL) return;
846 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
847 if (ret != 1)
848 reader->rngValidErrors++;
849#endif
850 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000851}
852
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000853/**
854 * xmlTextReaderValidatePop:
855 * @reader: the xmlTextReaderPtr used
856 *
857 * Pop the current node from validation
858 */
859static void
860xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
861 xmlNodePtr node = reader->node;
862
Daniel Veillardf4e55762003-04-15 23:32:22 +0000863 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
864 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
865 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
866 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
867 reader->ctxt->myDoc, node, node->name);
868 } else {
869 /* TODO use the BuildQName interface */
870 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000871
Daniel Veillardf4e55762003-04-15 23:32:22 +0000872 qname = xmlStrdup(node->ns->prefix);
873 qname = xmlStrcat(qname, BAD_CAST ":");
874 qname = xmlStrcat(qname, node->name);
875 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
876 reader->ctxt->myDoc, node, qname);
877 if (qname != NULL)
878 xmlFree(qname);
879 }
880#ifdef LIBXML_SCHEMAS_ENABLED
881 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
882 (reader->rngValidCtxt != NULL)) {
883 int ret;
884
885 if (reader->rngFullNode != NULL) {
886 if (node == reader->rngFullNode)
887 reader->rngFullNode = NULL;
888 return;
889 }
890 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
891 reader->ctxt->myDoc,
892 node);
893 if (ret != 1)
894 reader->rngValidErrors++;
895#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000896 }
897}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000898
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000899/**
900 * xmlTextReaderValidateEntity:
901 * @reader: the xmlTextReaderPtr used
902 *
903 * Handle the validation when an entity reference is encountered and
904 * entity substitution is not activated. As a result the parser interface
905 * must walk through the entity and do the validation calls
906 */
907static void
908xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
909 xmlNodePtr oldnode = reader->node;
910 xmlNodePtr node = reader->node;
911 xmlParserCtxtPtr ctxt = reader->ctxt;
912
913 do {
914 if (node->type == XML_ENTITY_REF_NODE) {
915 /*
916 * Case where the underlying tree is not availble, lookup the entity
917 * and walk it.
918 */
919 if ((node->children == NULL) && (ctxt->sax != NULL) &&
920 (ctxt->sax->getEntity != NULL)) {
921 node->children = (xmlNodePtr)
922 ctxt->sax->getEntity(ctxt, node->name);
923 }
924
925 if ((node->children != NULL) &&
926 (node->children->type == XML_ENTITY_DECL) &&
927 (node->children->children != NULL)) {
928 xmlTextReaderEntPush(reader, node);
929 node = node->children->children;
930 continue;
931 } else {
932 /*
933 * The error has probably be raised already.
934 */
935 if (node == oldnode)
936 break;
937 node = node->next;
938 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000939#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000940 } else if (node->type == XML_ELEMENT_NODE) {
941 reader->node = node;
942 xmlTextReaderValidatePush(reader);
943 } else if ((node->type == XML_TEXT_NODE) ||
944 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000945 xmlTextReaderValidateCData(reader, node->content,
946 xmlStrlen(node->content));
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000947#endif
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000948 }
949
950 /*
951 * go to next node
952 */
953 if (node->children != NULL) {
954 node = node->children;
955 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +0000956 } else if (node->type == XML_ELEMENT_NODE) {
957 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000958 }
959 if (node->next != NULL) {
960 node = node->next;
961 continue;
962 }
963 do {
964 node = node->parent;
965 if (node->type == XML_ELEMENT_NODE) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +0000966 xmlNodePtr tmp;
967 while ((tmp = node->last) != NULL) {
968 if ((tmp->_private != xmlTextReaderIsEmptyPreserved) &&
969 (tmp->_private != xmlTextReaderIsPreserved)) {
970 xmlUnlinkNode(tmp);
971 xmlTextReaderFreeNode(reader, tmp);
972 } else
973 break;
974 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000975 reader->node = node;
976 xmlTextReaderValidatePop(reader);
977 }
978 if ((node->type == XML_ENTITY_DECL) &&
979 (reader->ent != NULL) && (reader->ent->children == node)) {
980 node = xmlTextReaderEntPop(reader);
981 }
982 if (node == oldnode)
983 break;
984 if (node->next != NULL) {
985 node = node->next;
986 break;
987 }
988 } while ((node != NULL) && (node != oldnode));
989 } while ((node != NULL) && (node != oldnode));
990 reader->node = oldnode;
991}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000992#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000993
994
995/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000996 * xmlTextReaderGetSuccessor:
997 * @cur: the current node
998 *
999 * Get the successor of a node if available.
1000 *
1001 * Returns the successor node or NULL
1002 */
1003static xmlNodePtr
1004xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1005 if (cur == NULL) return(NULL) ; /* ERROR */
1006 if (cur->next != NULL) return(cur->next) ;
1007 do {
1008 cur = cur->parent;
1009 if (cur == NULL) return(NULL);
1010 if (cur->next != NULL) return(cur->next);
1011 } while (cur != NULL);
1012 return(cur);
1013}
1014
1015/**
1016 * xmlTextReaderDoExpand:
1017 * @reader: the xmlTextReaderPtr used
1018 *
1019 * Makes sure that the current node is fully read as well as all its
1020 * descendant. It means the full DOM subtree must be available at the
1021 * end of the call.
1022 *
1023 * Returns 1 if the node was expanded successfully, 0 if there is no more
1024 * nodes to read, or -1 in case of error
1025 */
1026static int
1027xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1028 int val;
1029
1030 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1031 return(-1);
1032
1033 do {
1034 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1035 return(1);
Daniel Veillarda37aab82003-06-09 09:10:36 +00001036 if (reader->ctxt->nodeNr <= reader->depth)
1037 return(1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001038 if (reader->mode == XML_TEXTREADER_MODE_EOF)
1039 return(1);
1040 val = xmlTextReaderPushData(reader);
1041 if (val < 0)
1042 return(-1);
1043 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1044 return(1);
1045}
1046
1047/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001048 * xmlTextReaderRead:
1049 * @reader: the xmlTextReaderPtr used
1050 *
1051 * Moves the position of the current instance to the next node in
1052 * the stream, exposing its properties.
1053 *
1054 * Returns 1 if the node was read successfully, 0 if there is no more
1055 * nodes to read, or -1 in case of error
1056 */
1057int
1058xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +00001059 int val, olddepth = 0;
William M. Brack899e64a2003-09-26 18:03:42 +00001060 xmlTextReaderState oldstate = XML_TEXTREADER_START;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001061 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001062
1063 if ((reader == NULL) || (reader->ctxt == NULL))
1064 return(-1);
1065 if (reader->ctxt->wellFormed != 1)
1066 return(-1);
1067
1068#ifdef DEBUG_READER
1069 fprintf(stderr, "\nREAD ");
1070 DUMP_READER
1071#endif
Daniel Veillard29b3e282002-12-29 11:14:41 +00001072 reader->curnode = NULL;
Daniel Veillard67df8092002-12-16 22:04:11 +00001073 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1074 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001075 /*
1076 * Initial state
1077 */
1078 do {
1079 val = xmlTextReaderPushData(reader);
1080 if (val < 0)
1081 return(-1);
1082 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001083 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1084 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001085 if (reader->ctxt->myDoc != NULL)
1086 reader->ctxt->myDoc->_private = reader;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001087 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001088 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001089 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001090 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001091 if (reader->node == NULL)
1092 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001093 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001094 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +00001095 if (reader->ctxt->myDoc != NULL) {
1096 reader->node = reader->ctxt->myDoc->children;
1097 }
1098 if (reader->node == NULL)
1099 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +00001100 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001101 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001102 reader->depth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001103 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001104 }
1105 oldstate = reader->state;
1106 olddepth = reader->ctxt->nodeNr;
1107 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001108
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001109get_next_node:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001110 /*
1111 * If we are not backtracking on ancestors or examined nodes,
1112 * that the parser didn't finished or that we arent at the end
1113 * of stream, continue processing.
1114 */
Daniel Veillarda880b122003-04-21 21:36:41 +00001115 while ((reader->node->next == NULL) &&
1116 (reader->ctxt->nodeNr == olddepth) &&
1117 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +00001118 (reader->node->children == NULL) ||
1119 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard409a8142003-07-18 15:16:57 +00001120 ((reader->node->children != NULL) &&
1121 (reader->node->children->type == XML_TEXT_NODE) &&
1122 (reader->node->children->next == NULL)) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001123 (reader->node->type == XML_DTD_NODE) ||
1124 (reader->node->type == XML_DOCUMENT_NODE) ||
1125 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001126 ((reader->ctxt->node == NULL) ||
1127 (reader->ctxt->node == reader->node) ||
1128 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +00001129 (reader->ctxt->instate != XML_PARSER_EOF)) {
1130 val = xmlTextReaderPushData(reader);
1131 if (val < 0)
1132 return(-1);
1133 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001134 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001135 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001136 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001137 if ((reader->node->children != NULL) &&
1138 (reader->node->type != XML_ENTITY_REF_NODE) &&
1139 (reader->node->type != XML_DTD_NODE)) {
1140 reader->node = reader->node->children;
1141 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +00001142 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001143 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001144 }
1145 }
1146 if (reader->node->next != NULL) {
1147 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +00001148 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001149 (reader->node->children == NULL) &&
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001150 (reader->node->_private != (void *)xmlTextReaderIsEmpty) &&
1151 (reader->node->_private != (void *)xmlTextReaderIsEmptyPreserved)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001152 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001153 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001154 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001155#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001156 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001157 (reader->node->type == XML_ELEMENT_NODE))
1158 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001159#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001160 reader->node = reader->node->next;
1161 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001162
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001163 /*
1164 * Cleanup of the old node
1165 */
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00001166 if ((reader->node->prev != NULL) &&
1167 (reader->node->prev->type != XML_DTD_NODE)) {
1168 xmlNodePtr tmp = reader->node->prev;
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001169 if ((tmp->_private != xmlTextReaderIsEmptyPreserved) &&
1170 (tmp->_private != xmlTextReaderIsPreserved)) {
1171 xmlUnlinkNode(tmp);
1172 xmlTextReaderFreeNode(reader, tmp);
1173 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001174 }
1175
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001176 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001177 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001178 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +00001179 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +00001180 (reader->node->children == NULL) &&
1181 (reader->node->_private != (void *)xmlTextReaderIsEmpty)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001182 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001183 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001184 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001185#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001186 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001187 xmlTextReaderValidatePop(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001188#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001189 reader->node = reader->node->parent;
1190 if ((reader->node == NULL) ||
1191 (reader->node->type == XML_DOCUMENT_NODE) ||
1192#ifdef LIBXML_DOCB_ENABLED
1193 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
1194#endif
1195 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001196 if (reader->mode != XML_TEXTREADER_DONE) {
1197 val = xmlParseChunk(reader->ctxt, "", 0, 1);
1198 reader->mode = XML_TEXTREADER_DONE;
1199 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001200 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +00001201 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001202
1203 /*
1204 * Cleanup of the old node
1205 */
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001206 if ((oldnode->type != XML_DTD_NODE) &&
1207 (oldnode->_private != xmlTextReaderIsEmptyPreserved) &&
1208 (oldnode->_private != xmlTextReaderIsPreserved)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001209 xmlUnlinkNode(oldnode);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001210 xmlTextReaderFreeNode(reader, oldnode);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001211 }
1212
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001213 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001214 }
1215 reader->depth--;
1216 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001217
1218node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001219 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001220
1221 /*
Daniel Veillarda880b122003-04-21 21:36:41 +00001222 * If we are in the middle of a piece of CDATA make sure it's finished
1223 */
1224 if ((reader->node != NULL) &&
Daniel Veillardccc4d2b2003-09-17 21:27:31 +00001225 (reader->node->next == NULL) &&
Daniel Veillarda880b122003-04-21 21:36:41 +00001226 ((reader->node->type == XML_TEXT_NODE) ||
1227 (reader->node->type == XML_CDATA_SECTION_NODE))) {
1228 xmlTextReaderExpand(reader);
1229 }
1230
1231 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001232 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001233 */
1234 if ((reader->node != NULL) &&
1235 (reader->node->type == XML_ENTITY_REF_NODE) &&
1236 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1237 /*
1238 * Case where the underlying tree is not availble, lookup the entity
1239 * and walk it.
1240 */
1241 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
1242 (reader->ctxt->sax->getEntity != NULL)) {
1243 reader->node->children = (xmlNodePtr)
1244 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
1245 }
1246
1247 if ((reader->node->children != NULL) &&
1248 (reader->node->children->type == XML_ENTITY_DECL) &&
1249 (reader->node->children->children != NULL)) {
1250 xmlTextReaderEntPush(reader, reader->node);
1251 reader->node = reader->node->children->children;
1252 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001253#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001254 } else if ((reader->node != NULL) &&
1255 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +00001256 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +00001257 xmlTextReaderValidateEntity(reader);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001258#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001259 }
1260 if ((reader->node != NULL) &&
1261 (reader->node->type == XML_ENTITY_DECL) &&
1262 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1263 reader->node = xmlTextReaderEntPop(reader);
1264 reader->depth++;
1265 goto get_next_node;
1266 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001267#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001268 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001269 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001270
1271 if ((node->type == XML_ELEMENT_NODE) &&
1272 ((reader->state != XML_TEXTREADER_END) &&
1273 (reader->state != XML_TEXTREADER_BACKTRACK))) {
1274 xmlTextReaderValidatePush(reader);
1275 } else if ((node->type == XML_TEXT_NODE) ||
1276 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001277 xmlTextReaderValidateCData(reader, node->content,
1278 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001279 }
1280 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +00001281#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001282 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001283node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001284 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001285 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001286}
1287
Daniel Veillard67df8092002-12-16 22:04:11 +00001288/**
1289 * xmlTextReaderReadState:
1290 * @reader: the xmlTextReaderPtr used
1291 *
1292 * Gets the read state of the reader.
1293 *
1294 * Returns the state value, or -1 in case of error
1295 */
1296int
1297xmlTextReaderReadState(xmlTextReaderPtr reader) {
1298 if (reader == NULL)
1299 return(-1);
1300 return(reader->mode);
1301}
1302
1303/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001304 * xmlTextReaderExpand:
1305 * @reader: the xmlTextReaderPtr used
1306 *
1307 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +00001308 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001309 *
1310 * Returns a node pointer valid until the next xmlTextReaderRead() call
1311 * or NULL in case of error.
1312 */
1313xmlNodePtr
1314xmlTextReaderExpand(xmlTextReaderPtr reader) {
1315 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1316 return(NULL);
1317 if (xmlTextReaderDoExpand(reader) < 0)
1318 return(NULL);
1319 return(reader->node);
1320}
1321
1322/**
1323 * xmlTextReaderNext:
1324 * @reader: the xmlTextReaderPtr used
1325 *
1326 * Skip to the node following the current one in document order while
1327 * avoiding the subtree if any.
1328 *
1329 * Returns 1 if the node was read successfully, 0 if there is no more
1330 * nodes to read, or -1 in case of error
1331 */
1332int
1333xmlTextReaderNext(xmlTextReaderPtr reader) {
1334 int ret;
1335 xmlNodePtr cur;
1336
1337 if (reader == NULL)
1338 return(-1);
1339 cur = reader->node;
1340 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1341 return(xmlTextReaderRead(reader));
1342 if (reader->state == XML_TEXTREADER_END)
1343 return(xmlTextReaderRead(reader));
1344 if (cur->_private == (void *)xmlTextReaderIsEmpty)
1345 return(xmlTextReaderRead(reader));
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001346 if (cur->_private == (void *)xmlTextReaderIsEmptyPreserved)
1347 return(xmlTextReaderRead(reader));
Daniel Veillardc6cae7b2003-04-11 09:02:11 +00001348 do {
1349 ret = xmlTextReaderRead(reader);
1350 if (ret != 1)
1351 return(ret);
1352 } while (reader->node != cur);
1353 return(xmlTextReaderRead(reader));
1354}
1355
1356/**
Daniel Veillard67df8092002-12-16 22:04:11 +00001357 * xmlTextReaderReadInnerXml:
1358 * @reader: the xmlTextReaderPtr used
1359 *
1360 * Reads the contents of the current node, including child nodes and markup.
1361 *
1362 * Returns a string containing the XML content, or NULL if the current node
1363 * is neither an element nor attribute, or has no child nodes. The
1364 * string must be deallocated by the caller.
1365 */
1366xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001367xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001368 TODO
1369 return(NULL);
1370}
1371
1372/**
1373 * xmlTextReaderReadOuterXml:
1374 * @reader: the xmlTextReaderPtr used
1375 *
1376 * Reads the contents of the current node, including child nodes and markup.
1377 *
1378 * Returns a string containing the XML content, or NULL if the current node
1379 * is neither an element nor attribute, or has no child nodes. The
1380 * string must be deallocated by the caller.
1381 */
1382xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001383xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001384 TODO
1385 return(NULL);
1386}
1387
1388/**
1389 * xmlTextReaderReadString:
1390 * @reader: the xmlTextReaderPtr used
1391 *
1392 * Reads the contents of an element or a text node as a string.
1393 *
1394 * Returns a string containing the contents of the Element or Text node,
1395 * or NULL if the reader is positioned on any other type of node.
1396 * The string must be deallocated by the caller.
1397 */
1398xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001399xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001400 TODO
1401 return(NULL);
1402}
1403
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001404#if 0
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001405/**
1406 * xmlTextReaderReadBase64:
1407 * @reader: the xmlTextReaderPtr used
1408 * @array: a byte array to store the content.
1409 * @offset: the zero-based index into array where the method should
1410 * begin to write.
1411 * @len: the number of bytes to write.
1412 *
1413 * Reads and decodes the Base64 encoded contents of an element and
1414 * stores the result in a byte buffer.
1415 *
1416 * Returns the number of bytes written to array, or zero if the current
1417 * instance is not positioned on an element or -1 in case of error.
1418 */
1419int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001420xmlTextReaderReadBase64(xmlTextReaderPtr reader,
1421 unsigned char *array ATTRIBUTE_UNUSED,
1422 int offset ATTRIBUTE_UNUSED,
1423 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001424 if ((reader == NULL) || (reader->ctxt == NULL))
1425 return(-1);
1426 if (reader->ctxt->wellFormed != 1)
1427 return(-1);
1428
1429 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1430 return(0);
1431 TODO
1432 return(0);
1433}
1434
1435/**
1436 * xmlTextReaderReadBinHex:
1437 * @reader: the xmlTextReaderPtr used
1438 * @array: a byte array to store the content.
1439 * @offset: the zero-based index into array where the method should
1440 * begin to write.
1441 * @len: the number of bytes to write.
1442 *
1443 * Reads and decodes the BinHex encoded contents of an element and
1444 * stores the result in a byte buffer.
1445 *
1446 * Returns the number of bytes written to array, or zero if the current
1447 * instance is not positioned on an element or -1 in case of error.
1448 */
1449int
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001450xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
1451 unsigned char *array ATTRIBUTE_UNUSED,
1452 int offset ATTRIBUTE_UNUSED,
1453 int len ATTRIBUTE_UNUSED) {
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001454 if ((reader == NULL) || (reader->ctxt == NULL))
1455 return(-1);
1456 if (reader->ctxt->wellFormed != 1)
1457 return(-1);
1458
1459 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1460 return(0);
1461 TODO
1462 return(0);
1463}
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001464#endif
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001465
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001466/************************************************************************
1467 * *
1468 * Constructor and destructors *
1469 * *
1470 ************************************************************************/
1471/**
1472 * xmlNewTextReader:
1473 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001474 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001475 *
1476 * Create an xmlTextReader structure fed with @input
1477 *
1478 * Returns the new xmlTextReaderPtr or NULL in case of error
1479 */
1480xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001481xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001482 xmlTextReaderPtr ret;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001483
1484 if (input == NULL)
1485 return(NULL);
1486 ret = xmlMalloc(sizeof(xmlTextReader));
1487 if (ret == NULL) {
1488 xmlGenericError(xmlGenericErrorContext,
1489 "xmlNewTextReader : malloc failed\n");
1490 return(NULL);
1491 }
1492 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001493 ret->entTab = NULL;
1494 ret->entMax = 0;
1495 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001496 ret->input = input;
1497 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1498 if (ret->sax == NULL) {
1499 xmlFree(ret);
1500 xmlGenericError(xmlGenericErrorContext,
1501 "xmlNewTextReader : malloc failed\n");
1502 return(NULL);
1503 }
1504 memcpy(ret->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler));
1505 ret->startElement = ret->sax->startElement;
1506 ret->sax->startElement = xmlTextReaderStartElement;
1507 ret->endElement = ret->sax->endElement;
1508 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001509 if (ret->sax->initialized == XML_SAX2_MAGIC) {
1510 ret->startElementNs = ret->sax->startElementNs;
1511 ret->sax->startElementNs = xmlTextReaderStartElementNs;
1512 ret->endElementNs = ret->sax->endElementNs;
1513 ret->sax->endElementNs = xmlTextReaderEndElementNs;
1514 } else {
1515 ret->startElementNs = NULL;
1516 ret->endElementNs = NULL;
1517 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00001518 ret->characters = ret->sax->characters;
1519 ret->sax->characters = xmlTextReaderCharacters;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001520 ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001521 ret->cdataBlock = ret->sax->cdataBlock;
1522 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001523
Daniel Veillard67df8092002-12-16 22:04:11 +00001524 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001525 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001526 ret->curnode = NULL;
Daniel Veillard53350552003-09-18 13:35:51 +00001527 if (ret->input->buffer->use < 4) {
William M. Brack899e64a2003-09-26 18:03:42 +00001528 xmlParserInputBufferRead(input, 4);
Daniel Veillard53350552003-09-18 13:35:51 +00001529 }
1530 if (ret->input->buffer->use >= 4) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001531 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001532 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001533 ret->base = 0;
1534 ret->cur = 4;
1535 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001536 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001537 ret->base = 0;
1538 ret->cur = 0;
1539 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001540 if (ret->ctxt == NULL) {
1541 xmlGenericError(xmlGenericErrorContext,
1542 "xmlNewTextReader : malloc failed\n");
1543 xmlFree(ret->sax);
1544 xmlFree(ret);
1545 return(NULL);
1546 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001547 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001548 ret->ctxt->linenumbers = 1;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001549 ret->ctxt->dictNames = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001550 ret->allocs = XML_TEXTREADER_CTXT;
Daniel Veillard40412cd2003-09-03 13:28:32 +00001551 /*
1552 * use the parser dictionnary to allocate all elements and attributes names
1553 */
1554 ret->ctxt->docdict = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001555 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001556}
1557
1558/**
1559 * xmlNewTextReaderFilename:
1560 * @URI: the URI of the resource to process
1561 *
1562 * Create an xmlTextReader structure fed with the resource at @URI
1563 *
1564 * Returns the new xmlTextReaderPtr or NULL in case of error
1565 */
1566xmlTextReaderPtr
1567xmlNewTextReaderFilename(const char *URI) {
1568 xmlParserInputBufferPtr input;
1569 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001570 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001571
1572 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1573 if (input == NULL)
1574 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001575 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001576 if (ret == NULL) {
1577 xmlFreeParserInputBuffer(input);
1578 return(NULL);
1579 }
1580 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001581 if (ret->ctxt->directory == NULL)
1582 directory = xmlParserGetDirectory(URI);
1583 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1584 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1585 if (directory != NULL)
1586 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001587 return(ret);
1588}
1589
1590/**
1591 * xmlFreeTextReader:
1592 * @reader: the xmlTextReaderPtr
1593 *
1594 * Deallocate all the resources associated to the reader
1595 */
1596void
1597xmlFreeTextReader(xmlTextReaderPtr reader) {
1598 if (reader == NULL)
1599 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001600#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001601 if (reader->rngSchemas != NULL) {
1602 xmlRelaxNGFree(reader->rngSchemas);
1603 reader->rngSchemas = NULL;
1604 }
1605 if (reader->rngValidCtxt != NULL) {
1606 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
1607 reader->rngValidCtxt = NULL;
1608 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001609#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001610 if (reader->ctxt != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001611 if (reader->ctxt->myDoc != NULL) {
1612 if (reader->preserve == 0)
1613 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
1614 reader->ctxt->myDoc = NULL;
1615 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00001616 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
1617 (reader->ctxt->vctxt.vstateMax > 0)){
1618 xmlFree(reader->ctxt->vctxt.vstateTab);
1619 reader->ctxt->vctxt.vstateTab = 0;
1620 reader->ctxt->vctxt.vstateMax = 0;
1621 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001622 if (reader->allocs & XML_TEXTREADER_CTXT)
1623 xmlFreeParserCtxt(reader->ctxt);
1624 }
1625 if (reader->sax != NULL)
1626 xmlFree(reader->sax);
1627 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
1628 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001629 if (reader->faketext != NULL) {
1630 xmlFreeNode(reader->faketext);
1631 }
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001632 if (reader->entTab != NULL)
1633 xmlFree(reader->entTab);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001634 xmlFree(reader);
1635}
1636
1637/************************************************************************
1638 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001639 * Methods for XmlTextReader *
1640 * *
1641 ************************************************************************/
1642/**
1643 * xmlTextReaderClose:
1644 * @reader: the xmlTextReaderPtr used
1645 *
1646 * This method releases any resources allocated by the current instance
1647 * changes the state to Closed and close any underlying input.
1648 *
1649 * Returns 0 or -1 in case of error
1650 */
1651int
1652xmlTextReaderClose(xmlTextReaderPtr reader) {
1653 if (reader == NULL)
1654 return(-1);
1655 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001656 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001657 reader->mode = XML_TEXTREADER_MODE_CLOSED;
1658 if (reader->ctxt != NULL) {
1659 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001660 if (reader->preserve == 0)
1661 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001662 reader->ctxt->myDoc = NULL;
1663 }
1664 if (reader->allocs & XML_TEXTREADER_CTXT) {
1665 xmlFreeParserCtxt(reader->ctxt);
1666 reader->allocs -= XML_TEXTREADER_CTXT;
1667 }
1668 }
1669 if (reader->sax != NULL) {
1670 xmlFree(reader->sax);
1671 reader->sax = NULL;
1672 }
1673 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
1674 xmlFreeParserInputBuffer(reader->input);
1675 reader->allocs -= XML_TEXTREADER_INPUT;
1676 }
1677 return(0);
1678}
1679
1680/**
1681 * xmlTextReaderGetAttributeNo:
1682 * @reader: the xmlTextReaderPtr used
1683 * @no: the zero-based index of the attribute relative to the containing element
1684 *
1685 * Provides the value of the attribute with the specified index relative
1686 * to the containing element.
1687 *
1688 * Returns a string containing the value of the specified attribute, or NULL
1689 * in case of error. The string must be deallocated by the caller.
1690 */
1691xmlChar *
1692xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
1693 xmlChar *ret;
1694 int i;
1695 xmlAttrPtr cur;
1696 xmlNsPtr ns;
1697
1698 if (reader == NULL)
1699 return(NULL);
1700 if (reader->node == NULL)
1701 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001702 if (reader->curnode != NULL)
1703 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001704 /* TODO: handle the xmlDecl */
1705 if (reader->node->type != XML_ELEMENT_NODE)
1706 return(NULL);
1707
1708 ns = reader->node->nsDef;
1709 for (i = 0;(i < no) && (ns != NULL);i++) {
1710 ns = ns->next;
1711 }
1712 if (ns != NULL)
1713 return(xmlStrdup(ns->href));
1714
1715 cur = reader->node->properties;
1716 if (cur == NULL)
1717 return(NULL);
1718 for (;i < no;i++) {
1719 cur = cur->next;
1720 if (cur == NULL)
1721 return(NULL);
1722 }
1723 /* TODO walk the DTD if present */
1724
1725 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
1726 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
1727 return(ret);
1728}
1729
1730/**
1731 * xmlTextReaderGetAttribute:
1732 * @reader: the xmlTextReaderPtr used
1733 * @name: the qualified name of the attribute.
1734 *
1735 * Provides the value of the attribute with the specified qualified name.
1736 *
1737 * Returns a string containing the value of the specified attribute, or NULL
1738 * in case of error. The string must be deallocated by the caller.
1739 */
1740xmlChar *
1741xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1742 xmlChar *prefix = NULL;
1743 xmlChar *localname;
1744 xmlNsPtr ns;
1745 xmlChar *ret = NULL;
1746
1747 if ((reader == NULL) || (name == NULL))
1748 return(NULL);
1749 if (reader->node == NULL)
1750 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001751 if (reader->curnode != NULL)
1752 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001753
1754 /* TODO: handle the xmlDecl */
1755 if (reader->node->type != XML_ELEMENT_NODE)
1756 return(NULL);
1757
1758 localname = xmlSplitQName2(name, &prefix);
1759 if (localname == NULL)
1760 return(xmlGetProp(reader->node, name));
1761
1762 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1763 if (ns != NULL)
1764 ret = xmlGetNsProp(reader->node, localname, ns->href);
1765
1766 if (localname != NULL)
1767 xmlFree(localname);
1768 if (prefix != NULL)
1769 xmlFree(prefix);
1770 return(ret);
1771}
1772
1773
1774/**
1775 * xmlTextReaderGetAttributeNs:
1776 * @reader: the xmlTextReaderPtr used
1777 * @localName: the local name of the attribute.
1778 * @namespaceURI: the namespace URI of the attribute.
1779 *
1780 * Provides the value of the specified attribute
1781 *
1782 * Returns a string containing the value of the specified attribute, or NULL
1783 * in case of error. The string must be deallocated by the caller.
1784 */
1785xmlChar *
1786xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
1787 const xmlChar *namespaceURI) {
1788 if ((reader == NULL) || (localName == NULL))
1789 return(NULL);
1790 if (reader->node == NULL)
1791 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001792 if (reader->curnode != NULL)
1793 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001794
1795 /* TODO: handle the xmlDecl */
1796 if (reader->node->type != XML_ELEMENT_NODE)
1797 return(NULL);
1798
1799 return(xmlGetNsProp(reader->node, localName, namespaceURI));
1800}
1801
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001802/**
1803 * xmlTextReaderGetRemainder:
1804 * @reader: the xmlTextReaderPtr used
1805 *
1806 * Method to get the remainder of the buffered XML. this method stops the
1807 * parser, set its state to End Of File and return the input stream with
1808 * what is left that the parser did not use.
1809 *
1810 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
1811 * in case of error.
1812 */
1813xmlParserInputBufferPtr
1814xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
1815 xmlParserInputBufferPtr ret = NULL;
1816
1817 if (reader == NULL)
1818 return(NULL);
1819 if (reader->node == NULL)
1820 return(NULL);
1821
1822 reader->node = NULL;
1823 reader->curnode = NULL;
1824 reader->mode = XML_TEXTREADER_MODE_EOF;
1825 if (reader->ctxt != NULL) {
1826 if (reader->ctxt->myDoc != NULL) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00001827 if (reader->preserve == 0)
1828 xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001829 reader->ctxt->myDoc = NULL;
1830 }
1831 if (reader->allocs & XML_TEXTREADER_CTXT) {
1832 xmlFreeParserCtxt(reader->ctxt);
1833 reader->allocs -= XML_TEXTREADER_CTXT;
1834 }
1835 }
1836 if (reader->sax != NULL) {
1837 xmlFree(reader->sax);
1838 reader->sax = NULL;
1839 }
1840 if (reader->allocs & XML_TEXTREADER_INPUT) {
1841 ret = reader->input;
1842 reader->allocs -= XML_TEXTREADER_INPUT;
1843 } else {
1844 /*
1845 * Hum, one may need to duplicate the data structure because
1846 * without reference counting the input may be freed twice:
1847 * - by the layer which allocated it.
1848 * - by the layer to which would have been returned to.
1849 */
1850 TODO
1851 return(NULL);
1852 }
1853 return(ret);
1854}
1855
1856/**
1857 * xmlTextReaderLookupNamespace:
1858 * @reader: the xmlTextReaderPtr used
1859 * @prefix: the prefix whose namespace URI is to be resolved. To return
1860 * the default namespace, specify NULL
1861 *
1862 * Resolves a namespace prefix in the scope of the current element.
1863 *
1864 * Returns a string containing the namespace URI to which the prefix maps
1865 * or NULL in case of error. The string must be deallocated by the caller.
1866 */
1867xmlChar *
1868xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
1869 xmlNsPtr ns;
1870
1871 if (reader == NULL)
1872 return(NULL);
1873 if (reader->node == NULL)
1874 return(NULL);
1875
1876 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1877 if (ns == NULL)
1878 return(NULL);
1879 return(xmlStrdup(ns->href));
1880}
1881
1882/**
1883 * xmlTextReaderMoveToAttributeNo:
1884 * @reader: the xmlTextReaderPtr used
1885 * @no: the zero-based index of the attribute relative to the containing
1886 * element.
1887 *
1888 * Moves the position of the current instance to the attribute with
1889 * the specified index relative to the containing element.
1890 *
1891 * Returns 1 in case of success, -1 in case of error, 0 if not found
1892 */
1893int
1894xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
1895 int i;
1896 xmlAttrPtr cur;
1897 xmlNsPtr ns;
1898
1899 if (reader == NULL)
1900 return(-1);
1901 if (reader->node == NULL)
1902 return(-1);
1903 /* TODO: handle the xmlDecl */
1904 if (reader->node->type != XML_ELEMENT_NODE)
1905 return(-1);
1906
1907 reader->curnode = NULL;
1908
1909 ns = reader->node->nsDef;
1910 for (i = 0;(i < no) && (ns != NULL);i++) {
1911 ns = ns->next;
1912 }
1913 if (ns != NULL) {
1914 reader->curnode = (xmlNodePtr) ns;
1915 return(1);
1916 }
1917
1918 cur = reader->node->properties;
1919 if (cur == NULL)
1920 return(0);
1921 for (;i < no;i++) {
1922 cur = cur->next;
1923 if (cur == NULL)
1924 return(0);
1925 }
1926 /* TODO walk the DTD if present */
1927
1928 reader->curnode = (xmlNodePtr) cur;
1929 return(1);
1930}
1931
1932/**
1933 * xmlTextReaderMoveToAttribute:
1934 * @reader: the xmlTextReaderPtr used
1935 * @name: the qualified name of the attribute.
1936 *
1937 * Moves the position of the current instance to the attribute with
1938 * the specified qualified name.
1939 *
1940 * Returns 1 in case of success, -1 in case of error, 0 if not found
1941 */
1942int
1943xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1944 xmlChar *prefix = NULL;
1945 xmlChar *localname;
1946 xmlNsPtr ns;
1947 xmlAttrPtr prop;
1948
1949 if ((reader == NULL) || (name == NULL))
1950 return(-1);
1951 if (reader->node == NULL)
1952 return(-1);
1953
1954 /* TODO: handle the xmlDecl */
1955 if (reader->node->type != XML_ELEMENT_NODE)
1956 return(0);
1957
1958 localname = xmlSplitQName2(name, &prefix);
1959 if (localname == NULL) {
1960 /*
1961 * Namespace default decl
1962 */
1963 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
1964 ns = reader->node->nsDef;
1965 while (ns != NULL) {
1966 if (ns->prefix == NULL) {
1967 reader->curnode = (xmlNodePtr) ns;
1968 return(1);
1969 }
1970 ns = ns->next;
1971 }
1972 return(0);
1973 }
1974
1975 prop = reader->node->properties;
1976 while (prop != NULL) {
1977 /*
1978 * One need to have
1979 * - same attribute names
1980 * - and the attribute carrying that namespace
1981 */
1982 if ((xmlStrEqual(prop->name, name)) &&
1983 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
1984 reader->curnode = (xmlNodePtr) prop;
1985 return(1);
1986 }
1987 prop = prop->next;
1988 }
1989 return(0);
1990 }
1991
1992 /*
1993 * Namespace default decl
1994 */
1995 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
1996 ns = reader->node->nsDef;
1997 while (ns != NULL) {
1998 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
1999 reader->curnode = (xmlNodePtr) ns;
2000 goto found;
2001 }
2002 ns = ns->next;
2003 }
2004 goto not_found;
2005 }
2006 prop = reader->node->properties;
2007 while (prop != NULL) {
2008 /*
2009 * One need to have
2010 * - same attribute names
2011 * - and the attribute carrying that namespace
2012 */
2013 if ((xmlStrEqual(prop->name, localname)) &&
2014 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2015 reader->curnode = (xmlNodePtr) prop;
2016 goto found;
2017 }
2018 prop = prop->next;
2019 }
2020not_found:
2021 if (localname != NULL)
2022 xmlFree(localname);
2023 if (prefix != NULL)
2024 xmlFree(prefix);
2025 return(0);
2026
2027found:
2028 if (localname != NULL)
2029 xmlFree(localname);
2030 if (prefix != NULL)
2031 xmlFree(prefix);
2032 return(1);
2033}
2034
2035/**
2036 * xmlTextReaderMoveToAttributeNs:
2037 * @reader: the xmlTextReaderPtr used
2038 * @localName: the local name of the attribute.
2039 * @namespaceURI: the namespace URI of the attribute.
2040 *
2041 * Moves the position of the current instance to the attribute with the
2042 * specified local name and namespace URI.
2043 *
2044 * Returns 1 in case of success, -1 in case of error, 0 if not found
2045 */
2046int
2047xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
2048 const xmlChar *localName, const xmlChar *namespaceURI) {
2049 xmlAttrPtr prop;
2050 xmlNodePtr node;
2051
2052 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2053 return(-1);
2054 if (reader->node == NULL)
2055 return(-1);
2056 if (reader->node->type != XML_ELEMENT_NODE)
2057 return(0);
2058 node = reader->node;
2059
2060 /*
2061 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
2062 * namespace name associated to "xmlns"
2063 */
2064 prop = node->properties;
2065 while (prop != NULL) {
2066 /*
2067 * One need to have
2068 * - same attribute names
2069 * - and the attribute carrying that namespace
2070 */
2071 if (xmlStrEqual(prop->name, localName) &&
2072 ((prop->ns != NULL) &&
2073 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2074 reader->curnode = (xmlNodePtr) prop;
2075 return(1);
2076 }
2077 prop = prop->next;
2078 }
2079 return(0);
2080}
2081
2082/**
2083 * xmlTextReaderMoveToFirstAttribute:
2084 * @reader: the xmlTextReaderPtr used
2085 *
2086 * Moves the position of the current instance to the first attribute
2087 * associated with the current node.
2088 *
2089 * Returns 1 in case of success, -1 in case of error, 0 if not found
2090 */
2091int
2092xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
2093 if (reader == NULL)
2094 return(-1);
2095 if (reader->node == NULL)
2096 return(-1);
2097 if (reader->node->type != XML_ELEMENT_NODE)
2098 return(0);
2099
2100 if (reader->node->nsDef != NULL) {
2101 reader->curnode = (xmlNodePtr) reader->node->nsDef;
2102 return(1);
2103 }
2104 if (reader->node->properties != NULL) {
2105 reader->curnode = (xmlNodePtr) reader->node->properties;
2106 return(1);
2107 }
2108 return(0);
2109}
2110
2111/**
2112 * xmlTextReaderMoveToNextAttribute:
2113 * @reader: the xmlTextReaderPtr used
2114 *
2115 * Moves the position of the current instance to the next attribute
2116 * associated with the current node.
2117 *
2118 * Returns 1 in case of success, -1 in case of error, 0 if not found
2119 */
2120int
2121xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
2122 if (reader == NULL)
2123 return(-1);
2124 if (reader->node == NULL)
2125 return(-1);
2126 if (reader->node->type != XML_ELEMENT_NODE)
2127 return(0);
2128 if (reader->curnode == NULL)
2129 return(xmlTextReaderMoveToFirstAttribute(reader));
2130
2131 if (reader->curnode->type == XML_NAMESPACE_DECL) {
2132 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2133 if (ns->next != NULL) {
2134 reader->curnode = (xmlNodePtr) ns->next;
2135 return(1);
2136 }
2137 if (reader->node->properties != NULL) {
2138 reader->curnode = (xmlNodePtr) reader->node->properties;
2139 return(1);
2140 }
2141 return(0);
2142 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2143 (reader->curnode->next != NULL)) {
2144 reader->curnode = reader->curnode->next;
2145 return(1);
2146 }
2147 return(0);
2148}
2149
2150/**
2151 * xmlTextReaderMoveToElement:
2152 * @reader: the xmlTextReaderPtr used
2153 *
2154 * Moves the position of the current instance to the node that
2155 * contains the current Attribute node.
2156 *
2157 * Returns 1 in case of success, -1 in case of error, 0 if not moved
2158 */
2159int
2160xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
2161 if (reader == NULL)
2162 return(-1);
2163 if (reader->node == NULL)
2164 return(-1);
2165 if (reader->node->type != XML_ELEMENT_NODE)
2166 return(0);
2167 if (reader->curnode != NULL) {
2168 reader->curnode = NULL;
2169 return(1);
2170 }
2171 return(0);
2172}
2173
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002174/**
2175 * xmlTextReaderReadAttributeValue:
2176 * @reader: the xmlTextReaderPtr used
2177 *
2178 * Parses an attribute value into one or more Text and EntityReference nodes.
2179 *
2180 * Returns 1 in case of success, 0 if the reader was not positionned on an
2181 * ttribute node or all the attribute values have been read, or -1
2182 * in case of error.
2183 */
2184int
2185xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
2186 if (reader == NULL)
2187 return(-1);
2188 if (reader->node == NULL)
2189 return(-1);
2190 if (reader->curnode == NULL)
2191 return(0);
2192 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2193 if (reader->curnode->children == NULL)
2194 return(0);
2195 reader->curnode = reader->curnode->children;
2196 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2197 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2198
2199 if (reader->faketext == NULL) {
2200 reader->faketext = xmlNewDocText(reader->node->doc,
2201 ns->href);
2202 } else {
2203 if (reader->faketext->content != NULL)
2204 xmlFree(reader->faketext->content);
2205 reader->faketext->content = xmlStrdup(ns->href);
2206 }
2207 reader->curnode = reader->faketext;
2208 } else {
2209 if (reader->curnode->next == NULL)
2210 return(0);
2211 reader->curnode = reader->curnode->next;
2212 }
2213 return(1);
2214}
2215
Daniel Veillard0eb38c72002-12-14 23:00:35 +00002216/************************************************************************
2217 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002218 * Acces API to the current node *
2219 * *
2220 ************************************************************************/
2221/**
2222 * xmlTextReaderAttributeCount:
2223 * @reader: the xmlTextReaderPtr used
2224 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002225 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002226 *
2227 * Returns 0 i no attributes, -1 in case of error or the attribute count
2228 */
2229int
2230xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
2231 int ret;
2232 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00002233 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002234 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002235
2236 if (reader == NULL)
2237 return(-1);
2238 if (reader->node == NULL)
2239 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002240
2241 if (reader->curnode != NULL)
2242 node = reader->curnode;
2243 else
2244 node = reader->node;
2245
2246 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002247 return(0);
2248 if ((reader->state == XML_TEXTREADER_END) ||
2249 (reader->state == XML_TEXTREADER_BACKTRACK))
2250 return(0);
2251 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002252 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002253 while (attr != NULL) {
2254 ret++;
2255 attr = attr->next;
2256 }
Daniel Veillard67df8092002-12-16 22:04:11 +00002257 ns = node->nsDef;
2258 while (ns != NULL) {
2259 ret++;
2260 ns = ns->next;
2261 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002262 return(ret);
2263}
2264
2265/**
2266 * xmlTextReaderNodeType:
2267 * @reader: the xmlTextReaderPtr used
2268 *
2269 * Get the node type of the current node
2270 * Reference:
2271 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
2272 *
2273 * Returns the xmlNodeType of the current node or -1 in case of error
2274 */
2275int
2276xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002277 xmlNodePtr node;
Daniel Veillardd6038e02003-07-30 16:37:18 +00002278
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002279 if (reader == NULL)
2280 return(-1);
2281 if (reader->node == NULL)
Daniel Veillardd6038e02003-07-30 16:37:18 +00002282 return(XML_READER_TYPE_NONE);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002283 if (reader->curnode != NULL)
2284 node = reader->curnode;
2285 else
2286 node = reader->node;
2287 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002288 case XML_ELEMENT_NODE:
2289 if ((reader->state == XML_TEXTREADER_END) ||
2290 (reader->state == XML_TEXTREADER_BACKTRACK))
Daniel Veillardd6038e02003-07-30 16:37:18 +00002291 return(XML_READER_TYPE_END_ELEMENT);
2292 return(XML_READER_TYPE_ELEMENT);
Daniel Veillardecaba492002-12-30 10:55:29 +00002293 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002294 case XML_ATTRIBUTE_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002295 return(XML_READER_TYPE_ATTRIBUTE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002296 case XML_TEXT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002297 if (xmlIsBlankNode(reader->node)) {
2298 if (xmlNodeGetSpacePreserve(reader->node))
2299 return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2300 else
2301 return(XML_READER_TYPE_WHITESPACE);
2302 } else {
2303 return(XML_READER_TYPE_TEXT);
2304 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002305 case XML_CDATA_SECTION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002306 return(XML_READER_TYPE_CDATA);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002307 case XML_ENTITY_REF_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002308 return(XML_READER_TYPE_ENTITY_REFERENCE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002309 case XML_ENTITY_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002310 return(XML_READER_TYPE_ENTITY);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002311 case XML_PI_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002312 return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002313 case XML_COMMENT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002314 return(XML_READER_TYPE_COMMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002315 case XML_DOCUMENT_NODE:
2316 case XML_HTML_DOCUMENT_NODE:
2317#ifdef LIBXML_DOCB_ENABLED
2318 case XML_DOCB_DOCUMENT_NODE:
2319#endif
Daniel Veillardd6038e02003-07-30 16:37:18 +00002320 return(XML_READER_TYPE_DOCUMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002321 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002322 return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002323 case XML_NOTATION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002324 return(XML_READER_TYPE_NOTATION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002325 case XML_DOCUMENT_TYPE_NODE:
2326 case XML_DTD_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002327 return(XML_READER_TYPE_DOCUMENT_TYPE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002328
2329 case XML_ELEMENT_DECL:
2330 case XML_ATTRIBUTE_DECL:
2331 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002332 case XML_XINCLUDE_START:
2333 case XML_XINCLUDE_END:
Daniel Veillardd6038e02003-07-30 16:37:18 +00002334 return(XML_READER_TYPE_NONE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002335 }
2336 return(-1);
2337}
2338
2339/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002340 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002341 * @reader: the xmlTextReaderPtr used
2342 *
2343 * Check if the current node is empty
2344 *
2345 * Returns 1 if empty, 0 if not and -1 in case of error
2346 */
2347int
2348xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
2349 if ((reader == NULL) || (reader->node == NULL))
2350 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00002351 if (reader->node->type != XML_ELEMENT_NODE)
2352 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00002353 if (reader->curnode != NULL)
2354 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002355 if (reader->node->children != NULL)
2356 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00002357 if (reader->state == XML_TEXTREADER_END)
2358 return(0);
Daniel Veillard9ee35f32003-09-28 00:19:54 +00002359 return((reader->node->_private == (void *)xmlTextReaderIsEmpty) ||
2360 (reader->node->_private == (void *)xmlTextReaderIsEmptyPreserved));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002361}
2362
2363/**
2364 * xmlTextReaderLocalName:
2365 * @reader: the xmlTextReaderPtr used
2366 *
2367 * The local name of the node.
2368 *
2369 * Returns the local name or NULL if not available
2370 */
2371xmlChar *
2372xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002373 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002374 if ((reader == NULL) || (reader->node == NULL))
2375 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002376 if (reader->curnode != NULL)
2377 node = reader->curnode;
2378 else
2379 node = reader->node;
2380 if (node->type == XML_NAMESPACE_DECL) {
2381 xmlNsPtr ns = (xmlNsPtr) node;
2382 if (ns->prefix == NULL)
2383 return(xmlStrdup(BAD_CAST "xmlns"));
2384 else
2385 return(xmlStrdup(ns->prefix));
2386 }
2387 if ((node->type != XML_ELEMENT_NODE) &&
2388 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002389 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002390 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002391}
2392
2393/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002394 * xmlTextReaderConstLocalName:
2395 * @reader: the xmlTextReaderPtr used
2396 *
2397 * The local name of the node.
2398 *
2399 * Returns the local name or NULL if not available, the
2400 * string will be deallocated with the reader.
2401 */
2402const xmlChar *
2403xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
2404 xmlNodePtr node;
2405 if ((reader == NULL) || (reader->node == NULL))
2406 return(NULL);
2407 if (reader->curnode != NULL)
2408 node = reader->curnode;
2409 else
2410 node = reader->node;
2411 if (node->type == XML_NAMESPACE_DECL) {
2412 xmlNsPtr ns = (xmlNsPtr) node;
2413 if (ns->prefix == NULL)
2414 return(CONSTSTR(BAD_CAST "xmlns"));
2415 else
2416 return(ns->prefix);
2417 }
2418 if ((node->type != XML_ELEMENT_NODE) &&
2419 (node->type != XML_ATTRIBUTE_NODE))
2420 return(xmlTextReaderConstName(reader));
2421 return(node->name);
2422}
2423
2424/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002425 * xmlTextReaderName:
2426 * @reader: the xmlTextReaderPtr used
2427 *
2428 * The qualified name of the node, equal to Prefix :LocalName.
2429 *
2430 * Returns the local name or NULL if not available
2431 */
2432xmlChar *
2433xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002434 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002435 xmlChar *ret;
2436
2437 if ((reader == NULL) || (reader->node == NULL))
2438 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002439 if (reader->curnode != NULL)
2440 node = reader->curnode;
2441 else
2442 node = reader->node;
2443 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002444 case XML_ELEMENT_NODE:
2445 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002446 if ((node->ns == NULL) ||
2447 (node->ns->prefix == NULL))
2448 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002449
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002450 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002451 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002452 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002453 return(ret);
2454 case XML_TEXT_NODE:
2455 return(xmlStrdup(BAD_CAST "#text"));
2456 case XML_CDATA_SECTION_NODE:
2457 return(xmlStrdup(BAD_CAST "#cdata-section"));
2458 case XML_ENTITY_NODE:
2459 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002460 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002461 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002462 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002463 case XML_COMMENT_NODE:
2464 return(xmlStrdup(BAD_CAST "#comment"));
2465 case XML_DOCUMENT_NODE:
2466 case XML_HTML_DOCUMENT_NODE:
2467#ifdef LIBXML_DOCB_ENABLED
2468 case XML_DOCB_DOCUMENT_NODE:
2469#endif
2470 return(xmlStrdup(BAD_CAST "#document"));
2471 case XML_DOCUMENT_FRAG_NODE:
2472 return(xmlStrdup(BAD_CAST "#document-fragment"));
2473 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002474 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002475 case XML_DOCUMENT_TYPE_NODE:
2476 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002477 return(xmlStrdup(node->name));
2478 case XML_NAMESPACE_DECL: {
2479 xmlNsPtr ns = (xmlNsPtr) node;
2480
2481 ret = xmlStrdup(BAD_CAST "xmlns");
2482 if (ns->prefix == NULL)
2483 return(ret);
2484 ret = xmlStrcat(ret, BAD_CAST ":");
2485 ret = xmlStrcat(ret, ns->prefix);
2486 return(ret);
2487 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002488
2489 case XML_ELEMENT_DECL:
2490 case XML_ATTRIBUTE_DECL:
2491 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002492 case XML_XINCLUDE_START:
2493 case XML_XINCLUDE_END:
2494 return(NULL);
2495 }
2496 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002497}
2498
2499/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002500 * xmlTextReaderConstName:
2501 * @reader: the xmlTextReaderPtr used
2502 *
2503 * The qualified name of the node, equal to Prefix :LocalName.
2504 *
2505 * Returns the local name or NULL if not available, the string is
2506 * deallocated with the reader.
2507 */
2508const xmlChar *
2509xmlTextReaderConstName(xmlTextReaderPtr reader) {
2510 xmlNodePtr node;
2511
2512 if ((reader == NULL) || (reader->node == NULL))
2513 return(NULL);
2514 if (reader->curnode != NULL)
2515 node = reader->curnode;
2516 else
2517 node = reader->node;
2518 switch (node->type) {
2519 case XML_ELEMENT_NODE:
2520 case XML_ATTRIBUTE_NODE:
2521 if ((node->ns == NULL) ||
2522 (node->ns->prefix == NULL))
2523 return(node->name);
2524 return(CONSTQSTR(node->ns->prefix, node->name));
2525 case XML_TEXT_NODE:
2526 return(CONSTSTR(BAD_CAST "#text"));
2527 case XML_CDATA_SECTION_NODE:
2528 return(CONSTSTR(BAD_CAST "#cdata-section"));
2529 case XML_ENTITY_NODE:
2530 case XML_ENTITY_REF_NODE:
2531 return(CONSTSTR(node->name));
2532 case XML_PI_NODE:
2533 return(CONSTSTR(node->name));
2534 case XML_COMMENT_NODE:
2535 return(CONSTSTR(BAD_CAST "#comment"));
2536 case XML_DOCUMENT_NODE:
2537 case XML_HTML_DOCUMENT_NODE:
2538#ifdef LIBXML_DOCB_ENABLED
2539 case XML_DOCB_DOCUMENT_NODE:
2540#endif
2541 return(CONSTSTR(BAD_CAST "#document"));
2542 case XML_DOCUMENT_FRAG_NODE:
2543 return(CONSTSTR(BAD_CAST "#document-fragment"));
2544 case XML_NOTATION_NODE:
2545 return(CONSTSTR(node->name));
2546 case XML_DOCUMENT_TYPE_NODE:
2547 case XML_DTD_NODE:
2548 return(CONSTSTR(node->name));
2549 case XML_NAMESPACE_DECL: {
2550 xmlNsPtr ns = (xmlNsPtr) node;
2551
2552 if (ns->prefix == NULL)
2553 return(CONSTSTR(BAD_CAST "xmlns"));
2554 return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
2555 }
2556
2557 case XML_ELEMENT_DECL:
2558 case XML_ATTRIBUTE_DECL:
2559 case XML_ENTITY_DECL:
2560 case XML_XINCLUDE_START:
2561 case XML_XINCLUDE_END:
2562 return(NULL);
2563 }
2564 return(NULL);
2565}
2566
2567/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002568 * xmlTextReaderPrefix:
2569 * @reader: the xmlTextReaderPtr used
2570 *
2571 * A shorthand reference to the namespace associated with the node.
2572 *
2573 * Returns the prefix or NULL if not available
2574 */
2575xmlChar *
2576xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002577 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002578 if ((reader == NULL) || (reader->node == NULL))
2579 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002580 if (reader->curnode != NULL)
2581 node = reader->curnode;
2582 else
2583 node = reader->node;
2584 if (node->type == XML_NAMESPACE_DECL) {
2585 xmlNsPtr ns = (xmlNsPtr) node;
2586 if (ns->prefix == NULL)
2587 return(NULL);
2588 return(xmlStrdup(BAD_CAST "xmlns"));
2589 }
2590 if ((node->type != XML_ELEMENT_NODE) &&
2591 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002592 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00002593 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002594 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002595 return(NULL);
2596}
2597
2598/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002599 * xmlTextReaderConstPrefix:
2600 * @reader: the xmlTextReaderPtr used
2601 *
2602 * A shorthand reference to the namespace associated with the node.
2603 *
2604 * Returns the prefix or NULL if not available, the string is deallocated
2605 * with the reader.
2606 */
2607const xmlChar *
2608xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
2609 xmlNodePtr node;
2610 if ((reader == NULL) || (reader->node == NULL))
2611 return(NULL);
2612 if (reader->curnode != NULL)
2613 node = reader->curnode;
2614 else
2615 node = reader->node;
2616 if (node->type == XML_NAMESPACE_DECL) {
2617 xmlNsPtr ns = (xmlNsPtr) node;
2618 if (ns->prefix == NULL)
2619 return(NULL);
2620 return(CONSTSTR(BAD_CAST "xmlns"));
2621 }
2622 if ((node->type != XML_ELEMENT_NODE) &&
2623 (node->type != XML_ATTRIBUTE_NODE))
2624 return(NULL);
2625 if ((node->ns != NULL) && (node->ns->prefix != NULL))
2626 return(CONSTSTR(node->ns->prefix));
2627 return(NULL);
2628}
2629
2630/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002631 * xmlTextReaderNamespaceUri:
2632 * @reader: the xmlTextReaderPtr used
2633 *
2634 * The URI defining the namespace associated with the node.
2635 *
2636 * Returns the namespace URI or NULL if not available
2637 */
2638xmlChar *
2639xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002640 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002641 if ((reader == NULL) || (reader->node == NULL))
2642 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002643 if (reader->curnode != NULL)
2644 node = reader->curnode;
2645 else
2646 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00002647 if (node->type == XML_NAMESPACE_DECL)
2648 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002649 if ((node->type != XML_ELEMENT_NODE) &&
2650 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002651 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002652 if (node->ns != NULL)
2653 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002654 return(NULL);
2655}
2656
2657/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002658 * xmlTextReaderConstNamespaceUri:
2659 * @reader: the xmlTextReaderPtr used
2660 *
2661 * The URI defining the namespace associated with the node.
2662 *
2663 * Returns the namespace URI or NULL if not available, the string
2664 * will be deallocated with the reader
2665 */
2666const xmlChar *
2667xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
2668 xmlNodePtr node;
2669 if ((reader == NULL) || (reader->node == NULL))
2670 return(NULL);
2671 if (reader->curnode != NULL)
2672 node = reader->curnode;
2673 else
2674 node = reader->node;
2675 if (node->type == XML_NAMESPACE_DECL)
2676 return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
2677 if ((node->type != XML_ELEMENT_NODE) &&
2678 (node->type != XML_ATTRIBUTE_NODE))
2679 return(NULL);
2680 if (node->ns != NULL)
2681 return(CONSTSTR(node->ns->href));
2682 return(NULL);
2683}
2684
2685/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002686 * xmlTextReaderBaseUri:
2687 * @reader: the xmlTextReaderPtr used
2688 *
2689 * The base URI of the node.
2690 *
2691 * Returns the base URI or NULL if not available
2692 */
2693xmlChar *
2694xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
2695 if ((reader == NULL) || (reader->node == NULL))
2696 return(NULL);
2697 return(xmlNodeGetBase(NULL, reader->node));
2698}
2699
2700/**
Daniel Veillarde72c5082003-09-19 12:44:05 +00002701 * xmlTextReaderConstBaseUri:
2702 * @reader: the xmlTextReaderPtr used
2703 *
2704 * The base URI of the node.
2705 *
2706 * Returns the base URI or NULL if not available, the string
2707 * will be deallocated with the reader
2708 */
2709const xmlChar *
2710xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
2711 xmlChar *tmp;
2712 const xmlChar *ret;
2713
2714 if ((reader == NULL) || (reader->node == NULL))
2715 return(NULL);
2716 tmp = xmlNodeGetBase(NULL, reader->node);
2717 if (tmp == NULL)
2718 return(NULL);
2719 ret = CONSTSTR(tmp);
2720 xmlFree(tmp);
2721 return(ret);
2722}
2723
2724/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002725 * xmlTextReaderDepth:
2726 * @reader: the xmlTextReaderPtr used
2727 *
2728 * The depth of the node in the tree.
2729 *
2730 * Returns the depth or -1 in case of error
2731 */
2732int
2733xmlTextReaderDepth(xmlTextReaderPtr reader) {
2734 if (reader == NULL)
2735 return(-1);
2736 if (reader->node == NULL)
2737 return(0);
2738
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002739 if (reader->curnode != NULL) {
2740 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
2741 (reader->curnode->type == XML_NAMESPACE_DECL))
2742 return(reader->depth + 1);
2743 return(reader->depth + 2);
2744 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002745 return(reader->depth);
2746}
2747
2748/**
2749 * xmlTextReaderHasAttributes:
2750 * @reader: the xmlTextReaderPtr used
2751 *
2752 * Whether the node has attributes.
2753 *
2754 * Returns 1 if true, 0 if false, and -1 in case or error
2755 */
2756int
2757xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002758 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002759 if (reader == NULL)
2760 return(-1);
2761 if (reader->node == NULL)
2762 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002763 if (reader->curnode != NULL)
2764 node = reader->curnode;
2765 else
2766 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002767
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002768 if ((node->type == XML_ELEMENT_NODE) &&
2769 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002770 return(1);
2771 /* TODO: handle the xmlDecl */
2772 return(0);
2773}
2774
2775/**
2776 * xmlTextReaderHasValue:
2777 * @reader: the xmlTextReaderPtr used
2778 *
2779 * Whether the node can have a text value.
2780 *
2781 * Returns 1 if true, 0 if false, and -1 in case or error
2782 */
2783int
2784xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002785 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002786 if (reader == NULL)
2787 return(-1);
2788 if (reader->node == NULL)
2789 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002790 if (reader->curnode != NULL)
2791 node = reader->curnode;
2792 else
2793 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002794
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002795 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002796 case XML_ATTRIBUTE_NODE:
2797 case XML_TEXT_NODE:
2798 case XML_CDATA_SECTION_NODE:
2799 case XML_PI_NODE:
2800 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00002801 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002802 return(1);
2803 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002804 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002805 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002806 return(0);
2807}
2808
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002809/**
2810 * xmlTextReaderValue:
2811 * @reader: the xmlTextReaderPtr used
2812 *
2813 * Provides the text value of the node if present
2814 *
2815 * Returns the string or NULL if not available. The retsult must be deallocated
2816 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002817 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002818xmlChar *
2819xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002820 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002821 if (reader == NULL)
2822 return(NULL);
2823 if (reader->node == NULL)
2824 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002825 if (reader->curnode != NULL)
2826 node = reader->curnode;
2827 else
2828 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002829
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002830 switch (node->type) {
2831 case XML_NAMESPACE_DECL:
2832 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002833 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002834 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002835
2836 if (attr->parent != NULL)
2837 return (xmlNodeListGetString
2838 (attr->parent->doc, attr->children, 1));
2839 else
2840 return (xmlNodeListGetString(NULL, attr->children, 1));
2841 break;
2842 }
2843 case XML_TEXT_NODE:
2844 case XML_CDATA_SECTION_NODE:
2845 case XML_PI_NODE:
2846 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002847 if (node->content != NULL)
2848 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002849 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002850 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002851 }
2852 return(NULL);
2853}
2854
2855/**
2856 * xmlTextReaderIsDefault:
2857 * @reader: the xmlTextReaderPtr used
2858 *
2859 * Whether an Attribute node was generated from the default value
2860 * defined in the DTD or schema.
2861 *
2862 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
2863 */
2864int
2865xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
2866 if (reader == NULL)
2867 return(-1);
2868 return(0);
2869}
2870
2871/**
2872 * xmlTextReaderQuoteChar:
2873 * @reader: the xmlTextReaderPtr used
2874 *
2875 * The quotation mark character used to enclose the value of an attribute.
2876 *
2877 * Returns " or ' and -1 in case of error
2878 */
2879int
2880xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
2881 if (reader == NULL)
2882 return(-1);
2883 /* TODO maybe lookup the attribute value for " first */
2884 return((int) '"');
2885}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002886
2887/**
2888 * xmlTextReaderXmlLang:
2889 * @reader: the xmlTextReaderPtr used
2890 *
2891 * The xml:lang scope within which the node resides.
2892 *
2893 * Returns the xml:lang value or NULL if none exists.
2894 */
2895xmlChar *
2896xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
2897 if (reader == NULL)
2898 return(NULL);
2899 if (reader->node == NULL)
2900 return(NULL);
2901 return(xmlNodeGetLang(reader->node));
2902}
2903
Daniel Veillard67df8092002-12-16 22:04:11 +00002904/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00002905 * xmlTextReaderConstXmlLang:
Daniel Veillarde72c5082003-09-19 12:44:05 +00002906 * @reader: the xmlTextReaderPtr used
2907 *
2908 * The xml:lang scope within which the node resides.
2909 *
2910 * Returns the xml:lang value or NULL if none exists.
2911 */
2912const xmlChar *
2913xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
2914 xmlChar *tmp;
2915 const xmlChar *ret;
2916
2917 if (reader == NULL)
2918 return(NULL);
2919 if (reader->node == NULL)
2920 return(NULL);
2921 tmp = xmlNodeGetLang(reader->node);
2922 if (tmp == NULL)
2923 return(NULL);
2924 ret = CONSTSTR(tmp);
2925 xmlFree(tmp);
2926 return(ret);
2927}
2928
2929/**
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00002930 * xmlTextReaderConstString:
2931 * @reader: the xmlTextReaderPtr used
2932 * @str: the string to intern.
2933 *
2934 * Get an interned string from the reader, allows for example to
2935 * speedup string name comparisons
2936 *
2937 * Returns an interned copy of the string or NULL in case of error. The
2938 * string will be deallocated with the reader.
2939 */
2940const xmlChar *
2941xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
2942 if (reader == NULL)
2943 return(NULL);
2944 return(CONSTSTR(str));
2945}
2946
2947/**
Daniel Veillard67df8092002-12-16 22:04:11 +00002948 * xmlTextReaderNormalization:
2949 * @reader: the xmlTextReaderPtr used
2950 *
2951 * The value indicating whether to normalize white space and attribute values.
2952 * Since attribute value and end of line normalizations are a MUST in the XML
2953 * specification only the value true is accepted. The broken bahaviour of
2954 * accepting out of range character entities like &#0; is of course not
2955 * supported either.
2956 *
2957 * Returns 1 or -1 in case of error.
2958 */
2959int
2960xmlTextReaderNormalization(xmlTextReaderPtr reader) {
2961 if (reader == NULL)
2962 return(-1);
2963 return(1);
2964}
2965
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002966/************************************************************************
2967 * *
2968 * Extensions to the base APIs *
2969 * *
2970 ************************************************************************/
2971
2972/**
2973 * xmlTextReaderSetParserProp:
2974 * @reader: the xmlTextReaderPtr used
2975 * @prop: the xmlParserProperties to set
2976 * @value: usually 0 or 1 to (de)activate it
2977 *
2978 * Change the parser processing behaviour by changing some of its internal
2979 * properties. Note that some properties can only be changed before any
2980 * read has been done.
2981 *
2982 * Returns 0 if the call was successful, or -1 in case of error
2983 */
2984int
2985xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
2986 xmlParserProperties p = (xmlParserProperties) prop;
2987 xmlParserCtxtPtr ctxt;
2988
2989 if ((reader == NULL) || (reader->ctxt == NULL))
2990 return(-1);
2991 ctxt = reader->ctxt;
2992
2993 switch (p) {
2994 case XML_PARSER_LOADDTD:
2995 if (value != 0) {
2996 if (ctxt->loadsubset == 0) {
2997 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2998 return(-1);
2999 ctxt->loadsubset = XML_DETECT_IDS;
3000 }
3001 } else {
3002 ctxt->loadsubset = 0;
3003 }
3004 return(0);
3005 case XML_PARSER_DEFAULTATTRS:
3006 if (value != 0) {
3007 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
3008 } else {
3009 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3010 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
3011 }
3012 return(0);
3013 case XML_PARSER_VALIDATE:
3014 if (value != 0) {
3015 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00003016 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003017 } else {
3018 ctxt->validate = 0;
3019 }
3020 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003021 case XML_PARSER_SUBST_ENTITIES:
3022 if (value != 0) {
3023 ctxt->replaceEntities = 1;
3024 } else {
3025 ctxt->replaceEntities = 0;
3026 }
3027 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003028 }
3029 return(-1);
3030}
3031
3032/**
3033 * xmlTextReaderGetParserProp:
3034 * @reader: the xmlTextReaderPtr used
3035 * @prop: the xmlParserProperties to get
3036 *
3037 * Read the parser internal property.
3038 *
3039 * Returns the value, usually 0 or 1, or -1 in case of error.
3040 */
3041int
3042xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
3043 xmlParserProperties p = (xmlParserProperties) prop;
3044 xmlParserCtxtPtr ctxt;
3045
3046 if ((reader == NULL) || (reader->ctxt == NULL))
3047 return(-1);
3048 ctxt = reader->ctxt;
3049
3050 switch (p) {
3051 case XML_PARSER_LOADDTD:
3052 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
3053 return(1);
3054 return(0);
3055 case XML_PARSER_DEFAULTATTRS:
3056 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3057 return(1);
3058 return(0);
3059 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00003060 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003061 case XML_PARSER_SUBST_ENTITIES:
3062 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003063 }
3064 return(-1);
3065}
3066
Daniel Veillarde18fc182002-12-28 22:56:33 +00003067/**
3068 * xmlTextReaderCurrentNode:
3069 * @reader: the xmlTextReaderPtr used
3070 *
3071 * Hacking interface allowing to get the xmlNodePtr correponding to the
3072 * current node being accessed by the xmlTextReader. This is dangerous
3073 * because the underlying node may be destroyed on the next Reads.
3074 *
3075 * Returns the xmlNodePtr or NULL in case of error.
3076 */
3077xmlNodePtr
3078xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
3079 if (reader == NULL)
3080 return(NULL);
3081
3082 if (reader->curnode != NULL)
3083 return(reader->curnode);
3084 return(reader->node);
3085}
3086
3087/**
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003088 * xmlTextReaderPreserve:
3089 * @reader: the xmlTextReaderPtr used
3090 *
3091 *
3092 * current node being accessed by the xmlTextReader. This is dangerous
3093 * because the underlying node may be destroyed on the next Reads.
3094 *
3095 * Returns the xmlNodePtr or NULL in case of error.
3096 */
3097xmlNodePtr
3098xmlTextReaderPreserve(xmlTextReaderPtr reader) {
3099 xmlNodePtr cur, parent;
3100
3101 if (reader == NULL)
3102 return(NULL);
3103
3104 if (reader->curnode != NULL)
3105 cur = reader->curnode;
3106 else
3107 cur = reader->node;
3108 if (cur == NULL)
3109 return(NULL);
3110 if (cur->_private == (void *)xmlTextReaderIsEmpty)
3111 cur->_private = (void *)xmlTextReaderIsEmptyPreserved;
3112 else
3113 cur->_private = (void *)xmlTextReaderIsPreserved;
3114
3115 parent = cur->parent;;
3116 while (parent != NULL) {
3117 parent->_private = (void *)xmlTextReaderIsPreserved;
3118 parent = parent->parent;
3119 }
3120 return(cur);
3121}
3122
3123/**
Daniel Veillarde18fc182002-12-28 22:56:33 +00003124 * xmlTextReaderCurrentDoc:
3125 * @reader: the xmlTextReaderPtr used
3126 *
3127 * Hacking interface allowing to get the xmlDocPtr correponding to the
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003128 * current document being accessed by the xmlTextReader.
3129 * NOTE: as a result of this call, the reader will not destroy the
3130 * associated XML document and calling xmlFreeDoc() on the result
3131 * is needed once the reader parsing has finished.
Daniel Veillarde18fc182002-12-28 22:56:33 +00003132 *
3133 * Returns the xmlDocPtr or NULL in case of error.
3134 */
3135xmlDocPtr
3136xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003137 if ((reader == NULL) || (reader->ctxt == NULL) ||
3138 (reader->ctxt->myDoc == NULL))
Daniel Veillarde18fc182002-12-28 22:56:33 +00003139 return(NULL);
3140
Daniel Veillard9ee35f32003-09-28 00:19:54 +00003141 reader->preserve = 1;
3142 if ((reader->ctxt->myDoc->dict != NULL) &&
3143 (reader->ctxt->myDoc->dict == reader->ctxt->dict))
3144 xmlDictReference(reader->ctxt->dict);
Daniel Veillarde18fc182002-12-28 22:56:33 +00003145 return(reader->ctxt->myDoc);
3146}
3147
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003148#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00003149/**
Daniel Veillard33300b42003-04-17 09:09:19 +00003150 * xmlTextReaderRelaxNGSetSchema:
3151 * @reader: the xmlTextReaderPtr used
3152 * @schema: a precompiled RelaxNG schema
3153 *
3154 * Use RelaxNG to validate the document as it is processed.
3155 * Activation is only possible before the first Read().
3156 * if @schema is NULL, then RelaxNG validation is desactivated.
3157 @ The @schema should not be freed until the reader is deallocated
3158 * or its use has been deactivated.
3159 *
3160 * Returns 0 in case the RelaxNG validation could be (des)activated and
3161 * -1 in case of error.
3162 */
3163int
3164xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
3165 if (schema == NULL) {
3166 if (reader->rngSchemas != NULL) {
3167 xmlRelaxNGFree(reader->rngSchemas);
3168 reader->rngSchemas = NULL;
3169 }
3170 if (reader->rngValidCtxt != NULL) {
3171 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3172 reader->rngValidCtxt = NULL;
3173 }
3174 return(0);
3175 }
3176 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3177 return(-1);
3178 if (reader->rngSchemas != NULL) {
3179 xmlRelaxNGFree(reader->rngSchemas);
3180 reader->rngSchemas = NULL;
3181 }
3182 if (reader->rngValidCtxt != NULL) {
3183 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3184 reader->rngValidCtxt = NULL;
3185 }
3186 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
3187 if (reader->rngValidCtxt == NULL)
3188 return(-1);
3189 if (reader->errorFunc != NULL) {
3190 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3191 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3192 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3193 reader->errorFuncArg);
3194 }
3195 reader->rngValidErrors = 0;
3196 reader->rngFullNode = NULL;
3197 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3198 return(0);
3199}
3200
3201/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00003202 * xmlTextReaderRelaxNGValidate:
3203 * @reader: the xmlTextReaderPtr used
3204 * @rng: the path to a RelaxNG schema or NULL
3205 *
3206 * Use RelaxNG to validate the document as it is processed.
3207 * Activation is only possible before the first Read().
3208 * if @rng is NULL, then RelaxNG validation is desactivated.
3209 *
3210 * Returns 0 in case the RelaxNG validation could be (des)activated and
3211 * -1 in case of error.
3212 */
3213int
3214xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
3215 xmlRelaxNGParserCtxtPtr ctxt;
3216
3217 if (reader == NULL)
3218 return(-1);
3219
3220 if (rng == NULL) {
3221 if (reader->rngSchemas != NULL) {
3222 xmlRelaxNGFree(reader->rngSchemas);
3223 reader->rngSchemas = NULL;
3224 }
3225 if (reader->rngValidCtxt != NULL) {
3226 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3227 reader->rngValidCtxt = NULL;
3228 }
3229 return(0);
3230 }
3231 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3232 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00003233 if (reader->rngSchemas != NULL) {
3234 xmlRelaxNGFree(reader->rngSchemas);
3235 reader->rngSchemas = NULL;
3236 }
3237 if (reader->rngValidCtxt != NULL) {
3238 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3239 reader->rngValidCtxt = NULL;
3240 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00003241 ctxt = xmlRelaxNGNewParserCtxt(rng);
3242 if (reader->errorFunc != NULL) {
3243 xmlRelaxNGSetParserErrors(ctxt,
3244 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
3245 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3246 reader->errorFuncArg);
3247 }
3248 reader->rngSchemas = xmlRelaxNGParse(ctxt);
3249 xmlRelaxNGFreeParserCtxt(ctxt);
3250 if (reader->rngSchemas == NULL)
3251 return(-1);
3252 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
3253 if (reader->rngValidCtxt == NULL)
3254 return(-1);
3255 if (reader->errorFunc != NULL) {
3256 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
3257 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
3258 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
3259 reader->errorFuncArg);
3260 }
3261 reader->rngValidErrors = 0;
3262 reader->rngFullNode = NULL;
3263 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3264 return(0);
3265}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00003266#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00003267
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003268/************************************************************************
3269 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00003270 * Error Handling Extensions *
3271 * *
3272 ************************************************************************/
3273
3274/* helper to build a xmlMalloc'ed string from a format and va_list */
3275static char *
3276xmlTextReaderBuildMessage(const char *msg, va_list ap) {
3277 int size;
3278 int chars;
3279 char *larger;
3280 char *str;
3281
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003282 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00003283 if (str == NULL) {
3284 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
3285 return NULL;
3286 }
3287
3288 size = 150;
3289
3290 while (1) {
3291 chars = vsnprintf(str, size, msg, ap);
3292 if ((chars > -1) && (chars < size))
3293 break;
3294 if (chars > -1)
3295 size += chars + 1;
3296 else
3297 size += 100;
3298 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
3299 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
3300 xmlFree(str);
3301 return NULL;
3302 }
3303 str = larger;
3304 }
3305
3306 return str;
3307}
3308
Daniel Veillard417be3a2003-01-20 21:26:34 +00003309/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003310 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003311 * @locator: the xmlTextReaderLocatorPtr used
3312 *
3313 * Obtain the line number for the given locator.
3314 *
3315 * Returns the line number or -1 in case of error.
3316 */
3317int
3318xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
3319 /* we know that locator is a xmlParserCtxtPtr */
3320 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3321 int ret = -1;
3322
3323 if (ctx->node != NULL) {
3324 ret = xmlGetLineNo(ctx->node);
3325 }
3326 else {
3327 /* inspired from error.c */
3328 xmlParserInputPtr input;
3329 input = ctx->input;
3330 if ((input->filename == NULL) && (ctx->inputNr > 1))
3331 input = ctx->inputTab[ctx->inputNr - 2];
3332 if (input != NULL) {
3333 ret = input->line;
3334 }
3335 else {
3336 ret = -1;
3337 }
3338 }
3339
3340 return ret;
3341}
3342
3343/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00003344 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00003345 * @locator: the xmlTextReaderLocatorPtr used
3346 *
3347 * Obtain the base URI for the given locator.
3348 *
3349 * Returns the base URI or NULL in case of error.
3350 */
3351xmlChar *
3352xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
3353 /* we know that locator is a xmlParserCtxtPtr */
3354 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
3355 xmlChar *ret = NULL;
3356
3357 if (ctx->node != NULL) {
3358 ret = xmlNodeGetBase(NULL,ctx->node);
3359 }
3360 else {
3361 /* inspired from error.c */
3362 xmlParserInputPtr input;
3363 input = ctx->input;
3364 if ((input->filename == NULL) && (ctx->inputNr > 1))
3365 input = ctx->inputTab[ctx->inputNr - 2];
3366 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00003367 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00003368 }
3369 else {
3370 ret = NULL;
3371 }
3372 }
3373
3374 return ret;
3375}
3376
Daniel Veillard26f70262003-01-16 22:45:08 +00003377static void
William M. Brack899e64a2003-09-26 18:03:42 +00003378xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity, char *str) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003379 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
3380 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
3381
3382 if (str != NULL) {
3383 reader->errorFunc(reader->errorFuncArg,
3384 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003385 severity,
3386 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00003387 xmlFree(str);
3388 }
3389}
3390
3391static void
3392xmlTextReaderError(void *ctxt, const char *msg, ...) {
3393 va_list ap;
3394
3395 va_start(ap,msg);
3396 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003397 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00003398 xmlTextReaderBuildMessage(msg,ap));
3399 va_end(ap);
3400
3401}
3402
3403static void
3404xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
3405 va_list ap;
3406
3407 va_start(ap,msg);
3408 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003409 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00003410 xmlTextReaderBuildMessage(msg,ap));
3411 va_end(ap);
3412}
3413
3414static void
3415xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
3416 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003417 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003418
Daniel Veillard417be3a2003-01-20 21:26:34 +00003419 if ((len > 1) && (msg[len - 2] != ':')) {
3420 /*
3421 * some callbacks only report locator information:
3422 * skip them (mimicking behaviour in error.c)
3423 */
3424 va_start(ap,msg);
3425 xmlTextReaderGenericError(ctxt,
3426 XML_PARSER_SEVERITY_VALIDITY_ERROR,
3427 xmlTextReaderBuildMessage(msg,ap));
3428 va_end(ap);
3429 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003430}
3431
3432static void
3433xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
3434 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00003435 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00003436
Daniel Veillard417be3a2003-01-20 21:26:34 +00003437 if ((len != 0) && (msg[len - 1] != ':')) {
3438 /*
3439 * some callbacks only report locator information:
3440 * skip them (mimicking behaviour in error.c)
3441 */
3442 va_start(ap,msg);
3443 xmlTextReaderGenericError(ctxt,
3444 XML_PARSER_SEVERITY_VALIDITY_WARNING,
3445 xmlTextReaderBuildMessage(msg,ap));
3446 va_end(ap);
3447 }
Daniel Veillard26f70262003-01-16 22:45:08 +00003448}
3449
3450/**
3451 * xmlTextReaderSetErrorHandler:
3452 * @reader: the xmlTextReaderPtr used
3453 * @f: the callback function to call on error and warnings
3454 * @arg: a user argument to pass to the callback function
3455 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00003456 * Register a callback function that will be called on error and warnings.
3457 *
Daniel Veillard26f70262003-01-16 22:45:08 +00003458 * If @f is NULL, the default error and warning handlers are restored.
3459 */
3460void
3461xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
3462 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003463 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003464 if (f != NULL) {
3465 reader->ctxt->sax->error = xmlTextReaderError;
3466 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
3467 reader->ctxt->sax->warning = xmlTextReaderWarning;
3468 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
3469 reader->errorFunc = f;
3470 reader->errorFuncArg = arg;
3471 }
3472 else {
3473 /* restore defaults */
3474 reader->ctxt->sax->error = xmlParserError;
3475 reader->ctxt->vctxt.error = xmlParserValidityError;
3476 reader->ctxt->sax->warning = xmlParserWarning;
3477 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
3478 reader->errorFunc = NULL;
3479 reader->errorFuncArg = NULL;
3480 }
3481}
3482
Daniel Veillard417be3a2003-01-20 21:26:34 +00003483/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00003484 * xmlTextReaderIsValid:
3485 * @reader: the xmlTextReaderPtr used
3486 *
3487 * Retrieve the validity status from the parser context
3488 *
3489 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
3490 */
3491int
3492xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00003493 if (reader == NULL) return(-1);
3494#ifdef LIBXML_SCHEMAS_ENABLED
3495 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
3496 return(reader->rngValidErrors == 0);
3497#endif
3498 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
3499 (reader->ctxt != NULL))
3500 return(reader->ctxt->valid);
3501 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00003502}
3503
3504/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00003505 * xmlTextReaderGetErrorHandler:
3506 * @reader: the xmlTextReaderPtr used
3507 * @f: the callback function or NULL is no callback has been registered
3508 * @arg: a user argument
3509 *
3510 * Retrieve the error callback function and user argument.
3511 */
Daniel Veillard26f70262003-01-16 22:45:08 +00003512void
3513xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
3514 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00003515 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00003516 *f = reader->errorFunc;
3517 *arg = reader->errorFuncArg;
3518}
3519
3520/************************************************************************
3521 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003522 * Utilities *
3523 * *
3524 ************************************************************************/
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00003525#ifdef NOT_USED_YET
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003526/**
3527 * xmlBase64Decode:
3528 * @in: the input buffer
3529 * @inlen: the size of the input (in), the size read from it (out)
3530 * @to: the output buffer
3531 * @tolen: the size of the output (in), the size written to (out)
3532 *
3533 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00003534 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003535 *
3536 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
3537 * 2 if there wasn't enough space on the output or -1 in case of error.
3538 */
3539static int
3540xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
3541 unsigned char *to, unsigned long *tolen) {
3542 unsigned long incur; /* current index in in[] */
3543 unsigned long inblk; /* last block index in in[] */
3544 unsigned long outcur; /* current index in out[] */
3545 unsigned long inmax; /* size of in[] */
3546 unsigned long outmax; /* size of out[] */
3547 unsigned char cur; /* the current value read from in[] */
Daniel Veillardc127adc2003-07-23 15:07:08 +00003548 unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00003549 int nbintmp; /* number of byte in intmp[] */
3550 int is_ignore; /* cur should be ignored */
3551 int is_end = 0; /* the end of the base64 was found */
3552 int retval = 1;
3553 int i;
3554
3555 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
3556 return(-1);
3557
3558 incur = 0;
3559 inblk = 0;
3560 outcur = 0;
3561 inmax = *inlen;
3562 outmax = *tolen;
3563 nbintmp = 0;
3564
3565 while (1) {
3566 if (incur >= inmax)
3567 break;
3568 cur = in[incur++];
3569 is_ignore = 0;
3570 if ((cur >= 'A') && (cur <= 'Z'))
3571 cur = cur - 'A';
3572 else if ((cur >= 'a') && (cur <= 'z'))
3573 cur = cur - 'a' + 26;
3574 else if ((cur >= '0') && (cur <= '9'))
3575 cur = cur - '0' + 52;
3576 else if (cur == '+')
3577 cur = 62;
3578 else if (cur == '/')
3579 cur = 63;
3580 else if (cur == '.')
3581 cur = 0;
3582 else if (cur == '=') /*no op , end of the base64 stream */
3583 is_end = 1;
3584 else {
3585 is_ignore = 1;
3586 if (nbintmp == 0)
3587 inblk = incur;
3588 }
3589
3590 if (!is_ignore) {
3591 int nbouttmp = 3;
3592 int is_break = 0;
3593
3594 if (is_end) {
3595 if (nbintmp == 0)
3596 break;
3597 if ((nbintmp == 1) || (nbintmp == 2))
3598 nbouttmp = 1;
3599 else
3600 nbouttmp = 2;
3601 nbintmp = 3;
3602 is_break = 1;
3603 }
3604 intmp[nbintmp++] = cur;
3605 /*
3606 * if intmp is full, push the 4byte sequence as a 3 byte
3607 * sequence out
3608 */
3609 if (nbintmp == 4) {
3610 nbintmp = 0;
3611 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
3612 outtmp[1] =
3613 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
3614 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
3615 if (outcur + 3 >= outmax) {
3616 retval = 2;
3617 break;
3618 }
3619
3620 for (i = 0; i < nbouttmp; i++)
3621 to[outcur++] = outtmp[i];
3622 inblk = incur;
3623 }
3624
3625 if (is_break) {
3626 retval = 0;
3627 break;
3628 }
3629 }
3630 }
3631
3632 *tolen = outcur;
3633 *inlen = inblk;
3634 return (retval);
3635}
3636
3637/*
3638 * Test routine for the xmlBase64Decode function
3639 */
3640#if 0
3641int main(int argc, char **argv) {
3642 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
3643 char output[100];
3644 char output2[100];
3645 char output3[100];
3646 unsigned long inlen = strlen(input);
3647 unsigned long outlen = 100;
3648 int ret;
3649 unsigned long cons, tmp, tmp2, prod;
3650
3651 /*
3652 * Direct
3653 */
3654 ret = xmlBase64Decode(input, &inlen, output, &outlen);
3655
3656 output[outlen] = 0;
3657 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
3658
3659 /*
3660 * output chunking
3661 */
3662 cons = 0;
3663 prod = 0;
3664 while (cons < inlen) {
3665 tmp = 5;
3666 tmp2 = inlen - cons;
3667
3668 printf("%ld %ld\n", cons, prod);
3669 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
3670 cons += tmp2;
3671 prod += tmp;
3672 printf("%ld %ld\n", cons, prod);
3673 }
3674 output2[outlen] = 0;
3675 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
3676
3677 /*
3678 * input chunking
3679 */
3680 cons = 0;
3681 prod = 0;
3682 while (cons < inlen) {
3683 tmp = 100 - prod;
3684 tmp2 = inlen - cons;
3685 if (tmp2 > 5)
3686 tmp2 = 5;
3687
3688 printf("%ld %ld\n", cons, prod);
3689 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
3690 cons += tmp2;
3691 prod += tmp;
3692 printf("%ld %ld\n", cons, prod);
3693 }
3694 output3[outlen] = 0;
3695 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
3696 return(0);
3697
3698}
3699#endif
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00003700#endif /* NOT_USED_YET */