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