blob: 4d3716504f6095e25c22373bcfcb809911618f69 [file] [log] [blame]
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001/*
2 * xmlreader.c: implements the xmlTextReader streaming node API
3 *
Daniel Veillard67df8092002-12-16 22:04:11 +00004 * NOTE:
5 * XmlTextReader.Normalization Property won't be supported, since
6 * it makes the parser non compliant to the XML recommendation
7 *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00008 * See Copyright for the status of this software.
9 *
10 * daniel@veillard.com
11 */
12
Daniel Veillard7704fb12003-01-03 16:19:51 +000013/*
14 * TODOs:
Daniel Veillard7704fb12003-01-03 16:19:51 +000015 * - provide an API to preserve part of the tree
16 * - Streaming XInclude support
Daniel Veillard067bae52003-01-05 01:27:54 +000017 * - validation against a provided DTD
18 * - XML Schemas validation
Daniel Veillard7704fb12003-01-03 16:19:51 +000019 * - setting(s) for NoBlanks
20 * - performances and tuning ...
21 */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000022#define IN_LIBXML
23#include "libxml.h"
24
25#include <string.h> /* for memset() only ! */
Daniel Veillard26f70262003-01-16 22:45:08 +000026#include <stdarg.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000027
28#ifdef HAVE_CTYPE_H
29#include <ctype.h>
30#endif
31#ifdef HAVE_STDLIB_H
32#include <stdlib.h>
33#endif
34
35#include <libxml/xmlmemory.h>
36#include <libxml/xmlIO.h>
37#include <libxml/xmlreader.h>
Daniel Veillardf4e55762003-04-15 23:32:22 +000038#include <libxml/relaxng.h>
Daniel Veillarde1ca5032002-12-09 14:13:43 +000039
40/* #define DEBUG_CALLBACKS */
41/* #define DEBUG_READER */
42
43/**
44 * TODO:
45 *
46 * macro to flag unimplemented blocks
47 */
48#define TODO \
49 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
51 __FILE__, __LINE__);
52
53#ifdef DEBUG_READER
54#define DUMP_READER xmlTextReaderDebug(reader);
55#else
56#define DUMP_READER
57#endif
58
Daniel Veillarda880b122003-04-21 21:36:41 +000059#define CHUNK_SIZE 512
Daniel Veillarde1ca5032002-12-09 14:13:43 +000060/************************************************************************
61 * *
62 * The parser: maps the Text Reader API on top of the existing *
63 * parsing routines building a tree *
64 * *
65 ************************************************************************/
66
67#define XML_TEXTREADER_INPUT 1
68#define XML_TEXTREADER_CTXT 2
69
70typedef enum {
Daniel Veillard67df8092002-12-16 22:04:11 +000071 XML_TEXTREADER_MODE_INITIAL = 0,
72 XML_TEXTREADER_MODE_INTERACTIVE = 1,
73 XML_TEXTREADER_MODE_ERROR = 2,
74 XML_TEXTREADER_MODE_EOF =3,
75 XML_TEXTREADER_MODE_CLOSED = 4,
76 XML_TEXTREADER_MODE_READING = 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000077} xmlTextReaderMode;
78
79typedef enum {
80 XML_TEXTREADER_NONE = -1,
81 XML_TEXTREADER_START= 0,
82 XML_TEXTREADER_ELEMENT= 1,
83 XML_TEXTREADER_END= 2,
84 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000085 XML_TEXTREADER_BACKTRACK= 4,
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000086 XML_TEXTREADER_DONE= 5,
87 XML_TEXTREADER_ERROR= 6
Daniel Veillarde1ca5032002-12-09 14:13:43 +000088} xmlTextReaderState;
89
Daniel Veillardf4e55762003-04-15 23:32:22 +000090typedef enum {
91 XML_TEXTREADER_NOT_VALIDATE = 0,
92 XML_TEXTREADER_VALIDATE_DTD = 1,
93 XML_TEXTREADER_VALIDATE_RNG = 2
94} xmlTextReaderValidate;
95
Daniel Veillarde1ca5032002-12-09 14:13:43 +000096struct _xmlTextReader {
97 int mode; /* the parsing mode */
Daniel Veillardf4e55762003-04-15 23:32:22 +000098 xmlTextReaderValidate validate;/* is there any validation */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000099 int allocs; /* what structure were deallocated */
100 xmlTextReaderState state;
101 xmlParserCtxtPtr ctxt; /* the parser context */
102 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
103 xmlParserInputBufferPtr input; /* the input */
104 startElementSAXFunc startElement;/* initial SAX callbacks */
105 endElementSAXFunc endElement; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000106 charactersSAXFunc characters;
107 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000108 unsigned int base; /* base of the segment in the input */
109 unsigned int cur; /* current position in the input */
110 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000111 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000112 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000113 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000114
115 /* entity stack when traversing entities content */
116 xmlNodePtr ent; /* Current Entity Ref Node */
117 int entNr; /* Depth of the entities stack */
118 int entMax; /* Max depth of the entities stack */
119 xmlNodePtr *entTab; /* array of entities */
Daniel Veillard26f70262003-01-16 22:45:08 +0000120
121 /* error handling */
122 xmlTextReaderErrorFunc errorFunc; /* callback function */
123 void *errorFuncArg; /* callback function user argument */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000124
125#ifdef LIBXML_SCHEMAS_ENABLED
126 /* Handling of RelaxNG validation */
127 xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
128 xmlRelaxNGValidCtxtPtr rngValidCtxt; /* The Relax NG validation context */
129 int rngValidErrors; /* The number of errors detected */
130 xmlNodePtr rngFullNode; /* the node if RNG not progressive */
131#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000132};
133
Daniel Veillard067bae52003-01-05 01:27:54 +0000134static const char *xmlTextReaderIsEmpty = "This element is empty";
135
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000136#ifdef DEBUG_READER
137static void
138xmlTextReaderDebug(xmlTextReaderPtr reader) {
139 if ((reader == NULL) || (reader->ctxt == NULL)) {
140 fprintf(stderr, "xmlTextReader NULL\n");
141 return;
142 }
143 fprintf(stderr, "xmlTextReader: state %d depth %d ",
144 reader->state, reader->depth);
145 if (reader->node == NULL) {
146 fprintf(stderr, "node = NULL\n");
147 } else {
148 fprintf(stderr, "node %s\n", reader->node->name);
149 }
150 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
151 reader->base, reader->cur, reader->ctxt->nodeNr);
152 if (reader->input->buffer == NULL) {
153 fprintf(stderr, "buffer is NULL\n");
154 } else {
155#ifdef LIBXML_DEBUG_ENABLED
156 xmlDebugDumpString(stderr,
157 &reader->input->buffer->content[reader->cur]);
158#endif
159 fprintf(stderr, "\n");
160 }
161}
162#endif
163
164/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000165 * xmlTextReaderEntPush:
166 * @reader: the xmlTextReaderPtr used
167 * @value: the entity reference node
168 *
169 * Pushes a new entity reference node on top of the entities stack
170 *
171 * Returns 0 in case of error, the index in the stack otherwise
172 */
173static int
174xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
175{
176 if (reader->entMax <= 0) {
177 reader->entMax = 10;
178 reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
179 sizeof(reader->entTab[0]));
180 if (reader->entTab == NULL) {
181 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
182 return (0);
183 }
184 }
185 if (reader->entNr >= reader->entMax) {
186 reader->entMax *= 2;
187 reader->entTab =
188 (xmlNodePtr *) xmlRealloc(reader->entTab,
189 reader->entMax *
190 sizeof(reader->entTab[0]));
191 if (reader->entTab == NULL) {
192 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
193 return (0);
194 }
195 }
196 reader->entTab[reader->entNr] = value;
197 reader->ent = value;
198 return (reader->entNr++);
199}
200
201/**
202 * xmlTextReaderEntPop:
203 * @reader: the xmlTextReaderPtr used
204 *
205 * Pops the top element entity from the entities stack
206 *
207 * Returns the entity just removed
208 */
209static xmlNodePtr
210xmlTextReaderEntPop(xmlTextReaderPtr reader)
211{
212 xmlNodePtr ret;
213
214 if (reader->entNr <= 0)
215 return (0);
216 reader->entNr--;
217 if (reader->entNr > 0)
218 reader->ent = reader->entTab[reader->entNr - 1];
219 else
220 reader->ent = NULL;
221 ret = reader->entTab[reader->entNr];
222 reader->entTab[reader->entNr] = 0;
223 return (ret);
224}
225
226/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000227 * xmlTextReaderStartElement:
228 * @ctx: the user data (XML parser context)
229 * @fullname: The element name, including namespace prefix
230 * @atts: An array of name/value attributes pairs, NULL terminated
231 *
232 * called when an opening tag has been processed.
233 */
234static void
235xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
236 const xmlChar **atts) {
237 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillardd5896142002-12-31 14:45:26 +0000238 xmlParserCtxtPtr origctxt;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000239 xmlTextReaderPtr reader = ctxt->_private;
240
241#ifdef DEBUG_CALLBACKS
242 printf("xmlTextReaderStartElement(%s)\n", fullname);
243#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000244 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillardd5896142002-12-31 14:45:26 +0000245 /*
246 * when processing an entity, the context may have been changed
247 */
248 origctxt = reader->ctxt;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000249 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000250 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
251 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
252 (ctxt->input->cur[1] == '>'))
253 ctxt->node->_private = (void *) xmlTextReaderIsEmpty;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000254 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000255 if (reader != NULL)
256 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000257}
258
259/**
260 * xmlTextReaderEndElement:
261 * @ctx: the user data (XML parser context)
262 * @fullname: The element name, including namespace prefix
263 *
264 * called when an ending tag has been processed.
265 */
266static void
267xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
268 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillardd5896142002-12-31 14:45:26 +0000269 xmlParserCtxtPtr origctxt;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000270 xmlTextReaderPtr reader = ctxt->_private;
271
272#ifdef DEBUG_CALLBACKS
273 printf("xmlTextReaderEndElement(%s)\n", fullname);
274#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000275 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillardd5896142002-12-31 14:45:26 +0000276 /*
277 * when processing an entity, the context may have been changed
278 */
279 origctxt = reader->ctxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000280
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000281 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000282 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000283}
284
285/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000286 * xmlTextReaderCharacters:
287 * @ctx: the user data (XML parser context)
288 * @ch: a xmlChar string
289 * @len: the number of xmlChar
290 *
291 * receiving some chars from the parser.
292 */
293static void
294xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
295{
296 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillardd5896142002-12-31 14:45:26 +0000297 xmlParserCtxtPtr origctxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000298 xmlTextReaderPtr reader = ctxt->_private;
299
300#ifdef DEBUG_CALLBACKS
301 printf("xmlTextReaderCharacters()\n");
302#endif
303 if ((reader != NULL) && (reader->characters != NULL)) {
304 reader->characters(ctx, ch, len);
Daniel Veillardd5896142002-12-31 14:45:26 +0000305 /*
306 * when processing an entity, the context may have been changed
307 */
308 origctxt = reader->ctxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000309 }
310}
311
312/**
313 * xmlTextReaderCDataBlock:
314 * @ctx: the user data (XML parser context)
315 * @value: The pcdata content
316 * @len: the block length
317 *
318 * called when a pcdata block has been parsed
319 */
320static void
321xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
322{
323 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
324 xmlTextReaderPtr reader = ctxt->_private;
325
326#ifdef DEBUG_CALLBACKS
327 printf("xmlTextReaderCDataBlock()\n");
328#endif
329 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
330 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000331 }
332}
333
334/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000335 * xmlTextReaderPushData:
336 * @reader: the xmlTextReaderPtr used
337 *
338 * Push data down the progressive parser until a significant callback
339 * got raised.
340 *
341 * Returns -1 in case of failure, 0 otherwise
342 */
343static int
344xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000345 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000346 int val, s;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000347 int oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000348
349 if ((reader->input == NULL) || (reader->input->buffer == NULL))
350 return(-1);
351
Daniel Veillardea7751d2002-12-20 00:16:24 +0000352 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000353 reader->state = XML_TEXTREADER_NONE;
354 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000355
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000356 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000357 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000358 /*
359 * Refill the buffer unless we are at the end of the stream
360 */
361 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
362 val = xmlParserInputBufferRead(reader->input, 4096);
363 if (val <= 0) {
364 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000365 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000366 if ((oldstate != XML_TEXTREADER_START) ||
367 (reader->ctxt->myDoc != NULL))
368 return(val);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000369 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000370
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000371 } else
372 break;
373 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000374 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000375 * parse by block of CHUNK_SIZE bytes, various tests show that
376 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000377 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000378 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000379 val = xmlParseChunk(reader->ctxt,
380 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000381 CHUNK_SIZE, 0);
382 reader->cur += CHUNK_SIZE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000383 if (val != 0)
384 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000385 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000386 s = inbuf->use - reader->cur;
387 val = xmlParseChunk(reader->ctxt,
388 (const char *) &inbuf->content[reader->cur],
389 s, 0);
390 reader->cur += s;
391 if (val != 0)
392 return(-1);
393 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000394 }
395 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000396
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000397 /*
398 * Discard the consumed input when needed and possible
399 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000400 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000401 if (reader->cur >= 4096) {
402 val = xmlBufferShrink(inbuf, reader->cur);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000403 if (val >= 0) {
404 reader->cur -= val;
405 }
406 }
407 }
408
409 /*
410 * At the end of the stream signal that the work is done to the Push
411 * parser.
412 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000413 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000414 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000415 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000416 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000417 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000418 s, 1);
419 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000420 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000421 if (val != 0) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000422 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000423 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000424 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000425 return(0);
426}
427
428/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000429 * xmlTextReaderValidatePush:
430 * @reader: the xmlTextReaderPtr used
431 *
432 * Push the current node for validation
433 */
434static void
435xmlTextReaderValidatePush(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000436#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000437 xmlNodePtr node = reader->node;
438
Daniel Veillardf4e55762003-04-15 23:32:22 +0000439 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
440 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
441 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
442 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
443 reader->ctxt->myDoc, node, node->name);
444 } else {
445 /* TODO use the BuildQName interface */
446 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000447
Daniel Veillardf4e55762003-04-15 23:32:22 +0000448 qname = xmlStrdup(node->ns->prefix);
449 qname = xmlStrcat(qname, BAD_CAST ":");
450 qname = xmlStrcat(qname, node->name);
451 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
452 reader->ctxt->myDoc, node, qname);
453 if (qname != NULL)
454 xmlFree(qname);
455 }
456#ifdef LIBXML_SCHEMAS_ENABLED
457 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
458 (reader->rngValidCtxt != NULL)) {
459 int ret;
460
461 if (reader->rngFullNode != NULL) return;
462 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
463 reader->ctxt->myDoc,
464 node);
465 if (ret == 0) {
466 /*
467 * this element requires a full tree
468 */
469 node = xmlTextReaderExpand(reader);
470 if (node == NULL) {
471printf("Expand failed !\n");
472 ret = -1;
473 } else {
474 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
475 reader->ctxt->myDoc,
476 node);
477 reader->rngFullNode = node;
478 }
479 }
480 if (ret != 1)
481 reader->rngValidErrors++;
482#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000483 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000484#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000485}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000486
487/**
488 * xmlTextReaderValidateCData:
489 * @reader: the xmlTextReaderPtr used
490 * @data: pointer to the CData
491 * @len: lenght of the CData block in bytes.
492 *
493 * Push some CData for validation
494 */
495static void
496xmlTextReaderValidateCData(xmlTextReaderPtr reader,
497 const xmlChar *data, int len) {
498#ifdef LIBXML_REGEXP_ENABLED
499 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
500 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
501 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
502 data, len);
503#ifdef LIBXML_SCHEMAS_ENABLED
504 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
505 (reader->rngValidCtxt != NULL)) {
506 int ret;
507
508 if (reader->rngFullNode != NULL) return;
509 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
510 if (ret != 1)
511 reader->rngValidErrors++;
512#endif
513 }
514#endif /* LIBXML_REGEXP_ENABLED */
515}
516
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000517/**
518 * xmlTextReaderValidatePop:
519 * @reader: the xmlTextReaderPtr used
520 *
521 * Pop the current node from validation
522 */
523static void
524xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000525#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000526 xmlNodePtr node = reader->node;
527
Daniel Veillardf4e55762003-04-15 23:32:22 +0000528 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
529 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
530 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
531 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
532 reader->ctxt->myDoc, node, node->name);
533 } else {
534 /* TODO use the BuildQName interface */
535 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000536
Daniel Veillardf4e55762003-04-15 23:32:22 +0000537 qname = xmlStrdup(node->ns->prefix);
538 qname = xmlStrcat(qname, BAD_CAST ":");
539 qname = xmlStrcat(qname, node->name);
540 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
541 reader->ctxt->myDoc, node, qname);
542 if (qname != NULL)
543 xmlFree(qname);
544 }
545#ifdef LIBXML_SCHEMAS_ENABLED
546 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
547 (reader->rngValidCtxt != NULL)) {
548 int ret;
549
550 if (reader->rngFullNode != NULL) {
551 if (node == reader->rngFullNode)
552 reader->rngFullNode = NULL;
553 return;
554 }
555 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
556 reader->ctxt->myDoc,
557 node);
558 if (ret != 1)
559 reader->rngValidErrors++;
560#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000561 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000562#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000563}
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000564/**
565 * xmlTextReaderValidateEntity:
566 * @reader: the xmlTextReaderPtr used
567 *
568 * Handle the validation when an entity reference is encountered and
569 * entity substitution is not activated. As a result the parser interface
570 * must walk through the entity and do the validation calls
571 */
572static void
573xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000574#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000575 xmlNodePtr oldnode = reader->node;
576 xmlNodePtr node = reader->node;
577 xmlParserCtxtPtr ctxt = reader->ctxt;
578
579 do {
580 if (node->type == XML_ENTITY_REF_NODE) {
581 /*
582 * Case where the underlying tree is not availble, lookup the entity
583 * and walk it.
584 */
585 if ((node->children == NULL) && (ctxt->sax != NULL) &&
586 (ctxt->sax->getEntity != NULL)) {
587 node->children = (xmlNodePtr)
588 ctxt->sax->getEntity(ctxt, node->name);
589 }
590
591 if ((node->children != NULL) &&
592 (node->children->type == XML_ENTITY_DECL) &&
593 (node->children->children != NULL)) {
594 xmlTextReaderEntPush(reader, node);
595 node = node->children->children;
596 continue;
597 } else {
598 /*
599 * The error has probably be raised already.
600 */
601 if (node == oldnode)
602 break;
603 node = node->next;
604 }
605 } else if (node->type == XML_ELEMENT_NODE) {
606 reader->node = node;
607 xmlTextReaderValidatePush(reader);
608 } else if ((node->type == XML_TEXT_NODE) ||
609 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000610 xmlTextReaderValidateCData(reader, node->content,
611 xmlStrlen(node->content));
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000612 }
613
614 /*
615 * go to next node
616 */
617 if (node->children != NULL) {
618 node = node->children;
619 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +0000620 } else if (node->type == XML_ELEMENT_NODE) {
621 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000622 }
623 if (node->next != NULL) {
624 node = node->next;
625 continue;
626 }
627 do {
628 node = node->parent;
629 if (node->type == XML_ELEMENT_NODE) {
630 reader->node = node;
631 xmlTextReaderValidatePop(reader);
632 }
633 if ((node->type == XML_ENTITY_DECL) &&
634 (reader->ent != NULL) && (reader->ent->children == node)) {
635 node = xmlTextReaderEntPop(reader);
636 }
637 if (node == oldnode)
638 break;
639 if (node->next != NULL) {
640 node = node->next;
641 break;
642 }
643 } while ((node != NULL) && (node != oldnode));
644 } while ((node != NULL) && (node != oldnode));
645 reader->node = oldnode;
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000646#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000647}
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000648
649
650/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000651 * xmlTextReaderGetSuccessor:
652 * @cur: the current node
653 *
654 * Get the successor of a node if available.
655 *
656 * Returns the successor node or NULL
657 */
658static xmlNodePtr
659xmlTextReaderGetSuccessor(xmlNodePtr cur) {
660 if (cur == NULL) return(NULL) ; /* ERROR */
661 if (cur->next != NULL) return(cur->next) ;
662 do {
663 cur = cur->parent;
664 if (cur == NULL) return(NULL);
665 if (cur->next != NULL) return(cur->next);
666 } while (cur != NULL);
667 return(cur);
668}
669
670/**
671 * xmlTextReaderDoExpand:
672 * @reader: the xmlTextReaderPtr used
673 *
674 * Makes sure that the current node is fully read as well as all its
675 * descendant. It means the full DOM subtree must be available at the
676 * end of the call.
677 *
678 * Returns 1 if the node was expanded successfully, 0 if there is no more
679 * nodes to read, or -1 in case of error
680 */
681static int
682xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
683 int val;
684
685 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
686 return(-1);
687
688 do {
689 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
690 return(1);
691 if (reader->mode == XML_TEXTREADER_MODE_EOF)
692 return(1);
693 val = xmlTextReaderPushData(reader);
694 if (val < 0)
695 return(-1);
696 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
697 return(1);
698}
699
700/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000701 * xmlTextReaderRead:
702 * @reader: the xmlTextReaderPtr used
703 *
704 * Moves the position of the current instance to the next node in
705 * the stream, exposing its properties.
706 *
707 * Returns 1 if the node was read successfully, 0 if there is no more
708 * nodes to read, or -1 in case of error
709 */
710int
711xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +0000712 int val, olddepth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000713 xmlTextReaderState oldstate = 0;
714 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000715
716 if ((reader == NULL) || (reader->ctxt == NULL))
717 return(-1);
718 if (reader->ctxt->wellFormed != 1)
719 return(-1);
720
721#ifdef DEBUG_READER
722 fprintf(stderr, "\nREAD ");
723 DUMP_READER
724#endif
Daniel Veillard29b3e282002-12-29 11:14:41 +0000725 reader->curnode = NULL;
Daniel Veillard67df8092002-12-16 22:04:11 +0000726 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
727 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000728 /*
729 * Initial state
730 */
731 do {
732 val = xmlTextReaderPushData(reader);
733 if (val < 0)
734 return(-1);
735 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +0000736 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
737 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000738 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +0000739 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000740 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +0000741 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000742 if (reader->node == NULL)
743 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +0000744 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000745 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +0000746 if (reader->ctxt->myDoc != NULL) {
747 reader->node = reader->ctxt->myDoc->children;
748 }
749 if (reader->node == NULL)
750 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +0000751 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000752 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +0000753 reader->depth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000754 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000755 }
756 oldstate = reader->state;
757 olddepth = reader->ctxt->nodeNr;
758 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +0000759
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000760get_next_node:
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000761 /*
762 * If we are not backtracking on ancestors or examined nodes,
763 * that the parser didn't finished or that we arent at the end
764 * of stream, continue processing.
765 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000766 while ((reader->node->next == NULL) &&
767 (reader->ctxt->nodeNr == olddepth) &&
768 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +0000769 (reader->node->children == NULL) ||
770 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +0000771 (reader->node->type == XML_DTD_NODE) ||
772 (reader->node->type == XML_DOCUMENT_NODE) ||
773 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +0000774 ((reader->ctxt->node == NULL) ||
775 (reader->ctxt->node == reader->node) ||
776 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +0000777 (reader->ctxt->instate != XML_PARSER_EOF)) {
778 val = xmlTextReaderPushData(reader);
779 if (val < 0)
780 return(-1);
781 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000782 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000783 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000784 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000785 if ((reader->node->children != NULL) &&
786 (reader->node->type != XML_ENTITY_REF_NODE) &&
787 (reader->node->type != XML_DTD_NODE)) {
788 reader->node = reader->node->children;
789 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +0000790 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000791 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000792 }
793 }
794 if (reader->node->next != NULL) {
795 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +0000796 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +0000797 (reader->node->children == NULL) &&
798 (reader->node->_private != (void *)xmlTextReaderIsEmpty)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000799 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000800 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000801 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000802 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000803 (reader->node->type == XML_ELEMENT_NODE))
804 xmlTextReaderValidatePop(reader);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000805 reader->node = reader->node->next;
806 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000807
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000808 /*
809 * Cleanup of the old node
810 */
Daniel Veillard4dbe77a2003-01-14 00:17:42 +0000811 if ((reader->node->prev != NULL) &&
812 (reader->node->prev->type != XML_DTD_NODE)) {
813 xmlNodePtr tmp = reader->node->prev;
814 xmlUnlinkNode(tmp);
815 xmlFreeNode(tmp);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000816 }
817
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000818 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000819 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000820 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +0000821 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +0000822 (reader->node->children == NULL) &&
823 (reader->node->_private != (void *)xmlTextReaderIsEmpty)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000824 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000825 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000826 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000827 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000828 xmlTextReaderValidatePop(reader);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000829 reader->node = reader->node->parent;
830 if ((reader->node == NULL) ||
831 (reader->node->type == XML_DOCUMENT_NODE) ||
832#ifdef LIBXML_DOCB_ENABLED
833 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
834#endif
835 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000836 if (reader->mode != XML_TEXTREADER_DONE) {
837 val = xmlParseChunk(reader->ctxt, "", 0, 1);
838 reader->mode = XML_TEXTREADER_DONE;
839 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000840 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +0000841 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000842
843 /*
844 * Cleanup of the old node
845 */
846 if (oldnode->type != XML_DTD_NODE) {
847 xmlUnlinkNode(oldnode);
848 xmlFreeNode(oldnode);
849 }
850
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000851 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000852 }
853 reader->depth--;
854 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000855
856node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000857 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000858
859 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000860 * If we are in the middle of a piece of CDATA make sure it's finished
861 */
862 if ((reader->node != NULL) &&
863 ((reader->node->type == XML_TEXT_NODE) ||
864 (reader->node->type == XML_CDATA_SECTION_NODE))) {
865 xmlTextReaderExpand(reader);
866 }
867
868 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000869 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000870 */
871 if ((reader->node != NULL) &&
872 (reader->node->type == XML_ENTITY_REF_NODE) &&
873 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
874 /*
875 * Case where the underlying tree is not availble, lookup the entity
876 * and walk it.
877 */
878 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
879 (reader->ctxt->sax->getEntity != NULL)) {
880 reader->node->children = (xmlNodePtr)
881 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
882 }
883
884 if ((reader->node->children != NULL) &&
885 (reader->node->children->type == XML_ENTITY_DECL) &&
886 (reader->node->children->children != NULL)) {
887 xmlTextReaderEntPush(reader, reader->node);
888 reader->node = reader->node->children->children;
889 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000890 } else if ((reader->node != NULL) &&
891 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +0000892 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000893 xmlTextReaderValidateEntity(reader);
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000894 }
895 if ((reader->node != NULL) &&
896 (reader->node->type == XML_ENTITY_DECL) &&
897 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
898 reader->node = xmlTextReaderEntPop(reader);
899 reader->depth++;
900 goto get_next_node;
901 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000902#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000903 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000904 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000905
906 if ((node->type == XML_ELEMENT_NODE) &&
907 ((reader->state != XML_TEXTREADER_END) &&
908 (reader->state != XML_TEXTREADER_BACKTRACK))) {
909 xmlTextReaderValidatePush(reader);
910 } else if ((node->type == XML_TEXT_NODE) ||
911 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000912 xmlTextReaderValidateCData(reader, node->content,
913 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000914 }
915 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000916#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000917 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000918node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000919 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000920 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000921}
922
Daniel Veillard67df8092002-12-16 22:04:11 +0000923/**
924 * xmlTextReaderReadState:
925 * @reader: the xmlTextReaderPtr used
926 *
927 * Gets the read state of the reader.
928 *
929 * Returns the state value, or -1 in case of error
930 */
931int
932xmlTextReaderReadState(xmlTextReaderPtr reader) {
933 if (reader == NULL)
934 return(-1);
935 return(reader->mode);
936}
937
938/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000939 * xmlTextReaderExpand:
940 * @reader: the xmlTextReaderPtr used
941 *
942 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +0000943 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000944 *
945 * Returns a node pointer valid until the next xmlTextReaderRead() call
946 * or NULL in case of error.
947 */
948xmlNodePtr
949xmlTextReaderExpand(xmlTextReaderPtr reader) {
950 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
951 return(NULL);
952 if (xmlTextReaderDoExpand(reader) < 0)
953 return(NULL);
954 return(reader->node);
955}
956
957/**
958 * xmlTextReaderNext:
959 * @reader: the xmlTextReaderPtr used
960 *
961 * Skip to the node following the current one in document order while
962 * avoiding the subtree if any.
963 *
964 * Returns 1 if the node was read successfully, 0 if there is no more
965 * nodes to read, or -1 in case of error
966 */
967int
968xmlTextReaderNext(xmlTextReaderPtr reader) {
969 int ret;
970 xmlNodePtr cur;
971
972 if (reader == NULL)
973 return(-1);
974 cur = reader->node;
975 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
976 return(xmlTextReaderRead(reader));
977 if (reader->state == XML_TEXTREADER_END)
978 return(xmlTextReaderRead(reader));
979 if (cur->_private == (void *)xmlTextReaderIsEmpty)
980 return(xmlTextReaderRead(reader));
981 do {
982 ret = xmlTextReaderRead(reader);
983 if (ret != 1)
984 return(ret);
985 } while (reader->node != cur);
986 return(xmlTextReaderRead(reader));
987}
988
989/**
Daniel Veillard67df8092002-12-16 22:04:11 +0000990 * xmlTextReaderReadInnerXml:
991 * @reader: the xmlTextReaderPtr used
992 *
993 * Reads the contents of the current node, including child nodes and markup.
994 *
995 * Returns a string containing the XML content, or NULL if the current node
996 * is neither an element nor attribute, or has no child nodes. The
997 * string must be deallocated by the caller.
998 */
999xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001000xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001001 TODO
1002 return(NULL);
1003}
1004
1005/**
1006 * xmlTextReaderReadOuterXml:
1007 * @reader: the xmlTextReaderPtr used
1008 *
1009 * Reads the contents of the current node, including child nodes and markup.
1010 *
1011 * Returns a string containing the XML content, or NULL if the current node
1012 * is neither an element nor attribute, or has no child nodes. The
1013 * string must be deallocated by the caller.
1014 */
1015xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001016xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001017 TODO
1018 return(NULL);
1019}
1020
1021/**
1022 * xmlTextReaderReadString:
1023 * @reader: the xmlTextReaderPtr used
1024 *
1025 * Reads the contents of an element or a text node as a string.
1026 *
1027 * Returns a string containing the contents of the Element or Text node,
1028 * or NULL if the reader is positioned on any other type of node.
1029 * The string must be deallocated by the caller.
1030 */
1031xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001032xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001033 TODO
1034 return(NULL);
1035}
1036
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001037/**
1038 * xmlTextReaderReadBase64:
1039 * @reader: the xmlTextReaderPtr used
1040 * @array: a byte array to store the content.
1041 * @offset: the zero-based index into array where the method should
1042 * begin to write.
1043 * @len: the number of bytes to write.
1044 *
1045 * Reads and decodes the Base64 encoded contents of an element and
1046 * stores the result in a byte buffer.
1047 *
1048 * Returns the number of bytes written to array, or zero if the current
1049 * instance is not positioned on an element or -1 in case of error.
1050 */
1051int
1052xmlTextReaderReadBase64(xmlTextReaderPtr reader, unsigned char *array,
1053 int offset, int len) {
1054 if ((reader == NULL) || (reader->ctxt == NULL))
1055 return(-1);
1056 if (reader->ctxt->wellFormed != 1)
1057 return(-1);
1058
1059 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1060 return(0);
1061 TODO
1062 return(0);
1063}
1064
1065/**
1066 * xmlTextReaderReadBinHex:
1067 * @reader: the xmlTextReaderPtr used
1068 * @array: a byte array to store the content.
1069 * @offset: the zero-based index into array where the method should
1070 * begin to write.
1071 * @len: the number of bytes to write.
1072 *
1073 * Reads and decodes the BinHex encoded contents of an element and
1074 * stores the result in a byte buffer.
1075 *
1076 * Returns the number of bytes written to array, or zero if the current
1077 * instance is not positioned on an element or -1 in case of error.
1078 */
1079int
1080xmlTextReaderReadBinHex(xmlTextReaderPtr reader, unsigned char *array,
1081 int offset, int len) {
1082 if ((reader == NULL) || (reader->ctxt == NULL))
1083 return(-1);
1084 if (reader->ctxt->wellFormed != 1)
1085 return(-1);
1086
1087 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1088 return(0);
1089 TODO
1090 return(0);
1091}
1092
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001093/************************************************************************
1094 * *
1095 * Constructor and destructors *
1096 * *
1097 ************************************************************************/
1098/**
1099 * xmlNewTextReader:
1100 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001101 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001102 *
1103 * Create an xmlTextReader structure fed with @input
1104 *
1105 * Returns the new xmlTextReaderPtr or NULL in case of error
1106 */
1107xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001108xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001109 xmlTextReaderPtr ret;
1110 int val;
1111
1112 if (input == NULL)
1113 return(NULL);
1114 ret = xmlMalloc(sizeof(xmlTextReader));
1115 if (ret == NULL) {
1116 xmlGenericError(xmlGenericErrorContext,
1117 "xmlNewTextReader : malloc failed\n");
1118 return(NULL);
1119 }
1120 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001121 ret->entTab = NULL;
1122 ret->entMax = 0;
1123 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001124 ret->input = input;
1125 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1126 if (ret->sax == NULL) {
1127 xmlFree(ret);
1128 xmlGenericError(xmlGenericErrorContext,
1129 "xmlNewTextReader : malloc failed\n");
1130 return(NULL);
1131 }
1132 memcpy(ret->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler));
1133 ret->startElement = ret->sax->startElement;
1134 ret->sax->startElement = xmlTextReaderStartElement;
1135 ret->endElement = ret->sax->endElement;
1136 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001137 ret->characters = ret->sax->characters;
1138 ret->sax->characters = xmlTextReaderCharacters;
1139 ret->cdataBlock = ret->sax->cdataBlock;
1140 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001141
Daniel Veillard67df8092002-12-16 22:04:11 +00001142 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001143 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001144 ret->curnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001145 val = xmlParserInputBufferRead(input, 4);
1146 if (val >= 4) {
1147 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001148 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001149 ret->base = 0;
1150 ret->cur = 4;
1151 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001152 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001153 ret->base = 0;
1154 ret->cur = 0;
1155 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001156 if (ret->ctxt == NULL) {
1157 xmlGenericError(xmlGenericErrorContext,
1158 "xmlNewTextReader : malloc failed\n");
1159 xmlFree(ret->sax);
1160 xmlFree(ret);
1161 return(NULL);
1162 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001163 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001164 ret->ctxt->linenumbers = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001165 ret->allocs = XML_TEXTREADER_CTXT;
1166 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001167}
1168
1169/**
1170 * xmlNewTextReaderFilename:
1171 * @URI: the URI of the resource to process
1172 *
1173 * Create an xmlTextReader structure fed with the resource at @URI
1174 *
1175 * Returns the new xmlTextReaderPtr or NULL in case of error
1176 */
1177xmlTextReaderPtr
1178xmlNewTextReaderFilename(const char *URI) {
1179 xmlParserInputBufferPtr input;
1180 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001181 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001182
1183 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1184 if (input == NULL)
1185 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001186 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001187 if (ret == NULL) {
1188 xmlFreeParserInputBuffer(input);
1189 return(NULL);
1190 }
1191 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001192 if (ret->ctxt->directory == NULL)
1193 directory = xmlParserGetDirectory(URI);
1194 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1195 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1196 if (directory != NULL)
1197 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001198 return(ret);
1199}
1200
1201/**
1202 * xmlFreeTextReader:
1203 * @reader: the xmlTextReaderPtr
1204 *
1205 * Deallocate all the resources associated to the reader
1206 */
1207void
1208xmlFreeTextReader(xmlTextReaderPtr reader) {
1209 if (reader == NULL)
1210 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001211#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001212 if (reader->rngSchemas != NULL) {
1213 xmlRelaxNGFree(reader->rngSchemas);
1214 reader->rngSchemas = NULL;
1215 }
1216 if (reader->rngValidCtxt != NULL) {
1217 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
1218 reader->rngValidCtxt = NULL;
1219 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001220#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001221 if (reader->ctxt != NULL) {
1222 if (reader->ctxt->myDoc != NULL) {
1223 xmlFreeDoc(reader->ctxt->myDoc);
1224 reader->ctxt->myDoc = NULL;
1225 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00001226 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
1227 (reader->ctxt->vctxt.vstateMax > 0)){
1228 xmlFree(reader->ctxt->vctxt.vstateTab);
1229 reader->ctxt->vctxt.vstateTab = 0;
1230 reader->ctxt->vctxt.vstateMax = 0;
1231 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001232 if (reader->allocs & XML_TEXTREADER_CTXT)
1233 xmlFreeParserCtxt(reader->ctxt);
1234 }
1235 if (reader->sax != NULL)
1236 xmlFree(reader->sax);
1237 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
1238 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001239 if (reader->faketext != NULL) {
1240 xmlFreeNode(reader->faketext);
1241 }
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001242 if (reader->entTab != NULL)
1243 xmlFree(reader->entTab);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001244 xmlFree(reader);
1245}
1246
1247/************************************************************************
1248 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001249 * Methods for XmlTextReader *
1250 * *
1251 ************************************************************************/
1252/**
1253 * xmlTextReaderClose:
1254 * @reader: the xmlTextReaderPtr used
1255 *
1256 * This method releases any resources allocated by the current instance
1257 * changes the state to Closed and close any underlying input.
1258 *
1259 * Returns 0 or -1 in case of error
1260 */
1261int
1262xmlTextReaderClose(xmlTextReaderPtr reader) {
1263 if (reader == NULL)
1264 return(-1);
1265 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001266 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001267 reader->mode = XML_TEXTREADER_MODE_CLOSED;
1268 if (reader->ctxt != NULL) {
1269 if (reader->ctxt->myDoc != NULL) {
1270 xmlFreeDoc(reader->ctxt->myDoc);
1271 reader->ctxt->myDoc = NULL;
1272 }
1273 if (reader->allocs & XML_TEXTREADER_CTXT) {
1274 xmlFreeParserCtxt(reader->ctxt);
1275 reader->allocs -= XML_TEXTREADER_CTXT;
1276 }
1277 }
1278 if (reader->sax != NULL) {
1279 xmlFree(reader->sax);
1280 reader->sax = NULL;
1281 }
1282 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
1283 xmlFreeParserInputBuffer(reader->input);
1284 reader->allocs -= XML_TEXTREADER_INPUT;
1285 }
1286 return(0);
1287}
1288
1289/**
1290 * xmlTextReaderGetAttributeNo:
1291 * @reader: the xmlTextReaderPtr used
1292 * @no: the zero-based index of the attribute relative to the containing element
1293 *
1294 * Provides the value of the attribute with the specified index relative
1295 * to the containing element.
1296 *
1297 * Returns a string containing the value of the specified attribute, or NULL
1298 * in case of error. The string must be deallocated by the caller.
1299 */
1300xmlChar *
1301xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
1302 xmlChar *ret;
1303 int i;
1304 xmlAttrPtr cur;
1305 xmlNsPtr ns;
1306
1307 if (reader == NULL)
1308 return(NULL);
1309 if (reader->node == NULL)
1310 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001311 if (reader->curnode != NULL)
1312 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001313 /* TODO: handle the xmlDecl */
1314 if (reader->node->type != XML_ELEMENT_NODE)
1315 return(NULL);
1316
1317 ns = reader->node->nsDef;
1318 for (i = 0;(i < no) && (ns != NULL);i++) {
1319 ns = ns->next;
1320 }
1321 if (ns != NULL)
1322 return(xmlStrdup(ns->href));
1323
1324 cur = reader->node->properties;
1325 if (cur == NULL)
1326 return(NULL);
1327 for (;i < no;i++) {
1328 cur = cur->next;
1329 if (cur == NULL)
1330 return(NULL);
1331 }
1332 /* TODO walk the DTD if present */
1333
1334 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
1335 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
1336 return(ret);
1337}
1338
1339/**
1340 * xmlTextReaderGetAttribute:
1341 * @reader: the xmlTextReaderPtr used
1342 * @name: the qualified name of the attribute.
1343 *
1344 * Provides the value of the attribute with the specified qualified name.
1345 *
1346 * Returns a string containing the value of the specified attribute, or NULL
1347 * in case of error. The string must be deallocated by the caller.
1348 */
1349xmlChar *
1350xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1351 xmlChar *prefix = NULL;
1352 xmlChar *localname;
1353 xmlNsPtr ns;
1354 xmlChar *ret = NULL;
1355
1356 if ((reader == NULL) || (name == NULL))
1357 return(NULL);
1358 if (reader->node == NULL)
1359 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001360 if (reader->curnode != NULL)
1361 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001362
1363 /* TODO: handle the xmlDecl */
1364 if (reader->node->type != XML_ELEMENT_NODE)
1365 return(NULL);
1366
1367 localname = xmlSplitQName2(name, &prefix);
1368 if (localname == NULL)
1369 return(xmlGetProp(reader->node, name));
1370
1371 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1372 if (ns != NULL)
1373 ret = xmlGetNsProp(reader->node, localname, ns->href);
1374
1375 if (localname != NULL)
1376 xmlFree(localname);
1377 if (prefix != NULL)
1378 xmlFree(prefix);
1379 return(ret);
1380}
1381
1382
1383/**
1384 * xmlTextReaderGetAttributeNs:
1385 * @reader: the xmlTextReaderPtr used
1386 * @localName: the local name of the attribute.
1387 * @namespaceURI: the namespace URI of the attribute.
1388 *
1389 * Provides the value of the specified attribute
1390 *
1391 * Returns a string containing the value of the specified attribute, or NULL
1392 * in case of error. The string must be deallocated by the caller.
1393 */
1394xmlChar *
1395xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
1396 const xmlChar *namespaceURI) {
1397 if ((reader == NULL) || (localName == NULL))
1398 return(NULL);
1399 if (reader->node == NULL)
1400 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001401 if (reader->curnode != NULL)
1402 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001403
1404 /* TODO: handle the xmlDecl */
1405 if (reader->node->type != XML_ELEMENT_NODE)
1406 return(NULL);
1407
1408 return(xmlGetNsProp(reader->node, localName, namespaceURI));
1409}
1410
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001411/**
1412 * xmlTextReaderGetRemainder:
1413 * @reader: the xmlTextReaderPtr used
1414 *
1415 * Method to get the remainder of the buffered XML. this method stops the
1416 * parser, set its state to End Of File and return the input stream with
1417 * what is left that the parser did not use.
1418 *
1419 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
1420 * in case of error.
1421 */
1422xmlParserInputBufferPtr
1423xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
1424 xmlParserInputBufferPtr ret = NULL;
1425
1426 if (reader == NULL)
1427 return(NULL);
1428 if (reader->node == NULL)
1429 return(NULL);
1430
1431 reader->node = NULL;
1432 reader->curnode = NULL;
1433 reader->mode = XML_TEXTREADER_MODE_EOF;
1434 if (reader->ctxt != NULL) {
1435 if (reader->ctxt->myDoc != NULL) {
1436 xmlFreeDoc(reader->ctxt->myDoc);
1437 reader->ctxt->myDoc = NULL;
1438 }
1439 if (reader->allocs & XML_TEXTREADER_CTXT) {
1440 xmlFreeParserCtxt(reader->ctxt);
1441 reader->allocs -= XML_TEXTREADER_CTXT;
1442 }
1443 }
1444 if (reader->sax != NULL) {
1445 xmlFree(reader->sax);
1446 reader->sax = NULL;
1447 }
1448 if (reader->allocs & XML_TEXTREADER_INPUT) {
1449 ret = reader->input;
1450 reader->allocs -= XML_TEXTREADER_INPUT;
1451 } else {
1452 /*
1453 * Hum, one may need to duplicate the data structure because
1454 * without reference counting the input may be freed twice:
1455 * - by the layer which allocated it.
1456 * - by the layer to which would have been returned to.
1457 */
1458 TODO
1459 return(NULL);
1460 }
1461 return(ret);
1462}
1463
1464/**
1465 * xmlTextReaderLookupNamespace:
1466 * @reader: the xmlTextReaderPtr used
1467 * @prefix: the prefix whose namespace URI is to be resolved. To return
1468 * the default namespace, specify NULL
1469 *
1470 * Resolves a namespace prefix in the scope of the current element.
1471 *
1472 * Returns a string containing the namespace URI to which the prefix maps
1473 * or NULL in case of error. The string must be deallocated by the caller.
1474 */
1475xmlChar *
1476xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
1477 xmlNsPtr ns;
1478
1479 if (reader == NULL)
1480 return(NULL);
1481 if (reader->node == NULL)
1482 return(NULL);
1483
1484 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1485 if (ns == NULL)
1486 return(NULL);
1487 return(xmlStrdup(ns->href));
1488}
1489
1490/**
1491 * xmlTextReaderMoveToAttributeNo:
1492 * @reader: the xmlTextReaderPtr used
1493 * @no: the zero-based index of the attribute relative to the containing
1494 * element.
1495 *
1496 * Moves the position of the current instance to the attribute with
1497 * the specified index relative to the containing element.
1498 *
1499 * Returns 1 in case of success, -1 in case of error, 0 if not found
1500 */
1501int
1502xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
1503 int i;
1504 xmlAttrPtr cur;
1505 xmlNsPtr ns;
1506
1507 if (reader == NULL)
1508 return(-1);
1509 if (reader->node == NULL)
1510 return(-1);
1511 /* TODO: handle the xmlDecl */
1512 if (reader->node->type != XML_ELEMENT_NODE)
1513 return(-1);
1514
1515 reader->curnode = NULL;
1516
1517 ns = reader->node->nsDef;
1518 for (i = 0;(i < no) && (ns != NULL);i++) {
1519 ns = ns->next;
1520 }
1521 if (ns != NULL) {
1522 reader->curnode = (xmlNodePtr) ns;
1523 return(1);
1524 }
1525
1526 cur = reader->node->properties;
1527 if (cur == NULL)
1528 return(0);
1529 for (;i < no;i++) {
1530 cur = cur->next;
1531 if (cur == NULL)
1532 return(0);
1533 }
1534 /* TODO walk the DTD if present */
1535
1536 reader->curnode = (xmlNodePtr) cur;
1537 return(1);
1538}
1539
1540/**
1541 * xmlTextReaderMoveToAttribute:
1542 * @reader: the xmlTextReaderPtr used
1543 * @name: the qualified name of the attribute.
1544 *
1545 * Moves the position of the current instance to the attribute with
1546 * the specified qualified name.
1547 *
1548 * Returns 1 in case of success, -1 in case of error, 0 if not found
1549 */
1550int
1551xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1552 xmlChar *prefix = NULL;
1553 xmlChar *localname;
1554 xmlNsPtr ns;
1555 xmlAttrPtr prop;
1556
1557 if ((reader == NULL) || (name == NULL))
1558 return(-1);
1559 if (reader->node == NULL)
1560 return(-1);
1561
1562 /* TODO: handle the xmlDecl */
1563 if (reader->node->type != XML_ELEMENT_NODE)
1564 return(0);
1565
1566 localname = xmlSplitQName2(name, &prefix);
1567 if (localname == NULL) {
1568 /*
1569 * Namespace default decl
1570 */
1571 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
1572 ns = reader->node->nsDef;
1573 while (ns != NULL) {
1574 if (ns->prefix == NULL) {
1575 reader->curnode = (xmlNodePtr) ns;
1576 return(1);
1577 }
1578 ns = ns->next;
1579 }
1580 return(0);
1581 }
1582
1583 prop = reader->node->properties;
1584 while (prop != NULL) {
1585 /*
1586 * One need to have
1587 * - same attribute names
1588 * - and the attribute carrying that namespace
1589 */
1590 if ((xmlStrEqual(prop->name, name)) &&
1591 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
1592 reader->curnode = (xmlNodePtr) prop;
1593 return(1);
1594 }
1595 prop = prop->next;
1596 }
1597 return(0);
1598 }
1599
1600 /*
1601 * Namespace default decl
1602 */
1603 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
1604 ns = reader->node->nsDef;
1605 while (ns != NULL) {
1606 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
1607 reader->curnode = (xmlNodePtr) ns;
1608 goto found;
1609 }
1610 ns = ns->next;
1611 }
1612 goto not_found;
1613 }
1614 prop = reader->node->properties;
1615 while (prop != NULL) {
1616 /*
1617 * One need to have
1618 * - same attribute names
1619 * - and the attribute carrying that namespace
1620 */
1621 if ((xmlStrEqual(prop->name, localname)) &&
1622 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
1623 reader->curnode = (xmlNodePtr) prop;
1624 goto found;
1625 }
1626 prop = prop->next;
1627 }
1628not_found:
1629 if (localname != NULL)
1630 xmlFree(localname);
1631 if (prefix != NULL)
1632 xmlFree(prefix);
1633 return(0);
1634
1635found:
1636 if (localname != NULL)
1637 xmlFree(localname);
1638 if (prefix != NULL)
1639 xmlFree(prefix);
1640 return(1);
1641}
1642
1643/**
1644 * xmlTextReaderMoveToAttributeNs:
1645 * @reader: the xmlTextReaderPtr used
1646 * @localName: the local name of the attribute.
1647 * @namespaceURI: the namespace URI of the attribute.
1648 *
1649 * Moves the position of the current instance to the attribute with the
1650 * specified local name and namespace URI.
1651 *
1652 * Returns 1 in case of success, -1 in case of error, 0 if not found
1653 */
1654int
1655xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
1656 const xmlChar *localName, const xmlChar *namespaceURI) {
1657 xmlAttrPtr prop;
1658 xmlNodePtr node;
1659
1660 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
1661 return(-1);
1662 if (reader->node == NULL)
1663 return(-1);
1664 if (reader->node->type != XML_ELEMENT_NODE)
1665 return(0);
1666 node = reader->node;
1667
1668 /*
1669 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
1670 * namespace name associated to "xmlns"
1671 */
1672 prop = node->properties;
1673 while (prop != NULL) {
1674 /*
1675 * One need to have
1676 * - same attribute names
1677 * - and the attribute carrying that namespace
1678 */
1679 if (xmlStrEqual(prop->name, localName) &&
1680 ((prop->ns != NULL) &&
1681 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
1682 reader->curnode = (xmlNodePtr) prop;
1683 return(1);
1684 }
1685 prop = prop->next;
1686 }
1687 return(0);
1688}
1689
1690/**
1691 * xmlTextReaderMoveToFirstAttribute:
1692 * @reader: the xmlTextReaderPtr used
1693 *
1694 * Moves the position of the current instance to the first attribute
1695 * associated with the current node.
1696 *
1697 * Returns 1 in case of success, -1 in case of error, 0 if not found
1698 */
1699int
1700xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
1701 if (reader == NULL)
1702 return(-1);
1703 if (reader->node == NULL)
1704 return(-1);
1705 if (reader->node->type != XML_ELEMENT_NODE)
1706 return(0);
1707
1708 if (reader->node->nsDef != NULL) {
1709 reader->curnode = (xmlNodePtr) reader->node->nsDef;
1710 return(1);
1711 }
1712 if (reader->node->properties != NULL) {
1713 reader->curnode = (xmlNodePtr) reader->node->properties;
1714 return(1);
1715 }
1716 return(0);
1717}
1718
1719/**
1720 * xmlTextReaderMoveToNextAttribute:
1721 * @reader: the xmlTextReaderPtr used
1722 *
1723 * Moves the position of the current instance to the next attribute
1724 * associated with the current node.
1725 *
1726 * Returns 1 in case of success, -1 in case of error, 0 if not found
1727 */
1728int
1729xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
1730 if (reader == NULL)
1731 return(-1);
1732 if (reader->node == NULL)
1733 return(-1);
1734 if (reader->node->type != XML_ELEMENT_NODE)
1735 return(0);
1736 if (reader->curnode == NULL)
1737 return(xmlTextReaderMoveToFirstAttribute(reader));
1738
1739 if (reader->curnode->type == XML_NAMESPACE_DECL) {
1740 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
1741 if (ns->next != NULL) {
1742 reader->curnode = (xmlNodePtr) ns->next;
1743 return(1);
1744 }
1745 if (reader->node->properties != NULL) {
1746 reader->curnode = (xmlNodePtr) reader->node->properties;
1747 return(1);
1748 }
1749 return(0);
1750 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
1751 (reader->curnode->next != NULL)) {
1752 reader->curnode = reader->curnode->next;
1753 return(1);
1754 }
1755 return(0);
1756}
1757
1758/**
1759 * xmlTextReaderMoveToElement:
1760 * @reader: the xmlTextReaderPtr used
1761 *
1762 * Moves the position of the current instance to the node that
1763 * contains the current Attribute node.
1764 *
1765 * Returns 1 in case of success, -1 in case of error, 0 if not moved
1766 */
1767int
1768xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
1769 if (reader == NULL)
1770 return(-1);
1771 if (reader->node == NULL)
1772 return(-1);
1773 if (reader->node->type != XML_ELEMENT_NODE)
1774 return(0);
1775 if (reader->curnode != NULL) {
1776 reader->curnode = NULL;
1777 return(1);
1778 }
1779 return(0);
1780}
1781
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001782/**
1783 * xmlTextReaderReadAttributeValue:
1784 * @reader: the xmlTextReaderPtr used
1785 *
1786 * Parses an attribute value into one or more Text and EntityReference nodes.
1787 *
1788 * Returns 1 in case of success, 0 if the reader was not positionned on an
1789 * ttribute node or all the attribute values have been read, or -1
1790 * in case of error.
1791 */
1792int
1793xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
1794 if (reader == NULL)
1795 return(-1);
1796 if (reader->node == NULL)
1797 return(-1);
1798 if (reader->curnode == NULL)
1799 return(0);
1800 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
1801 if (reader->curnode->children == NULL)
1802 return(0);
1803 reader->curnode = reader->curnode->children;
1804 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
1805 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
1806
1807 if (reader->faketext == NULL) {
1808 reader->faketext = xmlNewDocText(reader->node->doc,
1809 ns->href);
1810 } else {
1811 if (reader->faketext->content != NULL)
1812 xmlFree(reader->faketext->content);
1813 reader->faketext->content = xmlStrdup(ns->href);
1814 }
1815 reader->curnode = reader->faketext;
1816 } else {
1817 if (reader->curnode->next == NULL)
1818 return(0);
1819 reader->curnode = reader->curnode->next;
1820 }
1821 return(1);
1822}
1823
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001824/************************************************************************
1825 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001826 * Acces API to the current node *
1827 * *
1828 ************************************************************************/
1829/**
1830 * xmlTextReaderAttributeCount:
1831 * @reader: the xmlTextReaderPtr used
1832 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001833 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001834 *
1835 * Returns 0 i no attributes, -1 in case of error or the attribute count
1836 */
1837int
1838xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
1839 int ret;
1840 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00001841 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001842 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001843
1844 if (reader == NULL)
1845 return(-1);
1846 if (reader->node == NULL)
1847 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001848
1849 if (reader->curnode != NULL)
1850 node = reader->curnode;
1851 else
1852 node = reader->node;
1853
1854 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001855 return(0);
1856 if ((reader->state == XML_TEXTREADER_END) ||
1857 (reader->state == XML_TEXTREADER_BACKTRACK))
1858 return(0);
1859 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001860 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001861 while (attr != NULL) {
1862 ret++;
1863 attr = attr->next;
1864 }
Daniel Veillard67df8092002-12-16 22:04:11 +00001865 ns = node->nsDef;
1866 while (ns != NULL) {
1867 ret++;
1868 ns = ns->next;
1869 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001870 return(ret);
1871}
1872
1873/**
1874 * xmlTextReaderNodeType:
1875 * @reader: the xmlTextReaderPtr used
1876 *
1877 * Get the node type of the current node
1878 * Reference:
1879 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
1880 *
1881 * Returns the xmlNodeType of the current node or -1 in case of error
1882 */
1883int
1884xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001885 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001886 if (reader == NULL)
1887 return(-1);
1888 if (reader->node == NULL)
1889 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001890 if (reader->curnode != NULL)
1891 node = reader->curnode;
1892 else
1893 node = reader->node;
1894 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001895 case XML_ELEMENT_NODE:
1896 if ((reader->state == XML_TEXTREADER_END) ||
1897 (reader->state == XML_TEXTREADER_BACKTRACK))
1898 return(15);
1899 return(1);
Daniel Veillardecaba492002-12-30 10:55:29 +00001900 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001901 case XML_ATTRIBUTE_NODE:
1902 return(2);
1903 case XML_TEXT_NODE:
1904 return(3); /* TODO: SignificantWhitespace == 14 Whitespace == 13 */
1905 case XML_CDATA_SECTION_NODE:
1906 return(4);
1907 case XML_ENTITY_REF_NODE:
1908 return(5);
1909 case XML_ENTITY_NODE:
1910 return(6);
1911 case XML_PI_NODE:
1912 return(7);
1913 case XML_COMMENT_NODE:
1914 return(8);
1915 case XML_DOCUMENT_NODE:
1916 case XML_HTML_DOCUMENT_NODE:
1917#ifdef LIBXML_DOCB_ENABLED
1918 case XML_DOCB_DOCUMENT_NODE:
1919#endif
1920 return(9);
1921 case XML_DOCUMENT_FRAG_NODE:
1922 return(11);
1923 case XML_NOTATION_NODE:
1924 return(12);
1925 case XML_DOCUMENT_TYPE_NODE:
1926 case XML_DTD_NODE:
1927 return(10);
1928
1929 case XML_ELEMENT_DECL:
1930 case XML_ATTRIBUTE_DECL:
1931 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001932 case XML_XINCLUDE_START:
1933 case XML_XINCLUDE_END:
1934 return(0);
1935 }
1936 return(-1);
1937}
1938
1939/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001940 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001941 * @reader: the xmlTextReaderPtr used
1942 *
1943 * Check if the current node is empty
1944 *
1945 * Returns 1 if empty, 0 if not and -1 in case of error
1946 */
1947int
1948xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
1949 if ((reader == NULL) || (reader->node == NULL))
1950 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00001951 if (reader->node->type != XML_ELEMENT_NODE)
1952 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00001953 if (reader->curnode != NULL)
1954 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001955 if (reader->node->children != NULL)
1956 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001957 if (reader->state == XML_TEXTREADER_END)
1958 return(0);
Daniel Veillard067bae52003-01-05 01:27:54 +00001959 return(reader->node->_private == (void *)xmlTextReaderIsEmpty);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001960}
1961
1962/**
1963 * xmlTextReaderLocalName:
1964 * @reader: the xmlTextReaderPtr used
1965 *
1966 * The local name of the node.
1967 *
1968 * Returns the local name or NULL if not available
1969 */
1970xmlChar *
1971xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001972 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001973 if ((reader == NULL) || (reader->node == NULL))
1974 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001975 if (reader->curnode != NULL)
1976 node = reader->curnode;
1977 else
1978 node = reader->node;
1979 if (node->type == XML_NAMESPACE_DECL) {
1980 xmlNsPtr ns = (xmlNsPtr) node;
1981 if (ns->prefix == NULL)
1982 return(xmlStrdup(BAD_CAST "xmlns"));
1983 else
1984 return(xmlStrdup(ns->prefix));
1985 }
1986 if ((node->type != XML_ELEMENT_NODE) &&
1987 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001988 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001989 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001990}
1991
1992/**
1993 * xmlTextReaderName:
1994 * @reader: the xmlTextReaderPtr used
1995 *
1996 * The qualified name of the node, equal to Prefix :LocalName.
1997 *
1998 * Returns the local name or NULL if not available
1999 */
2000xmlChar *
2001xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002002 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002003 xmlChar *ret;
2004
2005 if ((reader == NULL) || (reader->node == NULL))
2006 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002007 if (reader->curnode != NULL)
2008 node = reader->curnode;
2009 else
2010 node = reader->node;
2011 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002012 case XML_ELEMENT_NODE:
2013 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002014 if ((node->ns == NULL) ||
2015 (node->ns->prefix == NULL))
2016 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002017
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002018 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002019 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002020 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002021 return(ret);
2022 case XML_TEXT_NODE:
2023 return(xmlStrdup(BAD_CAST "#text"));
2024 case XML_CDATA_SECTION_NODE:
2025 return(xmlStrdup(BAD_CAST "#cdata-section"));
2026 case XML_ENTITY_NODE:
2027 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002028 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002029 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002030 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002031 case XML_COMMENT_NODE:
2032 return(xmlStrdup(BAD_CAST "#comment"));
2033 case XML_DOCUMENT_NODE:
2034 case XML_HTML_DOCUMENT_NODE:
2035#ifdef LIBXML_DOCB_ENABLED
2036 case XML_DOCB_DOCUMENT_NODE:
2037#endif
2038 return(xmlStrdup(BAD_CAST "#document"));
2039 case XML_DOCUMENT_FRAG_NODE:
2040 return(xmlStrdup(BAD_CAST "#document-fragment"));
2041 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002042 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002043 case XML_DOCUMENT_TYPE_NODE:
2044 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002045 return(xmlStrdup(node->name));
2046 case XML_NAMESPACE_DECL: {
2047 xmlNsPtr ns = (xmlNsPtr) node;
2048
2049 ret = xmlStrdup(BAD_CAST "xmlns");
2050 if (ns->prefix == NULL)
2051 return(ret);
2052 ret = xmlStrcat(ret, BAD_CAST ":");
2053 ret = xmlStrcat(ret, ns->prefix);
2054 return(ret);
2055 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002056
2057 case XML_ELEMENT_DECL:
2058 case XML_ATTRIBUTE_DECL:
2059 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002060 case XML_XINCLUDE_START:
2061 case XML_XINCLUDE_END:
2062 return(NULL);
2063 }
2064 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002065}
2066
2067/**
2068 * xmlTextReaderPrefix:
2069 * @reader: the xmlTextReaderPtr used
2070 *
2071 * A shorthand reference to the namespace associated with the node.
2072 *
2073 * Returns the prefix or NULL if not available
2074 */
2075xmlChar *
2076xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002077 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002078 if ((reader == NULL) || (reader->node == NULL))
2079 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002080 if (reader->curnode != NULL)
2081 node = reader->curnode;
2082 else
2083 node = reader->node;
2084 if (node->type == XML_NAMESPACE_DECL) {
2085 xmlNsPtr ns = (xmlNsPtr) node;
2086 if (ns->prefix == NULL)
2087 return(NULL);
2088 return(xmlStrdup(BAD_CAST "xmlns"));
2089 }
2090 if ((node->type != XML_ELEMENT_NODE) &&
2091 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002092 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00002093 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002094 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002095 return(NULL);
2096}
2097
2098/**
2099 * xmlTextReaderNamespaceUri:
2100 * @reader: the xmlTextReaderPtr used
2101 *
2102 * The URI defining the namespace associated with the node.
2103 *
2104 * Returns the namespace URI or NULL if not available
2105 */
2106xmlChar *
2107xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002108 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002109 if ((reader == NULL) || (reader->node == NULL))
2110 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002111 if (reader->curnode != NULL)
2112 node = reader->curnode;
2113 else
2114 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00002115 if (node->type == XML_NAMESPACE_DECL)
2116 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002117 if ((node->type != XML_ELEMENT_NODE) &&
2118 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002119 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002120 if (node->ns != NULL)
2121 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002122 return(NULL);
2123}
2124
2125/**
2126 * xmlTextReaderBaseUri:
2127 * @reader: the xmlTextReaderPtr used
2128 *
2129 * The base URI of the node.
2130 *
2131 * Returns the base URI or NULL if not available
2132 */
2133xmlChar *
2134xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
2135 if ((reader == NULL) || (reader->node == NULL))
2136 return(NULL);
2137 return(xmlNodeGetBase(NULL, reader->node));
2138}
2139
2140/**
2141 * xmlTextReaderDepth:
2142 * @reader: the xmlTextReaderPtr used
2143 *
2144 * The depth of the node in the tree.
2145 *
2146 * Returns the depth or -1 in case of error
2147 */
2148int
2149xmlTextReaderDepth(xmlTextReaderPtr reader) {
2150 if (reader == NULL)
2151 return(-1);
2152 if (reader->node == NULL)
2153 return(0);
2154
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002155 if (reader->curnode != NULL) {
2156 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
2157 (reader->curnode->type == XML_NAMESPACE_DECL))
2158 return(reader->depth + 1);
2159 return(reader->depth + 2);
2160 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002161 return(reader->depth);
2162}
2163
2164/**
2165 * xmlTextReaderHasAttributes:
2166 * @reader: the xmlTextReaderPtr used
2167 *
2168 * Whether the node has attributes.
2169 *
2170 * Returns 1 if true, 0 if false, and -1 in case or error
2171 */
2172int
2173xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002174 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002175 if (reader == NULL)
2176 return(-1);
2177 if (reader->node == NULL)
2178 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002179 if (reader->curnode != NULL)
2180 node = reader->curnode;
2181 else
2182 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002183
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002184 if ((node->type == XML_ELEMENT_NODE) &&
2185 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002186 return(1);
2187 /* TODO: handle the xmlDecl */
2188 return(0);
2189}
2190
2191/**
2192 * xmlTextReaderHasValue:
2193 * @reader: the xmlTextReaderPtr used
2194 *
2195 * Whether the node can have a text value.
2196 *
2197 * Returns 1 if true, 0 if false, and -1 in case or error
2198 */
2199int
2200xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002201 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002202 if (reader == NULL)
2203 return(-1);
2204 if (reader->node == NULL)
2205 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002206 if (reader->curnode != NULL)
2207 node = reader->curnode;
2208 else
2209 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002210
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002211 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002212 case XML_ATTRIBUTE_NODE:
2213 case XML_TEXT_NODE:
2214 case XML_CDATA_SECTION_NODE:
2215 case XML_PI_NODE:
2216 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00002217 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002218 return(1);
2219 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002220 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002221 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002222 return(0);
2223}
2224
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002225/**
2226 * xmlTextReaderValue:
2227 * @reader: the xmlTextReaderPtr used
2228 *
2229 * Provides the text value of the node if present
2230 *
2231 * Returns the string or NULL if not available. The retsult must be deallocated
2232 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002233 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002234xmlChar *
2235xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002236 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002237 if (reader == NULL)
2238 return(NULL);
2239 if (reader->node == NULL)
2240 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002241 if (reader->curnode != NULL)
2242 node = reader->curnode;
2243 else
2244 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002245
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002246 switch (node->type) {
2247 case XML_NAMESPACE_DECL:
2248 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002249 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002250 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002251
2252 if (attr->parent != NULL)
2253 return (xmlNodeListGetString
2254 (attr->parent->doc, attr->children, 1));
2255 else
2256 return (xmlNodeListGetString(NULL, attr->children, 1));
2257 break;
2258 }
2259 case XML_TEXT_NODE:
2260 case XML_CDATA_SECTION_NODE:
2261 case XML_PI_NODE:
2262 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002263 if (node->content != NULL)
2264 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002265 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002266 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002267 }
2268 return(NULL);
2269}
2270
2271/**
2272 * xmlTextReaderIsDefault:
2273 * @reader: the xmlTextReaderPtr used
2274 *
2275 * Whether an Attribute node was generated from the default value
2276 * defined in the DTD or schema.
2277 *
2278 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
2279 */
2280int
2281xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
2282 if (reader == NULL)
2283 return(-1);
2284 return(0);
2285}
2286
2287/**
2288 * xmlTextReaderQuoteChar:
2289 * @reader: the xmlTextReaderPtr used
2290 *
2291 * The quotation mark character used to enclose the value of an attribute.
2292 *
2293 * Returns " or ' and -1 in case of error
2294 */
2295int
2296xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
2297 if (reader == NULL)
2298 return(-1);
2299 /* TODO maybe lookup the attribute value for " first */
2300 return((int) '"');
2301}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002302
2303/**
2304 * xmlTextReaderXmlLang:
2305 * @reader: the xmlTextReaderPtr used
2306 *
2307 * The xml:lang scope within which the node resides.
2308 *
2309 * Returns the xml:lang value or NULL if none exists.
2310 */
2311xmlChar *
2312xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
2313 if (reader == NULL)
2314 return(NULL);
2315 if (reader->node == NULL)
2316 return(NULL);
2317 return(xmlNodeGetLang(reader->node));
2318}
2319
Daniel Veillard67df8092002-12-16 22:04:11 +00002320/**
2321 * xmlTextReaderNormalization:
2322 * @reader: the xmlTextReaderPtr used
2323 *
2324 * The value indicating whether to normalize white space and attribute values.
2325 * Since attribute value and end of line normalizations are a MUST in the XML
2326 * specification only the value true is accepted. The broken bahaviour of
2327 * accepting out of range character entities like &#0; is of course not
2328 * supported either.
2329 *
2330 * Returns 1 or -1 in case of error.
2331 */
2332int
2333xmlTextReaderNormalization(xmlTextReaderPtr reader) {
2334 if (reader == NULL)
2335 return(-1);
2336 return(1);
2337}
2338
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002339/************************************************************************
2340 * *
2341 * Extensions to the base APIs *
2342 * *
2343 ************************************************************************/
2344
2345/**
2346 * xmlTextReaderSetParserProp:
2347 * @reader: the xmlTextReaderPtr used
2348 * @prop: the xmlParserProperties to set
2349 * @value: usually 0 or 1 to (de)activate it
2350 *
2351 * Change the parser processing behaviour by changing some of its internal
2352 * properties. Note that some properties can only be changed before any
2353 * read has been done.
2354 *
2355 * Returns 0 if the call was successful, or -1 in case of error
2356 */
2357int
2358xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
2359 xmlParserProperties p = (xmlParserProperties) prop;
2360 xmlParserCtxtPtr ctxt;
2361
2362 if ((reader == NULL) || (reader->ctxt == NULL))
2363 return(-1);
2364 ctxt = reader->ctxt;
2365
2366 switch (p) {
2367 case XML_PARSER_LOADDTD:
2368 if (value != 0) {
2369 if (ctxt->loadsubset == 0) {
2370 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2371 return(-1);
2372 ctxt->loadsubset = XML_DETECT_IDS;
2373 }
2374 } else {
2375 ctxt->loadsubset = 0;
2376 }
2377 return(0);
2378 case XML_PARSER_DEFAULTATTRS:
2379 if (value != 0) {
2380 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
2381 } else {
2382 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
2383 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
2384 }
2385 return(0);
2386 case XML_PARSER_VALIDATE:
2387 if (value != 0) {
2388 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00002389 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002390 } else {
2391 ctxt->validate = 0;
2392 }
2393 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00002394 case XML_PARSER_SUBST_ENTITIES:
2395 if (value != 0) {
2396 ctxt->replaceEntities = 1;
2397 } else {
2398 ctxt->replaceEntities = 0;
2399 }
2400 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002401 }
2402 return(-1);
2403}
2404
2405/**
2406 * xmlTextReaderGetParserProp:
2407 * @reader: the xmlTextReaderPtr used
2408 * @prop: the xmlParserProperties to get
2409 *
2410 * Read the parser internal property.
2411 *
2412 * Returns the value, usually 0 or 1, or -1 in case of error.
2413 */
2414int
2415xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
2416 xmlParserProperties p = (xmlParserProperties) prop;
2417 xmlParserCtxtPtr ctxt;
2418
2419 if ((reader == NULL) || (reader->ctxt == NULL))
2420 return(-1);
2421 ctxt = reader->ctxt;
2422
2423 switch (p) {
2424 case XML_PARSER_LOADDTD:
2425 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
2426 return(1);
2427 return(0);
2428 case XML_PARSER_DEFAULTATTRS:
2429 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
2430 return(1);
2431 return(0);
2432 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00002433 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00002434 case XML_PARSER_SUBST_ENTITIES:
2435 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002436 }
2437 return(-1);
2438}
2439
Daniel Veillarde18fc182002-12-28 22:56:33 +00002440/**
2441 * xmlTextReaderCurrentNode:
2442 * @reader: the xmlTextReaderPtr used
2443 *
2444 * Hacking interface allowing to get the xmlNodePtr correponding to the
2445 * current node being accessed by the xmlTextReader. This is dangerous
2446 * because the underlying node may be destroyed on the next Reads.
2447 *
2448 * Returns the xmlNodePtr or NULL in case of error.
2449 */
2450xmlNodePtr
2451xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
2452 if (reader == NULL)
2453 return(NULL);
2454
2455 if (reader->curnode != NULL)
2456 return(reader->curnode);
2457 return(reader->node);
2458}
2459
2460/**
2461 * xmlTextReaderCurrentDoc:
2462 * @reader: the xmlTextReaderPtr used
2463 *
2464 * Hacking interface allowing to get the xmlDocPtr correponding to the
2465 * current document being accessed by the xmlTextReader. This is dangerous
2466 * because the associated node may be destroyed on the next Reads.
2467 *
2468 * Returns the xmlDocPtr or NULL in case of error.
2469 */
2470xmlDocPtr
2471xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
2472 if ((reader == NULL) || (reader->ctxt == NULL))
2473 return(NULL);
2474
2475 return(reader->ctxt->myDoc);
2476}
2477
Daniel Veillard37fc84d2003-05-09 19:38:15 +00002478#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00002479/**
Daniel Veillard33300b42003-04-17 09:09:19 +00002480 * xmlTextReaderRelaxNGSetSchema:
2481 * @reader: the xmlTextReaderPtr used
2482 * @schema: a precompiled RelaxNG schema
2483 *
2484 * Use RelaxNG to validate the document as it is processed.
2485 * Activation is only possible before the first Read().
2486 * if @schema is NULL, then RelaxNG validation is desactivated.
2487 @ The @schema should not be freed until the reader is deallocated
2488 * or its use has been deactivated.
2489 *
2490 * Returns 0 in case the RelaxNG validation could be (des)activated and
2491 * -1 in case of error.
2492 */
2493int
2494xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
2495 if (schema == NULL) {
2496 if (reader->rngSchemas != NULL) {
2497 xmlRelaxNGFree(reader->rngSchemas);
2498 reader->rngSchemas = NULL;
2499 }
2500 if (reader->rngValidCtxt != NULL) {
2501 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2502 reader->rngValidCtxt = NULL;
2503 }
2504 return(0);
2505 }
2506 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2507 return(-1);
2508 if (reader->rngSchemas != NULL) {
2509 xmlRelaxNGFree(reader->rngSchemas);
2510 reader->rngSchemas = NULL;
2511 }
2512 if (reader->rngValidCtxt != NULL) {
2513 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2514 reader->rngValidCtxt = NULL;
2515 }
2516 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
2517 if (reader->rngValidCtxt == NULL)
2518 return(-1);
2519 if (reader->errorFunc != NULL) {
2520 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
2521 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
2522 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
2523 reader->errorFuncArg);
2524 }
2525 reader->rngValidErrors = 0;
2526 reader->rngFullNode = NULL;
2527 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
2528 return(0);
2529}
2530
2531/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00002532 * xmlTextReaderRelaxNGValidate:
2533 * @reader: the xmlTextReaderPtr used
2534 * @rng: the path to a RelaxNG schema or NULL
2535 *
2536 * Use RelaxNG to validate the document as it is processed.
2537 * Activation is only possible before the first Read().
2538 * if @rng is NULL, then RelaxNG validation is desactivated.
2539 *
2540 * Returns 0 in case the RelaxNG validation could be (des)activated and
2541 * -1 in case of error.
2542 */
2543int
2544xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
2545 xmlRelaxNGParserCtxtPtr ctxt;
2546
2547 if (reader == NULL)
2548 return(-1);
2549
2550 if (rng == NULL) {
2551 if (reader->rngSchemas != NULL) {
2552 xmlRelaxNGFree(reader->rngSchemas);
2553 reader->rngSchemas = NULL;
2554 }
2555 if (reader->rngValidCtxt != NULL) {
2556 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2557 reader->rngValidCtxt = NULL;
2558 }
2559 return(0);
2560 }
2561 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2562 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00002563 if (reader->rngSchemas != NULL) {
2564 xmlRelaxNGFree(reader->rngSchemas);
2565 reader->rngSchemas = NULL;
2566 }
2567 if (reader->rngValidCtxt != NULL) {
2568 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2569 reader->rngValidCtxt = NULL;
2570 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00002571 ctxt = xmlRelaxNGNewParserCtxt(rng);
2572 if (reader->errorFunc != NULL) {
2573 xmlRelaxNGSetParserErrors(ctxt,
2574 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
2575 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
2576 reader->errorFuncArg);
2577 }
2578 reader->rngSchemas = xmlRelaxNGParse(ctxt);
2579 xmlRelaxNGFreeParserCtxt(ctxt);
2580 if (reader->rngSchemas == NULL)
2581 return(-1);
2582 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
2583 if (reader->rngValidCtxt == NULL)
2584 return(-1);
2585 if (reader->errorFunc != NULL) {
2586 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
2587 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
2588 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
2589 reader->errorFuncArg);
2590 }
2591 reader->rngValidErrors = 0;
2592 reader->rngFullNode = NULL;
2593 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
2594 return(0);
2595}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00002596#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00002597
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002598/************************************************************************
2599 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00002600 * Error Handling Extensions *
2601 * *
2602 ************************************************************************/
2603
2604/* helper to build a xmlMalloc'ed string from a format and va_list */
2605static char *
2606xmlTextReaderBuildMessage(const char *msg, va_list ap) {
2607 int size;
2608 int chars;
2609 char *larger;
2610 char *str;
2611
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002612 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00002613 if (str == NULL) {
2614 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
2615 return NULL;
2616 }
2617
2618 size = 150;
2619
2620 while (1) {
2621 chars = vsnprintf(str, size, msg, ap);
2622 if ((chars > -1) && (chars < size))
2623 break;
2624 if (chars > -1)
2625 size += chars + 1;
2626 else
2627 size += 100;
2628 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
2629 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
2630 xmlFree(str);
2631 return NULL;
2632 }
2633 str = larger;
2634 }
2635
2636 return str;
2637}
2638
Daniel Veillard417be3a2003-01-20 21:26:34 +00002639/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00002640 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00002641 * @locator: the xmlTextReaderLocatorPtr used
2642 *
2643 * Obtain the line number for the given locator.
2644 *
2645 * Returns the line number or -1 in case of error.
2646 */
2647int
2648xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
2649 /* we know that locator is a xmlParserCtxtPtr */
2650 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
2651 int ret = -1;
2652
2653 if (ctx->node != NULL) {
2654 ret = xmlGetLineNo(ctx->node);
2655 }
2656 else {
2657 /* inspired from error.c */
2658 xmlParserInputPtr input;
2659 input = ctx->input;
2660 if ((input->filename == NULL) && (ctx->inputNr > 1))
2661 input = ctx->inputTab[ctx->inputNr - 2];
2662 if (input != NULL) {
2663 ret = input->line;
2664 }
2665 else {
2666 ret = -1;
2667 }
2668 }
2669
2670 return ret;
2671}
2672
2673/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00002674 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00002675 * @locator: the xmlTextReaderLocatorPtr used
2676 *
2677 * Obtain the base URI for the given locator.
2678 *
2679 * Returns the base URI or NULL in case of error.
2680 */
2681xmlChar *
2682xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
2683 /* we know that locator is a xmlParserCtxtPtr */
2684 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
2685 xmlChar *ret = NULL;
2686
2687 if (ctx->node != NULL) {
2688 ret = xmlNodeGetBase(NULL,ctx->node);
2689 }
2690 else {
2691 /* inspired from error.c */
2692 xmlParserInputPtr input;
2693 input = ctx->input;
2694 if ((input->filename == NULL) && (ctx->inputNr > 1))
2695 input = ctx->inputTab[ctx->inputNr - 2];
2696 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00002697 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00002698 }
2699 else {
2700 ret = NULL;
2701 }
2702 }
2703
2704 return ret;
2705}
2706
Daniel Veillard26f70262003-01-16 22:45:08 +00002707static void
2708xmlTextReaderGenericError(void *ctxt, int severity, char *str) {
2709 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
2710 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
2711
2712 if (str != NULL) {
2713 reader->errorFunc(reader->errorFuncArg,
2714 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002715 severity,
2716 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00002717 xmlFree(str);
2718 }
2719}
2720
2721static void
2722xmlTextReaderError(void *ctxt, const char *msg, ...) {
2723 va_list ap;
2724
2725 va_start(ap,msg);
2726 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002727 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00002728 xmlTextReaderBuildMessage(msg,ap));
2729 va_end(ap);
2730
2731}
2732
2733static void
2734xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
2735 va_list ap;
2736
2737 va_start(ap,msg);
2738 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002739 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00002740 xmlTextReaderBuildMessage(msg,ap));
2741 va_end(ap);
2742}
2743
2744static void
2745xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
2746 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00002747 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00002748
Daniel Veillard417be3a2003-01-20 21:26:34 +00002749 if ((len > 1) && (msg[len - 2] != ':')) {
2750 /*
2751 * some callbacks only report locator information:
2752 * skip them (mimicking behaviour in error.c)
2753 */
2754 va_start(ap,msg);
2755 xmlTextReaderGenericError(ctxt,
2756 XML_PARSER_SEVERITY_VALIDITY_ERROR,
2757 xmlTextReaderBuildMessage(msg,ap));
2758 va_end(ap);
2759 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002760}
2761
2762static void
2763xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
2764 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00002765 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00002766
Daniel Veillard417be3a2003-01-20 21:26:34 +00002767 if ((len != 0) && (msg[len - 1] != ':')) {
2768 /*
2769 * some callbacks only report locator information:
2770 * skip them (mimicking behaviour in error.c)
2771 */
2772 va_start(ap,msg);
2773 xmlTextReaderGenericError(ctxt,
2774 XML_PARSER_SEVERITY_VALIDITY_WARNING,
2775 xmlTextReaderBuildMessage(msg,ap));
2776 va_end(ap);
2777 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002778}
2779
2780/**
2781 * xmlTextReaderSetErrorHandler:
2782 * @reader: the xmlTextReaderPtr used
2783 * @f: the callback function to call on error and warnings
2784 * @arg: a user argument to pass to the callback function
2785 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00002786 * Register a callback function that will be called on error and warnings.
2787 *
Daniel Veillard26f70262003-01-16 22:45:08 +00002788 * If @f is NULL, the default error and warning handlers are restored.
2789 */
2790void
2791xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
2792 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002793 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002794 if (f != NULL) {
2795 reader->ctxt->sax->error = xmlTextReaderError;
2796 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
2797 reader->ctxt->sax->warning = xmlTextReaderWarning;
2798 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
2799 reader->errorFunc = f;
2800 reader->errorFuncArg = arg;
2801 }
2802 else {
2803 /* restore defaults */
2804 reader->ctxt->sax->error = xmlParserError;
2805 reader->ctxt->vctxt.error = xmlParserValidityError;
2806 reader->ctxt->sax->warning = xmlParserWarning;
2807 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
2808 reader->errorFunc = NULL;
2809 reader->errorFuncArg = NULL;
2810 }
2811}
2812
Daniel Veillard417be3a2003-01-20 21:26:34 +00002813/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00002814 * xmlTextReaderIsValid:
2815 * @reader: the xmlTextReaderPtr used
2816 *
2817 * Retrieve the validity status from the parser context
2818 *
2819 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
2820 */
2821int
2822xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00002823 if (reader == NULL) return(-1);
2824#ifdef LIBXML_SCHEMAS_ENABLED
2825 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
2826 return(reader->rngValidErrors == 0);
2827#endif
2828 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
2829 (reader->ctxt != NULL))
2830 return(reader->ctxt->valid);
2831 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00002832}
2833
2834/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00002835 * xmlTextReaderGetErrorHandler:
2836 * @reader: the xmlTextReaderPtr used
2837 * @f: the callback function or NULL is no callback has been registered
2838 * @arg: a user argument
2839 *
2840 * Retrieve the error callback function and user argument.
2841 */
Daniel Veillard26f70262003-01-16 22:45:08 +00002842void
2843xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
2844 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002845 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002846 *f = reader->errorFunc;
2847 *arg = reader->errorFuncArg;
2848}
2849
2850/************************************************************************
2851 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002852 * Utilities *
2853 * *
2854 ************************************************************************/
2855/**
2856 * xmlBase64Decode:
2857 * @in: the input buffer
2858 * @inlen: the size of the input (in), the size read from it (out)
2859 * @to: the output buffer
2860 * @tolen: the size of the output (in), the size written to (out)
2861 *
2862 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00002863 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002864 *
2865 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
2866 * 2 if there wasn't enough space on the output or -1 in case of error.
2867 */
2868static int
2869xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
2870 unsigned char *to, unsigned long *tolen) {
2871 unsigned long incur; /* current index in in[] */
2872 unsigned long inblk; /* last block index in in[] */
2873 unsigned long outcur; /* current index in out[] */
2874 unsigned long inmax; /* size of in[] */
2875 unsigned long outmax; /* size of out[] */
2876 unsigned char cur; /* the current value read from in[] */
2877 unsigned char intmp[3], outtmp[4]; /* temporary buffers for the convert */
2878 int nbintmp; /* number of byte in intmp[] */
2879 int is_ignore; /* cur should be ignored */
2880 int is_end = 0; /* the end of the base64 was found */
2881 int retval = 1;
2882 int i;
2883
2884 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
2885 return(-1);
2886
2887 incur = 0;
2888 inblk = 0;
2889 outcur = 0;
2890 inmax = *inlen;
2891 outmax = *tolen;
2892 nbintmp = 0;
2893
2894 while (1) {
2895 if (incur >= inmax)
2896 break;
2897 cur = in[incur++];
2898 is_ignore = 0;
2899 if ((cur >= 'A') && (cur <= 'Z'))
2900 cur = cur - 'A';
2901 else if ((cur >= 'a') && (cur <= 'z'))
2902 cur = cur - 'a' + 26;
2903 else if ((cur >= '0') && (cur <= '9'))
2904 cur = cur - '0' + 52;
2905 else if (cur == '+')
2906 cur = 62;
2907 else if (cur == '/')
2908 cur = 63;
2909 else if (cur == '.')
2910 cur = 0;
2911 else if (cur == '=') /*no op , end of the base64 stream */
2912 is_end = 1;
2913 else {
2914 is_ignore = 1;
2915 if (nbintmp == 0)
2916 inblk = incur;
2917 }
2918
2919 if (!is_ignore) {
2920 int nbouttmp = 3;
2921 int is_break = 0;
2922
2923 if (is_end) {
2924 if (nbintmp == 0)
2925 break;
2926 if ((nbintmp == 1) || (nbintmp == 2))
2927 nbouttmp = 1;
2928 else
2929 nbouttmp = 2;
2930 nbintmp = 3;
2931 is_break = 1;
2932 }
2933 intmp[nbintmp++] = cur;
2934 /*
2935 * if intmp is full, push the 4byte sequence as a 3 byte
2936 * sequence out
2937 */
2938 if (nbintmp == 4) {
2939 nbintmp = 0;
2940 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
2941 outtmp[1] =
2942 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
2943 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
2944 if (outcur + 3 >= outmax) {
2945 retval = 2;
2946 break;
2947 }
2948
2949 for (i = 0; i < nbouttmp; i++)
2950 to[outcur++] = outtmp[i];
2951 inblk = incur;
2952 }
2953
2954 if (is_break) {
2955 retval = 0;
2956 break;
2957 }
2958 }
2959 }
2960
2961 *tolen = outcur;
2962 *inlen = inblk;
2963 return (retval);
2964}
2965
2966/*
2967 * Test routine for the xmlBase64Decode function
2968 */
2969#if 0
2970int main(int argc, char **argv) {
2971 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
2972 char output[100];
2973 char output2[100];
2974 char output3[100];
2975 unsigned long inlen = strlen(input);
2976 unsigned long outlen = 100;
2977 int ret;
2978 unsigned long cons, tmp, tmp2, prod;
2979
2980 /*
2981 * Direct
2982 */
2983 ret = xmlBase64Decode(input, &inlen, output, &outlen);
2984
2985 output[outlen] = 0;
2986 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
2987
2988 /*
2989 * output chunking
2990 */
2991 cons = 0;
2992 prod = 0;
2993 while (cons < inlen) {
2994 tmp = 5;
2995 tmp2 = inlen - cons;
2996
2997 printf("%ld %ld\n", cons, prod);
2998 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
2999 cons += tmp2;
3000 prod += tmp;
3001 printf("%ld %ld\n", cons, prod);
3002 }
3003 output2[outlen] = 0;
3004 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
3005
3006 /*
3007 * input chunking
3008 */
3009 cons = 0;
3010 prod = 0;
3011 while (cons < inlen) {
3012 tmp = 100 - prod;
3013 tmp2 = inlen - cons;
3014 if (tmp2 > 5)
3015 tmp2 = 5;
3016
3017 printf("%ld %ld\n", cons, prod);
3018 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
3019 cons += tmp2;
3020 prod += tmp;
3021 printf("%ld %ld\n", cons, prod);
3022 }
3023 output3[outlen] = 0;
3024 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
3025 return(0);
3026
3027}
3028#endif