blob: cdc4f9ecee6d041629bb42f9d8cf38fcd10a0e7f [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;
238 xmlTextReaderPtr reader = ctxt->_private;
239
240#ifdef DEBUG_CALLBACKS
241 printf("xmlTextReaderStartElement(%s)\n", fullname);
242#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000243 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000244 reader->startElement(ctx, fullname, atts);
Daniel Veillard067bae52003-01-05 01:27:54 +0000245 if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
246 (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
247 (ctxt->input->cur[1] == '>'))
248 ctxt->node->_private = (void *) xmlTextReaderIsEmpty;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000249 }
Daniel Veillard9e395c22003-01-01 14:50:44 +0000250 if (reader != NULL)
251 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000252}
253
254/**
255 * xmlTextReaderEndElement:
256 * @ctx: the user data (XML parser context)
257 * @fullname: The element name, including namespace prefix
258 *
259 * called when an ending tag has been processed.
260 */
261static void
262xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
263 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
264 xmlTextReaderPtr reader = ctxt->_private;
265
266#ifdef DEBUG_CALLBACKS
267 printf("xmlTextReaderEndElement(%s)\n", fullname);
268#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000269 if ((reader != NULL) && (reader->endElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000270 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000271 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000272}
273
274/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000275 * xmlTextReaderCharacters:
276 * @ctx: the user data (XML parser context)
277 * @ch: a xmlChar string
278 * @len: the number of xmlChar
279 *
280 * receiving some chars from the parser.
281 */
282static void
283xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
284{
285 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
286 xmlTextReaderPtr reader = ctxt->_private;
287
288#ifdef DEBUG_CALLBACKS
289 printf("xmlTextReaderCharacters()\n");
290#endif
291 if ((reader != NULL) && (reader->characters != NULL)) {
292 reader->characters(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000293 }
294}
295
296/**
297 * xmlTextReaderCDataBlock:
298 * @ctx: the user data (XML parser context)
299 * @value: The pcdata content
300 * @len: the block length
301 *
302 * called when a pcdata block has been parsed
303 */
304static void
305xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
306{
307 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
308 xmlTextReaderPtr reader = ctxt->_private;
309
310#ifdef DEBUG_CALLBACKS
311 printf("xmlTextReaderCDataBlock()\n");
312#endif
313 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
314 reader->cdataBlock(ctx, ch, len);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000315 }
316}
317
318/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000319 * xmlTextReaderPushData:
320 * @reader: the xmlTextReaderPtr used
321 *
322 * Push data down the progressive parser until a significant callback
323 * got raised.
324 *
325 * Returns -1 in case of failure, 0 otherwise
326 */
327static int
328xmlTextReaderPushData(xmlTextReaderPtr reader) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000329 xmlBufferPtr inbuf;
Daniel Veillarda880b122003-04-21 21:36:41 +0000330 int val, s;
William M. Brack779af002003-08-01 15:55:39 +0000331 xmlTextReaderState oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000332
333 if ((reader->input == NULL) || (reader->input->buffer == NULL))
334 return(-1);
335
Daniel Veillardea7751d2002-12-20 00:16:24 +0000336 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000337 reader->state = XML_TEXTREADER_NONE;
338 inbuf = reader->input->buffer;
Daniel Veillarda880b122003-04-21 21:36:41 +0000339
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000340 while (reader->state == XML_TEXTREADER_NONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000341 if (inbuf->use < reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000342 /*
343 * Refill the buffer unless we are at the end of the stream
344 */
345 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
346 val = xmlParserInputBufferRead(reader->input, 4096);
347 if (val <= 0) {
348 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000349 reader->state = oldstate;
Daniel Veillardaaa105b2002-12-30 11:42:17 +0000350 if ((oldstate != XML_TEXTREADER_START) ||
351 (reader->ctxt->myDoc != NULL))
352 return(val);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000353 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000354
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000355 } else
356 break;
357 }
Daniel Veillard067bae52003-01-05 01:27:54 +0000358 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000359 * parse by block of CHUNK_SIZE bytes, various tests show that
360 * it's the best tradeoff at least on a 1.2GH Duron
Daniel Veillard067bae52003-01-05 01:27:54 +0000361 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000362 if (inbuf->use >= reader->cur + CHUNK_SIZE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000363 val = xmlParseChunk(reader->ctxt,
364 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000365 CHUNK_SIZE, 0);
366 reader->cur += CHUNK_SIZE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000367 if (val != 0)
368 return(-1);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000369 } else {
Daniel Veillarda880b122003-04-21 21:36:41 +0000370 s = inbuf->use - reader->cur;
371 val = xmlParseChunk(reader->ctxt,
372 (const char *) &inbuf->content[reader->cur],
373 s, 0);
374 reader->cur += s;
375 if (val != 0)
376 return(-1);
377 break;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000378 }
379 }
Daniel Veillarda880b122003-04-21 21:36:41 +0000380
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000381 /*
382 * Discard the consumed input when needed and possible
383 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000384 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000385 if (reader->cur >= 4096) {
386 val = xmlBufferShrink(inbuf, reader->cur);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000387 if (val >= 0) {
388 reader->cur -= val;
389 }
390 }
391 }
392
393 /*
394 * At the end of the stream signal that the work is done to the Push
395 * parser.
396 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000397 else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000398 if (reader->mode != XML_TEXTREADER_DONE) {
Daniel Veillarda880b122003-04-21 21:36:41 +0000399 s = inbuf->use - reader->cur;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000400 val = xmlParseChunk(reader->ctxt,
Daniel Veillard067bae52003-01-05 01:27:54 +0000401 (const char *) &inbuf->content[reader->cur],
Daniel Veillarda880b122003-04-21 21:36:41 +0000402 s, 1);
403 reader->cur = inbuf->use;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000404 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000405 if (val != 0) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000406 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000407 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000408 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000409 return(0);
410}
411
412/**
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000413 * xmlTextReaderValidatePush:
414 * @reader: the xmlTextReaderPtr used
415 *
416 * Push the current node for validation
417 */
418static void
419xmlTextReaderValidatePush(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000420#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000421 xmlNodePtr node = reader->node;
422
Daniel Veillardf4e55762003-04-15 23:32:22 +0000423 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
424 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
425 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
426 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
427 reader->ctxt->myDoc, node, node->name);
428 } else {
429 /* TODO use the BuildQName interface */
430 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000431
Daniel Veillardf4e55762003-04-15 23:32:22 +0000432 qname = xmlStrdup(node->ns->prefix);
433 qname = xmlStrcat(qname, BAD_CAST ":");
434 qname = xmlStrcat(qname, node->name);
435 reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
436 reader->ctxt->myDoc, node, qname);
437 if (qname != NULL)
438 xmlFree(qname);
439 }
440#ifdef LIBXML_SCHEMAS_ENABLED
441 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
442 (reader->rngValidCtxt != NULL)) {
443 int ret;
444
445 if (reader->rngFullNode != NULL) return;
446 ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
447 reader->ctxt->myDoc,
448 node);
449 if (ret == 0) {
450 /*
451 * this element requires a full tree
452 */
453 node = xmlTextReaderExpand(reader);
454 if (node == NULL) {
455printf("Expand failed !\n");
456 ret = -1;
457 } else {
458 ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
459 reader->ctxt->myDoc,
460 node);
461 reader->rngFullNode = node;
462 }
463 }
464 if (ret != 1)
465 reader->rngValidErrors++;
466#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000467 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000468#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000469}
Daniel Veillardf4e55762003-04-15 23:32:22 +0000470
471/**
472 * xmlTextReaderValidateCData:
473 * @reader: the xmlTextReaderPtr used
474 * @data: pointer to the CData
475 * @len: lenght of the CData block in bytes.
476 *
477 * Push some CData for validation
478 */
479static void
480xmlTextReaderValidateCData(xmlTextReaderPtr reader,
481 const xmlChar *data, int len) {
482#ifdef LIBXML_REGEXP_ENABLED
483 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
484 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
485 reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
486 data, len);
487#ifdef LIBXML_SCHEMAS_ENABLED
488 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
489 (reader->rngValidCtxt != NULL)) {
490 int ret;
491
492 if (reader->rngFullNode != NULL) return;
493 ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
494 if (ret != 1)
495 reader->rngValidErrors++;
496#endif
497 }
498#endif /* LIBXML_REGEXP_ENABLED */
499}
500
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000501/**
502 * xmlTextReaderValidatePop:
503 * @reader: the xmlTextReaderPtr used
504 *
505 * Pop the current node from validation
506 */
507static void
508xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000509#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000510 xmlNodePtr node = reader->node;
511
Daniel Veillardf4e55762003-04-15 23:32:22 +0000512 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
513 (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
514 if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
515 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
516 reader->ctxt->myDoc, node, node->name);
517 } else {
518 /* TODO use the BuildQName interface */
519 xmlChar *qname;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000520
Daniel Veillardf4e55762003-04-15 23:32:22 +0000521 qname = xmlStrdup(node->ns->prefix);
522 qname = xmlStrcat(qname, BAD_CAST ":");
523 qname = xmlStrcat(qname, node->name);
524 reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
525 reader->ctxt->myDoc, node, qname);
526 if (qname != NULL)
527 xmlFree(qname);
528 }
529#ifdef LIBXML_SCHEMAS_ENABLED
530 } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
531 (reader->rngValidCtxt != NULL)) {
532 int ret;
533
534 if (reader->rngFullNode != NULL) {
535 if (node == reader->rngFullNode)
536 reader->rngFullNode = NULL;
537 return;
538 }
539 ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
540 reader->ctxt->myDoc,
541 node);
542 if (ret != 1)
543 reader->rngValidErrors++;
544#endif
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000545 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000546#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000547}
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000548/**
549 * xmlTextReaderValidateEntity:
550 * @reader: the xmlTextReaderPtr used
551 *
552 * Handle the validation when an entity reference is encountered and
553 * entity substitution is not activated. As a result the parser interface
554 * must walk through the entity and do the validation calls
555 */
556static void
557xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000558#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000559 xmlNodePtr oldnode = reader->node;
560 xmlNodePtr node = reader->node;
561 xmlParserCtxtPtr ctxt = reader->ctxt;
562
563 do {
564 if (node->type == XML_ENTITY_REF_NODE) {
565 /*
566 * Case where the underlying tree is not availble, lookup the entity
567 * and walk it.
568 */
569 if ((node->children == NULL) && (ctxt->sax != NULL) &&
570 (ctxt->sax->getEntity != NULL)) {
571 node->children = (xmlNodePtr)
572 ctxt->sax->getEntity(ctxt, node->name);
573 }
574
575 if ((node->children != NULL) &&
576 (node->children->type == XML_ENTITY_DECL) &&
577 (node->children->children != NULL)) {
578 xmlTextReaderEntPush(reader, node);
579 node = node->children->children;
580 continue;
581 } else {
582 /*
583 * The error has probably be raised already.
584 */
585 if (node == oldnode)
586 break;
587 node = node->next;
588 }
589 } else if (node->type == XML_ELEMENT_NODE) {
590 reader->node = node;
591 xmlTextReaderValidatePush(reader);
592 } else if ((node->type == XML_TEXT_NODE) ||
593 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000594 xmlTextReaderValidateCData(reader, node->content,
595 xmlStrlen(node->content));
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000596 }
597
598 /*
599 * go to next node
600 */
601 if (node->children != NULL) {
602 node = node->children;
603 continue;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +0000604 } else if (node->type == XML_ELEMENT_NODE) {
605 xmlTextReaderValidatePop(reader);
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000606 }
607 if (node->next != NULL) {
608 node = node->next;
609 continue;
610 }
611 do {
612 node = node->parent;
613 if (node->type == XML_ELEMENT_NODE) {
614 reader->node = node;
615 xmlTextReaderValidatePop(reader);
616 }
617 if ((node->type == XML_ENTITY_DECL) &&
618 (reader->ent != NULL) && (reader->ent->children == node)) {
619 node = xmlTextReaderEntPop(reader);
620 }
621 if (node == oldnode)
622 break;
623 if (node->next != NULL) {
624 node = node->next;
625 break;
626 }
627 } while ((node != NULL) && (node != oldnode));
628 } while ((node != NULL) && (node != oldnode));
629 reader->node = oldnode;
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000630#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000631}
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000632
633
634/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000635 * xmlTextReaderGetSuccessor:
636 * @cur: the current node
637 *
638 * Get the successor of a node if available.
639 *
640 * Returns the successor node or NULL
641 */
642static xmlNodePtr
643xmlTextReaderGetSuccessor(xmlNodePtr cur) {
644 if (cur == NULL) return(NULL) ; /* ERROR */
645 if (cur->next != NULL) return(cur->next) ;
646 do {
647 cur = cur->parent;
648 if (cur == NULL) return(NULL);
649 if (cur->next != NULL) return(cur->next);
650 } while (cur != NULL);
651 return(cur);
652}
653
654/**
655 * xmlTextReaderDoExpand:
656 * @reader: the xmlTextReaderPtr used
657 *
658 * Makes sure that the current node is fully read as well as all its
659 * descendant. It means the full DOM subtree must be available at the
660 * end of the call.
661 *
662 * Returns 1 if the node was expanded successfully, 0 if there is no more
663 * nodes to read, or -1 in case of error
664 */
665static int
666xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
667 int val;
668
669 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
670 return(-1);
671
672 do {
673 if (xmlTextReaderGetSuccessor(reader->node) != NULL)
674 return(1);
Daniel Veillarda37aab82003-06-09 09:10:36 +0000675 if (reader->ctxt->nodeNr <= reader->depth)
676 return(1);
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000677 if (reader->mode == XML_TEXTREADER_MODE_EOF)
678 return(1);
679 val = xmlTextReaderPushData(reader);
680 if (val < 0)
681 return(-1);
682 } while(reader->mode != XML_TEXTREADER_MODE_EOF);
683 return(1);
684}
685
686/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000687 * xmlTextReaderRead:
688 * @reader: the xmlTextReaderPtr used
689 *
690 * Moves the position of the current instance to the next node in
691 * the stream, exposing its properties.
692 *
693 * Returns 1 if the node was read successfully, 0 if there is no more
694 * nodes to read, or -1 in case of error
695 */
696int
697xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillard067bae52003-01-05 01:27:54 +0000698 int val, olddepth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000699 xmlTextReaderState oldstate = 0;
700 xmlNodePtr oldnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000701
702 if ((reader == NULL) || (reader->ctxt == NULL))
703 return(-1);
704 if (reader->ctxt->wellFormed != 1)
705 return(-1);
706
707#ifdef DEBUG_READER
708 fprintf(stderr, "\nREAD ");
709 DUMP_READER
710#endif
Daniel Veillard29b3e282002-12-29 11:14:41 +0000711 reader->curnode = NULL;
Daniel Veillard67df8092002-12-16 22:04:11 +0000712 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
713 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000714 /*
715 * Initial state
716 */
717 do {
718 val = xmlTextReaderPushData(reader);
719 if (val < 0)
720 return(-1);
721 } while ((reader->ctxt->node == NULL) &&
Daniel Veillard067bae52003-01-05 01:27:54 +0000722 ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
723 (reader->mode != XML_TEXTREADER_DONE)));
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000724 if (reader->ctxt->node == NULL) {
Daniel Veillarddab8ea92003-01-02 14:16:45 +0000725 if (reader->ctxt->myDoc != NULL) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000726 reader->node = reader->ctxt->myDoc->children;
Daniel Veillarddab8ea92003-01-02 14:16:45 +0000727 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000728 if (reader->node == NULL)
729 return(-1);
Daniel Veillarddab8ea92003-01-02 14:16:45 +0000730 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000731 } else {
Daniel Veillard48ef4c92003-03-22 12:38:15 +0000732 if (reader->ctxt->myDoc != NULL) {
733 reader->node = reader->ctxt->myDoc->children;
734 }
735 if (reader->node == NULL)
736 reader->node = reader->ctxt->nodeTab[0];
Daniel Veillarde59494f2003-01-04 16:35:29 +0000737 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000738 }
Daniel Veillard4d8db8a2002-12-30 18:40:42 +0000739 reader->depth = 0;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000740 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000741 }
742 oldstate = reader->state;
743 olddepth = reader->ctxt->nodeNr;
744 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +0000745
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000746get_next_node:
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000747 /*
748 * If we are not backtracking on ancestors or examined nodes,
749 * that the parser didn't finished or that we arent at the end
750 * of stream, continue processing.
751 */
Daniel Veillarda880b122003-04-21 21:36:41 +0000752 while ((reader->node->next == NULL) &&
753 (reader->ctxt->nodeNr == olddepth) &&
754 ((oldstate == XML_TEXTREADER_BACKTRACK) ||
Daniel Veillardea7751d2002-12-20 00:16:24 +0000755 (reader->node->children == NULL) ||
756 (reader->node->type == XML_ENTITY_REF_NODE) ||
Daniel Veillard409a8142003-07-18 15:16:57 +0000757 ((reader->node->children != NULL) &&
758 (reader->node->children->type == XML_TEXT_NODE) &&
759 (reader->node->children->next == NULL)) ||
Daniel Veillard4dbe77a2003-01-14 00:17:42 +0000760 (reader->node->type == XML_DTD_NODE) ||
761 (reader->node->type == XML_DOCUMENT_NODE) ||
762 (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
Daniel Veillard4dbe77a2003-01-14 00:17:42 +0000763 ((reader->ctxt->node == NULL) ||
764 (reader->ctxt->node == reader->node) ||
765 (reader->ctxt->node == reader->node->parent)) &&
Daniel Veillardea7751d2002-12-20 00:16:24 +0000766 (reader->ctxt->instate != XML_PARSER_EOF)) {
767 val = xmlTextReaderPushData(reader);
768 if (val < 0)
769 return(-1);
770 if (reader->node == NULL)
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000771 goto node_end;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000772 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000773 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000774 if ((reader->node->children != NULL) &&
775 (reader->node->type != XML_ENTITY_REF_NODE) &&
776 (reader->node->type != XML_DTD_NODE)) {
777 reader->node = reader->node->children;
778 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +0000779 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000780 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000781 }
782 }
783 if (reader->node->next != NULL) {
784 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +0000785 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +0000786 (reader->node->children == NULL) &&
787 (reader->node->_private != (void *)xmlTextReaderIsEmpty)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000788 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000789 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000790 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000791 if ((reader->validate) &&
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000792 (reader->node->type == XML_ELEMENT_NODE))
793 xmlTextReaderValidatePop(reader);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000794 reader->node = reader->node->next;
795 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000796
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000797 /*
798 * Cleanup of the old node
799 */
Daniel Veillard4dbe77a2003-01-14 00:17:42 +0000800 if ((reader->node->prev != NULL) &&
801 (reader->node->prev->type != XML_DTD_NODE)) {
802 xmlNodePtr tmp = reader->node->prev;
803 xmlUnlinkNode(tmp);
804 xmlFreeNode(tmp);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000805 }
806
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000807 goto node_found;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000808 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000809 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillard571b8892002-12-30 12:37:59 +0000810 (reader->node->type == XML_ELEMENT_NODE) &&
Daniel Veillard067bae52003-01-05 01:27:54 +0000811 (reader->node->children == NULL) &&
812 (reader->node->_private != (void *)xmlTextReaderIsEmpty)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000813 reader->state = XML_TEXTREADER_END;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000814 goto node_found;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000815 }
Daniel Veillardf4e55762003-04-15 23:32:22 +0000816 if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000817 xmlTextReaderValidatePop(reader);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000818 reader->node = reader->node->parent;
819 if ((reader->node == NULL) ||
820 (reader->node->type == XML_DOCUMENT_NODE) ||
821#ifdef LIBXML_DOCB_ENABLED
822 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
823#endif
824 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000825 if (reader->mode != XML_TEXTREADER_DONE) {
826 val = xmlParseChunk(reader->ctxt, "", 0, 1);
827 reader->mode = XML_TEXTREADER_DONE;
828 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000829 reader->node = NULL;
Daniel Veillard4d8db8a2002-12-30 18:40:42 +0000830 reader->depth = -1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000831
832 /*
833 * Cleanup of the old node
834 */
835 if (oldnode->type != XML_DTD_NODE) {
836 xmlUnlinkNode(oldnode);
837 xmlFreeNode(oldnode);
838 }
839
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000840 goto node_end;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000841 }
842 reader->depth--;
843 reader->state = XML_TEXTREADER_BACKTRACK;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000844
845node_found:
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000846 DUMP_READER
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000847
848 /*
Daniel Veillarda880b122003-04-21 21:36:41 +0000849 * If we are in the middle of a piece of CDATA make sure it's finished
850 */
851 if ((reader->node != NULL) &&
852 ((reader->node->type == XML_TEXT_NODE) ||
853 (reader->node->type == XML_CDATA_SECTION_NODE))) {
854 xmlTextReaderExpand(reader);
855 }
856
857 /*
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000858 * Handle entities enter and exit when in entity replacement mode
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000859 */
860 if ((reader->node != NULL) &&
861 (reader->node->type == XML_ENTITY_REF_NODE) &&
862 (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
863 /*
864 * Case where the underlying tree is not availble, lookup the entity
865 * and walk it.
866 */
867 if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
868 (reader->ctxt->sax->getEntity != NULL)) {
869 reader->node->children = (xmlNodePtr)
870 reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
871 }
872
873 if ((reader->node->children != NULL) &&
874 (reader->node->children->type == XML_ENTITY_DECL) &&
875 (reader->node->children->children != NULL)) {
876 xmlTextReaderEntPush(reader, reader->node);
877 reader->node = reader->node->children->children;
878 }
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000879 } else if ((reader->node != NULL) &&
880 (reader->node->type == XML_ENTITY_REF_NODE) &&
Daniel Veillardf4e55762003-04-15 23:32:22 +0000881 (reader->ctxt != NULL) && (reader->validate)) {
Daniel Veillarda80ff6e2003-01-03 12:52:08 +0000882 xmlTextReaderValidateEntity(reader);
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000883 }
884 if ((reader->node != NULL) &&
885 (reader->node->type == XML_ENTITY_DECL) &&
886 (reader->ent != NULL) && (reader->ent->children == reader->node)) {
887 reader->node = xmlTextReaderEntPop(reader);
888 reader->depth++;
889 goto get_next_node;
890 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000891#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000892 if ((reader->validate) && (reader->node != NULL)) {
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000893 xmlNodePtr node = reader->node;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000894
895 if ((node->type == XML_ELEMENT_NODE) &&
896 ((reader->state != XML_TEXTREADER_END) &&
897 (reader->state != XML_TEXTREADER_BACKTRACK))) {
898 xmlTextReaderValidatePush(reader);
899 } else if ((node->type == XML_TEXT_NODE) ||
900 (node->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +0000901 xmlTextReaderValidateCData(reader, node->content,
902 xmlStrlen(node->content));
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000903 }
904 }
Daniel Veillard0e298ad2003-02-04 16:14:33 +0000905#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000906 return(1);
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000907node_end:
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000908 reader->mode = XML_TEXTREADER_DONE;
Daniel Veillard1fdfd112003-01-03 01:18:43 +0000909 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000910}
911
Daniel Veillard67df8092002-12-16 22:04:11 +0000912/**
913 * xmlTextReaderReadState:
914 * @reader: the xmlTextReaderPtr used
915 *
916 * Gets the read state of the reader.
917 *
918 * Returns the state value, or -1 in case of error
919 */
920int
921xmlTextReaderReadState(xmlTextReaderPtr reader) {
922 if (reader == NULL)
923 return(-1);
924 return(reader->mode);
925}
926
927/**
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000928 * xmlTextReaderExpand:
929 * @reader: the xmlTextReaderPtr used
930 *
931 * Reads the contents of the current node and the full subtree. It then makes
Daniel Veillard61c52202003-04-30 12:20:34 +0000932 * the subtree available until the next xmlTextReaderRead() call
Daniel Veillardc6cae7b2003-04-11 09:02:11 +0000933 *
934 * Returns a node pointer valid until the next xmlTextReaderRead() call
935 * or NULL in case of error.
936 */
937xmlNodePtr
938xmlTextReaderExpand(xmlTextReaderPtr reader) {
939 if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
940 return(NULL);
941 if (xmlTextReaderDoExpand(reader) < 0)
942 return(NULL);
943 return(reader->node);
944}
945
946/**
947 * xmlTextReaderNext:
948 * @reader: the xmlTextReaderPtr used
949 *
950 * Skip to the node following the current one in document order while
951 * avoiding the subtree if any.
952 *
953 * Returns 1 if the node was read successfully, 0 if there is no more
954 * nodes to read, or -1 in case of error
955 */
956int
957xmlTextReaderNext(xmlTextReaderPtr reader) {
958 int ret;
959 xmlNodePtr cur;
960
961 if (reader == NULL)
962 return(-1);
963 cur = reader->node;
964 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
965 return(xmlTextReaderRead(reader));
966 if (reader->state == XML_TEXTREADER_END)
967 return(xmlTextReaderRead(reader));
968 if (cur->_private == (void *)xmlTextReaderIsEmpty)
969 return(xmlTextReaderRead(reader));
970 do {
971 ret = xmlTextReaderRead(reader);
972 if (ret != 1)
973 return(ret);
974 } while (reader->node != cur);
975 return(xmlTextReaderRead(reader));
976}
977
978/**
Daniel Veillard67df8092002-12-16 22:04:11 +0000979 * xmlTextReaderReadInnerXml:
980 * @reader: the xmlTextReaderPtr used
981 *
982 * Reads the contents of the current node, including child nodes and markup.
983 *
984 * Returns a string containing the XML content, or NULL if the current node
985 * is neither an element nor attribute, or has no child nodes. The
986 * string must be deallocated by the caller.
987 */
988xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +0000989xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +0000990 TODO
991 return(NULL);
992}
993
994/**
995 * xmlTextReaderReadOuterXml:
996 * @reader: the xmlTextReaderPtr used
997 *
998 * Reads the contents of the current node, including child nodes and markup.
999 *
1000 * Returns a string containing the XML content, or NULL if the current node
1001 * is neither an element nor attribute, or has no child nodes. The
1002 * string must be deallocated by the caller.
1003 */
1004xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001005xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001006 TODO
1007 return(NULL);
1008}
1009
1010/**
1011 * xmlTextReaderReadString:
1012 * @reader: the xmlTextReaderPtr used
1013 *
1014 * Reads the contents of an element or a text node as a string.
1015 *
1016 * Returns a string containing the contents of the Element or Text node,
1017 * or NULL if the reader is positioned on any other type of node.
1018 * The string must be deallocated by the caller.
1019 */
1020xmlChar *
Daniel Veillard33300b42003-04-17 09:09:19 +00001021xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
Daniel Veillard67df8092002-12-16 22:04:11 +00001022 TODO
1023 return(NULL);
1024}
1025
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001026/**
1027 * xmlTextReaderReadBase64:
1028 * @reader: the xmlTextReaderPtr used
1029 * @array: a byte array to store the content.
1030 * @offset: the zero-based index into array where the method should
1031 * begin to write.
1032 * @len: the number of bytes to write.
1033 *
1034 * Reads and decodes the Base64 encoded contents of an element and
1035 * stores the result in a byte buffer.
1036 *
1037 * Returns the number of bytes written to array, or zero if the current
1038 * instance is not positioned on an element or -1 in case of error.
1039 */
1040int
1041xmlTextReaderReadBase64(xmlTextReaderPtr reader, unsigned char *array,
1042 int offset, int len) {
1043 if ((reader == NULL) || (reader->ctxt == NULL))
1044 return(-1);
1045 if (reader->ctxt->wellFormed != 1)
1046 return(-1);
1047
1048 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1049 return(0);
1050 TODO
1051 return(0);
1052}
1053
1054/**
1055 * xmlTextReaderReadBinHex:
1056 * @reader: the xmlTextReaderPtr used
1057 * @array: a byte array to store the content.
1058 * @offset: the zero-based index into array where the method should
1059 * begin to write.
1060 * @len: the number of bytes to write.
1061 *
1062 * Reads and decodes the BinHex encoded contents of an element and
1063 * stores the result in a byte buffer.
1064 *
1065 * Returns the number of bytes written to array, or zero if the current
1066 * instance is not positioned on an element or -1 in case of error.
1067 */
1068int
1069xmlTextReaderReadBinHex(xmlTextReaderPtr reader, unsigned char *array,
1070 int offset, int len) {
1071 if ((reader == NULL) || (reader->ctxt == NULL))
1072 return(-1);
1073 if (reader->ctxt->wellFormed != 1)
1074 return(-1);
1075
1076 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
1077 return(0);
1078 TODO
1079 return(0);
1080}
1081
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001082/************************************************************************
1083 * *
1084 * Constructor and destructors *
1085 * *
1086 ************************************************************************/
1087/**
1088 * xmlNewTextReader:
1089 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +00001090 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001091 *
1092 * Create an xmlTextReader structure fed with @input
1093 *
1094 * Returns the new xmlTextReaderPtr or NULL in case of error
1095 */
1096xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +00001097xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001098 xmlTextReaderPtr ret;
1099 int val;
1100
1101 if (input == NULL)
1102 return(NULL);
1103 ret = xmlMalloc(sizeof(xmlTextReader));
1104 if (ret == NULL) {
1105 xmlGenericError(xmlGenericErrorContext,
1106 "xmlNewTextReader : malloc failed\n");
1107 return(NULL);
1108 }
1109 memset(ret, 0, sizeof(xmlTextReader));
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001110 ret->entTab = NULL;
1111 ret->entMax = 0;
1112 ret->entNr = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001113 ret->input = input;
1114 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1115 if (ret->sax == NULL) {
1116 xmlFree(ret);
1117 xmlGenericError(xmlGenericErrorContext,
1118 "xmlNewTextReader : malloc failed\n");
1119 return(NULL);
1120 }
1121 memcpy(ret->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler));
1122 ret->startElement = ret->sax->startElement;
1123 ret->sax->startElement = xmlTextReaderStartElement;
1124 ret->endElement = ret->sax->endElement;
1125 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001126 ret->characters = ret->sax->characters;
1127 ret->sax->characters = xmlTextReaderCharacters;
1128 ret->cdataBlock = ret->sax->cdataBlock;
1129 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001130
Daniel Veillard67df8092002-12-16 22:04:11 +00001131 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001132 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001133 ret->curnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001134 val = xmlParserInputBufferRead(input, 4);
1135 if (val >= 4) {
1136 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +00001137 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001138 ret->base = 0;
1139 ret->cur = 4;
1140 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +00001141 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001142 ret->base = 0;
1143 ret->cur = 0;
1144 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001145 if (ret->ctxt == NULL) {
1146 xmlGenericError(xmlGenericErrorContext,
1147 "xmlNewTextReader : malloc failed\n");
1148 xmlFree(ret->sax);
1149 xmlFree(ret);
1150 return(NULL);
1151 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001152 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001153 ret->ctxt->linenumbers = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001154 ret->allocs = XML_TEXTREADER_CTXT;
1155 return(ret);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001156}
1157
1158/**
1159 * xmlNewTextReaderFilename:
1160 * @URI: the URI of the resource to process
1161 *
1162 * Create an xmlTextReader structure fed with the resource at @URI
1163 *
1164 * Returns the new xmlTextReaderPtr or NULL in case of error
1165 */
1166xmlTextReaderPtr
1167xmlNewTextReaderFilename(const char *URI) {
1168 xmlParserInputBufferPtr input;
1169 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001170 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001171
1172 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
1173 if (input == NULL)
1174 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001175 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001176 if (ret == NULL) {
1177 xmlFreeParserInputBuffer(input);
1178 return(NULL);
1179 }
1180 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001181 if (ret->ctxt->directory == NULL)
1182 directory = xmlParserGetDirectory(URI);
1183 if ((ret->ctxt->directory == NULL) && (directory != NULL))
1184 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
1185 if (directory != NULL)
1186 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001187 return(ret);
1188}
1189
1190/**
1191 * xmlFreeTextReader:
1192 * @reader: the xmlTextReaderPtr
1193 *
1194 * Deallocate all the resources associated to the reader
1195 */
1196void
1197xmlFreeTextReader(xmlTextReaderPtr reader) {
1198 if (reader == NULL)
1199 return;
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001200#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001201 if (reader->rngSchemas != NULL) {
1202 xmlRelaxNGFree(reader->rngSchemas);
1203 reader->rngSchemas = NULL;
1204 }
1205 if (reader->rngValidCtxt != NULL) {
1206 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
1207 reader->rngValidCtxt = NULL;
1208 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001209#endif
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001210 if (reader->ctxt != NULL) {
1211 if (reader->ctxt->myDoc != NULL) {
1212 xmlFreeDoc(reader->ctxt->myDoc);
1213 reader->ctxt->myDoc = NULL;
1214 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +00001215 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
1216 (reader->ctxt->vctxt.vstateMax > 0)){
1217 xmlFree(reader->ctxt->vctxt.vstateTab);
1218 reader->ctxt->vctxt.vstateTab = 0;
1219 reader->ctxt->vctxt.vstateMax = 0;
1220 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001221 if (reader->allocs & XML_TEXTREADER_CTXT)
1222 xmlFreeParserCtxt(reader->ctxt);
1223 }
1224 if (reader->sax != NULL)
1225 xmlFree(reader->sax);
1226 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
1227 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001228 if (reader->faketext != NULL) {
1229 xmlFreeNode(reader->faketext);
1230 }
Daniel Veillard1fdfd112003-01-03 01:18:43 +00001231 if (reader->entTab != NULL)
1232 xmlFree(reader->entTab);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001233 xmlFree(reader);
1234}
1235
1236/************************************************************************
1237 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001238 * Methods for XmlTextReader *
1239 * *
1240 ************************************************************************/
1241/**
1242 * xmlTextReaderClose:
1243 * @reader: the xmlTextReaderPtr used
1244 *
1245 * This method releases any resources allocated by the current instance
1246 * changes the state to Closed and close any underlying input.
1247 *
1248 * Returns 0 or -1 in case of error
1249 */
1250int
1251xmlTextReaderClose(xmlTextReaderPtr reader) {
1252 if (reader == NULL)
1253 return(-1);
1254 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001255 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001256 reader->mode = XML_TEXTREADER_MODE_CLOSED;
1257 if (reader->ctxt != NULL) {
1258 if (reader->ctxt->myDoc != NULL) {
1259 xmlFreeDoc(reader->ctxt->myDoc);
1260 reader->ctxt->myDoc = NULL;
1261 }
1262 if (reader->allocs & XML_TEXTREADER_CTXT) {
1263 xmlFreeParserCtxt(reader->ctxt);
1264 reader->allocs -= XML_TEXTREADER_CTXT;
1265 }
1266 }
1267 if (reader->sax != NULL) {
1268 xmlFree(reader->sax);
1269 reader->sax = NULL;
1270 }
1271 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
1272 xmlFreeParserInputBuffer(reader->input);
1273 reader->allocs -= XML_TEXTREADER_INPUT;
1274 }
1275 return(0);
1276}
1277
1278/**
1279 * xmlTextReaderGetAttributeNo:
1280 * @reader: the xmlTextReaderPtr used
1281 * @no: the zero-based index of the attribute relative to the containing element
1282 *
1283 * Provides the value of the attribute with the specified index relative
1284 * to the containing element.
1285 *
1286 * Returns a string containing the value of the specified attribute, or NULL
1287 * in case of error. The string must be deallocated by the caller.
1288 */
1289xmlChar *
1290xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
1291 xmlChar *ret;
1292 int i;
1293 xmlAttrPtr cur;
1294 xmlNsPtr ns;
1295
1296 if (reader == NULL)
1297 return(NULL);
1298 if (reader->node == NULL)
1299 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001300 if (reader->curnode != NULL)
1301 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001302 /* TODO: handle the xmlDecl */
1303 if (reader->node->type != XML_ELEMENT_NODE)
1304 return(NULL);
1305
1306 ns = reader->node->nsDef;
1307 for (i = 0;(i < no) && (ns != NULL);i++) {
1308 ns = ns->next;
1309 }
1310 if (ns != NULL)
1311 return(xmlStrdup(ns->href));
1312
1313 cur = reader->node->properties;
1314 if (cur == NULL)
1315 return(NULL);
1316 for (;i < no;i++) {
1317 cur = cur->next;
1318 if (cur == NULL)
1319 return(NULL);
1320 }
1321 /* TODO walk the DTD if present */
1322
1323 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
1324 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
1325 return(ret);
1326}
1327
1328/**
1329 * xmlTextReaderGetAttribute:
1330 * @reader: the xmlTextReaderPtr used
1331 * @name: the qualified name of the attribute.
1332 *
1333 * Provides the value of the attribute with the specified qualified name.
1334 *
1335 * Returns a string containing the value of the specified attribute, or NULL
1336 * in case of error. The string must be deallocated by the caller.
1337 */
1338xmlChar *
1339xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1340 xmlChar *prefix = NULL;
1341 xmlChar *localname;
1342 xmlNsPtr ns;
1343 xmlChar *ret = NULL;
1344
1345 if ((reader == NULL) || (name == NULL))
1346 return(NULL);
1347 if (reader->node == NULL)
1348 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001349 if (reader->curnode != NULL)
1350 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001351
1352 /* TODO: handle the xmlDecl */
1353 if (reader->node->type != XML_ELEMENT_NODE)
1354 return(NULL);
1355
1356 localname = xmlSplitQName2(name, &prefix);
1357 if (localname == NULL)
1358 return(xmlGetProp(reader->node, name));
1359
1360 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1361 if (ns != NULL)
1362 ret = xmlGetNsProp(reader->node, localname, ns->href);
1363
1364 if (localname != NULL)
1365 xmlFree(localname);
1366 if (prefix != NULL)
1367 xmlFree(prefix);
1368 return(ret);
1369}
1370
1371
1372/**
1373 * xmlTextReaderGetAttributeNs:
1374 * @reader: the xmlTextReaderPtr used
1375 * @localName: the local name of the attribute.
1376 * @namespaceURI: the namespace URI of the attribute.
1377 *
1378 * Provides the value of the specified attribute
1379 *
1380 * Returns a string containing the value of the specified attribute, or NULL
1381 * in case of error. The string must be deallocated by the caller.
1382 */
1383xmlChar *
1384xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
1385 const xmlChar *namespaceURI) {
1386 if ((reader == NULL) || (localName == NULL))
1387 return(NULL);
1388 if (reader->node == NULL)
1389 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001390 if (reader->curnode != NULL)
1391 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001392
1393 /* TODO: handle the xmlDecl */
1394 if (reader->node->type != XML_ELEMENT_NODE)
1395 return(NULL);
1396
1397 return(xmlGetNsProp(reader->node, localName, namespaceURI));
1398}
1399
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001400/**
1401 * xmlTextReaderGetRemainder:
1402 * @reader: the xmlTextReaderPtr used
1403 *
1404 * Method to get the remainder of the buffered XML. this method stops the
1405 * parser, set its state to End Of File and return the input stream with
1406 * what is left that the parser did not use.
1407 *
1408 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
1409 * in case of error.
1410 */
1411xmlParserInputBufferPtr
1412xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
1413 xmlParserInputBufferPtr ret = NULL;
1414
1415 if (reader == NULL)
1416 return(NULL);
1417 if (reader->node == NULL)
1418 return(NULL);
1419
1420 reader->node = NULL;
1421 reader->curnode = NULL;
1422 reader->mode = XML_TEXTREADER_MODE_EOF;
1423 if (reader->ctxt != NULL) {
1424 if (reader->ctxt->myDoc != NULL) {
1425 xmlFreeDoc(reader->ctxt->myDoc);
1426 reader->ctxt->myDoc = NULL;
1427 }
1428 if (reader->allocs & XML_TEXTREADER_CTXT) {
1429 xmlFreeParserCtxt(reader->ctxt);
1430 reader->allocs -= XML_TEXTREADER_CTXT;
1431 }
1432 }
1433 if (reader->sax != NULL) {
1434 xmlFree(reader->sax);
1435 reader->sax = NULL;
1436 }
1437 if (reader->allocs & XML_TEXTREADER_INPUT) {
1438 ret = reader->input;
1439 reader->allocs -= XML_TEXTREADER_INPUT;
1440 } else {
1441 /*
1442 * Hum, one may need to duplicate the data structure because
1443 * without reference counting the input may be freed twice:
1444 * - by the layer which allocated it.
1445 * - by the layer to which would have been returned to.
1446 */
1447 TODO
1448 return(NULL);
1449 }
1450 return(ret);
1451}
1452
1453/**
1454 * xmlTextReaderLookupNamespace:
1455 * @reader: the xmlTextReaderPtr used
1456 * @prefix: the prefix whose namespace URI is to be resolved. To return
1457 * the default namespace, specify NULL
1458 *
1459 * Resolves a namespace prefix in the scope of the current element.
1460 *
1461 * Returns a string containing the namespace URI to which the prefix maps
1462 * or NULL in case of error. The string must be deallocated by the caller.
1463 */
1464xmlChar *
1465xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
1466 xmlNsPtr ns;
1467
1468 if (reader == NULL)
1469 return(NULL);
1470 if (reader->node == NULL)
1471 return(NULL);
1472
1473 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
1474 if (ns == NULL)
1475 return(NULL);
1476 return(xmlStrdup(ns->href));
1477}
1478
1479/**
1480 * xmlTextReaderMoveToAttributeNo:
1481 * @reader: the xmlTextReaderPtr used
1482 * @no: the zero-based index of the attribute relative to the containing
1483 * element.
1484 *
1485 * Moves the position of the current instance to the attribute with
1486 * the specified index relative to the containing element.
1487 *
1488 * Returns 1 in case of success, -1 in case of error, 0 if not found
1489 */
1490int
1491xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
1492 int i;
1493 xmlAttrPtr cur;
1494 xmlNsPtr ns;
1495
1496 if (reader == NULL)
1497 return(-1);
1498 if (reader->node == NULL)
1499 return(-1);
1500 /* TODO: handle the xmlDecl */
1501 if (reader->node->type != XML_ELEMENT_NODE)
1502 return(-1);
1503
1504 reader->curnode = NULL;
1505
1506 ns = reader->node->nsDef;
1507 for (i = 0;(i < no) && (ns != NULL);i++) {
1508 ns = ns->next;
1509 }
1510 if (ns != NULL) {
1511 reader->curnode = (xmlNodePtr) ns;
1512 return(1);
1513 }
1514
1515 cur = reader->node->properties;
1516 if (cur == NULL)
1517 return(0);
1518 for (;i < no;i++) {
1519 cur = cur->next;
1520 if (cur == NULL)
1521 return(0);
1522 }
1523 /* TODO walk the DTD if present */
1524
1525 reader->curnode = (xmlNodePtr) cur;
1526 return(1);
1527}
1528
1529/**
1530 * xmlTextReaderMoveToAttribute:
1531 * @reader: the xmlTextReaderPtr used
1532 * @name: the qualified name of the attribute.
1533 *
1534 * Moves the position of the current instance to the attribute with
1535 * the specified qualified name.
1536 *
1537 * Returns 1 in case of success, -1 in case of error, 0 if not found
1538 */
1539int
1540xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1541 xmlChar *prefix = NULL;
1542 xmlChar *localname;
1543 xmlNsPtr ns;
1544 xmlAttrPtr prop;
1545
1546 if ((reader == NULL) || (name == NULL))
1547 return(-1);
1548 if (reader->node == NULL)
1549 return(-1);
1550
1551 /* TODO: handle the xmlDecl */
1552 if (reader->node->type != XML_ELEMENT_NODE)
1553 return(0);
1554
1555 localname = xmlSplitQName2(name, &prefix);
1556 if (localname == NULL) {
1557 /*
1558 * Namespace default decl
1559 */
1560 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
1561 ns = reader->node->nsDef;
1562 while (ns != NULL) {
1563 if (ns->prefix == NULL) {
1564 reader->curnode = (xmlNodePtr) ns;
1565 return(1);
1566 }
1567 ns = ns->next;
1568 }
1569 return(0);
1570 }
1571
1572 prop = reader->node->properties;
1573 while (prop != NULL) {
1574 /*
1575 * One need to have
1576 * - same attribute names
1577 * - and the attribute carrying that namespace
1578 */
1579 if ((xmlStrEqual(prop->name, name)) &&
1580 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
1581 reader->curnode = (xmlNodePtr) prop;
1582 return(1);
1583 }
1584 prop = prop->next;
1585 }
1586 return(0);
1587 }
1588
1589 /*
1590 * Namespace default decl
1591 */
1592 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
1593 ns = reader->node->nsDef;
1594 while (ns != NULL) {
1595 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
1596 reader->curnode = (xmlNodePtr) ns;
1597 goto found;
1598 }
1599 ns = ns->next;
1600 }
1601 goto not_found;
1602 }
1603 prop = reader->node->properties;
1604 while (prop != NULL) {
1605 /*
1606 * One need to have
1607 * - same attribute names
1608 * - and the attribute carrying that namespace
1609 */
1610 if ((xmlStrEqual(prop->name, localname)) &&
1611 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
1612 reader->curnode = (xmlNodePtr) prop;
1613 goto found;
1614 }
1615 prop = prop->next;
1616 }
1617not_found:
1618 if (localname != NULL)
1619 xmlFree(localname);
1620 if (prefix != NULL)
1621 xmlFree(prefix);
1622 return(0);
1623
1624found:
1625 if (localname != NULL)
1626 xmlFree(localname);
1627 if (prefix != NULL)
1628 xmlFree(prefix);
1629 return(1);
1630}
1631
1632/**
1633 * xmlTextReaderMoveToAttributeNs:
1634 * @reader: the xmlTextReaderPtr used
1635 * @localName: the local name of the attribute.
1636 * @namespaceURI: the namespace URI of the attribute.
1637 *
1638 * Moves the position of the current instance to the attribute with the
1639 * specified local name and namespace URI.
1640 *
1641 * Returns 1 in case of success, -1 in case of error, 0 if not found
1642 */
1643int
1644xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
1645 const xmlChar *localName, const xmlChar *namespaceURI) {
1646 xmlAttrPtr prop;
1647 xmlNodePtr node;
1648
1649 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
1650 return(-1);
1651 if (reader->node == NULL)
1652 return(-1);
1653 if (reader->node->type != XML_ELEMENT_NODE)
1654 return(0);
1655 node = reader->node;
1656
1657 /*
1658 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
1659 * namespace name associated to "xmlns"
1660 */
1661 prop = node->properties;
1662 while (prop != NULL) {
1663 /*
1664 * One need to have
1665 * - same attribute names
1666 * - and the attribute carrying that namespace
1667 */
1668 if (xmlStrEqual(prop->name, localName) &&
1669 ((prop->ns != NULL) &&
1670 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
1671 reader->curnode = (xmlNodePtr) prop;
1672 return(1);
1673 }
1674 prop = prop->next;
1675 }
1676 return(0);
1677}
1678
1679/**
1680 * xmlTextReaderMoveToFirstAttribute:
1681 * @reader: the xmlTextReaderPtr used
1682 *
1683 * Moves the position of the current instance to the first attribute
1684 * associated with the current node.
1685 *
1686 * Returns 1 in case of success, -1 in case of error, 0 if not found
1687 */
1688int
1689xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
1690 if (reader == NULL)
1691 return(-1);
1692 if (reader->node == NULL)
1693 return(-1);
1694 if (reader->node->type != XML_ELEMENT_NODE)
1695 return(0);
1696
1697 if (reader->node->nsDef != NULL) {
1698 reader->curnode = (xmlNodePtr) reader->node->nsDef;
1699 return(1);
1700 }
1701 if (reader->node->properties != NULL) {
1702 reader->curnode = (xmlNodePtr) reader->node->properties;
1703 return(1);
1704 }
1705 return(0);
1706}
1707
1708/**
1709 * xmlTextReaderMoveToNextAttribute:
1710 * @reader: the xmlTextReaderPtr used
1711 *
1712 * Moves the position of the current instance to the next attribute
1713 * associated with the current node.
1714 *
1715 * Returns 1 in case of success, -1 in case of error, 0 if not found
1716 */
1717int
1718xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
1719 if (reader == NULL)
1720 return(-1);
1721 if (reader->node == NULL)
1722 return(-1);
1723 if (reader->node->type != XML_ELEMENT_NODE)
1724 return(0);
1725 if (reader->curnode == NULL)
1726 return(xmlTextReaderMoveToFirstAttribute(reader));
1727
1728 if (reader->curnode->type == XML_NAMESPACE_DECL) {
1729 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
1730 if (ns->next != NULL) {
1731 reader->curnode = (xmlNodePtr) ns->next;
1732 return(1);
1733 }
1734 if (reader->node->properties != NULL) {
1735 reader->curnode = (xmlNodePtr) reader->node->properties;
1736 return(1);
1737 }
1738 return(0);
1739 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
1740 (reader->curnode->next != NULL)) {
1741 reader->curnode = reader->curnode->next;
1742 return(1);
1743 }
1744 return(0);
1745}
1746
1747/**
1748 * xmlTextReaderMoveToElement:
1749 * @reader: the xmlTextReaderPtr used
1750 *
1751 * Moves the position of the current instance to the node that
1752 * contains the current Attribute node.
1753 *
1754 * Returns 1 in case of success, -1 in case of error, 0 if not moved
1755 */
1756int
1757xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
1758 if (reader == NULL)
1759 return(-1);
1760 if (reader->node == NULL)
1761 return(-1);
1762 if (reader->node->type != XML_ELEMENT_NODE)
1763 return(0);
1764 if (reader->curnode != NULL) {
1765 reader->curnode = NULL;
1766 return(1);
1767 }
1768 return(0);
1769}
1770
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001771/**
1772 * xmlTextReaderReadAttributeValue:
1773 * @reader: the xmlTextReaderPtr used
1774 *
1775 * Parses an attribute value into one or more Text and EntityReference nodes.
1776 *
1777 * Returns 1 in case of success, 0 if the reader was not positionned on an
1778 * ttribute node or all the attribute values have been read, or -1
1779 * in case of error.
1780 */
1781int
1782xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
1783 if (reader == NULL)
1784 return(-1);
1785 if (reader->node == NULL)
1786 return(-1);
1787 if (reader->curnode == NULL)
1788 return(0);
1789 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
1790 if (reader->curnode->children == NULL)
1791 return(0);
1792 reader->curnode = reader->curnode->children;
1793 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
1794 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
1795
1796 if (reader->faketext == NULL) {
1797 reader->faketext = xmlNewDocText(reader->node->doc,
1798 ns->href);
1799 } else {
1800 if (reader->faketext->content != NULL)
1801 xmlFree(reader->faketext->content);
1802 reader->faketext->content = xmlStrdup(ns->href);
1803 }
1804 reader->curnode = reader->faketext;
1805 } else {
1806 if (reader->curnode->next == NULL)
1807 return(0);
1808 reader->curnode = reader->curnode->next;
1809 }
1810 return(1);
1811}
1812
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001813/************************************************************************
1814 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001815 * Acces API to the current node *
1816 * *
1817 ************************************************************************/
1818/**
1819 * xmlTextReaderAttributeCount:
1820 * @reader: the xmlTextReaderPtr used
1821 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001822 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001823 *
1824 * Returns 0 i no attributes, -1 in case of error or the attribute count
1825 */
1826int
1827xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
1828 int ret;
1829 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00001830 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001831 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001832
1833 if (reader == NULL)
1834 return(-1);
1835 if (reader->node == NULL)
1836 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001837
1838 if (reader->curnode != NULL)
1839 node = reader->curnode;
1840 else
1841 node = reader->node;
1842
1843 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001844 return(0);
1845 if ((reader->state == XML_TEXTREADER_END) ||
1846 (reader->state == XML_TEXTREADER_BACKTRACK))
1847 return(0);
1848 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001849 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001850 while (attr != NULL) {
1851 ret++;
1852 attr = attr->next;
1853 }
Daniel Veillard67df8092002-12-16 22:04:11 +00001854 ns = node->nsDef;
1855 while (ns != NULL) {
1856 ret++;
1857 ns = ns->next;
1858 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001859 return(ret);
1860}
1861
1862/**
1863 * xmlTextReaderNodeType:
1864 * @reader: the xmlTextReaderPtr used
1865 *
1866 * Get the node type of the current node
1867 * Reference:
1868 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
1869 *
1870 * Returns the xmlNodeType of the current node or -1 in case of error
1871 */
1872int
1873xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001874 xmlNodePtr node;
Daniel Veillardd6038e02003-07-30 16:37:18 +00001875
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001876 if (reader == NULL)
1877 return(-1);
1878 if (reader->node == NULL)
Daniel Veillardd6038e02003-07-30 16:37:18 +00001879 return(XML_READER_TYPE_NONE);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001880 if (reader->curnode != NULL)
1881 node = reader->curnode;
1882 else
1883 node = reader->node;
1884 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001885 case XML_ELEMENT_NODE:
1886 if ((reader->state == XML_TEXTREADER_END) ||
1887 (reader->state == XML_TEXTREADER_BACKTRACK))
Daniel Veillardd6038e02003-07-30 16:37:18 +00001888 return(XML_READER_TYPE_END_ELEMENT);
1889 return(XML_READER_TYPE_ELEMENT);
Daniel Veillardecaba492002-12-30 10:55:29 +00001890 case XML_NAMESPACE_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001891 case XML_ATTRIBUTE_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001892 return(XML_READER_TYPE_ATTRIBUTE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001893 case XML_TEXT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001894 if (xmlIsBlankNode(reader->node)) {
1895 if (xmlNodeGetSpacePreserve(reader->node))
1896 return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
1897 else
1898 return(XML_READER_TYPE_WHITESPACE);
1899 } else {
1900 return(XML_READER_TYPE_TEXT);
1901 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001902 case XML_CDATA_SECTION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001903 return(XML_READER_TYPE_CDATA);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001904 case XML_ENTITY_REF_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001905 return(XML_READER_TYPE_ENTITY_REFERENCE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001906 case XML_ENTITY_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001907 return(XML_READER_TYPE_ENTITY);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001908 case XML_PI_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001909 return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001910 case XML_COMMENT_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001911 return(XML_READER_TYPE_COMMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001912 case XML_DOCUMENT_NODE:
1913 case XML_HTML_DOCUMENT_NODE:
1914#ifdef LIBXML_DOCB_ENABLED
1915 case XML_DOCB_DOCUMENT_NODE:
1916#endif
Daniel Veillardd6038e02003-07-30 16:37:18 +00001917 return(XML_READER_TYPE_DOCUMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001918 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001919 return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001920 case XML_NOTATION_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001921 return(XML_READER_TYPE_NOTATION);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001922 case XML_DOCUMENT_TYPE_NODE:
1923 case XML_DTD_NODE:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001924 return(XML_READER_TYPE_DOCUMENT_TYPE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001925
1926 case XML_ELEMENT_DECL:
1927 case XML_ATTRIBUTE_DECL:
1928 case XML_ENTITY_DECL:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001929 case XML_XINCLUDE_START:
1930 case XML_XINCLUDE_END:
Daniel Veillardd6038e02003-07-30 16:37:18 +00001931 return(XML_READER_TYPE_NONE);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001932 }
1933 return(-1);
1934}
1935
1936/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001937 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001938 * @reader: the xmlTextReaderPtr used
1939 *
1940 * Check if the current node is empty
1941 *
1942 * Returns 1 if empty, 0 if not and -1 in case of error
1943 */
1944int
1945xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
1946 if ((reader == NULL) || (reader->node == NULL))
1947 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00001948 if (reader->node->type != XML_ELEMENT_NODE)
1949 return(0);
Daniel Veillarde3c036e2003-01-01 15:11:05 +00001950 if (reader->curnode != NULL)
1951 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001952 if (reader->node->children != NULL)
1953 return(0);
Daniel Veillarddab8ea92003-01-02 14:16:45 +00001954 if (reader->state == XML_TEXTREADER_END)
1955 return(0);
Daniel Veillard067bae52003-01-05 01:27:54 +00001956 return(reader->node->_private == (void *)xmlTextReaderIsEmpty);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001957}
1958
1959/**
1960 * xmlTextReaderLocalName:
1961 * @reader: the xmlTextReaderPtr used
1962 *
1963 * The local name of the node.
1964 *
1965 * Returns the local name or NULL if not available
1966 */
1967xmlChar *
1968xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001969 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001970 if ((reader == NULL) || (reader->node == NULL))
1971 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001972 if (reader->curnode != NULL)
1973 node = reader->curnode;
1974 else
1975 node = reader->node;
1976 if (node->type == XML_NAMESPACE_DECL) {
1977 xmlNsPtr ns = (xmlNsPtr) node;
1978 if (ns->prefix == NULL)
1979 return(xmlStrdup(BAD_CAST "xmlns"));
1980 else
1981 return(xmlStrdup(ns->prefix));
1982 }
1983 if ((node->type != XML_ELEMENT_NODE) &&
1984 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001985 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001986 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001987}
1988
1989/**
1990 * xmlTextReaderName:
1991 * @reader: the xmlTextReaderPtr used
1992 *
1993 * The qualified name of the node, equal to Prefix :LocalName.
1994 *
1995 * Returns the local name or NULL if not available
1996 */
1997xmlChar *
1998xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001999 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002000 xmlChar *ret;
2001
2002 if ((reader == NULL) || (reader->node == NULL))
2003 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002004 if (reader->curnode != NULL)
2005 node = reader->curnode;
2006 else
2007 node = reader->node;
2008 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002009 case XML_ELEMENT_NODE:
2010 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002011 if ((node->ns == NULL) ||
2012 (node->ns->prefix == NULL))
2013 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002014
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002015 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002016 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002017 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002018 return(ret);
2019 case XML_TEXT_NODE:
2020 return(xmlStrdup(BAD_CAST "#text"));
2021 case XML_CDATA_SECTION_NODE:
2022 return(xmlStrdup(BAD_CAST "#cdata-section"));
2023 case XML_ENTITY_NODE:
2024 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002025 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002026 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002027 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002028 case XML_COMMENT_NODE:
2029 return(xmlStrdup(BAD_CAST "#comment"));
2030 case XML_DOCUMENT_NODE:
2031 case XML_HTML_DOCUMENT_NODE:
2032#ifdef LIBXML_DOCB_ENABLED
2033 case XML_DOCB_DOCUMENT_NODE:
2034#endif
2035 return(xmlStrdup(BAD_CAST "#document"));
2036 case XML_DOCUMENT_FRAG_NODE:
2037 return(xmlStrdup(BAD_CAST "#document-fragment"));
2038 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002039 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002040 case XML_DOCUMENT_TYPE_NODE:
2041 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002042 return(xmlStrdup(node->name));
2043 case XML_NAMESPACE_DECL: {
2044 xmlNsPtr ns = (xmlNsPtr) node;
2045
2046 ret = xmlStrdup(BAD_CAST "xmlns");
2047 if (ns->prefix == NULL)
2048 return(ret);
2049 ret = xmlStrcat(ret, BAD_CAST ":");
2050 ret = xmlStrcat(ret, ns->prefix);
2051 return(ret);
2052 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002053
2054 case XML_ELEMENT_DECL:
2055 case XML_ATTRIBUTE_DECL:
2056 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002057 case XML_XINCLUDE_START:
2058 case XML_XINCLUDE_END:
2059 return(NULL);
2060 }
2061 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002062}
2063
2064/**
2065 * xmlTextReaderPrefix:
2066 * @reader: the xmlTextReaderPtr used
2067 *
2068 * A shorthand reference to the namespace associated with the node.
2069 *
2070 * Returns the prefix or NULL if not available
2071 */
2072xmlChar *
2073xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002074 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002075 if ((reader == NULL) || (reader->node == NULL))
2076 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002077 if (reader->curnode != NULL)
2078 node = reader->curnode;
2079 else
2080 node = reader->node;
2081 if (node->type == XML_NAMESPACE_DECL) {
2082 xmlNsPtr ns = (xmlNsPtr) node;
2083 if (ns->prefix == NULL)
2084 return(NULL);
2085 return(xmlStrdup(BAD_CAST "xmlns"));
2086 }
2087 if ((node->type != XML_ELEMENT_NODE) &&
2088 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002089 return(NULL);
Daniel Veillard952379b2003-03-17 15:37:12 +00002090 if ((node->ns != NULL) && (node->ns->prefix != NULL))
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002091 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002092 return(NULL);
2093}
2094
2095/**
2096 * xmlTextReaderNamespaceUri:
2097 * @reader: the xmlTextReaderPtr used
2098 *
2099 * The URI defining the namespace associated with the node.
2100 *
2101 * Returns the namespace URI or NULL if not available
2102 */
2103xmlChar *
2104xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002105 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002106 if ((reader == NULL) || (reader->node == NULL))
2107 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002108 if (reader->curnode != NULL)
2109 node = reader->curnode;
2110 else
2111 node = reader->node;
Daniel Veillardecaba492002-12-30 10:55:29 +00002112 if (node->type == XML_NAMESPACE_DECL)
2113 return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002114 if ((node->type != XML_ELEMENT_NODE) &&
2115 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002116 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002117 if (node->ns != NULL)
2118 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002119 return(NULL);
2120}
2121
2122/**
2123 * xmlTextReaderBaseUri:
2124 * @reader: the xmlTextReaderPtr used
2125 *
2126 * The base URI of the node.
2127 *
2128 * Returns the base URI or NULL if not available
2129 */
2130xmlChar *
2131xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
2132 if ((reader == NULL) || (reader->node == NULL))
2133 return(NULL);
2134 return(xmlNodeGetBase(NULL, reader->node));
2135}
2136
2137/**
2138 * xmlTextReaderDepth:
2139 * @reader: the xmlTextReaderPtr used
2140 *
2141 * The depth of the node in the tree.
2142 *
2143 * Returns the depth or -1 in case of error
2144 */
2145int
2146xmlTextReaderDepth(xmlTextReaderPtr reader) {
2147 if (reader == NULL)
2148 return(-1);
2149 if (reader->node == NULL)
2150 return(0);
2151
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002152 if (reader->curnode != NULL) {
2153 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
2154 (reader->curnode->type == XML_NAMESPACE_DECL))
2155 return(reader->depth + 1);
2156 return(reader->depth + 2);
2157 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002158 return(reader->depth);
2159}
2160
2161/**
2162 * xmlTextReaderHasAttributes:
2163 * @reader: the xmlTextReaderPtr used
2164 *
2165 * Whether the node has attributes.
2166 *
2167 * Returns 1 if true, 0 if false, and -1 in case or error
2168 */
2169int
2170xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002171 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002172 if (reader == NULL)
2173 return(-1);
2174 if (reader->node == NULL)
2175 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002176 if (reader->curnode != NULL)
2177 node = reader->curnode;
2178 else
2179 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002180
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002181 if ((node->type == XML_ELEMENT_NODE) &&
2182 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002183 return(1);
2184 /* TODO: handle the xmlDecl */
2185 return(0);
2186}
2187
2188/**
2189 * xmlTextReaderHasValue:
2190 * @reader: the xmlTextReaderPtr used
2191 *
2192 * Whether the node can have a text value.
2193 *
2194 * Returns 1 if true, 0 if false, and -1 in case or error
2195 */
2196int
2197xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002198 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002199 if (reader == NULL)
2200 return(-1);
2201 if (reader->node == NULL)
2202 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002203 if (reader->curnode != NULL)
2204 node = reader->curnode;
2205 else
2206 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002207
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002208 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002209 case XML_ATTRIBUTE_NODE:
2210 case XML_TEXT_NODE:
2211 case XML_CDATA_SECTION_NODE:
2212 case XML_PI_NODE:
2213 case XML_COMMENT_NODE:
Daniel Veillard9e077102003-04-10 13:36:54 +00002214 case XML_NAMESPACE_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002215 return(1);
2216 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002217 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002218 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002219 return(0);
2220}
2221
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002222/**
2223 * xmlTextReaderValue:
2224 * @reader: the xmlTextReaderPtr used
2225 *
2226 * Provides the text value of the node if present
2227 *
2228 * Returns the string or NULL if not available. The retsult must be deallocated
2229 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002230 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002231xmlChar *
2232xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002233 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002234 if (reader == NULL)
2235 return(NULL);
2236 if (reader->node == NULL)
2237 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002238 if (reader->curnode != NULL)
2239 node = reader->curnode;
2240 else
2241 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002242
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002243 switch (node->type) {
2244 case XML_NAMESPACE_DECL:
2245 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002246 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002247 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002248
2249 if (attr->parent != NULL)
2250 return (xmlNodeListGetString
2251 (attr->parent->doc, attr->children, 1));
2252 else
2253 return (xmlNodeListGetString(NULL, attr->children, 1));
2254 break;
2255 }
2256 case XML_TEXT_NODE:
2257 case XML_CDATA_SECTION_NODE:
2258 case XML_PI_NODE:
2259 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00002260 if (node->content != NULL)
2261 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002262 default:
Daniel Veillard2cfd9df2003-03-22 22:39:16 +00002263 break;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00002264 }
2265 return(NULL);
2266}
2267
2268/**
2269 * xmlTextReaderIsDefault:
2270 * @reader: the xmlTextReaderPtr used
2271 *
2272 * Whether an Attribute node was generated from the default value
2273 * defined in the DTD or schema.
2274 *
2275 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
2276 */
2277int
2278xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
2279 if (reader == NULL)
2280 return(-1);
2281 return(0);
2282}
2283
2284/**
2285 * xmlTextReaderQuoteChar:
2286 * @reader: the xmlTextReaderPtr used
2287 *
2288 * The quotation mark character used to enclose the value of an attribute.
2289 *
2290 * Returns " or ' and -1 in case of error
2291 */
2292int
2293xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
2294 if (reader == NULL)
2295 return(-1);
2296 /* TODO maybe lookup the attribute value for " first */
2297 return((int) '"');
2298}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002299
2300/**
2301 * xmlTextReaderXmlLang:
2302 * @reader: the xmlTextReaderPtr used
2303 *
2304 * The xml:lang scope within which the node resides.
2305 *
2306 * Returns the xml:lang value or NULL if none exists.
2307 */
2308xmlChar *
2309xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
2310 if (reader == NULL)
2311 return(NULL);
2312 if (reader->node == NULL)
2313 return(NULL);
2314 return(xmlNodeGetLang(reader->node));
2315}
2316
Daniel Veillard67df8092002-12-16 22:04:11 +00002317/**
2318 * xmlTextReaderNormalization:
2319 * @reader: the xmlTextReaderPtr used
2320 *
2321 * The value indicating whether to normalize white space and attribute values.
2322 * Since attribute value and end of line normalizations are a MUST in the XML
2323 * specification only the value true is accepted. The broken bahaviour of
2324 * accepting out of range character entities like &#0; is of course not
2325 * supported either.
2326 *
2327 * Returns 1 or -1 in case of error.
2328 */
2329int
2330xmlTextReaderNormalization(xmlTextReaderPtr reader) {
2331 if (reader == NULL)
2332 return(-1);
2333 return(1);
2334}
2335
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002336/************************************************************************
2337 * *
2338 * Extensions to the base APIs *
2339 * *
2340 ************************************************************************/
2341
2342/**
2343 * xmlTextReaderSetParserProp:
2344 * @reader: the xmlTextReaderPtr used
2345 * @prop: the xmlParserProperties to set
2346 * @value: usually 0 or 1 to (de)activate it
2347 *
2348 * Change the parser processing behaviour by changing some of its internal
2349 * properties. Note that some properties can only be changed before any
2350 * read has been done.
2351 *
2352 * Returns 0 if the call was successful, or -1 in case of error
2353 */
2354int
2355xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
2356 xmlParserProperties p = (xmlParserProperties) prop;
2357 xmlParserCtxtPtr ctxt;
2358
2359 if ((reader == NULL) || (reader->ctxt == NULL))
2360 return(-1);
2361 ctxt = reader->ctxt;
2362
2363 switch (p) {
2364 case XML_PARSER_LOADDTD:
2365 if (value != 0) {
2366 if (ctxt->loadsubset == 0) {
2367 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2368 return(-1);
2369 ctxt->loadsubset = XML_DETECT_IDS;
2370 }
2371 } else {
2372 ctxt->loadsubset = 0;
2373 }
2374 return(0);
2375 case XML_PARSER_DEFAULTATTRS:
2376 if (value != 0) {
2377 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
2378 } else {
2379 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
2380 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
2381 }
2382 return(0);
2383 case XML_PARSER_VALIDATE:
2384 if (value != 0) {
2385 ctxt->validate = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00002386 reader->validate = XML_TEXTREADER_VALIDATE_DTD;
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002387 } else {
2388 ctxt->validate = 0;
2389 }
2390 return(0);
Daniel Veillarde18fc182002-12-28 22:56:33 +00002391 case XML_PARSER_SUBST_ENTITIES:
2392 if (value != 0) {
2393 ctxt->replaceEntities = 1;
2394 } else {
2395 ctxt->replaceEntities = 0;
2396 }
2397 return(0);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002398 }
2399 return(-1);
2400}
2401
2402/**
2403 * xmlTextReaderGetParserProp:
2404 * @reader: the xmlTextReaderPtr used
2405 * @prop: the xmlParserProperties to get
2406 *
2407 * Read the parser internal property.
2408 *
2409 * Returns the value, usually 0 or 1, or -1 in case of error.
2410 */
2411int
2412xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
2413 xmlParserProperties p = (xmlParserProperties) prop;
2414 xmlParserCtxtPtr ctxt;
2415
2416 if ((reader == NULL) || (reader->ctxt == NULL))
2417 return(-1);
2418 ctxt = reader->ctxt;
2419
2420 switch (p) {
2421 case XML_PARSER_LOADDTD:
2422 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
2423 return(1);
2424 return(0);
2425 case XML_PARSER_DEFAULTATTRS:
2426 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
2427 return(1);
2428 return(0);
2429 case XML_PARSER_VALIDATE:
Daniel Veillardf4e55762003-04-15 23:32:22 +00002430 return(reader->validate);
Daniel Veillarde18fc182002-12-28 22:56:33 +00002431 case XML_PARSER_SUBST_ENTITIES:
2432 return(ctxt->replaceEntities);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002433 }
2434 return(-1);
2435}
2436
Daniel Veillarde18fc182002-12-28 22:56:33 +00002437/**
2438 * xmlTextReaderCurrentNode:
2439 * @reader: the xmlTextReaderPtr used
2440 *
2441 * Hacking interface allowing to get the xmlNodePtr correponding to the
2442 * current node being accessed by the xmlTextReader. This is dangerous
2443 * because the underlying node may be destroyed on the next Reads.
2444 *
2445 * Returns the xmlNodePtr or NULL in case of error.
2446 */
2447xmlNodePtr
2448xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
2449 if (reader == NULL)
2450 return(NULL);
2451
2452 if (reader->curnode != NULL)
2453 return(reader->curnode);
2454 return(reader->node);
2455}
2456
2457/**
2458 * xmlTextReaderCurrentDoc:
2459 * @reader: the xmlTextReaderPtr used
2460 *
2461 * Hacking interface allowing to get the xmlDocPtr correponding to the
2462 * current document being accessed by the xmlTextReader. This is dangerous
2463 * because the associated node may be destroyed on the next Reads.
2464 *
2465 * Returns the xmlDocPtr or NULL in case of error.
2466 */
2467xmlDocPtr
2468xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
2469 if ((reader == NULL) || (reader->ctxt == NULL))
2470 return(NULL);
2471
2472 return(reader->ctxt->myDoc);
2473}
2474
Daniel Veillard37fc84d2003-05-09 19:38:15 +00002475#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00002476/**
Daniel Veillard33300b42003-04-17 09:09:19 +00002477 * xmlTextReaderRelaxNGSetSchema:
2478 * @reader: the xmlTextReaderPtr used
2479 * @schema: a precompiled RelaxNG schema
2480 *
2481 * Use RelaxNG to validate the document as it is processed.
2482 * Activation is only possible before the first Read().
2483 * if @schema is NULL, then RelaxNG validation is desactivated.
2484 @ The @schema should not be freed until the reader is deallocated
2485 * or its use has been deactivated.
2486 *
2487 * Returns 0 in case the RelaxNG validation could be (des)activated and
2488 * -1 in case of error.
2489 */
2490int
2491xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
2492 if (schema == NULL) {
2493 if (reader->rngSchemas != NULL) {
2494 xmlRelaxNGFree(reader->rngSchemas);
2495 reader->rngSchemas = NULL;
2496 }
2497 if (reader->rngValidCtxt != NULL) {
2498 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2499 reader->rngValidCtxt = NULL;
2500 }
2501 return(0);
2502 }
2503 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2504 return(-1);
2505 if (reader->rngSchemas != NULL) {
2506 xmlRelaxNGFree(reader->rngSchemas);
2507 reader->rngSchemas = NULL;
2508 }
2509 if (reader->rngValidCtxt != NULL) {
2510 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2511 reader->rngValidCtxt = NULL;
2512 }
2513 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
2514 if (reader->rngValidCtxt == NULL)
2515 return(-1);
2516 if (reader->errorFunc != NULL) {
2517 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
2518 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
2519 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
2520 reader->errorFuncArg);
2521 }
2522 reader->rngValidErrors = 0;
2523 reader->rngFullNode = NULL;
2524 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
2525 return(0);
2526}
2527
2528/**
Daniel Veillardf4e55762003-04-15 23:32:22 +00002529 * xmlTextReaderRelaxNGValidate:
2530 * @reader: the xmlTextReaderPtr used
2531 * @rng: the path to a RelaxNG schema or NULL
2532 *
2533 * Use RelaxNG to validate the document as it is processed.
2534 * Activation is only possible before the first Read().
2535 * if @rng is NULL, then RelaxNG validation is desactivated.
2536 *
2537 * Returns 0 in case the RelaxNG validation could be (des)activated and
2538 * -1 in case of error.
2539 */
2540int
2541xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
2542 xmlRelaxNGParserCtxtPtr ctxt;
2543
2544 if (reader == NULL)
2545 return(-1);
2546
2547 if (rng == NULL) {
2548 if (reader->rngSchemas != NULL) {
2549 xmlRelaxNGFree(reader->rngSchemas);
2550 reader->rngSchemas = NULL;
2551 }
2552 if (reader->rngValidCtxt != NULL) {
2553 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2554 reader->rngValidCtxt = NULL;
2555 }
2556 return(0);
2557 }
2558 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
2559 return(-1);
Daniel Veillard33300b42003-04-17 09:09:19 +00002560 if (reader->rngSchemas != NULL) {
2561 xmlRelaxNGFree(reader->rngSchemas);
2562 reader->rngSchemas = NULL;
2563 }
2564 if (reader->rngValidCtxt != NULL) {
2565 xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2566 reader->rngValidCtxt = NULL;
2567 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00002568 ctxt = xmlRelaxNGNewParserCtxt(rng);
2569 if (reader->errorFunc != NULL) {
2570 xmlRelaxNGSetParserErrors(ctxt,
2571 (xmlRelaxNGValidityErrorFunc) reader->errorFunc,
2572 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
2573 reader->errorFuncArg);
2574 }
2575 reader->rngSchemas = xmlRelaxNGParse(ctxt);
2576 xmlRelaxNGFreeParserCtxt(ctxt);
2577 if (reader->rngSchemas == NULL)
2578 return(-1);
2579 reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
2580 if (reader->rngValidCtxt == NULL)
2581 return(-1);
2582 if (reader->errorFunc != NULL) {
2583 xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
2584 (xmlRelaxNGValidityErrorFunc)reader->errorFunc,
2585 (xmlRelaxNGValidityWarningFunc) reader->errorFunc,
2586 reader->errorFuncArg);
2587 }
2588 reader->rngValidErrors = 0;
2589 reader->rngFullNode = NULL;
2590 reader->validate = XML_TEXTREADER_VALIDATE_RNG;
2591 return(0);
2592}
Daniel Veillard37fc84d2003-05-09 19:38:15 +00002593#endif
Daniel Veillardf4e55762003-04-15 23:32:22 +00002594
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002595/************************************************************************
2596 * *
Daniel Veillard26f70262003-01-16 22:45:08 +00002597 * Error Handling Extensions *
2598 * *
2599 ************************************************************************/
2600
2601/* helper to build a xmlMalloc'ed string from a format and va_list */
2602static char *
2603xmlTextReaderBuildMessage(const char *msg, va_list ap) {
2604 int size;
2605 int chars;
2606 char *larger;
2607 char *str;
2608
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002609 str = (char *) xmlMallocAtomic(150);
Daniel Veillard26f70262003-01-16 22:45:08 +00002610 if (str == NULL) {
2611 xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
2612 return NULL;
2613 }
2614
2615 size = 150;
2616
2617 while (1) {
2618 chars = vsnprintf(str, size, msg, ap);
2619 if ((chars > -1) && (chars < size))
2620 break;
2621 if (chars > -1)
2622 size += chars + 1;
2623 else
2624 size += 100;
2625 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
2626 xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
2627 xmlFree(str);
2628 return NULL;
2629 }
2630 str = larger;
2631 }
2632
2633 return str;
2634}
2635
Daniel Veillard417be3a2003-01-20 21:26:34 +00002636/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00002637 * xmlTextReaderLocatorLineNumber:
Daniel Veillard417be3a2003-01-20 21:26:34 +00002638 * @locator: the xmlTextReaderLocatorPtr used
2639 *
2640 * Obtain the line number for the given locator.
2641 *
2642 * Returns the line number or -1 in case of error.
2643 */
2644int
2645xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
2646 /* we know that locator is a xmlParserCtxtPtr */
2647 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
2648 int ret = -1;
2649
2650 if (ctx->node != NULL) {
2651 ret = xmlGetLineNo(ctx->node);
2652 }
2653 else {
2654 /* inspired from error.c */
2655 xmlParserInputPtr input;
2656 input = ctx->input;
2657 if ((input->filename == NULL) && (ctx->inputNr > 1))
2658 input = ctx->inputTab[ctx->inputNr - 2];
2659 if (input != NULL) {
2660 ret = input->line;
2661 }
2662 else {
2663 ret = -1;
2664 }
2665 }
2666
2667 return ret;
2668}
2669
2670/**
Daniel Veillard540a31a2003-01-21 11:21:07 +00002671 * xmlTextReaderLocatorBaseURI:
Daniel Veillard417be3a2003-01-20 21:26:34 +00002672 * @locator: the xmlTextReaderLocatorPtr used
2673 *
2674 * Obtain the base URI for the given locator.
2675 *
2676 * Returns the base URI or NULL in case of error.
2677 */
2678xmlChar *
2679xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
2680 /* we know that locator is a xmlParserCtxtPtr */
2681 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
2682 xmlChar *ret = NULL;
2683
2684 if (ctx->node != NULL) {
2685 ret = xmlNodeGetBase(NULL,ctx->node);
2686 }
2687 else {
2688 /* inspired from error.c */
2689 xmlParserInputPtr input;
2690 input = ctx->input;
2691 if ((input->filename == NULL) && (ctx->inputNr > 1))
2692 input = ctx->inputTab[ctx->inputNr - 2];
2693 if (input != NULL) {
Daniel Veillard580ced82003-03-21 21:22:48 +00002694 ret = xmlStrdup(BAD_CAST input->filename);
Daniel Veillard417be3a2003-01-20 21:26:34 +00002695 }
2696 else {
2697 ret = NULL;
2698 }
2699 }
2700
2701 return ret;
2702}
2703
Daniel Veillard26f70262003-01-16 22:45:08 +00002704static void
2705xmlTextReaderGenericError(void *ctxt, int severity, char *str) {
2706 xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt;
2707 xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private;
2708
2709 if (str != NULL) {
2710 reader->errorFunc(reader->errorFuncArg,
2711 str,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002712 severity,
2713 (xmlTextReaderLocatorPtr)ctx);
Daniel Veillard26f70262003-01-16 22:45:08 +00002714 xmlFree(str);
2715 }
2716}
2717
2718static void
2719xmlTextReaderError(void *ctxt, const char *msg, ...) {
2720 va_list ap;
2721
2722 va_start(ap,msg);
2723 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002724 XML_PARSER_SEVERITY_ERROR,
Daniel Veillard26f70262003-01-16 22:45:08 +00002725 xmlTextReaderBuildMessage(msg,ap));
2726 va_end(ap);
2727
2728}
2729
2730static void
2731xmlTextReaderWarning(void *ctxt, const char *msg, ...) {
2732 va_list ap;
2733
2734 va_start(ap,msg);
2735 xmlTextReaderGenericError(ctxt,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002736 XML_PARSER_SEVERITY_WARNING,
Daniel Veillard26f70262003-01-16 22:45:08 +00002737 xmlTextReaderBuildMessage(msg,ap));
2738 va_end(ap);
2739}
2740
2741static void
2742xmlTextReaderValidityError(void *ctxt, const char *msg, ...) {
2743 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00002744 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00002745
Daniel Veillard417be3a2003-01-20 21:26:34 +00002746 if ((len > 1) && (msg[len - 2] != ':')) {
2747 /*
2748 * some callbacks only report locator information:
2749 * skip them (mimicking behaviour in error.c)
2750 */
2751 va_start(ap,msg);
2752 xmlTextReaderGenericError(ctxt,
2753 XML_PARSER_SEVERITY_VALIDITY_ERROR,
2754 xmlTextReaderBuildMessage(msg,ap));
2755 va_end(ap);
2756 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002757}
2758
2759static void
2760xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) {
2761 va_list ap;
Daniel Veillard417be3a2003-01-20 21:26:34 +00002762 int len = xmlStrlen((const xmlChar *) msg);
Daniel Veillard26f70262003-01-16 22:45:08 +00002763
Daniel Veillard417be3a2003-01-20 21:26:34 +00002764 if ((len != 0) && (msg[len - 1] != ':')) {
2765 /*
2766 * some callbacks only report locator information:
2767 * skip them (mimicking behaviour in error.c)
2768 */
2769 va_start(ap,msg);
2770 xmlTextReaderGenericError(ctxt,
2771 XML_PARSER_SEVERITY_VALIDITY_WARNING,
2772 xmlTextReaderBuildMessage(msg,ap));
2773 va_end(ap);
2774 }
Daniel Veillard26f70262003-01-16 22:45:08 +00002775}
2776
2777/**
2778 * xmlTextReaderSetErrorHandler:
2779 * @reader: the xmlTextReaderPtr used
2780 * @f: the callback function to call on error and warnings
2781 * @arg: a user argument to pass to the callback function
2782 *
Daniel Veillard417be3a2003-01-20 21:26:34 +00002783 * Register a callback function that will be called on error and warnings.
2784 *
Daniel Veillard26f70262003-01-16 22:45:08 +00002785 * If @f is NULL, the default error and warning handlers are restored.
2786 */
2787void
2788xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
2789 xmlTextReaderErrorFunc f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002790 void *arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002791 if (f != NULL) {
2792 reader->ctxt->sax->error = xmlTextReaderError;
2793 reader->ctxt->vctxt.error = xmlTextReaderValidityError;
2794 reader->ctxt->sax->warning = xmlTextReaderWarning;
2795 reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
2796 reader->errorFunc = f;
2797 reader->errorFuncArg = arg;
2798 }
2799 else {
2800 /* restore defaults */
2801 reader->ctxt->sax->error = xmlParserError;
2802 reader->ctxt->vctxt.error = xmlParserValidityError;
2803 reader->ctxt->sax->warning = xmlParserWarning;
2804 reader->ctxt->vctxt.warning = xmlParserValidityWarning;
2805 reader->errorFunc = NULL;
2806 reader->errorFuncArg = NULL;
2807 }
2808}
2809
Daniel Veillard417be3a2003-01-20 21:26:34 +00002810/**
Daniel Veillardf6bad792003-04-11 19:38:54 +00002811 * xmlTextReaderIsValid:
2812 * @reader: the xmlTextReaderPtr used
2813 *
2814 * Retrieve the validity status from the parser context
2815 *
2816 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
2817 */
2818int
2819xmlTextReaderIsValid(xmlTextReaderPtr reader) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00002820 if (reader == NULL) return(-1);
2821#ifdef LIBXML_SCHEMAS_ENABLED
2822 if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
2823 return(reader->rngValidErrors == 0);
2824#endif
2825 if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
2826 (reader->ctxt != NULL))
2827 return(reader->ctxt->valid);
2828 return(0);
Daniel Veillardf6bad792003-04-11 19:38:54 +00002829}
2830
2831/**
Daniel Veillard417be3a2003-01-20 21:26:34 +00002832 * xmlTextReaderGetErrorHandler:
2833 * @reader: the xmlTextReaderPtr used
2834 * @f: the callback function or NULL is no callback has been registered
2835 * @arg: a user argument
2836 *
2837 * Retrieve the error callback function and user argument.
2838 */
Daniel Veillard26f70262003-01-16 22:45:08 +00002839void
2840xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
2841 xmlTextReaderErrorFunc *f,
Daniel Veillard417be3a2003-01-20 21:26:34 +00002842 void **arg) {
Daniel Veillard26f70262003-01-16 22:45:08 +00002843 *f = reader->errorFunc;
2844 *arg = reader->errorFuncArg;
2845}
2846
2847/************************************************************************
2848 * *
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002849 * Utilities *
2850 * *
2851 ************************************************************************/
2852/**
2853 * xmlBase64Decode:
2854 * @in: the input buffer
2855 * @inlen: the size of the input (in), the size read from it (out)
2856 * @to: the output buffer
2857 * @tolen: the size of the output (in), the size written to (out)
2858 *
2859 * Base64 decoder, reads from @in and save in @to
Daniel Veillardd4310742003-02-18 21:12:46 +00002860 * TODO: tell jody when this is actually exported
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002861 *
2862 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
2863 * 2 if there wasn't enough space on the output or -1 in case of error.
2864 */
2865static int
2866xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
2867 unsigned char *to, unsigned long *tolen) {
2868 unsigned long incur; /* current index in in[] */
2869 unsigned long inblk; /* last block index in in[] */
2870 unsigned long outcur; /* current index in out[] */
2871 unsigned long inmax; /* size of in[] */
2872 unsigned long outmax; /* size of out[] */
2873 unsigned char cur; /* the current value read from in[] */
Daniel Veillardc127adc2003-07-23 15:07:08 +00002874 unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00002875 int nbintmp; /* number of byte in intmp[] */
2876 int is_ignore; /* cur should be ignored */
2877 int is_end = 0; /* the end of the base64 was found */
2878 int retval = 1;
2879 int i;
2880
2881 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
2882 return(-1);
2883
2884 incur = 0;
2885 inblk = 0;
2886 outcur = 0;
2887 inmax = *inlen;
2888 outmax = *tolen;
2889 nbintmp = 0;
2890
2891 while (1) {
2892 if (incur >= inmax)
2893 break;
2894 cur = in[incur++];
2895 is_ignore = 0;
2896 if ((cur >= 'A') && (cur <= 'Z'))
2897 cur = cur - 'A';
2898 else if ((cur >= 'a') && (cur <= 'z'))
2899 cur = cur - 'a' + 26;
2900 else if ((cur >= '0') && (cur <= '9'))
2901 cur = cur - '0' + 52;
2902 else if (cur == '+')
2903 cur = 62;
2904 else if (cur == '/')
2905 cur = 63;
2906 else if (cur == '.')
2907 cur = 0;
2908 else if (cur == '=') /*no op , end of the base64 stream */
2909 is_end = 1;
2910 else {
2911 is_ignore = 1;
2912 if (nbintmp == 0)
2913 inblk = incur;
2914 }
2915
2916 if (!is_ignore) {
2917 int nbouttmp = 3;
2918 int is_break = 0;
2919
2920 if (is_end) {
2921 if (nbintmp == 0)
2922 break;
2923 if ((nbintmp == 1) || (nbintmp == 2))
2924 nbouttmp = 1;
2925 else
2926 nbouttmp = 2;
2927 nbintmp = 3;
2928 is_break = 1;
2929 }
2930 intmp[nbintmp++] = cur;
2931 /*
2932 * if intmp is full, push the 4byte sequence as a 3 byte
2933 * sequence out
2934 */
2935 if (nbintmp == 4) {
2936 nbintmp = 0;
2937 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
2938 outtmp[1] =
2939 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
2940 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
2941 if (outcur + 3 >= outmax) {
2942 retval = 2;
2943 break;
2944 }
2945
2946 for (i = 0; i < nbouttmp; i++)
2947 to[outcur++] = outtmp[i];
2948 inblk = incur;
2949 }
2950
2951 if (is_break) {
2952 retval = 0;
2953 break;
2954 }
2955 }
2956 }
2957
2958 *tolen = outcur;
2959 *inlen = inblk;
2960 return (retval);
2961}
2962
2963/*
2964 * Test routine for the xmlBase64Decode function
2965 */
2966#if 0
2967int main(int argc, char **argv) {
2968 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
2969 char output[100];
2970 char output2[100];
2971 char output3[100];
2972 unsigned long inlen = strlen(input);
2973 unsigned long outlen = 100;
2974 int ret;
2975 unsigned long cons, tmp, tmp2, prod;
2976
2977 /*
2978 * Direct
2979 */
2980 ret = xmlBase64Decode(input, &inlen, output, &outlen);
2981
2982 output[outlen] = 0;
2983 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
2984
2985 /*
2986 * output chunking
2987 */
2988 cons = 0;
2989 prod = 0;
2990 while (cons < inlen) {
2991 tmp = 5;
2992 tmp2 = inlen - cons;
2993
2994 printf("%ld %ld\n", cons, prod);
2995 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
2996 cons += tmp2;
2997 prod += tmp;
2998 printf("%ld %ld\n", cons, prod);
2999 }
3000 output2[outlen] = 0;
3001 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
3002
3003 /*
3004 * input chunking
3005 */
3006 cons = 0;
3007 prod = 0;
3008 while (cons < inlen) {
3009 tmp = 100 - prod;
3010 tmp2 = inlen - cons;
3011 if (tmp2 > 5)
3012 tmp2 = 5;
3013
3014 printf("%ld %ld\n", cons, prod);
3015 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
3016 cons += tmp2;
3017 prod += tmp;
3018 printf("%ld %ld\n", cons, prod);
3019 }
3020 output3[outlen] = 0;
3021 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
3022 return(0);
3023
3024}
3025#endif