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)); |
Daniel Veillard | 5963aa7 | 2005-03-10 12:23:24 +0000 | [diff] [blame] | 1592 | if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK) |
Daniel Veillard | c6cae7b | 2003-04-11 09:02:11 +0000 | [diff] [blame] | 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 | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 1615 | xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) |
| 1616 | { |
| 1617 | xmlChar *resbuf; |
| 1618 | xmlNodePtr node, cur_node; |
| 1619 | xmlBufferPtr buff, buff2; |
| 1620 | xmlDocPtr doc; |
| 1621 | |
| 1622 | if (xmlTextReaderExpand(reader) == NULL) { |
| 1623 | return NULL; |
| 1624 | } |
| 1625 | doc = reader->doc; |
| 1626 | buff = xmlBufferCreate(); |
| 1627 | for (cur_node = reader->node->children; cur_node != NULL; |
| 1628 | cur_node = cur_node->next) { |
| 1629 | node = xmlDocCopyNode(cur_node, doc, 1); |
| 1630 | buff2 = xmlBufferCreate(); |
| 1631 | if (xmlNodeDump(buff2, doc, node, 0, 0) == -1) { |
| 1632 | xmlFreeNode(node); |
| 1633 | xmlBufferFree(buff2); |
| 1634 | xmlBufferFree(buff); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | xmlBufferCat(buff, buff2->content); |
| 1638 | xmlFreeNode(node); |
| 1639 | xmlBufferFree(buff2); |
| 1640 | } |
| 1641 | resbuf = buff->content; |
| 1642 | return resbuf; |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | /** |
| 1646 | * xmlTextReaderReadOuterXml: |
| 1647 | * @reader: the xmlTextReaderPtr used |
| 1648 | * |
| 1649 | * Reads the contents of the current node, including child nodes and markup. |
| 1650 | * |
| 1651 | * Returns a string containing the XML content, or NULL if the current node |
| 1652 | * is neither an element nor attribute, or has no child nodes. The |
| 1653 | * string must be deallocated by the caller. |
| 1654 | */ |
| 1655 | xmlChar * |
Daniel Veillard | da6f4af | 2005-06-20 17:17:54 +0000 | [diff] [blame] | 1656 | xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) |
| 1657 | { |
| 1658 | xmlChar *resbuf; |
| 1659 | xmlNodePtr node; |
| 1660 | xmlBufferPtr buff; |
| 1661 | xmlDocPtr doc; |
| 1662 | |
| 1663 | node = reader->node; |
| 1664 | doc = reader->doc; |
| 1665 | if (xmlTextReaderExpand(reader) == NULL) { |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | node = xmlDocCopyNode(node, doc, 1); |
| 1669 | buff = xmlBufferCreate(); |
| 1670 | if (xmlNodeDump(buff, doc, node, 0, 0) == -1) { |
| 1671 | xmlFreeNode(node); |
| 1672 | xmlBufferFree(buff); |
| 1673 | return NULL; |
| 1674 | } |
| 1675 | |
| 1676 | resbuf = buff->content; |
| 1677 | buff->content = NULL; |
| 1678 | |
| 1679 | xmlFreeNode(node); |
| 1680 | xmlBufferFree(buff); |
| 1681 | return resbuf; |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /** |
| 1685 | * xmlTextReaderReadString: |
| 1686 | * @reader: the xmlTextReaderPtr used |
| 1687 | * |
| 1688 | * Reads the contents of an element or a text node as a string. |
| 1689 | * |
| 1690 | * Returns a string containing the contents of the Element or Text node, |
| 1691 | * or NULL if the reader is positioned on any other type of node. |
| 1692 | * The string must be deallocated by the caller. |
| 1693 | */ |
| 1694 | xmlChar * |
Daniel Veillard | e125b31 | 2005-01-28 17:39:49 +0000 | [diff] [blame] | 1695 | xmlTextReaderReadString(xmlTextReaderPtr reader) |
| 1696 | { |
| 1697 | xmlNodePtr node; |
| 1698 | |
| 1699 | if ((reader == NULL) || (reader->node == NULL)) |
| 1700 | return(NULL); |
| 1701 | |
| 1702 | node = (reader->curnode != NULL) ? reader->curnode : reader->node; |
| 1703 | switch (node->type) { |
| 1704 | case XML_TEXT_NODE: |
| 1705 | if (node->content != NULL) |
| 1706 | return(xmlStrdup(node->content)); |
| 1707 | break; |
| 1708 | case XML_ELEMENT_NODE: |
| 1709 | if (xmlTextReaderDoExpand(reader) != -1) { |
| 1710 | return xmlTextReaderCollectSiblings(node->children); |
| 1711 | } |
| 1712 | case XML_ATTRIBUTE_NODE: |
| 1713 | TODO |
| 1714 | break; |
| 1715 | default: |
| 1716 | break; |
| 1717 | } |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1718 | return(NULL); |
| 1719 | } |
| 1720 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1721 | #if 0 |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1722 | /** |
| 1723 | * xmlTextReaderReadBase64: |
| 1724 | * @reader: the xmlTextReaderPtr used |
| 1725 | * @array: a byte array to store the content. |
| 1726 | * @offset: the zero-based index into array where the method should |
| 1727 | * begin to write. |
| 1728 | * @len: the number of bytes to write. |
| 1729 | * |
| 1730 | * Reads and decodes the Base64 encoded contents of an element and |
| 1731 | * stores the result in a byte buffer. |
| 1732 | * |
| 1733 | * Returns the number of bytes written to array, or zero if the current |
| 1734 | * instance is not positioned on an element or -1 in case of error. |
| 1735 | */ |
| 1736 | int |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1737 | xmlTextReaderReadBase64(xmlTextReaderPtr reader, |
| 1738 | unsigned char *array ATTRIBUTE_UNUSED, |
| 1739 | int offset ATTRIBUTE_UNUSED, |
| 1740 | int len ATTRIBUTE_UNUSED) { |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1741 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 1742 | return(-1); |
| 1743 | if (reader->ctxt->wellFormed != 1) |
| 1744 | return(-1); |
| 1745 | |
| 1746 | if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE)) |
| 1747 | return(0); |
| 1748 | TODO |
| 1749 | return(0); |
| 1750 | } |
| 1751 | |
| 1752 | /** |
| 1753 | * xmlTextReaderReadBinHex: |
| 1754 | * @reader: the xmlTextReaderPtr used |
| 1755 | * @array: a byte array to store the content. |
| 1756 | * @offset: the zero-based index into array where the method should |
| 1757 | * begin to write. |
| 1758 | * @len: the number of bytes to write. |
| 1759 | * |
| 1760 | * Reads and decodes the BinHex encoded contents of an element and |
| 1761 | * stores the result in a byte buffer. |
| 1762 | * |
| 1763 | * Returns the number of bytes written to array, or zero if the current |
| 1764 | * instance is not positioned on an element or -1 in case of error. |
| 1765 | */ |
| 1766 | int |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1767 | xmlTextReaderReadBinHex(xmlTextReaderPtr reader, |
| 1768 | unsigned char *array ATTRIBUTE_UNUSED, |
| 1769 | int offset ATTRIBUTE_UNUSED, |
| 1770 | int len ATTRIBUTE_UNUSED) { |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1771 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 1772 | return(-1); |
| 1773 | if (reader->ctxt->wellFormed != 1) |
| 1774 | return(-1); |
| 1775 | |
| 1776 | if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE)) |
| 1777 | return(0); |
| 1778 | TODO |
| 1779 | return(0); |
| 1780 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1781 | #endif |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1782 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1783 | /************************************************************************ |
| 1784 | * * |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1785 | * Operating on a preparsed tree * |
| 1786 | * * |
| 1787 | ************************************************************************/ |
| 1788 | static int |
| 1789 | xmlTextReaderNextTree(xmlTextReaderPtr reader) |
| 1790 | { |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1791 | if (reader == NULL) |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1792 | return(-1); |
| 1793 | |
| 1794 | if (reader->state == XML_TEXTREADER_END) |
| 1795 | return(0); |
| 1796 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1797 | if (reader->node == NULL) { |
| 1798 | if (reader->doc->children == NULL) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1799 | reader->state = XML_TEXTREADER_END; |
| 1800 | return(0); |
| 1801 | } |
| 1802 | |
| 1803 | reader->node = reader->doc->children; |
| 1804 | reader->state = XML_TEXTREADER_START; |
| 1805 | return(1); |
| 1806 | } |
| 1807 | |
| 1808 | if (reader->state != XML_TEXTREADER_BACKTRACK) { |
| 1809 | if (reader->node->children != 0) { |
| 1810 | reader->node = reader->node->children; |
| 1811 | reader->depth++; |
| 1812 | reader->state = XML_TEXTREADER_START; |
| 1813 | return(1); |
| 1814 | } |
| 1815 | |
| 1816 | if ((reader->node->type == XML_ELEMENT_NODE) || |
| 1817 | (reader->node->type == XML_ATTRIBUTE_NODE)) { |
| 1818 | reader->state = XML_TEXTREADER_BACKTRACK; |
| 1819 | return(1); |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | if (reader->node->next != 0) { |
| 1824 | reader->node = reader->node->next; |
| 1825 | reader->state = XML_TEXTREADER_START; |
| 1826 | return(1); |
| 1827 | } |
| 1828 | |
| 1829 | if (reader->node->parent != 0) { |
| 1830 | if (reader->node->parent->type == XML_DOCUMENT_NODE) { |
| 1831 | reader->state = XML_TEXTREADER_END; |
| 1832 | return(0); |
| 1833 | } |
| 1834 | |
| 1835 | reader->node = reader->node->parent; |
| 1836 | reader->depth--; |
| 1837 | reader->state = XML_TEXTREADER_BACKTRACK; |
| 1838 | return(1); |
| 1839 | } |
| 1840 | |
| 1841 | reader->state = XML_TEXTREADER_END; |
| 1842 | |
| 1843 | return(1); |
| 1844 | } |
| 1845 | |
| 1846 | /** |
| 1847 | * xmlTextReaderReadTree: |
| 1848 | * @reader: the xmlTextReaderPtr used |
| 1849 | * |
| 1850 | * Moves the position of the current instance to the next node in |
| 1851 | * the stream, exposing its properties. |
| 1852 | * |
| 1853 | * Returns 1 if the node was read successfully, 0 if there is no more |
| 1854 | * nodes to read, or -1 in case of error |
| 1855 | */ |
| 1856 | static int |
| 1857 | xmlTextReaderReadTree(xmlTextReaderPtr reader) { |
| 1858 | if (reader->state == XML_TEXTREADER_END) |
| 1859 | return(0); |
| 1860 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1861 | next_node: |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1862 | if (reader->node == NULL) { |
| 1863 | if (reader->doc->children == NULL) { |
| 1864 | reader->state = XML_TEXTREADER_END; |
| 1865 | return(0); |
| 1866 | } |
| 1867 | |
| 1868 | reader->node = reader->doc->children; |
| 1869 | reader->state = XML_TEXTREADER_START; |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1870 | goto found_node; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1873 | if ((reader->state != XML_TEXTREADER_BACKTRACK) && |
| 1874 | (reader->node->type != XML_DTD_NODE) && |
| 1875 | (reader->node->type != XML_XINCLUDE_START) && |
| 1876 | (reader->node->type != XML_ENTITY_REF_NODE)) { |
| 1877 | if (reader->node->children != NULL) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1878 | reader->node = reader->node->children; |
| 1879 | reader->depth++; |
| 1880 | reader->state = XML_TEXTREADER_START; |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1881 | goto found_node; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1884 | if (reader->node->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1885 | reader->state = XML_TEXTREADER_BACKTRACK; |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1886 | goto found_node; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1890 | if (reader->node->next != NULL) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1891 | reader->node = reader->node->next; |
| 1892 | reader->state = XML_TEXTREADER_START; |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1893 | goto found_node; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1896 | if (reader->node->parent != NULL) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1897 | if ((reader->node->parent->type == XML_DOCUMENT_NODE) || |
| 1898 | (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) { |
| 1899 | reader->state = XML_TEXTREADER_END; |
| 1900 | return(0); |
| 1901 | } |
| 1902 | |
| 1903 | reader->node = reader->node->parent; |
| 1904 | reader->depth--; |
| 1905 | reader->state = XML_TEXTREADER_BACKTRACK; |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1906 | goto found_node; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | reader->state = XML_TEXTREADER_END; |
| 1910 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1911 | found_node: |
| 1912 | if ((reader->node->type == XML_XINCLUDE_START) || |
| 1913 | (reader->node->type == XML_XINCLUDE_END)) |
| 1914 | goto next_node; |
| 1915 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1916 | return(1); |
| 1917 | } |
| 1918 | |
| 1919 | /** |
William M. Brack | b1d5316 | 2003-11-18 06:54:40 +0000 | [diff] [blame] | 1920 | * xmlTextReaderNextSibling: |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1921 | * @reader: the xmlTextReaderPtr used |
| 1922 | * |
| 1923 | * Skip to the node following the current one in document order while |
| 1924 | * avoiding the subtree if any. |
| 1925 | * Currently implemented only for Readers built on a document |
| 1926 | * |
| 1927 | * Returns 1 if the node was read successfully, 0 if there is no more |
| 1928 | * nodes to read, or -1 in case of error |
| 1929 | */ |
| 1930 | int |
| 1931 | xmlTextReaderNextSibling(xmlTextReaderPtr reader) { |
| 1932 | if (reader == NULL) |
| 1933 | return(-1); |
| 1934 | if (reader->doc == NULL) { |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 1935 | /* TODO */ |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1936 | return(-1); |
| 1937 | } |
| 1938 | |
| 1939 | if (reader->state == XML_TEXTREADER_END) |
| 1940 | return(0); |
| 1941 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1942 | if (reader->node == NULL) |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1943 | return(xmlTextReaderNextTree(reader)); |
| 1944 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 1945 | if (reader->node->next != NULL) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1946 | reader->node = reader->node->next; |
| 1947 | reader->state = XML_TEXTREADER_START; |
| 1948 | return(1); |
| 1949 | } |
| 1950 | |
| 1951 | return(0); |
| 1952 | } |
| 1953 | |
| 1954 | /************************************************************************ |
| 1955 | * * |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1956 | * Constructor and destructors * |
| 1957 | * * |
| 1958 | ************************************************************************/ |
| 1959 | /** |
| 1960 | * xmlNewTextReader: |
| 1961 | * @input: the xmlParserInputBufferPtr used to read data |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1962 | * @URI: the URI information for the source if available |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1963 | * |
| 1964 | * Create an xmlTextReader structure fed with @input |
| 1965 | * |
| 1966 | * Returns the new xmlTextReaderPtr or NULL in case of error |
| 1967 | */ |
| 1968 | xmlTextReaderPtr |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1969 | xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1970 | xmlTextReaderPtr ret; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1971 | |
| 1972 | if (input == NULL) |
| 1973 | return(NULL); |
| 1974 | ret = xmlMalloc(sizeof(xmlTextReader)); |
| 1975 | if (ret == NULL) { |
| 1976 | xmlGenericError(xmlGenericErrorContext, |
| 1977 | "xmlNewTextReader : malloc failed\n"); |
| 1978 | return(NULL); |
| 1979 | } |
| 1980 | memset(ret, 0, sizeof(xmlTextReader)); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1981 | ret->doc = NULL; |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 1982 | ret->entTab = NULL; |
| 1983 | ret->entMax = 0; |
| 1984 | ret->entNr = 0; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1985 | ret->input = input; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 1986 | ret->buffer = xmlBufferCreateSize(100); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 1987 | if (ret->buffer == NULL) { |
| 1988 | xmlFree(ret); |
| 1989 | xmlGenericError(xmlGenericErrorContext, |
| 1990 | "xmlNewTextReader : malloc failed\n"); |
| 1991 | return(NULL); |
| 1992 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1993 | ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
| 1994 | if (ret->sax == NULL) { |
William M. Brack | 42331a9 | 2004-07-29 07:07:16 +0000 | [diff] [blame] | 1995 | xmlBufferFree(ret->buffer); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1996 | xmlFree(ret); |
| 1997 | xmlGenericError(xmlGenericErrorContext, |
| 1998 | "xmlNewTextReader : malloc failed\n"); |
| 1999 | return(NULL); |
| 2000 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 2001 | xmlSAXVersion(ret->sax, 2); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2002 | ret->startElement = ret->sax->startElement; |
| 2003 | ret->sax->startElement = xmlTextReaderStartElement; |
| 2004 | ret->endElement = ret->sax->endElement; |
| 2005 | ret->sax->endElement = xmlTextReaderEndElement; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 2006 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 2007 | if (ret->sax->initialized == XML_SAX2_MAGIC) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 2008 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 2009 | ret->startElementNs = ret->sax->startElementNs; |
| 2010 | ret->sax->startElementNs = xmlTextReaderStartElementNs; |
| 2011 | ret->endElementNs = ret->sax->endElementNs; |
| 2012 | ret->sax->endElementNs = xmlTextReaderEndElementNs; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 2013 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 2014 | } else { |
| 2015 | ret->startElementNs = NULL; |
| 2016 | ret->endElementNs = NULL; |
| 2017 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 2018 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2019 | ret->characters = ret->sax->characters; |
| 2020 | ret->sax->characters = xmlTextReaderCharacters; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 2021 | ret->sax->ignorableWhitespace = xmlTextReaderCharacters; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2022 | ret->cdataBlock = ret->sax->cdataBlock; |
| 2023 | ret->sax->cdataBlock = xmlTextReaderCDataBlock; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2024 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 2025 | ret->mode = XML_TEXTREADER_MODE_INITIAL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2026 | ret->node = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2027 | ret->curnode = NULL; |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 2028 | if (ret->input->buffer->use < 4) { |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 2029 | xmlParserInputBufferRead(input, 4); |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 2030 | } |
| 2031 | if (ret->input->buffer->use >= 4) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2032 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2033 | (const char *) ret->input->buffer->content, 4, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2034 | ret->base = 0; |
| 2035 | ret->cur = 4; |
| 2036 | } else { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2037 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2038 | ret->base = 0; |
| 2039 | ret->cur = 0; |
| 2040 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 2041 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2042 | if (ret->ctxt == NULL) { |
| 2043 | xmlGenericError(xmlGenericErrorContext, |
| 2044 | "xmlNewTextReader : malloc failed\n"); |
William M. Brack | 42331a9 | 2004-07-29 07:07:16 +0000 | [diff] [blame] | 2045 | xmlBufferFree(ret->buffer); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2046 | xmlFree(ret->sax); |
| 2047 | xmlFree(ret); |
| 2048 | return(NULL); |
| 2049 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 2050 | ret->ctxt->parseMode = XML_PARSE_READER; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2051 | ret->ctxt->_private = ret; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2052 | ret->ctxt->linenumbers = 1; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 2053 | ret->ctxt->dictNames = 1; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2054 | ret->allocs = XML_TEXTREADER_CTXT; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 2055 | /* |
| 2056 | * use the parser dictionnary to allocate all elements and attributes names |
| 2057 | */ |
| 2058 | ret->ctxt->docdict = 1; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 2059 | ret->dict = ret->ctxt->dict; |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 2060 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 2061 | ret->xinclude = 0; |
| 2062 | #endif |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 2063 | #ifdef LIBXML_PATTERN_ENABLED |
| 2064 | ret->patternMax = 0; |
| 2065 | ret->patternTab = NULL; |
| 2066 | #endif |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2067 | return(ret); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * xmlNewTextReaderFilename: |
| 2072 | * @URI: the URI of the resource to process |
| 2073 | * |
| 2074 | * Create an xmlTextReader structure fed with the resource at @URI |
| 2075 | * |
| 2076 | * Returns the new xmlTextReaderPtr or NULL in case of error |
| 2077 | */ |
| 2078 | xmlTextReaderPtr |
| 2079 | xmlNewTextReaderFilename(const char *URI) { |
| 2080 | xmlParserInputBufferPtr input; |
| 2081 | xmlTextReaderPtr ret; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2082 | char *directory = NULL; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2083 | |
| 2084 | input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE); |
| 2085 | if (input == NULL) |
| 2086 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2087 | ret = xmlNewTextReader(input, URI); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2088 | if (ret == NULL) { |
| 2089 | xmlFreeParserInputBuffer(input); |
| 2090 | return(NULL); |
| 2091 | } |
| 2092 | ret->allocs |= XML_TEXTREADER_INPUT; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2093 | if (ret->ctxt->directory == NULL) |
| 2094 | directory = xmlParserGetDirectory(URI); |
| 2095 | if ((ret->ctxt->directory == NULL) && (directory != NULL)) |
| 2096 | ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory); |
| 2097 | if (directory != NULL) |
| 2098 | xmlFree(directory); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2099 | return(ret); |
| 2100 | } |
| 2101 | |
| 2102 | /** |
| 2103 | * xmlFreeTextReader: |
| 2104 | * @reader: the xmlTextReaderPtr |
| 2105 | * |
| 2106 | * Deallocate all the resources associated to the reader |
| 2107 | */ |
| 2108 | void |
| 2109 | xmlFreeTextReader(xmlTextReaderPtr reader) { |
| 2110 | if (reader == NULL) |
| 2111 | return; |
Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 2112 | #ifdef LIBXML_SCHEMAS_ENABLED |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2113 | if (reader->rngSchemas != NULL) { |
| 2114 | xmlRelaxNGFree(reader->rngSchemas); |
| 2115 | reader->rngSchemas = NULL; |
| 2116 | } |
| 2117 | if (reader->rngValidCtxt != NULL) { |
| 2118 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 2119 | reader->rngValidCtxt = NULL; |
| 2120 | } |
Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 2121 | #endif |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 2122 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 2123 | if (reader->xincctxt != NULL) |
| 2124 | xmlXIncludeFreeContext(reader->xincctxt); |
| 2125 | #endif |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 2126 | #ifdef LIBXML_PATTERN_ENABLED |
| 2127 | if (reader->patternTab != NULL) { |
| 2128 | int i; |
| 2129 | for (i = 0;i < reader->patternNr;i++) { |
| 2130 | if (reader->patternTab[i] != NULL) |
| 2131 | xmlFreePattern(reader->patternTab[i]); |
| 2132 | } |
| 2133 | xmlFree(reader->patternTab); |
| 2134 | } |
| 2135 | #endif |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2136 | if (reader->ctxt != NULL) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 2137 | if (reader->dict == reader->ctxt->dict) |
| 2138 | reader->dict = NULL; |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 2139 | if (reader->ctxt->myDoc != NULL) { |
| 2140 | if (reader->preserve == 0) |
| 2141 | xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc); |
| 2142 | reader->ctxt->myDoc = NULL; |
| 2143 | } |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 2144 | if ((reader->ctxt->vctxt.vstateTab != NULL) && |
| 2145 | (reader->ctxt->vctxt.vstateMax > 0)){ |
| 2146 | xmlFree(reader->ctxt->vctxt.vstateTab); |
| 2147 | reader->ctxt->vctxt.vstateTab = 0; |
| 2148 | reader->ctxt->vctxt.vstateMax = 0; |
| 2149 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2150 | if (reader->allocs & XML_TEXTREADER_CTXT) |
| 2151 | xmlFreeParserCtxt(reader->ctxt); |
| 2152 | } |
| 2153 | if (reader->sax != NULL) |
| 2154 | xmlFree(reader->sax); |
| 2155 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) |
| 2156 | xmlFreeParserInputBuffer(reader->input); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2157 | if (reader->faketext != NULL) { |
| 2158 | xmlFreeNode(reader->faketext); |
| 2159 | } |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 2160 | if (reader->buffer != NULL) |
| 2161 | xmlBufferFree(reader->buffer); |
Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 2162 | if (reader->entTab != NULL) |
| 2163 | xmlFree(reader->entTab); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 2164 | if (reader->dict != NULL) |
| 2165 | xmlDictFree(reader->dict); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2166 | xmlFree(reader); |
| 2167 | } |
| 2168 | |
| 2169 | /************************************************************************ |
| 2170 | * * |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2171 | * Methods for XmlTextReader * |
| 2172 | * * |
| 2173 | ************************************************************************/ |
| 2174 | /** |
| 2175 | * xmlTextReaderClose: |
| 2176 | * @reader: the xmlTextReaderPtr used |
| 2177 | * |
| 2178 | * This method releases any resources allocated by the current instance |
| 2179 | * changes the state to Closed and close any underlying input. |
| 2180 | * |
| 2181 | * Returns 0 or -1 in case of error |
| 2182 | */ |
| 2183 | int |
| 2184 | xmlTextReaderClose(xmlTextReaderPtr reader) { |
| 2185 | if (reader == NULL) |
| 2186 | return(-1); |
| 2187 | reader->node = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2188 | reader->curnode = NULL; |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2189 | reader->mode = XML_TEXTREADER_MODE_CLOSED; |
| 2190 | if (reader->ctxt != NULL) { |
Daniel Veillard | ee1d692 | 2004-04-18 14:58:57 +0000 | [diff] [blame] | 2191 | xmlStopParser(reader->ctxt); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2192 | if (reader->ctxt->myDoc != NULL) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 2193 | if (reader->preserve == 0) |
| 2194 | xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2195 | reader->ctxt->myDoc = NULL; |
| 2196 | } |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2197 | } |
| 2198 | if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) { |
| 2199 | xmlFreeParserInputBuffer(reader->input); |
| 2200 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 2201 | } |
| 2202 | return(0); |
| 2203 | } |
| 2204 | |
| 2205 | /** |
| 2206 | * xmlTextReaderGetAttributeNo: |
| 2207 | * @reader: the xmlTextReaderPtr used |
| 2208 | * @no: the zero-based index of the attribute relative to the containing element |
| 2209 | * |
| 2210 | * Provides the value of the attribute with the specified index relative |
| 2211 | * to the containing element. |
| 2212 | * |
| 2213 | * Returns a string containing the value of the specified attribute, or NULL |
| 2214 | * in case of error. The string must be deallocated by the caller. |
| 2215 | */ |
| 2216 | xmlChar * |
| 2217 | xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) { |
| 2218 | xmlChar *ret; |
| 2219 | int i; |
| 2220 | xmlAttrPtr cur; |
| 2221 | xmlNsPtr ns; |
| 2222 | |
| 2223 | if (reader == NULL) |
| 2224 | return(NULL); |
| 2225 | if (reader->node == NULL) |
| 2226 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2227 | if (reader->curnode != NULL) |
| 2228 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2229 | /* TODO: handle the xmlDecl */ |
| 2230 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2231 | return(NULL); |
| 2232 | |
| 2233 | ns = reader->node->nsDef; |
| 2234 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 2235 | ns = ns->next; |
| 2236 | } |
| 2237 | if (ns != NULL) |
| 2238 | return(xmlStrdup(ns->href)); |
| 2239 | |
| 2240 | cur = reader->node->properties; |
| 2241 | if (cur == NULL) |
| 2242 | return(NULL); |
| 2243 | for (;i < no;i++) { |
| 2244 | cur = cur->next; |
| 2245 | if (cur == NULL) |
| 2246 | return(NULL); |
| 2247 | } |
| 2248 | /* TODO walk the DTD if present */ |
| 2249 | |
| 2250 | ret = xmlNodeListGetString(reader->node->doc, cur->children, 1); |
| 2251 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 2252 | return(ret); |
| 2253 | } |
| 2254 | |
| 2255 | /** |
| 2256 | * xmlTextReaderGetAttribute: |
| 2257 | * @reader: the xmlTextReaderPtr used |
| 2258 | * @name: the qualified name of the attribute. |
| 2259 | * |
| 2260 | * Provides the value of the attribute with the specified qualified name. |
| 2261 | * |
| 2262 | * Returns a string containing the value of the specified attribute, or NULL |
| 2263 | * in case of error. The string must be deallocated by the caller. |
| 2264 | */ |
| 2265 | xmlChar * |
| 2266 | xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 2267 | xmlChar *prefix = NULL; |
| 2268 | xmlChar *localname; |
| 2269 | xmlNsPtr ns; |
| 2270 | xmlChar *ret = NULL; |
| 2271 | |
| 2272 | if ((reader == NULL) || (name == NULL)) |
| 2273 | return(NULL); |
| 2274 | if (reader->node == NULL) |
| 2275 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2276 | if (reader->curnode != NULL) |
| 2277 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2278 | |
| 2279 | /* TODO: handle the xmlDecl */ |
| 2280 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2281 | return(NULL); |
| 2282 | |
| 2283 | localname = xmlSplitQName2(name, &prefix); |
| 2284 | if (localname == NULL) |
| 2285 | return(xmlGetProp(reader->node, name)); |
| 2286 | |
| 2287 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 2288 | if (ns != NULL) |
| 2289 | ret = xmlGetNsProp(reader->node, localname, ns->href); |
| 2290 | |
| 2291 | if (localname != NULL) |
| 2292 | xmlFree(localname); |
| 2293 | if (prefix != NULL) |
| 2294 | xmlFree(prefix); |
| 2295 | return(ret); |
| 2296 | } |
| 2297 | |
| 2298 | |
| 2299 | /** |
| 2300 | * xmlTextReaderGetAttributeNs: |
| 2301 | * @reader: the xmlTextReaderPtr used |
| 2302 | * @localName: the local name of the attribute. |
| 2303 | * @namespaceURI: the namespace URI of the attribute. |
| 2304 | * |
| 2305 | * Provides the value of the specified attribute |
| 2306 | * |
| 2307 | * Returns a string containing the value of the specified attribute, or NULL |
| 2308 | * in case of error. The string must be deallocated by the caller. |
| 2309 | */ |
| 2310 | xmlChar * |
| 2311 | xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName, |
| 2312 | const xmlChar *namespaceURI) { |
| 2313 | if ((reader == NULL) || (localName == NULL)) |
| 2314 | return(NULL); |
| 2315 | if (reader->node == NULL) |
| 2316 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2317 | if (reader->curnode != NULL) |
| 2318 | return(NULL); |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2319 | |
| 2320 | /* TODO: handle the xmlDecl */ |
| 2321 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2322 | return(NULL); |
| 2323 | |
| 2324 | return(xmlGetNsProp(reader->node, localName, namespaceURI)); |
| 2325 | } |
| 2326 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2327 | /** |
| 2328 | * xmlTextReaderGetRemainder: |
| 2329 | * @reader: the xmlTextReaderPtr used |
| 2330 | * |
| 2331 | * Method to get the remainder of the buffered XML. this method stops the |
| 2332 | * parser, set its state to End Of File and return the input stream with |
| 2333 | * what is left that the parser did not use. |
| 2334 | * |
Daniel Veillard | ee1d692 | 2004-04-18 14:58:57 +0000 | [diff] [blame] | 2335 | * The implementation is not good, the parser certainly procgressed past |
| 2336 | * what's left in reader->input, and there is an allocation problem. Best |
| 2337 | * would be to rewrite it differently. |
| 2338 | * |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2339 | * Returns the xmlParserInputBufferPtr attached to the XML or NULL |
| 2340 | * in case of error. |
| 2341 | */ |
| 2342 | xmlParserInputBufferPtr |
| 2343 | xmlTextReaderGetRemainder(xmlTextReaderPtr reader) { |
| 2344 | xmlParserInputBufferPtr ret = NULL; |
| 2345 | |
| 2346 | if (reader == NULL) |
| 2347 | return(NULL); |
| 2348 | if (reader->node == NULL) |
| 2349 | return(NULL); |
| 2350 | |
| 2351 | reader->node = NULL; |
| 2352 | reader->curnode = NULL; |
| 2353 | reader->mode = XML_TEXTREADER_MODE_EOF; |
| 2354 | if (reader->ctxt != NULL) { |
Daniel Veillard | ee1d692 | 2004-04-18 14:58:57 +0000 | [diff] [blame] | 2355 | xmlStopParser(reader->ctxt); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2356 | if (reader->ctxt->myDoc != NULL) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 2357 | if (reader->preserve == 0) |
| 2358 | xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2359 | reader->ctxt->myDoc = NULL; |
| 2360 | } |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2361 | } |
| 2362 | if (reader->allocs & XML_TEXTREADER_INPUT) { |
| 2363 | ret = reader->input; |
Daniel Veillard | ee1d692 | 2004-04-18 14:58:57 +0000 | [diff] [blame] | 2364 | reader->input = NULL; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2365 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 2366 | } else { |
| 2367 | /* |
| 2368 | * Hum, one may need to duplicate the data structure because |
| 2369 | * without reference counting the input may be freed twice: |
| 2370 | * - by the layer which allocated it. |
| 2371 | * - by the layer to which would have been returned to. |
| 2372 | */ |
| 2373 | TODO |
| 2374 | return(NULL); |
| 2375 | } |
| 2376 | return(ret); |
| 2377 | } |
| 2378 | |
| 2379 | /** |
| 2380 | * xmlTextReaderLookupNamespace: |
| 2381 | * @reader: the xmlTextReaderPtr used |
| 2382 | * @prefix: the prefix whose namespace URI is to be resolved. To return |
| 2383 | * the default namespace, specify NULL |
| 2384 | * |
| 2385 | * Resolves a namespace prefix in the scope of the current element. |
| 2386 | * |
| 2387 | * Returns a string containing the namespace URI to which the prefix maps |
| 2388 | * or NULL in case of error. The string must be deallocated by the caller. |
| 2389 | */ |
| 2390 | xmlChar * |
| 2391 | xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) { |
| 2392 | xmlNsPtr ns; |
| 2393 | |
| 2394 | if (reader == NULL) |
| 2395 | return(NULL); |
| 2396 | if (reader->node == NULL) |
| 2397 | return(NULL); |
| 2398 | |
| 2399 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); |
| 2400 | if (ns == NULL) |
| 2401 | return(NULL); |
| 2402 | return(xmlStrdup(ns->href)); |
| 2403 | } |
| 2404 | |
| 2405 | /** |
| 2406 | * xmlTextReaderMoveToAttributeNo: |
| 2407 | * @reader: the xmlTextReaderPtr used |
| 2408 | * @no: the zero-based index of the attribute relative to the containing |
| 2409 | * element. |
| 2410 | * |
| 2411 | * Moves the position of the current instance to the attribute with |
| 2412 | * the specified index relative to the containing element. |
| 2413 | * |
| 2414 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 2415 | */ |
| 2416 | int |
| 2417 | xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) { |
| 2418 | int i; |
| 2419 | xmlAttrPtr cur; |
| 2420 | xmlNsPtr ns; |
| 2421 | |
| 2422 | if (reader == NULL) |
| 2423 | return(-1); |
| 2424 | if (reader->node == NULL) |
| 2425 | return(-1); |
| 2426 | /* TODO: handle the xmlDecl */ |
| 2427 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2428 | return(-1); |
| 2429 | |
| 2430 | reader->curnode = NULL; |
| 2431 | |
| 2432 | ns = reader->node->nsDef; |
| 2433 | for (i = 0;(i < no) && (ns != NULL);i++) { |
| 2434 | ns = ns->next; |
| 2435 | } |
| 2436 | if (ns != NULL) { |
| 2437 | reader->curnode = (xmlNodePtr) ns; |
| 2438 | return(1); |
| 2439 | } |
| 2440 | |
| 2441 | cur = reader->node->properties; |
| 2442 | if (cur == NULL) |
| 2443 | return(0); |
| 2444 | for (;i < no;i++) { |
| 2445 | cur = cur->next; |
| 2446 | if (cur == NULL) |
| 2447 | return(0); |
| 2448 | } |
| 2449 | /* TODO walk the DTD if present */ |
| 2450 | |
| 2451 | reader->curnode = (xmlNodePtr) cur; |
| 2452 | return(1); |
| 2453 | } |
| 2454 | |
| 2455 | /** |
| 2456 | * xmlTextReaderMoveToAttribute: |
| 2457 | * @reader: the xmlTextReaderPtr used |
| 2458 | * @name: the qualified name of the attribute. |
| 2459 | * |
| 2460 | * Moves the position of the current instance to the attribute with |
| 2461 | * the specified qualified name. |
| 2462 | * |
| 2463 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 2464 | */ |
| 2465 | int |
| 2466 | xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) { |
| 2467 | xmlChar *prefix = NULL; |
| 2468 | xmlChar *localname; |
| 2469 | xmlNsPtr ns; |
| 2470 | xmlAttrPtr prop; |
| 2471 | |
| 2472 | if ((reader == NULL) || (name == NULL)) |
| 2473 | return(-1); |
| 2474 | if (reader->node == NULL) |
| 2475 | return(-1); |
| 2476 | |
| 2477 | /* TODO: handle the xmlDecl */ |
| 2478 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2479 | return(0); |
| 2480 | |
| 2481 | localname = xmlSplitQName2(name, &prefix); |
| 2482 | if (localname == NULL) { |
| 2483 | /* |
| 2484 | * Namespace default decl |
| 2485 | */ |
| 2486 | if (xmlStrEqual(name, BAD_CAST "xmlns")) { |
| 2487 | ns = reader->node->nsDef; |
| 2488 | while (ns != NULL) { |
| 2489 | if (ns->prefix == NULL) { |
| 2490 | reader->curnode = (xmlNodePtr) ns; |
| 2491 | return(1); |
| 2492 | } |
| 2493 | ns = ns->next; |
| 2494 | } |
| 2495 | return(0); |
| 2496 | } |
| 2497 | |
| 2498 | prop = reader->node->properties; |
| 2499 | while (prop != NULL) { |
| 2500 | /* |
| 2501 | * One need to have |
| 2502 | * - same attribute names |
| 2503 | * - and the attribute carrying that namespace |
| 2504 | */ |
| 2505 | if ((xmlStrEqual(prop->name, name)) && |
| 2506 | ((prop->ns == NULL) || (prop->ns->prefix == NULL))) { |
| 2507 | reader->curnode = (xmlNodePtr) prop; |
| 2508 | return(1); |
| 2509 | } |
| 2510 | prop = prop->next; |
| 2511 | } |
| 2512 | return(0); |
| 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | * Namespace default decl |
| 2517 | */ |
| 2518 | if (xmlStrEqual(prefix, BAD_CAST "xmlns")) { |
| 2519 | ns = reader->node->nsDef; |
| 2520 | while (ns != NULL) { |
| 2521 | if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) { |
| 2522 | reader->curnode = (xmlNodePtr) ns; |
| 2523 | goto found; |
| 2524 | } |
| 2525 | ns = ns->next; |
| 2526 | } |
| 2527 | goto not_found; |
| 2528 | } |
| 2529 | prop = reader->node->properties; |
| 2530 | while (prop != NULL) { |
| 2531 | /* |
| 2532 | * One need to have |
| 2533 | * - same attribute names |
| 2534 | * - and the attribute carrying that namespace |
| 2535 | */ |
| 2536 | if ((xmlStrEqual(prop->name, localname)) && |
| 2537 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) { |
| 2538 | reader->curnode = (xmlNodePtr) prop; |
| 2539 | goto found; |
| 2540 | } |
| 2541 | prop = prop->next; |
| 2542 | } |
| 2543 | not_found: |
| 2544 | if (localname != NULL) |
| 2545 | xmlFree(localname); |
| 2546 | if (prefix != NULL) |
| 2547 | xmlFree(prefix); |
| 2548 | return(0); |
| 2549 | |
| 2550 | found: |
| 2551 | if (localname != NULL) |
| 2552 | xmlFree(localname); |
| 2553 | if (prefix != NULL) |
| 2554 | xmlFree(prefix); |
| 2555 | return(1); |
| 2556 | } |
| 2557 | |
| 2558 | /** |
| 2559 | * xmlTextReaderMoveToAttributeNs: |
| 2560 | * @reader: the xmlTextReaderPtr used |
| 2561 | * @localName: the local name of the attribute. |
| 2562 | * @namespaceURI: the namespace URI of the attribute. |
| 2563 | * |
| 2564 | * Moves the position of the current instance to the attribute with the |
| 2565 | * specified local name and namespace URI. |
| 2566 | * |
| 2567 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 2568 | */ |
| 2569 | int |
| 2570 | xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader, |
| 2571 | const xmlChar *localName, const xmlChar *namespaceURI) { |
| 2572 | xmlAttrPtr prop; |
| 2573 | xmlNodePtr node; |
| 2574 | |
| 2575 | if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL)) |
| 2576 | return(-1); |
| 2577 | if (reader->node == NULL) |
| 2578 | return(-1); |
| 2579 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2580 | return(0); |
| 2581 | node = reader->node; |
| 2582 | |
| 2583 | /* |
| 2584 | * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no |
| 2585 | * namespace name associated to "xmlns" |
| 2586 | */ |
| 2587 | prop = node->properties; |
| 2588 | while (prop != NULL) { |
| 2589 | /* |
| 2590 | * One need to have |
| 2591 | * - same attribute names |
| 2592 | * - and the attribute carrying that namespace |
| 2593 | */ |
| 2594 | if (xmlStrEqual(prop->name, localName) && |
| 2595 | ((prop->ns != NULL) && |
| 2596 | (xmlStrEqual(prop->ns->href, namespaceURI)))) { |
| 2597 | reader->curnode = (xmlNodePtr) prop; |
| 2598 | return(1); |
| 2599 | } |
| 2600 | prop = prop->next; |
| 2601 | } |
| 2602 | return(0); |
| 2603 | } |
| 2604 | |
| 2605 | /** |
| 2606 | * xmlTextReaderMoveToFirstAttribute: |
| 2607 | * @reader: the xmlTextReaderPtr used |
| 2608 | * |
| 2609 | * Moves the position of the current instance to the first attribute |
| 2610 | * associated with the current node. |
| 2611 | * |
| 2612 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 2613 | */ |
| 2614 | int |
| 2615 | xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) { |
| 2616 | if (reader == NULL) |
| 2617 | return(-1); |
| 2618 | if (reader->node == NULL) |
| 2619 | return(-1); |
| 2620 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2621 | return(0); |
| 2622 | |
| 2623 | if (reader->node->nsDef != NULL) { |
| 2624 | reader->curnode = (xmlNodePtr) reader->node->nsDef; |
| 2625 | return(1); |
| 2626 | } |
| 2627 | if (reader->node->properties != NULL) { |
| 2628 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 2629 | return(1); |
| 2630 | } |
| 2631 | return(0); |
| 2632 | } |
| 2633 | |
| 2634 | /** |
| 2635 | * xmlTextReaderMoveToNextAttribute: |
| 2636 | * @reader: the xmlTextReaderPtr used |
| 2637 | * |
| 2638 | * Moves the position of the current instance to the next attribute |
| 2639 | * associated with the current node. |
| 2640 | * |
| 2641 | * Returns 1 in case of success, -1 in case of error, 0 if not found |
| 2642 | */ |
| 2643 | int |
| 2644 | xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) { |
| 2645 | if (reader == NULL) |
| 2646 | return(-1); |
| 2647 | if (reader->node == NULL) |
| 2648 | return(-1); |
| 2649 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2650 | return(0); |
| 2651 | if (reader->curnode == NULL) |
| 2652 | return(xmlTextReaderMoveToFirstAttribute(reader)); |
| 2653 | |
| 2654 | if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 2655 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 2656 | if (ns->next != NULL) { |
| 2657 | reader->curnode = (xmlNodePtr) ns->next; |
| 2658 | return(1); |
| 2659 | } |
| 2660 | if (reader->node->properties != NULL) { |
| 2661 | reader->curnode = (xmlNodePtr) reader->node->properties; |
| 2662 | return(1); |
| 2663 | } |
| 2664 | return(0); |
| 2665 | } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) && |
| 2666 | (reader->curnode->next != NULL)) { |
| 2667 | reader->curnode = reader->curnode->next; |
| 2668 | return(1); |
| 2669 | } |
| 2670 | return(0); |
| 2671 | } |
| 2672 | |
| 2673 | /** |
| 2674 | * xmlTextReaderMoveToElement: |
| 2675 | * @reader: the xmlTextReaderPtr used |
| 2676 | * |
| 2677 | * Moves the position of the current instance to the node that |
| 2678 | * contains the current Attribute node. |
| 2679 | * |
| 2680 | * Returns 1 in case of success, -1 in case of error, 0 if not moved |
| 2681 | */ |
| 2682 | int |
| 2683 | xmlTextReaderMoveToElement(xmlTextReaderPtr reader) { |
| 2684 | if (reader == NULL) |
| 2685 | return(-1); |
| 2686 | if (reader->node == NULL) |
| 2687 | return(-1); |
| 2688 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2689 | return(0); |
| 2690 | if (reader->curnode != NULL) { |
| 2691 | reader->curnode = NULL; |
| 2692 | return(1); |
| 2693 | } |
| 2694 | return(0); |
| 2695 | } |
| 2696 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2697 | /** |
| 2698 | * xmlTextReaderReadAttributeValue: |
| 2699 | * @reader: the xmlTextReaderPtr used |
| 2700 | * |
| 2701 | * Parses an attribute value into one or more Text and EntityReference nodes. |
| 2702 | * |
| 2703 | * Returns 1 in case of success, 0 if the reader was not positionned on an |
| 2704 | * ttribute node or all the attribute values have been read, or -1 |
| 2705 | * in case of error. |
| 2706 | */ |
| 2707 | int |
| 2708 | xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) { |
| 2709 | if (reader == NULL) |
| 2710 | return(-1); |
| 2711 | if (reader->node == NULL) |
| 2712 | return(-1); |
| 2713 | if (reader->curnode == NULL) |
| 2714 | return(0); |
| 2715 | if (reader->curnode->type == XML_ATTRIBUTE_NODE) { |
| 2716 | if (reader->curnode->children == NULL) |
| 2717 | return(0); |
| 2718 | reader->curnode = reader->curnode->children; |
| 2719 | } else if (reader->curnode->type == XML_NAMESPACE_DECL) { |
| 2720 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; |
| 2721 | |
| 2722 | if (reader->faketext == NULL) { |
| 2723 | reader->faketext = xmlNewDocText(reader->node->doc, |
| 2724 | ns->href); |
| 2725 | } else { |
| 2726 | if (reader->faketext->content != NULL) |
| 2727 | xmlFree(reader->faketext->content); |
| 2728 | reader->faketext->content = xmlStrdup(ns->href); |
| 2729 | } |
| 2730 | reader->curnode = reader->faketext; |
| 2731 | } else { |
| 2732 | if (reader->curnode->next == NULL) |
| 2733 | return(0); |
| 2734 | reader->curnode = reader->curnode->next; |
| 2735 | } |
| 2736 | return(1); |
| 2737 | } |
| 2738 | |
Daniel Veillard | e281127 | 2004-10-19 09:04:23 +0000 | [diff] [blame] | 2739 | /** |
| 2740 | * xmlTextReaderConstEncoding: |
| 2741 | * @reader: the xmlTextReaderPtr used |
| 2742 | * |
| 2743 | * Determine the encoding of the document being read. |
| 2744 | * |
| 2745 | * Returns a string containing the encoding of the document or NULL in |
| 2746 | * case of error. The string is deallocated with the reader. |
| 2747 | */ |
| 2748 | const xmlChar * |
| 2749 | xmlTextReaderConstEncoding(xmlTextReaderPtr reader) { |
| 2750 | xmlDocPtr doc = NULL; |
| 2751 | if (reader == NULL) |
| 2752 | return(NULL); |
| 2753 | if (reader->doc != NULL) |
| 2754 | doc = reader->doc; |
| 2755 | else if (reader->ctxt != NULL) |
| 2756 | doc = reader->ctxt->myDoc; |
| 2757 | if (doc == NULL) |
| 2758 | return(NULL); |
| 2759 | |
| 2760 | if (doc->encoding == NULL) |
| 2761 | return(NULL); |
| 2762 | else |
| 2763 | return(CONSTSTR(doc->encoding)); |
| 2764 | } |
| 2765 | |
| 2766 | |
Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 2767 | /************************************************************************ |
| 2768 | * * |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2769 | * Acces API to the current node * |
| 2770 | * * |
| 2771 | ************************************************************************/ |
| 2772 | /** |
| 2773 | * xmlTextReaderAttributeCount: |
| 2774 | * @reader: the xmlTextReaderPtr used |
| 2775 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 2776 | * Provides the number of attributes of the current node |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2777 | * |
| 2778 | * Returns 0 i no attributes, -1 in case of error or the attribute count |
| 2779 | */ |
| 2780 | int |
| 2781 | xmlTextReaderAttributeCount(xmlTextReaderPtr reader) { |
| 2782 | int ret; |
| 2783 | xmlAttrPtr attr; |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 2784 | xmlNsPtr ns; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2785 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2786 | |
| 2787 | if (reader == NULL) |
| 2788 | return(-1); |
| 2789 | if (reader->node == NULL) |
| 2790 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2791 | |
| 2792 | if (reader->curnode != NULL) |
| 2793 | node = reader->curnode; |
| 2794 | else |
| 2795 | node = reader->node; |
| 2796 | |
| 2797 | if (node->type != XML_ELEMENT_NODE) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2798 | return(0); |
| 2799 | if ((reader->state == XML_TEXTREADER_END) || |
| 2800 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
| 2801 | return(0); |
| 2802 | ret = 0; |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2803 | attr = node->properties; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2804 | while (attr != NULL) { |
| 2805 | ret++; |
| 2806 | attr = attr->next; |
| 2807 | } |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 2808 | ns = node->nsDef; |
| 2809 | while (ns != NULL) { |
| 2810 | ret++; |
| 2811 | ns = ns->next; |
| 2812 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2813 | return(ret); |
| 2814 | } |
| 2815 | |
| 2816 | /** |
| 2817 | * xmlTextReaderNodeType: |
| 2818 | * @reader: the xmlTextReaderPtr used |
| 2819 | * |
| 2820 | * Get the node type of the current node |
| 2821 | * Reference: |
| 2822 | * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html |
| 2823 | * |
| 2824 | * Returns the xmlNodeType of the current node or -1 in case of error |
| 2825 | */ |
| 2826 | int |
| 2827 | xmlTextReaderNodeType(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2828 | xmlNodePtr node; |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2829 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2830 | if (reader == NULL) |
| 2831 | return(-1); |
| 2832 | if (reader->node == NULL) |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2833 | return(XML_READER_TYPE_NONE); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2834 | if (reader->curnode != NULL) |
| 2835 | node = reader->curnode; |
| 2836 | else |
| 2837 | node = reader->node; |
| 2838 | switch (node->type) { |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2839 | case XML_ELEMENT_NODE: |
| 2840 | if ((reader->state == XML_TEXTREADER_END) || |
| 2841 | (reader->state == XML_TEXTREADER_BACKTRACK)) |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2842 | return(XML_READER_TYPE_END_ELEMENT); |
| 2843 | return(XML_READER_TYPE_ELEMENT); |
Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 2844 | case XML_NAMESPACE_DECL: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2845 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2846 | return(XML_READER_TYPE_ATTRIBUTE); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2847 | case XML_TEXT_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2848 | if (xmlIsBlankNode(reader->node)) { |
| 2849 | if (xmlNodeGetSpacePreserve(reader->node)) |
| 2850 | return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE); |
| 2851 | else |
| 2852 | return(XML_READER_TYPE_WHITESPACE); |
| 2853 | } else { |
| 2854 | return(XML_READER_TYPE_TEXT); |
| 2855 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2856 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2857 | return(XML_READER_TYPE_CDATA); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2858 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2859 | return(XML_READER_TYPE_ENTITY_REFERENCE); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2860 | case XML_ENTITY_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2861 | return(XML_READER_TYPE_ENTITY); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2862 | case XML_PI_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2863 | return(XML_READER_TYPE_PROCESSING_INSTRUCTION); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2864 | case XML_COMMENT_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2865 | return(XML_READER_TYPE_COMMENT); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2866 | case XML_DOCUMENT_NODE: |
| 2867 | case XML_HTML_DOCUMENT_NODE: |
| 2868 | #ifdef LIBXML_DOCB_ENABLED |
| 2869 | case XML_DOCB_DOCUMENT_NODE: |
| 2870 | #endif |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2871 | return(XML_READER_TYPE_DOCUMENT); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2872 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2873 | return(XML_READER_TYPE_DOCUMENT_FRAGMENT); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2874 | case XML_NOTATION_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2875 | return(XML_READER_TYPE_NOTATION); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2876 | case XML_DOCUMENT_TYPE_NODE: |
| 2877 | case XML_DTD_NODE: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2878 | return(XML_READER_TYPE_DOCUMENT_TYPE); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2879 | |
| 2880 | case XML_ELEMENT_DECL: |
| 2881 | case XML_ATTRIBUTE_DECL: |
| 2882 | case XML_ENTITY_DECL: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2883 | case XML_XINCLUDE_START: |
| 2884 | case XML_XINCLUDE_END: |
Daniel Veillard | d6038e0 | 2003-07-30 16:37:18 +0000 | [diff] [blame] | 2885 | return(XML_READER_TYPE_NONE); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2886 | } |
| 2887 | return(-1); |
| 2888 | } |
| 2889 | |
| 2890 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2891 | * xmlTextReaderIsEmptyElement: |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2892 | * @reader: the xmlTextReaderPtr used |
| 2893 | * |
| 2894 | * Check if the current node is empty |
| 2895 | * |
| 2896 | * Returns 1 if empty, 0 if not and -1 in case of error |
| 2897 | */ |
| 2898 | int |
| 2899 | xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) { |
| 2900 | if ((reader == NULL) || (reader->node == NULL)) |
| 2901 | return(-1); |
Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 2902 | if (reader->node->type != XML_ELEMENT_NODE) |
| 2903 | return(0); |
Daniel Veillard | e3c036e | 2003-01-01 15:11:05 +0000 | [diff] [blame] | 2904 | if (reader->curnode != NULL) |
| 2905 | return(0); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2906 | if (reader->node->children != NULL) |
| 2907 | return(0); |
Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 2908 | if (reader->state == XML_TEXTREADER_END) |
| 2909 | return(0); |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 2910 | if (reader->doc != NULL) |
| 2911 | return(1); |
Daniel Veillard | 96b6cd2 | 2004-01-08 16:49:50 +0000 | [diff] [blame] | 2912 | #ifdef LIBXML_XINCLUDE_ENABLED |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 2913 | if (reader->in_xinclude > 0) |
| 2914 | return(1); |
Daniel Veillard | 96b6cd2 | 2004-01-08 16:49:50 +0000 | [diff] [blame] | 2915 | #endif |
Daniel Veillard | e8039df | 2003-10-27 11:25:13 +0000 | [diff] [blame] | 2916 | return((reader->node->extra & NODE_IS_EMPTY) != 0); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | /** |
| 2920 | * xmlTextReaderLocalName: |
| 2921 | * @reader: the xmlTextReaderPtr used |
| 2922 | * |
| 2923 | * The local name of the node. |
| 2924 | * |
| 2925 | * Returns the local name or NULL if not available |
| 2926 | */ |
| 2927 | xmlChar * |
| 2928 | xmlTextReaderLocalName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2929 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2930 | if ((reader == NULL) || (reader->node == NULL)) |
| 2931 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2932 | if (reader->curnode != NULL) |
| 2933 | node = reader->curnode; |
| 2934 | else |
| 2935 | node = reader->node; |
| 2936 | if (node->type == XML_NAMESPACE_DECL) { |
| 2937 | xmlNsPtr ns = (xmlNsPtr) node; |
| 2938 | if (ns->prefix == NULL) |
| 2939 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 2940 | else |
| 2941 | return(xmlStrdup(ns->prefix)); |
| 2942 | } |
| 2943 | if ((node->type != XML_ELEMENT_NODE) && |
| 2944 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2945 | return(xmlTextReaderName(reader)); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2946 | return(xmlStrdup(node->name)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | /** |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 2950 | * xmlTextReaderConstLocalName: |
| 2951 | * @reader: the xmlTextReaderPtr used |
| 2952 | * |
| 2953 | * The local name of the node. |
| 2954 | * |
| 2955 | * Returns the local name or NULL if not available, the |
| 2956 | * string will be deallocated with the reader. |
| 2957 | */ |
| 2958 | const xmlChar * |
| 2959 | xmlTextReaderConstLocalName(xmlTextReaderPtr reader) { |
| 2960 | xmlNodePtr node; |
| 2961 | if ((reader == NULL) || (reader->node == NULL)) |
| 2962 | return(NULL); |
| 2963 | if (reader->curnode != NULL) |
| 2964 | node = reader->curnode; |
| 2965 | else |
| 2966 | node = reader->node; |
| 2967 | if (node->type == XML_NAMESPACE_DECL) { |
| 2968 | xmlNsPtr ns = (xmlNsPtr) node; |
| 2969 | if (ns->prefix == NULL) |
| 2970 | return(CONSTSTR(BAD_CAST "xmlns")); |
| 2971 | else |
| 2972 | return(ns->prefix); |
| 2973 | } |
| 2974 | if ((node->type != XML_ELEMENT_NODE) && |
| 2975 | (node->type != XML_ATTRIBUTE_NODE)) |
| 2976 | return(xmlTextReaderConstName(reader)); |
| 2977 | return(node->name); |
| 2978 | } |
| 2979 | |
| 2980 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2981 | * xmlTextReaderName: |
| 2982 | * @reader: the xmlTextReaderPtr used |
| 2983 | * |
| 2984 | * The qualified name of the node, equal to Prefix :LocalName. |
| 2985 | * |
| 2986 | * Returns the local name or NULL if not available |
| 2987 | */ |
| 2988 | xmlChar * |
| 2989 | xmlTextReaderName(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2990 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2991 | xmlChar *ret; |
| 2992 | |
| 2993 | if ((reader == NULL) || (reader->node == NULL)) |
| 2994 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2995 | if (reader->curnode != NULL) |
| 2996 | node = reader->curnode; |
| 2997 | else |
| 2998 | node = reader->node; |
| 2999 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3000 | case XML_ELEMENT_NODE: |
| 3001 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3002 | if ((node->ns == NULL) || |
| 3003 | (node->ns->prefix == NULL)) |
| 3004 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3005 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3006 | ret = xmlStrdup(node->ns->prefix); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3007 | ret = xmlStrcat(ret, BAD_CAST ":"); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3008 | ret = xmlStrcat(ret, node->name); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3009 | return(ret); |
| 3010 | case XML_TEXT_NODE: |
| 3011 | return(xmlStrdup(BAD_CAST "#text")); |
| 3012 | case XML_CDATA_SECTION_NODE: |
| 3013 | return(xmlStrdup(BAD_CAST "#cdata-section")); |
| 3014 | case XML_ENTITY_NODE: |
| 3015 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3016 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3017 | case XML_PI_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3018 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3019 | case XML_COMMENT_NODE: |
| 3020 | return(xmlStrdup(BAD_CAST "#comment")); |
| 3021 | case XML_DOCUMENT_NODE: |
| 3022 | case XML_HTML_DOCUMENT_NODE: |
| 3023 | #ifdef LIBXML_DOCB_ENABLED |
| 3024 | case XML_DOCB_DOCUMENT_NODE: |
| 3025 | #endif |
| 3026 | return(xmlStrdup(BAD_CAST "#document")); |
| 3027 | case XML_DOCUMENT_FRAG_NODE: |
| 3028 | return(xmlStrdup(BAD_CAST "#document-fragment")); |
| 3029 | case XML_NOTATION_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3030 | return(xmlStrdup(node->name)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3031 | case XML_DOCUMENT_TYPE_NODE: |
| 3032 | case XML_DTD_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3033 | return(xmlStrdup(node->name)); |
| 3034 | case XML_NAMESPACE_DECL: { |
| 3035 | xmlNsPtr ns = (xmlNsPtr) node; |
| 3036 | |
| 3037 | ret = xmlStrdup(BAD_CAST "xmlns"); |
| 3038 | if (ns->prefix == NULL) |
| 3039 | return(ret); |
| 3040 | ret = xmlStrcat(ret, BAD_CAST ":"); |
| 3041 | ret = xmlStrcat(ret, ns->prefix); |
| 3042 | return(ret); |
| 3043 | } |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3044 | |
| 3045 | case XML_ELEMENT_DECL: |
| 3046 | case XML_ATTRIBUTE_DECL: |
| 3047 | case XML_ENTITY_DECL: |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3048 | case XML_XINCLUDE_START: |
| 3049 | case XML_XINCLUDE_END: |
| 3050 | return(NULL); |
| 3051 | } |
| 3052 | return(NULL); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | /** |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 3056 | * xmlTextReaderConstName: |
| 3057 | * @reader: the xmlTextReaderPtr used |
| 3058 | * |
| 3059 | * The qualified name of the node, equal to Prefix :LocalName. |
| 3060 | * |
| 3061 | * Returns the local name or NULL if not available, the string is |
| 3062 | * deallocated with the reader. |
| 3063 | */ |
| 3064 | const xmlChar * |
| 3065 | xmlTextReaderConstName(xmlTextReaderPtr reader) { |
| 3066 | xmlNodePtr node; |
| 3067 | |
| 3068 | if ((reader == NULL) || (reader->node == NULL)) |
| 3069 | return(NULL); |
| 3070 | if (reader->curnode != NULL) |
| 3071 | node = reader->curnode; |
| 3072 | else |
| 3073 | node = reader->node; |
| 3074 | switch (node->type) { |
| 3075 | case XML_ELEMENT_NODE: |
| 3076 | case XML_ATTRIBUTE_NODE: |
| 3077 | if ((node->ns == NULL) || |
| 3078 | (node->ns->prefix == NULL)) |
| 3079 | return(node->name); |
| 3080 | return(CONSTQSTR(node->ns->prefix, node->name)); |
| 3081 | case XML_TEXT_NODE: |
| 3082 | return(CONSTSTR(BAD_CAST "#text")); |
| 3083 | case XML_CDATA_SECTION_NODE: |
| 3084 | return(CONSTSTR(BAD_CAST "#cdata-section")); |
| 3085 | case XML_ENTITY_NODE: |
| 3086 | case XML_ENTITY_REF_NODE: |
| 3087 | return(CONSTSTR(node->name)); |
| 3088 | case XML_PI_NODE: |
| 3089 | return(CONSTSTR(node->name)); |
| 3090 | case XML_COMMENT_NODE: |
| 3091 | return(CONSTSTR(BAD_CAST "#comment")); |
| 3092 | case XML_DOCUMENT_NODE: |
| 3093 | case XML_HTML_DOCUMENT_NODE: |
| 3094 | #ifdef LIBXML_DOCB_ENABLED |
| 3095 | case XML_DOCB_DOCUMENT_NODE: |
| 3096 | #endif |
| 3097 | return(CONSTSTR(BAD_CAST "#document")); |
| 3098 | case XML_DOCUMENT_FRAG_NODE: |
| 3099 | return(CONSTSTR(BAD_CAST "#document-fragment")); |
| 3100 | case XML_NOTATION_NODE: |
| 3101 | return(CONSTSTR(node->name)); |
| 3102 | case XML_DOCUMENT_TYPE_NODE: |
| 3103 | case XML_DTD_NODE: |
| 3104 | return(CONSTSTR(node->name)); |
| 3105 | case XML_NAMESPACE_DECL: { |
| 3106 | xmlNsPtr ns = (xmlNsPtr) node; |
| 3107 | |
| 3108 | if (ns->prefix == NULL) |
| 3109 | return(CONSTSTR(BAD_CAST "xmlns")); |
| 3110 | return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix)); |
| 3111 | } |
| 3112 | |
| 3113 | case XML_ELEMENT_DECL: |
| 3114 | case XML_ATTRIBUTE_DECL: |
| 3115 | case XML_ENTITY_DECL: |
| 3116 | case XML_XINCLUDE_START: |
| 3117 | case XML_XINCLUDE_END: |
| 3118 | return(NULL); |
| 3119 | } |
| 3120 | return(NULL); |
| 3121 | } |
| 3122 | |
| 3123 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3124 | * xmlTextReaderPrefix: |
| 3125 | * @reader: the xmlTextReaderPtr used |
| 3126 | * |
| 3127 | * A shorthand reference to the namespace associated with the node. |
| 3128 | * |
| 3129 | * Returns the prefix or NULL if not available |
| 3130 | */ |
| 3131 | xmlChar * |
| 3132 | xmlTextReaderPrefix(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3133 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3134 | if ((reader == NULL) || (reader->node == NULL)) |
| 3135 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3136 | if (reader->curnode != NULL) |
| 3137 | node = reader->curnode; |
| 3138 | else |
| 3139 | node = reader->node; |
| 3140 | if (node->type == XML_NAMESPACE_DECL) { |
| 3141 | xmlNsPtr ns = (xmlNsPtr) node; |
| 3142 | if (ns->prefix == NULL) |
| 3143 | return(NULL); |
| 3144 | return(xmlStrdup(BAD_CAST "xmlns")); |
| 3145 | } |
| 3146 | if ((node->type != XML_ELEMENT_NODE) && |
| 3147 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3148 | return(NULL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3149 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3150 | return(xmlStrdup(node->ns->prefix)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3151 | return(NULL); |
| 3152 | } |
| 3153 | |
| 3154 | /** |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 3155 | * xmlTextReaderConstPrefix: |
| 3156 | * @reader: the xmlTextReaderPtr used |
| 3157 | * |
| 3158 | * A shorthand reference to the namespace associated with the node. |
| 3159 | * |
| 3160 | * Returns the prefix or NULL if not available, the string is deallocated |
| 3161 | * with the reader. |
| 3162 | */ |
| 3163 | const xmlChar * |
| 3164 | xmlTextReaderConstPrefix(xmlTextReaderPtr reader) { |
| 3165 | xmlNodePtr node; |
| 3166 | if ((reader == NULL) || (reader->node == NULL)) |
| 3167 | return(NULL); |
| 3168 | if (reader->curnode != NULL) |
| 3169 | node = reader->curnode; |
| 3170 | else |
| 3171 | node = reader->node; |
| 3172 | if (node->type == XML_NAMESPACE_DECL) { |
| 3173 | xmlNsPtr ns = (xmlNsPtr) node; |
| 3174 | if (ns->prefix == NULL) |
| 3175 | return(NULL); |
| 3176 | return(CONSTSTR(BAD_CAST "xmlns")); |
| 3177 | } |
| 3178 | if ((node->type != XML_ELEMENT_NODE) && |
| 3179 | (node->type != XML_ATTRIBUTE_NODE)) |
| 3180 | return(NULL); |
| 3181 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) |
| 3182 | return(CONSTSTR(node->ns->prefix)); |
| 3183 | return(NULL); |
| 3184 | } |
| 3185 | |
| 3186 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3187 | * xmlTextReaderNamespaceUri: |
| 3188 | * @reader: the xmlTextReaderPtr used |
| 3189 | * |
| 3190 | * The URI defining the namespace associated with the node. |
| 3191 | * |
| 3192 | * Returns the namespace URI or NULL if not available |
| 3193 | */ |
| 3194 | xmlChar * |
| 3195 | xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3196 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3197 | if ((reader == NULL) || (reader->node == NULL)) |
| 3198 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3199 | if (reader->curnode != NULL) |
| 3200 | node = reader->curnode; |
| 3201 | else |
| 3202 | node = reader->node; |
Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 3203 | if (node->type == XML_NAMESPACE_DECL) |
| 3204 | return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/")); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3205 | if ((node->type != XML_ELEMENT_NODE) && |
| 3206 | (node->type != XML_ATTRIBUTE_NODE)) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3207 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3208 | if (node->ns != NULL) |
| 3209 | return(xmlStrdup(node->ns->href)); |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3210 | return(NULL); |
| 3211 | } |
| 3212 | |
| 3213 | /** |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 3214 | * xmlTextReaderConstNamespaceUri: |
| 3215 | * @reader: the xmlTextReaderPtr used |
| 3216 | * |
| 3217 | * The URI defining the namespace associated with the node. |
| 3218 | * |
| 3219 | * Returns the namespace URI or NULL if not available, the string |
| 3220 | * will be deallocated with the reader |
| 3221 | */ |
| 3222 | const xmlChar * |
| 3223 | xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) { |
| 3224 | xmlNodePtr node; |
| 3225 | if ((reader == NULL) || (reader->node == NULL)) |
| 3226 | return(NULL); |
| 3227 | if (reader->curnode != NULL) |
| 3228 | node = reader->curnode; |
| 3229 | else |
| 3230 | node = reader->node; |
| 3231 | if (node->type == XML_NAMESPACE_DECL) |
| 3232 | return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/")); |
| 3233 | if ((node->type != XML_ELEMENT_NODE) && |
| 3234 | (node->type != XML_ATTRIBUTE_NODE)) |
| 3235 | return(NULL); |
| 3236 | if (node->ns != NULL) |
| 3237 | return(CONSTSTR(node->ns->href)); |
| 3238 | return(NULL); |
| 3239 | } |
| 3240 | |
| 3241 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3242 | * xmlTextReaderBaseUri: |
| 3243 | * @reader: the xmlTextReaderPtr used |
| 3244 | * |
| 3245 | * The base URI of the node. |
| 3246 | * |
| 3247 | * Returns the base URI or NULL if not available |
| 3248 | */ |
| 3249 | xmlChar * |
| 3250 | xmlTextReaderBaseUri(xmlTextReaderPtr reader) { |
| 3251 | if ((reader == NULL) || (reader->node == NULL)) |
| 3252 | return(NULL); |
| 3253 | return(xmlNodeGetBase(NULL, reader->node)); |
| 3254 | } |
| 3255 | |
| 3256 | /** |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 3257 | * xmlTextReaderConstBaseUri: |
| 3258 | * @reader: the xmlTextReaderPtr used |
| 3259 | * |
| 3260 | * The base URI of the node. |
| 3261 | * |
| 3262 | * Returns the base URI or NULL if not available, the string |
| 3263 | * will be deallocated with the reader |
| 3264 | */ |
| 3265 | const xmlChar * |
| 3266 | xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) { |
| 3267 | xmlChar *tmp; |
| 3268 | const xmlChar *ret; |
| 3269 | |
| 3270 | if ((reader == NULL) || (reader->node == NULL)) |
| 3271 | return(NULL); |
| 3272 | tmp = xmlNodeGetBase(NULL, reader->node); |
| 3273 | if (tmp == NULL) |
| 3274 | return(NULL); |
| 3275 | ret = CONSTSTR(tmp); |
| 3276 | xmlFree(tmp); |
| 3277 | return(ret); |
| 3278 | } |
| 3279 | |
| 3280 | /** |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3281 | * xmlTextReaderDepth: |
| 3282 | * @reader: the xmlTextReaderPtr used |
| 3283 | * |
| 3284 | * The depth of the node in the tree. |
| 3285 | * |
| 3286 | * Returns the depth or -1 in case of error |
| 3287 | */ |
| 3288 | int |
| 3289 | xmlTextReaderDepth(xmlTextReaderPtr reader) { |
| 3290 | if (reader == NULL) |
| 3291 | return(-1); |
| 3292 | if (reader->node == NULL) |
| 3293 | return(0); |
| 3294 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 3295 | if (reader->curnode != NULL) { |
| 3296 | if ((reader->curnode->type == XML_ATTRIBUTE_NODE) || |
| 3297 | (reader->curnode->type == XML_NAMESPACE_DECL)) |
| 3298 | return(reader->depth + 1); |
| 3299 | return(reader->depth + 2); |
| 3300 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3301 | return(reader->depth); |
| 3302 | } |
| 3303 | |
| 3304 | /** |
| 3305 | * xmlTextReaderHasAttributes: |
| 3306 | * @reader: the xmlTextReaderPtr used |
| 3307 | * |
| 3308 | * Whether the node has attributes. |
| 3309 | * |
| 3310 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 3311 | */ |
| 3312 | int |
| 3313 | xmlTextReaderHasAttributes(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3314 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3315 | if (reader == NULL) |
| 3316 | return(-1); |
| 3317 | if (reader->node == NULL) |
| 3318 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3319 | if (reader->curnode != NULL) |
| 3320 | node = reader->curnode; |
| 3321 | else |
| 3322 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3323 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3324 | if ((node->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 6bb3e86 | 2004-11-24 12:39:00 +0000 | [diff] [blame] | 3325 | ((node->properties != NULL) || (node->nsDef != NULL))) |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3326 | return(1); |
| 3327 | /* TODO: handle the xmlDecl */ |
| 3328 | return(0); |
| 3329 | } |
| 3330 | |
| 3331 | /** |
| 3332 | * xmlTextReaderHasValue: |
| 3333 | * @reader: the xmlTextReaderPtr used |
| 3334 | * |
| 3335 | * Whether the node can have a text value. |
| 3336 | * |
| 3337 | * Returns 1 if true, 0 if false, and -1 in case or error |
| 3338 | */ |
| 3339 | int |
| 3340 | xmlTextReaderHasValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3341 | xmlNodePtr node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3342 | if (reader == NULL) |
| 3343 | return(-1); |
| 3344 | if (reader->node == NULL) |
| 3345 | return(0); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3346 | if (reader->curnode != NULL) |
| 3347 | node = reader->curnode; |
| 3348 | else |
| 3349 | node = reader->node; |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3350 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3351 | switch (node->type) { |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3352 | case XML_ATTRIBUTE_NODE: |
| 3353 | case XML_TEXT_NODE: |
| 3354 | case XML_CDATA_SECTION_NODE: |
| 3355 | case XML_PI_NODE: |
| 3356 | case XML_COMMENT_NODE: |
Daniel Veillard | 9e07710 | 2003-04-10 13:36:54 +0000 | [diff] [blame] | 3357 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3358 | return(1); |
| 3359 | default: |
Daniel Veillard | 2cfd9df | 2003-03-22 22:39:16 +0000 | [diff] [blame] | 3360 | break; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3361 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3362 | return(0); |
| 3363 | } |
| 3364 | |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3365 | /** |
| 3366 | * xmlTextReaderValue: |
| 3367 | * @reader: the xmlTextReaderPtr used |
| 3368 | * |
| 3369 | * Provides the text value of the node if present |
| 3370 | * |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 3371 | * 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] | 3372 | * with xmlFree() |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3373 | */ |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3374 | xmlChar * |
| 3375 | xmlTextReaderValue(xmlTextReaderPtr reader) { |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3376 | xmlNodePtr node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3377 | if (reader == NULL) |
| 3378 | return(NULL); |
| 3379 | if (reader->node == NULL) |
| 3380 | return(NULL); |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3381 | if (reader->curnode != NULL) |
| 3382 | node = reader->curnode; |
| 3383 | else |
| 3384 | node = reader->node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3385 | |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3386 | switch (node->type) { |
| 3387 | case XML_NAMESPACE_DECL: |
| 3388 | return(xmlStrdup(((xmlNsPtr) node)->href)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3389 | case XML_ATTRIBUTE_NODE:{ |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3390 | xmlAttrPtr attr = (xmlAttrPtr) node; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3391 | |
| 3392 | if (attr->parent != NULL) |
| 3393 | return (xmlNodeListGetString |
| 3394 | (attr->parent->doc, attr->children, 1)); |
| 3395 | else |
| 3396 | return (xmlNodeListGetString(NULL, attr->children, 1)); |
| 3397 | break; |
| 3398 | } |
| 3399 | case XML_TEXT_NODE: |
| 3400 | case XML_CDATA_SECTION_NODE: |
| 3401 | case XML_PI_NODE: |
| 3402 | case XML_COMMENT_NODE: |
Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 3403 | if (node->content != NULL) |
| 3404 | return (xmlStrdup(node->content)); |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3405 | default: |
Daniel Veillard | 2cfd9df | 2003-03-22 22:39:16 +0000 | [diff] [blame] | 3406 | break; |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3407 | } |
| 3408 | return(NULL); |
| 3409 | } |
| 3410 | |
| 3411 | /** |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 3412 | * xmlTextReaderConstValue: |
| 3413 | * @reader: the xmlTextReaderPtr used |
| 3414 | * |
| 3415 | * Provides the text value of the node if present |
| 3416 | * |
| 3417 | * Returns the string or NULL if not available. The result will be |
| 3418 | * deallocated on the next Read() operation. |
| 3419 | */ |
| 3420 | const xmlChar * |
| 3421 | xmlTextReaderConstValue(xmlTextReaderPtr reader) { |
| 3422 | xmlNodePtr node; |
| 3423 | if (reader == NULL) |
| 3424 | return(NULL); |
| 3425 | if (reader->node == NULL) |
| 3426 | return(NULL); |
| 3427 | if (reader->curnode != NULL) |
| 3428 | node = reader->curnode; |
| 3429 | else |
| 3430 | node = reader->node; |
| 3431 | |
| 3432 | switch (node->type) { |
| 3433 | case XML_NAMESPACE_DECL: |
| 3434 | return(((xmlNsPtr) node)->href); |
| 3435 | case XML_ATTRIBUTE_NODE:{ |
| 3436 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 3437 | |
| 3438 | if ((attr->children != NULL) && |
| 3439 | (attr->children->type == XML_TEXT_NODE) && |
| 3440 | (attr->children->next == NULL)) |
| 3441 | return(attr->children->content); |
| 3442 | else { |
Daniel Veillard | 8165a6b | 2004-07-01 11:20:33 +0000 | [diff] [blame] | 3443 | if (reader->buffer == NULL) |
| 3444 | reader->buffer = xmlBufferCreateSize(100); |
| 3445 | if (reader->buffer == NULL) { |
| 3446 | xmlGenericError(xmlGenericErrorContext, |
| 3447 | "xmlTextReaderSetup : malloc failed\n"); |
| 3448 | return (NULL); |
| 3449 | } |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 3450 | reader->buffer->use = 0; |
| 3451 | xmlNodeBufGetContent(reader->buffer, node); |
| 3452 | return(reader->buffer->content); |
| 3453 | } |
| 3454 | break; |
| 3455 | } |
| 3456 | case XML_TEXT_NODE: |
| 3457 | case XML_CDATA_SECTION_NODE: |
| 3458 | case XML_PI_NODE: |
| 3459 | case XML_COMMENT_NODE: |
| 3460 | return(node->content); |
| 3461 | default: |
| 3462 | break; |
| 3463 | } |
| 3464 | return(NULL); |
| 3465 | } |
| 3466 | |
| 3467 | /** |
Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 3468 | * xmlTextReaderIsDefault: |
| 3469 | * @reader: the xmlTextReaderPtr used |
| 3470 | * |
| 3471 | * Whether an Attribute node was generated from the default value |
| 3472 | * defined in the DTD or schema. |
| 3473 | * |
| 3474 | * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error |
| 3475 | */ |
| 3476 | int |
| 3477 | xmlTextReaderIsDefault(xmlTextReaderPtr reader) { |
| 3478 | if (reader == NULL) |
| 3479 | return(-1); |
| 3480 | return(0); |
| 3481 | } |
| 3482 | |
| 3483 | /** |
| 3484 | * xmlTextReaderQuoteChar: |
| 3485 | * @reader: the xmlTextReaderPtr used |
| 3486 | * |
| 3487 | * The quotation mark character used to enclose the value of an attribute. |
| 3488 | * |
| 3489 | * Returns " or ' and -1 in case of error |
| 3490 | */ |
| 3491 | int |
| 3492 | xmlTextReaderQuoteChar(xmlTextReaderPtr reader) { |
| 3493 | if (reader == NULL) |
| 3494 | return(-1); |
| 3495 | /* TODO maybe lookup the attribute value for " first */ |
| 3496 | return((int) '"'); |
| 3497 | } |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 3498 | |
| 3499 | /** |
| 3500 | * xmlTextReaderXmlLang: |
| 3501 | * @reader: the xmlTextReaderPtr used |
| 3502 | * |
| 3503 | * The xml:lang scope within which the node resides. |
| 3504 | * |
| 3505 | * Returns the xml:lang value or NULL if none exists. |
| 3506 | */ |
| 3507 | xmlChar * |
| 3508 | xmlTextReaderXmlLang(xmlTextReaderPtr reader) { |
| 3509 | if (reader == NULL) |
| 3510 | return(NULL); |
| 3511 | if (reader->node == NULL) |
| 3512 | return(NULL); |
| 3513 | return(xmlNodeGetLang(reader->node)); |
| 3514 | } |
| 3515 | |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 3516 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 3517 | * xmlTextReaderConstXmlLang: |
Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 3518 | * @reader: the xmlTextReaderPtr used |
| 3519 | * |
| 3520 | * The xml:lang scope within which the node resides. |
| 3521 | * |
| 3522 | * Returns the xml:lang value or NULL if none exists. |
| 3523 | */ |
| 3524 | const xmlChar * |
| 3525 | xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) { |
| 3526 | xmlChar *tmp; |
| 3527 | const xmlChar *ret; |
| 3528 | |
| 3529 | if (reader == NULL) |
| 3530 | return(NULL); |
| 3531 | if (reader->node == NULL) |
| 3532 | return(NULL); |
| 3533 | tmp = xmlNodeGetLang(reader->node); |
| 3534 | if (tmp == NULL) |
| 3535 | return(NULL); |
| 3536 | ret = CONSTSTR(tmp); |
| 3537 | xmlFree(tmp); |
| 3538 | return(ret); |
| 3539 | } |
| 3540 | |
| 3541 | /** |
Daniel Veillard | f85ce8e | 2003-09-22 10:24:45 +0000 | [diff] [blame] | 3542 | * xmlTextReaderConstString: |
| 3543 | * @reader: the xmlTextReaderPtr used |
| 3544 | * @str: the string to intern. |
| 3545 | * |
| 3546 | * Get an interned string from the reader, allows for example to |
| 3547 | * speedup string name comparisons |
| 3548 | * |
| 3549 | * Returns an interned copy of the string or NULL in case of error. The |
| 3550 | * string will be deallocated with the reader. |
| 3551 | */ |
| 3552 | const xmlChar * |
| 3553 | xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) { |
| 3554 | if (reader == NULL) |
| 3555 | return(NULL); |
| 3556 | return(CONSTSTR(str)); |
| 3557 | } |
| 3558 | |
| 3559 | /** |
Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 3560 | * xmlTextReaderNormalization: |
| 3561 | * @reader: the xmlTextReaderPtr used |
| 3562 | * |
| 3563 | * The value indicating whether to normalize white space and attribute values. |
| 3564 | * Since attribute value and end of line normalizations are a MUST in the XML |
| 3565 | * specification only the value true is accepted. The broken bahaviour of |
| 3566 | * accepting out of range character entities like � is of course not |
| 3567 | * supported either. |
| 3568 | * |
| 3569 | * Returns 1 or -1 in case of error. |
| 3570 | */ |
| 3571 | int |
| 3572 | xmlTextReaderNormalization(xmlTextReaderPtr reader) { |
| 3573 | if (reader == NULL) |
| 3574 | return(-1); |
| 3575 | return(1); |
| 3576 | } |
| 3577 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 3578 | /************************************************************************ |
| 3579 | * * |
| 3580 | * Extensions to the base APIs * |
| 3581 | * * |
| 3582 | ************************************************************************/ |
| 3583 | |
| 3584 | /** |
| 3585 | * xmlTextReaderSetParserProp: |
| 3586 | * @reader: the xmlTextReaderPtr used |
| 3587 | * @prop: the xmlParserProperties to set |
| 3588 | * @value: usually 0 or 1 to (de)activate it |
| 3589 | * |
| 3590 | * Change the parser processing behaviour by changing some of its internal |
| 3591 | * properties. Note that some properties can only be changed before any |
| 3592 | * read has been done. |
| 3593 | * |
| 3594 | * Returns 0 if the call was successful, or -1 in case of error |
| 3595 | */ |
| 3596 | int |
| 3597 | xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) { |
| 3598 | xmlParserProperties p = (xmlParserProperties) prop; |
| 3599 | xmlParserCtxtPtr ctxt; |
| 3600 | |
| 3601 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 3602 | return(-1); |
| 3603 | ctxt = reader->ctxt; |
| 3604 | |
| 3605 | switch (p) { |
| 3606 | case XML_PARSER_LOADDTD: |
| 3607 | if (value != 0) { |
| 3608 | if (ctxt->loadsubset == 0) { |
| 3609 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 3610 | return(-1); |
| 3611 | ctxt->loadsubset = XML_DETECT_IDS; |
| 3612 | } |
| 3613 | } else { |
| 3614 | ctxt->loadsubset = 0; |
| 3615 | } |
| 3616 | return(0); |
| 3617 | case XML_PARSER_DEFAULTATTRS: |
| 3618 | if (value != 0) { |
| 3619 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 3620 | } else { |
| 3621 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 3622 | ctxt->loadsubset -= XML_COMPLETE_ATTRS; |
| 3623 | } |
| 3624 | return(0); |
| 3625 | case XML_PARSER_VALIDATE: |
| 3626 | if (value != 0) { |
| 3627 | ctxt->validate = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 3628 | reader->validate = XML_TEXTREADER_VALIDATE_DTD; |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 3629 | } else { |
| 3630 | ctxt->validate = 0; |
| 3631 | } |
| 3632 | return(0); |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3633 | case XML_PARSER_SUBST_ENTITIES: |
| 3634 | if (value != 0) { |
| 3635 | ctxt->replaceEntities = 1; |
| 3636 | } else { |
| 3637 | ctxt->replaceEntities = 0; |
| 3638 | } |
| 3639 | return(0); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 3640 | } |
| 3641 | return(-1); |
| 3642 | } |
| 3643 | |
| 3644 | /** |
| 3645 | * xmlTextReaderGetParserProp: |
| 3646 | * @reader: the xmlTextReaderPtr used |
| 3647 | * @prop: the xmlParserProperties to get |
| 3648 | * |
| 3649 | * Read the parser internal property. |
| 3650 | * |
| 3651 | * Returns the value, usually 0 or 1, or -1 in case of error. |
| 3652 | */ |
| 3653 | int |
| 3654 | xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) { |
| 3655 | xmlParserProperties p = (xmlParserProperties) prop; |
| 3656 | xmlParserCtxtPtr ctxt; |
| 3657 | |
| 3658 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 3659 | return(-1); |
| 3660 | ctxt = reader->ctxt; |
| 3661 | |
| 3662 | switch (p) { |
| 3663 | case XML_PARSER_LOADDTD: |
| 3664 | if ((ctxt->loadsubset != 0) || (ctxt->validate != 0)) |
| 3665 | return(1); |
| 3666 | return(0); |
| 3667 | case XML_PARSER_DEFAULTATTRS: |
| 3668 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) |
| 3669 | return(1); |
| 3670 | return(0); |
| 3671 | case XML_PARSER_VALIDATE: |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 3672 | return(reader->validate); |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3673 | case XML_PARSER_SUBST_ENTITIES: |
| 3674 | return(ctxt->replaceEntities); |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 3675 | } |
| 3676 | return(-1); |
| 3677 | } |
| 3678 | |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3679 | |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3680 | /** |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3681 | * xmlTextReaderGetParserLineNumber: |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3682 | * @reader: the user data (XML reader context) |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3683 | * |
| 3684 | * Provide the line number of the current parsing point. |
| 3685 | * |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3686 | * Returns an int or 0 if not available |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3687 | */ |
| 3688 | int |
| 3689 | xmlTextReaderGetParserLineNumber(xmlTextReaderPtr reader) |
| 3690 | { |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3691 | if ((reader == NULL) || (reader->ctxt == NULL) || |
| 3692 | (reader->ctxt->input == NULL)) { |
| 3693 | return (0); |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3694 | } |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3695 | return (reader->ctxt->input->line); |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | /** |
| 3699 | * xmlTextReaderGetParserColumnNumber: |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3700 | * @reader: the user data (XML reader context) |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3701 | * |
| 3702 | * Provide the column number of the current parsing point. |
| 3703 | * |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3704 | * Returns an int or 0 if not available |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3705 | */ |
| 3706 | int |
| 3707 | xmlTextReaderGetParserColumnNumber(xmlTextReaderPtr reader) |
| 3708 | { |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3709 | if ((reader == NULL) || (reader->ctxt == NULL) || |
| 3710 | (reader->ctxt->input == NULL)) { |
| 3711 | return (0); |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3712 | } |
Daniel Veillard | f6b71bd | 2005-01-04 17:50:14 +0000 | [diff] [blame] | 3713 | return (reader->ctxt->input->col); |
Aleksey Sanin | d671e28 | 2005-01-03 21:58:59 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | /** |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3717 | * xmlTextReaderCurrentNode: |
| 3718 | * @reader: the xmlTextReaderPtr used |
| 3719 | * |
| 3720 | * Hacking interface allowing to get the xmlNodePtr correponding to the |
| 3721 | * current node being accessed by the xmlTextReader. This is dangerous |
| 3722 | * because the underlying node may be destroyed on the next Reads. |
| 3723 | * |
| 3724 | * Returns the xmlNodePtr or NULL in case of error. |
| 3725 | */ |
| 3726 | xmlNodePtr |
| 3727 | xmlTextReaderCurrentNode(xmlTextReaderPtr reader) { |
| 3728 | if (reader == NULL) |
| 3729 | return(NULL); |
| 3730 | |
| 3731 | if (reader->curnode != NULL) |
| 3732 | return(reader->curnode); |
| 3733 | return(reader->node); |
| 3734 | } |
| 3735 | |
| 3736 | /** |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3737 | * xmlTextReaderPreserve: |
| 3738 | * @reader: the xmlTextReaderPtr used |
| 3739 | * |
Daniel Veillard | 4a14fb8 | 2004-06-14 19:58:20 +0000 | [diff] [blame] | 3740 | * This tells the XML Reader to preserve the current node. |
| 3741 | * The caller must also use xmlTextReaderCurrentDoc() to |
| 3742 | * keep an handle on the resulting document once parsing has finished |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3743 | * |
| 3744 | * Returns the xmlNodePtr or NULL in case of error. |
| 3745 | */ |
| 3746 | xmlNodePtr |
| 3747 | xmlTextReaderPreserve(xmlTextReaderPtr reader) { |
| 3748 | xmlNodePtr cur, parent; |
| 3749 | |
| 3750 | if (reader == NULL) |
| 3751 | return(NULL); |
| 3752 | |
| 3753 | if (reader->curnode != NULL) |
| 3754 | cur = reader->curnode; |
| 3755 | else |
| 3756 | cur = reader->node; |
| 3757 | if (cur == NULL) |
| 3758 | return(NULL); |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 3759 | |
Daniel Veillard | 4a14fb8 | 2004-06-14 19:58:20 +0000 | [diff] [blame] | 3760 | if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) { |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 3761 | cur->extra |= NODE_IS_PRESERVED; |
| 3762 | cur->extra |= NODE_IS_SPRESERVED; |
| 3763 | } |
| 3764 | reader->preserves++; |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3765 | |
| 3766 | parent = cur->parent;; |
| 3767 | while (parent != NULL) { |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 3768 | if (parent->type == XML_ELEMENT_NODE) |
| 3769 | parent->extra |= NODE_IS_PRESERVED; |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3770 | parent = parent->parent; |
| 3771 | } |
| 3772 | return(cur); |
| 3773 | } |
| 3774 | |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 3775 | #ifdef LIBXML_PATTERN_ENABLED |
| 3776 | /** |
| 3777 | * xmlTextReaderPreservePattern: |
| 3778 | * @reader: the xmlTextReaderPtr used |
| 3779 | * @pattern: an XPath subset pattern |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 3780 | * @namespaces: the prefix definitions, array of [URI, prefix] or NULL |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 3781 | * |
| 3782 | * This tells the XML Reader to preserve all nodes matched by the |
| 3783 | * pattern. The caller must also use xmlTextReaderCurrentDoc() to |
| 3784 | * keep an handle on the resulting document once parsing has finished |
| 3785 | * |
| 3786 | * Returns a positive number in case of success and -1 in case of error |
| 3787 | */ |
| 3788 | int |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 3789 | xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern, |
| 3790 | const xmlChar **namespaces) |
| 3791 | { |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 3792 | xmlPatternPtr comp; |
| 3793 | |
| 3794 | if ((reader == NULL) || (pattern == NULL)) |
| 3795 | return(-1); |
| 3796 | |
Daniel Veillard | ffa7b7e | 2003-12-05 16:10:21 +0000 | [diff] [blame] | 3797 | comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces); |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 3798 | if (comp == NULL) |
| 3799 | return(-1); |
| 3800 | |
| 3801 | if (reader->patternMax <= 0) { |
| 3802 | reader->patternMax = 4; |
| 3803 | reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax * |
| 3804 | sizeof(reader->patternTab[0])); |
| 3805 | if (reader->patternTab == NULL) { |
| 3806 | xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n"); |
| 3807 | return (-1); |
| 3808 | } |
| 3809 | } |
| 3810 | if (reader->patternNr >= reader->patternMax) { |
| 3811 | xmlPatternPtr *tmp; |
| 3812 | reader->patternMax *= 2; |
| 3813 | tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab, |
| 3814 | reader->patternMax * |
| 3815 | sizeof(reader->patternTab[0])); |
| 3816 | if (tmp == NULL) { |
| 3817 | xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n"); |
| 3818 | reader->patternMax /= 2; |
| 3819 | return (-1); |
| 3820 | } |
| 3821 | reader->patternTab = tmp; |
| 3822 | } |
| 3823 | reader->patternTab[reader->patternNr] = comp; |
| 3824 | return(reader->patternNr++); |
| 3825 | } |
| 3826 | #endif |
| 3827 | |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3828 | /** |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3829 | * xmlTextReaderCurrentDoc: |
| 3830 | * @reader: the xmlTextReaderPtr used |
| 3831 | * |
| 3832 | * Hacking interface allowing to get the xmlDocPtr correponding to the |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3833 | * current document being accessed by the xmlTextReader. |
| 3834 | * NOTE: as a result of this call, the reader will not destroy the |
| 3835 | * associated XML document and calling xmlFreeDoc() on the result |
| 3836 | * is needed once the reader parsing has finished. |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3837 | * |
| 3838 | * Returns the xmlDocPtr or NULL in case of error. |
| 3839 | */ |
| 3840 | xmlDocPtr |
| 3841 | xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 3842 | if (reader == NULL) |
| 3843 | return(NULL); |
| 3844 | if (reader->doc != NULL) |
| 3845 | return(reader->doc); |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3846 | if ((reader == NULL) || (reader->ctxt == NULL) || |
| 3847 | (reader->ctxt->myDoc == NULL)) |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3848 | return(NULL); |
| 3849 | |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 3850 | reader->preserve = 1; |
Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 3851 | return(reader->ctxt->myDoc); |
| 3852 | } |
| 3853 | |
Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 3854 | #ifdef LIBXML_SCHEMAS_ENABLED |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 3855 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 3856 | * xmlTextReaderRelaxNGSetSchema: |
| 3857 | * @reader: the xmlTextReaderPtr used |
| 3858 | * @schema: a precompiled RelaxNG schema |
| 3859 | * |
| 3860 | * Use RelaxNG to validate the document as it is processed. |
| 3861 | * Activation is only possible before the first Read(). |
| 3862 | * if @schema is NULL, then RelaxNG validation is desactivated. |
| 3863 | @ The @schema should not be freed until the reader is deallocated |
| 3864 | * or its use has been deactivated. |
| 3865 | * |
| 3866 | * Returns 0 in case the RelaxNG validation could be (des)activated and |
| 3867 | * -1 in case of error. |
| 3868 | */ |
| 3869 | int |
| 3870 | xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 3871 | if (reader == NULL) |
| 3872 | return(-1); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 3873 | if (schema == NULL) { |
| 3874 | if (reader->rngSchemas != NULL) { |
| 3875 | xmlRelaxNGFree(reader->rngSchemas); |
| 3876 | reader->rngSchemas = NULL; |
| 3877 | } |
| 3878 | if (reader->rngValidCtxt != NULL) { |
| 3879 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 3880 | reader->rngValidCtxt = NULL; |
| 3881 | } |
| 3882 | return(0); |
| 3883 | } |
| 3884 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 3885 | return(-1); |
| 3886 | if (reader->rngSchemas != NULL) { |
| 3887 | xmlRelaxNGFree(reader->rngSchemas); |
| 3888 | reader->rngSchemas = NULL; |
| 3889 | } |
| 3890 | if (reader->rngValidCtxt != NULL) { |
| 3891 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 3892 | reader->rngValidCtxt = NULL; |
| 3893 | } |
| 3894 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema); |
| 3895 | if (reader->rngValidCtxt == NULL) |
| 3896 | return(-1); |
| 3897 | if (reader->errorFunc != NULL) { |
| 3898 | xmlRelaxNGSetValidErrors(reader->rngValidCtxt, |
| 3899 | (xmlRelaxNGValidityErrorFunc)reader->errorFunc, |
| 3900 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, |
| 3901 | reader->errorFuncArg); |
| 3902 | } |
| 3903 | reader->rngValidErrors = 0; |
| 3904 | reader->rngFullNode = NULL; |
| 3905 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; |
| 3906 | return(0); |
| 3907 | } |
| 3908 | |
| 3909 | /** |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 3910 | * xmlTextReaderRelaxNGValidate: |
| 3911 | * @reader: the xmlTextReaderPtr used |
| 3912 | * @rng: the path to a RelaxNG schema or NULL |
| 3913 | * |
| 3914 | * Use RelaxNG to validate the document as it is processed. |
| 3915 | * Activation is only possible before the first Read(). |
| 3916 | * if @rng is NULL, then RelaxNG validation is desactivated. |
| 3917 | * |
| 3918 | * Returns 0 in case the RelaxNG validation could be (des)activated and |
| 3919 | * -1 in case of error. |
| 3920 | */ |
| 3921 | int |
| 3922 | xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) { |
| 3923 | xmlRelaxNGParserCtxtPtr ctxt; |
| 3924 | |
| 3925 | if (reader == NULL) |
| 3926 | return(-1); |
| 3927 | |
| 3928 | if (rng == NULL) { |
| 3929 | if (reader->rngSchemas != NULL) { |
| 3930 | xmlRelaxNGFree(reader->rngSchemas); |
| 3931 | reader->rngSchemas = NULL; |
| 3932 | } |
| 3933 | if (reader->rngValidCtxt != NULL) { |
| 3934 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 3935 | reader->rngValidCtxt = NULL; |
| 3936 | } |
| 3937 | return(0); |
| 3938 | } |
| 3939 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) |
| 3940 | return(-1); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 3941 | if (reader->rngSchemas != NULL) { |
| 3942 | xmlRelaxNGFree(reader->rngSchemas); |
| 3943 | reader->rngSchemas = NULL; |
| 3944 | } |
| 3945 | if (reader->rngValidCtxt != NULL) { |
| 3946 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); |
| 3947 | reader->rngValidCtxt = NULL; |
| 3948 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 3949 | ctxt = xmlRelaxNGNewParserCtxt(rng); |
| 3950 | if (reader->errorFunc != NULL) { |
| 3951 | xmlRelaxNGSetParserErrors(ctxt, |
| 3952 | (xmlRelaxNGValidityErrorFunc) reader->errorFunc, |
| 3953 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, |
| 3954 | reader->errorFuncArg); |
| 3955 | } |
| 3956 | reader->rngSchemas = xmlRelaxNGParse(ctxt); |
| 3957 | xmlRelaxNGFreeParserCtxt(ctxt); |
| 3958 | if (reader->rngSchemas == NULL) |
| 3959 | return(-1); |
| 3960 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas); |
| 3961 | if (reader->rngValidCtxt == NULL) |
| 3962 | return(-1); |
| 3963 | if (reader->errorFunc != NULL) { |
| 3964 | xmlRelaxNGSetValidErrors(reader->rngValidCtxt, |
| 3965 | (xmlRelaxNGValidityErrorFunc)reader->errorFunc, |
| 3966 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, |
| 3967 | reader->errorFuncArg); |
| 3968 | } |
| 3969 | reader->rngValidErrors = 0; |
| 3970 | reader->rngFullNode = NULL; |
| 3971 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; |
| 3972 | return(0); |
| 3973 | } |
Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 3974 | #endif |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 3975 | |
Daniel Veillard | e281127 | 2004-10-19 09:04:23 +0000 | [diff] [blame] | 3976 | /** |
| 3977 | * xmlTextReaderIsNamespaceDecl: |
| 3978 | * @reader: the xmlTextReaderPtr used |
| 3979 | * |
| 3980 | * Determine whether the current node is a namespace declaration |
| 3981 | * rather than a regular attribute. |
| 3982 | * |
| 3983 | * Returns 1 if the current node is a namespace declaration, 0 if it |
| 3984 | * is a regular attribute or other type of node, or -1 in case of |
| 3985 | * error. |
| 3986 | */ |
| 3987 | int |
| 3988 | xmlTextReaderIsNamespaceDecl(xmlTextReaderPtr reader) { |
| 3989 | xmlNodePtr node; |
| 3990 | if (reader == NULL) |
| 3991 | return(-1); |
| 3992 | if (reader->node == NULL) |
| 3993 | return(-1); |
| 3994 | if (reader->curnode != NULL) |
| 3995 | node = reader->curnode; |
| 3996 | else |
| 3997 | node = reader->node; |
| 3998 | |
| 3999 | if (XML_NAMESPACE_DECL == node->type) |
| 4000 | return(1); |
| 4001 | else |
| 4002 | return(0); |
| 4003 | } |
| 4004 | |
| 4005 | /** |
| 4006 | * xmlTextReaderConstXmlVersion: |
| 4007 | * @reader: the xmlTextReaderPtr used |
| 4008 | * |
| 4009 | * Determine the XML version of the document being read. |
| 4010 | * |
| 4011 | * Returns a string containing the XML version of the document or NULL |
| 4012 | * in case of error. The string is deallocated with the reader. |
| 4013 | */ |
| 4014 | const xmlChar * |
| 4015 | xmlTextReaderConstXmlVersion(xmlTextReaderPtr reader) { |
| 4016 | xmlDocPtr doc = NULL; |
| 4017 | if (reader == NULL) |
| 4018 | return(NULL); |
| 4019 | if (reader->doc != NULL) |
| 4020 | doc = reader->doc; |
| 4021 | else if (reader->ctxt != NULL) |
| 4022 | doc = reader->ctxt->myDoc; |
| 4023 | if (doc == NULL) |
| 4024 | return(NULL); |
| 4025 | |
| 4026 | if (doc->version == NULL) |
| 4027 | return(NULL); |
| 4028 | else |
| 4029 | return(CONSTSTR(doc->version)); |
| 4030 | } |
| 4031 | |
| 4032 | /** |
| 4033 | * xmlTextReaderStandalone: |
| 4034 | * @reader: the xmlTextReaderPtr used |
| 4035 | * |
| 4036 | * Determine the standalone status of the document being read. |
| 4037 | * |
| 4038 | * Returns 1 if the document was declared to be standalone, 0 if it |
| 4039 | * was declared to be not standalone, or -1 if the document did not |
| 4040 | * specify its standalone status or in case of error. |
| 4041 | */ |
| 4042 | int |
| 4043 | xmlTextReaderStandalone(xmlTextReaderPtr reader) { |
| 4044 | xmlDocPtr doc = NULL; |
| 4045 | if (reader == NULL) |
| 4046 | return(-1); |
| 4047 | if (reader->doc != NULL) |
| 4048 | doc = reader->doc; |
| 4049 | else if (reader->ctxt != NULL) |
| 4050 | doc = reader->ctxt->myDoc; |
| 4051 | if (doc == NULL) |
| 4052 | return(-1); |
| 4053 | |
| 4054 | return(doc->standalone); |
| 4055 | } |
| 4056 | |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 4057 | /************************************************************************ |
| 4058 | * * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4059 | * Error Handling Extensions * |
| 4060 | * * |
| 4061 | ************************************************************************/ |
| 4062 | |
| 4063 | /* helper to build a xmlMalloc'ed string from a format and va_list */ |
| 4064 | static char * |
| 4065 | xmlTextReaderBuildMessage(const char *msg, va_list ap) { |
| 4066 | int size; |
| 4067 | int chars; |
| 4068 | char *larger; |
| 4069 | char *str; |
| 4070 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4071 | str = (char *) xmlMallocAtomic(150); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4072 | if (str == NULL) { |
| 4073 | xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n"); |
| 4074 | return NULL; |
| 4075 | } |
| 4076 | |
| 4077 | size = 150; |
| 4078 | |
| 4079 | while (1) { |
| 4080 | chars = vsnprintf(str, size, msg, ap); |
| 4081 | if ((chars > -1) && (chars < size)) |
| 4082 | break; |
| 4083 | if (chars > -1) |
| 4084 | size += chars + 1; |
| 4085 | else |
| 4086 | size += 100; |
| 4087 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) { |
| 4088 | xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n"); |
| 4089 | xmlFree(str); |
| 4090 | return NULL; |
| 4091 | } |
| 4092 | str = larger; |
| 4093 | } |
| 4094 | |
| 4095 | return str; |
| 4096 | } |
| 4097 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4098 | /** |
Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 4099 | * xmlTextReaderLocatorLineNumber: |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4100 | * @locator: the xmlTextReaderLocatorPtr used |
| 4101 | * |
| 4102 | * Obtain the line number for the given locator. |
| 4103 | * |
| 4104 | * Returns the line number or -1 in case of error. |
| 4105 | */ |
| 4106 | int |
| 4107 | xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) { |
| 4108 | /* we know that locator is a xmlParserCtxtPtr */ |
| 4109 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
| 4110 | int ret = -1; |
| 4111 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 4112 | if (locator == NULL) |
| 4113 | return(-1); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4114 | if (ctx->node != NULL) { |
| 4115 | ret = xmlGetLineNo(ctx->node); |
| 4116 | } |
| 4117 | else { |
| 4118 | /* inspired from error.c */ |
| 4119 | xmlParserInputPtr input; |
| 4120 | input = ctx->input; |
| 4121 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
| 4122 | input = ctx->inputTab[ctx->inputNr - 2]; |
| 4123 | if (input != NULL) { |
| 4124 | ret = input->line; |
| 4125 | } |
| 4126 | else { |
| 4127 | ret = -1; |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | return ret; |
| 4132 | } |
| 4133 | |
| 4134 | /** |
Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 4135 | * xmlTextReaderLocatorBaseURI: |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4136 | * @locator: the xmlTextReaderLocatorPtr used |
| 4137 | * |
| 4138 | * Obtain the base URI for the given locator. |
| 4139 | * |
| 4140 | * Returns the base URI or NULL in case of error. |
| 4141 | */ |
| 4142 | xmlChar * |
| 4143 | xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) { |
| 4144 | /* we know that locator is a xmlParserCtxtPtr */ |
| 4145 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; |
| 4146 | xmlChar *ret = NULL; |
| 4147 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 4148 | if (locator == NULL) |
| 4149 | return(NULL); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4150 | if (ctx->node != NULL) { |
| 4151 | ret = xmlNodeGetBase(NULL,ctx->node); |
| 4152 | } |
| 4153 | else { |
| 4154 | /* inspired from error.c */ |
| 4155 | xmlParserInputPtr input; |
| 4156 | input = ctx->input; |
| 4157 | if ((input->filename == NULL) && (ctx->inputNr > 1)) |
| 4158 | input = ctx->inputTab[ctx->inputNr - 2]; |
| 4159 | if (input != NULL) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 4160 | ret = xmlStrdup(BAD_CAST input->filename); |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4161 | } |
| 4162 | else { |
| 4163 | ret = NULL; |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | return ret; |
| 4168 | } |
| 4169 | |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4170 | static void |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 4171 | xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity, char *str) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4172 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt; |
| 4173 | xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private; |
| 4174 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4175 | if (str != NULL) { |
| 4176 | if (reader->errorFunc) |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4177 | reader->errorFunc(reader->errorFuncArg, |
| 4178 | str, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4179 | severity, |
| 4180 | (xmlTextReaderLocatorPtr)ctx); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4181 | xmlFree(str); |
| 4182 | } |
| 4183 | } |
| 4184 | |
| 4185 | static void |
William M. Brack | 93d004f | 2004-02-03 00:14:10 +0000 | [diff] [blame] | 4186 | xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error) { |
| 4187 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt; |
| 4188 | xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private; |
| 4189 | |
| 4190 | if (error && reader->sErrorFunc) { |
| 4191 | reader->sErrorFunc(reader->errorFuncArg, |
| 4192 | (xmlErrorPtr) error); |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | static void |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4197 | xmlTextReaderError(void *ctxt, const char *msg, ...) { |
| 4198 | va_list ap; |
| 4199 | |
| 4200 | va_start(ap,msg); |
| 4201 | xmlTextReaderGenericError(ctxt, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4202 | XML_PARSER_SEVERITY_ERROR, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4203 | xmlTextReaderBuildMessage(msg,ap)); |
| 4204 | va_end(ap); |
| 4205 | |
| 4206 | } |
| 4207 | |
| 4208 | static void |
| 4209 | xmlTextReaderWarning(void *ctxt, const char *msg, ...) { |
| 4210 | va_list ap; |
| 4211 | |
| 4212 | va_start(ap,msg); |
| 4213 | xmlTextReaderGenericError(ctxt, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4214 | XML_PARSER_SEVERITY_WARNING, |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4215 | xmlTextReaderBuildMessage(msg,ap)); |
| 4216 | va_end(ap); |
| 4217 | } |
| 4218 | |
| 4219 | static void |
| 4220 | xmlTextReaderValidityError(void *ctxt, const char *msg, ...) { |
| 4221 | va_list ap; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4222 | int len = xmlStrlen((const xmlChar *) msg); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4223 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4224 | if ((len > 1) && (msg[len - 2] != ':')) { |
| 4225 | /* |
| 4226 | * some callbacks only report locator information: |
| 4227 | * skip them (mimicking behaviour in error.c) |
| 4228 | */ |
| 4229 | va_start(ap,msg); |
| 4230 | xmlTextReaderGenericError(ctxt, |
| 4231 | XML_PARSER_SEVERITY_VALIDITY_ERROR, |
| 4232 | xmlTextReaderBuildMessage(msg,ap)); |
| 4233 | va_end(ap); |
| 4234 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
| 4237 | static void |
| 4238 | xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) { |
| 4239 | va_list ap; |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4240 | int len = xmlStrlen((const xmlChar *) msg); |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4241 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4242 | if ((len != 0) && (msg[len - 1] != ':')) { |
| 4243 | /* |
| 4244 | * some callbacks only report locator information: |
| 4245 | * skip them (mimicking behaviour in error.c) |
| 4246 | */ |
| 4247 | va_start(ap,msg); |
| 4248 | xmlTextReaderGenericError(ctxt, |
| 4249 | XML_PARSER_SEVERITY_VALIDITY_WARNING, |
| 4250 | xmlTextReaderBuildMessage(msg,ap)); |
| 4251 | va_end(ap); |
| 4252 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | /** |
| 4256 | * xmlTextReaderSetErrorHandler: |
| 4257 | * @reader: the xmlTextReaderPtr used |
| 4258 | * @f: the callback function to call on error and warnings |
| 4259 | * @arg: a user argument to pass to the callback function |
| 4260 | * |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4261 | * Register a callback function that will be called on error and warnings. |
| 4262 | * |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4263 | * If @f is NULL, the default error and warning handlers are restored. |
| 4264 | */ |
| 4265 | void |
| 4266 | xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader, |
| 4267 | xmlTextReaderErrorFunc f, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4268 | void *arg) { |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4269 | if (f != NULL) { |
| 4270 | reader->ctxt->sax->error = xmlTextReaderError; |
William M. Brack | 93d004f | 2004-02-03 00:14:10 +0000 | [diff] [blame] | 4271 | reader->ctxt->sax->serror = NULL; |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4272 | reader->ctxt->vctxt.error = xmlTextReaderValidityError; |
| 4273 | reader->ctxt->sax->warning = xmlTextReaderWarning; |
| 4274 | reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning; |
| 4275 | reader->errorFunc = f; |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 4276 | reader->sErrorFunc = NULL; |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4277 | reader->errorFuncArg = arg; |
| 4278 | } |
| 4279 | else { |
| 4280 | /* restore defaults */ |
| 4281 | reader->ctxt->sax->error = xmlParserError; |
| 4282 | reader->ctxt->vctxt.error = xmlParserValidityError; |
| 4283 | reader->ctxt->sax->warning = xmlParserWarning; |
| 4284 | reader->ctxt->vctxt.warning = xmlParserValidityWarning; |
| 4285 | reader->errorFunc = NULL; |
William M. Brack | 93d004f | 2004-02-03 00:14:10 +0000 | [diff] [blame] | 4286 | reader->sErrorFunc = NULL; |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4287 | reader->errorFuncArg = NULL; |
| 4288 | } |
| 4289 | } |
| 4290 | |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4291 | /** |
William M. Brack | 93d004f | 2004-02-03 00:14:10 +0000 | [diff] [blame] | 4292 | * xmlTextReaderSetStructuredErrorHandler: |
| 4293 | * @reader: the xmlTextReaderPtr used |
| 4294 | * @f: the callback function to call on error and warnings |
| 4295 | * @arg: a user argument to pass to the callback function |
| 4296 | * |
| 4297 | * Register a callback function that will be called on error and warnings. |
| 4298 | * |
| 4299 | * If @f is NULL, the default error and warning handlers are restored. |
| 4300 | */ |
| 4301 | void |
| 4302 | xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader, |
| 4303 | xmlStructuredErrorFunc f, |
| 4304 | void *arg) { |
| 4305 | if (f != NULL) { |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 4306 | reader->ctxt->sax->error = NULL; |
William M. Brack | 93d004f | 2004-02-03 00:14:10 +0000 | [diff] [blame] | 4307 | reader->ctxt->sax->serror = xmlTextReaderStructuredError; |
| 4308 | reader->ctxt->vctxt.error = xmlTextReaderValidityError; |
| 4309 | reader->ctxt->sax->warning = xmlTextReaderWarning; |
| 4310 | reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning; |
| 4311 | reader->sErrorFunc = f; |
| 4312 | reader->errorFunc = NULL; |
| 4313 | reader->errorFuncArg = arg; |
| 4314 | } |
| 4315 | else { |
| 4316 | /* restore defaults */ |
| 4317 | reader->ctxt->sax->error = xmlParserError; |
| 4318 | reader->ctxt->sax->serror = NULL; |
| 4319 | reader->ctxt->vctxt.error = xmlParserValidityError; |
| 4320 | reader->ctxt->sax->warning = xmlParserWarning; |
| 4321 | reader->ctxt->vctxt.warning = xmlParserValidityWarning; |
| 4322 | reader->errorFunc = NULL; |
| 4323 | reader->sErrorFunc = NULL; |
| 4324 | reader->errorFuncArg = NULL; |
| 4325 | } |
| 4326 | } |
| 4327 | |
| 4328 | /** |
Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 4329 | * xmlTextReaderIsValid: |
| 4330 | * @reader: the xmlTextReaderPtr used |
| 4331 | * |
| 4332 | * Retrieve the validity status from the parser context |
| 4333 | * |
| 4334 | * Returns the flag value 1 if valid, 0 if no, and -1 in case of error |
| 4335 | */ |
| 4336 | int |
| 4337 | xmlTextReaderIsValid(xmlTextReaderPtr reader) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 4338 | if (reader == NULL) return(-1); |
| 4339 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 4340 | if (reader->validate == XML_TEXTREADER_VALIDATE_RNG) |
| 4341 | return(reader->rngValidErrors == 0); |
| 4342 | #endif |
Daniel Veillard | 16ed597 | 2003-11-20 18:22:31 +0000 | [diff] [blame] | 4343 | if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 4344 | return(reader->ctxt->valid); |
| 4345 | return(0); |
Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | /** |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4349 | * xmlTextReaderGetErrorHandler: |
| 4350 | * @reader: the xmlTextReaderPtr used |
| 4351 | * @f: the callback function or NULL is no callback has been registered |
| 4352 | * @arg: a user argument |
| 4353 | * |
| 4354 | * Retrieve the error callback function and user argument. |
| 4355 | */ |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4356 | void |
| 4357 | xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader, |
| 4358 | xmlTextReaderErrorFunc *f, |
Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 4359 | void **arg) { |
Daniel Veillard | d5cc0f7 | 2004-11-06 19:24:28 +0000 | [diff] [blame] | 4360 | if (f != NULL) *f = reader->errorFunc; |
| 4361 | if (arg != NULL) *arg = reader->errorFuncArg; |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4364 | |
| 4365 | /************************************************************************ |
| 4366 | * * |
| 4367 | * New set (2.6.0) of simpler and more flexible APIs * |
| 4368 | * * |
| 4369 | ************************************************************************/ |
| 4370 | |
| 4371 | /** |
| 4372 | * xmlTextReaderSetup: |
| 4373 | * @reader: an XML reader |
| 4374 | * @URL: the base URL to use for the document |
| 4375 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4376 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4377 | * @reuse: keep the context for reuse |
| 4378 | * |
| 4379 | * Setup an XML reader with new options |
| 4380 | * |
| 4381 | * Returns 0 in case of success and -1 in case of error. |
| 4382 | */ |
| 4383 | static int |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4384 | xmlTextReaderSetup(xmlTextReaderPtr reader, |
| 4385 | xmlParserInputBufferPtr input, const char *URL, |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4386 | const char *encoding, int options) |
| 4387 | { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4388 | if (reader == NULL) |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4389 | return (-1); |
| 4390 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4391 | reader->doc = NULL; |
| 4392 | reader->entNr = 0; |
Daniel Veillard | e74d2e1 | 2003-12-09 11:35:37 +0000 | [diff] [blame] | 4393 | reader->parserFlags = options; |
Daniel Veillard | c36965d | 2003-12-02 10:28:48 +0000 | [diff] [blame] | 4394 | reader->validate = XML_TEXTREADER_NOT_VALIDATE; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4395 | if ((input != NULL) && (reader->input != NULL) && |
| 4396 | (reader->allocs & XML_TEXTREADER_INPUT)) { |
| 4397 | xmlFreeParserInputBuffer(reader->input); |
| 4398 | reader->input = NULL; |
| 4399 | reader->allocs -= XML_TEXTREADER_INPUT; |
| 4400 | } |
| 4401 | if (input != NULL) { |
| 4402 | reader->input = input; |
| 4403 | reader->allocs |= XML_TEXTREADER_INPUT; |
| 4404 | } |
| 4405 | if (reader->buffer == NULL) |
| 4406 | reader->buffer = xmlBufferCreateSize(100); |
| 4407 | if (reader->buffer == NULL) { |
| 4408 | xmlGenericError(xmlGenericErrorContext, |
| 4409 | "xmlTextReaderSetup : malloc failed\n"); |
| 4410 | return (-1); |
| 4411 | } |
| 4412 | if (reader->sax == NULL) |
| 4413 | reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
| 4414 | if (reader->sax == NULL) { |
| 4415 | xmlGenericError(xmlGenericErrorContext, |
| 4416 | "xmlTextReaderSetup : malloc failed\n"); |
| 4417 | return (-1); |
| 4418 | } |
| 4419 | xmlSAXVersion(reader->sax, 2); |
| 4420 | reader->startElement = reader->sax->startElement; |
| 4421 | reader->sax->startElement = xmlTextReaderStartElement; |
| 4422 | reader->endElement = reader->sax->endElement; |
| 4423 | reader->sax->endElement = xmlTextReaderEndElement; |
| 4424 | #ifdef LIBXML_SAX1_ENABLED |
| 4425 | if (reader->sax->initialized == XML_SAX2_MAGIC) { |
| 4426 | #endif /* LIBXML_SAX1_ENABLED */ |
| 4427 | reader->startElementNs = reader->sax->startElementNs; |
| 4428 | reader->sax->startElementNs = xmlTextReaderStartElementNs; |
| 4429 | reader->endElementNs = reader->sax->endElementNs; |
| 4430 | reader->sax->endElementNs = xmlTextReaderEndElementNs; |
| 4431 | #ifdef LIBXML_SAX1_ENABLED |
| 4432 | } else { |
| 4433 | reader->startElementNs = NULL; |
| 4434 | reader->endElementNs = NULL; |
| 4435 | } |
| 4436 | #endif /* LIBXML_SAX1_ENABLED */ |
| 4437 | reader->characters = reader->sax->characters; |
| 4438 | reader->sax->characters = xmlTextReaderCharacters; |
| 4439 | reader->sax->ignorableWhitespace = xmlTextReaderCharacters; |
| 4440 | reader->cdataBlock = reader->sax->cdataBlock; |
| 4441 | reader->sax->cdataBlock = xmlTextReaderCDataBlock; |
| 4442 | |
| 4443 | reader->mode = XML_TEXTREADER_MODE_INITIAL; |
| 4444 | reader->node = NULL; |
| 4445 | reader->curnode = NULL; |
| 4446 | if (input != NULL) { |
| 4447 | if (reader->input->buffer->use < 4) { |
| 4448 | xmlParserInputBufferRead(input, 4); |
| 4449 | } |
| 4450 | if (reader->ctxt == NULL) { |
| 4451 | if (reader->input->buffer->use >= 4) { |
| 4452 | reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL, |
| 4453 | (const char *) reader->input->buffer->content, 4, URL); |
| 4454 | reader->base = 0; |
| 4455 | reader->cur = 4; |
| 4456 | } else { |
| 4457 | reader->ctxt = |
| 4458 | xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL); |
| 4459 | reader->base = 0; |
| 4460 | reader->cur = 0; |
| 4461 | } |
| 4462 | } else { |
| 4463 | xmlParserInputPtr inputStream; |
| 4464 | xmlParserInputBufferPtr buf; |
| 4465 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 4466 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4467 | xmlCtxtReset(reader->ctxt); |
| 4468 | buf = xmlAllocParserInputBuffer(enc); |
| 4469 | if (buf == NULL) return(-1); |
| 4470 | inputStream = xmlNewInputStream(reader->ctxt); |
| 4471 | if (inputStream == NULL) { |
| 4472 | xmlFreeParserInputBuffer(buf); |
| 4473 | return(-1); |
| 4474 | } |
| 4475 | |
| 4476 | if (URL == NULL) |
| 4477 | inputStream->filename = NULL; |
| 4478 | else |
| 4479 | inputStream->filename = (char *) |
| 4480 | xmlCanonicPath((const xmlChar *) URL); |
| 4481 | inputStream->buf = buf; |
| 4482 | inputStream->base = inputStream->buf->buffer->content; |
| 4483 | inputStream->cur = inputStream->buf->buffer->content; |
| 4484 | inputStream->end = |
| 4485 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
| 4486 | |
| 4487 | inputPush(reader->ctxt, inputStream); |
| 4488 | reader->cur = 0; |
| 4489 | } |
| 4490 | if (reader->ctxt == NULL) { |
| 4491 | xmlGenericError(xmlGenericErrorContext, |
| 4492 | "xmlTextReaderSetup : malloc failed\n"); |
| 4493 | return (-1); |
| 4494 | } |
| 4495 | } |
| 4496 | if (reader->dict != NULL) { |
| 4497 | if (reader->ctxt->dict != NULL) { |
| 4498 | if (reader->dict != reader->ctxt->dict) { |
| 4499 | xmlDictFree(reader->dict); |
| 4500 | reader->dict = reader->ctxt->dict; |
| 4501 | } |
| 4502 | } else { |
| 4503 | reader->ctxt->dict = reader->dict; |
| 4504 | } |
| 4505 | } else { |
| 4506 | if (reader->ctxt->dict == NULL) |
| 4507 | reader->ctxt->dict = xmlDictCreate(); |
| 4508 | reader->dict = reader->ctxt->dict; |
| 4509 | } |
| 4510 | reader->ctxt->_private = reader; |
| 4511 | reader->ctxt->linenumbers = 1; |
| 4512 | reader->ctxt->dictNames = 1; |
| 4513 | /* |
| 4514 | * use the parser dictionnary to allocate all elements and attributes names |
| 4515 | */ |
| 4516 | reader->ctxt->docdict = 1; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 4517 | reader->ctxt->parseMode = XML_PARSE_READER; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4518 | |
Daniel Veillard | 7899c5c | 2003-11-03 12:31:38 +0000 | [diff] [blame] | 4519 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 4520 | if (reader->xincctxt != NULL) { |
| 4521 | xmlXIncludeFreeContext(reader->xincctxt); |
| 4522 | reader->xincctxt = NULL; |
| 4523 | } |
| 4524 | if (options & XML_PARSE_XINCLUDE) { |
| 4525 | reader->xinclude = 1; |
| 4526 | reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1); |
| 4527 | options -= XML_PARSE_XINCLUDE; |
| 4528 | } else |
| 4529 | reader->xinclude = 0; |
| 4530 | reader->in_xinclude = 0; |
| 4531 | #endif |
Daniel Veillard | 1e90661 | 2003-12-05 14:57:46 +0000 | [diff] [blame] | 4532 | #ifdef LIBXML_PATTERN_ENABLED |
| 4533 | if (reader->patternTab == NULL) { |
| 4534 | reader->patternNr = 0; |
| 4535 | reader->patternMax = 0; |
| 4536 | } |
| 4537 | while (reader->patternNr > 0) { |
| 4538 | reader->patternNr--; |
| 4539 | if (reader->patternTab[reader->patternNr] != NULL) { |
| 4540 | xmlFreePattern(reader->patternTab[reader->patternNr]); |
| 4541 | reader->patternTab[reader->patternNr] = NULL; |
| 4542 | } |
| 4543 | } |
| 4544 | #endif |
| 4545 | |
Daniel Veillard | c36965d | 2003-12-02 10:28:48 +0000 | [diff] [blame] | 4546 | if (options & XML_PARSE_DTDVALID) |
| 4547 | reader->validate = XML_TEXTREADER_VALIDATE_DTD; |
| 4548 | |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4549 | xmlCtxtUseOptions(reader->ctxt, options); |
| 4550 | if (encoding != NULL) { |
| 4551 | xmlCharEncodingHandlerPtr hdlr; |
| 4552 | |
| 4553 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 4554 | if (hdlr != NULL) |
| 4555 | xmlSwitchToEncoding(reader->ctxt, hdlr); |
| 4556 | } |
| 4557 | if ((URL != NULL) && (reader->ctxt->input != NULL) && |
| 4558 | (reader->ctxt->input->filename == NULL)) |
| 4559 | reader->ctxt->input->filename = (char *) |
| 4560 | xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4561 | |
| 4562 | reader->doc = NULL; |
| 4563 | |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4564 | return (0); |
| 4565 | } |
| 4566 | |
| 4567 | /** |
Daniel Veillard | 5e09414 | 2005-02-18 19:36:12 +0000 | [diff] [blame] | 4568 | * xmlTextReaderByteConsumed: |
| 4569 | * @reader: an XML reader |
| 4570 | * |
| 4571 | * This function provides the current index of the parser used |
| 4572 | * by the reader, relative to the start of the current entity. |
| 4573 | * This function actually just wraps a call to xmlBytesConsumed() |
| 4574 | * for the parser context associated with the reader. |
| 4575 | * See xmlBytesConsumed() for more information. |
| 4576 | * |
| 4577 | * Returns the index in bytes from the beginning of the entity or -1 |
| 4578 | * in case the index could not be computed. |
| 4579 | */ |
| 4580 | long |
| 4581 | xmlTextReaderByteConsumed(xmlTextReaderPtr reader) { |
| 4582 | if ((reader == NULL) || (reader->ctxt == NULL)) |
| 4583 | return(-1); |
| 4584 | return(xmlByteConsumed(reader->ctxt)); |
| 4585 | } |
| 4586 | |
| 4587 | |
| 4588 | /** |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4589 | * xmlReaderWalker: |
| 4590 | * @doc: a preparsed document |
| 4591 | * |
| 4592 | * Create an xmltextReader for a preparsed document. |
| 4593 | * |
| 4594 | * Returns the new reader or NULL in case of error. |
| 4595 | */ |
| 4596 | xmlTextReaderPtr |
| 4597 | xmlReaderWalker(xmlDocPtr doc) |
| 4598 | { |
| 4599 | xmlTextReaderPtr ret; |
| 4600 | |
| 4601 | if (doc == NULL) |
| 4602 | return(NULL); |
| 4603 | |
| 4604 | ret = xmlMalloc(sizeof(xmlTextReader)); |
| 4605 | if (ret == NULL) { |
| 4606 | xmlGenericError(xmlGenericErrorContext, |
| 4607 | "xmlNewTextReader : malloc failed\n"); |
| 4608 | return(NULL); |
| 4609 | } |
| 4610 | memset(ret, 0, sizeof(xmlTextReader)); |
| 4611 | ret->entNr = 0; |
| 4612 | ret->input = NULL; |
| 4613 | ret->mode = XML_TEXTREADER_MODE_INITIAL; |
| 4614 | ret->node = NULL; |
| 4615 | ret->curnode = NULL; |
| 4616 | ret->base = 0; |
| 4617 | ret->cur = 0; |
| 4618 | ret->allocs = XML_TEXTREADER_CTXT; |
| 4619 | ret->doc = doc; |
| 4620 | ret->state = XML_TEXTREADER_START; |
| 4621 | ret->dict = xmlDictCreate(); |
| 4622 | return(ret); |
| 4623 | } |
| 4624 | |
| 4625 | /** |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4626 | * xmlReaderForDoc: |
| 4627 | * @cur: a pointer to a zero terminated string |
| 4628 | * @URL: the base URL to use for the document |
| 4629 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4630 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4631 | * |
| 4632 | * Create an xmltextReader for an XML in-memory document. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4633 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4634 | * |
| 4635 | * Returns the new reader or NULL in case of error. |
| 4636 | */ |
| 4637 | xmlTextReaderPtr |
| 4638 | xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding, |
| 4639 | int options) |
| 4640 | { |
| 4641 | int len; |
| 4642 | |
| 4643 | if (cur == NULL) |
| 4644 | return (NULL); |
| 4645 | len = xmlStrlen(cur); |
| 4646 | |
| 4647 | return (xmlReaderForMemory |
| 4648 | ((const char *) cur, len, URL, encoding, options)); |
| 4649 | } |
| 4650 | |
| 4651 | /** |
| 4652 | * xmlReaderForFile: |
| 4653 | * @filename: a file or URL |
| 4654 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4655 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4656 | * |
| 4657 | * parse an XML file from the filesystem or the network. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4658 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4659 | * |
| 4660 | * Returns the new reader or NULL in case of error. |
| 4661 | */ |
| 4662 | xmlTextReaderPtr |
| 4663 | xmlReaderForFile(const char *filename, const char *encoding, int options) |
| 4664 | { |
| 4665 | xmlTextReaderPtr reader; |
| 4666 | |
| 4667 | reader = xmlNewTextReaderFilename(filename); |
| 4668 | if (reader == NULL) |
| 4669 | return (NULL); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4670 | xmlTextReaderSetup(reader, NULL, NULL, encoding, options); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4671 | return (reader); |
| 4672 | } |
| 4673 | |
| 4674 | /** |
| 4675 | * xmlReaderForMemory: |
| 4676 | * @buffer: a pointer to a char array |
| 4677 | * @size: the size of the array |
| 4678 | * @URL: the base URL to use for the document |
| 4679 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4680 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4681 | * |
| 4682 | * Create an xmltextReader for an XML in-memory document. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4683 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4684 | * |
| 4685 | * Returns the new reader or NULL in case of error. |
| 4686 | */ |
| 4687 | xmlTextReaderPtr |
| 4688 | xmlReaderForMemory(const char *buffer, int size, const char *URL, |
| 4689 | const char *encoding, int options) |
| 4690 | { |
| 4691 | xmlTextReaderPtr reader; |
| 4692 | xmlParserInputBufferPtr buf; |
| 4693 | |
Daniel Veillard | 2192452 | 2004-02-19 16:37:07 +0000 | [diff] [blame] | 4694 | buf = xmlParserInputBufferCreateStatic(buffer, size, |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4695 | XML_CHAR_ENCODING_NONE); |
| 4696 | if (buf == NULL) { |
| 4697 | return (NULL); |
| 4698 | } |
| 4699 | reader = xmlNewTextReader(buf, URL); |
| 4700 | if (reader == NULL) { |
| 4701 | xmlFreeParserInputBuffer(buf); |
| 4702 | return (NULL); |
| 4703 | } |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4704 | reader->allocs |= XML_TEXTREADER_INPUT; |
| 4705 | xmlTextReaderSetup(reader, NULL, URL, encoding, options); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4706 | return (reader); |
| 4707 | } |
| 4708 | |
| 4709 | /** |
| 4710 | * xmlReaderForFd: |
| 4711 | * @fd: an open file descriptor |
| 4712 | * @URL: the base URL to use for the document |
| 4713 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4714 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4715 | * |
| 4716 | * Create an xmltextReader for an XML from a file descriptor. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4717 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 4718 | * NOTE that the file descriptor will not be closed when the |
| 4719 | * reader is closed or reset. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4720 | * |
| 4721 | * Returns the new reader or NULL in case of error. |
| 4722 | */ |
| 4723 | xmlTextReaderPtr |
| 4724 | xmlReaderForFd(int fd, const char *URL, const char *encoding, int options) |
| 4725 | { |
| 4726 | xmlTextReaderPtr reader; |
| 4727 | xmlParserInputBufferPtr input; |
| 4728 | |
| 4729 | if (fd < 0) |
| 4730 | return (NULL); |
| 4731 | |
| 4732 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 4733 | if (input == NULL) |
| 4734 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 4735 | input->closecallback = NULL; |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4736 | reader = xmlNewTextReader(input, URL); |
| 4737 | if (reader == NULL) { |
| 4738 | xmlFreeParserInputBuffer(input); |
| 4739 | return (NULL); |
| 4740 | } |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4741 | reader->allocs |= XML_TEXTREADER_INPUT; |
| 4742 | xmlTextReaderSetup(reader, NULL, URL, encoding, options); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4743 | return (reader); |
| 4744 | } |
| 4745 | |
| 4746 | /** |
| 4747 | * xmlReaderForIO: |
| 4748 | * @ioread: an I/O read function |
| 4749 | * @ioclose: an I/O close function |
| 4750 | * @ioctx: an I/O handler |
| 4751 | * @URL: the base URL to use for the document |
| 4752 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4753 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4754 | * |
| 4755 | * 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] | 4756 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4757 | * |
| 4758 | * Returns the new reader or NULL in case of error. |
| 4759 | */ |
| 4760 | xmlTextReaderPtr |
| 4761 | xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 4762 | void *ioctx, const char *URL, const char *encoding, |
| 4763 | int options) |
| 4764 | { |
| 4765 | xmlTextReaderPtr reader; |
| 4766 | xmlParserInputBufferPtr input; |
| 4767 | |
| 4768 | if (ioread == NULL) |
| 4769 | return (NULL); |
| 4770 | |
| 4771 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 4772 | XML_CHAR_ENCODING_NONE); |
| 4773 | if (input == NULL) |
| 4774 | return (NULL); |
| 4775 | reader = xmlNewTextReader(input, URL); |
| 4776 | if (reader == NULL) { |
| 4777 | xmlFreeParserInputBuffer(input); |
| 4778 | return (NULL); |
| 4779 | } |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4780 | reader->allocs |= XML_TEXTREADER_INPUT; |
| 4781 | xmlTextReaderSetup(reader, NULL, URL, encoding, options); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4782 | return (reader); |
| 4783 | } |
| 4784 | |
| 4785 | /** |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4786 | * xmlReaderNewWalker: |
| 4787 | * @reader: an XML reader |
| 4788 | * @doc: a preparsed document |
| 4789 | * |
| 4790 | * Setup an xmltextReader to parse a preparsed XML document. |
| 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 | xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc) |
| 4797 | { |
| 4798 | if (doc == NULL) |
| 4799 | return (-1); |
| 4800 | if (reader == NULL) |
| 4801 | return (-1); |
| 4802 | |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 4803 | if (reader->input != NULL) { |
| 4804 | xmlFreeParserInputBuffer(reader->input); |
| 4805 | } |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4806 | if (reader->ctxt != NULL) { |
| 4807 | xmlCtxtReset(reader->ctxt); |
| 4808 | } |
| 4809 | |
| 4810 | reader->entNr = 0; |
| 4811 | reader->input = NULL; |
| 4812 | reader->mode = XML_TEXTREADER_MODE_INITIAL; |
| 4813 | reader->node = NULL; |
| 4814 | reader->curnode = NULL; |
| 4815 | reader->base = 0; |
| 4816 | reader->cur = 0; |
| 4817 | reader->allocs = XML_TEXTREADER_CTXT; |
| 4818 | reader->doc = doc; |
| 4819 | reader->state = XML_TEXTREADER_START; |
| 4820 | if (reader->dict == NULL) { |
| 4821 | if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL)) |
| 4822 | reader->dict = reader->ctxt->dict; |
| 4823 | else |
| 4824 | reader->dict = xmlDictCreate(); |
| 4825 | } |
| 4826 | return(0); |
| 4827 | } |
| 4828 | |
| 4829 | /** |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4830 | * xmlReaderNewDoc: |
| 4831 | * @reader: an XML reader |
| 4832 | * @cur: a pointer to a zero terminated string |
| 4833 | * @URL: the base URL to use for the document |
| 4834 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4835 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4836 | * |
| 4837 | * Setup an xmltextReader to parse an XML in-memory document. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4838 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4839 | * This reuses the existing @reader xmlTextReader. |
| 4840 | * |
| 4841 | * Returns 0 in case of success and -1 in case of error |
| 4842 | */ |
| 4843 | int |
| 4844 | xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur, |
| 4845 | const char *URL, const char *encoding, int options) |
| 4846 | { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4847 | |
| 4848 | int len; |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4849 | |
| 4850 | if (cur == NULL) |
| 4851 | return (-1); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4852 | if (reader == NULL) |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4853 | return (-1); |
| 4854 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4855 | len = xmlStrlen(cur); |
| 4856 | return (xmlReaderNewMemory(reader, (const char *)cur, len, |
| 4857 | URL, encoding, options)); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | /** |
| 4861 | * xmlReaderNewFile: |
| 4862 | * @reader: an XML reader |
| 4863 | * @filename: a file or URL |
| 4864 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4865 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4866 | * |
| 4867 | * parse an XML file from the filesystem or the network. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4868 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4869 | * This reuses the existing @reader xmlTextReader. |
| 4870 | * |
| 4871 | * Returns 0 in case of success and -1 in case of error |
| 4872 | */ |
| 4873 | int |
| 4874 | xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename, |
| 4875 | const char *encoding, int options) |
| 4876 | { |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4877 | xmlParserInputBufferPtr input; |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4878 | |
| 4879 | if (filename == NULL) |
| 4880 | return (-1); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4881 | if (reader == NULL) |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4882 | return (-1); |
| 4883 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4884 | input = |
| 4885 | xmlParserInputBufferCreateFilename(filename, |
| 4886 | XML_CHAR_ENCODING_NONE); |
| 4887 | if (input == NULL) |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4888 | return (-1); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4889 | return (xmlTextReaderSetup(reader, input, filename, encoding, options)); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | /** |
| 4893 | * xmlReaderNewMemory: |
| 4894 | * @reader: an XML reader |
| 4895 | * @buffer: a pointer to a char array |
| 4896 | * @size: the size of the array |
| 4897 | * @URL: the base URL to use for the document |
| 4898 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4899 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4900 | * |
| 4901 | * Setup an xmltextReader to parse an XML in-memory document. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4902 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4903 | * This reuses the existing @reader xmlTextReader. |
| 4904 | * |
| 4905 | * Returns 0 in case of success and -1 in case of error |
| 4906 | */ |
| 4907 | int |
| 4908 | xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size, |
| 4909 | const char *URL, const char *encoding, int options) |
| 4910 | { |
| 4911 | xmlParserInputBufferPtr input; |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4912 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4913 | if (reader == NULL) |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4914 | return (-1); |
| 4915 | if (buffer == NULL) |
| 4916 | return (-1); |
| 4917 | |
Daniel Veillard | 2192452 | 2004-02-19 16:37:07 +0000 | [diff] [blame] | 4918 | input = xmlParserInputBufferCreateStatic(buffer, size, |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4919 | XML_CHAR_ENCODING_NONE); |
| 4920 | if (input == NULL) { |
| 4921 | return (-1); |
| 4922 | } |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4923 | return (xmlTextReaderSetup(reader, input, URL, encoding, options)); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | /** |
| 4927 | * xmlReaderNewFd: |
| 4928 | * @reader: an XML reader |
| 4929 | * @fd: an open file descriptor |
| 4930 | * @URL: the base URL to use for the document |
| 4931 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4932 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4933 | * |
| 4934 | * Setup an xmltextReader to parse an XML from a file descriptor. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 4935 | * NOTE that the file descriptor will not be closed when the |
| 4936 | * reader is closed or reset. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4937 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4938 | * This reuses the existing @reader xmlTextReader. |
| 4939 | * |
| 4940 | * Returns 0 in case of success and -1 in case of error |
| 4941 | */ |
| 4942 | int |
| 4943 | xmlReaderNewFd(xmlTextReaderPtr reader, int fd, |
| 4944 | const char *URL, const char *encoding, int options) |
| 4945 | { |
| 4946 | xmlParserInputBufferPtr input; |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4947 | |
| 4948 | if (fd < 0) |
| 4949 | return (-1); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4950 | if (reader == NULL) |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4951 | return (-1); |
| 4952 | |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4953 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 4954 | if (input == NULL) |
| 4955 | return (-1); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 4956 | input->closecallback = NULL; |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4957 | return (xmlTextReaderSetup(reader, input, URL, encoding, options)); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /** |
| 4961 | * xmlReaderNewIO: |
| 4962 | * @reader: an XML reader |
| 4963 | * @ioread: an I/O read function |
| 4964 | * @ioclose: an I/O close function |
| 4965 | * @ioctx: an I/O handler |
| 4966 | * @URL: the base URL to use for the document |
| 4967 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4968 | * @options: a combination of xmlParserOption |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4969 | * |
| 4970 | * Setup an xmltextReader to parse an XML document from I/O functions |
| 4971 | * and source. |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 4972 | * The parsing flags @options are a combination of xmlParserOption. |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4973 | * This reuses the existing @reader xmlTextReader. |
| 4974 | * |
| 4975 | * Returns 0 in case of success and -1 in case of error |
| 4976 | */ |
| 4977 | int |
| 4978 | xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread, |
| 4979 | xmlInputCloseCallback ioclose, void *ioctx, |
| 4980 | const char *URL, const char *encoding, int options) |
| 4981 | { |
| 4982 | xmlParserInputBufferPtr input; |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4983 | |
| 4984 | if (ioread == NULL) |
| 4985 | return (-1); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4986 | if (reader == NULL) |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4987 | return (-1); |
| 4988 | |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4989 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 4990 | XML_CHAR_ENCODING_NONE); |
| 4991 | if (input == NULL) |
| 4992 | return (-1); |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 4993 | return (xmlTextReaderSetup(reader, input, URL, encoding, options)); |
Daniel Veillard | fc8dc35 | 2003-10-18 09:07:46 +0000 | [diff] [blame] | 4994 | } |
Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 4995 | /************************************************************************ |
| 4996 | * * |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 4997 | * Utilities * |
| 4998 | * * |
| 4999 | ************************************************************************/ |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 5000 | #ifdef NOT_USED_YET |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 5001 | /** |
| 5002 | * xmlBase64Decode: |
| 5003 | * @in: the input buffer |
| 5004 | * @inlen: the size of the input (in), the size read from it (out) |
| 5005 | * @to: the output buffer |
| 5006 | * @tolen: the size of the output (in), the size written to (out) |
| 5007 | * |
| 5008 | * Base64 decoder, reads from @in and save in @to |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5009 | * TODO: tell jody when this is actually exported |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 5010 | * |
| 5011 | * Returns 0 if all the input was consumer, 1 if the Base64 end was reached, |
| 5012 | * 2 if there wasn't enough space on the output or -1 in case of error. |
| 5013 | */ |
| 5014 | static int |
| 5015 | xmlBase64Decode(const unsigned char *in, unsigned long *inlen, |
| 5016 | unsigned char *to, unsigned long *tolen) { |
| 5017 | unsigned long incur; /* current index in in[] */ |
| 5018 | unsigned long inblk; /* last block index in in[] */ |
| 5019 | unsigned long outcur; /* current index in out[] */ |
| 5020 | unsigned long inmax; /* size of in[] */ |
| 5021 | unsigned long outmax; /* size of out[] */ |
| 5022 | unsigned char cur; /* the current value read from in[] */ |
Daniel Veillard | c127adc | 2003-07-23 15:07:08 +0000 | [diff] [blame] | 5023 | unsigned char intmp[4], outtmp[4]; /* temporary buffers for the convert */ |
Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 5024 | int nbintmp; /* number of byte in intmp[] */ |
| 5025 | int is_ignore; /* cur should be ignored */ |
| 5026 | int is_end = 0; /* the end of the base64 was found */ |
| 5027 | int retval = 1; |
| 5028 | int i; |
| 5029 | |
| 5030 | if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL)) |
| 5031 | return(-1); |
| 5032 | |
| 5033 | incur = 0; |
| 5034 | inblk = 0; |
| 5035 | outcur = 0; |
| 5036 | inmax = *inlen; |
| 5037 | outmax = *tolen; |
| 5038 | nbintmp = 0; |
| 5039 | |
| 5040 | while (1) { |
| 5041 | if (incur >= inmax) |
| 5042 | break; |
| 5043 | cur = in[incur++]; |
| 5044 | is_ignore = 0; |
| 5045 | if ((cur >= 'A') && (cur <= 'Z')) |
| 5046 | cur = cur - 'A'; |
| 5047 | else if ((cur >= 'a') && (cur <= 'z')) |
| 5048 | cur = cur - 'a' + 26; |
| 5049 | else if ((cur >= '0') && (cur <= '9')) |
| 5050 | cur = cur - '0' + 52; |
| 5051 | else if (cur == '+') |
| 5052 | cur = 62; |
| 5053 | else if (cur == '/') |
| 5054 | cur = 63; |
| 5055 | else if (cur == '.') |
| 5056 | cur = 0; |
| 5057 | else if (cur == '=') /*no op , end of the base64 stream */ |
| 5058 | is_end = 1; |
| 5059 | else { |
| 5060 | is_ignore = 1; |
| 5061 | if (nbintmp == 0) |
| 5062 | inblk = incur; |
| 5063 | } |
| 5064 | |
| 5065 | if (!is_ignore) { |
| 5066 | int nbouttmp = 3; |
| 5067 | int is_break = 0; |
| 5068 | |
| 5069 | if (is_end) { |
| 5070 | if (nbintmp == 0) |
| 5071 | break; |
| 5072 | if ((nbintmp == 1) || (nbintmp == 2)) |
| 5073 | nbouttmp = 1; |
| 5074 | else |
| 5075 | nbouttmp = 2; |
| 5076 | nbintmp = 3; |
| 5077 | is_break = 1; |
| 5078 | } |
| 5079 | intmp[nbintmp++] = cur; |
| 5080 | /* |
| 5081 | * if intmp is full, push the 4byte sequence as a 3 byte |
| 5082 | * sequence out |
| 5083 | */ |
| 5084 | if (nbintmp == 4) { |
| 5085 | nbintmp = 0; |
| 5086 | outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4); |
| 5087 | outtmp[1] = |
| 5088 | ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2); |
| 5089 | outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F); |
| 5090 | if (outcur + 3 >= outmax) { |
| 5091 | retval = 2; |
| 5092 | break; |
| 5093 | } |
| 5094 | |
| 5095 | for (i = 0; i < nbouttmp; i++) |
| 5096 | to[outcur++] = outtmp[i]; |
| 5097 | inblk = incur; |
| 5098 | } |
| 5099 | |
| 5100 | if (is_break) { |
| 5101 | retval = 0; |
| 5102 | break; |
| 5103 | } |
| 5104 | } |
| 5105 | } |
| 5106 | |
| 5107 | *tolen = outcur; |
| 5108 | *inlen = inblk; |
| 5109 | return (retval); |
| 5110 | } |
| 5111 | |
| 5112 | /* |
| 5113 | * Test routine for the xmlBase64Decode function |
| 5114 | */ |
| 5115 | #if 0 |
| 5116 | int main(int argc, char **argv) { |
| 5117 | char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== "; |
| 5118 | char output[100]; |
| 5119 | char output2[100]; |
| 5120 | char output3[100]; |
| 5121 | unsigned long inlen = strlen(input); |
| 5122 | unsigned long outlen = 100; |
| 5123 | int ret; |
| 5124 | unsigned long cons, tmp, tmp2, prod; |
| 5125 | |
| 5126 | /* |
| 5127 | * Direct |
| 5128 | */ |
| 5129 | ret = xmlBase64Decode(input, &inlen, output, &outlen); |
| 5130 | |
| 5131 | output[outlen] = 0; |
| 5132 | printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output); |
| 5133 | |
| 5134 | /* |
| 5135 | * output chunking |
| 5136 | */ |
| 5137 | cons = 0; |
| 5138 | prod = 0; |
| 5139 | while (cons < inlen) { |
| 5140 | tmp = 5; |
| 5141 | tmp2 = inlen - cons; |
| 5142 | |
| 5143 | printf("%ld %ld\n", cons, prod); |
| 5144 | ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp); |
| 5145 | cons += tmp2; |
| 5146 | prod += tmp; |
| 5147 | printf("%ld %ld\n", cons, prod); |
| 5148 | } |
| 5149 | output2[outlen] = 0; |
| 5150 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2); |
| 5151 | |
| 5152 | /* |
| 5153 | * input chunking |
| 5154 | */ |
| 5155 | cons = 0; |
| 5156 | prod = 0; |
| 5157 | while (cons < inlen) { |
| 5158 | tmp = 100 - prod; |
| 5159 | tmp2 = inlen - cons; |
| 5160 | if (tmp2 > 5) |
| 5161 | tmp2 = 5; |
| 5162 | |
| 5163 | printf("%ld %ld\n", cons, prod); |
| 5164 | ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp); |
| 5165 | cons += tmp2; |
| 5166 | prod += tmp; |
| 5167 | printf("%ld %ld\n", cons, prod); |
| 5168 | } |
| 5169 | output3[outlen] = 0; |
| 5170 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3); |
| 5171 | return(0); |
| 5172 | |
| 5173 | } |
| 5174 | #endif |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 5175 | #endif /* NOT_USED_YET */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 5176 | #define bottom_xmlreader |
| 5177 | #include "elfgcchack.h" |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5178 | #endif /* LIBXML_READER_ENABLED */ |