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