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