Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xmlreader.c: implements the xmlTextReader streaming node API |
| 3 | * |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 4 | * NOTE: |
| 5 | * XmlTextReader.Normalization Property won't be supported, since |
| 6 | * it makes the parser non compliant to the XML recommendation |
| 7 | * |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 8 | * 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 | |
| 58 | typedef enum { |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 59 | 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 Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 65 | } xmlTextReaderMode; |
| 66 | |
| 67 | typedef 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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 73 | XML_TEXTREADER_BACKTRACK= 4, |
| 74 | XML_TEXTREADER_DONE= 5 |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 75 | } xmlTextReaderState; |
| 76 | |
| 77 | struct _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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 86 | charactersSAXFunc characters; |
| 87 | cdataBlockSAXFunc cdataBlock; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 88 | 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 Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 91 | xmlNodePtr curnode;/* current attribute node */ |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 92 | int depth; /* depth of the current node */ |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 93 | xmlNodePtr faketext;/* fake xmlNs chld */ |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 94 | int wasempty;/* was the last node empty */ |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | #ifdef DEBUG_READER |
| 98 | static void |
| 99 | xmlTextReaderDebug(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 | */ |
| 133 | static void |
| 134 | xmlTextReaderStartElement(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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 142 | if ((reader != NULL) && (reader->startElement != NULL)) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 143 | reader->startElement(ctx, fullname, atts); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 144 | if (ctxt->validate) { |
| 145 | ctxt->valid &= xmlValidatePushElement(&ctxt->vctxt, ctxt->myDoc, |
| 146 | ctxt->node, fullname); |
| 147 | } |
| 148 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 149 | 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 | */ |
| 159 | static void |
| 160 | xmlTextReaderEndElement(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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 167 | if ((reader != NULL) && (reader->endElement != NULL)) { |
| 168 | xmlNodePtr node = ctxt->node; |
| 169 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 170 | reader->endElement(ctx, fullname); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 171 | |
| 172 | if (ctxt->validate) { |
| 173 | ctxt->valid &= xmlValidatePopElement(&ctxt->vctxt, ctxt->myDoc, |
| 174 | node, fullname); |
| 175 | } |
| 176 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 177 | if (reader->state == XML_TEXTREADER_ELEMENT) |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 178 | reader->wasempty = 1; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 179 | else |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 180 | reader->wasempty = 0; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 184 | * 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 | */ |
| 191 | static void |
| 192 | xmlTextReaderCharacters(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 | */ |
| 217 | static void |
| 218 | xmlTextReaderCDataBlock(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 Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 236 | * 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 | */ |
| 244 | static int |
| 245 | xmlTextReaderPushData(xmlTextReaderPtr reader) { |
| 246 | unsigned int cur = reader->cur; |
| 247 | xmlBufferPtr inbuf; |
| 248 | int val; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 249 | int oldstate; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 250 | |
| 251 | if ((reader->input == NULL) || (reader->input->buffer == NULL)) |
| 252 | return(-1); |
| 253 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 254 | oldstate = reader->state; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 255 | 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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 266 | reader->state = oldstate; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 267 | 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 Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 289 | (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE)) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 290 | 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 Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 303 | if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 304 | 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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 316 | 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 Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 322 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 323 | reader->state = oldstate; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 324 | 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 | */ |
| 337 | int |
| 338 | xmlTextReaderRead(xmlTextReaderPtr reader) { |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 339 | int val, olddepth, wasempty; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 340 | 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 Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 352 | if (reader->mode == XML_TEXTREADER_MODE_INITIAL) { |
| 353 | reader->mode = XML_TEXTREADER_MODE_INTERACTIVE; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 354 | /* |
| 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 Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 377 | wasempty = ((reader->wasempty == 1) && (reader->ctxt->node != NULL) && |
| 378 | (reader->ctxt->node->last == reader->node)); |
| 379 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 380 | /* |
| 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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 385 | 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 Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 398 | if (oldstate != XML_TEXTREADER_BACKTRACK) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 399 | 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 Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 404 | reader->state = XML_TEXTREADER_ELEMENT; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 405 | DUMP_READER |
| 406 | return(1); |
| 407 | } |
| 408 | } |
| 409 | if (reader->node->next != NULL) { |
| 410 | if ((oldstate == XML_TEXTREADER_ELEMENT) && |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 411 | (reader->node->type == XML_ELEMENT_NODE) && |
| 412 | (wasempty == 0)) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 413 | 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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 430 | 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 Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 436 | 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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 443 | if (reader->mode != XML_TEXTREADER_DONE) { |
| 444 | val = xmlParseChunk(reader->ctxt, "", 0, 1); |
| 445 | reader->mode = XML_TEXTREADER_DONE; |
| 446 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 447 | 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 Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 466 | /** |
| 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 | */ |
| 474 | int |
| 475 | xmlTextReaderReadState(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 | */ |
| 491 | xmlChar * |
| 492 | xmlTextReaderReadInnerXml(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 | */ |
| 507 | xmlChar * |
| 508 | xmlTextReaderReadOuterXml(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 | */ |
| 523 | xmlChar * |
| 524 | xmlTextReaderReadString(xmlTextReaderPtr reader) { |
| 525 | TODO |
| 526 | return(NULL); |
| 527 | } |
| 528 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 529 | /** |
| 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 | */ |
| 543 | int |
| 544 | xmlTextReaderReadBase64(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 | */ |
| 571 | int |
| 572 | xmlTextReaderReadBinHex(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 Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 585 | /************************************************************************ |
| 586 | * * |
| 587 | * Constructor and destructors * |
| 588 | * * |
| 589 | ************************************************************************/ |
| 590 | /** |
| 591 | * xmlNewTextReader: |
| 592 | * @input: the xmlParserInputBufferPtr used to read data |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 593 | * @URI: the URI information for the source if available |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 594 | * |
| 595 | * Create an xmlTextReader structure fed with @input |
| 596 | * |
| 597 | * Returns the new xmlTextReaderPtr or NULL in case of error |
| 598 | */ |
| 599 | xmlTextReaderPtr |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 600 | xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 601 | 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 Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 626 | ret->characters = ret->sax->characters; |
| 627 | ret->sax->characters = xmlTextReaderCharacters; |
| 628 | ret->cdataBlock = ret->sax->cdataBlock; |
| 629 | ret->sax->cdataBlock = xmlTextReaderCDataBlock; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 630 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 631 | ret->mode = XML_TEXTREADER_MODE_INITIAL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 632 | ret->node = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 633 | ret->curnode = NULL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 634 | val = xmlParserInputBufferRead(input, 4); |
| 635 | if (val >= 4) { |
| 636 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 637 | (const char *) ret->input->buffer->content, 4, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 638 | ret->base = 0; |
| 639 | ret->cur = 4; |
| 640 | } else { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 641 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 642 | ret->base = 0; |
| 643 | ret->cur = 0; |
| 644 | } |
| 645 | ret->ctxt->_private = ret; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 646 | ret->ctxt->linenumbers = 1; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 647 | 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 | */ |
| 660 | xmlTextReaderPtr |
| 661 | xmlNewTextReaderFilename(const char *URI) { |
| 662 | xmlParserInputBufferPtr input; |
| 663 | xmlTextReaderPtr ret; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 664 | char *directory = NULL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 665 | |
| 666 | input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE); |
| 667 | if (input == NULL) |
| 668 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 669 | ret = xmlNewTextReader(input, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 670 | if (ret == NULL) { |
| 671 | xmlFreeParserInputBuffer(input); |
| 672 | return(NULL); |
| 673 | } |
| 674 | ret->allocs |= XML_TEXTREADER_INPUT; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 675 | 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 Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 681 | return(ret); |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * xmlFreeTextReader: |
| 686 | * @reader: the xmlTextReaderPtr |
| 687 | * |
| 688 | * Deallocate all the resources associated to the reader |
| 689 | */ |
| 690 | void |
| 691 | xmlFreeTextReader(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 | } |
| 699 | if (reader->allocs & XML_TEXTREADER_CTXT) |
| 700 | xmlFreeParserCtxt(reader->ctxt); |
| 701 | } |
| 702 | if (reader->sax != NULL) |
| 703 | xmlFree(reader->sax); |
| 704 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) |
| 705 | xmlFreeParserInputBuffer(reader->input); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 706 | if (reader->faketext != NULL) { |
| 707 | xmlFreeNode(reader->faketext); |
| 708 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 709 | xmlFree(reader); |
| 710 | } |
| 711 | |
| 712 | /************************************************************************ |
| 713 | * * |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 714 | * Methods for XmlTextReader * |
| 715 | * * |
| 716 | ************************************************************************/ |
| 717 | /** |
| 718 | * xmlTextReaderClose: |
| 719 | * @reader: the xmlTextReaderPtr used |
| 720 | * |
| 721 | * This method releases any resources allocated by the current instance |
| 722 | * changes the state to Closed and close any underlying input. |
| 723 | * |
| 724 | * Returns 0 or -1 in case of error |
| 725 | */ |
| 726 | int |
| 727 | xmlTextReaderClose(xmlTextReaderPtr reader) { |
| 728 | if (reader == NULL) |
| 729 | return(-1); |
| 730 | reader->node = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 731 | reader->curnode = NULL; |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 732 | reader->mode = XML_TEXTREADER_MODE_CLOSED; |
| 733 | if (reader->ctxt != NULL) { |
| 734 | if (reader->ctxt->myDoc != NULL) { |
| 735 | xmlFreeDoc(reader->ctxt->myDoc); |
| 736 | reader->ctxt->myDoc = NULL; |
| 737 | } |
| 738 | if (reader->allocs & XML_TEXTREADER_CTXT) { |
| 739 | xmlFreeParserCtxt(reader->ctxt); |
| 740 | reader->allocs -= XML_TEXTREADER_CTXT; |
| 741 | } |
| 742 | } |
| 743 | if (reader->sax != NULL) { |
| 744 | xmlFree(reader->sax); |
| 745 | reader->sax = NULL; |
| 746 | } |
| 747 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) { |
| 748 | xmlFreeParserInputBuffer(reader->input); |
| 749 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 750 | } |
| 751 | return(0); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * xmlTextReaderGetAttributeNo: |
| 756 | * @reader: the xmlTextReaderPtr used |
| 757 | * @no: the zero-based index of the attribute relative to the containing element |
| 758 | * |
| 759 | * Provides the value of the attribute with the specified index relative |
| 760 | * to the containing element. |
| 761 | * |
| 762 | * Returns a string containing the value of the specified attribute, or NULL |
| 763 | * in case of error. The string must be deallocated by the caller. |
| 764 | */ |
| 765 | xmlChar * |
| 766 | xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) { |
| 767 | xmlChar *ret; |
| 768 | int i; |
| 769 | xmlAttrPtr cur; |
| 770 | xmlNsPtr ns; |
| 771 | |
| 772 | if (reader == NULL) |
| 773 | return(NULL); |
| 774 | if (reader->node == NULL) |
| 775 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 776 | if (reader->curnode != NULL) |
| 777 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 778 | /* TODO: handle the xmlDecl */ |
| 779 | if (reader->node->type != XML_ELEMENT_NODE) |
| 780 | return(NULL); |
| 781 | |
| 782 | ns = reader->node->nsDef; |
| 783 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 784 | ns = ns->next; |
| 785 | } |
| 786 | if (ns != NULL) |
| 787 | return(xmlStrdup(ns->href)); |
| 788 | |
| 789 | cur = reader->node->properties; |
| 790 | if (cur == NULL) |
| 791 | return(NULL); |
| 792 | for (;i < no;i++) { |
| 793 | cur = cur->next; |
| 794 | if (cur == NULL) |
| 795 | return(NULL); |
| 796 | } |
| 797 | /* TODO walk the DTD if present */ |
| 798 | |
| 799 | ret = xmlNodeListGetString(reader->node->doc, cur->children, 1); |
| 800 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 801 | return(ret); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * xmlTextReaderGetAttribute: |
| 806 | * @reader: the xmlTextReaderPtr used |
| 807 | * @name: the qualified name of the attribute. |
| 808 | * |
| 809 | * Provides the value of the attribute with the specified qualified name. |
| 810 | * |
| 811 | * Returns a string containing the value of the specified attribute, or NULL |
| 812 | * in case of error. The string must be deallocated by the caller. |
| 813 | */ |
| 814 | xmlChar * |
| 815 | xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 816 | xmlChar *prefix = NULL; |
| 817 | xmlChar *localname; |
| 818 | xmlNsPtr ns; |
| 819 | xmlChar *ret = NULL; |
| 820 | |
| 821 | if ((reader == NULL) || (name == NULL)) |
| 822 | return(NULL); |
| 823 | if (reader->node == NULL) |
| 824 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 825 | if (reader->curnode != NULL) |
| 826 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 827 | |
| 828 | /* TODO: handle the xmlDecl */ |
| 829 | if (reader->node->type != XML_ELEMENT_NODE) |
| 830 | return(NULL); |
| 831 | |
| 832 | localname = xmlSplitQName2(name, &prefix); |
| 833 | if (localname == NULL) |
| 834 | return(xmlGetProp(reader->node, name)); |
| 835 | |
| 836 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 837 | if (ns != NULL) |
| 838 | ret = xmlGetNsProp(reader->node, localname, ns->href); |
| 839 | |
| 840 | if (localname != NULL) |
| 841 | xmlFree(localname); |
| 842 | if (prefix != NULL) |
| 843 | xmlFree(prefix); |
| 844 | return(ret); |
| 845 | } |
| 846 | |
| 847 | |
| 848 | /** |
| 849 | * xmlTextReaderGetAttributeNs: |
| 850 | * @reader: the xmlTextReaderPtr used |
| 851 | * @localName: the local name of the attribute. |
| 852 | * @namespaceURI: the namespace URI of the attribute. |
| 853 | * |
| 854 | * Provides the value of the specified attribute |
| 855 | * |
| 856 | * Returns a string containing the value of the specified attribute, or NULL |
| 857 | * in case of error. The string must be deallocated by the caller. |
| 858 | */ |
| 859 | xmlChar * |
| 860 | xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName, |
| 861 | const xmlChar *namespaceURI) { |
| 862 | if ((reader == NULL) || (localName == NULL)) |
| 863 | return(NULL); |
| 864 | if (reader->node == NULL) |
| 865 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 866 | if (reader->curnode != NULL) |
| 867 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 868 | |
| 869 | /* TODO: handle the xmlDecl */ |
| 870 | if (reader->node->type != XML_ELEMENT_NODE) |
| 871 | return(NULL); |
| 872 | |
| 873 | return(xmlGetNsProp(reader->node, localName, namespaceURI)); |
| 874 | } |
| 875 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 876 | /** |
| 877 | * xmlTextReaderGetRemainder: |
| 878 | * @reader: the xmlTextReaderPtr used |
| 879 | * |
| 880 | * Method to get the remainder of the buffered XML. this method stops the |
| 881 | * parser, set its state to End Of File and return the input stream with |
| 882 | * what is left that the parser did not use. |
| 883 | * |
| 884 | * Returns the xmlParserInputBufferPtr attached to the XML or NULL |
| 885 | * in case of error. |
| 886 | */ |
| 887 | xmlParserInputBufferPtr |
| 888 | xmlTextReaderGetRemainder(xmlTextReaderPtr reader) { |
| 889 | xmlParserInputBufferPtr ret = NULL; |
| 890 | |
| 891 | if (reader == NULL) |
| 892 | return(NULL); |
| 893 | if (reader->node == NULL) |
| 894 | return(NULL); |
| 895 | |
| 896 | reader->node = NULL; |
| 897 | reader->curnode = NULL; |
| 898 | reader->mode = XML_TEXTREADER_MODE_EOF; |
| 899 | if (reader->ctxt != NULL) { |
| 900 | if (reader->ctxt->myDoc != NULL) { |
| 901 | xmlFreeDoc(reader->ctxt->myDoc); |
| 902 | reader->ctxt->myDoc = NULL; |
| 903 | } |
| 904 | if (reader->allocs & XML_TEXTREADER_CTXT) { |
| 905 | xmlFreeParserCtxt(reader->ctxt); |
| 906 | reader->allocs -= XML_TEXTREADER_CTXT; |
| 907 | } |
| 908 | } |
| 909 | if (reader->sax != NULL) { |
| 910 | xmlFree(reader->sax); |
| 911 | reader->sax = NULL; |
| 912 | } |
| 913 | if (reader->allocs & XML_TEXTREADER_INPUT) { |
| 914 | ret = reader->input; |
| 915 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 916 | } else { |
| 917 | /* |
| 918 | * Hum, one may need to duplicate the data structure because |
| 919 | * without reference counting the input may be freed twice: |
| 920 | * - by the layer which allocated it. |
| 921 | * - by the layer to which would have been returned to. |
| 922 | */ |
| 923 | TODO |
| 924 | return(NULL); |
| 925 | } |
| 926 | return(ret); |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * xmlTextReaderLookupNamespace: |
| 931 | * @reader: the xmlTextReaderPtr used |
| 932 | * @prefix: the prefix whose namespace URI is to be resolved. To return |
| 933 | * the default namespace, specify NULL |
| 934 | * |
| 935 | * Resolves a namespace prefix in the scope of the current element. |
| 936 | * |
| 937 | * Returns a string containing the namespace URI to which the prefix maps |
| 938 | * or NULL in case of error. The string must be deallocated by the caller. |
| 939 | */ |
| 940 | xmlChar * |
| 941 | xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) { |
| 942 | xmlNsPtr ns; |
| 943 | |
| 944 | if (reader == NULL) |
| 945 | return(NULL); |
| 946 | if (reader->node == NULL) |
| 947 | return(NULL); |
| 948 | |
| 949 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 950 | if (ns == NULL) |
| 951 | return(NULL); |
| 952 | return(xmlStrdup(ns->href)); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * xmlTextReaderMoveToAttributeNo: |
| 957 | * @reader: the xmlTextReaderPtr used |
| 958 | * @no: the zero-based index of the attribute relative to the containing |
| 959 | * element. |
| 960 | * |
| 961 | * Moves the position of the current instance to the attribute with |
| 962 | * the specified index relative to the containing element. |
| 963 | * |
| 964 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 965 | */ |
| 966 | int |
| 967 | xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) { |
| 968 | int i; |
| 969 | xmlAttrPtr cur; |
| 970 | xmlNsPtr ns; |
| 971 | |
| 972 | if (reader == NULL) |
| 973 | return(-1); |
| 974 | if (reader->node == NULL) |
| 975 | return(-1); |
| 976 | /* TODO: handle the xmlDecl */ |
| 977 | if (reader->node->type != XML_ELEMENT_NODE) |
| 978 | return(-1); |
| 979 | |
| 980 | reader->curnode = NULL; |
| 981 | |
| 982 | ns = reader->node->nsDef; |
| 983 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 984 | ns = ns->next; |
| 985 | } |
| 986 | if (ns != NULL) { |
| 987 | reader->curnode = (xmlNodePtr) ns; |
| 988 | return(1); |
| 989 | } |
| 990 | |
| 991 | cur = reader->node->properties; |
| 992 | if (cur == NULL) |
| 993 | return(0); |
| 994 | for (;i < no;i++) { |
| 995 | cur = cur->next; |
| 996 | if (cur == NULL) |
| 997 | return(0); |
| 998 | } |
| 999 | /* TODO walk the DTD if present */ |
| 1000 | |
| 1001 | reader->curnode = (xmlNodePtr) cur; |
| 1002 | return(1); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * xmlTextReaderMoveToAttribute: |
| 1007 | * @reader: the xmlTextReaderPtr used |
| 1008 | * @name: the qualified name of the attribute. |
| 1009 | * |
| 1010 | * Moves the position of the current instance to the attribute with |
| 1011 | * the specified qualified name. |
| 1012 | * |
| 1013 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1014 | */ |
| 1015 | int |
| 1016 | xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 1017 | xmlChar *prefix = NULL; |
| 1018 | xmlChar *localname; |
| 1019 | xmlNsPtr ns; |
| 1020 | xmlAttrPtr prop; |
| 1021 | |
| 1022 | if ((reader == NULL) || (name == NULL)) |
| 1023 | return(-1); |
| 1024 | if (reader->node == NULL) |
| 1025 | return(-1); |
| 1026 | |
| 1027 | /* TODO: handle the xmlDecl */ |
| 1028 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1029 | return(0); |
| 1030 | |
| 1031 | localname = xmlSplitQName2(name, &prefix); |
| 1032 | if (localname == NULL) { |
| 1033 | /* |
| 1034 | * Namespace default decl |
| 1035 | */ |
| 1036 | if (xmlStrEqual(name, BAD_CAST "xmlns")) { |
| 1037 | ns = reader->node->nsDef; |
| 1038 | while (ns != NULL) { |
| 1039 | if (ns->prefix == NULL) { |
| 1040 | reader->curnode = (xmlNodePtr) ns; |
| 1041 | return(1); |
| 1042 | } |
| 1043 | ns = ns->next; |
| 1044 | } |
| 1045 | return(0); |
| 1046 | } |
| 1047 | |
| 1048 | prop = reader->node->properties; |
| 1049 | while (prop != NULL) { |
| 1050 | /* |
| 1051 | * One need to have |
| 1052 | * - same attribute names |
| 1053 | * - and the attribute carrying that namespace |
| 1054 | */ |
| 1055 | if ((xmlStrEqual(prop->name, name)) && |
| 1056 | ((prop->ns == NULL) || (prop->ns->prefix == NULL))) { |
| 1057 | reader->curnode = (xmlNodePtr) prop; |
| 1058 | return(1); |
| 1059 | } |
| 1060 | prop = prop->next; |
| 1061 | } |
| 1062 | return(0); |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Namespace default decl |
| 1067 | */ |
| 1068 | if (xmlStrEqual(prefix, BAD_CAST "xmlns")) { |
| 1069 | ns = reader->node->nsDef; |
| 1070 | while (ns != NULL) { |
| 1071 | if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) { |
| 1072 | reader->curnode = (xmlNodePtr) ns; |
| 1073 | goto found; |
| 1074 | } |
| 1075 | ns = ns->next; |
| 1076 | } |
| 1077 | goto not_found; |
| 1078 | } |
| 1079 | prop = reader->node->properties; |
| 1080 | while (prop != NULL) { |
| 1081 | /* |
| 1082 | * One need to have |
| 1083 | * - same attribute names |
| 1084 | * - and the attribute carrying that namespace |
| 1085 | */ |
| 1086 | if ((xmlStrEqual(prop->name, localname)) && |
| 1087 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) { |
| 1088 | reader->curnode = (xmlNodePtr) prop; |
| 1089 | goto found; |
| 1090 | } |
| 1091 | prop = prop->next; |
| 1092 | } |
| 1093 | not_found: |
| 1094 | if (localname != NULL) |
| 1095 | xmlFree(localname); |
| 1096 | if (prefix != NULL) |
| 1097 | xmlFree(prefix); |
| 1098 | return(0); |
| 1099 | |
| 1100 | found: |
| 1101 | if (localname != NULL) |
| 1102 | xmlFree(localname); |
| 1103 | if (prefix != NULL) |
| 1104 | xmlFree(prefix); |
| 1105 | return(1); |
| 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * xmlTextReaderMoveToAttributeNs: |
| 1110 | * @reader: the xmlTextReaderPtr used |
| 1111 | * @localName: the local name of the attribute. |
| 1112 | * @namespaceURI: the namespace URI of the attribute. |
| 1113 | * |
| 1114 | * Moves the position of the current instance to the attribute with the |
| 1115 | * specified local name and namespace URI. |
| 1116 | * |
| 1117 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1118 | */ |
| 1119 | int |
| 1120 | xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader, |
| 1121 | const xmlChar *localName, const xmlChar *namespaceURI) { |
| 1122 | xmlAttrPtr prop; |
| 1123 | xmlNodePtr node; |
| 1124 | |
| 1125 | if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL)) |
| 1126 | return(-1); |
| 1127 | if (reader->node == NULL) |
| 1128 | return(-1); |
| 1129 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1130 | return(0); |
| 1131 | node = reader->node; |
| 1132 | |
| 1133 | /* |
| 1134 | * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no |
| 1135 | * namespace name associated to "xmlns" |
| 1136 | */ |
| 1137 | prop = node->properties; |
| 1138 | while (prop != NULL) { |
| 1139 | /* |
| 1140 | * One need to have |
| 1141 | * - same attribute names |
| 1142 | * - and the attribute carrying that namespace |
| 1143 | */ |
| 1144 | if (xmlStrEqual(prop->name, localName) && |
| 1145 | ((prop->ns != NULL) && |
| 1146 | (xmlStrEqual(prop->ns->href, namespaceURI)))) { |
| 1147 | reader->curnode = (xmlNodePtr) prop; |
| 1148 | return(1); |
| 1149 | } |
| 1150 | prop = prop->next; |
| 1151 | } |
| 1152 | return(0); |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * xmlTextReaderMoveToFirstAttribute: |
| 1157 | * @reader: the xmlTextReaderPtr used |
| 1158 | * |
| 1159 | * Moves the position of the current instance to the first attribute |
| 1160 | * associated with the current node. |
| 1161 | * |
| 1162 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1163 | */ |
| 1164 | int |
| 1165 | xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) { |
| 1166 | if (reader == NULL) |
| 1167 | return(-1); |
| 1168 | if (reader->node == NULL) |
| 1169 | return(-1); |
| 1170 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1171 | return(0); |
| 1172 | |
| 1173 | if (reader->node->nsDef != NULL) { |
| 1174 | reader->curnode = (xmlNodePtr) reader->node->nsDef; |
| 1175 | return(1); |
| 1176 | } |
| 1177 | if (reader->node->properties != NULL) { |
| 1178 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 1179 | return(1); |
| 1180 | } |
| 1181 | return(0); |
| 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * xmlTextReaderMoveToNextAttribute: |
| 1186 | * @reader: the xmlTextReaderPtr used |
| 1187 | * |
| 1188 | * Moves the position of the current instance to the next attribute |
| 1189 | * associated with the current node. |
| 1190 | * |
| 1191 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1192 | */ |
| 1193 | int |
| 1194 | xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) { |
| 1195 | if (reader == NULL) |
| 1196 | return(-1); |
| 1197 | if (reader->node == NULL) |
| 1198 | return(-1); |
| 1199 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1200 | return(0); |
| 1201 | if (reader->curnode == NULL) |
| 1202 | return(xmlTextReaderMoveToFirstAttribute(reader)); |
| 1203 | |
| 1204 | if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 1205 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 1206 | if (ns->next != NULL) { |
| 1207 | reader->curnode = (xmlNodePtr) ns->next; |
| 1208 | return(1); |
| 1209 | } |
| 1210 | if (reader->node->properties != NULL) { |
| 1211 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 1212 | return(1); |
| 1213 | } |
| 1214 | return(0); |
| 1215 | } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) && |
| 1216 | (reader->curnode->next != NULL)) { |
| 1217 | reader->curnode = reader->curnode->next; |
| 1218 | return(1); |
| 1219 | } |
| 1220 | return(0); |
| 1221 | } |
| 1222 | |
| 1223 | /** |
| 1224 | * xmlTextReaderMoveToElement: |
| 1225 | * @reader: the xmlTextReaderPtr used |
| 1226 | * |
| 1227 | * Moves the position of the current instance to the node that |
| 1228 | * contains the current Attribute node. |
| 1229 | * |
| 1230 | * Returns 1 in case of success, -1 in case of error, 0 if not moved |
| 1231 | */ |
| 1232 | int |
| 1233 | xmlTextReaderMoveToElement(xmlTextReaderPtr reader) { |
| 1234 | if (reader == NULL) |
| 1235 | return(-1); |
| 1236 | if (reader->node == NULL) |
| 1237 | return(-1); |
| 1238 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1239 | return(0); |
| 1240 | if (reader->curnode != NULL) { |
| 1241 | reader->curnode = NULL; |
| 1242 | return(1); |
| 1243 | } |
| 1244 | return(0); |
| 1245 | } |
| 1246 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1247 | /** |
| 1248 | * xmlTextReaderReadAttributeValue: |
| 1249 | * @reader: the xmlTextReaderPtr used |
| 1250 | * |
| 1251 | * Parses an attribute value into one or more Text and EntityReference nodes. |
| 1252 | * |
| 1253 | * Returns 1 in case of success, 0 if the reader was not positionned on an |
| 1254 | * ttribute node or all the attribute values have been read, or -1 |
| 1255 | * in case of error. |
| 1256 | */ |
| 1257 | int |
| 1258 | xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) { |
| 1259 | if (reader == NULL) |
| 1260 | return(-1); |
| 1261 | if (reader->node == NULL) |
| 1262 | return(-1); |
| 1263 | if (reader->curnode == NULL) |
| 1264 | return(0); |
| 1265 | if (reader->curnode->type == XML_ATTRIBUTE_NODE) { |
| 1266 | if (reader->curnode->children == NULL) |
| 1267 | return(0); |
| 1268 | reader->curnode = reader->curnode->children; |
| 1269 | } else if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 1270 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 1271 | |
| 1272 | if (reader->faketext == NULL) { |
| 1273 | reader->faketext = xmlNewDocText(reader->node->doc, |
| 1274 | ns->href); |
| 1275 | } else { |
| 1276 | if (reader->faketext->content != NULL) |
| 1277 | xmlFree(reader->faketext->content); |
| 1278 | reader->faketext->content = xmlStrdup(ns->href); |
| 1279 | } |
| 1280 | reader->curnode = reader->faketext; |
| 1281 | } else { |
| 1282 | if (reader->curnode->next == NULL) |
| 1283 | return(0); |
| 1284 | reader->curnode = reader->curnode->next; |
| 1285 | } |
| 1286 | return(1); |
| 1287 | } |
| 1288 | |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1289 | /************************************************************************ |
| 1290 | * * |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1291 | * Acces API to the current node * |
| 1292 | * * |
| 1293 | ************************************************************************/ |
| 1294 | /** |
| 1295 | * xmlTextReaderAttributeCount: |
| 1296 | * @reader: the xmlTextReaderPtr used |
| 1297 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1298 | * Provides the number of attributes of the current node |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1299 | * |
| 1300 | * Returns 0 i no attributes, -1 in case of error or the attribute count |
| 1301 | */ |
| 1302 | int |
| 1303 | xmlTextReaderAttributeCount(xmlTextReaderPtr reader) { |
| 1304 | int ret; |
| 1305 | xmlAttrPtr attr; |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1306 | xmlNsPtr ns; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1307 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (reader == NULL) |
| 1310 | return(-1); |
| 1311 | if (reader->node == NULL) |
| 1312 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1313 | |
| 1314 | if (reader->curnode != NULL) |
| 1315 | node = reader->curnode; |
| 1316 | else |
| 1317 | node = reader->node; |
| 1318 | |
| 1319 | if (node->type != XML_ELEMENT_NODE) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1320 | return(0); |
| 1321 | if ((reader->state == XML_TEXTREADER_END) || |
| 1322 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
| 1323 | return(0); |
| 1324 | ret = 0; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1325 | attr = node->properties; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1326 | while (attr != NULL) { |
| 1327 | ret++; |
| 1328 | attr = attr->next; |
| 1329 | } |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1330 | ns = node->nsDef; |
| 1331 | while (ns != NULL) { |
| 1332 | ret++; |
| 1333 | ns = ns->next; |
| 1334 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1335 | return(ret); |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * xmlTextReaderNodeType: |
| 1340 | * @reader: the xmlTextReaderPtr used |
| 1341 | * |
| 1342 | * Get the node type of the current node |
| 1343 | * Reference: |
| 1344 | * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html |
| 1345 | * |
| 1346 | * Returns the xmlNodeType of the current node or -1 in case of error |
| 1347 | */ |
| 1348 | int |
| 1349 | xmlTextReaderNodeType(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1350 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1351 | if (reader == NULL) |
| 1352 | return(-1); |
| 1353 | if (reader->node == NULL) |
| 1354 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1355 | if (reader->curnode != NULL) |
| 1356 | node = reader->curnode; |
| 1357 | else |
| 1358 | node = reader->node; |
| 1359 | switch (node->type) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1360 | case XML_ELEMENT_NODE: |
| 1361 | if ((reader->state == XML_TEXTREADER_END) || |
| 1362 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
| 1363 | return(15); |
| 1364 | return(1); |
| 1365 | case XML_ATTRIBUTE_NODE: |
| 1366 | return(2); |
| 1367 | case XML_TEXT_NODE: |
| 1368 | return(3); /* TODO: SignificantWhitespace == 14 Whitespace == 13 */ |
| 1369 | case XML_CDATA_SECTION_NODE: |
| 1370 | return(4); |
| 1371 | case XML_ENTITY_REF_NODE: |
| 1372 | return(5); |
| 1373 | case XML_ENTITY_NODE: |
| 1374 | return(6); |
| 1375 | case XML_PI_NODE: |
| 1376 | return(7); |
| 1377 | case XML_COMMENT_NODE: |
| 1378 | return(8); |
| 1379 | case XML_DOCUMENT_NODE: |
| 1380 | case XML_HTML_DOCUMENT_NODE: |
| 1381 | #ifdef LIBXML_DOCB_ENABLED |
| 1382 | case XML_DOCB_DOCUMENT_NODE: |
| 1383 | #endif |
| 1384 | return(9); |
| 1385 | case XML_DOCUMENT_FRAG_NODE: |
| 1386 | return(11); |
| 1387 | case XML_NOTATION_NODE: |
| 1388 | return(12); |
| 1389 | case XML_DOCUMENT_TYPE_NODE: |
| 1390 | case XML_DTD_NODE: |
| 1391 | return(10); |
| 1392 | |
| 1393 | case XML_ELEMENT_DECL: |
| 1394 | case XML_ATTRIBUTE_DECL: |
| 1395 | case XML_ENTITY_DECL: |
| 1396 | case XML_NAMESPACE_DECL: |
| 1397 | case XML_XINCLUDE_START: |
| 1398 | case XML_XINCLUDE_END: |
| 1399 | return(0); |
| 1400 | } |
| 1401 | return(-1); |
| 1402 | } |
| 1403 | |
| 1404 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1405 | * xmlTextReaderIsEmptyElement: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1406 | * @reader: the xmlTextReaderPtr used |
| 1407 | * |
| 1408 | * Check if the current node is empty |
| 1409 | * |
| 1410 | * Returns 1 if empty, 0 if not and -1 in case of error |
| 1411 | */ |
| 1412 | int |
| 1413 | xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) { |
| 1414 | if ((reader == NULL) || (reader->node == NULL)) |
| 1415 | return(-1); |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 1416 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1417 | return(0); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1418 | if (reader->node->children != NULL) |
| 1419 | return(0); |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 1420 | if (reader->node != reader->ctxt->node) |
| 1421 | return(1); |
| 1422 | if ((reader->ctxt->node != NULL) && |
| 1423 | (reader->node == reader->ctxt->node->last) && |
| 1424 | (reader->wasempty == 1)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1425 | return(1); |
| 1426 | return(0); |
| 1427 | } |
| 1428 | |
| 1429 | /** |
| 1430 | * xmlTextReaderLocalName: |
| 1431 | * @reader: the xmlTextReaderPtr used |
| 1432 | * |
| 1433 | * The local name of the node. |
| 1434 | * |
| 1435 | * Returns the local name or NULL if not available |
| 1436 | */ |
| 1437 | xmlChar * |
| 1438 | xmlTextReaderLocalName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1439 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1440 | if ((reader == NULL) || (reader->node == NULL)) |
| 1441 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1442 | if (reader->curnode != NULL) |
| 1443 | node = reader->curnode; |
| 1444 | else |
| 1445 | node = reader->node; |
| 1446 | if (node->type == XML_NAMESPACE_DECL) { |
| 1447 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1448 | if (ns->prefix == NULL) |
| 1449 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 1450 | else |
| 1451 | return(xmlStrdup(ns->prefix)); |
| 1452 | } |
| 1453 | if ((node->type != XML_ELEMENT_NODE) && |
| 1454 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1455 | return(xmlTextReaderName(reader)); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1456 | return(xmlStrdup(node->name)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * xmlTextReaderName: |
| 1461 | * @reader: the xmlTextReaderPtr used |
| 1462 | * |
| 1463 | * The qualified name of the node, equal to Prefix :LocalName. |
| 1464 | * |
| 1465 | * Returns the local name or NULL if not available |
| 1466 | */ |
| 1467 | xmlChar * |
| 1468 | xmlTextReaderName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1469 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1470 | xmlChar *ret; |
| 1471 | |
| 1472 | if ((reader == NULL) || (reader->node == NULL)) |
| 1473 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1474 | if (reader->curnode != NULL) |
| 1475 | node = reader->curnode; |
| 1476 | else |
| 1477 | node = reader->node; |
| 1478 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1479 | case XML_ELEMENT_NODE: |
| 1480 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1481 | if ((node->ns == NULL) || |
| 1482 | (node->ns->prefix == NULL)) |
| 1483 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1484 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1485 | ret = xmlStrdup(node->ns->prefix); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1486 | ret = xmlStrcat(ret, BAD_CAST ":"); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1487 | ret = xmlStrcat(ret, node->name); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1488 | return(ret); |
| 1489 | case XML_TEXT_NODE: |
| 1490 | return(xmlStrdup(BAD_CAST "#text")); |
| 1491 | case XML_CDATA_SECTION_NODE: |
| 1492 | return(xmlStrdup(BAD_CAST "#cdata-section")); |
| 1493 | case XML_ENTITY_NODE: |
| 1494 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1495 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1496 | case XML_PI_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1497 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1498 | case XML_COMMENT_NODE: |
| 1499 | return(xmlStrdup(BAD_CAST "#comment")); |
| 1500 | case XML_DOCUMENT_NODE: |
| 1501 | case XML_HTML_DOCUMENT_NODE: |
| 1502 | #ifdef LIBXML_DOCB_ENABLED |
| 1503 | case XML_DOCB_DOCUMENT_NODE: |
| 1504 | #endif |
| 1505 | return(xmlStrdup(BAD_CAST "#document")); |
| 1506 | case XML_DOCUMENT_FRAG_NODE: |
| 1507 | return(xmlStrdup(BAD_CAST "#document-fragment")); |
| 1508 | case XML_NOTATION_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1509 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1510 | case XML_DOCUMENT_TYPE_NODE: |
| 1511 | case XML_DTD_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1512 | return(xmlStrdup(node->name)); |
| 1513 | case XML_NAMESPACE_DECL: { |
| 1514 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1515 | |
| 1516 | ret = xmlStrdup(BAD_CAST "xmlns"); |
| 1517 | if (ns->prefix == NULL) |
| 1518 | return(ret); |
| 1519 | ret = xmlStrcat(ret, BAD_CAST ":"); |
| 1520 | ret = xmlStrcat(ret, ns->prefix); |
| 1521 | return(ret); |
| 1522 | } |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1523 | |
| 1524 | case XML_ELEMENT_DECL: |
| 1525 | case XML_ATTRIBUTE_DECL: |
| 1526 | case XML_ENTITY_DECL: |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1527 | case XML_XINCLUDE_START: |
| 1528 | case XML_XINCLUDE_END: |
| 1529 | return(NULL); |
| 1530 | } |
| 1531 | return(NULL); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /** |
| 1535 | * xmlTextReaderPrefix: |
| 1536 | * @reader: the xmlTextReaderPtr used |
| 1537 | * |
| 1538 | * A shorthand reference to the namespace associated with the node. |
| 1539 | * |
| 1540 | * Returns the prefix or NULL if not available |
| 1541 | */ |
| 1542 | xmlChar * |
| 1543 | xmlTextReaderPrefix(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1544 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1545 | if ((reader == NULL) || (reader->node == NULL)) |
| 1546 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1547 | if (reader->curnode != NULL) |
| 1548 | node = reader->curnode; |
| 1549 | else |
| 1550 | node = reader->node; |
| 1551 | if (node->type == XML_NAMESPACE_DECL) { |
| 1552 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1553 | if (ns->prefix == NULL) |
| 1554 | return(NULL); |
| 1555 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 1556 | } |
| 1557 | if ((node->type != XML_ELEMENT_NODE) && |
| 1558 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1559 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1560 | if ((node->ns != NULL) || (node->ns->prefix != NULL)) |
| 1561 | return(xmlStrdup(node->ns->prefix)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1562 | return(NULL); |
| 1563 | } |
| 1564 | |
| 1565 | /** |
| 1566 | * xmlTextReaderNamespaceUri: |
| 1567 | * @reader: the xmlTextReaderPtr used |
| 1568 | * |
| 1569 | * The URI defining the namespace associated with the node. |
| 1570 | * |
| 1571 | * Returns the namespace URI or NULL if not available |
| 1572 | */ |
| 1573 | xmlChar * |
| 1574 | xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1575 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1576 | if ((reader == NULL) || (reader->node == NULL)) |
| 1577 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1578 | if (reader->curnode != NULL) |
| 1579 | node = reader->curnode; |
| 1580 | else |
| 1581 | node = reader->node; |
| 1582 | if (node->type == XML_NAMESPACE_DECL) { |
| 1583 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1584 | return(xmlStrdup(ns->href)); |
| 1585 | } |
| 1586 | if ((node->type != XML_ELEMENT_NODE) && |
| 1587 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1588 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1589 | if (node->ns != NULL) |
| 1590 | return(xmlStrdup(node->ns->href)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1591 | return(NULL); |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * xmlTextReaderBaseUri: |
| 1596 | * @reader: the xmlTextReaderPtr used |
| 1597 | * |
| 1598 | * The base URI of the node. |
| 1599 | * |
| 1600 | * Returns the base URI or NULL if not available |
| 1601 | */ |
| 1602 | xmlChar * |
| 1603 | xmlTextReaderBaseUri(xmlTextReaderPtr reader) { |
| 1604 | if ((reader == NULL) || (reader->node == NULL)) |
| 1605 | return(NULL); |
| 1606 | return(xmlNodeGetBase(NULL, reader->node)); |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * xmlTextReaderDepth: |
| 1611 | * @reader: the xmlTextReaderPtr used |
| 1612 | * |
| 1613 | * The depth of the node in the tree. |
| 1614 | * |
| 1615 | * Returns the depth or -1 in case of error |
| 1616 | */ |
| 1617 | int |
| 1618 | xmlTextReaderDepth(xmlTextReaderPtr reader) { |
| 1619 | if (reader == NULL) |
| 1620 | return(-1); |
| 1621 | if (reader->node == NULL) |
| 1622 | return(0); |
| 1623 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1624 | if (reader->curnode != NULL) { |
| 1625 | if ((reader->curnode->type == XML_ATTRIBUTE_NODE) || |
| 1626 | (reader->curnode->type == XML_NAMESPACE_DECL)) |
| 1627 | return(reader->depth + 1); |
| 1628 | return(reader->depth + 2); |
| 1629 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1630 | return(reader->depth); |
| 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * xmlTextReaderHasAttributes: |
| 1635 | * @reader: the xmlTextReaderPtr used |
| 1636 | * |
| 1637 | * Whether the node has attributes. |
| 1638 | * |
| 1639 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 1640 | */ |
| 1641 | int |
| 1642 | xmlTextReaderHasAttributes(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1643 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1644 | if (reader == NULL) |
| 1645 | return(-1); |
| 1646 | if (reader->node == NULL) |
| 1647 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1648 | if (reader->curnode != NULL) |
| 1649 | node = reader->curnode; |
| 1650 | else |
| 1651 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1652 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1653 | if ((node->type == XML_ELEMENT_NODE) && |
| 1654 | (node->properties != NULL)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1655 | return(1); |
| 1656 | /* TODO: handle the xmlDecl */ |
| 1657 | return(0); |
| 1658 | } |
| 1659 | |
| 1660 | /** |
| 1661 | * xmlTextReaderHasValue: |
| 1662 | * @reader: the xmlTextReaderPtr used |
| 1663 | * |
| 1664 | * Whether the node can have a text value. |
| 1665 | * |
| 1666 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 1667 | */ |
| 1668 | int |
| 1669 | xmlTextReaderHasValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1670 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1671 | if (reader == NULL) |
| 1672 | return(-1); |
| 1673 | if (reader->node == NULL) |
| 1674 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1675 | if (reader->curnode != NULL) |
| 1676 | node = reader->curnode; |
| 1677 | else |
| 1678 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1679 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1680 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1681 | case XML_ATTRIBUTE_NODE: |
| 1682 | case XML_TEXT_NODE: |
| 1683 | case XML_CDATA_SECTION_NODE: |
| 1684 | case XML_PI_NODE: |
| 1685 | case XML_COMMENT_NODE: |
| 1686 | return(1); |
| 1687 | default: |
| 1688 | return(0); |
| 1689 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1690 | return(0); |
| 1691 | } |
| 1692 | |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1693 | /** |
| 1694 | * xmlTextReaderValue: |
| 1695 | * @reader: the xmlTextReaderPtr used |
| 1696 | * |
| 1697 | * Provides the text value of the node if present |
| 1698 | * |
| 1699 | * Returns the string or NULL if not available. The retsult must be deallocated |
| 1700 | * with xmlFree() |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1701 | */ |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1702 | xmlChar * |
| 1703 | xmlTextReaderValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1704 | xmlNodePtr node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1705 | if (reader == NULL) |
| 1706 | return(NULL); |
| 1707 | if (reader->node == NULL) |
| 1708 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1709 | if (reader->curnode != NULL) |
| 1710 | node = reader->curnode; |
| 1711 | else |
| 1712 | node = reader->node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1713 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1714 | switch (node->type) { |
| 1715 | case XML_NAMESPACE_DECL: |
| 1716 | return(xmlStrdup(((xmlNsPtr) node)->href)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1717 | case XML_ATTRIBUTE_NODE:{ |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1718 | xmlAttrPtr attr = (xmlAttrPtr) node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1719 | |
| 1720 | if (attr->parent != NULL) |
| 1721 | return (xmlNodeListGetString |
| 1722 | (attr->parent->doc, attr->children, 1)); |
| 1723 | else |
| 1724 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 1725 | break; |
| 1726 | } |
| 1727 | case XML_TEXT_NODE: |
| 1728 | case XML_CDATA_SECTION_NODE: |
| 1729 | case XML_PI_NODE: |
| 1730 | case XML_COMMENT_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1731 | if (node->content != NULL) |
| 1732 | return (xmlStrdup(node->content)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1733 | default: |
| 1734 | return(NULL); |
| 1735 | } |
| 1736 | return(NULL); |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * xmlTextReaderIsDefault: |
| 1741 | * @reader: the xmlTextReaderPtr used |
| 1742 | * |
| 1743 | * Whether an Attribute node was generated from the default value |
| 1744 | * defined in the DTD or schema. |
| 1745 | * |
| 1746 | * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error |
| 1747 | */ |
| 1748 | int |
| 1749 | xmlTextReaderIsDefault(xmlTextReaderPtr reader) { |
| 1750 | if (reader == NULL) |
| 1751 | return(-1); |
| 1752 | return(0); |
| 1753 | } |
| 1754 | |
| 1755 | /** |
| 1756 | * xmlTextReaderQuoteChar: |
| 1757 | * @reader: the xmlTextReaderPtr used |
| 1758 | * |
| 1759 | * The quotation mark character used to enclose the value of an attribute. |
| 1760 | * |
| 1761 | * Returns " or ' and -1 in case of error |
| 1762 | */ |
| 1763 | int |
| 1764 | xmlTextReaderQuoteChar(xmlTextReaderPtr reader) { |
| 1765 | if (reader == NULL) |
| 1766 | return(-1); |
| 1767 | /* TODO maybe lookup the attribute value for " first */ |
| 1768 | return((int) '"'); |
| 1769 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1770 | |
| 1771 | /** |
| 1772 | * xmlTextReaderXmlLang: |
| 1773 | * @reader: the xmlTextReaderPtr used |
| 1774 | * |
| 1775 | * The xml:lang scope within which the node resides. |
| 1776 | * |
| 1777 | * Returns the xml:lang value or NULL if none exists. |
| 1778 | */ |
| 1779 | xmlChar * |
| 1780 | xmlTextReaderXmlLang(xmlTextReaderPtr reader) { |
| 1781 | if (reader == NULL) |
| 1782 | return(NULL); |
| 1783 | if (reader->node == NULL) |
| 1784 | return(NULL); |
| 1785 | return(xmlNodeGetLang(reader->node)); |
| 1786 | } |
| 1787 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1788 | /** |
| 1789 | * xmlTextReaderNormalization: |
| 1790 | * @reader: the xmlTextReaderPtr used |
| 1791 | * |
| 1792 | * The value indicating whether to normalize white space and attribute values. |
| 1793 | * Since attribute value and end of line normalizations are a MUST in the XML |
| 1794 | * specification only the value true is accepted. The broken bahaviour of |
| 1795 | * accepting out of range character entities like � is of course not |
| 1796 | * supported either. |
| 1797 | * |
| 1798 | * Returns 1 or -1 in case of error. |
| 1799 | */ |
| 1800 | int |
| 1801 | xmlTextReaderNormalization(xmlTextReaderPtr reader) { |
| 1802 | if (reader == NULL) |
| 1803 | return(-1); |
| 1804 | return(1); |
| 1805 | } |
| 1806 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1807 | /************************************************************************ |
| 1808 | * * |
| 1809 | * Extensions to the base APIs * |
| 1810 | * * |
| 1811 | ************************************************************************/ |
| 1812 | |
| 1813 | /** |
| 1814 | * xmlTextReaderSetParserProp: |
| 1815 | * @reader: the xmlTextReaderPtr used |
| 1816 | * @prop: the xmlParserProperties to set |
| 1817 | * @value: usually 0 or 1 to (de)activate it |
| 1818 | * |
| 1819 | * Change the parser processing behaviour by changing some of its internal |
| 1820 | * properties. Note that some properties can only be changed before any |
| 1821 | * read has been done. |
| 1822 | * |
| 1823 | * Returns 0 if the call was successful, or -1 in case of error |
| 1824 | */ |
| 1825 | int |
| 1826 | xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) { |
| 1827 | xmlParserProperties p = (xmlParserProperties) prop; |
| 1828 | xmlParserCtxtPtr ctxt; |
| 1829 | |
| 1830 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 1831 | return(-1); |
| 1832 | ctxt = reader->ctxt; |
| 1833 | |
| 1834 | switch (p) { |
| 1835 | case XML_PARSER_LOADDTD: |
| 1836 | if (value != 0) { |
| 1837 | if (ctxt->loadsubset == 0) { |
| 1838 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 1839 | return(-1); |
| 1840 | ctxt->loadsubset = XML_DETECT_IDS; |
| 1841 | } |
| 1842 | } else { |
| 1843 | ctxt->loadsubset = 0; |
| 1844 | } |
| 1845 | return(0); |
| 1846 | case XML_PARSER_DEFAULTATTRS: |
| 1847 | if (value != 0) { |
| 1848 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 1849 | } else { |
| 1850 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 1851 | ctxt->loadsubset -= XML_COMPLETE_ATTRS; |
| 1852 | } |
| 1853 | return(0); |
| 1854 | case XML_PARSER_VALIDATE: |
| 1855 | if (value != 0) { |
| 1856 | ctxt->validate = 1; |
| 1857 | } else { |
| 1858 | ctxt->validate = 0; |
| 1859 | } |
| 1860 | return(0); |
| 1861 | } |
| 1862 | return(-1); |
| 1863 | } |
| 1864 | |
| 1865 | /** |
| 1866 | * xmlTextReaderGetParserProp: |
| 1867 | * @reader: the xmlTextReaderPtr used |
| 1868 | * @prop: the xmlParserProperties to get |
| 1869 | * |
| 1870 | * Read the parser internal property. |
| 1871 | * |
| 1872 | * Returns the value, usually 0 or 1, or -1 in case of error. |
| 1873 | */ |
| 1874 | int |
| 1875 | xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) { |
| 1876 | xmlParserProperties p = (xmlParserProperties) prop; |
| 1877 | xmlParserCtxtPtr ctxt; |
| 1878 | |
| 1879 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 1880 | return(-1); |
| 1881 | ctxt = reader->ctxt; |
| 1882 | |
| 1883 | switch (p) { |
| 1884 | case XML_PARSER_LOADDTD: |
| 1885 | if ((ctxt->loadsubset != 0) || (ctxt->validate != 0)) |
| 1886 | return(1); |
| 1887 | return(0); |
| 1888 | case XML_PARSER_DEFAULTATTRS: |
| 1889 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 1890 | return(1); |
| 1891 | return(0); |
| 1892 | case XML_PARSER_VALIDATE: |
| 1893 | return(ctxt->validate); |
| 1894 | } |
| 1895 | return(-1); |
| 1896 | } |
| 1897 | |
| 1898 | /************************************************************************ |
| 1899 | * * |
| 1900 | * Utilities * |
| 1901 | * * |
| 1902 | ************************************************************************/ |
| 1903 | /** |
| 1904 | * xmlBase64Decode: |
| 1905 | * @in: the input buffer |
| 1906 | * @inlen: the size of the input (in), the size read from it (out) |
| 1907 | * @to: the output buffer |
| 1908 | * @tolen: the size of the output (in), the size written to (out) |
| 1909 | * |
| 1910 | * Base64 decoder, reads from @in and save in @to |
| 1911 | * |
| 1912 | * Returns 0 if all the input was consumer, 1 if the Base64 end was reached, |
| 1913 | * 2 if there wasn't enough space on the output or -1 in case of error. |
| 1914 | */ |
| 1915 | static int |
| 1916 | xmlBase64Decode(const unsigned char *in, unsigned long *inlen, |
| 1917 | unsigned char *to, unsigned long *tolen) { |
| 1918 | unsigned long incur; /* current index in in[] */ |
| 1919 | unsigned long inblk; /* last block index in in[] */ |
| 1920 | unsigned long outcur; /* current index in out[] */ |
| 1921 | unsigned long inmax; /* size of in[] */ |
| 1922 | unsigned long outmax; /* size of out[] */ |
| 1923 | unsigned char cur; /* the current value read from in[] */ |
| 1924 | unsigned char intmp[3], outtmp[4]; /* temporary buffers for the convert */ |
| 1925 | int nbintmp; /* number of byte in intmp[] */ |
| 1926 | int is_ignore; /* cur should be ignored */ |
| 1927 | int is_end = 0; /* the end of the base64 was found */ |
| 1928 | int retval = 1; |
| 1929 | int i; |
| 1930 | |
| 1931 | if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL)) |
| 1932 | return(-1); |
| 1933 | |
| 1934 | incur = 0; |
| 1935 | inblk = 0; |
| 1936 | outcur = 0; |
| 1937 | inmax = *inlen; |
| 1938 | outmax = *tolen; |
| 1939 | nbintmp = 0; |
| 1940 | |
| 1941 | while (1) { |
| 1942 | if (incur >= inmax) |
| 1943 | break; |
| 1944 | cur = in[incur++]; |
| 1945 | is_ignore = 0; |
| 1946 | if ((cur >= 'A') && (cur <= 'Z')) |
| 1947 | cur = cur - 'A'; |
| 1948 | else if ((cur >= 'a') && (cur <= 'z')) |
| 1949 | cur = cur - 'a' + 26; |
| 1950 | else if ((cur >= '0') && (cur <= '9')) |
| 1951 | cur = cur - '0' + 52; |
| 1952 | else if (cur == '+') |
| 1953 | cur = 62; |
| 1954 | else if (cur == '/') |
| 1955 | cur = 63; |
| 1956 | else if (cur == '.') |
| 1957 | cur = 0; |
| 1958 | else if (cur == '=') /*no op , end of the base64 stream */ |
| 1959 | is_end = 1; |
| 1960 | else { |
| 1961 | is_ignore = 1; |
| 1962 | if (nbintmp == 0) |
| 1963 | inblk = incur; |
| 1964 | } |
| 1965 | |
| 1966 | if (!is_ignore) { |
| 1967 | int nbouttmp = 3; |
| 1968 | int is_break = 0; |
| 1969 | |
| 1970 | if (is_end) { |
| 1971 | if (nbintmp == 0) |
| 1972 | break; |
| 1973 | if ((nbintmp == 1) || (nbintmp == 2)) |
| 1974 | nbouttmp = 1; |
| 1975 | else |
| 1976 | nbouttmp = 2; |
| 1977 | nbintmp = 3; |
| 1978 | is_break = 1; |
| 1979 | } |
| 1980 | intmp[nbintmp++] = cur; |
| 1981 | /* |
| 1982 | * if intmp is full, push the 4byte sequence as a 3 byte |
| 1983 | * sequence out |
| 1984 | */ |
| 1985 | if (nbintmp == 4) { |
| 1986 | nbintmp = 0; |
| 1987 | outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4); |
| 1988 | outtmp[1] = |
| 1989 | ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2); |
| 1990 | outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F); |
| 1991 | if (outcur + 3 >= outmax) { |
| 1992 | retval = 2; |
| 1993 | break; |
| 1994 | } |
| 1995 | |
| 1996 | for (i = 0; i < nbouttmp; i++) |
| 1997 | to[outcur++] = outtmp[i]; |
| 1998 | inblk = incur; |
| 1999 | } |
| 2000 | |
| 2001 | if (is_break) { |
| 2002 | retval = 0; |
| 2003 | break; |
| 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | *tolen = outcur; |
| 2009 | *inlen = inblk; |
| 2010 | return (retval); |
| 2011 | } |
| 2012 | |
| 2013 | /* |
| 2014 | * Test routine for the xmlBase64Decode function |
| 2015 | */ |
| 2016 | #if 0 |
| 2017 | int main(int argc, char **argv) { |
| 2018 | char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== "; |
| 2019 | char output[100]; |
| 2020 | char output2[100]; |
| 2021 | char output3[100]; |
| 2022 | unsigned long inlen = strlen(input); |
| 2023 | unsigned long outlen = 100; |
| 2024 | int ret; |
| 2025 | unsigned long cons, tmp, tmp2, prod; |
| 2026 | |
| 2027 | /* |
| 2028 | * Direct |
| 2029 | */ |
| 2030 | ret = xmlBase64Decode(input, &inlen, output, &outlen); |
| 2031 | |
| 2032 | output[outlen] = 0; |
| 2033 | printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output); |
| 2034 | |
| 2035 | /* |
| 2036 | * output chunking |
| 2037 | */ |
| 2038 | cons = 0; |
| 2039 | prod = 0; |
| 2040 | while (cons < inlen) { |
| 2041 | tmp = 5; |
| 2042 | tmp2 = inlen - cons; |
| 2043 | |
| 2044 | printf("%ld %ld\n", cons, prod); |
| 2045 | ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp); |
| 2046 | cons += tmp2; |
| 2047 | prod += tmp; |
| 2048 | printf("%ld %ld\n", cons, prod); |
| 2049 | } |
| 2050 | output2[outlen] = 0; |
| 2051 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2); |
| 2052 | |
| 2053 | /* |
| 2054 | * input chunking |
| 2055 | */ |
| 2056 | cons = 0; |
| 2057 | prod = 0; |
| 2058 | while (cons < inlen) { |
| 2059 | tmp = 100 - prod; |
| 2060 | tmp2 = inlen - cons; |
| 2061 | if (tmp2 > 5) |
| 2062 | tmp2 = 5; |
| 2063 | |
| 2064 | printf("%ld %ld\n", cons, prod); |
| 2065 | ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp); |
| 2066 | cons += tmp2; |
| 2067 | prod += tmp; |
| 2068 | printf("%ld %ld\n", cons, prod); |
| 2069 | } |
| 2070 | output3[outlen] = 0; |
| 2071 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3); |
| 2072 | return(0); |
| 2073 | |
| 2074 | } |
| 2075 | #endif |