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