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 |
Daniel Veillard | 61c5220 | 2003-04-30 12:20:34 +0000 | [diff] [blame] | 943 | * the subtree available until the next xmlTextReaderRead() call |
Daniel Veillard | c6cae7b | 2003-04-11 09:02:11 +0000 | [diff] [blame] | 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 | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 1211 | #ifdef LIBXML_SCHEMAS_ENABLED |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 1212 | if (reader->rngSchemas != NULL) { |
| 1213 | xmlRelaxNGFree(reader->rngSchemas); |
| 1214 | reader->rngSchemas = NULL; |
| 1215 | } |
| 1216 | if (reader->rngValidCtxt != NULL) { |
| 1217 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 1218 | reader->rngValidCtxt = NULL; |
| 1219 | } |
Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 1220 | #endif |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1221 | if (reader->ctxt != NULL) { |
| 1222 | if (reader->ctxt->myDoc != NULL) { |
| 1223 | xmlFreeDoc(reader->ctxt->myDoc); |
| 1224 | reader->ctxt->myDoc = NULL; |
| 1225 | } |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 1226 | if ((reader->ctxt->vctxt.vstateTab != NULL) && |
| 1227 | (reader->ctxt->vctxt.vstateMax > 0)){ |
| 1228 | xmlFree(reader->ctxt->vctxt.vstateTab); |
| 1229 | reader->ctxt->vctxt.vstateTab = 0; |
| 1230 | reader->ctxt->vctxt.vstateMax = 0; |
| 1231 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1232 | if (reader->allocs & XML_TEXTREADER_CTXT) |
| 1233 | xmlFreeParserCtxt(reader->ctxt); |
| 1234 | } |
| 1235 | if (reader->sax != NULL) |
| 1236 | xmlFree(reader->sax); |
| 1237 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) |
| 1238 | xmlFreeParserInputBuffer(reader->input); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1239 | if (reader->faketext != NULL) { |
| 1240 | xmlFreeNode(reader->faketext); |
| 1241 | } |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 1242 | if (reader->entTab != NULL) |
| 1243 | xmlFree(reader->entTab); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1244 | xmlFree(reader); |
| 1245 | } |
| 1246 | |
| 1247 | /************************************************************************ |
| 1248 | * * |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1249 | * Methods for XmlTextReader * |
| 1250 | * * |
| 1251 | ************************************************************************/ |
| 1252 | /** |
| 1253 | * xmlTextReaderClose: |
| 1254 | * @reader: the xmlTextReaderPtr used |
| 1255 | * |
| 1256 | * This method releases any resources allocated by the current instance |
| 1257 | * changes the state to Closed and close any underlying input. |
| 1258 | * |
| 1259 | * Returns 0 or -1 in case of error |
| 1260 | */ |
| 1261 | int |
| 1262 | xmlTextReaderClose(xmlTextReaderPtr reader) { |
| 1263 | if (reader == NULL) |
| 1264 | return(-1); |
| 1265 | reader->node = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1266 | reader->curnode = NULL; |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1267 | reader->mode = XML_TEXTREADER_MODE_CLOSED; |
| 1268 | if (reader->ctxt != NULL) { |
| 1269 | if (reader->ctxt->myDoc != NULL) { |
| 1270 | xmlFreeDoc(reader->ctxt->myDoc); |
| 1271 | reader->ctxt->myDoc = NULL; |
| 1272 | } |
| 1273 | if (reader->allocs & XML_TEXTREADER_CTXT) { |
| 1274 | xmlFreeParserCtxt(reader->ctxt); |
| 1275 | reader->allocs -= XML_TEXTREADER_CTXT; |
| 1276 | } |
| 1277 | } |
| 1278 | if (reader->sax != NULL) { |
| 1279 | xmlFree(reader->sax); |
| 1280 | reader->sax = NULL; |
| 1281 | } |
| 1282 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) { |
| 1283 | xmlFreeParserInputBuffer(reader->input); |
| 1284 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 1285 | } |
| 1286 | return(0); |
| 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * xmlTextReaderGetAttributeNo: |
| 1291 | * @reader: the xmlTextReaderPtr used |
| 1292 | * @no: the zero-based index of the attribute relative to the containing element |
| 1293 | * |
| 1294 | * Provides the value of the attribute with the specified index relative |
| 1295 | * to the containing element. |
| 1296 | * |
| 1297 | * Returns a string containing the value of the specified attribute, or NULL |
| 1298 | * in case of error. The string must be deallocated by the caller. |
| 1299 | */ |
| 1300 | xmlChar * |
| 1301 | xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) { |
| 1302 | xmlChar *ret; |
| 1303 | int i; |
| 1304 | xmlAttrPtr cur; |
| 1305 | xmlNsPtr ns; |
| 1306 | |
| 1307 | if (reader == NULL) |
| 1308 | return(NULL); |
| 1309 | if (reader->node == NULL) |
| 1310 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1311 | if (reader->curnode != NULL) |
| 1312 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1313 | /* TODO: handle the xmlDecl */ |
| 1314 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1315 | return(NULL); |
| 1316 | |
| 1317 | ns = reader->node->nsDef; |
| 1318 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 1319 | ns = ns->next; |
| 1320 | } |
| 1321 | if (ns != NULL) |
| 1322 | return(xmlStrdup(ns->href)); |
| 1323 | |
| 1324 | cur = reader->node->properties; |
| 1325 | if (cur == NULL) |
| 1326 | return(NULL); |
| 1327 | for (;i < no;i++) { |
| 1328 | cur = cur->next; |
| 1329 | if (cur == NULL) |
| 1330 | return(NULL); |
| 1331 | } |
| 1332 | /* TODO walk the DTD if present */ |
| 1333 | |
| 1334 | ret = xmlNodeListGetString(reader->node->doc, cur->children, 1); |
| 1335 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 1336 | return(ret); |
| 1337 | } |
| 1338 | |
| 1339 | /** |
| 1340 | * xmlTextReaderGetAttribute: |
| 1341 | * @reader: the xmlTextReaderPtr used |
| 1342 | * @name: the qualified name of the attribute. |
| 1343 | * |
| 1344 | * Provides the value of the attribute with the specified qualified name. |
| 1345 | * |
| 1346 | * Returns a string containing the value of the specified attribute, or NULL |
| 1347 | * in case of error. The string must be deallocated by the caller. |
| 1348 | */ |
| 1349 | xmlChar * |
| 1350 | xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 1351 | xmlChar *prefix = NULL; |
| 1352 | xmlChar *localname; |
| 1353 | xmlNsPtr ns; |
| 1354 | xmlChar *ret = NULL; |
| 1355 | |
| 1356 | if ((reader == NULL) || (name == NULL)) |
| 1357 | return(NULL); |
| 1358 | if (reader->node == NULL) |
| 1359 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1360 | if (reader->curnode != NULL) |
| 1361 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1362 | |
| 1363 | /* TODO: handle the xmlDecl */ |
| 1364 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1365 | return(NULL); |
| 1366 | |
| 1367 | localname = xmlSplitQName2(name, &prefix); |
| 1368 | if (localname == NULL) |
| 1369 | return(xmlGetProp(reader->node, name)); |
| 1370 | |
| 1371 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 1372 | if (ns != NULL) |
| 1373 | ret = xmlGetNsProp(reader->node, localname, ns->href); |
| 1374 | |
| 1375 | if (localname != NULL) |
| 1376 | xmlFree(localname); |
| 1377 | if (prefix != NULL) |
| 1378 | xmlFree(prefix); |
| 1379 | return(ret); |
| 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | /** |
| 1384 | * xmlTextReaderGetAttributeNs: |
| 1385 | * @reader: the xmlTextReaderPtr used |
| 1386 | * @localName: the local name of the attribute. |
| 1387 | * @namespaceURI: the namespace URI of the attribute. |
| 1388 | * |
| 1389 | * Provides the value of the specified attribute |
| 1390 | * |
| 1391 | * Returns a string containing the value of the specified attribute, or NULL |
| 1392 | * in case of error. The string must be deallocated by the caller. |
| 1393 | */ |
| 1394 | xmlChar * |
| 1395 | xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName, |
| 1396 | const xmlChar *namespaceURI) { |
| 1397 | if ((reader == NULL) || (localName == NULL)) |
| 1398 | return(NULL); |
| 1399 | if (reader->node == NULL) |
| 1400 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1401 | if (reader->curnode != NULL) |
| 1402 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1403 | |
| 1404 | /* TODO: handle the xmlDecl */ |
| 1405 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1406 | return(NULL); |
| 1407 | |
| 1408 | return(xmlGetNsProp(reader->node, localName, namespaceURI)); |
| 1409 | } |
| 1410 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1411 | /** |
| 1412 | * xmlTextReaderGetRemainder: |
| 1413 | * @reader: the xmlTextReaderPtr used |
| 1414 | * |
| 1415 | * Method to get the remainder of the buffered XML. this method stops the |
| 1416 | * parser, set its state to End Of File and return the input stream with |
| 1417 | * what is left that the parser did not use. |
| 1418 | * |
| 1419 | * Returns the xmlParserInputBufferPtr attached to the XML or NULL |
| 1420 | * in case of error. |
| 1421 | */ |
| 1422 | xmlParserInputBufferPtr |
| 1423 | xmlTextReaderGetRemainder(xmlTextReaderPtr reader) { |
| 1424 | xmlParserInputBufferPtr ret = NULL; |
| 1425 | |
| 1426 | if (reader == NULL) |
| 1427 | return(NULL); |
| 1428 | if (reader->node == NULL) |
| 1429 | return(NULL); |
| 1430 | |
| 1431 | reader->node = NULL; |
| 1432 | reader->curnode = NULL; |
| 1433 | reader->mode = XML_TEXTREADER_MODE_EOF; |
| 1434 | if (reader->ctxt != NULL) { |
| 1435 | if (reader->ctxt->myDoc != NULL) { |
| 1436 | xmlFreeDoc(reader->ctxt->myDoc); |
| 1437 | reader->ctxt->myDoc = NULL; |
| 1438 | } |
| 1439 | if (reader->allocs & XML_TEXTREADER_CTXT) { |
| 1440 | xmlFreeParserCtxt(reader->ctxt); |
| 1441 | reader->allocs -= XML_TEXTREADER_CTXT; |
| 1442 | } |
| 1443 | } |
| 1444 | if (reader->sax != NULL) { |
| 1445 | xmlFree(reader->sax); |
| 1446 | reader->sax = NULL; |
| 1447 | } |
| 1448 | if (reader->allocs & XML_TEXTREADER_INPUT) { |
| 1449 | ret = reader->input; |
| 1450 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 1451 | } else { |
| 1452 | /* |
| 1453 | * Hum, one may need to duplicate the data structure because |
| 1454 | * without reference counting the input may be freed twice: |
| 1455 | * - by the layer which allocated it. |
| 1456 | * - by the layer to which would have been returned to. |
| 1457 | */ |
| 1458 | TODO |
| 1459 | return(NULL); |
| 1460 | } |
| 1461 | return(ret); |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * xmlTextReaderLookupNamespace: |
| 1466 | * @reader: the xmlTextReaderPtr used |
| 1467 | * @prefix: the prefix whose namespace URI is to be resolved. To return |
| 1468 | * the default namespace, specify NULL |
| 1469 | * |
| 1470 | * Resolves a namespace prefix in the scope of the current element. |
| 1471 | * |
| 1472 | * Returns a string containing the namespace URI to which the prefix maps |
| 1473 | * or NULL in case of error. The string must be deallocated by the caller. |
| 1474 | */ |
| 1475 | xmlChar * |
| 1476 | xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) { |
| 1477 | xmlNsPtr ns; |
| 1478 | |
| 1479 | if (reader == NULL) |
| 1480 | return(NULL); |
| 1481 | if (reader->node == NULL) |
| 1482 | return(NULL); |
| 1483 | |
| 1484 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 1485 | if (ns == NULL) |
| 1486 | return(NULL); |
| 1487 | return(xmlStrdup(ns->href)); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * xmlTextReaderMoveToAttributeNo: |
| 1492 | * @reader: the xmlTextReaderPtr used |
| 1493 | * @no: the zero-based index of the attribute relative to the containing |
| 1494 | * element. |
| 1495 | * |
| 1496 | * Moves the position of the current instance to the attribute with |
| 1497 | * the specified index relative to the containing element. |
| 1498 | * |
| 1499 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1500 | */ |
| 1501 | int |
| 1502 | xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) { |
| 1503 | int i; |
| 1504 | xmlAttrPtr cur; |
| 1505 | xmlNsPtr ns; |
| 1506 | |
| 1507 | if (reader == NULL) |
| 1508 | return(-1); |
| 1509 | if (reader->node == NULL) |
| 1510 | return(-1); |
| 1511 | /* TODO: handle the xmlDecl */ |
| 1512 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1513 | return(-1); |
| 1514 | |
| 1515 | reader->curnode = NULL; |
| 1516 | |
| 1517 | ns = reader->node->nsDef; |
| 1518 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 1519 | ns = ns->next; |
| 1520 | } |
| 1521 | if (ns != NULL) { |
| 1522 | reader->curnode = (xmlNodePtr) ns; |
| 1523 | return(1); |
| 1524 | } |
| 1525 | |
| 1526 | cur = reader->node->properties; |
| 1527 | if (cur == NULL) |
| 1528 | return(0); |
| 1529 | for (;i < no;i++) { |
| 1530 | cur = cur->next; |
| 1531 | if (cur == NULL) |
| 1532 | return(0); |
| 1533 | } |
| 1534 | /* TODO walk the DTD if present */ |
| 1535 | |
| 1536 | reader->curnode = (xmlNodePtr) cur; |
| 1537 | return(1); |
| 1538 | } |
| 1539 | |
| 1540 | /** |
| 1541 | * xmlTextReaderMoveToAttribute: |
| 1542 | * @reader: the xmlTextReaderPtr used |
| 1543 | * @name: the qualified name of the attribute. |
| 1544 | * |
| 1545 | * Moves the position of the current instance to the attribute with |
| 1546 | * the specified qualified name. |
| 1547 | * |
| 1548 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1549 | */ |
| 1550 | int |
| 1551 | xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 1552 | xmlChar *prefix = NULL; |
| 1553 | xmlChar *localname; |
| 1554 | xmlNsPtr ns; |
| 1555 | xmlAttrPtr prop; |
| 1556 | |
| 1557 | if ((reader == NULL) || (name == NULL)) |
| 1558 | return(-1); |
| 1559 | if (reader->node == NULL) |
| 1560 | return(-1); |
| 1561 | |
| 1562 | /* TODO: handle the xmlDecl */ |
| 1563 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1564 | return(0); |
| 1565 | |
| 1566 | localname = xmlSplitQName2(name, &prefix); |
| 1567 | if (localname == NULL) { |
| 1568 | /* |
| 1569 | * Namespace default decl |
| 1570 | */ |
| 1571 | if (xmlStrEqual(name, BAD_CAST "xmlns")) { |
| 1572 | ns = reader->node->nsDef; |
| 1573 | while (ns != NULL) { |
| 1574 | if (ns->prefix == NULL) { |
| 1575 | reader->curnode = (xmlNodePtr) ns; |
| 1576 | return(1); |
| 1577 | } |
| 1578 | ns = ns->next; |
| 1579 | } |
| 1580 | return(0); |
| 1581 | } |
| 1582 | |
| 1583 | prop = reader->node->properties; |
| 1584 | while (prop != NULL) { |
| 1585 | /* |
| 1586 | * One need to have |
| 1587 | * - same attribute names |
| 1588 | * - and the attribute carrying that namespace |
| 1589 | */ |
| 1590 | if ((xmlStrEqual(prop->name, name)) && |
| 1591 | ((prop->ns == NULL) || (prop->ns->prefix == NULL))) { |
| 1592 | reader->curnode = (xmlNodePtr) prop; |
| 1593 | return(1); |
| 1594 | } |
| 1595 | prop = prop->next; |
| 1596 | } |
| 1597 | return(0); |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * Namespace default decl |
| 1602 | */ |
| 1603 | if (xmlStrEqual(prefix, BAD_CAST "xmlns")) { |
| 1604 | ns = reader->node->nsDef; |
| 1605 | while (ns != NULL) { |
| 1606 | if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) { |
| 1607 | reader->curnode = (xmlNodePtr) ns; |
| 1608 | goto found; |
| 1609 | } |
| 1610 | ns = ns->next; |
| 1611 | } |
| 1612 | goto not_found; |
| 1613 | } |
| 1614 | prop = reader->node->properties; |
| 1615 | while (prop != NULL) { |
| 1616 | /* |
| 1617 | * One need to have |
| 1618 | * - same attribute names |
| 1619 | * - and the attribute carrying that namespace |
| 1620 | */ |
| 1621 | if ((xmlStrEqual(prop->name, localname)) && |
| 1622 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) { |
| 1623 | reader->curnode = (xmlNodePtr) prop; |
| 1624 | goto found; |
| 1625 | } |
| 1626 | prop = prop->next; |
| 1627 | } |
| 1628 | not_found: |
| 1629 | if (localname != NULL) |
| 1630 | xmlFree(localname); |
| 1631 | if (prefix != NULL) |
| 1632 | xmlFree(prefix); |
| 1633 | return(0); |
| 1634 | |
| 1635 | found: |
| 1636 | if (localname != NULL) |
| 1637 | xmlFree(localname); |
| 1638 | if (prefix != NULL) |
| 1639 | xmlFree(prefix); |
| 1640 | return(1); |
| 1641 | } |
| 1642 | |
| 1643 | /** |
| 1644 | * xmlTextReaderMoveToAttributeNs: |
| 1645 | * @reader: the xmlTextReaderPtr used |
| 1646 | * @localName: the local name of the attribute. |
| 1647 | * @namespaceURI: the namespace URI of the attribute. |
| 1648 | * |
| 1649 | * Moves the position of the current instance to the attribute with the |
| 1650 | * specified local name and namespace URI. |
| 1651 | * |
| 1652 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1653 | */ |
| 1654 | int |
| 1655 | xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader, |
| 1656 | const xmlChar *localName, const xmlChar *namespaceURI) { |
| 1657 | xmlAttrPtr prop; |
| 1658 | xmlNodePtr node; |
| 1659 | |
| 1660 | if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL)) |
| 1661 | return(-1); |
| 1662 | if (reader->node == NULL) |
| 1663 | return(-1); |
| 1664 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1665 | return(0); |
| 1666 | node = reader->node; |
| 1667 | |
| 1668 | /* |
| 1669 | * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no |
| 1670 | * namespace name associated to "xmlns" |
| 1671 | */ |
| 1672 | prop = node->properties; |
| 1673 | while (prop != NULL) { |
| 1674 | /* |
| 1675 | * One need to have |
| 1676 | * - same attribute names |
| 1677 | * - and the attribute carrying that namespace |
| 1678 | */ |
| 1679 | if (xmlStrEqual(prop->name, localName) && |
| 1680 | ((prop->ns != NULL) && |
| 1681 | (xmlStrEqual(prop->ns->href, namespaceURI)))) { |
| 1682 | reader->curnode = (xmlNodePtr) prop; |
| 1683 | return(1); |
| 1684 | } |
| 1685 | prop = prop->next; |
| 1686 | } |
| 1687 | return(0); |
| 1688 | } |
| 1689 | |
| 1690 | /** |
| 1691 | * xmlTextReaderMoveToFirstAttribute: |
| 1692 | * @reader: the xmlTextReaderPtr used |
| 1693 | * |
| 1694 | * Moves the position of the current instance to the first attribute |
| 1695 | * associated with the current node. |
| 1696 | * |
| 1697 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1698 | */ |
| 1699 | int |
| 1700 | xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) { |
| 1701 | if (reader == NULL) |
| 1702 | return(-1); |
| 1703 | if (reader->node == NULL) |
| 1704 | return(-1); |
| 1705 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1706 | return(0); |
| 1707 | |
| 1708 | if (reader->node->nsDef != NULL) { |
| 1709 | reader->curnode = (xmlNodePtr) reader->node->nsDef; |
| 1710 | return(1); |
| 1711 | } |
| 1712 | if (reader->node->properties != NULL) { |
| 1713 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 1714 | return(1); |
| 1715 | } |
| 1716 | return(0); |
| 1717 | } |
| 1718 | |
| 1719 | /** |
| 1720 | * xmlTextReaderMoveToNextAttribute: |
| 1721 | * @reader: the xmlTextReaderPtr used |
| 1722 | * |
| 1723 | * Moves the position of the current instance to the next attribute |
| 1724 | * associated with the current node. |
| 1725 | * |
| 1726 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 1727 | */ |
| 1728 | int |
| 1729 | xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) { |
| 1730 | if (reader == NULL) |
| 1731 | return(-1); |
| 1732 | if (reader->node == NULL) |
| 1733 | return(-1); |
| 1734 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1735 | return(0); |
| 1736 | if (reader->curnode == NULL) |
| 1737 | return(xmlTextReaderMoveToFirstAttribute(reader)); |
| 1738 | |
| 1739 | if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 1740 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 1741 | if (ns->next != NULL) { |
| 1742 | reader->curnode = (xmlNodePtr) ns->next; |
| 1743 | return(1); |
| 1744 | } |
| 1745 | if (reader->node->properties != NULL) { |
| 1746 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 1747 | return(1); |
| 1748 | } |
| 1749 | return(0); |
| 1750 | } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) && |
| 1751 | (reader->curnode->next != NULL)) { |
| 1752 | reader->curnode = reader->curnode->next; |
| 1753 | return(1); |
| 1754 | } |
| 1755 | return(0); |
| 1756 | } |
| 1757 | |
| 1758 | /** |
| 1759 | * xmlTextReaderMoveToElement: |
| 1760 | * @reader: the xmlTextReaderPtr used |
| 1761 | * |
| 1762 | * Moves the position of the current instance to the node that |
| 1763 | * contains the current Attribute node. |
| 1764 | * |
| 1765 | * Returns 1 in case of success, -1 in case of error, 0 if not moved |
| 1766 | */ |
| 1767 | int |
| 1768 | xmlTextReaderMoveToElement(xmlTextReaderPtr reader) { |
| 1769 | if (reader == NULL) |
| 1770 | return(-1); |
| 1771 | if (reader->node == NULL) |
| 1772 | return(-1); |
| 1773 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1774 | return(0); |
| 1775 | if (reader->curnode != NULL) { |
| 1776 | reader->curnode = NULL; |
| 1777 | return(1); |
| 1778 | } |
| 1779 | return(0); |
| 1780 | } |
| 1781 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1782 | /** |
| 1783 | * xmlTextReaderReadAttributeValue: |
| 1784 | * @reader: the xmlTextReaderPtr used |
| 1785 | * |
| 1786 | * Parses an attribute value into one or more Text and EntityReference nodes. |
| 1787 | * |
| 1788 | * Returns 1 in case of success, 0 if the reader was not positionned on an |
| 1789 | * ttribute node or all the attribute values have been read, or -1 |
| 1790 | * in case of error. |
| 1791 | */ |
| 1792 | int |
| 1793 | xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) { |
| 1794 | if (reader == NULL) |
| 1795 | return(-1); |
| 1796 | if (reader->node == NULL) |
| 1797 | return(-1); |
| 1798 | if (reader->curnode == NULL) |
| 1799 | return(0); |
| 1800 | if (reader->curnode->type == XML_ATTRIBUTE_NODE) { |
| 1801 | if (reader->curnode->children == NULL) |
| 1802 | return(0); |
| 1803 | reader->curnode = reader->curnode->children; |
| 1804 | } else if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 1805 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 1806 | |
| 1807 | if (reader->faketext == NULL) { |
| 1808 | reader->faketext = xmlNewDocText(reader->node->doc, |
| 1809 | ns->href); |
| 1810 | } else { |
| 1811 | if (reader->faketext->content != NULL) |
| 1812 | xmlFree(reader->faketext->content); |
| 1813 | reader->faketext->content = xmlStrdup(ns->href); |
| 1814 | } |
| 1815 | reader->curnode = reader->faketext; |
| 1816 | } else { |
| 1817 | if (reader->curnode->next == NULL) |
| 1818 | return(0); |
| 1819 | reader->curnode = reader->curnode->next; |
| 1820 | } |
| 1821 | return(1); |
| 1822 | } |
| 1823 | |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1824 | /************************************************************************ |
| 1825 | * * |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1826 | * Acces API to the current node * |
| 1827 | * * |
| 1828 | ************************************************************************/ |
| 1829 | /** |
| 1830 | * xmlTextReaderAttributeCount: |
| 1831 | * @reader: the xmlTextReaderPtr used |
| 1832 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1833 | * Provides the number of attributes of the current node |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1834 | * |
| 1835 | * Returns 0 i no attributes, -1 in case of error or the attribute count |
| 1836 | */ |
| 1837 | int |
| 1838 | xmlTextReaderAttributeCount(xmlTextReaderPtr reader) { |
| 1839 | int ret; |
| 1840 | xmlAttrPtr attr; |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1841 | xmlNsPtr ns; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1842 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1843 | |
| 1844 | if (reader == NULL) |
| 1845 | return(-1); |
| 1846 | if (reader->node == NULL) |
| 1847 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1848 | |
| 1849 | if (reader->curnode != NULL) |
| 1850 | node = reader->curnode; |
| 1851 | else |
| 1852 | node = reader->node; |
| 1853 | |
| 1854 | if (node->type != XML_ELEMENT_NODE) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1855 | return(0); |
| 1856 | if ((reader->state == XML_TEXTREADER_END) || |
| 1857 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
| 1858 | return(0); |
| 1859 | ret = 0; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1860 | attr = node->properties; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1861 | while (attr != NULL) { |
| 1862 | ret++; |
| 1863 | attr = attr->next; |
| 1864 | } |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1865 | ns = node->nsDef; |
| 1866 | while (ns != NULL) { |
| 1867 | ret++; |
| 1868 | ns = ns->next; |
| 1869 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1870 | return(ret); |
| 1871 | } |
| 1872 | |
| 1873 | /** |
| 1874 | * xmlTextReaderNodeType: |
| 1875 | * @reader: the xmlTextReaderPtr used |
| 1876 | * |
| 1877 | * Get the node type of the current node |
| 1878 | * Reference: |
| 1879 | * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html |
| 1880 | * |
| 1881 | * Returns the xmlNodeType of the current node or -1 in case of error |
| 1882 | */ |
| 1883 | int |
| 1884 | xmlTextReaderNodeType(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1885 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1886 | if (reader == NULL) |
| 1887 | return(-1); |
| 1888 | if (reader->node == NULL) |
| 1889 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1890 | if (reader->curnode != NULL) |
| 1891 | node = reader->curnode; |
| 1892 | else |
| 1893 | node = reader->node; |
| 1894 | switch (node->type) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1895 | case XML_ELEMENT_NODE: |
| 1896 | if ((reader->state == XML_TEXTREADER_END) || |
| 1897 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
| 1898 | return(15); |
| 1899 | return(1); |
Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 1900 | case XML_NAMESPACE_DECL: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1901 | case XML_ATTRIBUTE_NODE: |
| 1902 | return(2); |
| 1903 | case XML_TEXT_NODE: |
| 1904 | return(3); /* TODO: SignificantWhitespace == 14 Whitespace == 13 */ |
| 1905 | case XML_CDATA_SECTION_NODE: |
| 1906 | return(4); |
| 1907 | case XML_ENTITY_REF_NODE: |
| 1908 | return(5); |
| 1909 | case XML_ENTITY_NODE: |
| 1910 | return(6); |
| 1911 | case XML_PI_NODE: |
| 1912 | return(7); |
| 1913 | case XML_COMMENT_NODE: |
| 1914 | return(8); |
| 1915 | case XML_DOCUMENT_NODE: |
| 1916 | case XML_HTML_DOCUMENT_NODE: |
| 1917 | #ifdef LIBXML_DOCB_ENABLED |
| 1918 | case XML_DOCB_DOCUMENT_NODE: |
| 1919 | #endif |
| 1920 | return(9); |
| 1921 | case XML_DOCUMENT_FRAG_NODE: |
| 1922 | return(11); |
| 1923 | case XML_NOTATION_NODE: |
| 1924 | return(12); |
| 1925 | case XML_DOCUMENT_TYPE_NODE: |
| 1926 | case XML_DTD_NODE: |
| 1927 | return(10); |
| 1928 | |
| 1929 | case XML_ELEMENT_DECL: |
| 1930 | case XML_ATTRIBUTE_DECL: |
| 1931 | case XML_ENTITY_DECL: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1932 | case XML_XINCLUDE_START: |
| 1933 | case XML_XINCLUDE_END: |
| 1934 | return(0); |
| 1935 | } |
| 1936 | return(-1); |
| 1937 | } |
| 1938 | |
| 1939 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1940 | * xmlTextReaderIsEmptyElement: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1941 | * @reader: the xmlTextReaderPtr used |
| 1942 | * |
| 1943 | * Check if the current node is empty |
| 1944 | * |
| 1945 | * Returns 1 if empty, 0 if not and -1 in case of error |
| 1946 | */ |
| 1947 | int |
| 1948 | xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) { |
| 1949 | if ((reader == NULL) || (reader->node == NULL)) |
| 1950 | return(-1); |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 1951 | if (reader->node->type != XML_ELEMENT_NODE) |
| 1952 | return(0); |
Daniel Veillard | e3c036e | 2003-01-01 15:11:05 +0000 | [diff] [blame] | 1953 | if (reader->curnode != NULL) |
| 1954 | return(0); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1955 | if (reader->node->children != NULL) |
| 1956 | return(0); |
Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 1957 | if (reader->state == XML_TEXTREADER_END) |
| 1958 | return(0); |
Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 1959 | return(reader->node->_private == (void *)xmlTextReaderIsEmpty); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /** |
| 1963 | * xmlTextReaderLocalName: |
| 1964 | * @reader: the xmlTextReaderPtr used |
| 1965 | * |
| 1966 | * The local name of the node. |
| 1967 | * |
| 1968 | * Returns the local name or NULL if not available |
| 1969 | */ |
| 1970 | xmlChar * |
| 1971 | xmlTextReaderLocalName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1972 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1973 | if ((reader == NULL) || (reader->node == NULL)) |
| 1974 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1975 | if (reader->curnode != NULL) |
| 1976 | node = reader->curnode; |
| 1977 | else |
| 1978 | node = reader->node; |
| 1979 | if (node->type == XML_NAMESPACE_DECL) { |
| 1980 | xmlNsPtr ns = (xmlNsPtr) node; |
| 1981 | if (ns->prefix == NULL) |
| 1982 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 1983 | else |
| 1984 | return(xmlStrdup(ns->prefix)); |
| 1985 | } |
| 1986 | if ((node->type != XML_ELEMENT_NODE) && |
| 1987 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1988 | return(xmlTextReaderName(reader)); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1989 | return(xmlStrdup(node->name)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /** |
| 1993 | * xmlTextReaderName: |
| 1994 | * @reader: the xmlTextReaderPtr used |
| 1995 | * |
| 1996 | * The qualified name of the node, equal to Prefix :LocalName. |
| 1997 | * |
| 1998 | * Returns the local name or NULL if not available |
| 1999 | */ |
| 2000 | xmlChar * |
| 2001 | xmlTextReaderName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2002 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2003 | xmlChar *ret; |
| 2004 | |
| 2005 | if ((reader == NULL) || (reader->node == NULL)) |
| 2006 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2007 | if (reader->curnode != NULL) |
| 2008 | node = reader->curnode; |
| 2009 | else |
| 2010 | node = reader->node; |
| 2011 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2012 | case XML_ELEMENT_NODE: |
| 2013 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2014 | if ((node->ns == NULL) || |
| 2015 | (node->ns->prefix == NULL)) |
| 2016 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2017 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2018 | ret = xmlStrdup(node->ns->prefix); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2019 | ret = xmlStrcat(ret, BAD_CAST ":"); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2020 | ret = xmlStrcat(ret, node->name); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2021 | return(ret); |
| 2022 | case XML_TEXT_NODE: |
| 2023 | return(xmlStrdup(BAD_CAST "#text")); |
| 2024 | case XML_CDATA_SECTION_NODE: |
| 2025 | return(xmlStrdup(BAD_CAST "#cdata-section")); |
| 2026 | case XML_ENTITY_NODE: |
| 2027 | case XML_ENTITY_REF_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_PI_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2030 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2031 | case XML_COMMENT_NODE: |
| 2032 | return(xmlStrdup(BAD_CAST "#comment")); |
| 2033 | case XML_DOCUMENT_NODE: |
| 2034 | case XML_HTML_DOCUMENT_NODE: |
| 2035 | #ifdef LIBXML_DOCB_ENABLED |
| 2036 | case XML_DOCB_DOCUMENT_NODE: |
| 2037 | #endif |
| 2038 | return(xmlStrdup(BAD_CAST "#document")); |
| 2039 | case XML_DOCUMENT_FRAG_NODE: |
| 2040 | return(xmlStrdup(BAD_CAST "#document-fragment")); |
| 2041 | case XML_NOTATION_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2042 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2043 | case XML_DOCUMENT_TYPE_NODE: |
| 2044 | case XML_DTD_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2045 | return(xmlStrdup(node->name)); |
| 2046 | case XML_NAMESPACE_DECL: { |
| 2047 | xmlNsPtr ns = (xmlNsPtr) node; |
| 2048 | |
| 2049 | ret = xmlStrdup(BAD_CAST "xmlns"); |
| 2050 | if (ns->prefix == NULL) |
| 2051 | return(ret); |
| 2052 | ret = xmlStrcat(ret, BAD_CAST ":"); |
| 2053 | ret = xmlStrcat(ret, ns->prefix); |
| 2054 | return(ret); |
| 2055 | } |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2056 | |
| 2057 | case XML_ELEMENT_DECL: |
| 2058 | case XML_ATTRIBUTE_DECL: |
| 2059 | case XML_ENTITY_DECL: |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2060 | case XML_XINCLUDE_START: |
| 2061 | case XML_XINCLUDE_END: |
| 2062 | return(NULL); |
| 2063 | } |
| 2064 | return(NULL); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /** |
| 2068 | * xmlTextReaderPrefix: |
| 2069 | * @reader: the xmlTextReaderPtr used |
| 2070 | * |
| 2071 | * A shorthand reference to the namespace associated with the node. |
| 2072 | * |
| 2073 | * Returns the prefix or NULL if not available |
| 2074 | */ |
| 2075 | xmlChar * |
| 2076 | xmlTextReaderPrefix(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2077 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2078 | if ((reader == NULL) || (reader->node == NULL)) |
| 2079 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2080 | if (reader->curnode != NULL) |
| 2081 | node = reader->curnode; |
| 2082 | else |
| 2083 | node = reader->node; |
| 2084 | if (node->type == XML_NAMESPACE_DECL) { |
| 2085 | xmlNsPtr ns = (xmlNsPtr) node; |
| 2086 | if (ns->prefix == NULL) |
| 2087 | return(NULL); |
| 2088 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 2089 | } |
| 2090 | if ((node->type != XML_ELEMENT_NODE) && |
| 2091 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2092 | return(NULL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2093 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2094 | return(xmlStrdup(node->ns->prefix)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2095 | return(NULL); |
| 2096 | } |
| 2097 | |
| 2098 | /** |
| 2099 | * xmlTextReaderNamespaceUri: |
| 2100 | * @reader: the xmlTextReaderPtr used |
| 2101 | * |
| 2102 | * The URI defining the namespace associated with the node. |
| 2103 | * |
| 2104 | * Returns the namespace URI or NULL if not available |
| 2105 | */ |
| 2106 | xmlChar * |
| 2107 | xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2108 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2109 | if ((reader == NULL) || (reader->node == NULL)) |
| 2110 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2111 | if (reader->curnode != NULL) |
| 2112 | node = reader->curnode; |
| 2113 | else |
| 2114 | node = reader->node; |
Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 2115 | if (node->type == XML_NAMESPACE_DECL) |
| 2116 | return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/")); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2117 | if ((node->type != XML_ELEMENT_NODE) && |
| 2118 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2119 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2120 | if (node->ns != NULL) |
| 2121 | return(xmlStrdup(node->ns->href)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2122 | return(NULL); |
| 2123 | } |
| 2124 | |
| 2125 | /** |
| 2126 | * xmlTextReaderBaseUri: |
| 2127 | * @reader: the xmlTextReaderPtr used |
| 2128 | * |
| 2129 | * The base URI of the node. |
| 2130 | * |
| 2131 | * Returns the base URI or NULL if not available |
| 2132 | */ |
| 2133 | xmlChar * |
| 2134 | xmlTextReaderBaseUri(xmlTextReaderPtr reader) { |
| 2135 | if ((reader == NULL) || (reader->node == NULL)) |
| 2136 | return(NULL); |
| 2137 | return(xmlNodeGetBase(NULL, reader->node)); |
| 2138 | } |
| 2139 | |
| 2140 | /** |
| 2141 | * xmlTextReaderDepth: |
| 2142 | * @reader: the xmlTextReaderPtr used |
| 2143 | * |
| 2144 | * The depth of the node in the tree. |
| 2145 | * |
| 2146 | * Returns the depth or -1 in case of error |
| 2147 | */ |
| 2148 | int |
| 2149 | xmlTextReaderDepth(xmlTextReaderPtr reader) { |
| 2150 | if (reader == NULL) |
| 2151 | return(-1); |
| 2152 | if (reader->node == NULL) |
| 2153 | return(0); |
| 2154 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2155 | if (reader->curnode != NULL) { |
| 2156 | if ((reader->curnode->type == XML_ATTRIBUTE_NODE) || |
| 2157 | (reader->curnode->type == XML_NAMESPACE_DECL)) |
| 2158 | return(reader->depth + 1); |
| 2159 | return(reader->depth + 2); |
| 2160 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2161 | return(reader->depth); |
| 2162 | } |
| 2163 | |
| 2164 | /** |
| 2165 | * xmlTextReaderHasAttributes: |
| 2166 | * @reader: the xmlTextReaderPtr used |
| 2167 | * |
| 2168 | * Whether the node has attributes. |
| 2169 | * |
| 2170 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 2171 | */ |
| 2172 | int |
| 2173 | xmlTextReaderHasAttributes(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2174 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2175 | if (reader == NULL) |
| 2176 | return(-1); |
| 2177 | if (reader->node == NULL) |
| 2178 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2179 | if (reader->curnode != NULL) |
| 2180 | node = reader->curnode; |
| 2181 | else |
| 2182 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2183 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2184 | if ((node->type == XML_ELEMENT_NODE) && |
| 2185 | (node->properties != NULL)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2186 | return(1); |
| 2187 | /* TODO: handle the xmlDecl */ |
| 2188 | return(0); |
| 2189 | } |
| 2190 | |
| 2191 | /** |
| 2192 | * xmlTextReaderHasValue: |
| 2193 | * @reader: the xmlTextReaderPtr used |
| 2194 | * |
| 2195 | * Whether the node can have a text value. |
| 2196 | * |
| 2197 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 2198 | */ |
| 2199 | int |
| 2200 | xmlTextReaderHasValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2201 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2202 | if (reader == NULL) |
| 2203 | return(-1); |
| 2204 | if (reader->node == NULL) |
| 2205 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2206 | if (reader->curnode != NULL) |
| 2207 | node = reader->curnode; |
| 2208 | else |
| 2209 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2210 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2211 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2212 | case XML_ATTRIBUTE_NODE: |
| 2213 | case XML_TEXT_NODE: |
| 2214 | case XML_CDATA_SECTION_NODE: |
| 2215 | case XML_PI_NODE: |
| 2216 | case XML_COMMENT_NODE: |
Daniel Veillard | 9e07710 | 2003-04-10 13:36:54 +0000 | [diff] [blame] | 2217 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2218 | return(1); |
| 2219 | default: |
Daniel Veillard | 2cfd9df | 2003-03-22 22:39:16 +0000 | [diff] [blame] | 2220 | break; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2221 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2222 | return(0); |
| 2223 | } |
| 2224 | |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2225 | /** |
| 2226 | * xmlTextReaderValue: |
| 2227 | * @reader: the xmlTextReaderPtr used |
| 2228 | * |
| 2229 | * Provides the text value of the node if present |
| 2230 | * |
| 2231 | * Returns the string or NULL if not available. The retsult must be deallocated |
| 2232 | * with xmlFree() |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2233 | */ |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2234 | xmlChar * |
| 2235 | xmlTextReaderValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2236 | xmlNodePtr node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2237 | if (reader == NULL) |
| 2238 | return(NULL); |
| 2239 | if (reader->node == NULL) |
| 2240 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2241 | if (reader->curnode != NULL) |
| 2242 | node = reader->curnode; |
| 2243 | else |
| 2244 | node = reader->node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2245 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2246 | switch (node->type) { |
| 2247 | case XML_NAMESPACE_DECL: |
| 2248 | return(xmlStrdup(((xmlNsPtr) node)->href)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2249 | case XML_ATTRIBUTE_NODE:{ |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2250 | xmlAttrPtr attr = (xmlAttrPtr) node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2251 | |
| 2252 | if (attr->parent != NULL) |
| 2253 | return (xmlNodeListGetString |
| 2254 | (attr->parent->doc, attr->children, 1)); |
| 2255 | else |
| 2256 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 2257 | break; |
| 2258 | } |
| 2259 | case XML_TEXT_NODE: |
| 2260 | case XML_CDATA_SECTION_NODE: |
| 2261 | case XML_PI_NODE: |
| 2262 | case XML_COMMENT_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2263 | if (node->content != NULL) |
| 2264 | return (xmlStrdup(node->content)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2265 | default: |
Daniel Veillard | 2cfd9df | 2003-03-22 22:39:16 +0000 | [diff] [blame] | 2266 | break; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2267 | } |
| 2268 | return(NULL); |
| 2269 | } |
| 2270 | |
| 2271 | /** |
| 2272 | * xmlTextReaderIsDefault: |
| 2273 | * @reader: the xmlTextReaderPtr used |
| 2274 | * |
| 2275 | * Whether an Attribute node was generated from the default value |
| 2276 | * defined in the DTD or schema. |
| 2277 | * |
| 2278 | * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error |
| 2279 | */ |
| 2280 | int |
| 2281 | xmlTextReaderIsDefault(xmlTextReaderPtr reader) { |
| 2282 | if (reader == NULL) |
| 2283 | return(-1); |
| 2284 | return(0); |
| 2285 | } |
| 2286 | |
| 2287 | /** |
| 2288 | * xmlTextReaderQuoteChar: |
| 2289 | * @reader: the xmlTextReaderPtr used |
| 2290 | * |
| 2291 | * The quotation mark character used to enclose the value of an attribute. |
| 2292 | * |
| 2293 | * Returns " or ' and -1 in case of error |
| 2294 | */ |
| 2295 | int |
| 2296 | xmlTextReaderQuoteChar(xmlTextReaderPtr reader) { |
| 2297 | if (reader == NULL) |
| 2298 | return(-1); |
| 2299 | /* TODO maybe lookup the attribute value for " first */ |
| 2300 | return((int) '"'); |
| 2301 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2302 | |
| 2303 | /** |
| 2304 | * xmlTextReaderXmlLang: |
| 2305 | * @reader: the xmlTextReaderPtr used |
| 2306 | * |
| 2307 | * The xml:lang scope within which the node resides. |
| 2308 | * |
| 2309 | * Returns the xml:lang value or NULL if none exists. |
| 2310 | */ |
| 2311 | xmlChar * |
| 2312 | xmlTextReaderXmlLang(xmlTextReaderPtr reader) { |
| 2313 | if (reader == NULL) |
| 2314 | return(NULL); |
| 2315 | if (reader->node == NULL) |
| 2316 | return(NULL); |
| 2317 | return(xmlNodeGetLang(reader->node)); |
| 2318 | } |
| 2319 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 2320 | /** |
| 2321 | * xmlTextReaderNormalization: |
| 2322 | * @reader: the xmlTextReaderPtr used |
| 2323 | * |
| 2324 | * The value indicating whether to normalize white space and attribute values. |
| 2325 | * Since attribute value and end of line normalizations are a MUST in the XML |
| 2326 | * specification only the value true is accepted. The broken bahaviour of |
| 2327 | * accepting out of range character entities like � is of course not |
| 2328 | * supported either. |
| 2329 | * |
| 2330 | * Returns 1 or -1 in case of error. |
| 2331 | */ |
| 2332 | int |
| 2333 | xmlTextReaderNormalization(xmlTextReaderPtr reader) { |
| 2334 | if (reader == NULL) |
| 2335 | return(-1); |
| 2336 | return(1); |
| 2337 | } |
| 2338 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2339 | /************************************************************************ |
| 2340 | * * |
| 2341 | * Extensions to the base APIs * |
| 2342 | * * |
| 2343 | ************************************************************************/ |
| 2344 | |
| 2345 | /** |
| 2346 | * xmlTextReaderSetParserProp: |
| 2347 | * @reader: the xmlTextReaderPtr used |
| 2348 | * @prop: the xmlParserProperties to set |
| 2349 | * @value: usually 0 or 1 to (de)activate it |
| 2350 | * |
| 2351 | * Change the parser processing behaviour by changing some of its internal |
| 2352 | * properties. Note that some properties can only be changed before any |
| 2353 | * read has been done. |
| 2354 | * |
| 2355 | * Returns 0 if the call was successful, or -1 in case of error |
| 2356 | */ |
| 2357 | int |
| 2358 | xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) { |
| 2359 | xmlParserProperties p = (xmlParserProperties) prop; |
| 2360 | xmlParserCtxtPtr ctxt; |
| 2361 | |
| 2362 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 2363 | return(-1); |
| 2364 | ctxt = reader->ctxt; |
| 2365 | |
| 2366 | switch (p) { |
| 2367 | case XML_PARSER_LOADDTD: |
| 2368 | if (value != 0) { |
| 2369 | if (ctxt->loadsubset == 0) { |
| 2370 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 2371 | return(-1); |
| 2372 | ctxt->loadsubset = XML_DETECT_IDS; |
| 2373 | } |
| 2374 | } else { |
| 2375 | ctxt->loadsubset = 0; |
| 2376 | } |
| 2377 | return(0); |
| 2378 | case XML_PARSER_DEFAULTATTRS: |
| 2379 | if (value != 0) { |
| 2380 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 2381 | } else { |
| 2382 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 2383 | ctxt->loadsubset -= XML_COMPLETE_ATTRS; |
| 2384 | } |
| 2385 | return(0); |
| 2386 | case XML_PARSER_VALIDATE: |
| 2387 | if (value != 0) { |
| 2388 | ctxt->validate = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2389 | reader->validate = XML_TEXTREADER_VALIDATE_DTD; |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2390 | } else { |
| 2391 | ctxt->validate = 0; |
| 2392 | } |
| 2393 | return(0); |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2394 | case XML_PARSER_SUBST_ENTITIES: |
| 2395 | if (value != 0) { |
| 2396 | ctxt->replaceEntities = 1; |
| 2397 | } else { |
| 2398 | ctxt->replaceEntities = 0; |
| 2399 | } |
| 2400 | return(0); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2401 | } |
| 2402 | return(-1); |
| 2403 | } |
| 2404 | |
| 2405 | /** |
| 2406 | * xmlTextReaderGetParserProp: |
| 2407 | * @reader: the xmlTextReaderPtr used |
| 2408 | * @prop: the xmlParserProperties to get |
| 2409 | * |
| 2410 | * Read the parser internal property. |
| 2411 | * |
| 2412 | * Returns the value, usually 0 or 1, or -1 in case of error. |
| 2413 | */ |
| 2414 | int |
| 2415 | xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) { |
| 2416 | xmlParserProperties p = (xmlParserProperties) prop; |
| 2417 | xmlParserCtxtPtr ctxt; |
| 2418 | |
| 2419 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 2420 | return(-1); |
| 2421 | ctxt = reader->ctxt; |
| 2422 | |
| 2423 | switch (p) { |
| 2424 | case XML_PARSER_LOADDTD: |
| 2425 | if ((ctxt->loadsubset != 0) || (ctxt->validate != 0)) |
| 2426 | return(1); |
| 2427 | return(0); |
| 2428 | case XML_PARSER_DEFAULTATTRS: |
| 2429 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 2430 | return(1); |
| 2431 | return(0); |
| 2432 | case XML_PARSER_VALIDATE: |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2433 | return(reader->validate); |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2434 | case XML_PARSER_SUBST_ENTITIES: |
| 2435 | return(ctxt->replaceEntities); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2436 | } |
| 2437 | return(-1); |
| 2438 | } |
| 2439 | |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2440 | /** |
| 2441 | * xmlTextReaderCurrentNode: |
| 2442 | * @reader: the xmlTextReaderPtr used |
| 2443 | * |
| 2444 | * Hacking interface allowing to get the xmlNodePtr correponding to the |
| 2445 | * current node being accessed by the xmlTextReader. This is dangerous |
| 2446 | * because the underlying node may be destroyed on the next Reads. |
| 2447 | * |
| 2448 | * Returns the xmlNodePtr or NULL in case of error. |
| 2449 | */ |
| 2450 | xmlNodePtr |
| 2451 | xmlTextReaderCurrentNode(xmlTextReaderPtr reader) { |
| 2452 | if (reader == NULL) |
| 2453 | return(NULL); |
| 2454 | |
| 2455 | if (reader->curnode != NULL) |
| 2456 | return(reader->curnode); |
| 2457 | return(reader->node); |
| 2458 | } |
| 2459 | |
| 2460 | /** |
| 2461 | * xmlTextReaderCurrentDoc: |
| 2462 | * @reader: the xmlTextReaderPtr used |
| 2463 | * |
| 2464 | * Hacking interface allowing to get the xmlDocPtr correponding to the |
| 2465 | * current document being accessed by the xmlTextReader. This is dangerous |
| 2466 | * because the associated node may be destroyed on the next Reads. |
| 2467 | * |
| 2468 | * Returns the xmlDocPtr or NULL in case of error. |
| 2469 | */ |
| 2470 | xmlDocPtr |
| 2471 | xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) { |
| 2472 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 2473 | return(NULL); |
| 2474 | |
| 2475 | return(reader->ctxt->myDoc); |
| 2476 | } |
| 2477 | |
Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 2478 | #ifdef LIBXML_SCHEMAS_ENABLED |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2479 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 2480 | * xmlTextReaderRelaxNGSetSchema: |
| 2481 | * @reader: the xmlTextReaderPtr used |
| 2482 | * @schema: a precompiled RelaxNG schema |
| 2483 | * |
| 2484 | * Use RelaxNG to validate the document as it is processed. |
| 2485 | * Activation is only possible before the first Read(). |
| 2486 | * if @schema is NULL, then RelaxNG validation is desactivated. |
| 2487 | @ The @schema should not be freed until the reader is deallocated |
| 2488 | * or its use has been deactivated. |
| 2489 | * |
| 2490 | * Returns 0 in case the RelaxNG validation could be (des)activated and |
| 2491 | * -1 in case of error. |
| 2492 | */ |
| 2493 | int |
| 2494 | xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) { |
| 2495 | if (schema == NULL) { |
| 2496 | if (reader->rngSchemas != NULL) { |
| 2497 | xmlRelaxNGFree(reader->rngSchemas); |
| 2498 | reader->rngSchemas = NULL; |
| 2499 | } |
| 2500 | if (reader->rngValidCtxt != NULL) { |
| 2501 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 2502 | reader->rngValidCtxt = NULL; |
| 2503 | } |
| 2504 | return(0); |
| 2505 | } |
| 2506 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 2507 | return(-1); |
| 2508 | if (reader->rngSchemas != NULL) { |
| 2509 | xmlRelaxNGFree(reader->rngSchemas); |
| 2510 | reader->rngSchemas = NULL; |
| 2511 | } |
| 2512 | if (reader->rngValidCtxt != NULL) { |
| 2513 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 2514 | reader->rngValidCtxt = NULL; |
| 2515 | } |
| 2516 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema); |
| 2517 | if (reader->rngValidCtxt == NULL) |
| 2518 | return(-1); |
| 2519 | if (reader->errorFunc != NULL) { |
| 2520 | xmlRelaxNGSetValidErrors(reader->rngValidCtxt, |
| 2521 | (xmlRelaxNGValidityErrorFunc)reader->errorFunc, |
| 2522 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, |
| 2523 | reader->errorFuncArg); |
| 2524 | } |
| 2525 | reader->rngValidErrors = 0; |
| 2526 | reader->rngFullNode = NULL; |
| 2527 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; |
| 2528 | return(0); |
| 2529 | } |
| 2530 | |
| 2531 | /** |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2532 | * xmlTextReaderRelaxNGValidate: |
| 2533 | * @reader: the xmlTextReaderPtr used |
| 2534 | * @rng: the path to a RelaxNG schema or NULL |
| 2535 | * |
| 2536 | * Use RelaxNG to validate the document as it is processed. |
| 2537 | * Activation is only possible before the first Read(). |
| 2538 | * if @rng is NULL, then RelaxNG validation is desactivated. |
| 2539 | * |
| 2540 | * Returns 0 in case the RelaxNG validation could be (des)activated and |
| 2541 | * -1 in case of error. |
| 2542 | */ |
| 2543 | int |
| 2544 | xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) { |
| 2545 | xmlRelaxNGParserCtxtPtr ctxt; |
| 2546 | |
| 2547 | if (reader == NULL) |
| 2548 | return(-1); |
| 2549 | |
| 2550 | if (rng == NULL) { |
| 2551 | if (reader->rngSchemas != NULL) { |
| 2552 | xmlRelaxNGFree(reader->rngSchemas); |
| 2553 | reader->rngSchemas = NULL; |
| 2554 | } |
| 2555 | if (reader->rngValidCtxt != NULL) { |
| 2556 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 2557 | reader->rngValidCtxt = NULL; |
| 2558 | } |
| 2559 | return(0); |
| 2560 | } |
| 2561 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 2562 | return(-1); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 2563 | if (reader->rngSchemas != NULL) { |
| 2564 | xmlRelaxNGFree(reader->rngSchemas); |
| 2565 | reader->rngSchemas = NULL; |
| 2566 | } |
| 2567 | if (reader->rngValidCtxt != NULL) { |
| 2568 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 2569 | reader->rngValidCtxt = NULL; |
| 2570 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2571 | ctxt = xmlRelaxNGNewParserCtxt(rng); |
| 2572 | if (reader->errorFunc != NULL) { |
| 2573 | xmlRelaxNGSetParserErrors(ctxt, |
| 2574 | (xmlRelaxNGValidityErrorFunc) reader->errorFunc, |
| 2575 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, |
| 2576 | reader->errorFuncArg); |
| 2577 | } |
| 2578 | reader->rngSchemas = xmlRelaxNGParse(ctxt); |
| 2579 | xmlRelaxNGFreeParserCtxt(ctxt); |
| 2580 | if (reader->rngSchemas == NULL) |
| 2581 | return(-1); |
| 2582 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas); |
| 2583 | if (reader->rngValidCtxt == NULL) |
| 2584 | return(-1); |
| 2585 | if (reader->errorFunc != NULL) { |
| 2586 | xmlRelaxNGSetValidErrors(reader->rngValidCtxt, |
| 2587 | (xmlRelaxNGValidityErrorFunc)reader->errorFunc, |
| 2588 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, |
| 2589 | reader->errorFuncArg); |
| 2590 | } |
| 2591 | reader->rngValidErrors = 0; |
| 2592 | reader->rngFullNode = NULL; |
| 2593 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; |
| 2594 | return(0); |
| 2595 | } |
Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 2596 | #endif |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2597 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2598 | /************************************************************************ |
| 2599 | * * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2600 | * Error Handling Extensions * |
| 2601 | * * |
| 2602 | ************************************************************************/ |
| 2603 | |
| 2604 | /* helper to build a xmlMalloc'ed string from a format and va_list */ |
| 2605 | static char * |
| 2606 | xmlTextReaderBuildMessage(const char *msg, va_list ap) { |
| 2607 | int size; |
| 2608 | int chars; |
| 2609 | char *larger; |
| 2610 | char *str; |
| 2611 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2612 | str = (char *) xmlMallocAtomic(150); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2613 | if (str == NULL) { |
| 2614 | xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n"); |
| 2615 | return NULL; |
| 2616 | } |
| 2617 | |
| 2618 | size = 150; |
| 2619 | |
| 2620 | while (1) { |
| 2621 | chars = vsnprintf(str, size, msg, ap); |
| 2622 | if ((chars > -1) && (chars < size)) |
| 2623 | break; |
| 2624 | if (chars > -1) |
| 2625 | size += chars + 1; |
| 2626 | else |
| 2627 | size += 100; |
| 2628 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) { |
| 2629 | xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n"); |
| 2630 | xmlFree(str); |
| 2631 | return NULL; |
| 2632 | } |
| 2633 | str = larger; |
| 2634 | } |
| 2635 | |
| 2636 | return str; |
| 2637 | } |
| 2638 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2639 | /** |
Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 2640 | * xmlTextReaderLocatorLineNumber: |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2641 | * @locator: the xmlTextReaderLocatorPtr used |
| 2642 | * |
| 2643 | * Obtain the line number for the given locator. |
| 2644 | * |
| 2645 | * Returns the line number or -1 in case of error. |
| 2646 | */ |
| 2647 | int |
| 2648 | xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) { |
| 2649 | /* we know that locator is a xmlParserCtxtPtr */ |
| 2650 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
| 2651 | int ret = -1; |
| 2652 | |
| 2653 | if (ctx->node != NULL) { |
| 2654 | ret = xmlGetLineNo(ctx->node); |
| 2655 | } |
| 2656 | else { |
| 2657 | /* inspired from error.c */ |
| 2658 | xmlParserInputPtr input; |
| 2659 | input = ctx->input; |
| 2660 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
| 2661 | input = ctx->inputTab[ctx->inputNr - 2]; |
| 2662 | if (input != NULL) { |
| 2663 | ret = input->line; |
| 2664 | } |
| 2665 | else { |
| 2666 | ret = -1; |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | return ret; |
| 2671 | } |
| 2672 | |
| 2673 | /** |
Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 2674 | * xmlTextReaderLocatorBaseURI: |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2675 | * @locator: the xmlTextReaderLocatorPtr used |
| 2676 | * |
| 2677 | * Obtain the base URI for the given locator. |
| 2678 | * |
| 2679 | * Returns the base URI or NULL in case of error. |
| 2680 | */ |
| 2681 | xmlChar * |
| 2682 | xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) { |
| 2683 | /* we know that locator is a xmlParserCtxtPtr */ |
| 2684 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
| 2685 | xmlChar *ret = NULL; |
| 2686 | |
| 2687 | if (ctx->node != NULL) { |
| 2688 | ret = xmlNodeGetBase(NULL,ctx->node); |
| 2689 | } |
| 2690 | else { |
| 2691 | /* inspired from error.c */ |
| 2692 | xmlParserInputPtr input; |
| 2693 | input = ctx->input; |
| 2694 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
| 2695 | input = ctx->inputTab[ctx->inputNr - 2]; |
| 2696 | if (input != NULL) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2697 | ret = xmlStrdup(BAD_CAST input->filename); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2698 | } |
| 2699 | else { |
| 2700 | ret = NULL; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | return ret; |
| 2705 | } |
| 2706 | |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2707 | static void |
| 2708 | xmlTextReaderGenericError(void *ctxt, int severity, char *str) { |
| 2709 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt; |
| 2710 | xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private; |
| 2711 | |
| 2712 | if (str != NULL) { |
| 2713 | reader->errorFunc(reader->errorFuncArg, |
| 2714 | str, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2715 | severity, |
| 2716 | (xmlTextReaderLocatorPtr)ctx); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2717 | xmlFree(str); |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | static void |
| 2722 | xmlTextReaderError(void *ctxt, const char *msg, ...) { |
| 2723 | va_list ap; |
| 2724 | |
| 2725 | va_start(ap,msg); |
| 2726 | xmlTextReaderGenericError(ctxt, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2727 | XML_PARSER_SEVERITY_ERROR, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2728 | xmlTextReaderBuildMessage(msg,ap)); |
| 2729 | va_end(ap); |
| 2730 | |
| 2731 | } |
| 2732 | |
| 2733 | static void |
| 2734 | xmlTextReaderWarning(void *ctxt, const char *msg, ...) { |
| 2735 | va_list ap; |
| 2736 | |
| 2737 | va_start(ap,msg); |
| 2738 | xmlTextReaderGenericError(ctxt, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2739 | XML_PARSER_SEVERITY_WARNING, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2740 | xmlTextReaderBuildMessage(msg,ap)); |
| 2741 | va_end(ap); |
| 2742 | } |
| 2743 | |
| 2744 | static void |
| 2745 | xmlTextReaderValidityError(void *ctxt, const char *msg, ...) { |
| 2746 | va_list ap; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2747 | int len = xmlStrlen((const xmlChar *) msg); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2748 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2749 | if ((len > 1) && (msg[len - 2] != ':')) { |
| 2750 | /* |
| 2751 | * some callbacks only report locator information: |
| 2752 | * skip them (mimicking behaviour in error.c) |
| 2753 | */ |
| 2754 | va_start(ap,msg); |
| 2755 | xmlTextReaderGenericError(ctxt, |
| 2756 | XML_PARSER_SEVERITY_VALIDITY_ERROR, |
| 2757 | xmlTextReaderBuildMessage(msg,ap)); |
| 2758 | va_end(ap); |
| 2759 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | static void |
| 2763 | xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) { |
| 2764 | va_list ap; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2765 | int len = xmlStrlen((const xmlChar *) msg); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2766 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2767 | if ((len != 0) && (msg[len - 1] != ':')) { |
| 2768 | /* |
| 2769 | * some callbacks only report locator information: |
| 2770 | * skip them (mimicking behaviour in error.c) |
| 2771 | */ |
| 2772 | va_start(ap,msg); |
| 2773 | xmlTextReaderGenericError(ctxt, |
| 2774 | XML_PARSER_SEVERITY_VALIDITY_WARNING, |
| 2775 | xmlTextReaderBuildMessage(msg,ap)); |
| 2776 | va_end(ap); |
| 2777 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /** |
| 2781 | * xmlTextReaderSetErrorHandler: |
| 2782 | * @reader: the xmlTextReaderPtr used |
| 2783 | * @f: the callback function to call on error and warnings |
| 2784 | * @arg: a user argument to pass to the callback function |
| 2785 | * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2786 | * Register a callback function that will be called on error and warnings. |
| 2787 | * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2788 | * If @f is NULL, the default error and warning handlers are restored. |
| 2789 | */ |
| 2790 | void |
| 2791 | xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader, |
| 2792 | xmlTextReaderErrorFunc f, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2793 | void *arg) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2794 | if (f != NULL) { |
| 2795 | reader->ctxt->sax->error = xmlTextReaderError; |
| 2796 | reader->ctxt->vctxt.error = xmlTextReaderValidityError; |
| 2797 | reader->ctxt->sax->warning = xmlTextReaderWarning; |
| 2798 | reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning; |
| 2799 | reader->errorFunc = f; |
| 2800 | reader->errorFuncArg = arg; |
| 2801 | } |
| 2802 | else { |
| 2803 | /* restore defaults */ |
| 2804 | reader->ctxt->sax->error = xmlParserError; |
| 2805 | reader->ctxt->vctxt.error = xmlParserValidityError; |
| 2806 | reader->ctxt->sax->warning = xmlParserWarning; |
| 2807 | reader->ctxt->vctxt.warning = xmlParserValidityWarning; |
| 2808 | reader->errorFunc = NULL; |
| 2809 | reader->errorFuncArg = NULL; |
| 2810 | } |
| 2811 | } |
| 2812 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2813 | /** |
Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 2814 | * xmlTextReaderIsValid: |
| 2815 | * @reader: the xmlTextReaderPtr used |
| 2816 | * |
| 2817 | * Retrieve the validity status from the parser context |
| 2818 | * |
| 2819 | * Returns the flag value 1 if valid, 0 if no, and -1 in case of error |
| 2820 | */ |
| 2821 | int |
| 2822 | xmlTextReaderIsValid(xmlTextReaderPtr reader) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2823 | if (reader == NULL) return(-1); |
| 2824 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 2825 | if (reader->validate == XML_TEXTREADER_VALIDATE_RNG) |
| 2826 | return(reader->rngValidErrors == 0); |
| 2827 | #endif |
| 2828 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && |
| 2829 | (reader->ctxt != NULL)) |
| 2830 | return(reader->ctxt->valid); |
| 2831 | return(0); |
Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | /** |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2835 | * xmlTextReaderGetErrorHandler: |
| 2836 | * @reader: the xmlTextReaderPtr used |
| 2837 | * @f: the callback function or NULL is no callback has been registered |
| 2838 | * @arg: a user argument |
| 2839 | * |
| 2840 | * Retrieve the error callback function and user argument. |
| 2841 | */ |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2842 | void |
| 2843 | xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader, |
| 2844 | xmlTextReaderErrorFunc *f, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2845 | void **arg) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2846 | *f = reader->errorFunc; |
| 2847 | *arg = reader->errorFuncArg; |
| 2848 | } |
| 2849 | |
| 2850 | /************************************************************************ |
| 2851 | * * |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2852 | * Utilities * |
| 2853 | * * |
| 2854 | ************************************************************************/ |
| 2855 | /** |
| 2856 | * xmlBase64Decode: |
| 2857 | * @in: the input buffer |
| 2858 | * @inlen: the size of the input (in), the size read from it (out) |
| 2859 | * @to: the output buffer |
| 2860 | * @tolen: the size of the output (in), the size written to (out) |
| 2861 | * |
| 2862 | * Base64 decoder, reads from @in and save in @to |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2863 | * TODO: tell jody when this is actually exported |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2864 | * |
| 2865 | * Returns 0 if all the input was consumer, 1 if the Base64 end was reached, |
| 2866 | * 2 if there wasn't enough space on the output or -1 in case of error. |
| 2867 | */ |
| 2868 | static int |
| 2869 | xmlBase64Decode(const unsigned char *in, unsigned long *inlen, |
| 2870 | unsigned char *to, unsigned long *tolen) { |
| 2871 | unsigned long incur; /* current index in in[] */ |
| 2872 | unsigned long inblk; /* last block index in in[] */ |
| 2873 | unsigned long outcur; /* current index in out[] */ |
| 2874 | unsigned long inmax; /* size of in[] */ |
| 2875 | unsigned long outmax; /* size of out[] */ |
| 2876 | unsigned char cur; /* the current value read from in[] */ |
| 2877 | unsigned char intmp[3], outtmp[4]; /* temporary buffers for the convert */ |
| 2878 | int nbintmp; /* number of byte in intmp[] */ |
| 2879 | int is_ignore; /* cur should be ignored */ |
| 2880 | int is_end = 0; /* the end of the base64 was found */ |
| 2881 | int retval = 1; |
| 2882 | int i; |
| 2883 | |
| 2884 | if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL)) |
| 2885 | return(-1); |
| 2886 | |
| 2887 | incur = 0; |
| 2888 | inblk = 0; |
| 2889 | outcur = 0; |
| 2890 | inmax = *inlen; |
| 2891 | outmax = *tolen; |
| 2892 | nbintmp = 0; |
| 2893 | |
| 2894 | while (1) { |
| 2895 | if (incur >= inmax) |
| 2896 | break; |
| 2897 | cur = in[incur++]; |
| 2898 | is_ignore = 0; |
| 2899 | if ((cur >= 'A') && (cur <= 'Z')) |
| 2900 | cur = cur - 'A'; |
| 2901 | else if ((cur >= 'a') && (cur <= 'z')) |
| 2902 | cur = cur - 'a' + 26; |
| 2903 | else if ((cur >= '0') && (cur <= '9')) |
| 2904 | cur = cur - '0' + 52; |
| 2905 | else if (cur == '+') |
| 2906 | cur = 62; |
| 2907 | else if (cur == '/') |
| 2908 | cur = 63; |
| 2909 | else if (cur == '.') |
| 2910 | cur = 0; |
| 2911 | else if (cur == '=') /*no op , end of the base64 stream */ |
| 2912 | is_end = 1; |
| 2913 | else { |
| 2914 | is_ignore = 1; |
| 2915 | if (nbintmp == 0) |
| 2916 | inblk = incur; |
| 2917 | } |
| 2918 | |
| 2919 | if (!is_ignore) { |
| 2920 | int nbouttmp = 3; |
| 2921 | int is_break = 0; |
| 2922 | |
| 2923 | if (is_end) { |
| 2924 | if (nbintmp == 0) |
| 2925 | break; |
| 2926 | if ((nbintmp == 1) || (nbintmp == 2)) |
| 2927 | nbouttmp = 1; |
| 2928 | else |
| 2929 | nbouttmp = 2; |
| 2930 | nbintmp = 3; |
| 2931 | is_break = 1; |
| 2932 | } |
| 2933 | intmp[nbintmp++] = cur; |
| 2934 | /* |
| 2935 | * if intmp is full, push the 4byte sequence as a 3 byte |
| 2936 | * sequence out |
| 2937 | */ |
| 2938 | if (nbintmp == 4) { |
| 2939 | nbintmp = 0; |
| 2940 | outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4); |
| 2941 | outtmp[1] = |
| 2942 | ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2); |
| 2943 | outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F); |
| 2944 | if (outcur + 3 >= outmax) { |
| 2945 | retval = 2; |
| 2946 | break; |
| 2947 | } |
| 2948 | |
| 2949 | for (i = 0; i < nbouttmp; i++) |
| 2950 | to[outcur++] = outtmp[i]; |
| 2951 | inblk = incur; |
| 2952 | } |
| 2953 | |
| 2954 | if (is_break) { |
| 2955 | retval = 0; |
| 2956 | break; |
| 2957 | } |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | *tolen = outcur; |
| 2962 | *inlen = inblk; |
| 2963 | return (retval); |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * Test routine for the xmlBase64Decode function |
| 2968 | */ |
| 2969 | #if 0 |
| 2970 | int main(int argc, char **argv) { |
| 2971 | char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== "; |
| 2972 | char output[100]; |
| 2973 | char output2[100]; |
| 2974 | char output3[100]; |
| 2975 | unsigned long inlen = strlen(input); |
| 2976 | unsigned long outlen = 100; |
| 2977 | int ret; |
| 2978 | unsigned long cons, tmp, tmp2, prod; |
| 2979 | |
| 2980 | /* |
| 2981 | * Direct |
| 2982 | */ |
| 2983 | ret = xmlBase64Decode(input, &inlen, output, &outlen); |
| 2984 | |
| 2985 | output[outlen] = 0; |
| 2986 | printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output); |
| 2987 | |
| 2988 | /* |
| 2989 | * output chunking |
| 2990 | */ |
| 2991 | cons = 0; |
| 2992 | prod = 0; |
| 2993 | while (cons < inlen) { |
| 2994 | tmp = 5; |
| 2995 | tmp2 = inlen - cons; |
| 2996 | |
| 2997 | printf("%ld %ld\n", cons, prod); |
| 2998 | ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp); |
| 2999 | cons += tmp2; |
| 3000 | prod += tmp; |
| 3001 | printf("%ld %ld\n", cons, prod); |
| 3002 | } |
| 3003 | output2[outlen] = 0; |
| 3004 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2); |
| 3005 | |
| 3006 | /* |
| 3007 | * input chunking |
| 3008 | */ |
| 3009 | cons = 0; |
| 3010 | prod = 0; |
| 3011 | while (cons < inlen) { |
| 3012 | tmp = 100 - prod; |
| 3013 | tmp2 = inlen - cons; |
| 3014 | if (tmp2 > 5) |
| 3015 | tmp2 = 5; |
| 3016 | |
| 3017 | printf("%ld %ld\n", cons, prod); |
| 3018 | ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp); |
| 3019 | cons += tmp2; |
| 3020 | prod += tmp; |
| 3021 | printf("%ld %ld\n", cons, prod); |
| 3022 | } |
| 3023 | output3[outlen] = 0; |
| 3024 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3); |
| 3025 | return(0); |
| 3026 | |
| 3027 | } |
| 3028 | #endif |