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