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