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