blob: e36bbf35efbd70aedaa1cf9e4a4ad8c8a14f2cdc [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
13#define IN_LIBXML
14#include "libxml.h"
15
16#include <string.h> /* for memset() only ! */
17
18#ifdef HAVE_CTYPE_H
19#include <ctype.h>
20#endif
21#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
24
25#include <libxml/xmlmemory.h>
26#include <libxml/xmlIO.h>
27#include <libxml/xmlreader.h>
28
29/* #define DEBUG_CALLBACKS */
30/* #define DEBUG_READER */
31
32/**
33 * TODO:
34 *
35 * macro to flag unimplemented blocks
36 */
37#define TODO \
38 xmlGenericError(xmlGenericErrorContext, \
39 "Unimplemented block at %s:%d\n", \
40 __FILE__, __LINE__);
41
42#ifdef DEBUG_READER
43#define DUMP_READER xmlTextReaderDebug(reader);
44#else
45#define DUMP_READER
46#endif
47
48/************************************************************************
49 * *
50 * The parser: maps the Text Reader API on top of the existing *
51 * parsing routines building a tree *
52 * *
53 ************************************************************************/
54
55#define XML_TEXTREADER_INPUT 1
56#define XML_TEXTREADER_CTXT 2
57
58typedef enum {
Daniel Veillard67df8092002-12-16 22:04:11 +000059 XML_TEXTREADER_MODE_INITIAL = 0,
60 XML_TEXTREADER_MODE_INTERACTIVE = 1,
61 XML_TEXTREADER_MODE_ERROR = 2,
62 XML_TEXTREADER_MODE_EOF =3,
63 XML_TEXTREADER_MODE_CLOSED = 4,
64 XML_TEXTREADER_MODE_READING = 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000065} xmlTextReaderMode;
66
67typedef enum {
68 XML_TEXTREADER_NONE = -1,
69 XML_TEXTREADER_START= 0,
70 XML_TEXTREADER_ELEMENT= 1,
71 XML_TEXTREADER_END= 2,
72 XML_TEXTREADER_EMPTY= 3,
Daniel Veillardea7751d2002-12-20 00:16:24 +000073 XML_TEXTREADER_BACKTRACK= 4,
74 XML_TEXTREADER_DONE= 5
Daniel Veillarde1ca5032002-12-09 14:13:43 +000075} xmlTextReaderState;
76
77struct _xmlTextReader {
78 int mode; /* the parsing mode */
79 int allocs; /* what structure were deallocated */
80 xmlTextReaderState state;
81 xmlParserCtxtPtr ctxt; /* the parser context */
82 xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
83 xmlParserInputBufferPtr input; /* the input */
84 startElementSAXFunc startElement;/* initial SAX callbacks */
85 endElementSAXFunc endElement; /* idem */
Daniel Veillardea7751d2002-12-20 00:16:24 +000086 charactersSAXFunc characters;
87 cdataBlockSAXFunc cdataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +000088 unsigned int base; /* base of the segment in the input */
89 unsigned int cur; /* current position in the input */
90 xmlNodePtr node; /* current node */
Daniel Veillardda46d2d2002-12-15 23:36:49 +000091 xmlNodePtr curnode;/* current attribute node */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000092 int depth; /* depth of the current node */
Daniel Veillardbeb70bd2002-12-18 14:53:54 +000093 xmlNodePtr faketext;/* fake xmlNs chld */
Daniel Veillarddf512f42002-12-23 15:56:21 +000094 int wasempty;/* was the last node empty */
Daniel Veillarde1ca5032002-12-09 14:13:43 +000095};
96
97#ifdef DEBUG_READER
98static void
99xmlTextReaderDebug(xmlTextReaderPtr reader) {
100 if ((reader == NULL) || (reader->ctxt == NULL)) {
101 fprintf(stderr, "xmlTextReader NULL\n");
102 return;
103 }
104 fprintf(stderr, "xmlTextReader: state %d depth %d ",
105 reader->state, reader->depth);
106 if (reader->node == NULL) {
107 fprintf(stderr, "node = NULL\n");
108 } else {
109 fprintf(stderr, "node %s\n", reader->node->name);
110 }
111 fprintf(stderr, " input: base %d, cur %d, depth %d: ",
112 reader->base, reader->cur, reader->ctxt->nodeNr);
113 if (reader->input->buffer == NULL) {
114 fprintf(stderr, "buffer is NULL\n");
115 } else {
116#ifdef LIBXML_DEBUG_ENABLED
117 xmlDebugDumpString(stderr,
118 &reader->input->buffer->content[reader->cur]);
119#endif
120 fprintf(stderr, "\n");
121 }
122}
123#endif
124
125/**
126 * xmlTextReaderStartElement:
127 * @ctx: the user data (XML parser context)
128 * @fullname: The element name, including namespace prefix
129 * @atts: An array of name/value attributes pairs, NULL terminated
130 *
131 * called when an opening tag has been processed.
132 */
133static void
134xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
135 const xmlChar **atts) {
136 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
137 xmlTextReaderPtr reader = ctxt->_private;
138
139#ifdef DEBUG_CALLBACKS
140 printf("xmlTextReaderStartElement(%s)\n", fullname);
141#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000142 if ((reader != NULL) && (reader->startElement != NULL)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000143 reader->startElement(ctx, fullname, atts);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000144 if (ctxt->validate) {
145 ctxt->valid &= xmlValidatePushElement(&ctxt->vctxt, ctxt->myDoc,
146 ctxt->node, fullname);
147 }
148 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000149 reader->state = XML_TEXTREADER_ELEMENT;
150}
151
152/**
153 * xmlTextReaderEndElement:
154 * @ctx: the user data (XML parser context)
155 * @fullname: The element name, including namespace prefix
156 *
157 * called when an ending tag has been processed.
158 */
159static void
160xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
161 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
162 xmlTextReaderPtr reader = ctxt->_private;
163
164#ifdef DEBUG_CALLBACKS
165 printf("xmlTextReaderEndElement(%s)\n", fullname);
166#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000167 if ((reader != NULL) && (reader->endElement != NULL)) {
168 xmlNodePtr node = ctxt->node;
169
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000170 reader->endElement(ctx, fullname);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000171
172 if (ctxt->validate) {
173 ctxt->valid &= xmlValidatePopElement(&ctxt->vctxt, ctxt->myDoc,
174 node, fullname);
175 }
176 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000177 if (reader->state == XML_TEXTREADER_ELEMENT)
Daniel Veillarddf512f42002-12-23 15:56:21 +0000178 reader->wasempty = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000179 else
Daniel Veillarddf512f42002-12-23 15:56:21 +0000180 reader->wasempty = 0;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000181}
182
183/**
Daniel Veillardea7751d2002-12-20 00:16:24 +0000184 * xmlTextReaderCharacters:
185 * @ctx: the user data (XML parser context)
186 * @ch: a xmlChar string
187 * @len: the number of xmlChar
188 *
189 * receiving some chars from the parser.
190 */
191static void
192xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
193{
194 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
195 xmlTextReaderPtr reader = ctxt->_private;
196
197#ifdef DEBUG_CALLBACKS
198 printf("xmlTextReaderCharacters()\n");
199#endif
200 if ((reader != NULL) && (reader->characters != NULL)) {
201 reader->characters(ctx, ch, len);
202
203 if (ctxt->validate) {
204 ctxt->valid &= xmlValidatePushCData(&ctxt->vctxt, ch, len);
205 }
206 }
207}
208
209/**
210 * xmlTextReaderCDataBlock:
211 * @ctx: the user data (XML parser context)
212 * @value: The pcdata content
213 * @len: the block length
214 *
215 * called when a pcdata block has been parsed
216 */
217static void
218xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
219{
220 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
221 xmlTextReaderPtr reader = ctxt->_private;
222
223#ifdef DEBUG_CALLBACKS
224 printf("xmlTextReaderCDataBlock()\n");
225#endif
226 if ((reader != NULL) && (reader->cdataBlock != NULL)) {
227 reader->cdataBlock(ctx, ch, len);
228
229 if (ctxt->validate) {
230 ctxt->valid &= xmlValidatePushCData(&ctxt->vctxt, ch, len);
231 }
232 }
233}
234
235/**
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000236 * xmlTextReaderPushData:
237 * @reader: the xmlTextReaderPtr used
238 *
239 * Push data down the progressive parser until a significant callback
240 * got raised.
241 *
242 * Returns -1 in case of failure, 0 otherwise
243 */
244static int
245xmlTextReaderPushData(xmlTextReaderPtr reader) {
246 unsigned int cur = reader->cur;
247 xmlBufferPtr inbuf;
248 int val;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000249 int oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000250
251 if ((reader->input == NULL) || (reader->input->buffer == NULL))
252 return(-1);
253
Daniel Veillardea7751d2002-12-20 00:16:24 +0000254 oldstate = reader->state;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000255 reader->state = XML_TEXTREADER_NONE;
256 inbuf = reader->input->buffer;
257 while (reader->state == XML_TEXTREADER_NONE) {
258 if (cur >= inbuf->use) {
259 /*
260 * Refill the buffer unless we are at the end of the stream
261 */
262 if (reader->mode != XML_TEXTREADER_MODE_EOF) {
263 val = xmlParserInputBufferRead(reader->input, 4096);
264 if (val <= 0) {
265 reader->mode = XML_TEXTREADER_MODE_EOF;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000266 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000267 return(val);
268 }
269 } else
270 break;
271 }
272 if ((inbuf->content[cur] == '>') || (inbuf->content[cur] == '&')) {
273 cur = cur + 1;
274 val = xmlParseChunk(reader->ctxt,
275 (const char *) &inbuf->content[reader->cur],
276 cur - reader->cur, 0);
277 if (val != 0)
278 return(-1);
279 reader->cur = cur;
280 break;
281 } else {
282 cur = cur + 1;
283
284 /*
285 * One may have to force a flush at some point when parsing really
286 * large CDATA sections
287 */
288 if ((cur - reader->cur > 4096) && (reader->base == 0) &&
Daniel Veillard67df8092002-12-16 22:04:11 +0000289 (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000290 cur = cur + 1;
291 val = xmlParseChunk(reader->ctxt,
292 (const char *) &inbuf->content[reader->cur],
293 cur - reader->cur, 0);
294 if (val != 0)
295 return(-1);
296 reader->cur = cur;
297 }
298 }
299 }
300 /*
301 * Discard the consumed input when needed and possible
302 */
Daniel Veillard67df8092002-12-16 22:04:11 +0000303 if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000304 if ((reader->cur >= 4096) && (reader->base == 0)) {
305 val = xmlBufferShrink(inbuf, cur);
306 if (val >= 0) {
307 reader->cur -= val;
308 }
309 }
310 }
311
312 /*
313 * At the end of the stream signal that the work is done to the Push
314 * parser.
315 */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000316 if (reader->mode == XML_TEXTREADER_MODE_EOF) {
317 if (reader->mode != XML_TEXTREADER_DONE) {
318 val = xmlParseChunk(reader->ctxt,
319 (const char *) &inbuf->content[reader->cur], 0, 1);
320 reader->mode = XML_TEXTREADER_DONE;
321 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000322 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000323 reader->state = oldstate;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000324 return(0);
325}
326
327/**
328 * xmlTextReaderRead:
329 * @reader: the xmlTextReaderPtr used
330 *
331 * Moves the position of the current instance to the next node in
332 * the stream, exposing its properties.
333 *
334 * Returns 1 if the node was read successfully, 0 if there is no more
335 * nodes to read, or -1 in case of error
336 */
337int
338xmlTextReaderRead(xmlTextReaderPtr reader) {
Daniel Veillarddf512f42002-12-23 15:56:21 +0000339 int val, olddepth, wasempty;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000340 xmlTextReaderState oldstate;
341 xmlNodePtr oldnode;
342
343 if ((reader == NULL) || (reader->ctxt == NULL))
344 return(-1);
345 if (reader->ctxt->wellFormed != 1)
346 return(-1);
347
348#ifdef DEBUG_READER
349 fprintf(stderr, "\nREAD ");
350 DUMP_READER
351#endif
Daniel Veillard67df8092002-12-16 22:04:11 +0000352 if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
353 reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000354 /*
355 * Initial state
356 */
357 do {
358 val = xmlTextReaderPushData(reader);
359 if (val < 0)
360 return(-1);
361 } while ((reader->ctxt->node == NULL) &&
362 (reader->mode != XML_TEXTREADER_MODE_EOF));
363 if (reader->ctxt->node == NULL) {
364 if (reader->ctxt->myDoc != NULL)
365 reader->node = reader->ctxt->myDoc->children;
366 if (reader->node == NULL)
367 return(-1);
368 } else {
369 reader->node = reader->ctxt->node;
370 }
371 reader->depth = 1;
372 return(1);
373 }
374 oldstate = reader->state;
375 olddepth = reader->ctxt->nodeNr;
376 oldnode = reader->node;
Daniel Veillarddf512f42002-12-23 15:56:21 +0000377 wasempty = ((reader->wasempty == 1) && (reader->ctxt->node != NULL) &&
378 (reader->ctxt->node->last == reader->node));
379
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000380 /*
381 * If we are not backtracking on ancestors or examined nodes,
382 * that the parser didn't finished or that we arent at the end
383 * of stream, continue processing.
384 */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000385 while (((oldstate == XML_TEXTREADER_BACKTRACK) ||
386 (reader->node->children == NULL) ||
387 (reader->node->type == XML_ENTITY_REF_NODE) ||
388 (reader->node->type == XML_DTD_NODE)) &&
389 (reader->node->next == NULL) &&
390 (reader->ctxt->nodeNr == olddepth) &&
391 (reader->ctxt->instate != XML_PARSER_EOF)) {
392 val = xmlTextReaderPushData(reader);
393 if (val < 0)
394 return(-1);
395 if (reader->node == NULL)
396 return(0);
397 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000398 if (oldstate != XML_TEXTREADER_BACKTRACK) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000399 if ((reader->node->children != NULL) &&
400 (reader->node->type != XML_ENTITY_REF_NODE) &&
401 (reader->node->type != XML_DTD_NODE)) {
402 reader->node = reader->node->children;
403 reader->depth++;
Daniel Veillarddf512f42002-12-23 15:56:21 +0000404 reader->state = XML_TEXTREADER_ELEMENT;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000405 DUMP_READER
406 return(1);
407 }
408 }
409 if (reader->node->next != NULL) {
410 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
Daniel Veillarddf512f42002-12-23 15:56:21 +0000411 (reader->node->type == XML_ELEMENT_NODE) &&
412 (wasempty == 0)) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000413 reader->state = XML_TEXTREADER_END;
414 DUMP_READER
415 return(1);
416 }
417 reader->node = reader->node->next;
418 reader->state = XML_TEXTREADER_ELEMENT;
419 DUMP_READER
420 /*
421 * Cleanup of the old node
422 */
423 if (oldnode->type != XML_DTD_NODE) {
424 xmlUnlinkNode(oldnode);
425 xmlFreeNode(oldnode);
426 }
427
428 return(1);
429 }
Daniel Veillardea7751d2002-12-20 00:16:24 +0000430 if ((oldstate == XML_TEXTREADER_ELEMENT) &&
431 (reader->node->type == XML_ELEMENT_NODE)) {
432 reader->state = XML_TEXTREADER_END;
433 DUMP_READER
434 return(1);
435 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000436 reader->node = reader->node->parent;
437 if ((reader->node == NULL) ||
438 (reader->node->type == XML_DOCUMENT_NODE) ||
439#ifdef LIBXML_DOCB_ENABLED
440 (reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
441#endif
442 (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000443 if (reader->mode != XML_TEXTREADER_DONE) {
444 val = xmlParseChunk(reader->ctxt, "", 0, 1);
445 reader->mode = XML_TEXTREADER_DONE;
446 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000447 reader->node = NULL;
448 reader->depth = 0;
449
450 /*
451 * Cleanup of the old node
452 */
453 if (oldnode->type != XML_DTD_NODE) {
454 xmlUnlinkNode(oldnode);
455 xmlFreeNode(oldnode);
456 }
457
458 return(0);
459 }
460 reader->depth--;
461 reader->state = XML_TEXTREADER_BACKTRACK;
462 DUMP_READER
463 return(1);
464}
465
Daniel Veillard67df8092002-12-16 22:04:11 +0000466/**
467 * xmlTextReaderReadState:
468 * @reader: the xmlTextReaderPtr used
469 *
470 * Gets the read state of the reader.
471 *
472 * Returns the state value, or -1 in case of error
473 */
474int
475xmlTextReaderReadState(xmlTextReaderPtr reader) {
476 if (reader == NULL)
477 return(-1);
478 return(reader->mode);
479}
480
481/**
482 * xmlTextReaderReadInnerXml:
483 * @reader: the xmlTextReaderPtr used
484 *
485 * Reads the contents of the current node, including child nodes and markup.
486 *
487 * Returns a string containing the XML content, or NULL if the current node
488 * is neither an element nor attribute, or has no child nodes. The
489 * string must be deallocated by the caller.
490 */
491xmlChar *
492xmlTextReaderReadInnerXml(xmlTextReaderPtr reader) {
493 TODO
494 return(NULL);
495}
496
497/**
498 * xmlTextReaderReadOuterXml:
499 * @reader: the xmlTextReaderPtr used
500 *
501 * Reads the contents of the current node, including child nodes and markup.
502 *
503 * Returns a string containing the XML content, or NULL if the current node
504 * is neither an element nor attribute, or has no child nodes. The
505 * string must be deallocated by the caller.
506 */
507xmlChar *
508xmlTextReaderReadOuterXml(xmlTextReaderPtr reader) {
509 TODO
510 return(NULL);
511}
512
513/**
514 * xmlTextReaderReadString:
515 * @reader: the xmlTextReaderPtr used
516 *
517 * Reads the contents of an element or a text node as a string.
518 *
519 * Returns a string containing the contents of the Element or Text node,
520 * or NULL if the reader is positioned on any other type of node.
521 * The string must be deallocated by the caller.
522 */
523xmlChar *
524xmlTextReaderReadString(xmlTextReaderPtr reader) {
525 TODO
526 return(NULL);
527}
528
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000529/**
530 * xmlTextReaderReadBase64:
531 * @reader: the xmlTextReaderPtr used
532 * @array: a byte array to store the content.
533 * @offset: the zero-based index into array where the method should
534 * begin to write.
535 * @len: the number of bytes to write.
536 *
537 * Reads and decodes the Base64 encoded contents of an element and
538 * stores the result in a byte buffer.
539 *
540 * Returns the number of bytes written to array, or zero if the current
541 * instance is not positioned on an element or -1 in case of error.
542 */
543int
544xmlTextReaderReadBase64(xmlTextReaderPtr reader, unsigned char *array,
545 int offset, int len) {
546 if ((reader == NULL) || (reader->ctxt == NULL))
547 return(-1);
548 if (reader->ctxt->wellFormed != 1)
549 return(-1);
550
551 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
552 return(0);
553 TODO
554 return(0);
555}
556
557/**
558 * xmlTextReaderReadBinHex:
559 * @reader: the xmlTextReaderPtr used
560 * @array: a byte array to store the content.
561 * @offset: the zero-based index into array where the method should
562 * begin to write.
563 * @len: the number of bytes to write.
564 *
565 * Reads and decodes the BinHex encoded contents of an element and
566 * stores the result in a byte buffer.
567 *
568 * Returns the number of bytes written to array, or zero if the current
569 * instance is not positioned on an element or -1 in case of error.
570 */
571int
572xmlTextReaderReadBinHex(xmlTextReaderPtr reader, unsigned char *array,
573 int offset, int len) {
574 if ((reader == NULL) || (reader->ctxt == NULL))
575 return(-1);
576 if (reader->ctxt->wellFormed != 1)
577 return(-1);
578
579 if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
580 return(0);
581 TODO
582 return(0);
583}
584
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000585/************************************************************************
586 * *
587 * Constructor and destructors *
588 * *
589 ************************************************************************/
590/**
591 * xmlNewTextReader:
592 * @input: the xmlParserInputBufferPtr used to read data
Daniel Veillardea7751d2002-12-20 00:16:24 +0000593 * @URI: the URI information for the source if available
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000594 *
595 * Create an xmlTextReader structure fed with @input
596 *
597 * Returns the new xmlTextReaderPtr or NULL in case of error
598 */
599xmlTextReaderPtr
Daniel Veillardea7751d2002-12-20 00:16:24 +0000600xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000601 xmlTextReaderPtr ret;
602 int val;
603
604 if (input == NULL)
605 return(NULL);
606 ret = xmlMalloc(sizeof(xmlTextReader));
607 if (ret == NULL) {
608 xmlGenericError(xmlGenericErrorContext,
609 "xmlNewTextReader : malloc failed\n");
610 return(NULL);
611 }
612 memset(ret, 0, sizeof(xmlTextReader));
613 ret->input = input;
614 ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
615 if (ret->sax == NULL) {
616 xmlFree(ret);
617 xmlGenericError(xmlGenericErrorContext,
618 "xmlNewTextReader : malloc failed\n");
619 return(NULL);
620 }
621 memcpy(ret->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler));
622 ret->startElement = ret->sax->startElement;
623 ret->sax->startElement = xmlTextReaderStartElement;
624 ret->endElement = ret->sax->endElement;
625 ret->sax->endElement = xmlTextReaderEndElement;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000626 ret->characters = ret->sax->characters;
627 ret->sax->characters = xmlTextReaderCharacters;
628 ret->cdataBlock = ret->sax->cdataBlock;
629 ret->sax->cdataBlock = xmlTextReaderCDataBlock;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000630
Daniel Veillard67df8092002-12-16 22:04:11 +0000631 ret->mode = XML_TEXTREADER_MODE_INITIAL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000632 ret->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000633 ret->curnode = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000634 val = xmlParserInputBufferRead(input, 4);
635 if (val >= 4) {
636 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
Daniel Veillardea7751d2002-12-20 00:16:24 +0000637 (const char *) ret->input->buffer->content, 4, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000638 ret->base = 0;
639 ret->cur = 4;
640 } else {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000641 ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000642 ret->base = 0;
643 ret->cur = 0;
644 }
645 ret->ctxt->_private = ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000646 ret->ctxt->linenumbers = 1;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000647 ret->allocs = XML_TEXTREADER_CTXT;
648 return(ret);
649
650}
651
652/**
653 * xmlNewTextReaderFilename:
654 * @URI: the URI of the resource to process
655 *
656 * Create an xmlTextReader structure fed with the resource at @URI
657 *
658 * Returns the new xmlTextReaderPtr or NULL in case of error
659 */
660xmlTextReaderPtr
661xmlNewTextReaderFilename(const char *URI) {
662 xmlParserInputBufferPtr input;
663 xmlTextReaderPtr ret;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000664 char *directory = NULL;
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000665
666 input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
667 if (input == NULL)
668 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000669 ret = xmlNewTextReader(input, URI);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000670 if (ret == NULL) {
671 xmlFreeParserInputBuffer(input);
672 return(NULL);
673 }
674 ret->allocs |= XML_TEXTREADER_INPUT;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000675 if (ret->ctxt->directory == NULL)
676 directory = xmlParserGetDirectory(URI);
677 if ((ret->ctxt->directory == NULL) && (directory != NULL))
678 ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
679 if (directory != NULL)
680 xmlFree(directory);
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000681 return(ret);
682}
683
684/**
685 * xmlFreeTextReader:
686 * @reader: the xmlTextReaderPtr
687 *
688 * Deallocate all the resources associated to the reader
689 */
690void
691xmlFreeTextReader(xmlTextReaderPtr reader) {
692 if (reader == NULL)
693 return;
694 if (reader->ctxt != NULL) {
695 if (reader->ctxt->myDoc != NULL) {
696 xmlFreeDoc(reader->ctxt->myDoc);
697 reader->ctxt->myDoc = NULL;
698 }
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000699 if ((reader->ctxt->vctxt.vstateTab != NULL) &&
700 (reader->ctxt->vctxt.vstateMax > 0)){
701 xmlFree(reader->ctxt->vctxt.vstateTab);
702 reader->ctxt->vctxt.vstateTab = 0;
703 reader->ctxt->vctxt.vstateMax = 0;
704 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000705 if (reader->allocs & XML_TEXTREADER_CTXT)
706 xmlFreeParserCtxt(reader->ctxt);
707 }
708 if (reader->sax != NULL)
709 xmlFree(reader->sax);
710 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
711 xmlFreeParserInputBuffer(reader->input);
Daniel Veillardbeb70bd2002-12-18 14:53:54 +0000712 if (reader->faketext != NULL) {
713 xmlFreeNode(reader->faketext);
714 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +0000715 xmlFree(reader);
716}
717
718/************************************************************************
719 * *
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000720 * Methods for XmlTextReader *
721 * *
722 ************************************************************************/
723/**
724 * xmlTextReaderClose:
725 * @reader: the xmlTextReaderPtr used
726 *
727 * This method releases any resources allocated by the current instance
728 * changes the state to Closed and close any underlying input.
729 *
730 * Returns 0 or -1 in case of error
731 */
732int
733xmlTextReaderClose(xmlTextReaderPtr reader) {
734 if (reader == NULL)
735 return(-1);
736 reader->node = NULL;
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000737 reader->curnode = NULL;
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000738 reader->mode = XML_TEXTREADER_MODE_CLOSED;
739 if (reader->ctxt != NULL) {
740 if (reader->ctxt->myDoc != NULL) {
741 xmlFreeDoc(reader->ctxt->myDoc);
742 reader->ctxt->myDoc = NULL;
743 }
744 if (reader->allocs & XML_TEXTREADER_CTXT) {
745 xmlFreeParserCtxt(reader->ctxt);
746 reader->allocs -= XML_TEXTREADER_CTXT;
747 }
748 }
749 if (reader->sax != NULL) {
750 xmlFree(reader->sax);
751 reader->sax = NULL;
752 }
753 if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
754 xmlFreeParserInputBuffer(reader->input);
755 reader->allocs -= XML_TEXTREADER_INPUT;
756 }
757 return(0);
758}
759
760/**
761 * xmlTextReaderGetAttributeNo:
762 * @reader: the xmlTextReaderPtr used
763 * @no: the zero-based index of the attribute relative to the containing element
764 *
765 * Provides the value of the attribute with the specified index relative
766 * to the containing element.
767 *
768 * Returns a string containing the value of the specified attribute, or NULL
769 * in case of error. The string must be deallocated by the caller.
770 */
771xmlChar *
772xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
773 xmlChar *ret;
774 int i;
775 xmlAttrPtr cur;
776 xmlNsPtr ns;
777
778 if (reader == NULL)
779 return(NULL);
780 if (reader->node == NULL)
781 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000782 if (reader->curnode != NULL)
783 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000784 /* TODO: handle the xmlDecl */
785 if (reader->node->type != XML_ELEMENT_NODE)
786 return(NULL);
787
788 ns = reader->node->nsDef;
789 for (i = 0;(i < no) && (ns != NULL);i++) {
790 ns = ns->next;
791 }
792 if (ns != NULL)
793 return(xmlStrdup(ns->href));
794
795 cur = reader->node->properties;
796 if (cur == NULL)
797 return(NULL);
798 for (;i < no;i++) {
799 cur = cur->next;
800 if (cur == NULL)
801 return(NULL);
802 }
803 /* TODO walk the DTD if present */
804
805 ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
806 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
807 return(ret);
808}
809
810/**
811 * xmlTextReaderGetAttribute:
812 * @reader: the xmlTextReaderPtr used
813 * @name: the qualified name of the attribute.
814 *
815 * Provides the value of the attribute with the specified qualified name.
816 *
817 * Returns a string containing the value of the specified attribute, or NULL
818 * in case of error. The string must be deallocated by the caller.
819 */
820xmlChar *
821xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
822 xmlChar *prefix = NULL;
823 xmlChar *localname;
824 xmlNsPtr ns;
825 xmlChar *ret = NULL;
826
827 if ((reader == NULL) || (name == NULL))
828 return(NULL);
829 if (reader->node == NULL)
830 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000831 if (reader->curnode != NULL)
832 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000833
834 /* TODO: handle the xmlDecl */
835 if (reader->node->type != XML_ELEMENT_NODE)
836 return(NULL);
837
838 localname = xmlSplitQName2(name, &prefix);
839 if (localname == NULL)
840 return(xmlGetProp(reader->node, name));
841
842 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
843 if (ns != NULL)
844 ret = xmlGetNsProp(reader->node, localname, ns->href);
845
846 if (localname != NULL)
847 xmlFree(localname);
848 if (prefix != NULL)
849 xmlFree(prefix);
850 return(ret);
851}
852
853
854/**
855 * xmlTextReaderGetAttributeNs:
856 * @reader: the xmlTextReaderPtr used
857 * @localName: the local name of the attribute.
858 * @namespaceURI: the namespace URI of the attribute.
859 *
860 * Provides the value of the specified attribute
861 *
862 * Returns a string containing the value of the specified attribute, or NULL
863 * in case of error. The string must be deallocated by the caller.
864 */
865xmlChar *
866xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
867 const xmlChar *namespaceURI) {
868 if ((reader == NULL) || (localName == NULL))
869 return(NULL);
870 if (reader->node == NULL)
871 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000872 if (reader->curnode != NULL)
873 return(NULL);
Daniel Veillard0eb38c72002-12-14 23:00:35 +0000874
875 /* TODO: handle the xmlDecl */
876 if (reader->node->type != XML_ELEMENT_NODE)
877 return(NULL);
878
879 return(xmlGetNsProp(reader->node, localName, namespaceURI));
880}
881
Daniel Veillardda46d2d2002-12-15 23:36:49 +0000882/**
883 * xmlTextReaderGetRemainder:
884 * @reader: the xmlTextReaderPtr used
885 *
886 * Method to get the remainder of the buffered XML. this method stops the
887 * parser, set its state to End Of File and return the input stream with
888 * what is left that the parser did not use.
889 *
890 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
891 * in case of error.
892 */
893xmlParserInputBufferPtr
894xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
895 xmlParserInputBufferPtr ret = NULL;
896
897 if (reader == NULL)
898 return(NULL);
899 if (reader->node == NULL)
900 return(NULL);
901
902 reader->node = NULL;
903 reader->curnode = NULL;
904 reader->mode = XML_TEXTREADER_MODE_EOF;
905 if (reader->ctxt != NULL) {
906 if (reader->ctxt->myDoc != NULL) {
907 xmlFreeDoc(reader->ctxt->myDoc);
908 reader->ctxt->myDoc = NULL;
909 }
910 if (reader->allocs & XML_TEXTREADER_CTXT) {
911 xmlFreeParserCtxt(reader->ctxt);
912 reader->allocs -= XML_TEXTREADER_CTXT;
913 }
914 }
915 if (reader->sax != NULL) {
916 xmlFree(reader->sax);
917 reader->sax = NULL;
918 }
919 if (reader->allocs & XML_TEXTREADER_INPUT) {
920 ret = reader->input;
921 reader->allocs -= XML_TEXTREADER_INPUT;
922 } else {
923 /*
924 * Hum, one may need to duplicate the data structure because
925 * without reference counting the input may be freed twice:
926 * - by the layer which allocated it.
927 * - by the layer to which would have been returned to.
928 */
929 TODO
930 return(NULL);
931 }
932 return(ret);
933}
934
935/**
936 * xmlTextReaderLookupNamespace:
937 * @reader: the xmlTextReaderPtr used
938 * @prefix: the prefix whose namespace URI is to be resolved. To return
939 * the default namespace, specify NULL
940 *
941 * Resolves a namespace prefix in the scope of the current element.
942 *
943 * Returns a string containing the namespace URI to which the prefix maps
944 * or NULL in case of error. The string must be deallocated by the caller.
945 */
946xmlChar *
947xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
948 xmlNsPtr ns;
949
950 if (reader == NULL)
951 return(NULL);
952 if (reader->node == NULL)
953 return(NULL);
954
955 ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
956 if (ns == NULL)
957 return(NULL);
958 return(xmlStrdup(ns->href));
959}
960
961/**
962 * xmlTextReaderMoveToAttributeNo:
963 * @reader: the xmlTextReaderPtr used
964 * @no: the zero-based index of the attribute relative to the containing
965 * element.
966 *
967 * Moves the position of the current instance to the attribute with
968 * the specified index relative to the containing element.
969 *
970 * Returns 1 in case of success, -1 in case of error, 0 if not found
971 */
972int
973xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
974 int i;
975 xmlAttrPtr cur;
976 xmlNsPtr ns;
977
978 if (reader == NULL)
979 return(-1);
980 if (reader->node == NULL)
981 return(-1);
982 /* TODO: handle the xmlDecl */
983 if (reader->node->type != XML_ELEMENT_NODE)
984 return(-1);
985
986 reader->curnode = NULL;
987
988 ns = reader->node->nsDef;
989 for (i = 0;(i < no) && (ns != NULL);i++) {
990 ns = ns->next;
991 }
992 if (ns != NULL) {
993 reader->curnode = (xmlNodePtr) ns;
994 return(1);
995 }
996
997 cur = reader->node->properties;
998 if (cur == NULL)
999 return(0);
1000 for (;i < no;i++) {
1001 cur = cur->next;
1002 if (cur == NULL)
1003 return(0);
1004 }
1005 /* TODO walk the DTD if present */
1006
1007 reader->curnode = (xmlNodePtr) cur;
1008 return(1);
1009}
1010
1011/**
1012 * xmlTextReaderMoveToAttribute:
1013 * @reader: the xmlTextReaderPtr used
1014 * @name: the qualified name of the attribute.
1015 *
1016 * Moves the position of the current instance to the attribute with
1017 * the specified qualified name.
1018 *
1019 * Returns 1 in case of success, -1 in case of error, 0 if not found
1020 */
1021int
1022xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
1023 xmlChar *prefix = NULL;
1024 xmlChar *localname;
1025 xmlNsPtr ns;
1026 xmlAttrPtr prop;
1027
1028 if ((reader == NULL) || (name == NULL))
1029 return(-1);
1030 if (reader->node == NULL)
1031 return(-1);
1032
1033 /* TODO: handle the xmlDecl */
1034 if (reader->node->type != XML_ELEMENT_NODE)
1035 return(0);
1036
1037 localname = xmlSplitQName2(name, &prefix);
1038 if (localname == NULL) {
1039 /*
1040 * Namespace default decl
1041 */
1042 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
1043 ns = reader->node->nsDef;
1044 while (ns != NULL) {
1045 if (ns->prefix == NULL) {
1046 reader->curnode = (xmlNodePtr) ns;
1047 return(1);
1048 }
1049 ns = ns->next;
1050 }
1051 return(0);
1052 }
1053
1054 prop = reader->node->properties;
1055 while (prop != NULL) {
1056 /*
1057 * One need to have
1058 * - same attribute names
1059 * - and the attribute carrying that namespace
1060 */
1061 if ((xmlStrEqual(prop->name, name)) &&
1062 ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
1063 reader->curnode = (xmlNodePtr) prop;
1064 return(1);
1065 }
1066 prop = prop->next;
1067 }
1068 return(0);
1069 }
1070
1071 /*
1072 * Namespace default decl
1073 */
1074 if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
1075 ns = reader->node->nsDef;
1076 while (ns != NULL) {
1077 if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
1078 reader->curnode = (xmlNodePtr) ns;
1079 goto found;
1080 }
1081 ns = ns->next;
1082 }
1083 goto not_found;
1084 }
1085 prop = reader->node->properties;
1086 while (prop != NULL) {
1087 /*
1088 * One need to have
1089 * - same attribute names
1090 * - and the attribute carrying that namespace
1091 */
1092 if ((xmlStrEqual(prop->name, localname)) &&
1093 (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
1094 reader->curnode = (xmlNodePtr) prop;
1095 goto found;
1096 }
1097 prop = prop->next;
1098 }
1099not_found:
1100 if (localname != NULL)
1101 xmlFree(localname);
1102 if (prefix != NULL)
1103 xmlFree(prefix);
1104 return(0);
1105
1106found:
1107 if (localname != NULL)
1108 xmlFree(localname);
1109 if (prefix != NULL)
1110 xmlFree(prefix);
1111 return(1);
1112}
1113
1114/**
1115 * xmlTextReaderMoveToAttributeNs:
1116 * @reader: the xmlTextReaderPtr used
1117 * @localName: the local name of the attribute.
1118 * @namespaceURI: the namespace URI of the attribute.
1119 *
1120 * Moves the position of the current instance to the attribute with the
1121 * specified local name and namespace URI.
1122 *
1123 * Returns 1 in case of success, -1 in case of error, 0 if not found
1124 */
1125int
1126xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
1127 const xmlChar *localName, const xmlChar *namespaceURI) {
1128 xmlAttrPtr prop;
1129 xmlNodePtr node;
1130
1131 if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
1132 return(-1);
1133 if (reader->node == NULL)
1134 return(-1);
1135 if (reader->node->type != XML_ELEMENT_NODE)
1136 return(0);
1137 node = reader->node;
1138
1139 /*
1140 * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no
1141 * namespace name associated to "xmlns"
1142 */
1143 prop = node->properties;
1144 while (prop != NULL) {
1145 /*
1146 * One need to have
1147 * - same attribute names
1148 * - and the attribute carrying that namespace
1149 */
1150 if (xmlStrEqual(prop->name, localName) &&
1151 ((prop->ns != NULL) &&
1152 (xmlStrEqual(prop->ns->href, namespaceURI)))) {
1153 reader->curnode = (xmlNodePtr) prop;
1154 return(1);
1155 }
1156 prop = prop->next;
1157 }
1158 return(0);
1159}
1160
1161/**
1162 * xmlTextReaderMoveToFirstAttribute:
1163 * @reader: the xmlTextReaderPtr used
1164 *
1165 * Moves the position of the current instance to the first attribute
1166 * associated with the current node.
1167 *
1168 * Returns 1 in case of success, -1 in case of error, 0 if not found
1169 */
1170int
1171xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
1172 if (reader == NULL)
1173 return(-1);
1174 if (reader->node == NULL)
1175 return(-1);
1176 if (reader->node->type != XML_ELEMENT_NODE)
1177 return(0);
1178
1179 if (reader->node->nsDef != NULL) {
1180 reader->curnode = (xmlNodePtr) reader->node->nsDef;
1181 return(1);
1182 }
1183 if (reader->node->properties != NULL) {
1184 reader->curnode = (xmlNodePtr) reader->node->properties;
1185 return(1);
1186 }
1187 return(0);
1188}
1189
1190/**
1191 * xmlTextReaderMoveToNextAttribute:
1192 * @reader: the xmlTextReaderPtr used
1193 *
1194 * Moves the position of the current instance to the next attribute
1195 * associated with the current node.
1196 *
1197 * Returns 1 in case of success, -1 in case of error, 0 if not found
1198 */
1199int
1200xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
1201 if (reader == NULL)
1202 return(-1);
1203 if (reader->node == NULL)
1204 return(-1);
1205 if (reader->node->type != XML_ELEMENT_NODE)
1206 return(0);
1207 if (reader->curnode == NULL)
1208 return(xmlTextReaderMoveToFirstAttribute(reader));
1209
1210 if (reader->curnode->type == XML_NAMESPACE_DECL) {
1211 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
1212 if (ns->next != NULL) {
1213 reader->curnode = (xmlNodePtr) ns->next;
1214 return(1);
1215 }
1216 if (reader->node->properties != NULL) {
1217 reader->curnode = (xmlNodePtr) reader->node->properties;
1218 return(1);
1219 }
1220 return(0);
1221 } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
1222 (reader->curnode->next != NULL)) {
1223 reader->curnode = reader->curnode->next;
1224 return(1);
1225 }
1226 return(0);
1227}
1228
1229/**
1230 * xmlTextReaderMoveToElement:
1231 * @reader: the xmlTextReaderPtr used
1232 *
1233 * Moves the position of the current instance to the node that
1234 * contains the current Attribute node.
1235 *
1236 * Returns 1 in case of success, -1 in case of error, 0 if not moved
1237 */
1238int
1239xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
1240 if (reader == NULL)
1241 return(-1);
1242 if (reader->node == NULL)
1243 return(-1);
1244 if (reader->node->type != XML_ELEMENT_NODE)
1245 return(0);
1246 if (reader->curnode != NULL) {
1247 reader->curnode = NULL;
1248 return(1);
1249 }
1250 return(0);
1251}
1252
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001253/**
1254 * xmlTextReaderReadAttributeValue:
1255 * @reader: the xmlTextReaderPtr used
1256 *
1257 * Parses an attribute value into one or more Text and EntityReference nodes.
1258 *
1259 * Returns 1 in case of success, 0 if the reader was not positionned on an
1260 * ttribute node or all the attribute values have been read, or -1
1261 * in case of error.
1262 */
1263int
1264xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
1265 if (reader == NULL)
1266 return(-1);
1267 if (reader->node == NULL)
1268 return(-1);
1269 if (reader->curnode == NULL)
1270 return(0);
1271 if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
1272 if (reader->curnode->children == NULL)
1273 return(0);
1274 reader->curnode = reader->curnode->children;
1275 } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
1276 xmlNsPtr ns = (xmlNsPtr) reader->curnode;
1277
1278 if (reader->faketext == NULL) {
1279 reader->faketext = xmlNewDocText(reader->node->doc,
1280 ns->href);
1281 } else {
1282 if (reader->faketext->content != NULL)
1283 xmlFree(reader->faketext->content);
1284 reader->faketext->content = xmlStrdup(ns->href);
1285 }
1286 reader->curnode = reader->faketext;
1287 } else {
1288 if (reader->curnode->next == NULL)
1289 return(0);
1290 reader->curnode = reader->curnode->next;
1291 }
1292 return(1);
1293}
1294
Daniel Veillard0eb38c72002-12-14 23:00:35 +00001295/************************************************************************
1296 * *
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001297 * Acces API to the current node *
1298 * *
1299 ************************************************************************/
1300/**
1301 * xmlTextReaderAttributeCount:
1302 * @reader: the xmlTextReaderPtr used
1303 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001304 * Provides the number of attributes of the current node
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001305 *
1306 * Returns 0 i no attributes, -1 in case of error or the attribute count
1307 */
1308int
1309xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
1310 int ret;
1311 xmlAttrPtr attr;
Daniel Veillard67df8092002-12-16 22:04:11 +00001312 xmlNsPtr ns;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001313 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001314
1315 if (reader == NULL)
1316 return(-1);
1317 if (reader->node == NULL)
1318 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001319
1320 if (reader->curnode != NULL)
1321 node = reader->curnode;
1322 else
1323 node = reader->node;
1324
1325 if (node->type != XML_ELEMENT_NODE)
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001326 return(0);
1327 if ((reader->state == XML_TEXTREADER_END) ||
1328 (reader->state == XML_TEXTREADER_BACKTRACK))
1329 return(0);
1330 ret = 0;
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001331 attr = node->properties;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001332 while (attr != NULL) {
1333 ret++;
1334 attr = attr->next;
1335 }
Daniel Veillard67df8092002-12-16 22:04:11 +00001336 ns = node->nsDef;
1337 while (ns != NULL) {
1338 ret++;
1339 ns = ns->next;
1340 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001341 return(ret);
1342}
1343
1344/**
1345 * xmlTextReaderNodeType:
1346 * @reader: the xmlTextReaderPtr used
1347 *
1348 * Get the node type of the current node
1349 * Reference:
1350 * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
1351 *
1352 * Returns the xmlNodeType of the current node or -1 in case of error
1353 */
1354int
1355xmlTextReaderNodeType(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001356 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001357 if (reader == NULL)
1358 return(-1);
1359 if (reader->node == NULL)
1360 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001361 if (reader->curnode != NULL)
1362 node = reader->curnode;
1363 else
1364 node = reader->node;
1365 switch (node->type) {
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001366 case XML_ELEMENT_NODE:
1367 if ((reader->state == XML_TEXTREADER_END) ||
1368 (reader->state == XML_TEXTREADER_BACKTRACK))
1369 return(15);
1370 return(1);
1371 case XML_ATTRIBUTE_NODE:
1372 return(2);
1373 case XML_TEXT_NODE:
1374 return(3); /* TODO: SignificantWhitespace == 14 Whitespace == 13 */
1375 case XML_CDATA_SECTION_NODE:
1376 return(4);
1377 case XML_ENTITY_REF_NODE:
1378 return(5);
1379 case XML_ENTITY_NODE:
1380 return(6);
1381 case XML_PI_NODE:
1382 return(7);
1383 case XML_COMMENT_NODE:
1384 return(8);
1385 case XML_DOCUMENT_NODE:
1386 case XML_HTML_DOCUMENT_NODE:
1387#ifdef LIBXML_DOCB_ENABLED
1388 case XML_DOCB_DOCUMENT_NODE:
1389#endif
1390 return(9);
1391 case XML_DOCUMENT_FRAG_NODE:
1392 return(11);
1393 case XML_NOTATION_NODE:
1394 return(12);
1395 case XML_DOCUMENT_TYPE_NODE:
1396 case XML_DTD_NODE:
1397 return(10);
1398
1399 case XML_ELEMENT_DECL:
1400 case XML_ATTRIBUTE_DECL:
1401 case XML_ENTITY_DECL:
1402 case XML_NAMESPACE_DECL:
1403 case XML_XINCLUDE_START:
1404 case XML_XINCLUDE_END:
1405 return(0);
1406 }
1407 return(-1);
1408}
1409
1410/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00001411 * xmlTextReaderIsEmptyElement:
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001412 * @reader: the xmlTextReaderPtr used
1413 *
1414 * Check if the current node is empty
1415 *
1416 * Returns 1 if empty, 0 if not and -1 in case of error
1417 */
1418int
1419xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
1420 if ((reader == NULL) || (reader->node == NULL))
1421 return(-1);
Daniel Veillarddf512f42002-12-23 15:56:21 +00001422 if (reader->node->type != XML_ELEMENT_NODE)
1423 return(0);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001424 if (reader->node->children != NULL)
1425 return(0);
Daniel Veillarddf512f42002-12-23 15:56:21 +00001426 if (reader->node != reader->ctxt->node)
1427 return(1);
1428 if ((reader->ctxt->node != NULL) &&
1429 (reader->node == reader->ctxt->node->last) &&
1430 (reader->wasempty == 1))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001431 return(1);
1432 return(0);
1433}
1434
1435/**
1436 * xmlTextReaderLocalName:
1437 * @reader: the xmlTextReaderPtr used
1438 *
1439 * The local name of the node.
1440 *
1441 * Returns the local name or NULL if not available
1442 */
1443xmlChar *
1444xmlTextReaderLocalName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001445 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001446 if ((reader == NULL) || (reader->node == NULL))
1447 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001448 if (reader->curnode != NULL)
1449 node = reader->curnode;
1450 else
1451 node = reader->node;
1452 if (node->type == XML_NAMESPACE_DECL) {
1453 xmlNsPtr ns = (xmlNsPtr) node;
1454 if (ns->prefix == NULL)
1455 return(xmlStrdup(BAD_CAST "xmlns"));
1456 else
1457 return(xmlStrdup(ns->prefix));
1458 }
1459 if ((node->type != XML_ELEMENT_NODE) &&
1460 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001461 return(xmlTextReaderName(reader));
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001462 return(xmlStrdup(node->name));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001463}
1464
1465/**
1466 * xmlTextReaderName:
1467 * @reader: the xmlTextReaderPtr used
1468 *
1469 * The qualified name of the node, equal to Prefix :LocalName.
1470 *
1471 * Returns the local name or NULL if not available
1472 */
1473xmlChar *
1474xmlTextReaderName(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001475 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001476 xmlChar *ret;
1477
1478 if ((reader == NULL) || (reader->node == NULL))
1479 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001480 if (reader->curnode != NULL)
1481 node = reader->curnode;
1482 else
1483 node = reader->node;
1484 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001485 case XML_ELEMENT_NODE:
1486 case XML_ATTRIBUTE_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001487 if ((node->ns == NULL) ||
1488 (node->ns->prefix == NULL))
1489 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001490
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001491 ret = xmlStrdup(node->ns->prefix);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001492 ret = xmlStrcat(ret, BAD_CAST ":");
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001493 ret = xmlStrcat(ret, node->name);
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001494 return(ret);
1495 case XML_TEXT_NODE:
1496 return(xmlStrdup(BAD_CAST "#text"));
1497 case XML_CDATA_SECTION_NODE:
1498 return(xmlStrdup(BAD_CAST "#cdata-section"));
1499 case XML_ENTITY_NODE:
1500 case XML_ENTITY_REF_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001501 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001502 case XML_PI_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001503 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001504 case XML_COMMENT_NODE:
1505 return(xmlStrdup(BAD_CAST "#comment"));
1506 case XML_DOCUMENT_NODE:
1507 case XML_HTML_DOCUMENT_NODE:
1508#ifdef LIBXML_DOCB_ENABLED
1509 case XML_DOCB_DOCUMENT_NODE:
1510#endif
1511 return(xmlStrdup(BAD_CAST "#document"));
1512 case XML_DOCUMENT_FRAG_NODE:
1513 return(xmlStrdup(BAD_CAST "#document-fragment"));
1514 case XML_NOTATION_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001515 return(xmlStrdup(node->name));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001516 case XML_DOCUMENT_TYPE_NODE:
1517 case XML_DTD_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001518 return(xmlStrdup(node->name));
1519 case XML_NAMESPACE_DECL: {
1520 xmlNsPtr ns = (xmlNsPtr) node;
1521
1522 ret = xmlStrdup(BAD_CAST "xmlns");
1523 if (ns->prefix == NULL)
1524 return(ret);
1525 ret = xmlStrcat(ret, BAD_CAST ":");
1526 ret = xmlStrcat(ret, ns->prefix);
1527 return(ret);
1528 }
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001529
1530 case XML_ELEMENT_DECL:
1531 case XML_ATTRIBUTE_DECL:
1532 case XML_ENTITY_DECL:
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001533 case XML_XINCLUDE_START:
1534 case XML_XINCLUDE_END:
1535 return(NULL);
1536 }
1537 return(NULL);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001538}
1539
1540/**
1541 * xmlTextReaderPrefix:
1542 * @reader: the xmlTextReaderPtr used
1543 *
1544 * A shorthand reference to the namespace associated with the node.
1545 *
1546 * Returns the prefix or NULL if not available
1547 */
1548xmlChar *
1549xmlTextReaderPrefix(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001550 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001551 if ((reader == NULL) || (reader->node == NULL))
1552 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001553 if (reader->curnode != NULL)
1554 node = reader->curnode;
1555 else
1556 node = reader->node;
1557 if (node->type == XML_NAMESPACE_DECL) {
1558 xmlNsPtr ns = (xmlNsPtr) node;
1559 if (ns->prefix == NULL)
1560 return(NULL);
1561 return(xmlStrdup(BAD_CAST "xmlns"));
1562 }
1563 if ((node->type != XML_ELEMENT_NODE) &&
1564 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001565 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001566 if ((node->ns != NULL) || (node->ns->prefix != NULL))
1567 return(xmlStrdup(node->ns->prefix));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001568 return(NULL);
1569}
1570
1571/**
1572 * xmlTextReaderNamespaceUri:
1573 * @reader: the xmlTextReaderPtr used
1574 *
1575 * The URI defining the namespace associated with the node.
1576 *
1577 * Returns the namespace URI or NULL if not available
1578 */
1579xmlChar *
1580xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001581 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001582 if ((reader == NULL) || (reader->node == NULL))
1583 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001584 if (reader->curnode != NULL)
1585 node = reader->curnode;
1586 else
1587 node = reader->node;
1588 if (node->type == XML_NAMESPACE_DECL) {
1589 xmlNsPtr ns = (xmlNsPtr) node;
1590 return(xmlStrdup(ns->href));
1591 }
1592 if ((node->type != XML_ELEMENT_NODE) &&
1593 (node->type != XML_ATTRIBUTE_NODE))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001594 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001595 if (node->ns != NULL)
1596 return(xmlStrdup(node->ns->href));
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001597 return(NULL);
1598}
1599
1600/**
1601 * xmlTextReaderBaseUri:
1602 * @reader: the xmlTextReaderPtr used
1603 *
1604 * The base URI of the node.
1605 *
1606 * Returns the base URI or NULL if not available
1607 */
1608xmlChar *
1609xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
1610 if ((reader == NULL) || (reader->node == NULL))
1611 return(NULL);
1612 return(xmlNodeGetBase(NULL, reader->node));
1613}
1614
1615/**
1616 * xmlTextReaderDepth:
1617 * @reader: the xmlTextReaderPtr used
1618 *
1619 * The depth of the node in the tree.
1620 *
1621 * Returns the depth or -1 in case of error
1622 */
1623int
1624xmlTextReaderDepth(xmlTextReaderPtr reader) {
1625 if (reader == NULL)
1626 return(-1);
1627 if (reader->node == NULL)
1628 return(0);
1629
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001630 if (reader->curnode != NULL) {
1631 if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
1632 (reader->curnode->type == XML_NAMESPACE_DECL))
1633 return(reader->depth + 1);
1634 return(reader->depth + 2);
1635 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001636 return(reader->depth);
1637}
1638
1639/**
1640 * xmlTextReaderHasAttributes:
1641 * @reader: the xmlTextReaderPtr used
1642 *
1643 * Whether the node has attributes.
1644 *
1645 * Returns 1 if true, 0 if false, and -1 in case or error
1646 */
1647int
1648xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001649 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001650 if (reader == NULL)
1651 return(-1);
1652 if (reader->node == NULL)
1653 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001654 if (reader->curnode != NULL)
1655 node = reader->curnode;
1656 else
1657 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001658
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001659 if ((node->type == XML_ELEMENT_NODE) &&
1660 (node->properties != NULL))
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001661 return(1);
1662 /* TODO: handle the xmlDecl */
1663 return(0);
1664}
1665
1666/**
1667 * xmlTextReaderHasValue:
1668 * @reader: the xmlTextReaderPtr used
1669 *
1670 * Whether the node can have a text value.
1671 *
1672 * Returns 1 if true, 0 if false, and -1 in case or error
1673 */
1674int
1675xmlTextReaderHasValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001676 xmlNodePtr node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001677 if (reader == NULL)
1678 return(-1);
1679 if (reader->node == NULL)
1680 return(0);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001681 if (reader->curnode != NULL)
1682 node = reader->curnode;
1683 else
1684 node = reader->node;
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001685
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001686 switch (node->type) {
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001687 case XML_ATTRIBUTE_NODE:
1688 case XML_TEXT_NODE:
1689 case XML_CDATA_SECTION_NODE:
1690 case XML_PI_NODE:
1691 case XML_COMMENT_NODE:
1692 return(1);
1693 default:
1694 return(0);
1695 }
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001696 return(0);
1697}
1698
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001699/**
1700 * xmlTextReaderValue:
1701 * @reader: the xmlTextReaderPtr used
1702 *
1703 * Provides the text value of the node if present
1704 *
1705 * Returns the string or NULL if not available. The retsult must be deallocated
1706 * with xmlFree()
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001707 */
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001708xmlChar *
1709xmlTextReaderValue(xmlTextReaderPtr reader) {
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001710 xmlNodePtr node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001711 if (reader == NULL)
1712 return(NULL);
1713 if (reader->node == NULL)
1714 return(NULL);
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001715 if (reader->curnode != NULL)
1716 node = reader->curnode;
1717 else
1718 node = reader->node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001719
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001720 switch (node->type) {
1721 case XML_NAMESPACE_DECL:
1722 return(xmlStrdup(((xmlNsPtr) node)->href));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001723 case XML_ATTRIBUTE_NODE:{
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001724 xmlAttrPtr attr = (xmlAttrPtr) node;
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001725
1726 if (attr->parent != NULL)
1727 return (xmlNodeListGetString
1728 (attr->parent->doc, attr->children, 1));
1729 else
1730 return (xmlNodeListGetString(NULL, attr->children, 1));
1731 break;
1732 }
1733 case XML_TEXT_NODE:
1734 case XML_CDATA_SECTION_NODE:
1735 case XML_PI_NODE:
1736 case XML_COMMENT_NODE:
Daniel Veillardda46d2d2002-12-15 23:36:49 +00001737 if (node->content != NULL)
1738 return (xmlStrdup(node->content));
Daniel Veillard9b4bb4d2002-12-11 19:28:47 +00001739 default:
1740 return(NULL);
1741 }
1742 return(NULL);
1743}
1744
1745/**
1746 * xmlTextReaderIsDefault:
1747 * @reader: the xmlTextReaderPtr used
1748 *
1749 * Whether an Attribute node was generated from the default value
1750 * defined in the DTD or schema.
1751 *
1752 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
1753 */
1754int
1755xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
1756 if (reader == NULL)
1757 return(-1);
1758 return(0);
1759}
1760
1761/**
1762 * xmlTextReaderQuoteChar:
1763 * @reader: the xmlTextReaderPtr used
1764 *
1765 * The quotation mark character used to enclose the value of an attribute.
1766 *
1767 * Returns " or ' and -1 in case of error
1768 */
1769int
1770xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
1771 if (reader == NULL)
1772 return(-1);
1773 /* TODO maybe lookup the attribute value for " first */
1774 return((int) '"');
1775}
Daniel Veillarde1ca5032002-12-09 14:13:43 +00001776
1777/**
1778 * xmlTextReaderXmlLang:
1779 * @reader: the xmlTextReaderPtr used
1780 *
1781 * The xml:lang scope within which the node resides.
1782 *
1783 * Returns the xml:lang value or NULL if none exists.
1784 */
1785xmlChar *
1786xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
1787 if (reader == NULL)
1788 return(NULL);
1789 if (reader->node == NULL)
1790 return(NULL);
1791 return(xmlNodeGetLang(reader->node));
1792}
1793
Daniel Veillard67df8092002-12-16 22:04:11 +00001794/**
1795 * xmlTextReaderNormalization:
1796 * @reader: the xmlTextReaderPtr used
1797 *
1798 * The value indicating whether to normalize white space and attribute values.
1799 * Since attribute value and end of line normalizations are a MUST in the XML
1800 * specification only the value true is accepted. The broken bahaviour of
1801 * accepting out of range character entities like &#0; is of course not
1802 * supported either.
1803 *
1804 * Returns 1 or -1 in case of error.
1805 */
1806int
1807xmlTextReaderNormalization(xmlTextReaderPtr reader) {
1808 if (reader == NULL)
1809 return(-1);
1810 return(1);
1811}
1812
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00001813/************************************************************************
1814 * *
1815 * Extensions to the base APIs *
1816 * *
1817 ************************************************************************/
1818
1819/**
1820 * xmlTextReaderSetParserProp:
1821 * @reader: the xmlTextReaderPtr used
1822 * @prop: the xmlParserProperties to set
1823 * @value: usually 0 or 1 to (de)activate it
1824 *
1825 * Change the parser processing behaviour by changing some of its internal
1826 * properties. Note that some properties can only be changed before any
1827 * read has been done.
1828 *
1829 * Returns 0 if the call was successful, or -1 in case of error
1830 */
1831int
1832xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
1833 xmlParserProperties p = (xmlParserProperties) prop;
1834 xmlParserCtxtPtr ctxt;
1835
1836 if ((reader == NULL) || (reader->ctxt == NULL))
1837 return(-1);
1838 ctxt = reader->ctxt;
1839
1840 switch (p) {
1841 case XML_PARSER_LOADDTD:
1842 if (value != 0) {
1843 if (ctxt->loadsubset == 0) {
1844 if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
1845 return(-1);
1846 ctxt->loadsubset = XML_DETECT_IDS;
1847 }
1848 } else {
1849 ctxt->loadsubset = 0;
1850 }
1851 return(0);
1852 case XML_PARSER_DEFAULTATTRS:
1853 if (value != 0) {
1854 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
1855 } else {
1856 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
1857 ctxt->loadsubset -= XML_COMPLETE_ATTRS;
1858 }
1859 return(0);
1860 case XML_PARSER_VALIDATE:
1861 if (value != 0) {
1862 ctxt->validate = 1;
1863 } else {
1864 ctxt->validate = 0;
1865 }
1866 return(0);
1867 }
1868 return(-1);
1869}
1870
1871/**
1872 * xmlTextReaderGetParserProp:
1873 * @reader: the xmlTextReaderPtr used
1874 * @prop: the xmlParserProperties to get
1875 *
1876 * Read the parser internal property.
1877 *
1878 * Returns the value, usually 0 or 1, or -1 in case of error.
1879 */
1880int
1881xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
1882 xmlParserProperties p = (xmlParserProperties) prop;
1883 xmlParserCtxtPtr ctxt;
1884
1885 if ((reader == NULL) || (reader->ctxt == NULL))
1886 return(-1);
1887 ctxt = reader->ctxt;
1888
1889 switch (p) {
1890 case XML_PARSER_LOADDTD:
1891 if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
1892 return(1);
1893 return(0);
1894 case XML_PARSER_DEFAULTATTRS:
1895 if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
1896 return(1);
1897 return(0);
1898 case XML_PARSER_VALIDATE:
1899 return(ctxt->validate);
1900 }
1901 return(-1);
1902}
1903
1904/************************************************************************
1905 * *
1906 * Utilities *
1907 * *
1908 ************************************************************************/
1909/**
1910 * xmlBase64Decode:
1911 * @in: the input buffer
1912 * @inlen: the size of the input (in), the size read from it (out)
1913 * @to: the output buffer
1914 * @tolen: the size of the output (in), the size written to (out)
1915 *
1916 * Base64 decoder, reads from @in and save in @to
1917 *
1918 * Returns 0 if all the input was consumer, 1 if the Base64 end was reached,
1919 * 2 if there wasn't enough space on the output or -1 in case of error.
1920 */
1921static int
1922xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
1923 unsigned char *to, unsigned long *tolen) {
1924 unsigned long incur; /* current index in in[] */
1925 unsigned long inblk; /* last block index in in[] */
1926 unsigned long outcur; /* current index in out[] */
1927 unsigned long inmax; /* size of in[] */
1928 unsigned long outmax; /* size of out[] */
1929 unsigned char cur; /* the current value read from in[] */
1930 unsigned char intmp[3], outtmp[4]; /* temporary buffers for the convert */
1931 int nbintmp; /* number of byte in intmp[] */
1932 int is_ignore; /* cur should be ignored */
1933 int is_end = 0; /* the end of the base64 was found */
1934 int retval = 1;
1935 int i;
1936
1937 if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
1938 return(-1);
1939
1940 incur = 0;
1941 inblk = 0;
1942 outcur = 0;
1943 inmax = *inlen;
1944 outmax = *tolen;
1945 nbintmp = 0;
1946
1947 while (1) {
1948 if (incur >= inmax)
1949 break;
1950 cur = in[incur++];
1951 is_ignore = 0;
1952 if ((cur >= 'A') && (cur <= 'Z'))
1953 cur = cur - 'A';
1954 else if ((cur >= 'a') && (cur <= 'z'))
1955 cur = cur - 'a' + 26;
1956 else if ((cur >= '0') && (cur <= '9'))
1957 cur = cur - '0' + 52;
1958 else if (cur == '+')
1959 cur = 62;
1960 else if (cur == '/')
1961 cur = 63;
1962 else if (cur == '.')
1963 cur = 0;
1964 else if (cur == '=') /*no op , end of the base64 stream */
1965 is_end = 1;
1966 else {
1967 is_ignore = 1;
1968 if (nbintmp == 0)
1969 inblk = incur;
1970 }
1971
1972 if (!is_ignore) {
1973 int nbouttmp = 3;
1974 int is_break = 0;
1975
1976 if (is_end) {
1977 if (nbintmp == 0)
1978 break;
1979 if ((nbintmp == 1) || (nbintmp == 2))
1980 nbouttmp = 1;
1981 else
1982 nbouttmp = 2;
1983 nbintmp = 3;
1984 is_break = 1;
1985 }
1986 intmp[nbintmp++] = cur;
1987 /*
1988 * if intmp is full, push the 4byte sequence as a 3 byte
1989 * sequence out
1990 */
1991 if (nbintmp == 4) {
1992 nbintmp = 0;
1993 outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
1994 outtmp[1] =
1995 ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
1996 outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
1997 if (outcur + 3 >= outmax) {
1998 retval = 2;
1999 break;
2000 }
2001
2002 for (i = 0; i < nbouttmp; i++)
2003 to[outcur++] = outtmp[i];
2004 inblk = incur;
2005 }
2006
2007 if (is_break) {
2008 retval = 0;
2009 break;
2010 }
2011 }
2012 }
2013
2014 *tolen = outcur;
2015 *inlen = inblk;
2016 return (retval);
2017}
2018
2019/*
2020 * Test routine for the xmlBase64Decode function
2021 */
2022#if 0
2023int main(int argc, char **argv) {
2024 char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
2025 char output[100];
2026 char output2[100];
2027 char output3[100];
2028 unsigned long inlen = strlen(input);
2029 unsigned long outlen = 100;
2030 int ret;
2031 unsigned long cons, tmp, tmp2, prod;
2032
2033 /*
2034 * Direct
2035 */
2036 ret = xmlBase64Decode(input, &inlen, output, &outlen);
2037
2038 output[outlen] = 0;
2039 printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output);
2040
2041 /*
2042 * output chunking
2043 */
2044 cons = 0;
2045 prod = 0;
2046 while (cons < inlen) {
2047 tmp = 5;
2048 tmp2 = inlen - cons;
2049
2050 printf("%ld %ld\n", cons, prod);
2051 ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
2052 cons += tmp2;
2053 prod += tmp;
2054 printf("%ld %ld\n", cons, prod);
2055 }
2056 output2[outlen] = 0;
2057 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2);
2058
2059 /*
2060 * input chunking
2061 */
2062 cons = 0;
2063 prod = 0;
2064 while (cons < inlen) {
2065 tmp = 100 - prod;
2066 tmp2 = inlen - cons;
2067 if (tmp2 > 5)
2068 tmp2 = 5;
2069
2070 printf("%ld %ld\n", cons, prod);
2071 ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
2072 cons += tmp2;
2073 prod += tmp;
2074 printf("%ld %ld\n", cons, prod);
2075 }
2076 output3[outlen] = 0;
2077 printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3);
2078 return(0);
2079
2080}
2081#endif