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