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