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 | |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 13 | /* |
| 14 | * TODOs: |
| 15 | * - provide an API to expand part of the tree |
| 16 | * - provide an API to preserve part of the tree |
| 17 | * - Streaming XInclude support |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 18 | * - validation against a provided DTD |
| 19 | * - XML Schemas validation |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 20 | * - setting(s) for NoBlanks |
| 21 | * - performances and tuning ... |
| 22 | */ |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 23 | #define IN_LIBXML |
| 24 | #include "libxml.h" |
| 25 | |
| 26 | #include <string.h> /* for memset() only ! */ |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 27 | #include <stdarg.h> |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef HAVE_CTYPE_H |
| 30 | #include <ctype.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_STDLIB_H |
| 33 | #include <stdlib.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <libxml/xmlmemory.h> |
| 37 | #include <libxml/xmlIO.h> |
| 38 | #include <libxml/xmlreader.h> |
| 39 | |
| 40 | /* #define DEBUG_CALLBACKS */ |
| 41 | /* #define DEBUG_READER */ |
| 42 | |
| 43 | /** |
| 44 | * TODO: |
| 45 | * |
| 46 | * macro to flag unimplemented blocks |
| 47 | */ |
| 48 | #define TODO \ |
| 49 | xmlGenericError(xmlGenericErrorContext, \ |
| 50 | "Unimplemented block at %s:%d\n", \ |
| 51 | __FILE__, __LINE__); |
| 52 | |
| 53 | #ifdef DEBUG_READER |
| 54 | #define DUMP_READER xmlTextReaderDebug(reader); |
| 55 | #else |
| 56 | #define DUMP_READER |
| 57 | #endif |
| 58 | |
| 59 | /************************************************************************ |
| 60 | * * |
| 61 | * The parser: maps the Text Reader API on top of the existing * |
| 62 | * parsing routines building a tree * |
| 63 | * * |
| 64 | ************************************************************************/ |
| 65 | |
| 66 | #define XML_TEXTREADER_INPUT 1 |
| 67 | #define XML_TEXTREADER_CTXT 2 |
| 68 | |
| 69 | typedef enum { |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 70 | XML_TEXTREADER_MODE_INITIAL = 0, |
| 71 | XML_TEXTREADER_MODE_INTERACTIVE = 1, |
| 72 | XML_TEXTREADER_MODE_ERROR = 2, |
| 73 | XML_TEXTREADER_MODE_EOF =3, |
| 74 | XML_TEXTREADER_MODE_CLOSED = 4, |
| 75 | XML_TEXTREADER_MODE_READING = 5 |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 76 | } xmlTextReaderMode; |
| 77 | |
| 78 | typedef enum { |
| 79 | XML_TEXTREADER_NONE = -1, |
| 80 | XML_TEXTREADER_START= 0, |
| 81 | XML_TEXTREADER_ELEMENT= 1, |
| 82 | XML_TEXTREADER_END= 2, |
| 83 | XML_TEXTREADER_EMPTY= 3, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 84 | XML_TEXTREADER_BACKTRACK= 4, |
| 85 | XML_TEXTREADER_DONE= 5 |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 86 | } xmlTextReaderState; |
| 87 | |
| 88 | struct _xmlTextReader { |
| 89 | int mode; /* the parsing mode */ |
| 90 | int allocs; /* what structure were deallocated */ |
| 91 | xmlTextReaderState state; |
| 92 | xmlParserCtxtPtr ctxt; /* the parser context */ |
| 93 | xmlSAXHandlerPtr sax; /* the parser SAX callbacks */ |
| 94 | xmlParserInputBufferPtr input; /* the input */ |
| 95 | startElementSAXFunc startElement;/* initial SAX callbacks */ |
| 96 | endElementSAXFunc endElement; /* idem */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 97 | charactersSAXFunc characters; |
| 98 | cdataBlockSAXFunc cdataBlock; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 99 | unsigned int base; /* base of the segment in the input */ |
| 100 | unsigned int cur; /* current position in the input */ |
| 101 | xmlNodePtr node; /* current node */ |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 102 | xmlNodePtr curnode;/* current attribute node */ |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 103 | int depth; /* depth of the current node */ |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 104 | xmlNodePtr faketext;/* fake xmlNs chld */ |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 105 | |
| 106 | /* entity stack when traversing entities content */ |
| 107 | xmlNodePtr ent; /* Current Entity Ref Node */ |
| 108 | int entNr; /* Depth of the entities stack */ |
| 109 | int entMax; /* Max depth of the entities stack */ |
| 110 | xmlNodePtr *entTab; /* array of entities */ |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 111 | |
| 112 | /* error handling */ |
| 113 | xmlTextReaderErrorFunc errorFunc; /* callback function */ |
| 114 | void *errorFuncArg; /* callback function user argument */ |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 117 | static const char *xmlTextReaderIsEmpty = "This element is empty"; |
| 118 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 119 | #ifdef DEBUG_READER |
| 120 | static void |
| 121 | xmlTextReaderDebug(xmlTextReaderPtr reader) { |
| 122 | if ((reader == NULL) || (reader->ctxt == NULL)) { |
| 123 | fprintf(stderr, "xmlTextReader NULL\n"); |
| 124 | return; |
| 125 | } |
| 126 | fprintf(stderr, "xmlTextReader: state %d depth %d ", |
| 127 | reader->state, reader->depth); |
| 128 | if (reader->node == NULL) { |
| 129 | fprintf(stderr, "node = NULL\n"); |
| 130 | } else { |
| 131 | fprintf(stderr, "node %s\n", reader->node->name); |
| 132 | } |
| 133 | fprintf(stderr, " input: base %d, cur %d, depth %d: ", |
| 134 | reader->base, reader->cur, reader->ctxt->nodeNr); |
| 135 | if (reader->input->buffer == NULL) { |
| 136 | fprintf(stderr, "buffer is NULL\n"); |
| 137 | } else { |
| 138 | #ifdef LIBXML_DEBUG_ENABLED |
| 139 | xmlDebugDumpString(stderr, |
| 140 | &reader->input->buffer->content[reader->cur]); |
| 141 | #endif |
| 142 | fprintf(stderr, "\n"); |
| 143 | } |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | /** |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 148 | * xmlTextReaderEntPush: |
| 149 | * @reader: the xmlTextReaderPtr used |
| 150 | * @value: the entity reference node |
| 151 | * |
| 152 | * Pushes a new entity reference node on top of the entities stack |
| 153 | * |
| 154 | * Returns 0 in case of error, the index in the stack otherwise |
| 155 | */ |
| 156 | static int |
| 157 | xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value) |
| 158 | { |
| 159 | if (reader->entMax <= 0) { |
| 160 | reader->entMax = 10; |
| 161 | reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax * |
| 162 | sizeof(reader->entTab[0])); |
| 163 | if (reader->entTab == NULL) { |
| 164 | xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n"); |
| 165 | return (0); |
| 166 | } |
| 167 | } |
| 168 | if (reader->entNr >= reader->entMax) { |
| 169 | reader->entMax *= 2; |
| 170 | reader->entTab = |
| 171 | (xmlNodePtr *) xmlRealloc(reader->entTab, |
| 172 | reader->entMax * |
| 173 | sizeof(reader->entTab[0])); |
| 174 | if (reader->entTab == NULL) { |
| 175 | xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n"); |
| 176 | return (0); |
| 177 | } |
| 178 | } |
| 179 | reader->entTab[reader->entNr] = value; |
| 180 | reader->ent = value; |
| 181 | return (reader->entNr++); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * xmlTextReaderEntPop: |
| 186 | * @reader: the xmlTextReaderPtr used |
| 187 | * |
| 188 | * Pops the top element entity from the entities stack |
| 189 | * |
| 190 | * Returns the entity just removed |
| 191 | */ |
| 192 | static xmlNodePtr |
| 193 | xmlTextReaderEntPop(xmlTextReaderPtr reader) |
| 194 | { |
| 195 | xmlNodePtr ret; |
| 196 | |
| 197 | if (reader->entNr <= 0) |
| 198 | return (0); |
| 199 | reader->entNr--; |
| 200 | if (reader->entNr > 0) |
| 201 | reader->ent = reader->entTab[reader->entNr - 1]; |
| 202 | else |
| 203 | reader->ent = NULL; |
| 204 | ret = reader->entTab[reader->entNr]; |
| 205 | reader->entTab[reader->entNr] = 0; |
| 206 | return (ret); |
| 207 | } |
| 208 | |
| 209 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 210 | * xmlTextReaderStartElement: |
| 211 | * @ctx: the user data (XML parser context) |
| 212 | * @fullname: The element name, including namespace prefix |
| 213 | * @atts: An array of name/value attributes pairs, NULL terminated |
| 214 | * |
| 215 | * called when an opening tag has been processed. |
| 216 | */ |
| 217 | static void |
| 218 | xmlTextReaderStartElement(void *ctx, const xmlChar *fullname, |
| 219 | const xmlChar **atts) { |
| 220 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 221 | xmlParserCtxtPtr origctxt; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 222 | xmlTextReaderPtr reader = ctxt->_private; |
| 223 | |
| 224 | #ifdef DEBUG_CALLBACKS |
| 225 | printf("xmlTextReaderStartElement(%s)\n", fullname); |
| 226 | #endif |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 227 | if ((reader != NULL) && (reader->startElement != NULL)) { |
Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 228 | /* |
| 229 | * when processing an entity, the context may have been changed |
| 230 | */ |
| 231 | origctxt = reader->ctxt; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 232 | reader->startElement(ctx, fullname, atts); |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 233 | if ((ctxt->node != NULL) && (ctxt->input != NULL) && |
| 234 | (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') && |
| 235 | (ctxt->input->cur[1] == '>')) |
| 236 | ctxt->node->_private = (void *) xmlTextReaderIsEmpty; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 237 | } |
Daniel Veillard | 9e395c2 | 2003-01-01 14:50:44 +0000 | [diff] [blame] | 238 | if (reader != NULL) |
| 239 | reader->state = XML_TEXTREADER_ELEMENT; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * xmlTextReaderEndElement: |
| 244 | * @ctx: the user data (XML parser context) |
| 245 | * @fullname: The element name, including namespace prefix |
| 246 | * |
| 247 | * called when an ending tag has been processed. |
| 248 | */ |
| 249 | static void |
| 250 | xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) { |
| 251 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 252 | xmlParserCtxtPtr origctxt; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 253 | xmlTextReaderPtr reader = ctxt->_private; |
| 254 | |
| 255 | #ifdef DEBUG_CALLBACKS |
| 256 | printf("xmlTextReaderEndElement(%s)\n", fullname); |
| 257 | #endif |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 258 | if ((reader != NULL) && (reader->endElement != NULL)) { |
Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 259 | /* |
| 260 | * when processing an entity, the context may have been changed |
| 261 | */ |
| 262 | origctxt = reader->ctxt; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 263 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 264 | reader->endElement(ctx, fullname); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 265 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 269 | * xmlTextReaderCharacters: |
| 270 | * @ctx: the user data (XML parser context) |
| 271 | * @ch: a xmlChar string |
| 272 | * @len: the number of xmlChar |
| 273 | * |
| 274 | * receiving some chars from the parser. |
| 275 | */ |
| 276 | static void |
| 277 | xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len) |
| 278 | { |
| 279 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 280 | xmlParserCtxtPtr origctxt; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 281 | xmlTextReaderPtr reader = ctxt->_private; |
| 282 | |
| 283 | #ifdef DEBUG_CALLBACKS |
| 284 | printf("xmlTextReaderCharacters()\n"); |
| 285 | #endif |
| 286 | if ((reader != NULL) && (reader->characters != NULL)) { |
| 287 | reader->characters(ctx, ch, len); |
Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 288 | /* |
| 289 | * when processing an entity, the context may have been changed |
| 290 | */ |
| 291 | origctxt = reader->ctxt; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * xmlTextReaderCDataBlock: |
| 297 | * @ctx: the user data (XML parser context) |
| 298 | * @value: The pcdata content |
| 299 | * @len: the block length |
| 300 | * |
| 301 | * called when a pcdata block has been parsed |
| 302 | */ |
| 303 | static void |
| 304 | xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len) |
| 305 | { |
| 306 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 307 | xmlTextReaderPtr reader = ctxt->_private; |
| 308 | |
| 309 | #ifdef DEBUG_CALLBACKS |
| 310 | printf("xmlTextReaderCDataBlock()\n"); |
| 311 | #endif |
| 312 | if ((reader != NULL) && (reader->cdataBlock != NULL)) { |
| 313 | reader->cdataBlock(ctx, ch, len); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 318 | * xmlTextReaderPushData: |
| 319 | * @reader: the xmlTextReaderPtr used |
| 320 | * |
| 321 | * Push data down the progressive parser until a significant callback |
| 322 | * got raised. |
| 323 | * |
| 324 | * Returns -1 in case of failure, 0 otherwise |
| 325 | */ |
| 326 | static int |
| 327 | xmlTextReaderPushData(xmlTextReaderPtr reader) { |
| 328 | unsigned int cur = reader->cur; |
| 329 | xmlBufferPtr inbuf; |
| 330 | int val; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 331 | int oldstate; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 332 | |
| 333 | if ((reader->input == NULL) || (reader->input->buffer == NULL)) |
| 334 | return(-1); |
| 335 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 336 | oldstate = reader->state; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 337 | reader->state = XML_TEXTREADER_NONE; |
| 338 | inbuf = reader->input->buffer; |
| 339 | while (reader->state == XML_TEXTREADER_NONE) { |
| 340 | if (cur >= inbuf->use) { |
| 341 | /* |
| 342 | * Refill the buffer unless we are at the end of the stream |
| 343 | */ |
| 344 | if (reader->mode != XML_TEXTREADER_MODE_EOF) { |
| 345 | val = xmlParserInputBufferRead(reader->input, 4096); |
| 346 | if (val <= 0) { |
| 347 | reader->mode = XML_TEXTREADER_MODE_EOF; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 348 | reader->state = oldstate; |
Daniel Veillard | aaa105b | 2002-12-30 11:42:17 +0000 | [diff] [blame] | 349 | if ((oldstate != XML_TEXTREADER_START) || |
| 350 | (reader->ctxt->myDoc != NULL)) |
| 351 | return(val); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 352 | } |
| 353 | } else |
| 354 | break; |
| 355 | } |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 356 | /* |
| 357 | * parse by block of 512 bytes |
| 358 | */ |
| 359 | if ((cur >= reader->cur + 512) || (cur >= inbuf->use)) { |
| 360 | if (cur < inbuf->use) |
| 361 | cur = cur + 1; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 362 | val = xmlParseChunk(reader->ctxt, |
| 363 | (const char *) &inbuf->content[reader->cur], |
| 364 | cur - reader->cur, 0); |
| 365 | if (val != 0) |
| 366 | return(-1); |
| 367 | reader->cur = cur; |
| 368 | break; |
| 369 | } else { |
| 370 | cur = cur + 1; |
| 371 | |
| 372 | /* |
| 373 | * One may have to force a flush at some point when parsing really |
| 374 | * large CDATA sections |
| 375 | */ |
| 376 | if ((cur - reader->cur > 4096) && (reader->base == 0) && |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 377 | (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE)) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 378 | cur = cur + 1; |
| 379 | val = xmlParseChunk(reader->ctxt, |
| 380 | (const char *) &inbuf->content[reader->cur], |
| 381 | cur - reader->cur, 0); |
| 382 | if (val != 0) |
| 383 | return(-1); |
| 384 | reader->cur = cur; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | /* |
| 389 | * Discard the consumed input when needed and possible |
| 390 | */ |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 391 | if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 392 | if ((reader->cur >= 4096) && (reader->base == 0)) { |
| 393 | val = xmlBufferShrink(inbuf, cur); |
| 394 | if (val >= 0) { |
| 395 | reader->cur -= val; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * At the end of the stream signal that the work is done to the Push |
| 402 | * parser. |
| 403 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 404 | if (reader->mode == XML_TEXTREADER_MODE_EOF) { |
| 405 | if (reader->mode != XML_TEXTREADER_DONE) { |
| 406 | val = xmlParseChunk(reader->ctxt, |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 407 | (const char *) &inbuf->content[reader->cur], |
| 408 | cur - reader->cur, 1); |
| 409 | reader->cur = cur; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 410 | reader->mode = XML_TEXTREADER_DONE; |
| 411 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 412 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 413 | reader->state = oldstate; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 414 | return(0); |
| 415 | } |
| 416 | |
| 417 | /** |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 418 | * xmlTextReaderValidatePush: |
| 419 | * @reader: the xmlTextReaderPtr used |
| 420 | * |
| 421 | * Push the current node for validation |
| 422 | */ |
| 423 | static void |
| 424 | xmlTextReaderValidatePush(xmlTextReaderPtr reader) { |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 425 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 426 | xmlNodePtr node = reader->node; |
| 427 | |
| 428 | if ((node->ns == NULL) || (node->ns->prefix == NULL)) { |
| 429 | reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt, |
| 430 | reader->ctxt->myDoc, node, node->name); |
| 431 | } else { |
| 432 | xmlChar *qname; |
| 433 | |
| 434 | qname = xmlStrdup(node->ns->prefix); |
| 435 | qname = xmlStrcat(qname, BAD_CAST ":"); |
| 436 | qname = xmlStrcat(qname, node->name); |
| 437 | reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt, |
| 438 | reader->ctxt->myDoc, node, qname); |
| 439 | if (qname != NULL) |
| 440 | xmlFree(qname); |
| 441 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 442 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 443 | } |
| 444 | /** |
| 445 | * xmlTextReaderValidatePop: |
| 446 | * @reader: the xmlTextReaderPtr used |
| 447 | * |
| 448 | * Pop the current node from validation |
| 449 | */ |
| 450 | static void |
| 451 | xmlTextReaderValidatePop(xmlTextReaderPtr reader) { |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 452 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 453 | xmlNodePtr node = reader->node; |
| 454 | |
| 455 | if ((node->ns == NULL) || (node->ns->prefix == NULL)) { |
| 456 | reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt, |
| 457 | reader->ctxt->myDoc, node, node->name); |
| 458 | } else { |
| 459 | xmlChar *qname; |
| 460 | |
| 461 | qname = xmlStrdup(node->ns->prefix); |
| 462 | qname = xmlStrcat(qname, BAD_CAST ":"); |
| 463 | qname = xmlStrcat(qname, node->name); |
| 464 | reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt, |
| 465 | reader->ctxt->myDoc, node, qname); |
| 466 | if (qname != NULL) |
| 467 | xmlFree(qname); |
| 468 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 469 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 470 | } |
Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 471 | /** |
| 472 | * xmlTextReaderValidateEntity: |
| 473 | * @reader: the xmlTextReaderPtr used |
| 474 | * |
| 475 | * Handle the validation when an entity reference is encountered and |
| 476 | * entity substitution is not activated. As a result the parser interface |
| 477 | * must walk through the entity and do the validation calls |
| 478 | */ |
| 479 | static void |
| 480 | xmlTextReaderValidateEntity(xmlTextReaderPtr reader) { |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 481 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 482 | xmlNodePtr oldnode = reader->node; |
| 483 | xmlNodePtr node = reader->node; |
| 484 | xmlParserCtxtPtr ctxt = reader->ctxt; |
| 485 | |
| 486 | do { |
| 487 | if (node->type == XML_ENTITY_REF_NODE) { |
| 488 | /* |
| 489 | * Case where the underlying tree is not availble, lookup the entity |
| 490 | * and walk it. |
| 491 | */ |
| 492 | if ((node->children == NULL) && (ctxt->sax != NULL) && |
| 493 | (ctxt->sax->getEntity != NULL)) { |
| 494 | node->children = (xmlNodePtr) |
| 495 | ctxt->sax->getEntity(ctxt, node->name); |
| 496 | } |
| 497 | |
| 498 | if ((node->children != NULL) && |
| 499 | (node->children->type == XML_ENTITY_DECL) && |
| 500 | (node->children->children != NULL)) { |
| 501 | xmlTextReaderEntPush(reader, node); |
| 502 | node = node->children->children; |
| 503 | continue; |
| 504 | } else { |
| 505 | /* |
| 506 | * The error has probably be raised already. |
| 507 | */ |
| 508 | if (node == oldnode) |
| 509 | break; |
| 510 | node = node->next; |
| 511 | } |
| 512 | } else if (node->type == XML_ELEMENT_NODE) { |
| 513 | reader->node = node; |
| 514 | xmlTextReaderValidatePush(reader); |
| 515 | } else if ((node->type == XML_TEXT_NODE) || |
| 516 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 517 | ctxt->valid &= xmlValidatePushCData(&ctxt->vctxt, |
| 518 | node->content, xmlStrlen(node->content)); |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * go to next node |
| 523 | */ |
| 524 | if (node->children != NULL) { |
| 525 | node = node->children; |
| 526 | continue; |
| 527 | } |
| 528 | if (node->next != NULL) { |
| 529 | node = node->next; |
| 530 | continue; |
| 531 | } |
| 532 | do { |
| 533 | node = node->parent; |
| 534 | if (node->type == XML_ELEMENT_NODE) { |
| 535 | reader->node = node; |
| 536 | xmlTextReaderValidatePop(reader); |
| 537 | } |
| 538 | if ((node->type == XML_ENTITY_DECL) && |
| 539 | (reader->ent != NULL) && (reader->ent->children == node)) { |
| 540 | node = xmlTextReaderEntPop(reader); |
| 541 | } |
| 542 | if (node == oldnode) |
| 543 | break; |
| 544 | if (node->next != NULL) { |
| 545 | node = node->next; |
| 546 | break; |
| 547 | } |
| 548 | } while ((node != NULL) && (node != oldnode)); |
| 549 | } while ((node != NULL) && (node != oldnode)); |
| 550 | reader->node = oldnode; |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 551 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 552 | } |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 553 | |
| 554 | |
| 555 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 556 | * xmlTextReaderRead: |
| 557 | * @reader: the xmlTextReaderPtr used |
| 558 | * |
| 559 | * Moves the position of the current instance to the next node in |
| 560 | * the stream, exposing its properties. |
| 561 | * |
| 562 | * Returns 1 if the node was read successfully, 0 if there is no more |
| 563 | * nodes to read, or -1 in case of error |
| 564 | */ |
| 565 | int |
| 566 | xmlTextReaderRead(xmlTextReaderPtr reader) { |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 567 | int val, olddepth = 0; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 568 | xmlTextReaderState oldstate = 0; |
| 569 | xmlNodePtr oldnode = NULL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 570 | |
| 571 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 572 | return(-1); |
| 573 | if (reader->ctxt->wellFormed != 1) |
| 574 | return(-1); |
| 575 | |
| 576 | #ifdef DEBUG_READER |
| 577 | fprintf(stderr, "\nREAD "); |
| 578 | DUMP_READER |
| 579 | #endif |
Daniel Veillard | 29b3e28 | 2002-12-29 11:14:41 +0000 | [diff] [blame] | 580 | reader->curnode = NULL; |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 581 | if (reader->mode == XML_TEXTREADER_MODE_INITIAL) { |
| 582 | reader->mode = XML_TEXTREADER_MODE_INTERACTIVE; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 583 | /* |
| 584 | * Initial state |
| 585 | */ |
| 586 | do { |
| 587 | val = xmlTextReaderPushData(reader); |
| 588 | if (val < 0) |
| 589 | return(-1); |
| 590 | } while ((reader->ctxt->node == NULL) && |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 591 | ((reader->mode != XML_TEXTREADER_MODE_EOF) && |
| 592 | (reader->mode != XML_TEXTREADER_DONE))); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 593 | if (reader->ctxt->node == NULL) { |
Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 594 | if (reader->ctxt->myDoc != NULL) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 595 | reader->node = reader->ctxt->myDoc->children; |
Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 596 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 597 | if (reader->node == NULL) |
| 598 | return(-1); |
Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 599 | reader->state = XML_TEXTREADER_ELEMENT; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 600 | } else { |
Daniel Veillard | 4d8db8a | 2002-12-30 18:40:42 +0000 | [diff] [blame] | 601 | reader->node = reader->ctxt->nodeTab[0]; |
Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 602 | reader->state = XML_TEXTREADER_ELEMENT; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 603 | } |
Daniel Veillard | 4d8db8a | 2002-12-30 18:40:42 +0000 | [diff] [blame] | 604 | reader->depth = 0; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 605 | goto node_found; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 606 | } |
| 607 | oldstate = reader->state; |
| 608 | olddepth = reader->ctxt->nodeNr; |
| 609 | oldnode = reader->node; |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 610 | |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 611 | get_next_node: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 612 | /* |
| 613 | * If we are not backtracking on ancestors or examined nodes, |
| 614 | * that the parser didn't finished or that we arent at the end |
| 615 | * of stream, continue processing. |
| 616 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 617 | while (((oldstate == XML_TEXTREADER_BACKTRACK) || |
| 618 | (reader->node->children == NULL) || |
| 619 | (reader->node->type == XML_ENTITY_REF_NODE) || |
Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 620 | (reader->node->type == XML_DTD_NODE) || |
| 621 | (reader->node->type == XML_DOCUMENT_NODE) || |
| 622 | (reader->node->type == XML_HTML_DOCUMENT_NODE)) && |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 623 | (reader->node->next == NULL) && |
Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 624 | ((reader->ctxt->node == NULL) || |
| 625 | (reader->ctxt->node == reader->node) || |
| 626 | (reader->ctxt->node == reader->node->parent)) && |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 627 | (reader->ctxt->nodeNr == olddepth) && |
| 628 | (reader->ctxt->instate != XML_PARSER_EOF)) { |
| 629 | val = xmlTextReaderPushData(reader); |
| 630 | if (val < 0) |
| 631 | return(-1); |
| 632 | if (reader->node == NULL) |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 633 | goto node_end; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 634 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 635 | if (oldstate != XML_TEXTREADER_BACKTRACK) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 636 | if ((reader->node->children != NULL) && |
| 637 | (reader->node->type != XML_ENTITY_REF_NODE) && |
| 638 | (reader->node->type != XML_DTD_NODE)) { |
| 639 | reader->node = reader->node->children; |
| 640 | reader->depth++; |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 641 | reader->state = XML_TEXTREADER_ELEMENT; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 642 | goto node_found; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | if (reader->node->next != NULL) { |
| 646 | if ((oldstate == XML_TEXTREADER_ELEMENT) && |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 647 | (reader->node->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 648 | (reader->node->children == NULL) && |
| 649 | (reader->node->_private != (void *)xmlTextReaderIsEmpty)) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 650 | reader->state = XML_TEXTREADER_END; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 651 | goto node_found; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 652 | } |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 653 | if ((reader->ctxt->validate) && |
| 654 | (reader->node->type == XML_ELEMENT_NODE)) |
| 655 | xmlTextReaderValidatePop(reader); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 656 | reader->node = reader->node->next; |
| 657 | reader->state = XML_TEXTREADER_ELEMENT; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 658 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 659 | /* |
| 660 | * Cleanup of the old node |
| 661 | */ |
Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 662 | if ((reader->node->prev != NULL) && |
| 663 | (reader->node->prev->type != XML_DTD_NODE)) { |
| 664 | xmlNodePtr tmp = reader->node->prev; |
| 665 | xmlUnlinkNode(tmp); |
| 666 | xmlFreeNode(tmp); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 669 | goto node_found; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 670 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 671 | if ((oldstate == XML_TEXTREADER_ELEMENT) && |
Daniel Veillard | 571b889 | 2002-12-30 12:37:59 +0000 | [diff] [blame] | 672 | (reader->node->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 673 | (reader->node->children == NULL) && |
| 674 | (reader->node->_private != (void *)xmlTextReaderIsEmpty)) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 675 | reader->state = XML_TEXTREADER_END; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 676 | goto node_found; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 677 | } |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 678 | if ((reader->ctxt->validate) && (reader->node->type == XML_ELEMENT_NODE)) |
| 679 | xmlTextReaderValidatePop(reader); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 680 | reader->node = reader->node->parent; |
| 681 | if ((reader->node == NULL) || |
| 682 | (reader->node->type == XML_DOCUMENT_NODE) || |
| 683 | #ifdef LIBXML_DOCB_ENABLED |
| 684 | (reader->node->type == XML_DOCB_DOCUMENT_NODE) || |
| 685 | #endif |
| 686 | (reader->node->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 687 | if (reader->mode != XML_TEXTREADER_DONE) { |
| 688 | val = xmlParseChunk(reader->ctxt, "", 0, 1); |
| 689 | reader->mode = XML_TEXTREADER_DONE; |
| 690 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 691 | reader->node = NULL; |
Daniel Veillard | 4d8db8a | 2002-12-30 18:40:42 +0000 | [diff] [blame] | 692 | reader->depth = -1; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * Cleanup of the old node |
| 696 | */ |
| 697 | if (oldnode->type != XML_DTD_NODE) { |
| 698 | xmlUnlinkNode(oldnode); |
| 699 | xmlFreeNode(oldnode); |
| 700 | } |
| 701 | |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 702 | goto node_end; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 703 | } |
| 704 | reader->depth--; |
| 705 | reader->state = XML_TEXTREADER_BACKTRACK; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 706 | |
| 707 | node_found: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 708 | DUMP_READER |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 709 | |
| 710 | /* |
Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 711 | * Handle entities enter and exit when in entity replacement mode |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 712 | */ |
| 713 | if ((reader->node != NULL) && |
| 714 | (reader->node->type == XML_ENTITY_REF_NODE) && |
| 715 | (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) { |
| 716 | /* |
| 717 | * Case where the underlying tree is not availble, lookup the entity |
| 718 | * and walk it. |
| 719 | */ |
| 720 | if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) && |
| 721 | (reader->ctxt->sax->getEntity != NULL)) { |
| 722 | reader->node->children = (xmlNodePtr) |
| 723 | reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name); |
| 724 | } |
| 725 | |
| 726 | if ((reader->node->children != NULL) && |
| 727 | (reader->node->children->type == XML_ENTITY_DECL) && |
| 728 | (reader->node->children->children != NULL)) { |
| 729 | xmlTextReaderEntPush(reader, reader->node); |
| 730 | reader->node = reader->node->children->children; |
| 731 | } |
Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 732 | } else if ((reader->node != NULL) && |
| 733 | (reader->node->type == XML_ENTITY_REF_NODE) && |
| 734 | (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) { |
| 735 | xmlTextReaderValidateEntity(reader); |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 736 | } |
| 737 | if ((reader->node != NULL) && |
| 738 | (reader->node->type == XML_ENTITY_DECL) && |
| 739 | (reader->ent != NULL) && (reader->ent->children == reader->node)) { |
| 740 | reader->node = xmlTextReaderEntPop(reader); |
| 741 | reader->depth++; |
| 742 | goto get_next_node; |
| 743 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 744 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 745 | if ((reader->ctxt->validate) && (reader->node != NULL)) { |
| 746 | xmlNodePtr node = reader->node; |
| 747 | xmlParserCtxtPtr ctxt = reader->ctxt; |
| 748 | |
| 749 | if ((node->type == XML_ELEMENT_NODE) && |
| 750 | ((reader->state != XML_TEXTREADER_END) && |
| 751 | (reader->state != XML_TEXTREADER_BACKTRACK))) { |
| 752 | xmlTextReaderValidatePush(reader); |
| 753 | } else if ((node->type == XML_TEXT_NODE) || |
| 754 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 755 | ctxt->valid &= xmlValidatePushCData(&ctxt->vctxt, |
| 756 | node->content, xmlStrlen(node->content)); |
| 757 | } |
| 758 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 759 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 760 | return(1); |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 761 | node_end: |
| 762 | return(0); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 765 | /** |
| 766 | * xmlTextReaderReadState: |
| 767 | * @reader: the xmlTextReaderPtr used |
| 768 | * |
| 769 | * Gets the read state of the reader. |
| 770 | * |
| 771 | * Returns the state value, or -1 in case of error |
| 772 | */ |
| 773 | int |
| 774 | xmlTextReaderReadState(xmlTextReaderPtr reader) { |
| 775 | if (reader == NULL) |
| 776 | return(-1); |
| 777 | return(reader->mode); |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * xmlTextReaderReadInnerXml: |
| 782 | * @reader: the xmlTextReaderPtr used |
| 783 | * |
| 784 | * Reads the contents of the current node, including child nodes and markup. |
| 785 | * |
| 786 | * Returns a string containing the XML content, or NULL if the current node |
| 787 | * is neither an element nor attribute, or has no child nodes. The |
| 788 | * string must be deallocated by the caller. |
| 789 | */ |
| 790 | xmlChar * |
| 791 | xmlTextReaderReadInnerXml(xmlTextReaderPtr reader) { |
| 792 | TODO |
| 793 | return(NULL); |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * xmlTextReaderReadOuterXml: |
| 798 | * @reader: the xmlTextReaderPtr used |
| 799 | * |
| 800 | * Reads the contents of the current node, including child nodes and markup. |
| 801 | * |
| 802 | * Returns a string containing the XML content, or NULL if the current node |
| 803 | * is neither an element nor attribute, or has no child nodes. The |
| 804 | * string must be deallocated by the caller. |
| 805 | */ |
| 806 | xmlChar * |
| 807 | xmlTextReaderReadOuterXml(xmlTextReaderPtr reader) { |
| 808 | TODO |
| 809 | return(NULL); |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * xmlTextReaderReadString: |
| 814 | * @reader: the xmlTextReaderPtr used |
| 815 | * |
| 816 | * Reads the contents of an element or a text node as a string. |
| 817 | * |
| 818 | * Returns a string containing the contents of the Element or Text node, |
| 819 | * or NULL if the reader is positioned on any other type of node. |
| 820 | * The string must be deallocated by the caller. |
| 821 | */ |
| 822 | xmlChar * |
| 823 | xmlTextReaderReadString(xmlTextReaderPtr reader) { |
| 824 | TODO |
| 825 | return(NULL); |
| 826 | } |
| 827 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 828 | /** |
| 829 | * xmlTextReaderReadBase64: |
| 830 | * @reader: the xmlTextReaderPtr used |
| 831 | * @array: a byte array to store the content. |
| 832 | * @offset: the zero-based index into array where the method should |
| 833 | * begin to write. |
| 834 | * @len: the number of bytes to write. |
| 835 | * |
| 836 | * Reads and decodes the Base64 encoded contents of an element and |
| 837 | * stores the result in a byte buffer. |
| 838 | * |
| 839 | * Returns the number of bytes written to array, or zero if the current |
| 840 | * instance is not positioned on an element or -1 in case of error. |
| 841 | */ |
| 842 | int |
| 843 | xmlTextReaderReadBase64(xmlTextReaderPtr reader, unsigned char *array, |
| 844 | int offset, int len) { |
| 845 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 846 | return(-1); |
| 847 | if (reader->ctxt->wellFormed != 1) |
| 848 | return(-1); |
| 849 | |
| 850 | if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE)) |
| 851 | return(0); |
| 852 | TODO |
| 853 | return(0); |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * xmlTextReaderReadBinHex: |
| 858 | * @reader: the xmlTextReaderPtr used |
| 859 | * @array: a byte array to store the content. |
| 860 | * @offset: the zero-based index into array where the method should |
| 861 | * begin to write. |
| 862 | * @len: the number of bytes to write. |
| 863 | * |
| 864 | * Reads and decodes the BinHex encoded contents of an element and |
| 865 | * stores the result in a byte buffer. |
| 866 | * |
| 867 | * Returns the number of bytes written to array, or zero if the current |
| 868 | * instance is not positioned on an element or -1 in case of error. |
| 869 | */ |
| 870 | int |
| 871 | xmlTextReaderReadBinHex(xmlTextReaderPtr reader, unsigned char *array, |
| 872 | int offset, int len) { |
| 873 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 874 | return(-1); |
| 875 | if (reader->ctxt->wellFormed != 1) |
| 876 | return(-1); |
| 877 | |
| 878 | if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE)) |
| 879 | return(0); |
| 880 | TODO |
| 881 | return(0); |
| 882 | } |
| 883 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 884 | /************************************************************************ |
| 885 | * * |
| 886 | * Constructor and destructors * |
| 887 | * * |
| 888 | ************************************************************************/ |
| 889 | /** |
| 890 | * xmlNewTextReader: |
| 891 | * @input: the xmlParserInputBufferPtr used to read data |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 892 | * @URI: the URI information for the source if available |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 893 | * |
| 894 | * Create an xmlTextReader structure fed with @input |
| 895 | * |
| 896 | * Returns the new xmlTextReaderPtr or NULL in case of error |
| 897 | */ |
| 898 | xmlTextReaderPtr |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 899 | xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 900 | xmlTextReaderPtr ret; |
| 901 | int val; |
| 902 | |
| 903 | if (input == NULL) |
| 904 | return(NULL); |
| 905 | ret = xmlMalloc(sizeof(xmlTextReader)); |
| 906 | if (ret == NULL) { |
| 907 | xmlGenericError(xmlGenericErrorContext, |
| 908 | "xmlNewTextReader : malloc failed\n"); |
| 909 | return(NULL); |
| 910 | } |
| 911 | memset(ret, 0, sizeof(xmlTextReader)); |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 912 | ret->entTab = NULL; |
| 913 | ret->entMax = 0; |
| 914 | ret->entNr = 0; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 915 | ret->input = input; |
| 916 | ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
| 917 | if (ret->sax == NULL) { |
| 918 | xmlFree(ret); |
| 919 | xmlGenericError(xmlGenericErrorContext, |
| 920 | "xmlNewTextReader : malloc failed\n"); |
| 921 | return(NULL); |
| 922 | } |
| 923 | memcpy(ret->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler)); |
| 924 | ret->startElement = ret->sax->startElement; |
| 925 | ret->sax->startElement = xmlTextReaderStartElement; |
| 926 | ret->endElement = ret->sax->endElement; |
| 927 | ret->sax->endElement = xmlTextReaderEndElement; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 928 | ret->characters = ret->sax->characters; |
| 929 | ret->sax->characters = xmlTextReaderCharacters; |
| 930 | ret->cdataBlock = ret->sax->cdataBlock; |
| 931 | ret->sax->cdataBlock = xmlTextReaderCDataBlock; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 932 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 933 | ret->mode = XML_TEXTREADER_MODE_INITIAL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 934 | ret->node = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 935 | ret->curnode = NULL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 936 | val = xmlParserInputBufferRead(input, 4); |
| 937 | if (val >= 4) { |
| 938 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 939 | (const char *) ret->input->buffer->content, 4, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 940 | ret->base = 0; |
| 941 | ret->cur = 4; |
| 942 | } else { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 943 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 944 | ret->base = 0; |
| 945 | ret->cur = 0; |
| 946 | } |
| 947 | ret->ctxt->_private = ret; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 948 | ret->ctxt->linenumbers = 1; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 949 | ret->allocs = XML_TEXTREADER_CTXT; |
| 950 | return(ret); |
| 951 | |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * xmlNewTextReaderFilename: |
| 956 | * @URI: the URI of the resource to process |
| 957 | * |
| 958 | * Create an xmlTextReader structure fed with the resource at @URI |
| 959 | * |
| 960 | * Returns the new xmlTextReaderPtr or NULL in case of error |
| 961 | */ |
| 962 | xmlTextReaderPtr |
| 963 | xmlNewTextReaderFilename(const char *URI) { |
| 964 | xmlParserInputBufferPtr input; |
| 965 | xmlTextReaderPtr ret; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 966 | char *directory = NULL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 967 | |
| 968 | input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE); |
| 969 | if (input == NULL) |
| 970 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 971 | ret = xmlNewTextReader(input, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 972 | if (ret == NULL) { |
| 973 | xmlFreeParserInputBuffer(input); |
| 974 | return(NULL); |
| 975 | } |
| 976 | ret->allocs |= XML_TEXTREADER_INPUT; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 977 | if (ret->ctxt->directory == NULL) |
| 978 | directory = xmlParserGetDirectory(URI); |
| 979 | if ((ret->ctxt->directory == NULL) && (directory != NULL)) |
| 980 | ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory); |
| 981 | if (directory != NULL) |
| 982 | xmlFree(directory); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 983 | return(ret); |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * xmlFreeTextReader: |
| 988 | * @reader: the xmlTextReaderPtr |
| 989 | * |
| 990 | * Deallocate all the resources associated to the reader |
| 991 | */ |
| 992 | void |
| 993 | xmlFreeTextReader(xmlTextReaderPtr reader) { |
| 994 | if (reader == NULL) |
| 995 | return; |
| 996 | if (reader->ctxt != NULL) { |
| 997 | if (reader->ctxt->myDoc != NULL) { |
| 998 | xmlFreeDoc(reader->ctxt->myDoc); |
| 999 | reader->ctxt->myDoc = NULL; |
| 1000 | } |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 1001 | if ((reader->ctxt->vctxt.vstateTab != NULL) && |
| 1002 | (reader->ctxt->vctxt.vstateMax > 0)){ |
| 1003 | xmlFree(reader->ctxt->vctxt.vstateTab); |
| 1004 | reader->ctxt->vctxt.vstateTab = 0; |
| 1005 | reader->ctxt->vctxt.vstateMax = 0; |
| 1006 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1007 | if (reader->allocs & XML_TEXTREADER_CTXT) |
| 1008 | xmlFreeParserCtxt(reader->ctxt); |
| 1009 | } |
| 1010 | if (reader->sax != NULL) |
| 1011 | xmlFree(reader->sax); |
| 1012 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) |
| 1013 | xmlFreeParserInputBuffer(reader->input); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1014 | if (reader->faketext != NULL) { |
| 1015 | xmlFreeNode(reader->faketext); |
| 1016 | } |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 1017 | if (reader->entTab != NULL) |
| 1018 | xmlFree(reader->entTab); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1019 | xmlFree(reader); |
| 1020 | } |
| 1021 | |
| 1022 | /************************************************************************ |
| 1023 | * * |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1024 | * Methods for XmlTextReader * |
| 1025 | * * |
| 1026 | ************************************************************************/ |
| 1027 | /** |
| 1028 | * xmlTextReaderClose: |
| 1029 | * @reader: the xmlTextReaderPtr used |
| 1030 | * |
| 1031 | * This method releases any resources allocated by the current instance |
| 1032 | * changes the state to Closed and close any underlying input. |
| 1033 | * |
| 1034 | * Returns 0 or -1 in case of error |
| 1035 | */ |
| 1036 | int |
| 1037 | xmlTextReaderClose(xmlTextReaderPtr reader) { |
| 1038 | if (reader == NULL) |
| 1039 | return(-1); |
| 1040 | reader->node = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1041 | reader->curnode = NULL; |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1042 | reader->mode = XML_TEXTREADER_MODE_CLOSED; |
| 1043 | if (reader->ctxt != NULL) { |
| 1044 | if (reader->ctxt->myDoc != NULL) { |
| 1045 | xmlFreeDoc(reader->ctxt->myDoc); |
| 1046 | reader->ctxt->myDoc = NULL; |
| 1047 | } |
| 1048 | if (reader->allocs & XML_TEXTREADER_CTXT) { |
| 1049 | xmlFreeParserCtxt(reader->ctxt); |
| 1050 | reader->allocs -= XML_TEXTREADER_CTXT; |
| 1051 | } |
| 1052 | } |
| 1053 | if (reader->sax != NULL) { |
| 1054 | xmlFree(reader->sax); |
| 1055 | reader->sax = NULL; |
| 1056 | } |
| 1057 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) { |
| 1058 | xmlFreeParserInputBuffer(reader->input); |
| 1059 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 1060 | } |
| 1061 | return(0); |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * xmlTextReaderGetAttributeNo: |
| 1066 | * @reader: the xmlTextReaderPtr used |
| 1067 | * @no: the zero-based index of the attribute relative to the containing element |
| 1068 | * |
| 1069 | * Provides the value of the attribute with the specified index relative |
| 1070 | * to the containing element. |
| 1071 | * |
| 1072 | * Returns a string containing the value of the specified attribute, or NULL |
| 1073 | * in case of error. The string must be deallocated by the caller. |
| 1074 | */ |
| 1075 | xmlChar * |
| 1076 | xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) { |
| 1077 | xmlChar *ret; |
| 1078 | int i; |
| 1079 | xmlAttrPtr cur; |
| 1080 | xmlNsPtr ns; |
| 1081 | |
| 1082 | if (reader == NULL) |
| 1083 | return(NULL); |
| 1084 | if (reader->node == NULL) |
| 1085 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1086 | if (reader->curnode != NULL) |
| 1087 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1088 | /* TODO: handle the xmlDecl */ |
| 1089 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1090 | return(NULL); |
| 1091 | |
| 1092 | ns = reader->node->nsDef; |
| 1093 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 1094 | ns = ns->next; |
| 1095 | } |
| 1096 | if (ns != NULL) |
| 1097 | return(xmlStrdup(ns->href)); |
| 1098 | |
| 1099 | cur = reader->node->properties; |
| 1100 | if (cur == NULL) |
| 1101 | return(NULL); |
| 1102 | for (;i < no;i++) { |
| 1103 | cur = cur->next; |
| 1104 | if (cur == NULL) |
| 1105 | return(NULL); |
| 1106 | } |
| 1107 | /* TODO walk the DTD if present */ |
| 1108 | |
| 1109 | ret = xmlNodeListGetString(reader->node->doc, cur->children, 1); |
| 1110 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 1111 | return(ret); |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * xmlTextReaderGetAttribute: |
| 1116 | * @reader: the xmlTextReaderPtr used |
| 1117 | * @name: the qualified name of the attribute. |
| 1118 | * |
| 1119 | * Provides the value of the attribute with the specified qualified name. |
| 1120 | * |
| 1121 | * Returns a string containing the value of the specified attribute, or NULL |
| 1122 | * in case of error. The string must be deallocated by the caller. |
| 1123 | */ |
| 1124 | xmlChar * |
| 1125 | xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 1126 | xmlChar *prefix = NULL; |
| 1127 | xmlChar *localname; |
| 1128 | xmlNsPtr ns; |
| 1129 | xmlChar *ret = NULL; |
| 1130 | |
| 1131 | if ((reader == NULL) || (name == NULL)) |
| 1132 | return(NULL); |
| 1133 | if (reader->node == NULL) |
| 1134 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1135 | if (reader->curnode != NULL) |
| 1136 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* TODO: handle the xmlDecl */ |
| 1139 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1140 | return(NULL); |
| 1141 | |
| 1142 | localname = xmlSplitQName2(name, &prefix); |
| 1143 | if (localname == NULL) |
| 1144 | return(xmlGetProp(reader->node, name)); |
| 1145 | |
| 1146 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 1147 | if (ns != NULL) |
| 1148 | ret = xmlGetNsProp(reader->node, localname, ns->href); |
| 1149 | |
| 1150 | if (localname != NULL) |
| 1151 | xmlFree(localname); |
| 1152 | if (prefix != NULL) |
| 1153 | xmlFree(prefix); |
| 1154 | return(ret); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /** |
| 1159 | * xmlTextReaderGetAttributeNs: |
| 1160 | * @reader: the xmlTextReaderPtr used |
| 1161 | * @localName: the local name of the attribute. |
| 1162 | * @namespaceURI: the namespace URI of the attribute. |
| 1163 | * |
| 1164 | * Provides the value of the specified attribute |
| 1165 | * |
| 1166 | * Returns a string containing the value of the specified attribute, or NULL |
| 1167 | * in case of error. The string must be deallocated by the caller. |
| 1168 | */ |
| 1169 | xmlChar * |
| 1170 | xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName, |
| 1171 | const xmlChar *namespaceURI) { |
| 1172 | if ((reader == NULL) || (localName == NULL)) |
| 1173 | return(NULL); |
| 1174 | if (reader->node == NULL) |
| 1175 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1176 | if (reader->curnode != NULL) |
| 1177 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* TODO: handle the xmlDecl */ |
| 1180 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1181 | return(NULL); |
| 1182 | |
| 1183 | return(xmlGetNsProp(reader->node, localName, namespaceURI)); |
| 1184 | } |
| 1185 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1186 | /** |
| 1187 | * xmlTextReaderGetRemainder: |
| 1188 | * @reader: the xmlTextReaderPtr used |
| 1189 | * |
| 1190 | * Method to get the remainder of the buffered XML. this method stops the |
| 1191 | * parser, set its state to End Of File and return the input stream with |
| 1192 | * what is left that the parser did not use. |
| 1193 | * |
| 1194 | * Returns the xmlParserInputBufferPtr attached to the XML or NULL |
| 1195 | * in case of error. |
| 1196 | */ |
| 1197 | xmlParserInputBufferPtr |
| 1198 | xmlTextReaderGetRemainder(xmlTextReaderPtr reader) { |
| 1199 | xmlParserInputBufferPtr ret = NULL; |
| 1200 | |
| 1201 | if (reader == NULL) |
| 1202 | return(NULL); |
| 1203 | if (reader->node == NULL) |
| 1204 | return(NULL); |
| 1205 | |
| 1206 | reader->node = NULL; |
| 1207 | reader->curnode = NULL; |
| 1208 | reader->mode = XML_TEXTREADER_MODE_EOF; |
| 1209 | if (reader->ctxt != NULL) { |
| 1210 | if (reader->ctxt->myDoc != NULL) { |
| 1211 | xmlFreeDoc(reader->ctxt->myDoc); |
| 1212 | reader->ctxt->myDoc = NULL; |
| 1213 | } |
| 1214 | if (reader->allocs & XML_TEXTREADER_CTXT) { |
| 1215 | xmlFreeParserCtxt(reader->ctxt); |
| 1216 | reader->allocs -= XML_TEXTREADER_CTXT; |
| 1217 | } |
| 1218 | } |
| 1219 | if (reader->sax != NULL) { |
| 1220 | xmlFree(reader->sax); |
| 1221 | reader->sax = NULL; |
| 1222 | } |
| 1223 | if (reader->allocs & XML_TEXTREADER_INPUT) { |
| 1224 | ret = reader->input; |
| 1225 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 1226 | } else { |
| 1227 | /* |
| 1228 | * Hum, one may need to duplicate the data structure because |
| 1229 | * without reference counting the input may be freed twice: |
| 1230 | * - by the layer which allocated it. |
| 1231 | * - by the layer to which would have been returned to. |
| 1232 | */ |
| 1233 | TODO |
| 1234 | return(NULL); |
| 1235 | } |
| 1236 | return(ret); |
| 1237 | } |
| 1238 | |
| 1239 | /** |
| 1240 | * xmlTextReaderLookupNamespace: |
| 1241 | * @reader: the xmlTextReaderPtr used |
| 1242 | * @prefix: the prefix whose namespace URI is to be resolved. To return |
| 1243 | * the default namespace, specify NULL |
| 1244 | * |
| 1245 | * Resolves a namespace prefix in the scope of the current element. |
| 1246 | * |
| 1247 | * Returns a string containing the namespace URI to which the prefix maps |
| 1248 | * or NULL in case of error. The string must be deallocated by the caller. |
| 1249 | */ |
| 1250 | xmlChar * |
| 1251 | xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) { |
| 1252 | xmlNsPtr ns; |
| 1253 | |
| 1254 | if (reader == NULL) |
| 1255 | return(NULL); |
| 1256 | if (reader->node == NULL) |
| 1257 | return(NULL); |
| 1258 | |
| 1259 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 1260 | if (ns == NULL) |
| 1261 | return(NULL); |
| 1262 | return(xmlStrdup(ns->href)); |
| 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * xmlTextReaderMoveToAttributeNo: |
| 1267 | * @reader: the xmlTextReaderPtr used |
| 1268 | * @no: the zero-based index of the attribute relative to the containing |
| 1269 | * element. |
| 1270 | * |
| 1271 | * Moves the position of the current instance to the attribute with |
| 1272 | * the specified index relative to the containing element. |
| 1273 | * |
| 1274 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1275 | */ |
| 1276 | int |
| 1277 | xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) { |
| 1278 | int i; |
| 1279 | xmlAttrPtr cur; |
| 1280 | xmlNsPtr ns; |
| 1281 | |
| 1282 | if (reader == NULL) |
| 1283 | return(-1); |
| 1284 | if (reader->node == NULL) |
| 1285 | return(-1); |
| 1286 | /* TODO: handle the xmlDecl */ |
| 1287 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1288 | return(-1); |
| 1289 | |
| 1290 | reader->curnode = NULL; |
| 1291 | |
| 1292 | ns = reader->node->nsDef; |
| 1293 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 1294 | ns = ns->next; |
| 1295 | } |
| 1296 | if (ns != NULL) { |
| 1297 | reader->curnode = (xmlNodePtr) ns; |
| 1298 | return(1); |
| 1299 | } |
| 1300 | |
| 1301 | cur = reader->node->properties; |
| 1302 | if (cur == NULL) |
| 1303 | return(0); |
| 1304 | for (;i < no;i++) { |
| 1305 | cur = cur->next; |
| 1306 | if (cur == NULL) |
| 1307 | return(0); |
| 1308 | } |
| 1309 | /* TODO walk the DTD if present */ |
| 1310 | |
| 1311 | reader->curnode = (xmlNodePtr) cur; |
| 1312 | return(1); |
| 1313 | } |
| 1314 | |
| 1315 | /** |
| 1316 | * xmlTextReaderMoveToAttribute: |
| 1317 | * @reader: the xmlTextReaderPtr used |
| 1318 | * @name: the qualified name of the attribute. |
| 1319 | * |
| 1320 | * Moves the position of the current instance to the attribute with |
| 1321 | * the specified qualified name. |
| 1322 | * |
| 1323 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1324 | */ |
| 1325 | int |
| 1326 | xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 1327 | xmlChar *prefix = NULL; |
| 1328 | xmlChar *localname; |
| 1329 | xmlNsPtr ns; |
| 1330 | xmlAttrPtr prop; |
| 1331 | |
| 1332 | if ((reader == NULL) || (name == NULL)) |
| 1333 | return(-1); |
| 1334 | if (reader->node == NULL) |
| 1335 | return(-1); |
| 1336 | |
| 1337 | /* TODO: handle the xmlDecl */ |
| 1338 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1339 | return(0); |
| 1340 | |
| 1341 | localname = xmlSplitQName2(name, &prefix); |
| 1342 | if (localname == NULL) { |
| 1343 | /* |
| 1344 | * Namespace default decl |
| 1345 | */ |
| 1346 | if (xmlStrEqual(name, BAD_CAST "xmlns")) { |
| 1347 | ns = reader->node->nsDef; |
| 1348 | while (ns != NULL) { |
| 1349 | if (ns->prefix == NULL) { |
| 1350 | reader->curnode = (xmlNodePtr) ns; |
| 1351 | return(1); |
| 1352 | } |
| 1353 | ns = ns->next; |
| 1354 | } |
| 1355 | return(0); |
| 1356 | } |
| 1357 | |
| 1358 | prop = reader->node->properties; |
| 1359 | while (prop != NULL) { |
| 1360 | /* |
| 1361 | * One need to have |
| 1362 | * - same attribute names |
| 1363 | * - and the attribute carrying that namespace |
| 1364 | */ |
| 1365 | if ((xmlStrEqual(prop->name, name)) && |
| 1366 | ((prop->ns == NULL) || (prop->ns->prefix == NULL))) { |
| 1367 | reader->curnode = (xmlNodePtr) prop; |
| 1368 | return(1); |
| 1369 | } |
| 1370 | prop = prop->next; |
| 1371 | } |
| 1372 | return(0); |
| 1373 | } |
| 1374 | |
| 1375 | /* |
| 1376 | * Namespace default decl |
| 1377 | */ |
| 1378 | if (xmlStrEqual(prefix, BAD_CAST "xmlns")) { |
| 1379 | ns = reader->node->nsDef; |
| 1380 | while (ns != NULL) { |
| 1381 | if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) { |
| 1382 | reader->curnode = (xmlNodePtr) ns; |
| 1383 | goto found; |
| 1384 | } |
| 1385 | ns = ns->next; |
| 1386 | } |
| 1387 | goto not_found; |
| 1388 | } |
| 1389 | prop = reader->node->properties; |
| 1390 | while (prop != NULL) { |
| 1391 | /* |
| 1392 | * One need to have |
| 1393 | * - same attribute names |
| 1394 | * - and the attribute carrying that namespace |
| 1395 | */ |
| 1396 | if ((xmlStrEqual(prop->name, localname)) && |
| 1397 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) { |
| 1398 | reader->curnode = (xmlNodePtr) prop; |
| 1399 | goto found; |
| 1400 | } |
| 1401 | prop = prop->next; |
| 1402 | } |
| 1403 | not_found: |
| 1404 | if (localname != NULL) |
| 1405 | xmlFree(localname); |
| 1406 | if (prefix != NULL) |
| 1407 | xmlFree(prefix); |
| 1408 | return(0); |
| 1409 | |
| 1410 | found: |
| 1411 | if (localname != NULL) |
| 1412 | xmlFree(localname); |
| 1413 | if (prefix != NULL) |
| 1414 | xmlFree(prefix); |
| 1415 | return(1); |
| 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * xmlTextReaderMoveToAttributeNs: |
| 1420 | * @reader: the xmlTextReaderPtr used |
| 1421 | * @localName: the local name of the attribute. |
| 1422 | * @namespaceURI: the namespace URI of the attribute. |
| 1423 | * |
| 1424 | * Moves the position of the current instance to the attribute with the |
| 1425 | * specified local name and namespace URI. |
| 1426 | * |
| 1427 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1428 | */ |
| 1429 | int |
| 1430 | xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader, |
| 1431 | const xmlChar *localName, const xmlChar *namespaceURI) { |
| 1432 | xmlAttrPtr prop; |
| 1433 | xmlNodePtr node; |
| 1434 | |
| 1435 | if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL)) |
| 1436 | return(-1); |
| 1437 | if (reader->node == NULL) |
| 1438 | return(-1); |
| 1439 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1440 | return(0); |
| 1441 | node = reader->node; |
| 1442 | |
| 1443 | /* |
| 1444 | * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no |
| 1445 | * namespace name associated to "xmlns" |
| 1446 | */ |
| 1447 | prop = node->properties; |
| 1448 | while (prop != NULL) { |
| 1449 | /* |
| 1450 | * One need to have |
| 1451 | * - same attribute names |
| 1452 | * - and the attribute carrying that namespace |
| 1453 | */ |
| 1454 | if (xmlStrEqual(prop->name, localName) && |
| 1455 | ((prop->ns != NULL) && |
| 1456 | (xmlStrEqual(prop->ns->href, namespaceURI)))) { |
| 1457 | reader->curnode = (xmlNodePtr) prop; |
| 1458 | return(1); |
| 1459 | } |
| 1460 | prop = prop->next; |
| 1461 | } |
| 1462 | return(0); |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * xmlTextReaderMoveToFirstAttribute: |
| 1467 | * @reader: the xmlTextReaderPtr used |
| 1468 | * |
| 1469 | * Moves the position of the current instance to the first attribute |
| 1470 | * associated with the current node. |
| 1471 | * |
| 1472 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1473 | */ |
| 1474 | int |
| 1475 | xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) { |
| 1476 | if (reader == NULL) |
| 1477 | return(-1); |
| 1478 | if (reader->node == NULL) |
| 1479 | return(-1); |
| 1480 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1481 | return(0); |
| 1482 | |
| 1483 | if (reader->node->nsDef != NULL) { |
| 1484 | reader->curnode = (xmlNodePtr) reader->node->nsDef; |
| 1485 | return(1); |
| 1486 | } |
| 1487 | if (reader->node->properties != NULL) { |
| 1488 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 1489 | return(1); |
| 1490 | } |
| 1491 | return(0); |
| 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * xmlTextReaderMoveToNextAttribute: |
| 1496 | * @reader: the xmlTextReaderPtr used |
| 1497 | * |
| 1498 | * Moves the position of the current instance to the next attribute |
| 1499 | * associated with the current node. |
| 1500 | * |
| 1501 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1502 | */ |
| 1503 | int |
| 1504 | xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) { |
| 1505 | if (reader == NULL) |
| 1506 | return(-1); |
| 1507 | if (reader->node == NULL) |
| 1508 | return(-1); |
| 1509 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1510 | return(0); |
| 1511 | if (reader->curnode == NULL) |
| 1512 | return(xmlTextReaderMoveToFirstAttribute(reader)); |
| 1513 | |
| 1514 | if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 1515 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 1516 | if (ns->next != NULL) { |
| 1517 | reader->curnode = (xmlNodePtr) ns->next; |
| 1518 | return(1); |
| 1519 | } |
| 1520 | if (reader->node->properties != NULL) { |
| 1521 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 1522 | return(1); |
| 1523 | } |
| 1524 | return(0); |
| 1525 | } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) && |
| 1526 | (reader->curnode->next != NULL)) { |
| 1527 | reader->curnode = reader->curnode->next; |
| 1528 | return(1); |
| 1529 | } |
| 1530 | return(0); |
| 1531 | } |
| 1532 | |
| 1533 | /** |
| 1534 | * xmlTextReaderMoveToElement: |
| 1535 | * @reader: the xmlTextReaderPtr used |
| 1536 | * |
| 1537 | * Moves the position of the current instance to the node that |
| 1538 | * contains the current Attribute node. |
| 1539 | * |
| 1540 | * Returns 1 in case of success, -1 in case of error, 0 if not moved |
| 1541 | */ |
| 1542 | int |
| 1543 | xmlTextReaderMoveToElement(xmlTextReaderPtr reader) { |
| 1544 | if (reader == NULL) |
| 1545 | return(-1); |
| 1546 | if (reader->node == NULL) |
| 1547 | return(-1); |
| 1548 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1549 | return(0); |
| 1550 | if (reader->curnode != NULL) { |
| 1551 | reader->curnode = NULL; |
| 1552 | return(1); |
| 1553 | } |
| 1554 | return(0); |
| 1555 | } |
| 1556 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1557 | /** |
| 1558 | * xmlTextReaderReadAttributeValue: |
| 1559 | * @reader: the xmlTextReaderPtr used |
| 1560 | * |
| 1561 | * Parses an attribute value into one or more Text and EntityReference nodes. |
| 1562 | * |
| 1563 | * Returns 1 in case of success, 0 if the reader was not positionned on an |
| 1564 | * ttribute node or all the attribute values have been read, or -1 |
| 1565 | * in case of error. |
| 1566 | */ |
| 1567 | int |
| 1568 | xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) { |
| 1569 | if (reader == NULL) |
| 1570 | return(-1); |
| 1571 | if (reader->node == NULL) |
| 1572 | return(-1); |
| 1573 | if (reader->curnode == NULL) |
| 1574 | return(0); |
| 1575 | if (reader->curnode->type == XML_ATTRIBUTE_NODE) { |
| 1576 | if (reader->curnode->children == NULL) |
| 1577 | return(0); |
| 1578 | reader->curnode = reader->curnode->children; |
| 1579 | } else if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 1580 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 1581 | |
| 1582 | if (reader->faketext == NULL) { |
| 1583 | reader->faketext = xmlNewDocText(reader->node->doc, |
| 1584 | ns->href); |
| 1585 | } else { |
| 1586 | if (reader->faketext->content != NULL) |
| 1587 | xmlFree(reader->faketext->content); |
| 1588 | reader->faketext->content = xmlStrdup(ns->href); |
| 1589 | } |
| 1590 | reader->curnode = reader->faketext; |
| 1591 | } else { |
| 1592 | if (reader->curnode->next == NULL) |
| 1593 | return(0); |
| 1594 | reader->curnode = reader->curnode->next; |
| 1595 | } |
| 1596 | return(1); |
| 1597 | } |
| 1598 | |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1599 | /************************************************************************ |
| 1600 | * * |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1601 | * Acces API to the current node * |
| 1602 | * * |
| 1603 | ************************************************************************/ |
| 1604 | /** |
| 1605 | * xmlTextReaderAttributeCount: |
| 1606 | * @reader: the xmlTextReaderPtr used |
| 1607 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1608 | * Provides the number of attributes of the current node |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1609 | * |
| 1610 | * Returns 0 i no attributes, -1 in case of error or the attribute count |
| 1611 | */ |
| 1612 | int |
| 1613 | xmlTextReaderAttributeCount(xmlTextReaderPtr reader) { |
| 1614 | int ret; |
| 1615 | xmlAttrPtr attr; |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1616 | xmlNsPtr ns; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1617 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1618 | |
| 1619 | if (reader == NULL) |
| 1620 | return(-1); |
| 1621 | if (reader->node == NULL) |
| 1622 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1623 | |
| 1624 | if (reader->curnode != NULL) |
| 1625 | node = reader->curnode; |
| 1626 | else |
| 1627 | node = reader->node; |
| 1628 | |
| 1629 | if (node->type != XML_ELEMENT_NODE) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1630 | return(0); |
| 1631 | if ((reader->state == XML_TEXTREADER_END) || |
| 1632 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
| 1633 | return(0); |
| 1634 | ret = 0; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1635 | attr = node->properties; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1636 | while (attr != NULL) { |
| 1637 | ret++; |
| 1638 | attr = attr->next; |
| 1639 | } |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1640 | ns = node->nsDef; |
| 1641 | while (ns != NULL) { |
| 1642 | ret++; |
| 1643 | ns = ns->next; |
| 1644 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1645 | return(ret); |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * xmlTextReaderNodeType: |
| 1650 | * @reader: the xmlTextReaderPtr used |
| 1651 | * |
| 1652 | * Get the node type of the current node |
| 1653 | * Reference: |
| 1654 | * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html |
| 1655 | * |
| 1656 | * Returns the xmlNodeType of the current node or -1 in case of error |
| 1657 | */ |
| 1658 | int |
| 1659 | xmlTextReaderNodeType(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1660 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1661 | if (reader == NULL) |
| 1662 | return(-1); |
| 1663 | if (reader->node == NULL) |
| 1664 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1665 | if (reader->curnode != NULL) |
| 1666 | node = reader->curnode; |
| 1667 | else |
| 1668 | node = reader->node; |
| 1669 | switch (node->type) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1670 | case XML_ELEMENT_NODE: |
| 1671 | if ((reader->state == XML_TEXTREADER_END) || |
| 1672 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
| 1673 | return(15); |
| 1674 | return(1); |
Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 1675 | case XML_NAMESPACE_DECL: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1676 | case XML_ATTRIBUTE_NODE: |
| 1677 | return(2); |
| 1678 | case XML_TEXT_NODE: |
| 1679 | return(3); /* TODO: SignificantWhitespace == 14 Whitespace == 13 */ |
| 1680 | case XML_CDATA_SECTION_NODE: |
| 1681 | return(4); |
| 1682 | case XML_ENTITY_REF_NODE: |
| 1683 | return(5); |
| 1684 | case XML_ENTITY_NODE: |
| 1685 | return(6); |
| 1686 | case XML_PI_NODE: |
| 1687 | return(7); |
| 1688 | case XML_COMMENT_NODE: |
| 1689 | return(8); |
| 1690 | case XML_DOCUMENT_NODE: |
| 1691 | case XML_HTML_DOCUMENT_NODE: |
| 1692 | #ifdef LIBXML_DOCB_ENABLED |
| 1693 | case XML_DOCB_DOCUMENT_NODE: |
| 1694 | #endif |
| 1695 | return(9); |
| 1696 | case XML_DOCUMENT_FRAG_NODE: |
| 1697 | return(11); |
| 1698 | case XML_NOTATION_NODE: |
| 1699 | return(12); |
| 1700 | case XML_DOCUMENT_TYPE_NODE: |
| 1701 | case XML_DTD_NODE: |
| 1702 | return(10); |
| 1703 | |
| 1704 | case XML_ELEMENT_DECL: |
| 1705 | case XML_ATTRIBUTE_DECL: |
| 1706 | case XML_ENTITY_DECL: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1707 | case XML_XINCLUDE_START: |
| 1708 | case XML_XINCLUDE_END: |
| 1709 | return(0); |
| 1710 | } |
| 1711 | return(-1); |
| 1712 | } |
| 1713 | |
| 1714 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1715 | * xmlTextReaderIsEmptyElement: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1716 | * @reader: the xmlTextReaderPtr used |
| 1717 | * |
| 1718 | * Check if the current node is empty |
| 1719 | * |
| 1720 | * Returns 1 if empty, 0 if not and -1 in case of error |
| 1721 | */ |
| 1722 | int |
| 1723 | xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) { |
| 1724 | if ((reader == NULL) || (reader->node == NULL)) |
| 1725 | return(-1); |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 1726 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1727 | return(0); |
Daniel Veillard | e3c036e | 2003-01-01 15:11:05 +0000 | [diff] [blame] | 1728 | if (reader->curnode != NULL) |
| 1729 | return(0); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1730 | if (reader->node->children != NULL) |
| 1731 | return(0); |
Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 1732 | if (reader->state == XML_TEXTREADER_END) |
| 1733 | return(0); |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 1734 | return(reader->node->_private == (void *)xmlTextReaderIsEmpty); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * xmlTextReaderLocalName: |
| 1739 | * @reader: the xmlTextReaderPtr used |
| 1740 | * |
| 1741 | * The local name of the node. |
| 1742 | * |
| 1743 | * Returns the local name or NULL if not available |
| 1744 | */ |
| 1745 | xmlChar * |
| 1746 | xmlTextReaderLocalName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1747 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1748 | if ((reader == NULL) || (reader->node == NULL)) |
| 1749 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1750 | if (reader->curnode != NULL) |
| 1751 | node = reader->curnode; |
| 1752 | else |
| 1753 | node = reader->node; |
| 1754 | if (node->type == XML_NAMESPACE_DECL) { |
| 1755 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1756 | if (ns->prefix == NULL) |
| 1757 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 1758 | else |
| 1759 | return(xmlStrdup(ns->prefix)); |
| 1760 | } |
| 1761 | if ((node->type != XML_ELEMENT_NODE) && |
| 1762 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1763 | return(xmlTextReaderName(reader)); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1764 | return(xmlStrdup(node->name)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | /** |
| 1768 | * xmlTextReaderName: |
| 1769 | * @reader: the xmlTextReaderPtr used |
| 1770 | * |
| 1771 | * The qualified name of the node, equal to Prefix :LocalName. |
| 1772 | * |
| 1773 | * Returns the local name or NULL if not available |
| 1774 | */ |
| 1775 | xmlChar * |
| 1776 | xmlTextReaderName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1777 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1778 | xmlChar *ret; |
| 1779 | |
| 1780 | if ((reader == NULL) || (reader->node == NULL)) |
| 1781 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1782 | if (reader->curnode != NULL) |
| 1783 | node = reader->curnode; |
| 1784 | else |
| 1785 | node = reader->node; |
| 1786 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1787 | case XML_ELEMENT_NODE: |
| 1788 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1789 | if ((node->ns == NULL) || |
| 1790 | (node->ns->prefix == NULL)) |
| 1791 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1792 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1793 | ret = xmlStrdup(node->ns->prefix); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1794 | ret = xmlStrcat(ret, BAD_CAST ":"); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1795 | ret = xmlStrcat(ret, node->name); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1796 | return(ret); |
| 1797 | case XML_TEXT_NODE: |
| 1798 | return(xmlStrdup(BAD_CAST "#text")); |
| 1799 | case XML_CDATA_SECTION_NODE: |
| 1800 | return(xmlStrdup(BAD_CAST "#cdata-section")); |
| 1801 | case XML_ENTITY_NODE: |
| 1802 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1803 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1804 | case XML_PI_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1805 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1806 | case XML_COMMENT_NODE: |
| 1807 | return(xmlStrdup(BAD_CAST "#comment")); |
| 1808 | case XML_DOCUMENT_NODE: |
| 1809 | case XML_HTML_DOCUMENT_NODE: |
| 1810 | #ifdef LIBXML_DOCB_ENABLED |
| 1811 | case XML_DOCB_DOCUMENT_NODE: |
| 1812 | #endif |
| 1813 | return(xmlStrdup(BAD_CAST "#document")); |
| 1814 | case XML_DOCUMENT_FRAG_NODE: |
| 1815 | return(xmlStrdup(BAD_CAST "#document-fragment")); |
| 1816 | case XML_NOTATION_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1817 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1818 | case XML_DOCUMENT_TYPE_NODE: |
| 1819 | case XML_DTD_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1820 | return(xmlStrdup(node->name)); |
| 1821 | case XML_NAMESPACE_DECL: { |
| 1822 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1823 | |
| 1824 | ret = xmlStrdup(BAD_CAST "xmlns"); |
| 1825 | if (ns->prefix == NULL) |
| 1826 | return(ret); |
| 1827 | ret = xmlStrcat(ret, BAD_CAST ":"); |
| 1828 | ret = xmlStrcat(ret, ns->prefix); |
| 1829 | return(ret); |
| 1830 | } |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1831 | |
| 1832 | case XML_ELEMENT_DECL: |
| 1833 | case XML_ATTRIBUTE_DECL: |
| 1834 | case XML_ENTITY_DECL: |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1835 | case XML_XINCLUDE_START: |
| 1836 | case XML_XINCLUDE_END: |
| 1837 | return(NULL); |
| 1838 | } |
| 1839 | return(NULL); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | /** |
| 1843 | * xmlTextReaderPrefix: |
| 1844 | * @reader: the xmlTextReaderPtr used |
| 1845 | * |
| 1846 | * A shorthand reference to the namespace associated with the node. |
| 1847 | * |
| 1848 | * Returns the prefix or NULL if not available |
| 1849 | */ |
| 1850 | xmlChar * |
| 1851 | xmlTextReaderPrefix(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1852 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1853 | if ((reader == NULL) || (reader->node == NULL)) |
| 1854 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1855 | if (reader->curnode != NULL) |
| 1856 | node = reader->curnode; |
| 1857 | else |
| 1858 | node = reader->node; |
| 1859 | if (node->type == XML_NAMESPACE_DECL) { |
| 1860 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1861 | if (ns->prefix == NULL) |
| 1862 | return(NULL); |
| 1863 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 1864 | } |
| 1865 | if ((node->type != XML_ELEMENT_NODE) && |
| 1866 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1867 | return(NULL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1868 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1869 | return(xmlStrdup(node->ns->prefix)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1870 | return(NULL); |
| 1871 | } |
| 1872 | |
| 1873 | /** |
| 1874 | * xmlTextReaderNamespaceUri: |
| 1875 | * @reader: the xmlTextReaderPtr used |
| 1876 | * |
| 1877 | * The URI defining the namespace associated with the node. |
| 1878 | * |
| 1879 | * Returns the namespace URI or NULL if not available |
| 1880 | */ |
| 1881 | xmlChar * |
| 1882 | xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1883 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1884 | if ((reader == NULL) || (reader->node == NULL)) |
| 1885 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1886 | if (reader->curnode != NULL) |
| 1887 | node = reader->curnode; |
| 1888 | else |
| 1889 | node = reader->node; |
Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 1890 | if (node->type == XML_NAMESPACE_DECL) |
| 1891 | return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/")); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1892 | if ((node->type != XML_ELEMENT_NODE) && |
| 1893 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1894 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1895 | if (node->ns != NULL) |
| 1896 | return(xmlStrdup(node->ns->href)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1897 | return(NULL); |
| 1898 | } |
| 1899 | |
| 1900 | /** |
| 1901 | * xmlTextReaderBaseUri: |
| 1902 | * @reader: the xmlTextReaderPtr used |
| 1903 | * |
| 1904 | * The base URI of the node. |
| 1905 | * |
| 1906 | * Returns the base URI or NULL if not available |
| 1907 | */ |
| 1908 | xmlChar * |
| 1909 | xmlTextReaderBaseUri(xmlTextReaderPtr reader) { |
| 1910 | if ((reader == NULL) || (reader->node == NULL)) |
| 1911 | return(NULL); |
| 1912 | return(xmlNodeGetBase(NULL, reader->node)); |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * xmlTextReaderDepth: |
| 1917 | * @reader: the xmlTextReaderPtr used |
| 1918 | * |
| 1919 | * The depth of the node in the tree. |
| 1920 | * |
| 1921 | * Returns the depth or -1 in case of error |
| 1922 | */ |
| 1923 | int |
| 1924 | xmlTextReaderDepth(xmlTextReaderPtr reader) { |
| 1925 | if (reader == NULL) |
| 1926 | return(-1); |
| 1927 | if (reader->node == NULL) |
| 1928 | return(0); |
| 1929 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1930 | if (reader->curnode != NULL) { |
| 1931 | if ((reader->curnode->type == XML_ATTRIBUTE_NODE) || |
| 1932 | (reader->curnode->type == XML_NAMESPACE_DECL)) |
| 1933 | return(reader->depth + 1); |
| 1934 | return(reader->depth + 2); |
| 1935 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1936 | return(reader->depth); |
| 1937 | } |
| 1938 | |
| 1939 | /** |
| 1940 | * xmlTextReaderHasAttributes: |
| 1941 | * @reader: the xmlTextReaderPtr used |
| 1942 | * |
| 1943 | * Whether the node has attributes. |
| 1944 | * |
| 1945 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 1946 | */ |
| 1947 | int |
| 1948 | xmlTextReaderHasAttributes(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1949 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1950 | if (reader == NULL) |
| 1951 | return(-1); |
| 1952 | if (reader->node == NULL) |
| 1953 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1954 | if (reader->curnode != NULL) |
| 1955 | node = reader->curnode; |
| 1956 | else |
| 1957 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1958 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1959 | if ((node->type == XML_ELEMENT_NODE) && |
| 1960 | (node->properties != NULL)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1961 | return(1); |
| 1962 | /* TODO: handle the xmlDecl */ |
| 1963 | return(0); |
| 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * xmlTextReaderHasValue: |
| 1968 | * @reader: the xmlTextReaderPtr used |
| 1969 | * |
| 1970 | * Whether the node can have a text value. |
| 1971 | * |
| 1972 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 1973 | */ |
| 1974 | int |
| 1975 | xmlTextReaderHasValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1976 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1977 | if (reader == NULL) |
| 1978 | return(-1); |
| 1979 | if (reader->node == NULL) |
| 1980 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1981 | if (reader->curnode != NULL) |
| 1982 | node = reader->curnode; |
| 1983 | else |
| 1984 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1985 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1986 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1987 | case XML_ATTRIBUTE_NODE: |
| 1988 | case XML_TEXT_NODE: |
| 1989 | case XML_CDATA_SECTION_NODE: |
| 1990 | case XML_PI_NODE: |
| 1991 | case XML_COMMENT_NODE: |
| 1992 | return(1); |
| 1993 | default: |
| 1994 | return(0); |
| 1995 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1996 | return(0); |
| 1997 | } |
| 1998 | |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1999 | /** |
| 2000 | * xmlTextReaderValue: |
| 2001 | * @reader: the xmlTextReaderPtr used |
| 2002 | * |
| 2003 | * Provides the text value of the node if present |
| 2004 | * |
| 2005 | * Returns the string or NULL if not available. The retsult must be deallocated |
| 2006 | * with xmlFree() |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2007 | */ |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2008 | xmlChar * |
| 2009 | xmlTextReaderValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2010 | xmlNodePtr node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2011 | if (reader == NULL) |
| 2012 | return(NULL); |
| 2013 | if (reader->node == NULL) |
| 2014 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2015 | if (reader->curnode != NULL) |
| 2016 | node = reader->curnode; |
| 2017 | else |
| 2018 | node = reader->node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2019 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2020 | switch (node->type) { |
| 2021 | case XML_NAMESPACE_DECL: |
| 2022 | return(xmlStrdup(((xmlNsPtr) node)->href)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2023 | case XML_ATTRIBUTE_NODE:{ |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2024 | xmlAttrPtr attr = (xmlAttrPtr) node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2025 | |
| 2026 | if (attr->parent != NULL) |
| 2027 | return (xmlNodeListGetString |
| 2028 | (attr->parent->doc, attr->children, 1)); |
| 2029 | else |
| 2030 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 2031 | break; |
| 2032 | } |
| 2033 | case XML_TEXT_NODE: |
| 2034 | case XML_CDATA_SECTION_NODE: |
| 2035 | case XML_PI_NODE: |
| 2036 | case XML_COMMENT_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2037 | if (node->content != NULL) |
| 2038 | return (xmlStrdup(node->content)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2039 | default: |
| 2040 | return(NULL); |
| 2041 | } |
| 2042 | return(NULL); |
| 2043 | } |
| 2044 | |
| 2045 | /** |
| 2046 | * xmlTextReaderIsDefault: |
| 2047 | * @reader: the xmlTextReaderPtr used |
| 2048 | * |
| 2049 | * Whether an Attribute node was generated from the default value |
| 2050 | * defined in the DTD or schema. |
| 2051 | * |
| 2052 | * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error |
| 2053 | */ |
| 2054 | int |
| 2055 | xmlTextReaderIsDefault(xmlTextReaderPtr reader) { |
| 2056 | if (reader == NULL) |
| 2057 | return(-1); |
| 2058 | return(0); |
| 2059 | } |
| 2060 | |
| 2061 | /** |
| 2062 | * xmlTextReaderQuoteChar: |
| 2063 | * @reader: the xmlTextReaderPtr used |
| 2064 | * |
| 2065 | * The quotation mark character used to enclose the value of an attribute. |
| 2066 | * |
| 2067 | * Returns " or ' and -1 in case of error |
| 2068 | */ |
| 2069 | int |
| 2070 | xmlTextReaderQuoteChar(xmlTextReaderPtr reader) { |
| 2071 | if (reader == NULL) |
| 2072 | return(-1); |
| 2073 | /* TODO maybe lookup the attribute value for " first */ |
| 2074 | return((int) '"'); |
| 2075 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2076 | |
| 2077 | /** |
| 2078 | * xmlTextReaderXmlLang: |
| 2079 | * @reader: the xmlTextReaderPtr used |
| 2080 | * |
| 2081 | * The xml:lang scope within which the node resides. |
| 2082 | * |
| 2083 | * Returns the xml:lang value or NULL if none exists. |
| 2084 | */ |
| 2085 | xmlChar * |
| 2086 | xmlTextReaderXmlLang(xmlTextReaderPtr reader) { |
| 2087 | if (reader == NULL) |
| 2088 | return(NULL); |
| 2089 | if (reader->node == NULL) |
| 2090 | return(NULL); |
| 2091 | return(xmlNodeGetLang(reader->node)); |
| 2092 | } |
| 2093 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 2094 | /** |
| 2095 | * xmlTextReaderNormalization: |
| 2096 | * @reader: the xmlTextReaderPtr used |
| 2097 | * |
| 2098 | * The value indicating whether to normalize white space and attribute values. |
| 2099 | * Since attribute value and end of line normalizations are a MUST in the XML |
| 2100 | * specification only the value true is accepted. The broken bahaviour of |
| 2101 | * accepting out of range character entities like � is of course not |
| 2102 | * supported either. |
| 2103 | * |
| 2104 | * Returns 1 or -1 in case of error. |
| 2105 | */ |
| 2106 | int |
| 2107 | xmlTextReaderNormalization(xmlTextReaderPtr reader) { |
| 2108 | if (reader == NULL) |
| 2109 | return(-1); |
| 2110 | return(1); |
| 2111 | } |
| 2112 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2113 | /************************************************************************ |
| 2114 | * * |
| 2115 | * Extensions to the base APIs * |
| 2116 | * * |
| 2117 | ************************************************************************/ |
| 2118 | |
| 2119 | /** |
| 2120 | * xmlTextReaderSetParserProp: |
| 2121 | * @reader: the xmlTextReaderPtr used |
| 2122 | * @prop: the xmlParserProperties to set |
| 2123 | * @value: usually 0 or 1 to (de)activate it |
| 2124 | * |
| 2125 | * Change the parser processing behaviour by changing some of its internal |
| 2126 | * properties. Note that some properties can only be changed before any |
| 2127 | * read has been done. |
| 2128 | * |
| 2129 | * Returns 0 if the call was successful, or -1 in case of error |
| 2130 | */ |
| 2131 | int |
| 2132 | xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) { |
| 2133 | xmlParserProperties p = (xmlParserProperties) prop; |
| 2134 | xmlParserCtxtPtr ctxt; |
| 2135 | |
| 2136 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 2137 | return(-1); |
| 2138 | ctxt = reader->ctxt; |
| 2139 | |
| 2140 | switch (p) { |
| 2141 | case XML_PARSER_LOADDTD: |
| 2142 | if (value != 0) { |
| 2143 | if (ctxt->loadsubset == 0) { |
| 2144 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 2145 | return(-1); |
| 2146 | ctxt->loadsubset = XML_DETECT_IDS; |
| 2147 | } |
| 2148 | } else { |
| 2149 | ctxt->loadsubset = 0; |
| 2150 | } |
| 2151 | return(0); |
| 2152 | case XML_PARSER_DEFAULTATTRS: |
| 2153 | if (value != 0) { |
| 2154 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 2155 | } else { |
| 2156 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 2157 | ctxt->loadsubset -= XML_COMPLETE_ATTRS; |
| 2158 | } |
| 2159 | return(0); |
| 2160 | case XML_PARSER_VALIDATE: |
| 2161 | if (value != 0) { |
| 2162 | ctxt->validate = 1; |
| 2163 | } else { |
| 2164 | ctxt->validate = 0; |
| 2165 | } |
| 2166 | return(0); |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2167 | case XML_PARSER_SUBST_ENTITIES: |
| 2168 | if (value != 0) { |
| 2169 | ctxt->replaceEntities = 1; |
| 2170 | } else { |
| 2171 | ctxt->replaceEntities = 0; |
| 2172 | } |
| 2173 | return(0); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2174 | } |
| 2175 | return(-1); |
| 2176 | } |
| 2177 | |
| 2178 | /** |
| 2179 | * xmlTextReaderGetParserProp: |
| 2180 | * @reader: the xmlTextReaderPtr used |
| 2181 | * @prop: the xmlParserProperties to get |
| 2182 | * |
| 2183 | * Read the parser internal property. |
| 2184 | * |
| 2185 | * Returns the value, usually 0 or 1, or -1 in case of error. |
| 2186 | */ |
| 2187 | int |
| 2188 | xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) { |
| 2189 | xmlParserProperties p = (xmlParserProperties) prop; |
| 2190 | xmlParserCtxtPtr ctxt; |
| 2191 | |
| 2192 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 2193 | return(-1); |
| 2194 | ctxt = reader->ctxt; |
| 2195 | |
| 2196 | switch (p) { |
| 2197 | case XML_PARSER_LOADDTD: |
| 2198 | if ((ctxt->loadsubset != 0) || (ctxt->validate != 0)) |
| 2199 | return(1); |
| 2200 | return(0); |
| 2201 | case XML_PARSER_DEFAULTATTRS: |
| 2202 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 2203 | return(1); |
| 2204 | return(0); |
| 2205 | case XML_PARSER_VALIDATE: |
| 2206 | return(ctxt->validate); |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2207 | case XML_PARSER_SUBST_ENTITIES: |
| 2208 | return(ctxt->replaceEntities); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2209 | } |
| 2210 | return(-1); |
| 2211 | } |
| 2212 | |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2213 | /** |
| 2214 | * xmlTextReaderCurrentNode: |
| 2215 | * @reader: the xmlTextReaderPtr used |
| 2216 | * |
| 2217 | * Hacking interface allowing to get the xmlNodePtr correponding to the |
| 2218 | * current node being accessed by the xmlTextReader. This is dangerous |
| 2219 | * because the underlying node may be destroyed on the next Reads. |
| 2220 | * |
| 2221 | * Returns the xmlNodePtr or NULL in case of error. |
| 2222 | */ |
| 2223 | xmlNodePtr |
| 2224 | xmlTextReaderCurrentNode(xmlTextReaderPtr reader) { |
| 2225 | if (reader == NULL) |
| 2226 | return(NULL); |
| 2227 | |
| 2228 | if (reader->curnode != NULL) |
| 2229 | return(reader->curnode); |
| 2230 | return(reader->node); |
| 2231 | } |
| 2232 | |
| 2233 | /** |
| 2234 | * xmlTextReaderCurrentDoc: |
| 2235 | * @reader: the xmlTextReaderPtr used |
| 2236 | * |
| 2237 | * Hacking interface allowing to get the xmlDocPtr correponding to the |
| 2238 | * current document being accessed by the xmlTextReader. This is dangerous |
| 2239 | * because the associated node may be destroyed on the next Reads. |
| 2240 | * |
| 2241 | * Returns the xmlDocPtr or NULL in case of error. |
| 2242 | */ |
| 2243 | xmlDocPtr |
| 2244 | xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) { |
| 2245 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 2246 | return(NULL); |
| 2247 | |
| 2248 | return(reader->ctxt->myDoc); |
| 2249 | } |
| 2250 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2251 | /************************************************************************ |
| 2252 | * * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2253 | * Error Handling Extensions * |
| 2254 | * * |
| 2255 | ************************************************************************/ |
| 2256 | |
| 2257 | /* helper to build a xmlMalloc'ed string from a format and va_list */ |
| 2258 | static char * |
| 2259 | xmlTextReaderBuildMessage(const char *msg, va_list ap) { |
| 2260 | int size; |
| 2261 | int chars; |
| 2262 | char *larger; |
| 2263 | char *str; |
| 2264 | |
| 2265 | str = (char *) xmlMalloc(150); |
| 2266 | if (str == NULL) { |
| 2267 | xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n"); |
| 2268 | return NULL; |
| 2269 | } |
| 2270 | |
| 2271 | size = 150; |
| 2272 | |
| 2273 | while (1) { |
| 2274 | chars = vsnprintf(str, size, msg, ap); |
| 2275 | if ((chars > -1) && (chars < size)) |
| 2276 | break; |
| 2277 | if (chars > -1) |
| 2278 | size += chars + 1; |
| 2279 | else |
| 2280 | size += 100; |
| 2281 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) { |
| 2282 | xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n"); |
| 2283 | xmlFree(str); |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | str = larger; |
| 2287 | } |
| 2288 | |
| 2289 | return str; |
| 2290 | } |
| 2291 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2292 | /** |
Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 2293 | * xmlTextReaderLocatorLineNumber: |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2294 | * @locator: the xmlTextReaderLocatorPtr used |
| 2295 | * |
| 2296 | * Obtain the line number for the given locator. |
| 2297 | * |
| 2298 | * Returns the line number or -1 in case of error. |
| 2299 | */ |
| 2300 | int |
| 2301 | xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) { |
| 2302 | /* we know that locator is a xmlParserCtxtPtr */ |
| 2303 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
| 2304 | int ret = -1; |
| 2305 | |
| 2306 | if (ctx->node != NULL) { |
| 2307 | ret = xmlGetLineNo(ctx->node); |
| 2308 | } |
| 2309 | else { |
| 2310 | /* inspired from error.c */ |
| 2311 | xmlParserInputPtr input; |
| 2312 | input = ctx->input; |
| 2313 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
| 2314 | input = ctx->inputTab[ctx->inputNr - 2]; |
| 2315 | if (input != NULL) { |
| 2316 | ret = input->line; |
| 2317 | } |
| 2318 | else { |
| 2319 | ret = -1; |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | return ret; |
| 2324 | } |
| 2325 | |
| 2326 | /** |
Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 2327 | * xmlTextReaderLocatorBaseURI: |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2328 | * @locator: the xmlTextReaderLocatorPtr used |
| 2329 | * |
| 2330 | * Obtain the base URI for the given locator. |
| 2331 | * |
| 2332 | * Returns the base URI or NULL in case of error. |
| 2333 | */ |
| 2334 | xmlChar * |
| 2335 | xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) { |
| 2336 | /* we know that locator is a xmlParserCtxtPtr */ |
| 2337 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
| 2338 | xmlChar *ret = NULL; |
| 2339 | |
| 2340 | if (ctx->node != NULL) { |
| 2341 | ret = xmlNodeGetBase(NULL,ctx->node); |
| 2342 | } |
| 2343 | else { |
| 2344 | /* inspired from error.c */ |
| 2345 | xmlParserInputPtr input; |
| 2346 | input = ctx->input; |
| 2347 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
| 2348 | input = ctx->inputTab[ctx->inputNr - 2]; |
| 2349 | if (input != NULL) { |
| 2350 | ret = xmlStrdup(input->filename); |
| 2351 | } |
| 2352 | else { |
| 2353 | ret = NULL; |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | return ret; |
| 2358 | } |
| 2359 | |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2360 | static void |
| 2361 | xmlTextReaderGenericError(void *ctxt, int severity, char *str) { |
| 2362 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt; |
| 2363 | xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private; |
| 2364 | |
| 2365 | if (str != NULL) { |
| 2366 | reader->errorFunc(reader->errorFuncArg, |
| 2367 | str, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2368 | severity, |
| 2369 | (xmlTextReaderLocatorPtr)ctx); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2370 | xmlFree(str); |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | static void |
| 2375 | xmlTextReaderError(void *ctxt, const char *msg, ...) { |
| 2376 | va_list ap; |
| 2377 | |
| 2378 | va_start(ap,msg); |
| 2379 | xmlTextReaderGenericError(ctxt, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2380 | XML_PARSER_SEVERITY_ERROR, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2381 | xmlTextReaderBuildMessage(msg,ap)); |
| 2382 | va_end(ap); |
| 2383 | |
| 2384 | } |
| 2385 | |
| 2386 | static void |
| 2387 | xmlTextReaderWarning(void *ctxt, const char *msg, ...) { |
| 2388 | va_list ap; |
| 2389 | |
| 2390 | va_start(ap,msg); |
| 2391 | xmlTextReaderGenericError(ctxt, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2392 | XML_PARSER_SEVERITY_WARNING, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2393 | xmlTextReaderBuildMessage(msg,ap)); |
| 2394 | va_end(ap); |
| 2395 | } |
| 2396 | |
| 2397 | static void |
| 2398 | xmlTextReaderValidityError(void *ctxt, const char *msg, ...) { |
| 2399 | va_list ap; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2400 | int len = xmlStrlen((const xmlChar *) msg); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2401 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2402 | if ((len > 1) && (msg[len - 2] != ':')) { |
| 2403 | /* |
| 2404 | * some callbacks only report locator information: |
| 2405 | * skip them (mimicking behaviour in error.c) |
| 2406 | */ |
| 2407 | va_start(ap,msg); |
| 2408 | xmlTextReaderGenericError(ctxt, |
| 2409 | XML_PARSER_SEVERITY_VALIDITY_ERROR, |
| 2410 | xmlTextReaderBuildMessage(msg,ap)); |
| 2411 | va_end(ap); |
| 2412 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | static void |
| 2416 | xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) { |
| 2417 | va_list ap; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2418 | int len = xmlStrlen((const xmlChar *) msg); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2419 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2420 | if ((len != 0) && (msg[len - 1] != ':')) { |
| 2421 | /* |
| 2422 | * some callbacks only report locator information: |
| 2423 | * skip them (mimicking behaviour in error.c) |
| 2424 | */ |
| 2425 | va_start(ap,msg); |
| 2426 | xmlTextReaderGenericError(ctxt, |
| 2427 | XML_PARSER_SEVERITY_VALIDITY_WARNING, |
| 2428 | xmlTextReaderBuildMessage(msg,ap)); |
| 2429 | va_end(ap); |
| 2430 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * xmlTextReaderSetErrorHandler: |
| 2435 | * @reader: the xmlTextReaderPtr used |
| 2436 | * @f: the callback function to call on error and warnings |
| 2437 | * @arg: a user argument to pass to the callback function |
| 2438 | * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2439 | * Register a callback function that will be called on error and warnings. |
| 2440 | * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2441 | * If @f is NULL, the default error and warning handlers are restored. |
| 2442 | */ |
| 2443 | void |
| 2444 | xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader, |
| 2445 | xmlTextReaderErrorFunc f, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2446 | void *arg) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2447 | if (f != NULL) { |
| 2448 | reader->ctxt->sax->error = xmlTextReaderError; |
| 2449 | reader->ctxt->vctxt.error = xmlTextReaderValidityError; |
| 2450 | reader->ctxt->sax->warning = xmlTextReaderWarning; |
| 2451 | reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning; |
| 2452 | reader->errorFunc = f; |
| 2453 | reader->errorFuncArg = arg; |
| 2454 | } |
| 2455 | else { |
| 2456 | /* restore defaults */ |
| 2457 | reader->ctxt->sax->error = xmlParserError; |
| 2458 | reader->ctxt->vctxt.error = xmlParserValidityError; |
| 2459 | reader->ctxt->sax->warning = xmlParserWarning; |
| 2460 | reader->ctxt->vctxt.warning = xmlParserValidityWarning; |
| 2461 | reader->errorFunc = NULL; |
| 2462 | reader->errorFuncArg = NULL; |
| 2463 | } |
| 2464 | } |
| 2465 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2466 | /** |
| 2467 | * xmlTextReaderGetErrorHandler: |
| 2468 | * @reader: the xmlTextReaderPtr used |
| 2469 | * @f: the callback function or NULL is no callback has been registered |
| 2470 | * @arg: a user argument |
| 2471 | * |
| 2472 | * Retrieve the error callback function and user argument. |
| 2473 | */ |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2474 | void |
| 2475 | xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader, |
| 2476 | xmlTextReaderErrorFunc *f, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2477 | void **arg) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2478 | *f = reader->errorFunc; |
| 2479 | *arg = reader->errorFuncArg; |
| 2480 | } |
| 2481 | |
| 2482 | /************************************************************************ |
| 2483 | * * |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2484 | * Utilities * |
| 2485 | * * |
| 2486 | ************************************************************************/ |
| 2487 | /** |
| 2488 | * xmlBase64Decode: |
| 2489 | * @in: the input buffer |
| 2490 | * @inlen: the size of the input (in), the size read from it (out) |
| 2491 | * @to: the output buffer |
| 2492 | * @tolen: the size of the output (in), the size written to (out) |
| 2493 | * |
| 2494 | * Base64 decoder, reads from @in and save in @to |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2495 | * TODO: tell jody when this is actually exported |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2496 | * |
| 2497 | * Returns 0 if all the input was consumer, 1 if the Base64 end was reached, |
| 2498 | * 2 if there wasn't enough space on the output or -1 in case of error. |
| 2499 | */ |
| 2500 | static int |
| 2501 | xmlBase64Decode(const unsigned char *in, unsigned long *inlen, |
| 2502 | unsigned char *to, unsigned long *tolen) { |
| 2503 | unsigned long incur; /* current index in in[] */ |
| 2504 | unsigned long inblk; /* last block index in in[] */ |
| 2505 | unsigned long outcur; /* current index in out[] */ |
| 2506 | unsigned long inmax; /* size of in[] */ |
| 2507 | unsigned long outmax; /* size of out[] */ |
| 2508 | unsigned char cur; /* the current value read from in[] */ |
| 2509 | unsigned char intmp[3], outtmp[4]; /* temporary buffers for the convert */ |
| 2510 | int nbintmp; /* number of byte in intmp[] */ |
| 2511 | int is_ignore; /* cur should be ignored */ |
| 2512 | int is_end = 0; /* the end of the base64 was found */ |
| 2513 | int retval = 1; |
| 2514 | int i; |
| 2515 | |
| 2516 | if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL)) |
| 2517 | return(-1); |
| 2518 | |
| 2519 | incur = 0; |
| 2520 | inblk = 0; |
| 2521 | outcur = 0; |
| 2522 | inmax = *inlen; |
| 2523 | outmax = *tolen; |
| 2524 | nbintmp = 0; |
| 2525 | |
| 2526 | while (1) { |
| 2527 | if (incur >= inmax) |
| 2528 | break; |
| 2529 | cur = in[incur++]; |
| 2530 | is_ignore = 0; |
| 2531 | if ((cur >= 'A') && (cur <= 'Z')) |
| 2532 | cur = cur - 'A'; |
| 2533 | else if ((cur >= 'a') && (cur <= 'z')) |
| 2534 | cur = cur - 'a' + 26; |
| 2535 | else if ((cur >= '0') && (cur <= '9')) |
| 2536 | cur = cur - '0' + 52; |
| 2537 | else if (cur == '+') |
| 2538 | cur = 62; |
| 2539 | else if (cur == '/') |
| 2540 | cur = 63; |
| 2541 | else if (cur == '.') |
| 2542 | cur = 0; |
| 2543 | else if (cur == '=') /*no op , end of the base64 stream */ |
| 2544 | is_end = 1; |
| 2545 | else { |
| 2546 | is_ignore = 1; |
| 2547 | if (nbintmp == 0) |
| 2548 | inblk = incur; |
| 2549 | } |
| 2550 | |
| 2551 | if (!is_ignore) { |
| 2552 | int nbouttmp = 3; |
| 2553 | int is_break = 0; |
| 2554 | |
| 2555 | if (is_end) { |
| 2556 | if (nbintmp == 0) |
| 2557 | break; |
| 2558 | if ((nbintmp == 1) || (nbintmp == 2)) |
| 2559 | nbouttmp = 1; |
| 2560 | else |
| 2561 | nbouttmp = 2; |
| 2562 | nbintmp = 3; |
| 2563 | is_break = 1; |
| 2564 | } |
| 2565 | intmp[nbintmp++] = cur; |
| 2566 | /* |
| 2567 | * if intmp is full, push the 4byte sequence as a 3 byte |
| 2568 | * sequence out |
| 2569 | */ |
| 2570 | if (nbintmp == 4) { |
| 2571 | nbintmp = 0; |
| 2572 | outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4); |
| 2573 | outtmp[1] = |
| 2574 | ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2); |
| 2575 | outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F); |
| 2576 | if (outcur + 3 >= outmax) { |
| 2577 | retval = 2; |
| 2578 | break; |
| 2579 | } |
| 2580 | |
| 2581 | for (i = 0; i < nbouttmp; i++) |
| 2582 | to[outcur++] = outtmp[i]; |
| 2583 | inblk = incur; |
| 2584 | } |
| 2585 | |
| 2586 | if (is_break) { |
| 2587 | retval = 0; |
| 2588 | break; |
| 2589 | } |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | *tolen = outcur; |
| 2594 | *inlen = inblk; |
| 2595 | return (retval); |
| 2596 | } |
| 2597 | |
| 2598 | /* |
| 2599 | * Test routine for the xmlBase64Decode function |
| 2600 | */ |
| 2601 | #if 0 |
| 2602 | int main(int argc, char **argv) { |
| 2603 | char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== "; |
| 2604 | char output[100]; |
| 2605 | char output2[100]; |
| 2606 | char output3[100]; |
| 2607 | unsigned long inlen = strlen(input); |
| 2608 | unsigned long outlen = 100; |
| 2609 | int ret; |
| 2610 | unsigned long cons, tmp, tmp2, prod; |
| 2611 | |
| 2612 | /* |
| 2613 | * Direct |
| 2614 | */ |
| 2615 | ret = xmlBase64Decode(input, &inlen, output, &outlen); |
| 2616 | |
| 2617 | output[outlen] = 0; |
| 2618 | printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output); |
| 2619 | |
| 2620 | /* |
| 2621 | * output chunking |
| 2622 | */ |
| 2623 | cons = 0; |
| 2624 | prod = 0; |
| 2625 | while (cons < inlen) { |
| 2626 | tmp = 5; |
| 2627 | tmp2 = inlen - cons; |
| 2628 | |
| 2629 | printf("%ld %ld\n", cons, prod); |
| 2630 | ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp); |
| 2631 | cons += tmp2; |
| 2632 | prod += tmp; |
| 2633 | printf("%ld %ld\n", cons, prod); |
| 2634 | } |
| 2635 | output2[outlen] = 0; |
| 2636 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2); |
| 2637 | |
| 2638 | /* |
| 2639 | * input chunking |
| 2640 | */ |
| 2641 | cons = 0; |
| 2642 | prod = 0; |
| 2643 | while (cons < inlen) { |
| 2644 | tmp = 100 - prod; |
| 2645 | tmp2 = inlen - cons; |
| 2646 | if (tmp2 > 5) |
| 2647 | tmp2 = 5; |
| 2648 | |
| 2649 | printf("%ld %ld\n", cons, prod); |
| 2650 | ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp); |
| 2651 | cons += tmp2; |
| 2652 | prod += tmp; |
| 2653 | printf("%ld %ld\n", cons, prod); |
| 2654 | } |
| 2655 | output3[outlen] = 0; |
| 2656 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3); |
| 2657 | return(0); |
| 2658 | |
| 2659 | } |
| 2660 | #endif |