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