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