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