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