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