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