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