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