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