| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * xmlreader.c: implements the xmlTextReader streaming node API | 
|  | 3 | * | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 4 | * NOTE: | 
|  | 5 | *   XmlTextReader.Normalization Property won't be supported, since | 
|  | 6 | *     it makes the parser non compliant to the XML recommendation | 
|  | 7 | * | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 8 | * See Copyright for the status of this software. | 
|  | 9 | * | 
|  | 10 | * daniel@veillard.com | 
|  | 11 | */ | 
|  | 12 |  | 
| Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 13 | /* | 
|  | 14 | * TODOs: | 
| Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 15 | *   - provide an API to preserve part of the tree | 
|  | 16 | *   - Streaming XInclude support | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 17 | *   - validation against a provided DTD | 
|  | 18 | *   - XML Schemas validation | 
| Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 19 | *   - setting(s) for NoBlanks | 
|  | 20 | *   - performances and tuning ... | 
|  | 21 | */ | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 22 | #define IN_LIBXML | 
|  | 23 | #include "libxml.h" | 
|  | 24 |  | 
|  | 25 | #include <string.h> /* for memset() only ! */ | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 26 | #include <stdarg.h> | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 27 |  | 
|  | 28 | #ifdef HAVE_CTYPE_H | 
|  | 29 | #include <ctype.h> | 
|  | 30 | #endif | 
|  | 31 | #ifdef HAVE_STDLIB_H | 
|  | 32 | #include <stdlib.h> | 
|  | 33 | #endif | 
|  | 34 |  | 
|  | 35 | #include <libxml/xmlmemory.h> | 
|  | 36 | #include <libxml/xmlIO.h> | 
|  | 37 | #include <libxml/xmlreader.h> | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 38 | #include <libxml/relaxng.h> | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 39 |  | 
|  | 40 | /* #define DEBUG_CALLBACKS */ | 
|  | 41 | /* #define DEBUG_READER */ | 
|  | 42 |  | 
|  | 43 | /** | 
|  | 44 | * TODO: | 
|  | 45 | * | 
|  | 46 | * macro to flag unimplemented blocks | 
|  | 47 | */ | 
|  | 48 | #define TODO 								\ | 
|  | 49 | xmlGenericError(xmlGenericErrorContext,				\ | 
|  | 50 | "Unimplemented block at %s:%d\n",				\ | 
|  | 51 | __FILE__, __LINE__); | 
|  | 52 |  | 
|  | 53 | #ifdef DEBUG_READER | 
|  | 54 | #define DUMP_READER xmlTextReaderDebug(reader); | 
|  | 55 | #else | 
|  | 56 | #define DUMP_READER | 
|  | 57 | #endif | 
|  | 58 |  | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 59 | #define CHUNK_SIZE 512 | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 60 | /************************************************************************ | 
|  | 61 | *									* | 
|  | 62 | *	The parser: maps the Text Reader API on top of the existing	* | 
|  | 63 | *		parsing routines building a tree			* | 
|  | 64 | *									* | 
|  | 65 | ************************************************************************/ | 
|  | 66 |  | 
|  | 67 | #define XML_TEXTREADER_INPUT	1 | 
|  | 68 | #define XML_TEXTREADER_CTXT	2 | 
|  | 69 |  | 
|  | 70 | typedef enum { | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 71 | XML_TEXTREADER_MODE_INITIAL = 0, | 
|  | 72 | XML_TEXTREADER_MODE_INTERACTIVE = 1, | 
|  | 73 | XML_TEXTREADER_MODE_ERROR = 2, | 
|  | 74 | XML_TEXTREADER_MODE_EOF =3, | 
|  | 75 | XML_TEXTREADER_MODE_CLOSED = 4, | 
|  | 76 | XML_TEXTREADER_MODE_READING = 5 | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 77 | } xmlTextReaderMode; | 
|  | 78 |  | 
|  | 79 | typedef enum { | 
|  | 80 | XML_TEXTREADER_NONE = -1, | 
|  | 81 | XML_TEXTREADER_START= 0, | 
|  | 82 | XML_TEXTREADER_ELEMENT= 1, | 
|  | 83 | XML_TEXTREADER_END= 2, | 
|  | 84 | XML_TEXTREADER_EMPTY= 3, | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 85 | XML_TEXTREADER_BACKTRACK= 4, | 
| Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 86 | XML_TEXTREADER_DONE= 5, | 
|  | 87 | XML_TEXTREADER_ERROR= 6 | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 88 | } xmlTextReaderState; | 
|  | 89 |  | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 90 | typedef enum { | 
|  | 91 | XML_TEXTREADER_NOT_VALIDATE = 0, | 
|  | 92 | XML_TEXTREADER_VALIDATE_DTD = 1, | 
|  | 93 | XML_TEXTREADER_VALIDATE_RNG = 2 | 
|  | 94 | } xmlTextReaderValidate; | 
|  | 95 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 96 | struct _xmlTextReader { | 
|  | 97 | int				mode;	/* the parsing mode */ | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 98 | xmlTextReaderValidate       validate;/* is there any validation */ | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 99 | int				allocs;	/* what structure were deallocated */ | 
|  | 100 | xmlTextReaderState		state; | 
|  | 101 | xmlParserCtxtPtr		ctxt;	/* the parser context */ | 
|  | 102 | xmlSAXHandlerPtr		sax;	/* the parser SAX callbacks */ | 
|  | 103 | xmlParserInputBufferPtr	input;	/* the input */ | 
|  | 104 | startElementSAXFunc		startElement;/* initial SAX callbacks */ | 
|  | 105 | endElementSAXFunc		endElement;  /* idem */ | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 106 | charactersSAXFunc		characters; | 
|  | 107 | cdataBlockSAXFunc		cdataBlock; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 108 | unsigned int 		base;	/* base of the segment in the input */ | 
|  | 109 | unsigned int 		cur;	/* current position in the input */ | 
|  | 110 | xmlNodePtr			node;	/* current node */ | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 111 | xmlNodePtr			curnode;/* current attribute node */ | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 112 | int				depth;  /* depth of the current node */ | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 113 | xmlNodePtr			faketext;/* fake xmlNs chld */ | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 114 |  | 
|  | 115 | /* entity stack when traversing entities content */ | 
|  | 116 | xmlNodePtr         ent;          /* Current Entity Ref Node */ | 
|  | 117 | int                entNr;        /* Depth of the entities stack */ | 
|  | 118 | int                entMax;       /* Max depth of the entities stack */ | 
|  | 119 | xmlNodePtr        *entTab;       /* array of entities */ | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 120 |  | 
|  | 121 | /* error handling */ | 
|  | 122 | xmlTextReaderErrorFunc errorFunc;    /* callback function */ | 
|  | 123 | void                  *errorFuncArg; /* callback function user argument */ | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 124 |  | 
|  | 125 | #ifdef LIBXML_SCHEMAS_ENABLED | 
|  | 126 | /* Handling of RelaxNG validation */ | 
|  | 127 | xmlRelaxNGPtr          rngSchemas;   /* The Relax NG schemas */ | 
|  | 128 | xmlRelaxNGValidCtxtPtr rngValidCtxt; /* The Relax NG validation context */ | 
|  | 129 | int                  rngValidErrors; /* The number of errors detected */ | 
|  | 130 | xmlNodePtr             rngFullNode;  /* the node if RNG not progressive */ | 
|  | 131 | #endif | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 132 | }; | 
|  | 133 |  | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 134 | static const char *xmlTextReaderIsEmpty = "This element is empty"; | 
|  | 135 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 136 | #ifdef DEBUG_READER | 
|  | 137 | static void | 
|  | 138 | xmlTextReaderDebug(xmlTextReaderPtr reader) { | 
|  | 139 | if ((reader == NULL) || (reader->ctxt == NULL)) { | 
|  | 140 | fprintf(stderr, "xmlTextReader NULL\n"); | 
|  | 141 | return; | 
|  | 142 | } | 
|  | 143 | fprintf(stderr, "xmlTextReader: state %d depth %d ", | 
|  | 144 | reader->state, reader->depth); | 
|  | 145 | if (reader->node == NULL) { | 
|  | 146 | fprintf(stderr, "node = NULL\n"); | 
|  | 147 | } else { | 
|  | 148 | fprintf(stderr, "node %s\n", reader->node->name); | 
|  | 149 | } | 
|  | 150 | fprintf(stderr, "  input: base %d, cur %d, depth %d: ", | 
|  | 151 | reader->base, reader->cur, reader->ctxt->nodeNr); | 
|  | 152 | if (reader->input->buffer == NULL) { | 
|  | 153 | fprintf(stderr, "buffer is NULL\n"); | 
|  | 154 | } else { | 
|  | 155 | #ifdef LIBXML_DEBUG_ENABLED | 
|  | 156 | xmlDebugDumpString(stderr, | 
|  | 157 | &reader->input->buffer->content[reader->cur]); | 
|  | 158 | #endif | 
|  | 159 | fprintf(stderr, "\n"); | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 | #endif | 
|  | 163 |  | 
|  | 164 | /** | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 165 | * xmlTextReaderEntPush: | 
|  | 166 | * @reader:  the xmlTextReaderPtr used | 
|  | 167 | * @value:  the entity reference node | 
|  | 168 | * | 
|  | 169 | * Pushes a new entity reference node on top of the entities stack | 
|  | 170 | * | 
|  | 171 | * Returns 0 in case of error, the index in the stack otherwise | 
|  | 172 | */ | 
|  | 173 | static int | 
|  | 174 | xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value) | 
|  | 175 | { | 
|  | 176 | if (reader->entMax <= 0) { | 
|  | 177 | reader->entMax = 10; | 
|  | 178 | reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax * | 
|  | 179 | sizeof(reader->entTab[0])); | 
|  | 180 | if (reader->entTab == NULL) { | 
|  | 181 | xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n"); | 
|  | 182 | return (0); | 
|  | 183 | } | 
|  | 184 | } | 
|  | 185 | if (reader->entNr >= reader->entMax) { | 
|  | 186 | reader->entMax *= 2; | 
|  | 187 | reader->entTab = | 
|  | 188 | (xmlNodePtr *) xmlRealloc(reader->entTab, | 
|  | 189 | reader->entMax * | 
|  | 190 | sizeof(reader->entTab[0])); | 
|  | 191 | if (reader->entTab == NULL) { | 
|  | 192 | xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n"); | 
|  | 193 | return (0); | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 | reader->entTab[reader->entNr] = value; | 
|  | 197 | reader->ent = value; | 
|  | 198 | return (reader->entNr++); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | /** | 
|  | 202 | * xmlTextReaderEntPop: | 
|  | 203 | * @reader:  the xmlTextReaderPtr used | 
|  | 204 | * | 
|  | 205 | * Pops the top element entity from the entities stack | 
|  | 206 | * | 
|  | 207 | * Returns the entity just removed | 
|  | 208 | */ | 
|  | 209 | static xmlNodePtr | 
|  | 210 | xmlTextReaderEntPop(xmlTextReaderPtr reader) | 
|  | 211 | { | 
|  | 212 | xmlNodePtr ret; | 
|  | 213 |  | 
|  | 214 | if (reader->entNr <= 0) | 
|  | 215 | return (0); | 
|  | 216 | reader->entNr--; | 
|  | 217 | if (reader->entNr > 0) | 
|  | 218 | reader->ent = reader->entTab[reader->entNr - 1]; | 
|  | 219 | else | 
|  | 220 | reader->ent = NULL; | 
|  | 221 | ret = reader->entTab[reader->entNr]; | 
|  | 222 | reader->entTab[reader->entNr] = 0; | 
|  | 223 | return (ret); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | /** | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 227 | * xmlTextReaderStartElement: | 
|  | 228 | * @ctx: the user data (XML parser context) | 
|  | 229 | * @fullname:  The element name, including namespace prefix | 
|  | 230 | * @atts:  An array of name/value attributes pairs, NULL terminated | 
|  | 231 | * | 
|  | 232 | * called when an opening tag has been processed. | 
|  | 233 | */ | 
|  | 234 | static void | 
|  | 235 | xmlTextReaderStartElement(void *ctx, const xmlChar *fullname, | 
|  | 236 | const xmlChar **atts) { | 
|  | 237 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; | 
| Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 238 | xmlParserCtxtPtr origctxt; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 239 | xmlTextReaderPtr reader = ctxt->_private; | 
|  | 240 |  | 
|  | 241 | #ifdef DEBUG_CALLBACKS | 
|  | 242 | printf("xmlTextReaderStartElement(%s)\n", fullname); | 
|  | 243 | #endif | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 244 | if ((reader != NULL) && (reader->startElement != NULL)) { | 
| Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 245 | /* | 
|  | 246 | * when processing an entity, the context may have been changed | 
|  | 247 | */ | 
|  | 248 | origctxt = reader->ctxt; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 249 | reader->startElement(ctx, fullname, atts); | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 250 | if ((ctxt->node != NULL) && (ctxt->input != NULL) && | 
|  | 251 | (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') && | 
|  | 252 | (ctxt->input->cur[1] == '>')) | 
|  | 253 | ctxt->node->_private = (void *) xmlTextReaderIsEmpty; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 254 | } | 
| Daniel Veillard | 9e395c2 | 2003-01-01 14:50:44 +0000 | [diff] [blame] | 255 | if (reader != NULL) | 
|  | 256 | reader->state = XML_TEXTREADER_ELEMENT; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
|  | 259 | /** | 
|  | 260 | * xmlTextReaderEndElement: | 
|  | 261 | * @ctx: the user data (XML parser context) | 
|  | 262 | * @fullname:  The element name, including namespace prefix | 
|  | 263 | * | 
|  | 264 | * called when an ending tag has been processed. | 
|  | 265 | */ | 
|  | 266 | static void | 
|  | 267 | xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) { | 
|  | 268 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; | 
| Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 269 | xmlParserCtxtPtr origctxt; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 270 | xmlTextReaderPtr reader = ctxt->_private; | 
|  | 271 |  | 
|  | 272 | #ifdef DEBUG_CALLBACKS | 
|  | 273 | printf("xmlTextReaderEndElement(%s)\n", fullname); | 
|  | 274 | #endif | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 275 | if ((reader != NULL) && (reader->endElement != NULL)) { | 
| Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 276 | /* | 
|  | 277 | * when processing an entity, the context may have been changed | 
|  | 278 | */ | 
|  | 279 | origctxt = reader->ctxt; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 280 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 281 | reader->endElement(ctx, fullname); | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 282 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 283 | } | 
|  | 284 |  | 
|  | 285 | /** | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 286 | * xmlTextReaderCharacters: | 
|  | 287 | * @ctx: the user data (XML parser context) | 
|  | 288 | * @ch:  a xmlChar string | 
|  | 289 | * @len: the number of xmlChar | 
|  | 290 | * | 
|  | 291 | * receiving some chars from the parser. | 
|  | 292 | */ | 
|  | 293 | static void | 
|  | 294 | xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len) | 
|  | 295 | { | 
|  | 296 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; | 
| Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 297 | xmlParserCtxtPtr origctxt; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 298 | xmlTextReaderPtr reader = ctxt->_private; | 
|  | 299 |  | 
|  | 300 | #ifdef DEBUG_CALLBACKS | 
|  | 301 | printf("xmlTextReaderCharacters()\n"); | 
|  | 302 | #endif | 
|  | 303 | if ((reader != NULL) && (reader->characters != NULL)) { | 
|  | 304 | reader->characters(ctx, ch, len); | 
| Daniel Veillard | d589614 | 2002-12-31 14:45:26 +0000 | [diff] [blame] | 305 | /* | 
|  | 306 | * when processing an entity, the context may have been changed | 
|  | 307 | */ | 
|  | 308 | origctxt = reader->ctxt; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 309 | } | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | /** | 
|  | 313 | * xmlTextReaderCDataBlock: | 
|  | 314 | * @ctx: the user data (XML parser context) | 
|  | 315 | * @value:  The pcdata content | 
|  | 316 | * @len:  the block length | 
|  | 317 | * | 
|  | 318 | * called when a pcdata block has been parsed | 
|  | 319 | */ | 
|  | 320 | static void | 
|  | 321 | xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len) | 
|  | 322 | { | 
|  | 323 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; | 
|  | 324 | xmlTextReaderPtr reader = ctxt->_private; | 
|  | 325 |  | 
|  | 326 | #ifdef DEBUG_CALLBACKS | 
|  | 327 | printf("xmlTextReaderCDataBlock()\n"); | 
|  | 328 | #endif | 
|  | 329 | if ((reader != NULL) && (reader->cdataBlock != NULL)) { | 
|  | 330 | reader->cdataBlock(ctx, ch, len); | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 331 | } | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | /** | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 335 | * xmlTextReaderPushData: | 
|  | 336 | * @reader:  the xmlTextReaderPtr used | 
|  | 337 | * | 
|  | 338 | * Push data down the progressive parser until a significant callback | 
|  | 339 | * got raised. | 
|  | 340 | * | 
|  | 341 | * Returns -1 in case of failure, 0 otherwise | 
|  | 342 | */ | 
|  | 343 | static int | 
|  | 344 | xmlTextReaderPushData(xmlTextReaderPtr reader) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 345 | xmlBufferPtr inbuf; | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 346 | int val, s; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 347 | int oldstate; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 348 |  | 
|  | 349 | if ((reader->input == NULL) || (reader->input->buffer == NULL)) | 
|  | 350 | return(-1); | 
|  | 351 |  | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 352 | oldstate = reader->state; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 353 | reader->state = XML_TEXTREADER_NONE; | 
|  | 354 | inbuf = reader->input->buffer; | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 355 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 356 | while (reader->state == XML_TEXTREADER_NONE) { | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 357 | if (inbuf->use < reader->cur + CHUNK_SIZE) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 358 | /* | 
|  | 359 | * Refill the buffer unless we are at the end of the stream | 
|  | 360 | */ | 
|  | 361 | if (reader->mode != XML_TEXTREADER_MODE_EOF) { | 
|  | 362 | val = xmlParserInputBufferRead(reader->input, 4096); | 
|  | 363 | if (val <= 0) { | 
|  | 364 | reader->mode = XML_TEXTREADER_MODE_EOF; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 365 | reader->state = oldstate; | 
| Daniel Veillard | aaa105b | 2002-12-30 11:42:17 +0000 | [diff] [blame] | 366 | if ((oldstate != XML_TEXTREADER_START) || | 
|  | 367 | (reader->ctxt->myDoc != NULL)) | 
|  | 368 | return(val); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 369 | } | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 370 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 371 | } else | 
|  | 372 | break; | 
|  | 373 | } | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 374 | /* | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 375 | * parse by block of CHUNK_SIZE bytes, various tests show that | 
|  | 376 | * it's the best tradeoff at least on a 1.2GH Duron | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 377 | */ | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 378 | if (inbuf->use >= reader->cur + CHUNK_SIZE) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 379 | val = xmlParseChunk(reader->ctxt, | 
|  | 380 | (const char *) &inbuf->content[reader->cur], | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 381 | CHUNK_SIZE, 0); | 
|  | 382 | reader->cur += CHUNK_SIZE; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 383 | if (val != 0) | 
|  | 384 | return(-1); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 385 | } else { | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 386 | s = inbuf->use - reader->cur; | 
|  | 387 | val = xmlParseChunk(reader->ctxt, | 
|  | 388 | (const char *) &inbuf->content[reader->cur], | 
|  | 389 | s, 0); | 
|  | 390 | reader->cur += s; | 
|  | 391 | if (val != 0) | 
|  | 392 | return(-1); | 
|  | 393 | break; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 394 | } | 
|  | 395 | } | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 396 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 397 | /* | 
|  | 398 | * Discard the consumed input when needed and possible | 
|  | 399 | */ | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 400 | if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) { | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 401 | if (reader->cur >= 4096) { | 
|  | 402 | val = xmlBufferShrink(inbuf, reader->cur); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 403 | if (val >= 0) { | 
|  | 404 | reader->cur -= val; | 
|  | 405 | } | 
|  | 406 | } | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | /* | 
|  | 410 | * At the end of the stream signal that the work is done to the Push | 
|  | 411 | * parser. | 
|  | 412 | */ | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 413 | else if (reader->mode == XML_TEXTREADER_MODE_EOF) { | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 414 | if (reader->mode != XML_TEXTREADER_DONE) { | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 415 | s = inbuf->use - reader->cur; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 416 | val = xmlParseChunk(reader->ctxt, | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 417 | (const char *) &inbuf->content[reader->cur], | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 418 | s, 1); | 
|  | 419 | reader->cur = inbuf->use; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 420 | reader->mode = XML_TEXTREADER_DONE; | 
| Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 421 | if (val != 0) return(-1); | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 422 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 423 | } | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 424 | reader->state = oldstate; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 425 | return(0); | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | /** | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 429 | * xmlTextReaderValidatePush: | 
|  | 430 | * @reader:  the xmlTextReaderPtr used | 
|  | 431 | * | 
|  | 432 | * Push the current node for validation | 
|  | 433 | */ | 
|  | 434 | static void | 
|  | 435 | xmlTextReaderValidatePush(xmlTextReaderPtr reader) { | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 436 | #ifdef LIBXML_REGEXP_ENABLED | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 437 | xmlNodePtr node = reader->node; | 
|  | 438 |  | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 439 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && | 
|  | 440 | (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) { | 
|  | 441 | if ((node->ns == NULL) || (node->ns->prefix == NULL)) { | 
|  | 442 | reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt, | 
|  | 443 | reader->ctxt->myDoc, node, node->name); | 
|  | 444 | } else { | 
|  | 445 | /* TODO use the BuildQName interface */ | 
|  | 446 | xmlChar *qname; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 447 |  | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 448 | qname = xmlStrdup(node->ns->prefix); | 
|  | 449 | qname = xmlStrcat(qname, BAD_CAST ":"); | 
|  | 450 | qname = xmlStrcat(qname, node->name); | 
|  | 451 | reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt, | 
|  | 452 | reader->ctxt->myDoc, node, qname); | 
|  | 453 | if (qname != NULL) | 
|  | 454 | xmlFree(qname); | 
|  | 455 | } | 
|  | 456 | #ifdef LIBXML_SCHEMAS_ENABLED | 
|  | 457 | } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) && | 
|  | 458 | (reader->rngValidCtxt != NULL)) { | 
|  | 459 | int ret; | 
|  | 460 |  | 
|  | 461 | if (reader->rngFullNode != NULL) return; | 
|  | 462 | ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt, | 
|  | 463 | reader->ctxt->myDoc, | 
|  | 464 | node); | 
|  | 465 | if (ret == 0) { | 
|  | 466 | /* | 
|  | 467 | * this element requires a full tree | 
|  | 468 | */ | 
|  | 469 | node = xmlTextReaderExpand(reader); | 
|  | 470 | if (node == NULL) { | 
|  | 471 | printf("Expand failed !\n"); | 
|  | 472 | ret = -1; | 
|  | 473 | } else { | 
|  | 474 | ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt, | 
|  | 475 | reader->ctxt->myDoc, | 
|  | 476 | node); | 
|  | 477 | reader->rngFullNode = node; | 
|  | 478 | } | 
|  | 479 | } | 
|  | 480 | if (ret != 1) | 
|  | 481 | reader->rngValidErrors++; | 
|  | 482 | #endif | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 483 | } | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 484 | #endif /* LIBXML_REGEXP_ENABLED */ | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 485 | } | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 486 |  | 
|  | 487 | /** | 
|  | 488 | * xmlTextReaderValidateCData: | 
|  | 489 | * @reader:  the xmlTextReaderPtr used | 
|  | 490 | * @data:  pointer to the CData | 
|  | 491 | * @len:  lenght of the CData block in bytes. | 
|  | 492 | * | 
|  | 493 | * Push some CData for validation | 
|  | 494 | */ | 
|  | 495 | static void | 
|  | 496 | xmlTextReaderValidateCData(xmlTextReaderPtr reader, | 
|  | 497 | const xmlChar *data, int len) { | 
|  | 498 | #ifdef LIBXML_REGEXP_ENABLED | 
|  | 499 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && | 
|  | 500 | (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) { | 
|  | 501 | reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt, | 
|  | 502 | data, len); | 
|  | 503 | #ifdef LIBXML_SCHEMAS_ENABLED | 
|  | 504 | } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) && | 
|  | 505 | (reader->rngValidCtxt != NULL)) { | 
|  | 506 | int ret; | 
|  | 507 |  | 
|  | 508 | if (reader->rngFullNode != NULL) return; | 
|  | 509 | ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len); | 
|  | 510 | if (ret != 1) | 
|  | 511 | reader->rngValidErrors++; | 
|  | 512 | #endif | 
|  | 513 | } | 
|  | 514 | #endif /* LIBXML_REGEXP_ENABLED */ | 
|  | 515 | } | 
|  | 516 |  | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 517 | /** | 
|  | 518 | * xmlTextReaderValidatePop: | 
|  | 519 | * @reader:  the xmlTextReaderPtr used | 
|  | 520 | * | 
|  | 521 | * Pop the current node from validation | 
|  | 522 | */ | 
|  | 523 | static void | 
|  | 524 | xmlTextReaderValidatePop(xmlTextReaderPtr reader) { | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 525 | #ifdef LIBXML_REGEXP_ENABLED | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 526 | xmlNodePtr node = reader->node; | 
|  | 527 |  | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 528 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && | 
|  | 529 | (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) { | 
|  | 530 | if ((node->ns == NULL) || (node->ns->prefix == NULL)) { | 
|  | 531 | reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt, | 
|  | 532 | reader->ctxt->myDoc, node, node->name); | 
|  | 533 | } else { | 
|  | 534 | /* TODO use the BuildQName interface */ | 
|  | 535 | xmlChar *qname; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 536 |  | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 537 | qname = xmlStrdup(node->ns->prefix); | 
|  | 538 | qname = xmlStrcat(qname, BAD_CAST ":"); | 
|  | 539 | qname = xmlStrcat(qname, node->name); | 
|  | 540 | reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt, | 
|  | 541 | reader->ctxt->myDoc, node, qname); | 
|  | 542 | if (qname != NULL) | 
|  | 543 | xmlFree(qname); | 
|  | 544 | } | 
|  | 545 | #ifdef LIBXML_SCHEMAS_ENABLED | 
|  | 546 | } else if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) && | 
|  | 547 | (reader->rngValidCtxt != NULL)) { | 
|  | 548 | int ret; | 
|  | 549 |  | 
|  | 550 | if (reader->rngFullNode != NULL) { | 
|  | 551 | if (node == reader->rngFullNode) | 
|  | 552 | reader->rngFullNode = NULL; | 
|  | 553 | return; | 
|  | 554 | } | 
|  | 555 | ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt, | 
|  | 556 | reader->ctxt->myDoc, | 
|  | 557 | node); | 
|  | 558 | if (ret != 1) | 
|  | 559 | reader->rngValidErrors++; | 
|  | 560 | #endif | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 561 | } | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 562 | #endif /* LIBXML_REGEXP_ENABLED */ | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 563 | } | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 564 | /** | 
|  | 565 | * xmlTextReaderValidateEntity: | 
|  | 566 | * @reader:  the xmlTextReaderPtr used | 
|  | 567 | * | 
|  | 568 | * Handle the validation when an entity reference is encountered and | 
|  | 569 | * entity substitution is not activated. As a result the parser interface | 
|  | 570 | * must walk through the entity and do the validation calls | 
|  | 571 | */ | 
|  | 572 | static void | 
|  | 573 | xmlTextReaderValidateEntity(xmlTextReaderPtr reader) { | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 574 | #ifdef LIBXML_REGEXP_ENABLED | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 575 | xmlNodePtr oldnode = reader->node; | 
|  | 576 | xmlNodePtr node = reader->node; | 
|  | 577 | xmlParserCtxtPtr ctxt = reader->ctxt; | 
|  | 578 |  | 
|  | 579 | do { | 
|  | 580 | if (node->type == XML_ENTITY_REF_NODE) { | 
|  | 581 | /* | 
|  | 582 | * Case where the underlying tree is not availble, lookup the entity | 
|  | 583 | * and walk it. | 
|  | 584 | */ | 
|  | 585 | if ((node->children == NULL) && (ctxt->sax != NULL) && | 
|  | 586 | (ctxt->sax->getEntity != NULL)) { | 
|  | 587 | node->children = (xmlNodePtr) | 
|  | 588 | ctxt->sax->getEntity(ctxt, node->name); | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | if ((node->children != NULL) && | 
|  | 592 | (node->children->type == XML_ENTITY_DECL) && | 
|  | 593 | (node->children->children != NULL)) { | 
|  | 594 | xmlTextReaderEntPush(reader, node); | 
|  | 595 | node = node->children->children; | 
|  | 596 | continue; | 
|  | 597 | } else { | 
|  | 598 | /* | 
|  | 599 | * The error has probably be raised already. | 
|  | 600 | */ | 
|  | 601 | if (node == oldnode) | 
|  | 602 | break; | 
|  | 603 | node = node->next; | 
|  | 604 | } | 
|  | 605 | } else if (node->type == XML_ELEMENT_NODE) { | 
|  | 606 | reader->node = node; | 
|  | 607 | xmlTextReaderValidatePush(reader); | 
|  | 608 | } else if ((node->type == XML_TEXT_NODE) || | 
|  | 609 | (node->type == XML_CDATA_SECTION_NODE)) { | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 610 | xmlTextReaderValidateCData(reader, node->content, | 
|  | 611 | xmlStrlen(node->content)); | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 612 | } | 
|  | 613 |  | 
|  | 614 | /* | 
|  | 615 | * go to next node | 
|  | 616 | */ | 
|  | 617 | if (node->children != NULL) { | 
|  | 618 | node = node->children; | 
|  | 619 | continue; | 
| Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 620 | } else if (node->type == XML_ELEMENT_NODE) { | 
|  | 621 | xmlTextReaderValidatePop(reader); | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 622 | } | 
|  | 623 | if (node->next != NULL) { | 
|  | 624 | node = node->next; | 
|  | 625 | continue; | 
|  | 626 | } | 
|  | 627 | do { | 
|  | 628 | node = node->parent; | 
|  | 629 | if (node->type == XML_ELEMENT_NODE) { | 
|  | 630 | reader->node = node; | 
|  | 631 | xmlTextReaderValidatePop(reader); | 
|  | 632 | } | 
|  | 633 | if ((node->type == XML_ENTITY_DECL) && | 
|  | 634 | (reader->ent != NULL) && (reader->ent->children == node)) { | 
|  | 635 | node = xmlTextReaderEntPop(reader); | 
|  | 636 | } | 
|  | 637 | if (node == oldnode) | 
|  | 638 | break; | 
|  | 639 | if (node->next != NULL) { | 
|  | 640 | node = node->next; | 
|  | 641 | break; | 
|  | 642 | } | 
|  | 643 | } while ((node != NULL) && (node != oldnode)); | 
|  | 644 | } while ((node != NULL) && (node != oldnode)); | 
|  | 645 | reader->node = oldnode; | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 646 | #endif /* LIBXML_REGEXP_ENABLED */ | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 647 | } | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 648 |  | 
|  | 649 |  | 
|  | 650 | /** | 
| Daniel Veillard | c6cae7b | 2003-04-11 09:02:11 +0000 | [diff] [blame] | 651 | * xmlTextReaderGetSuccessor: | 
|  | 652 | * @cur:  the current node | 
|  | 653 | * | 
|  | 654 | * Get the successor of a node if available. | 
|  | 655 | * | 
|  | 656 | * Returns the successor node or NULL | 
|  | 657 | */ | 
|  | 658 | static xmlNodePtr | 
|  | 659 | xmlTextReaderGetSuccessor(xmlNodePtr cur) { | 
|  | 660 | if (cur == NULL) return(NULL) ; /* ERROR */ | 
|  | 661 | if (cur->next != NULL) return(cur->next) ; | 
|  | 662 | do { | 
|  | 663 | cur = cur->parent; | 
|  | 664 | if (cur == NULL) return(NULL); | 
|  | 665 | if (cur->next != NULL) return(cur->next); | 
|  | 666 | } while (cur != NULL); | 
|  | 667 | return(cur); | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | /** | 
|  | 671 | * xmlTextReaderDoExpand: | 
|  | 672 | * @reader:  the xmlTextReaderPtr used | 
|  | 673 | * | 
|  | 674 | * Makes sure that the current node is fully read as well as all its | 
|  | 675 | * descendant. It means the full DOM subtree must be available at the | 
|  | 676 | * end of the call. | 
|  | 677 | * | 
|  | 678 | * Returns 1 if the node was expanded successfully, 0 if there is no more | 
|  | 679 | *          nodes to read, or -1 in case of error | 
|  | 680 | */ | 
|  | 681 | static int | 
|  | 682 | xmlTextReaderDoExpand(xmlTextReaderPtr reader) { | 
|  | 683 | int val; | 
|  | 684 |  | 
|  | 685 | if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL)) | 
|  | 686 | return(-1); | 
|  | 687 |  | 
|  | 688 | do { | 
|  | 689 | if (xmlTextReaderGetSuccessor(reader->node) != NULL) | 
|  | 690 | return(1); | 
| Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame^] | 691 | if (reader->ctxt->nodeNr <= reader->depth) | 
|  | 692 | return(1); | 
| Daniel Veillard | c6cae7b | 2003-04-11 09:02:11 +0000 | [diff] [blame] | 693 | if (reader->mode == XML_TEXTREADER_MODE_EOF) | 
|  | 694 | return(1); | 
|  | 695 | val = xmlTextReaderPushData(reader); | 
|  | 696 | if (val < 0) | 
|  | 697 | return(-1); | 
|  | 698 | } while(reader->mode != XML_TEXTREADER_MODE_EOF); | 
|  | 699 | return(1); | 
|  | 700 | } | 
|  | 701 |  | 
|  | 702 | /** | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 703 | * xmlTextReaderRead: | 
|  | 704 | * @reader:  the xmlTextReaderPtr used | 
|  | 705 | * | 
|  | 706 | *  Moves the position of the current instance to the next node in | 
|  | 707 | *  the stream, exposing its properties. | 
|  | 708 | * | 
|  | 709 | *  Returns 1 if the node was read successfully, 0 if there is no more | 
|  | 710 | *          nodes to read, or -1 in case of error | 
|  | 711 | */ | 
|  | 712 | int | 
|  | 713 | xmlTextReaderRead(xmlTextReaderPtr reader) { | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 714 | int val, olddepth = 0; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 715 | xmlTextReaderState oldstate = 0; | 
|  | 716 | xmlNodePtr oldnode = NULL; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 717 |  | 
|  | 718 | if ((reader == NULL) || (reader->ctxt == NULL)) | 
|  | 719 | return(-1); | 
|  | 720 | if (reader->ctxt->wellFormed != 1) | 
|  | 721 | return(-1); | 
|  | 722 |  | 
|  | 723 | #ifdef DEBUG_READER | 
|  | 724 | fprintf(stderr, "\nREAD "); | 
|  | 725 | DUMP_READER | 
|  | 726 | #endif | 
| Daniel Veillard | 29b3e28 | 2002-12-29 11:14:41 +0000 | [diff] [blame] | 727 | reader->curnode = NULL; | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 728 | if (reader->mode == XML_TEXTREADER_MODE_INITIAL) { | 
|  | 729 | reader->mode = XML_TEXTREADER_MODE_INTERACTIVE; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 730 | /* | 
|  | 731 | * Initial state | 
|  | 732 | */ | 
|  | 733 | do { | 
|  | 734 | val = xmlTextReaderPushData(reader); | 
|  | 735 | if (val < 0) | 
|  | 736 | return(-1); | 
|  | 737 | } while ((reader->ctxt->node == NULL) && | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 738 | ((reader->mode != XML_TEXTREADER_MODE_EOF) && | 
|  | 739 | (reader->mode != XML_TEXTREADER_DONE))); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 740 | if (reader->ctxt->node == NULL) { | 
| Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 741 | if (reader->ctxt->myDoc != NULL) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 742 | reader->node = reader->ctxt->myDoc->children; | 
| Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 743 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 744 | if (reader->node == NULL) | 
|  | 745 | return(-1); | 
| Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 746 | reader->state = XML_TEXTREADER_ELEMENT; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 747 | } else { | 
| Daniel Veillard | 48ef4c9 | 2003-03-22 12:38:15 +0000 | [diff] [blame] | 748 | if (reader->ctxt->myDoc != NULL) { | 
|  | 749 | reader->node = reader->ctxt->myDoc->children; | 
|  | 750 | } | 
|  | 751 | if (reader->node == NULL) | 
|  | 752 | reader->node = reader->ctxt->nodeTab[0]; | 
| Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 753 | reader->state = XML_TEXTREADER_ELEMENT; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 754 | } | 
| Daniel Veillard | 4d8db8a | 2002-12-30 18:40:42 +0000 | [diff] [blame] | 755 | reader->depth = 0; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 756 | goto node_found; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 757 | } | 
|  | 758 | oldstate = reader->state; | 
|  | 759 | olddepth = reader->ctxt->nodeNr; | 
|  | 760 | oldnode = reader->node; | 
| Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 761 |  | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 762 | get_next_node: | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 763 | /* | 
|  | 764 | * If we are not backtracking on ancestors or examined nodes, | 
|  | 765 | * that the parser didn't finished or that we arent at the end | 
|  | 766 | * of stream, continue processing. | 
|  | 767 | */ | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 768 | while ((reader->node->next == NULL) && | 
|  | 769 | (reader->ctxt->nodeNr == olddepth) && | 
|  | 770 | ((oldstate == XML_TEXTREADER_BACKTRACK) || | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 771 | (reader->node->children == NULL) || | 
|  | 772 | (reader->node->type == XML_ENTITY_REF_NODE) || | 
| Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 773 | (reader->node->type == XML_DTD_NODE) || | 
|  | 774 | (reader->node->type == XML_DOCUMENT_NODE) || | 
|  | 775 | (reader->node->type == XML_HTML_DOCUMENT_NODE)) && | 
| Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 776 | ((reader->ctxt->node == NULL) || | 
|  | 777 | (reader->ctxt->node == reader->node) || | 
|  | 778 | (reader->ctxt->node == reader->node->parent)) && | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 779 | (reader->ctxt->instate != XML_PARSER_EOF)) { | 
|  | 780 | val = xmlTextReaderPushData(reader); | 
|  | 781 | if (val < 0) | 
|  | 782 | return(-1); | 
|  | 783 | if (reader->node == NULL) | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 784 | goto node_end; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 785 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 786 | if (oldstate != XML_TEXTREADER_BACKTRACK) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 787 | if ((reader->node->children != NULL) && | 
|  | 788 | (reader->node->type != XML_ENTITY_REF_NODE) && | 
|  | 789 | (reader->node->type != XML_DTD_NODE)) { | 
|  | 790 | reader->node = reader->node->children; | 
|  | 791 | reader->depth++; | 
| Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 792 | reader->state = XML_TEXTREADER_ELEMENT; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 793 | goto node_found; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 794 | } | 
|  | 795 | } | 
|  | 796 | if (reader->node->next != NULL) { | 
|  | 797 | if ((oldstate == XML_TEXTREADER_ELEMENT) && | 
| Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 798 | (reader->node->type == XML_ELEMENT_NODE) && | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 799 | (reader->node->children == NULL) && | 
|  | 800 | (reader->node->_private != (void *)xmlTextReaderIsEmpty)) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 801 | reader->state = XML_TEXTREADER_END; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 802 | goto node_found; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 803 | } | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 804 | if ((reader->validate) && | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 805 | (reader->node->type == XML_ELEMENT_NODE)) | 
|  | 806 | xmlTextReaderValidatePop(reader); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 807 | reader->node = reader->node->next; | 
|  | 808 | reader->state = XML_TEXTREADER_ELEMENT; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 809 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 810 | /* | 
|  | 811 | * Cleanup of the old node | 
|  | 812 | */ | 
| Daniel Veillard | 4dbe77a | 2003-01-14 00:17:42 +0000 | [diff] [blame] | 813 | if ((reader->node->prev != NULL) && | 
|  | 814 | (reader->node->prev->type != XML_DTD_NODE)) { | 
|  | 815 | xmlNodePtr tmp = reader->node->prev; | 
|  | 816 | xmlUnlinkNode(tmp); | 
|  | 817 | xmlFreeNode(tmp); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 818 | } | 
|  | 819 |  | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 820 | goto node_found; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 821 | } | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 822 | if ((oldstate == XML_TEXTREADER_ELEMENT) && | 
| Daniel Veillard | 571b889 | 2002-12-30 12:37:59 +0000 | [diff] [blame] | 823 | (reader->node->type == XML_ELEMENT_NODE) && | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 824 | (reader->node->children == NULL) && | 
|  | 825 | (reader->node->_private != (void *)xmlTextReaderIsEmpty)) { | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 826 | reader->state = XML_TEXTREADER_END; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 827 | goto node_found; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 828 | } | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 829 | if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE)) | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 830 | xmlTextReaderValidatePop(reader); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 831 | reader->node = reader->node->parent; | 
|  | 832 | if ((reader->node == NULL) || | 
|  | 833 | (reader->node->type == XML_DOCUMENT_NODE) || | 
|  | 834 | #ifdef LIBXML_DOCB_ENABLED | 
|  | 835 | (reader->node->type == XML_DOCB_DOCUMENT_NODE) || | 
|  | 836 | #endif | 
|  | 837 | (reader->node->type == XML_HTML_DOCUMENT_NODE)) { | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 838 | if (reader->mode != XML_TEXTREADER_DONE) { | 
|  | 839 | val = xmlParseChunk(reader->ctxt, "", 0, 1); | 
|  | 840 | reader->mode = XML_TEXTREADER_DONE; | 
|  | 841 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 842 | reader->node = NULL; | 
| Daniel Veillard | 4d8db8a | 2002-12-30 18:40:42 +0000 | [diff] [blame] | 843 | reader->depth = -1; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 844 |  | 
|  | 845 | /* | 
|  | 846 | * Cleanup of the old node | 
|  | 847 | */ | 
|  | 848 | if (oldnode->type != XML_DTD_NODE) { | 
|  | 849 | xmlUnlinkNode(oldnode); | 
|  | 850 | xmlFreeNode(oldnode); | 
|  | 851 | } | 
|  | 852 |  | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 853 | goto node_end; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 854 | } | 
|  | 855 | reader->depth--; | 
|  | 856 | reader->state = XML_TEXTREADER_BACKTRACK; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 857 |  | 
|  | 858 | node_found: | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 859 | DUMP_READER | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 860 |  | 
|  | 861 | /* | 
| Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 862 | * If we are in the middle of a piece of CDATA make sure it's finished | 
|  | 863 | */ | 
|  | 864 | if ((reader->node != NULL) && | 
|  | 865 | ((reader->node->type == XML_TEXT_NODE) || | 
|  | 866 | (reader->node->type == XML_CDATA_SECTION_NODE))) { | 
|  | 867 | xmlTextReaderExpand(reader); | 
|  | 868 | } | 
|  | 869 |  | 
|  | 870 | /* | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 871 | * Handle entities enter and exit when in entity replacement mode | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 872 | */ | 
|  | 873 | if ((reader->node != NULL) && | 
|  | 874 | (reader->node->type == XML_ENTITY_REF_NODE) && | 
|  | 875 | (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) { | 
|  | 876 | /* | 
|  | 877 | * Case where the underlying tree is not availble, lookup the entity | 
|  | 878 | * and walk it. | 
|  | 879 | */ | 
|  | 880 | if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) && | 
|  | 881 | (reader->ctxt->sax->getEntity != NULL)) { | 
|  | 882 | reader->node->children = (xmlNodePtr) | 
|  | 883 | reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name); | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | if ((reader->node->children != NULL) && | 
|  | 887 | (reader->node->children->type == XML_ENTITY_DECL) && | 
|  | 888 | (reader->node->children->children != NULL)) { | 
|  | 889 | xmlTextReaderEntPush(reader, reader->node); | 
|  | 890 | reader->node = reader->node->children->children; | 
|  | 891 | } | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 892 | } else if ((reader->node != NULL) && | 
|  | 893 | (reader->node->type == XML_ENTITY_REF_NODE) && | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 894 | (reader->ctxt != NULL) && (reader->validate)) { | 
| Daniel Veillard | a80ff6e | 2003-01-03 12:52:08 +0000 | [diff] [blame] | 895 | xmlTextReaderValidateEntity(reader); | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 896 | } | 
|  | 897 | if ((reader->node != NULL) && | 
|  | 898 | (reader->node->type == XML_ENTITY_DECL) && | 
|  | 899 | (reader->ent != NULL) && (reader->ent->children == reader->node)) { | 
|  | 900 | reader->node = xmlTextReaderEntPop(reader); | 
|  | 901 | reader->depth++; | 
|  | 902 | goto get_next_node; | 
|  | 903 | } | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 904 | #ifdef LIBXML_REGEXP_ENABLED | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 905 | if ((reader->validate) && (reader->node != NULL)) { | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 906 | xmlNodePtr node = reader->node; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 907 |  | 
|  | 908 | if ((node->type == XML_ELEMENT_NODE) && | 
|  | 909 | ((reader->state != XML_TEXTREADER_END) && | 
|  | 910 | (reader->state != XML_TEXTREADER_BACKTRACK))) { | 
|  | 911 | xmlTextReaderValidatePush(reader); | 
|  | 912 | } else if ((node->type == XML_TEXT_NODE) || | 
|  | 913 | (node->type == XML_CDATA_SECTION_NODE)) { | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 914 | xmlTextReaderValidateCData(reader, node->content, | 
|  | 915 | xmlStrlen(node->content)); | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 916 | } | 
|  | 917 | } | 
| Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 918 | #endif /* LIBXML_REGEXP_ENABLED */ | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 919 | return(1); | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 920 | node_end: | 
| Daniel Veillard | c6cae7b | 2003-04-11 09:02:11 +0000 | [diff] [blame] | 921 | reader->mode = XML_TEXTREADER_DONE; | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 922 | return(0); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 923 | } | 
|  | 924 |  | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 925 | /** | 
|  | 926 | * xmlTextReaderReadState: | 
|  | 927 | * @reader:  the xmlTextReaderPtr used | 
|  | 928 | * | 
|  | 929 | * Gets the read state of the reader. | 
|  | 930 | * | 
|  | 931 | * Returns the state value, or -1 in case of error | 
|  | 932 | */ | 
|  | 933 | int | 
|  | 934 | xmlTextReaderReadState(xmlTextReaderPtr reader) { | 
|  | 935 | if (reader == NULL) | 
|  | 936 | return(-1); | 
|  | 937 | return(reader->mode); | 
|  | 938 | } | 
|  | 939 |  | 
|  | 940 | /** | 
| Daniel Veillard | c6cae7b | 2003-04-11 09:02:11 +0000 | [diff] [blame] | 941 | * xmlTextReaderExpand: | 
|  | 942 | * @reader:  the xmlTextReaderPtr used | 
|  | 943 | * | 
|  | 944 | * 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] | 945 | * the subtree available until the next xmlTextReaderRead() call | 
| Daniel Veillard | c6cae7b | 2003-04-11 09:02:11 +0000 | [diff] [blame] | 946 | * | 
|  | 947 | * Returns a node pointer valid until the next xmlTextReaderRead() call | 
|  | 948 | *         or NULL in case of error. | 
|  | 949 | */ | 
|  | 950 | xmlNodePtr | 
|  | 951 | xmlTextReaderExpand(xmlTextReaderPtr reader) { | 
|  | 952 | if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL)) | 
|  | 953 | return(NULL); | 
|  | 954 | if (xmlTextReaderDoExpand(reader) < 0) | 
|  | 955 | return(NULL); | 
|  | 956 | return(reader->node); | 
|  | 957 | } | 
|  | 958 |  | 
|  | 959 | /** | 
|  | 960 | * xmlTextReaderNext: | 
|  | 961 | * @reader:  the xmlTextReaderPtr used | 
|  | 962 | * | 
|  | 963 | * Skip to the node following the current one in document order while | 
|  | 964 | * avoiding the subtree if any. | 
|  | 965 | * | 
|  | 966 | * Returns 1 if the node was read successfully, 0 if there is no more | 
|  | 967 | *          nodes to read, or -1 in case of error | 
|  | 968 | */ | 
|  | 969 | int | 
|  | 970 | xmlTextReaderNext(xmlTextReaderPtr reader) { | 
|  | 971 | int ret; | 
|  | 972 | xmlNodePtr cur; | 
|  | 973 |  | 
|  | 974 | if (reader == NULL) | 
|  | 975 | return(-1); | 
|  | 976 | cur = reader->node; | 
|  | 977 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) | 
|  | 978 | return(xmlTextReaderRead(reader)); | 
|  | 979 | if (reader->state == XML_TEXTREADER_END) | 
|  | 980 | return(xmlTextReaderRead(reader)); | 
|  | 981 | if (cur->_private == (void *)xmlTextReaderIsEmpty) | 
|  | 982 | return(xmlTextReaderRead(reader)); | 
|  | 983 | do { | 
|  | 984 | ret = xmlTextReaderRead(reader); | 
|  | 985 | if (ret != 1) | 
|  | 986 | return(ret); | 
|  | 987 | } while (reader->node != cur); | 
|  | 988 | return(xmlTextReaderRead(reader)); | 
|  | 989 | } | 
|  | 990 |  | 
|  | 991 | /** | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 992 | * xmlTextReaderReadInnerXml: | 
|  | 993 | * @reader:  the xmlTextReaderPtr used | 
|  | 994 | * | 
|  | 995 | * Reads the contents of the current node, including child nodes and markup. | 
|  | 996 | * | 
|  | 997 | * Returns a string containing the XML content, or NULL if the current node | 
|  | 998 | *         is neither an element nor attribute, or has no child nodes. The | 
|  | 999 | *         string must be deallocated by the caller. | 
|  | 1000 | */ | 
|  | 1001 | xmlChar * | 
| Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 1002 | xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) { | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1003 | TODO | 
|  | 1004 | return(NULL); | 
|  | 1005 | } | 
|  | 1006 |  | 
|  | 1007 | /** | 
|  | 1008 | * xmlTextReaderReadOuterXml: | 
|  | 1009 | * @reader:  the xmlTextReaderPtr used | 
|  | 1010 | * | 
|  | 1011 | * Reads the contents of the current node, including child nodes and markup. | 
|  | 1012 | * | 
|  | 1013 | * Returns a string containing the XML content, or NULL if the current node | 
|  | 1014 | *         is neither an element nor attribute, or has no child nodes. The | 
|  | 1015 | *         string must be deallocated by the caller. | 
|  | 1016 | */ | 
|  | 1017 | xmlChar * | 
| Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 1018 | xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) { | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1019 | TODO | 
|  | 1020 | return(NULL); | 
|  | 1021 | } | 
|  | 1022 |  | 
|  | 1023 | /** | 
|  | 1024 | * xmlTextReaderReadString: | 
|  | 1025 | * @reader:  the xmlTextReaderPtr used | 
|  | 1026 | * | 
|  | 1027 | * Reads the contents of an element or a text node as a string. | 
|  | 1028 | * | 
|  | 1029 | * Returns a string containing the contents of the Element or Text node, | 
|  | 1030 | *         or NULL if the reader is positioned on any other type of node. | 
|  | 1031 | *         The string must be deallocated by the caller. | 
|  | 1032 | */ | 
|  | 1033 | xmlChar * | 
| Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 1034 | xmlTextReaderReadString(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) { | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1035 | TODO | 
|  | 1036 | return(NULL); | 
|  | 1037 | } | 
|  | 1038 |  | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1039 | /** | 
|  | 1040 | * xmlTextReaderReadBase64: | 
|  | 1041 | * @reader:  the xmlTextReaderPtr used | 
|  | 1042 | * @array:  a byte array to store the content. | 
|  | 1043 | * @offset:  the zero-based index into array where the method should | 
|  | 1044 | *           begin to write. | 
|  | 1045 | * @len:  the number of bytes to write. | 
|  | 1046 | * | 
|  | 1047 | * Reads and decodes the Base64 encoded contents of an element and | 
|  | 1048 | * stores the result in a byte buffer. | 
|  | 1049 | * | 
|  | 1050 | * Returns the number of bytes written to array, or zero if the current | 
|  | 1051 | *         instance is not positioned on an element or -1 in case of error. | 
|  | 1052 | */ | 
|  | 1053 | int | 
|  | 1054 | xmlTextReaderReadBase64(xmlTextReaderPtr reader, unsigned char *array, | 
|  | 1055 | int offset, int len) { | 
|  | 1056 | if ((reader == NULL) || (reader->ctxt == NULL)) | 
|  | 1057 | return(-1); | 
|  | 1058 | if (reader->ctxt->wellFormed != 1) | 
|  | 1059 | return(-1); | 
|  | 1060 |  | 
|  | 1061 | if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE)) | 
|  | 1062 | return(0); | 
|  | 1063 | TODO | 
|  | 1064 | return(0); | 
|  | 1065 | } | 
|  | 1066 |  | 
|  | 1067 | /** | 
|  | 1068 | * xmlTextReaderReadBinHex: | 
|  | 1069 | * @reader:  the xmlTextReaderPtr used | 
|  | 1070 | * @array:  a byte array to store the content. | 
|  | 1071 | * @offset:  the zero-based index into array where the method should | 
|  | 1072 | *           begin to write. | 
|  | 1073 | * @len:  the number of bytes to write. | 
|  | 1074 | * | 
|  | 1075 | * Reads and decodes the BinHex encoded contents of an element and | 
|  | 1076 | * stores the result in a byte buffer. | 
|  | 1077 | * | 
|  | 1078 | * Returns the number of bytes written to array, or zero if the current | 
|  | 1079 | *         instance is not positioned on an element or -1 in case of error. | 
|  | 1080 | */ | 
|  | 1081 | int | 
|  | 1082 | xmlTextReaderReadBinHex(xmlTextReaderPtr reader, unsigned char *array, | 
|  | 1083 | int offset, int len) { | 
|  | 1084 | if ((reader == NULL) || (reader->ctxt == NULL)) | 
|  | 1085 | return(-1); | 
|  | 1086 | if (reader->ctxt->wellFormed != 1) | 
|  | 1087 | return(-1); | 
|  | 1088 |  | 
|  | 1089 | if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE)) | 
|  | 1090 | return(0); | 
|  | 1091 | TODO | 
|  | 1092 | return(0); | 
|  | 1093 | } | 
|  | 1094 |  | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1095 | /************************************************************************ | 
|  | 1096 | *									* | 
|  | 1097 | *			Constructor and destructors			* | 
|  | 1098 | *									* | 
|  | 1099 | ************************************************************************/ | 
|  | 1100 | /** | 
|  | 1101 | * xmlNewTextReader: | 
|  | 1102 | * @input: the xmlParserInputBufferPtr used to read data | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1103 | * @URI: the URI information for the source if available | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1104 | * | 
|  | 1105 | * Create an xmlTextReader structure fed with @input | 
|  | 1106 | * | 
|  | 1107 | * Returns the new xmlTextReaderPtr or NULL in case of error | 
|  | 1108 | */ | 
|  | 1109 | xmlTextReaderPtr | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1110 | xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1111 | xmlTextReaderPtr ret; | 
|  | 1112 | int val; | 
|  | 1113 |  | 
|  | 1114 | if (input == NULL) | 
|  | 1115 | return(NULL); | 
|  | 1116 | ret = xmlMalloc(sizeof(xmlTextReader)); | 
|  | 1117 | if (ret == NULL) { | 
|  | 1118 | xmlGenericError(xmlGenericErrorContext, | 
|  | 1119 | "xmlNewTextReader : malloc failed\n"); | 
|  | 1120 | return(NULL); | 
|  | 1121 | } | 
|  | 1122 | memset(ret, 0, sizeof(xmlTextReader)); | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 1123 | ret->entTab = NULL; | 
|  | 1124 | ret->entMax = 0; | 
|  | 1125 | ret->entNr = 0; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1126 | ret->input = input; | 
|  | 1127 | ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); | 
|  | 1128 | if (ret->sax == NULL) { | 
|  | 1129 | xmlFree(ret); | 
|  | 1130 | xmlGenericError(xmlGenericErrorContext, | 
|  | 1131 | "xmlNewTextReader : malloc failed\n"); | 
|  | 1132 | return(NULL); | 
|  | 1133 | } | 
|  | 1134 | memcpy(ret->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler)); | 
|  | 1135 | ret->startElement = ret->sax->startElement; | 
|  | 1136 | ret->sax->startElement = xmlTextReaderStartElement; | 
|  | 1137 | ret->endElement = ret->sax->endElement; | 
|  | 1138 | ret->sax->endElement = xmlTextReaderEndElement; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1139 | ret->characters = ret->sax->characters; | 
|  | 1140 | ret->sax->characters = xmlTextReaderCharacters; | 
|  | 1141 | ret->cdataBlock = ret->sax->cdataBlock; | 
|  | 1142 | ret->sax->cdataBlock = xmlTextReaderCDataBlock; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1143 |  | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1144 | ret->mode = XML_TEXTREADER_MODE_INITIAL; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1145 | ret->node = NULL; | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1146 | ret->curnode = NULL; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1147 | val = xmlParserInputBufferRead(input, 4); | 
|  | 1148 | if (val >= 4) { | 
|  | 1149 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1150 | (const char *) ret->input->buffer->content, 4, URI); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1151 | ret->base = 0; | 
|  | 1152 | ret->cur = 4; | 
|  | 1153 | } else { | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1154 | ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1155 | ret->base = 0; | 
|  | 1156 | ret->cur = 0; | 
|  | 1157 | } | 
| Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1158 | if (ret->ctxt == NULL) { | 
|  | 1159 | xmlGenericError(xmlGenericErrorContext, | 
|  | 1160 | "xmlNewTextReader : malloc failed\n"); | 
|  | 1161 | xmlFree(ret->sax); | 
|  | 1162 | xmlFree(ret); | 
|  | 1163 | return(NULL); | 
|  | 1164 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1165 | ret->ctxt->_private = ret; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1166 | ret->ctxt->linenumbers = 1; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1167 | ret->allocs = XML_TEXTREADER_CTXT; | 
|  | 1168 | return(ret); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1169 | } | 
|  | 1170 |  | 
|  | 1171 | /** | 
|  | 1172 | * xmlNewTextReaderFilename: | 
|  | 1173 | * @URI: the URI of the resource to process | 
|  | 1174 | * | 
|  | 1175 | * Create an xmlTextReader structure fed with the resource at @URI | 
|  | 1176 | * | 
|  | 1177 | * Returns the new xmlTextReaderPtr or NULL in case of error | 
|  | 1178 | */ | 
|  | 1179 | xmlTextReaderPtr | 
|  | 1180 | xmlNewTextReaderFilename(const char *URI) { | 
|  | 1181 | xmlParserInputBufferPtr input; | 
|  | 1182 | xmlTextReaderPtr ret; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1183 | char *directory = NULL; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1184 |  | 
|  | 1185 | input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE); | 
|  | 1186 | if (input == NULL) | 
|  | 1187 | return(NULL); | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1188 | ret = xmlNewTextReader(input, URI); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1189 | if (ret == NULL) { | 
|  | 1190 | xmlFreeParserInputBuffer(input); | 
|  | 1191 | return(NULL); | 
|  | 1192 | } | 
|  | 1193 | ret->allocs |= XML_TEXTREADER_INPUT; | 
| Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 1194 | if (ret->ctxt->directory == NULL) | 
|  | 1195 | directory = xmlParserGetDirectory(URI); | 
|  | 1196 | if ((ret->ctxt->directory == NULL) && (directory != NULL)) | 
|  | 1197 | ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory); | 
|  | 1198 | if (directory != NULL) | 
|  | 1199 | xmlFree(directory); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1200 | return(ret); | 
|  | 1201 | } | 
|  | 1202 |  | 
|  | 1203 | /** | 
|  | 1204 | * xmlFreeTextReader: | 
|  | 1205 | * @reader:  the xmlTextReaderPtr | 
|  | 1206 | * | 
|  | 1207 | * Deallocate all the resources associated to the reader | 
|  | 1208 | */ | 
|  | 1209 | void | 
|  | 1210 | xmlFreeTextReader(xmlTextReaderPtr reader) { | 
|  | 1211 | if (reader == NULL) | 
|  | 1212 | return; | 
| Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 1213 | #ifdef LIBXML_SCHEMAS_ENABLED | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 1214 | if (reader->rngSchemas != NULL) { | 
|  | 1215 | xmlRelaxNGFree(reader->rngSchemas); | 
|  | 1216 | reader->rngSchemas = NULL; | 
|  | 1217 | } | 
|  | 1218 | if (reader->rngValidCtxt != NULL) { | 
|  | 1219 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); | 
|  | 1220 | reader->rngValidCtxt = NULL; | 
|  | 1221 | } | 
| Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 1222 | #endif | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1223 | if (reader->ctxt != NULL) { | 
|  | 1224 | if (reader->ctxt->myDoc != NULL) { | 
|  | 1225 | xmlFreeDoc(reader->ctxt->myDoc); | 
|  | 1226 | reader->ctxt->myDoc = NULL; | 
|  | 1227 | } | 
| Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 1228 | if ((reader->ctxt->vctxt.vstateTab != NULL) && | 
|  | 1229 | (reader->ctxt->vctxt.vstateMax > 0)){ | 
|  | 1230 | xmlFree(reader->ctxt->vctxt.vstateTab); | 
|  | 1231 | reader->ctxt->vctxt.vstateTab = 0; | 
|  | 1232 | reader->ctxt->vctxt.vstateMax = 0; | 
|  | 1233 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1234 | if (reader->allocs & XML_TEXTREADER_CTXT) | 
|  | 1235 | xmlFreeParserCtxt(reader->ctxt); | 
|  | 1236 | } | 
|  | 1237 | if (reader->sax != NULL) | 
|  | 1238 | xmlFree(reader->sax); | 
|  | 1239 | if ((reader->input != NULL)  && (reader->allocs & XML_TEXTREADER_INPUT)) | 
|  | 1240 | xmlFreeParserInputBuffer(reader->input); | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1241 | if (reader->faketext != NULL) { | 
|  | 1242 | xmlFreeNode(reader->faketext); | 
|  | 1243 | } | 
| Daniel Veillard | 1fdfd11 | 2003-01-03 01:18:43 +0000 | [diff] [blame] | 1244 | if (reader->entTab != NULL) | 
|  | 1245 | xmlFree(reader->entTab); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1246 | xmlFree(reader); | 
|  | 1247 | } | 
|  | 1248 |  | 
|  | 1249 | /************************************************************************ | 
|  | 1250 | *									* | 
| Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1251 | *			Methods for XmlTextReader			* | 
|  | 1252 | *									* | 
|  | 1253 | ************************************************************************/ | 
|  | 1254 | /** | 
|  | 1255 | * xmlTextReaderClose: | 
|  | 1256 | * @reader:  the xmlTextReaderPtr used | 
|  | 1257 | * | 
|  | 1258 | * This method releases any resources allocated by the current instance | 
|  | 1259 | * changes the state to Closed and close any underlying input. | 
|  | 1260 | * | 
|  | 1261 | * Returns 0 or -1 in case of error | 
|  | 1262 | */ | 
|  | 1263 | int | 
|  | 1264 | xmlTextReaderClose(xmlTextReaderPtr reader) { | 
|  | 1265 | if (reader == NULL) | 
|  | 1266 | return(-1); | 
|  | 1267 | reader->node = NULL; | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1268 | reader->curnode = NULL; | 
| Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1269 | reader->mode = XML_TEXTREADER_MODE_CLOSED; | 
|  | 1270 | if (reader->ctxt != NULL) { | 
|  | 1271 | if (reader->ctxt->myDoc != NULL) { | 
|  | 1272 | xmlFreeDoc(reader->ctxt->myDoc); | 
|  | 1273 | reader->ctxt->myDoc = NULL; | 
|  | 1274 | } | 
|  | 1275 | if (reader->allocs & XML_TEXTREADER_CTXT) { | 
|  | 1276 | xmlFreeParserCtxt(reader->ctxt); | 
|  | 1277 | reader->allocs -= XML_TEXTREADER_CTXT; | 
|  | 1278 | } | 
|  | 1279 | } | 
|  | 1280 | if (reader->sax != NULL) { | 
|  | 1281 | xmlFree(reader->sax); | 
|  | 1282 | reader->sax = NULL; | 
|  | 1283 | } | 
|  | 1284 | if ((reader->input != NULL)  && (reader->allocs & XML_TEXTREADER_INPUT)) { | 
|  | 1285 | xmlFreeParserInputBuffer(reader->input); | 
|  | 1286 | reader->allocs -= XML_TEXTREADER_INPUT; | 
|  | 1287 | } | 
|  | 1288 | return(0); | 
|  | 1289 | } | 
|  | 1290 |  | 
|  | 1291 | /** | 
|  | 1292 | * xmlTextReaderGetAttributeNo: | 
|  | 1293 | * @reader:  the xmlTextReaderPtr used | 
|  | 1294 | * @no: the zero-based index of the attribute relative to the containing element | 
|  | 1295 | * | 
|  | 1296 | * Provides the value of the attribute with the specified index relative | 
|  | 1297 | * to the containing element. | 
|  | 1298 | * | 
|  | 1299 | * Returns a string containing the value of the specified attribute, or NULL | 
|  | 1300 | *    in case of error. The string must be deallocated by the caller. | 
|  | 1301 | */ | 
|  | 1302 | xmlChar * | 
|  | 1303 | xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) { | 
|  | 1304 | xmlChar *ret; | 
|  | 1305 | int i; | 
|  | 1306 | xmlAttrPtr cur; | 
|  | 1307 | xmlNsPtr ns; | 
|  | 1308 |  | 
|  | 1309 | if (reader == NULL) | 
|  | 1310 | return(NULL); | 
|  | 1311 | if (reader->node == NULL) | 
|  | 1312 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1313 | if (reader->curnode != NULL) | 
|  | 1314 | return(NULL); | 
| Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1315 | /* TODO: handle the xmlDecl */ | 
|  | 1316 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1317 | return(NULL); | 
|  | 1318 |  | 
|  | 1319 | ns = reader->node->nsDef; | 
|  | 1320 | for (i = 0;(i < no) && (ns != NULL);i++) { | 
|  | 1321 | ns = ns->next; | 
|  | 1322 | } | 
|  | 1323 | if (ns != NULL) | 
|  | 1324 | return(xmlStrdup(ns->href)); | 
|  | 1325 |  | 
|  | 1326 | cur = reader->node->properties; | 
|  | 1327 | if (cur == NULL) | 
|  | 1328 | return(NULL); | 
|  | 1329 | for (;i < no;i++) { | 
|  | 1330 | cur = cur->next; | 
|  | 1331 | if (cur == NULL) | 
|  | 1332 | return(NULL); | 
|  | 1333 | } | 
|  | 1334 | /* TODO walk the DTD if present */ | 
|  | 1335 |  | 
|  | 1336 | ret = xmlNodeListGetString(reader->node->doc, cur->children, 1); | 
|  | 1337 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); | 
|  | 1338 | return(ret); | 
|  | 1339 | } | 
|  | 1340 |  | 
|  | 1341 | /** | 
|  | 1342 | * xmlTextReaderGetAttribute: | 
|  | 1343 | * @reader:  the xmlTextReaderPtr used | 
|  | 1344 | * @name: the qualified name of the attribute. | 
|  | 1345 | * | 
|  | 1346 | * Provides the value of the attribute with the specified qualified name. | 
|  | 1347 | * | 
|  | 1348 | * Returns a string containing the value of the specified attribute, or NULL | 
|  | 1349 | *    in case of error. The string must be deallocated by the caller. | 
|  | 1350 | */ | 
|  | 1351 | xmlChar * | 
|  | 1352 | xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) { | 
|  | 1353 | xmlChar *prefix = NULL; | 
|  | 1354 | xmlChar *localname; | 
|  | 1355 | xmlNsPtr ns; | 
|  | 1356 | xmlChar *ret = NULL; | 
|  | 1357 |  | 
|  | 1358 | if ((reader == NULL) || (name == NULL)) | 
|  | 1359 | return(NULL); | 
|  | 1360 | if (reader->node == NULL) | 
|  | 1361 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1362 | if (reader->curnode != NULL) | 
|  | 1363 | return(NULL); | 
| Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1364 |  | 
|  | 1365 | /* TODO: handle the xmlDecl */ | 
|  | 1366 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1367 | return(NULL); | 
|  | 1368 |  | 
|  | 1369 | localname = xmlSplitQName2(name, &prefix); | 
|  | 1370 | if (localname == NULL) | 
|  | 1371 | return(xmlGetProp(reader->node, name)); | 
|  | 1372 |  | 
|  | 1373 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); | 
|  | 1374 | if (ns != NULL) | 
|  | 1375 | ret = xmlGetNsProp(reader->node, localname, ns->href); | 
|  | 1376 |  | 
|  | 1377 | if (localname != NULL) | 
|  | 1378 | xmlFree(localname); | 
|  | 1379 | if (prefix != NULL) | 
|  | 1380 | xmlFree(prefix); | 
|  | 1381 | return(ret); | 
|  | 1382 | } | 
|  | 1383 |  | 
|  | 1384 |  | 
|  | 1385 | /** | 
|  | 1386 | * xmlTextReaderGetAttributeNs: | 
|  | 1387 | * @reader:  the xmlTextReaderPtr used | 
|  | 1388 | * @localName: the local name of the attribute. | 
|  | 1389 | * @namespaceURI: the namespace URI of the attribute. | 
|  | 1390 | * | 
|  | 1391 | * Provides the value of the specified attribute | 
|  | 1392 | * | 
|  | 1393 | * Returns a string containing the value of the specified attribute, or NULL | 
|  | 1394 | *    in case of error. The string must be deallocated by the caller. | 
|  | 1395 | */ | 
|  | 1396 | xmlChar * | 
|  | 1397 | xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName, | 
|  | 1398 | const xmlChar *namespaceURI) { | 
|  | 1399 | if ((reader == NULL) || (localName == NULL)) | 
|  | 1400 | return(NULL); | 
|  | 1401 | if (reader->node == NULL) | 
|  | 1402 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1403 | if (reader->curnode != NULL) | 
|  | 1404 | return(NULL); | 
| Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1405 |  | 
|  | 1406 | /* TODO: handle the xmlDecl */ | 
|  | 1407 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1408 | return(NULL); | 
|  | 1409 |  | 
|  | 1410 | return(xmlGetNsProp(reader->node, localName, namespaceURI)); | 
|  | 1411 | } | 
|  | 1412 |  | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1413 | /** | 
|  | 1414 | * xmlTextReaderGetRemainder: | 
|  | 1415 | * @reader:  the xmlTextReaderPtr used | 
|  | 1416 | * | 
|  | 1417 | * Method to get the remainder of the buffered XML. this method stops the | 
|  | 1418 | * parser, set its state to End Of File and return the input stream with | 
|  | 1419 | * what is left that the parser did not use. | 
|  | 1420 | * | 
|  | 1421 | * Returns the xmlParserInputBufferPtr attached to the XML or NULL | 
|  | 1422 | *    in case of error. | 
|  | 1423 | */ | 
|  | 1424 | xmlParserInputBufferPtr | 
|  | 1425 | xmlTextReaderGetRemainder(xmlTextReaderPtr reader) { | 
|  | 1426 | xmlParserInputBufferPtr ret = NULL; | 
|  | 1427 |  | 
|  | 1428 | if (reader == NULL) | 
|  | 1429 | return(NULL); | 
|  | 1430 | if (reader->node == NULL) | 
|  | 1431 | return(NULL); | 
|  | 1432 |  | 
|  | 1433 | reader->node = NULL; | 
|  | 1434 | reader->curnode = NULL; | 
|  | 1435 | reader->mode = XML_TEXTREADER_MODE_EOF; | 
|  | 1436 | if (reader->ctxt != NULL) { | 
|  | 1437 | if (reader->ctxt->myDoc != NULL) { | 
|  | 1438 | xmlFreeDoc(reader->ctxt->myDoc); | 
|  | 1439 | reader->ctxt->myDoc = NULL; | 
|  | 1440 | } | 
|  | 1441 | if (reader->allocs & XML_TEXTREADER_CTXT) { | 
|  | 1442 | xmlFreeParserCtxt(reader->ctxt); | 
|  | 1443 | reader->allocs -= XML_TEXTREADER_CTXT; | 
|  | 1444 | } | 
|  | 1445 | } | 
|  | 1446 | if (reader->sax != NULL) { | 
|  | 1447 | xmlFree(reader->sax); | 
|  | 1448 | reader->sax = NULL; | 
|  | 1449 | } | 
|  | 1450 | if (reader->allocs & XML_TEXTREADER_INPUT) { | 
|  | 1451 | ret = reader->input; | 
|  | 1452 | reader->allocs -= XML_TEXTREADER_INPUT; | 
|  | 1453 | } else { | 
|  | 1454 | /* | 
|  | 1455 | * Hum, one may need to duplicate the data structure because | 
|  | 1456 | * without reference counting the input may be freed twice: | 
|  | 1457 | *   - by the layer which allocated it. | 
|  | 1458 | *   - by the layer to which would have been returned to. | 
|  | 1459 | */ | 
|  | 1460 | TODO | 
|  | 1461 | return(NULL); | 
|  | 1462 | } | 
|  | 1463 | return(ret); | 
|  | 1464 | } | 
|  | 1465 |  | 
|  | 1466 | /** | 
|  | 1467 | * xmlTextReaderLookupNamespace: | 
|  | 1468 | * @reader:  the xmlTextReaderPtr used | 
|  | 1469 | * @prefix: the prefix whose namespace URI is to be resolved. To return | 
|  | 1470 | *          the default namespace, specify NULL | 
|  | 1471 | * | 
|  | 1472 | * Resolves a namespace prefix in the scope of the current element. | 
|  | 1473 | * | 
|  | 1474 | * Returns a string containing the namespace URI to which the prefix maps | 
|  | 1475 | *    or NULL in case of error. The string must be deallocated by the caller. | 
|  | 1476 | */ | 
|  | 1477 | xmlChar * | 
|  | 1478 | xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) { | 
|  | 1479 | xmlNsPtr ns; | 
|  | 1480 |  | 
|  | 1481 | if (reader == NULL) | 
|  | 1482 | return(NULL); | 
|  | 1483 | if (reader->node == NULL) | 
|  | 1484 | return(NULL); | 
|  | 1485 |  | 
|  | 1486 | ns = xmlSearchNs(reader->node->doc, reader->node, prefix); | 
|  | 1487 | if (ns == NULL) | 
|  | 1488 | return(NULL); | 
|  | 1489 | return(xmlStrdup(ns->href)); | 
|  | 1490 | } | 
|  | 1491 |  | 
|  | 1492 | /** | 
|  | 1493 | * xmlTextReaderMoveToAttributeNo: | 
|  | 1494 | * @reader:  the xmlTextReaderPtr used | 
|  | 1495 | * @no: the zero-based index of the attribute relative to the containing | 
|  | 1496 | *      element. | 
|  | 1497 | * | 
|  | 1498 | * Moves the position of the current instance to the attribute with | 
|  | 1499 | * the specified index relative to the containing element. | 
|  | 1500 | * | 
|  | 1501 | * Returns 1 in case of success, -1 in case of error, 0 if not found | 
|  | 1502 | */ | 
|  | 1503 | int | 
|  | 1504 | xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) { | 
|  | 1505 | int i; | 
|  | 1506 | xmlAttrPtr cur; | 
|  | 1507 | xmlNsPtr ns; | 
|  | 1508 |  | 
|  | 1509 | if (reader == NULL) | 
|  | 1510 | return(-1); | 
|  | 1511 | if (reader->node == NULL) | 
|  | 1512 | return(-1); | 
|  | 1513 | /* TODO: handle the xmlDecl */ | 
|  | 1514 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1515 | return(-1); | 
|  | 1516 |  | 
|  | 1517 | reader->curnode = NULL; | 
|  | 1518 |  | 
|  | 1519 | ns = reader->node->nsDef; | 
|  | 1520 | for (i = 0;(i < no) && (ns != NULL);i++) { | 
|  | 1521 | ns = ns->next; | 
|  | 1522 | } | 
|  | 1523 | if (ns != NULL) { | 
|  | 1524 | reader->curnode = (xmlNodePtr) ns; | 
|  | 1525 | return(1); | 
|  | 1526 | } | 
|  | 1527 |  | 
|  | 1528 | cur = reader->node->properties; | 
|  | 1529 | if (cur == NULL) | 
|  | 1530 | return(0); | 
|  | 1531 | for (;i < no;i++) { | 
|  | 1532 | cur = cur->next; | 
|  | 1533 | if (cur == NULL) | 
|  | 1534 | return(0); | 
|  | 1535 | } | 
|  | 1536 | /* TODO walk the DTD if present */ | 
|  | 1537 |  | 
|  | 1538 | reader->curnode = (xmlNodePtr) cur; | 
|  | 1539 | return(1); | 
|  | 1540 | } | 
|  | 1541 |  | 
|  | 1542 | /** | 
|  | 1543 | * xmlTextReaderMoveToAttribute: | 
|  | 1544 | * @reader:  the xmlTextReaderPtr used | 
|  | 1545 | * @name: the qualified name of the attribute. | 
|  | 1546 | * | 
|  | 1547 | * Moves the position of the current instance to the attribute with | 
|  | 1548 | * the specified qualified name. | 
|  | 1549 | * | 
|  | 1550 | * Returns 1 in case of success, -1 in case of error, 0 if not found | 
|  | 1551 | */ | 
|  | 1552 | int | 
|  | 1553 | xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) { | 
|  | 1554 | xmlChar *prefix = NULL; | 
|  | 1555 | xmlChar *localname; | 
|  | 1556 | xmlNsPtr ns; | 
|  | 1557 | xmlAttrPtr prop; | 
|  | 1558 |  | 
|  | 1559 | if ((reader == NULL) || (name == NULL)) | 
|  | 1560 | return(-1); | 
|  | 1561 | if (reader->node == NULL) | 
|  | 1562 | return(-1); | 
|  | 1563 |  | 
|  | 1564 | /* TODO: handle the xmlDecl */ | 
|  | 1565 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1566 | return(0); | 
|  | 1567 |  | 
|  | 1568 | localname = xmlSplitQName2(name, &prefix); | 
|  | 1569 | if (localname == NULL) { | 
|  | 1570 | /* | 
|  | 1571 | * Namespace default decl | 
|  | 1572 | */ | 
|  | 1573 | if (xmlStrEqual(name, BAD_CAST "xmlns")) { | 
|  | 1574 | ns = reader->node->nsDef; | 
|  | 1575 | while (ns != NULL) { | 
|  | 1576 | if (ns->prefix == NULL) { | 
|  | 1577 | reader->curnode = (xmlNodePtr) ns; | 
|  | 1578 | return(1); | 
|  | 1579 | } | 
|  | 1580 | ns = ns->next; | 
|  | 1581 | } | 
|  | 1582 | return(0); | 
|  | 1583 | } | 
|  | 1584 |  | 
|  | 1585 | prop = reader->node->properties; | 
|  | 1586 | while (prop != NULL) { | 
|  | 1587 | /* | 
|  | 1588 | * One need to have | 
|  | 1589 | *   - same attribute names | 
|  | 1590 | *   - and the attribute carrying that namespace | 
|  | 1591 | */ | 
|  | 1592 | if ((xmlStrEqual(prop->name, name)) && | 
|  | 1593 | ((prop->ns == NULL) || (prop->ns->prefix == NULL))) { | 
|  | 1594 | reader->curnode = (xmlNodePtr) prop; | 
|  | 1595 | return(1); | 
|  | 1596 | } | 
|  | 1597 | prop = prop->next; | 
|  | 1598 | } | 
|  | 1599 | return(0); | 
|  | 1600 | } | 
|  | 1601 |  | 
|  | 1602 | /* | 
|  | 1603 | * Namespace default decl | 
|  | 1604 | */ | 
|  | 1605 | if (xmlStrEqual(prefix, BAD_CAST "xmlns")) { | 
|  | 1606 | ns = reader->node->nsDef; | 
|  | 1607 | while (ns != NULL) { | 
|  | 1608 | if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) { | 
|  | 1609 | reader->curnode = (xmlNodePtr) ns; | 
|  | 1610 | goto found; | 
|  | 1611 | } | 
|  | 1612 | ns = ns->next; | 
|  | 1613 | } | 
|  | 1614 | goto not_found; | 
|  | 1615 | } | 
|  | 1616 | prop = reader->node->properties; | 
|  | 1617 | while (prop != NULL) { | 
|  | 1618 | /* | 
|  | 1619 | * One need to have | 
|  | 1620 | *   - same attribute names | 
|  | 1621 | *   - and the attribute carrying that namespace | 
|  | 1622 | */ | 
|  | 1623 | if ((xmlStrEqual(prop->name, localname)) && | 
|  | 1624 | (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) { | 
|  | 1625 | reader->curnode = (xmlNodePtr) prop; | 
|  | 1626 | goto found; | 
|  | 1627 | } | 
|  | 1628 | prop = prop->next; | 
|  | 1629 | } | 
|  | 1630 | not_found: | 
|  | 1631 | if (localname != NULL) | 
|  | 1632 | xmlFree(localname); | 
|  | 1633 | if (prefix != NULL) | 
|  | 1634 | xmlFree(prefix); | 
|  | 1635 | return(0); | 
|  | 1636 |  | 
|  | 1637 | found: | 
|  | 1638 | if (localname != NULL) | 
|  | 1639 | xmlFree(localname); | 
|  | 1640 | if (prefix != NULL) | 
|  | 1641 | xmlFree(prefix); | 
|  | 1642 | return(1); | 
|  | 1643 | } | 
|  | 1644 |  | 
|  | 1645 | /** | 
|  | 1646 | * xmlTextReaderMoveToAttributeNs: | 
|  | 1647 | * @reader:  the xmlTextReaderPtr used | 
|  | 1648 | * @localName:  the local name of the attribute. | 
|  | 1649 | * @namespaceURI:  the namespace URI of the attribute. | 
|  | 1650 | * | 
|  | 1651 | * Moves the position of the current instance to the attribute with the | 
|  | 1652 | * specified local name and namespace URI. | 
|  | 1653 | * | 
|  | 1654 | * Returns 1 in case of success, -1 in case of error, 0 if not found | 
|  | 1655 | */ | 
|  | 1656 | int | 
|  | 1657 | xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader, | 
|  | 1658 | const xmlChar *localName, const xmlChar *namespaceURI) { | 
|  | 1659 | xmlAttrPtr prop; | 
|  | 1660 | xmlNodePtr node; | 
|  | 1661 |  | 
|  | 1662 | if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL)) | 
|  | 1663 | return(-1); | 
|  | 1664 | if (reader->node == NULL) | 
|  | 1665 | return(-1); | 
|  | 1666 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1667 | return(0); | 
|  | 1668 | node = reader->node; | 
|  | 1669 |  | 
|  | 1670 | /* | 
|  | 1671 | * A priori reading http://www.w3.org/TR/REC-xml-names/ there is no | 
|  | 1672 | * namespace name associated to "xmlns" | 
|  | 1673 | */ | 
|  | 1674 | prop = node->properties; | 
|  | 1675 | while (prop != NULL) { | 
|  | 1676 | /* | 
|  | 1677 | * One need to have | 
|  | 1678 | *   - same attribute names | 
|  | 1679 | *   - and the attribute carrying that namespace | 
|  | 1680 | */ | 
|  | 1681 | if (xmlStrEqual(prop->name, localName) && | 
|  | 1682 | ((prop->ns != NULL) && | 
|  | 1683 | (xmlStrEqual(prop->ns->href, namespaceURI)))) { | 
|  | 1684 | reader->curnode = (xmlNodePtr) prop; | 
|  | 1685 | return(1); | 
|  | 1686 | } | 
|  | 1687 | prop = prop->next; | 
|  | 1688 | } | 
|  | 1689 | return(0); | 
|  | 1690 | } | 
|  | 1691 |  | 
|  | 1692 | /** | 
|  | 1693 | * xmlTextReaderMoveToFirstAttribute: | 
|  | 1694 | * @reader:  the xmlTextReaderPtr used | 
|  | 1695 | * | 
|  | 1696 | * Moves the position of the current instance to the first attribute | 
|  | 1697 | * associated with the current node. | 
|  | 1698 | * | 
|  | 1699 | * Returns 1 in case of success, -1 in case of error, 0 if not found | 
|  | 1700 | */ | 
|  | 1701 | int | 
|  | 1702 | xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) { | 
|  | 1703 | if (reader == NULL) | 
|  | 1704 | return(-1); | 
|  | 1705 | if (reader->node == NULL) | 
|  | 1706 | return(-1); | 
|  | 1707 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1708 | return(0); | 
|  | 1709 |  | 
|  | 1710 | if (reader->node->nsDef != NULL) { | 
|  | 1711 | reader->curnode = (xmlNodePtr) reader->node->nsDef; | 
|  | 1712 | return(1); | 
|  | 1713 | } | 
|  | 1714 | if (reader->node->properties != NULL) { | 
|  | 1715 | reader->curnode = (xmlNodePtr) reader->node->properties; | 
|  | 1716 | return(1); | 
|  | 1717 | } | 
|  | 1718 | return(0); | 
|  | 1719 | } | 
|  | 1720 |  | 
|  | 1721 | /** | 
|  | 1722 | * xmlTextReaderMoveToNextAttribute: | 
|  | 1723 | * @reader:  the xmlTextReaderPtr used | 
|  | 1724 | * | 
|  | 1725 | * Moves the position of the current instance to the next attribute | 
|  | 1726 | * associated with the current node. | 
|  | 1727 | * | 
|  | 1728 | * Returns 1 in case of success, -1 in case of error, 0 if not found | 
|  | 1729 | */ | 
|  | 1730 | int | 
|  | 1731 | xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) { | 
|  | 1732 | if (reader == NULL) | 
|  | 1733 | return(-1); | 
|  | 1734 | if (reader->node == NULL) | 
|  | 1735 | return(-1); | 
|  | 1736 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1737 | return(0); | 
|  | 1738 | if (reader->curnode == NULL) | 
|  | 1739 | return(xmlTextReaderMoveToFirstAttribute(reader)); | 
|  | 1740 |  | 
|  | 1741 | if (reader->curnode->type == XML_NAMESPACE_DECL) { | 
|  | 1742 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; | 
|  | 1743 | if (ns->next != NULL) { | 
|  | 1744 | reader->curnode = (xmlNodePtr) ns->next; | 
|  | 1745 | return(1); | 
|  | 1746 | } | 
|  | 1747 | if (reader->node->properties != NULL) { | 
|  | 1748 | reader->curnode = (xmlNodePtr) reader->node->properties; | 
|  | 1749 | return(1); | 
|  | 1750 | } | 
|  | 1751 | return(0); | 
|  | 1752 | } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) && | 
|  | 1753 | (reader->curnode->next != NULL)) { | 
|  | 1754 | reader->curnode = reader->curnode->next; | 
|  | 1755 | return(1); | 
|  | 1756 | } | 
|  | 1757 | return(0); | 
|  | 1758 | } | 
|  | 1759 |  | 
|  | 1760 | /** | 
|  | 1761 | * xmlTextReaderMoveToElement: | 
|  | 1762 | * @reader:  the xmlTextReaderPtr used | 
|  | 1763 | * | 
|  | 1764 | * Moves the position of the current instance to the node that | 
|  | 1765 | * contains the current Attribute  node. | 
|  | 1766 | * | 
|  | 1767 | * Returns 1 in case of success, -1 in case of error, 0 if not moved | 
|  | 1768 | */ | 
|  | 1769 | int | 
|  | 1770 | xmlTextReaderMoveToElement(xmlTextReaderPtr reader) { | 
|  | 1771 | if (reader == NULL) | 
|  | 1772 | return(-1); | 
|  | 1773 | if (reader->node == NULL) | 
|  | 1774 | return(-1); | 
|  | 1775 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1776 | return(0); | 
|  | 1777 | if (reader->curnode != NULL) { | 
|  | 1778 | reader->curnode = NULL; | 
|  | 1779 | return(1); | 
|  | 1780 | } | 
|  | 1781 | return(0); | 
|  | 1782 | } | 
|  | 1783 |  | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 1784 | /** | 
|  | 1785 | * xmlTextReaderReadAttributeValue: | 
|  | 1786 | * @reader:  the xmlTextReaderPtr used | 
|  | 1787 | * | 
|  | 1788 | * Parses an attribute value into one or more Text and EntityReference nodes. | 
|  | 1789 | * | 
|  | 1790 | * Returns 1 in case of success, 0 if the reader was not positionned on an | 
|  | 1791 | *         ttribute node or all the attribute values have been read, or -1 | 
|  | 1792 | *         in case of error. | 
|  | 1793 | */ | 
|  | 1794 | int | 
|  | 1795 | xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) { | 
|  | 1796 | if (reader == NULL) | 
|  | 1797 | return(-1); | 
|  | 1798 | if (reader->node == NULL) | 
|  | 1799 | return(-1); | 
|  | 1800 | if (reader->curnode == NULL) | 
|  | 1801 | return(0); | 
|  | 1802 | if (reader->curnode->type == XML_ATTRIBUTE_NODE) { | 
|  | 1803 | if (reader->curnode->children == NULL) | 
|  | 1804 | return(0); | 
|  | 1805 | reader->curnode = reader->curnode->children; | 
|  | 1806 | } else if (reader->curnode->type == XML_NAMESPACE_DECL) { | 
|  | 1807 | xmlNsPtr ns = (xmlNsPtr) reader->curnode; | 
|  | 1808 |  | 
|  | 1809 | if (reader->faketext == NULL) { | 
|  | 1810 | reader->faketext = xmlNewDocText(reader->node->doc, | 
|  | 1811 | ns->href); | 
|  | 1812 | } else { | 
|  | 1813 | if (reader->faketext->content != NULL) | 
|  | 1814 | xmlFree(reader->faketext->content); | 
|  | 1815 | reader->faketext->content = xmlStrdup(ns->href); | 
|  | 1816 | } | 
|  | 1817 | reader->curnode = reader->faketext; | 
|  | 1818 | } else { | 
|  | 1819 | if (reader->curnode->next == NULL) | 
|  | 1820 | return(0); | 
|  | 1821 | reader->curnode = reader->curnode->next; | 
|  | 1822 | } | 
|  | 1823 | return(1); | 
|  | 1824 | } | 
|  | 1825 |  | 
| Daniel Veillard | 0eb38c7 | 2002-12-14 23:00:35 +0000 | [diff] [blame] | 1826 | /************************************************************************ | 
|  | 1827 | *									* | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1828 | *			Acces API to the current node			* | 
|  | 1829 | *									* | 
|  | 1830 | ************************************************************************/ | 
|  | 1831 | /** | 
|  | 1832 | * xmlTextReaderAttributeCount: | 
|  | 1833 | * @reader:  the xmlTextReaderPtr used | 
|  | 1834 | * | 
| Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1835 | * Provides the number of attributes of the current node | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1836 | * | 
|  | 1837 | * Returns 0 i no attributes, -1 in case of error or the attribute count | 
|  | 1838 | */ | 
|  | 1839 | int | 
|  | 1840 | xmlTextReaderAttributeCount(xmlTextReaderPtr reader) { | 
|  | 1841 | int ret; | 
|  | 1842 | xmlAttrPtr attr; | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1843 | xmlNsPtr ns; | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1844 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1845 |  | 
|  | 1846 | if (reader == NULL) | 
|  | 1847 | return(-1); | 
|  | 1848 | if (reader->node == NULL) | 
|  | 1849 | return(0); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1850 |  | 
|  | 1851 | if (reader->curnode != NULL) | 
|  | 1852 | node = reader->curnode; | 
|  | 1853 | else | 
|  | 1854 | node = reader->node; | 
|  | 1855 |  | 
|  | 1856 | if (node->type != XML_ELEMENT_NODE) | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1857 | return(0); | 
|  | 1858 | if ((reader->state == XML_TEXTREADER_END) || | 
|  | 1859 | (reader->state == XML_TEXTREADER_BACKTRACK)) | 
|  | 1860 | return(0); | 
|  | 1861 | ret = 0; | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1862 | attr = node->properties; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1863 | while (attr != NULL) { | 
|  | 1864 | ret++; | 
|  | 1865 | attr = attr->next; | 
|  | 1866 | } | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 1867 | ns = node->nsDef; | 
|  | 1868 | while (ns != NULL) { | 
|  | 1869 | ret++; | 
|  | 1870 | ns = ns->next; | 
|  | 1871 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1872 | return(ret); | 
|  | 1873 | } | 
|  | 1874 |  | 
|  | 1875 | /** | 
|  | 1876 | * xmlTextReaderNodeType: | 
|  | 1877 | * @reader:  the xmlTextReaderPtr used | 
|  | 1878 | * | 
|  | 1879 | * Get the node type of the current node | 
|  | 1880 | * Reference: | 
|  | 1881 | * http://dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html | 
|  | 1882 | * | 
|  | 1883 | * Returns the xmlNodeType of the current node or -1 in case of error | 
|  | 1884 | */ | 
|  | 1885 | int | 
|  | 1886 | xmlTextReaderNodeType(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1887 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1888 | if (reader == NULL) | 
|  | 1889 | return(-1); | 
|  | 1890 | if (reader->node == NULL) | 
|  | 1891 | return(0); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1892 | if (reader->curnode != NULL) | 
|  | 1893 | node = reader->curnode; | 
|  | 1894 | else | 
|  | 1895 | node = reader->node; | 
|  | 1896 | switch (node->type) { | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1897 | case XML_ELEMENT_NODE: | 
|  | 1898 | if ((reader->state == XML_TEXTREADER_END) || | 
|  | 1899 | (reader->state == XML_TEXTREADER_BACKTRACK)) | 
|  | 1900 | return(15); | 
|  | 1901 | return(1); | 
| Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 1902 | case XML_NAMESPACE_DECL: | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1903 | case XML_ATTRIBUTE_NODE: | 
|  | 1904 | return(2); | 
|  | 1905 | case XML_TEXT_NODE: | 
|  | 1906 | return(3); /* TODO: SignificantWhitespace == 14 Whitespace == 13 */ | 
|  | 1907 | case XML_CDATA_SECTION_NODE: | 
|  | 1908 | return(4); | 
|  | 1909 | case XML_ENTITY_REF_NODE: | 
|  | 1910 | return(5); | 
|  | 1911 | case XML_ENTITY_NODE: | 
|  | 1912 | return(6); | 
|  | 1913 | case XML_PI_NODE: | 
|  | 1914 | return(7); | 
|  | 1915 | case XML_COMMENT_NODE: | 
|  | 1916 | return(8); | 
|  | 1917 | case XML_DOCUMENT_NODE: | 
|  | 1918 | case XML_HTML_DOCUMENT_NODE: | 
|  | 1919 | #ifdef LIBXML_DOCB_ENABLED | 
|  | 1920 | case XML_DOCB_DOCUMENT_NODE: | 
|  | 1921 | #endif | 
|  | 1922 | return(9); | 
|  | 1923 | case XML_DOCUMENT_FRAG_NODE: | 
|  | 1924 | return(11); | 
|  | 1925 | case XML_NOTATION_NODE: | 
|  | 1926 | return(12); | 
|  | 1927 | case XML_DOCUMENT_TYPE_NODE: | 
|  | 1928 | case XML_DTD_NODE: | 
|  | 1929 | return(10); | 
|  | 1930 |  | 
|  | 1931 | case XML_ELEMENT_DECL: | 
|  | 1932 | case XML_ATTRIBUTE_DECL: | 
|  | 1933 | case XML_ENTITY_DECL: | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1934 | case XML_XINCLUDE_START: | 
|  | 1935 | case XML_XINCLUDE_END: | 
|  | 1936 | return(0); | 
|  | 1937 | } | 
|  | 1938 | return(-1); | 
|  | 1939 | } | 
|  | 1940 |  | 
|  | 1941 | /** | 
| Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1942 | * xmlTextReaderIsEmptyElement: | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1943 | * @reader:  the xmlTextReaderPtr used | 
|  | 1944 | * | 
|  | 1945 | * Check if the current node is empty | 
|  | 1946 | * | 
|  | 1947 | * Returns 1 if empty, 0 if not and -1 in case of error | 
|  | 1948 | */ | 
|  | 1949 | int | 
|  | 1950 | xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) { | 
|  | 1951 | if ((reader == NULL) || (reader->node == NULL)) | 
|  | 1952 | return(-1); | 
| Daniel Veillard | df512f4 | 2002-12-23 15:56:21 +0000 | [diff] [blame] | 1953 | if (reader->node->type != XML_ELEMENT_NODE) | 
|  | 1954 | return(0); | 
| Daniel Veillard | e3c036e | 2003-01-01 15:11:05 +0000 | [diff] [blame] | 1955 | if (reader->curnode != NULL) | 
|  | 1956 | return(0); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1957 | if (reader->node->children != NULL) | 
|  | 1958 | return(0); | 
| Daniel Veillard | dab8ea9 | 2003-01-02 14:16:45 +0000 | [diff] [blame] | 1959 | if (reader->state == XML_TEXTREADER_END) | 
|  | 1960 | return(0); | 
| Daniel Veillard | 067bae5 | 2003-01-05 01:27:54 +0000 | [diff] [blame] | 1961 | return(reader->node->_private == (void *)xmlTextReaderIsEmpty); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1962 | } | 
|  | 1963 |  | 
|  | 1964 | /** | 
|  | 1965 | * xmlTextReaderLocalName: | 
|  | 1966 | * @reader:  the xmlTextReaderPtr used | 
|  | 1967 | * | 
|  | 1968 | * The local name of the node. | 
|  | 1969 | * | 
|  | 1970 | * Returns the local name or NULL if not available | 
|  | 1971 | */ | 
|  | 1972 | xmlChar * | 
|  | 1973 | xmlTextReaderLocalName(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1974 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1975 | if ((reader == NULL) || (reader->node == NULL)) | 
|  | 1976 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1977 | if (reader->curnode != NULL) | 
|  | 1978 | node = reader->curnode; | 
|  | 1979 | else | 
|  | 1980 | node = reader->node; | 
|  | 1981 | if (node->type == XML_NAMESPACE_DECL) { | 
|  | 1982 | xmlNsPtr ns = (xmlNsPtr) node; | 
|  | 1983 | if (ns->prefix == NULL) | 
|  | 1984 | return(xmlStrdup(BAD_CAST "xmlns")); | 
|  | 1985 | else | 
|  | 1986 | return(xmlStrdup(ns->prefix)); | 
|  | 1987 | } | 
|  | 1988 | if ((node->type != XML_ELEMENT_NODE) && | 
|  | 1989 | (node->type != XML_ATTRIBUTE_NODE)) | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 1990 | return(xmlTextReaderName(reader)); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 1991 | return(xmlStrdup(node->name)); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 1992 | } | 
|  | 1993 |  | 
|  | 1994 | /** | 
|  | 1995 | * xmlTextReaderName: | 
|  | 1996 | * @reader:  the xmlTextReaderPtr used | 
|  | 1997 | * | 
|  | 1998 | * The qualified name of the node, equal to Prefix :LocalName. | 
|  | 1999 | * | 
|  | 2000 | * Returns the local name or NULL if not available | 
|  | 2001 | */ | 
|  | 2002 | xmlChar * | 
|  | 2003 | xmlTextReaderName(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2004 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2005 | xmlChar *ret; | 
|  | 2006 |  | 
|  | 2007 | if ((reader == NULL) || (reader->node == NULL)) | 
|  | 2008 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2009 | if (reader->curnode != NULL) | 
|  | 2010 | node = reader->curnode; | 
|  | 2011 | else | 
|  | 2012 | node = reader->node; | 
|  | 2013 | switch (node->type) { | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2014 | case XML_ELEMENT_NODE: | 
|  | 2015 | case XML_ATTRIBUTE_NODE: | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2016 | if ((node->ns == NULL) || | 
|  | 2017 | (node->ns->prefix == NULL)) | 
|  | 2018 | return(xmlStrdup(node->name)); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2019 |  | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2020 | ret = xmlStrdup(node->ns->prefix); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2021 | ret = xmlStrcat(ret, BAD_CAST ":"); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2022 | ret = xmlStrcat(ret, node->name); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2023 | return(ret); | 
|  | 2024 | case XML_TEXT_NODE: | 
|  | 2025 | return(xmlStrdup(BAD_CAST "#text")); | 
|  | 2026 | case XML_CDATA_SECTION_NODE: | 
|  | 2027 | return(xmlStrdup(BAD_CAST "#cdata-section")); | 
|  | 2028 | case XML_ENTITY_NODE: | 
|  | 2029 | case XML_ENTITY_REF_NODE: | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2030 | return(xmlStrdup(node->name)); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2031 | case XML_PI_NODE: | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2032 | return(xmlStrdup(node->name)); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2033 | case XML_COMMENT_NODE: | 
|  | 2034 | return(xmlStrdup(BAD_CAST "#comment")); | 
|  | 2035 | case XML_DOCUMENT_NODE: | 
|  | 2036 | case XML_HTML_DOCUMENT_NODE: | 
|  | 2037 | #ifdef LIBXML_DOCB_ENABLED | 
|  | 2038 | case XML_DOCB_DOCUMENT_NODE: | 
|  | 2039 | #endif | 
|  | 2040 | return(xmlStrdup(BAD_CAST "#document")); | 
|  | 2041 | case XML_DOCUMENT_FRAG_NODE: | 
|  | 2042 | return(xmlStrdup(BAD_CAST "#document-fragment")); | 
|  | 2043 | case XML_NOTATION_NODE: | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2044 | return(xmlStrdup(node->name)); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2045 | case XML_DOCUMENT_TYPE_NODE: | 
|  | 2046 | case XML_DTD_NODE: | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2047 | return(xmlStrdup(node->name)); | 
|  | 2048 | case XML_NAMESPACE_DECL: { | 
|  | 2049 | xmlNsPtr ns = (xmlNsPtr) node; | 
|  | 2050 |  | 
|  | 2051 | ret = xmlStrdup(BAD_CAST "xmlns"); | 
|  | 2052 | if (ns->prefix == NULL) | 
|  | 2053 | return(ret); | 
|  | 2054 | ret = xmlStrcat(ret, BAD_CAST ":"); | 
|  | 2055 | ret = xmlStrcat(ret, ns->prefix); | 
|  | 2056 | return(ret); | 
|  | 2057 | } | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2058 |  | 
|  | 2059 | case XML_ELEMENT_DECL: | 
|  | 2060 | case XML_ATTRIBUTE_DECL: | 
|  | 2061 | case XML_ENTITY_DECL: | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2062 | case XML_XINCLUDE_START: | 
|  | 2063 | case XML_XINCLUDE_END: | 
|  | 2064 | return(NULL); | 
|  | 2065 | } | 
|  | 2066 | return(NULL); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2067 | } | 
|  | 2068 |  | 
|  | 2069 | /** | 
|  | 2070 | * xmlTextReaderPrefix: | 
|  | 2071 | * @reader:  the xmlTextReaderPtr used | 
|  | 2072 | * | 
|  | 2073 | * A shorthand reference to the namespace associated with the node. | 
|  | 2074 | * | 
|  | 2075 | * Returns the prefix or NULL if not available | 
|  | 2076 | */ | 
|  | 2077 | xmlChar * | 
|  | 2078 | xmlTextReaderPrefix(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2079 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2080 | if ((reader == NULL) || (reader->node == NULL)) | 
|  | 2081 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2082 | if (reader->curnode != NULL) | 
|  | 2083 | node = reader->curnode; | 
|  | 2084 | else | 
|  | 2085 | node = reader->node; | 
|  | 2086 | if (node->type == XML_NAMESPACE_DECL) { | 
|  | 2087 | xmlNsPtr ns = (xmlNsPtr) node; | 
|  | 2088 | if (ns->prefix == NULL) | 
|  | 2089 | return(NULL); | 
|  | 2090 | return(xmlStrdup(BAD_CAST "xmlns")); | 
|  | 2091 | } | 
|  | 2092 | if ((node->type != XML_ELEMENT_NODE) && | 
|  | 2093 | (node->type != XML_ATTRIBUTE_NODE)) | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2094 | return(NULL); | 
| Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2095 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2096 | return(xmlStrdup(node->ns->prefix)); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2097 | return(NULL); | 
|  | 2098 | } | 
|  | 2099 |  | 
|  | 2100 | /** | 
|  | 2101 | * xmlTextReaderNamespaceUri: | 
|  | 2102 | * @reader:  the xmlTextReaderPtr used | 
|  | 2103 | * | 
|  | 2104 | * The URI defining the namespace associated with the node. | 
|  | 2105 | * | 
|  | 2106 | * Returns the namespace URI or NULL if not available | 
|  | 2107 | */ | 
|  | 2108 | xmlChar * | 
|  | 2109 | xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2110 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2111 | if ((reader == NULL) || (reader->node == NULL)) | 
|  | 2112 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2113 | if (reader->curnode != NULL) | 
|  | 2114 | node = reader->curnode; | 
|  | 2115 | else | 
|  | 2116 | node = reader->node; | 
| Daniel Veillard | ecaba49 | 2002-12-30 10:55:29 +0000 | [diff] [blame] | 2117 | if (node->type == XML_NAMESPACE_DECL) | 
|  | 2118 | return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/")); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2119 | if ((node->type != XML_ELEMENT_NODE) && | 
|  | 2120 | (node->type != XML_ATTRIBUTE_NODE)) | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2121 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2122 | if (node->ns != NULL) | 
|  | 2123 | return(xmlStrdup(node->ns->href)); | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2124 | return(NULL); | 
|  | 2125 | } | 
|  | 2126 |  | 
|  | 2127 | /** | 
|  | 2128 | * xmlTextReaderBaseUri: | 
|  | 2129 | * @reader:  the xmlTextReaderPtr used | 
|  | 2130 | * | 
|  | 2131 | * The base URI of the node. | 
|  | 2132 | * | 
|  | 2133 | * Returns the base URI or NULL if not available | 
|  | 2134 | */ | 
|  | 2135 | xmlChar * | 
|  | 2136 | xmlTextReaderBaseUri(xmlTextReaderPtr reader) { | 
|  | 2137 | if ((reader == NULL) || (reader->node == NULL)) | 
|  | 2138 | return(NULL); | 
|  | 2139 | return(xmlNodeGetBase(NULL, reader->node)); | 
|  | 2140 | } | 
|  | 2141 |  | 
|  | 2142 | /** | 
|  | 2143 | * xmlTextReaderDepth: | 
|  | 2144 | * @reader:  the xmlTextReaderPtr used | 
|  | 2145 | * | 
|  | 2146 | * The depth of the node in the tree. | 
|  | 2147 | * | 
|  | 2148 | * Returns the depth or -1 in case of error | 
|  | 2149 | */ | 
|  | 2150 | int | 
|  | 2151 | xmlTextReaderDepth(xmlTextReaderPtr reader) { | 
|  | 2152 | if (reader == NULL) | 
|  | 2153 | return(-1); | 
|  | 2154 | if (reader->node == NULL) | 
|  | 2155 | return(0); | 
|  | 2156 |  | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2157 | if (reader->curnode != NULL) { | 
|  | 2158 | if ((reader->curnode->type == XML_ATTRIBUTE_NODE) || | 
|  | 2159 | (reader->curnode->type == XML_NAMESPACE_DECL)) | 
|  | 2160 | return(reader->depth + 1); | 
|  | 2161 | return(reader->depth + 2); | 
|  | 2162 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2163 | return(reader->depth); | 
|  | 2164 | } | 
|  | 2165 |  | 
|  | 2166 | /** | 
|  | 2167 | * xmlTextReaderHasAttributes: | 
|  | 2168 | * @reader:  the xmlTextReaderPtr used | 
|  | 2169 | * | 
|  | 2170 | * Whether the node has attributes. | 
|  | 2171 | * | 
|  | 2172 | * Returns 1 if true, 0 if false, and -1 in case or error | 
|  | 2173 | */ | 
|  | 2174 | int | 
|  | 2175 | xmlTextReaderHasAttributes(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2176 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2177 | if (reader == NULL) | 
|  | 2178 | return(-1); | 
|  | 2179 | if (reader->node == NULL) | 
|  | 2180 | return(0); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2181 | if (reader->curnode != NULL) | 
|  | 2182 | node = reader->curnode; | 
|  | 2183 | else | 
|  | 2184 | node = reader->node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2185 |  | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2186 | if ((node->type == XML_ELEMENT_NODE) && | 
|  | 2187 | (node->properties != NULL)) | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2188 | return(1); | 
|  | 2189 | /* TODO: handle the xmlDecl */ | 
|  | 2190 | return(0); | 
|  | 2191 | } | 
|  | 2192 |  | 
|  | 2193 | /** | 
|  | 2194 | * xmlTextReaderHasValue: | 
|  | 2195 | * @reader:  the xmlTextReaderPtr used | 
|  | 2196 | * | 
|  | 2197 | * Whether the node can have a text value. | 
|  | 2198 | * | 
|  | 2199 | * Returns 1 if true, 0 if false, and -1 in case or error | 
|  | 2200 | */ | 
|  | 2201 | int | 
|  | 2202 | xmlTextReaderHasValue(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2203 | xmlNodePtr node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2204 | if (reader == NULL) | 
|  | 2205 | return(-1); | 
|  | 2206 | if (reader->node == NULL) | 
|  | 2207 | return(0); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2208 | if (reader->curnode != NULL) | 
|  | 2209 | node = reader->curnode; | 
|  | 2210 | else | 
|  | 2211 | node = reader->node; | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2212 |  | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2213 | switch (node->type) { | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2214 | case XML_ATTRIBUTE_NODE: | 
|  | 2215 | case XML_TEXT_NODE: | 
|  | 2216 | case XML_CDATA_SECTION_NODE: | 
|  | 2217 | case XML_PI_NODE: | 
|  | 2218 | case XML_COMMENT_NODE: | 
| Daniel Veillard | 9e07710 | 2003-04-10 13:36:54 +0000 | [diff] [blame] | 2219 | case XML_NAMESPACE_DECL: | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2220 | return(1); | 
|  | 2221 | default: | 
| Daniel Veillard | 2cfd9df | 2003-03-22 22:39:16 +0000 | [diff] [blame] | 2222 | break; | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2223 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2224 | return(0); | 
|  | 2225 | } | 
|  | 2226 |  | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2227 | /** | 
|  | 2228 | * xmlTextReaderValue: | 
|  | 2229 | * @reader:  the xmlTextReaderPtr used | 
|  | 2230 | * | 
|  | 2231 | * Provides the text value of the node if present | 
|  | 2232 | * | 
|  | 2233 | * Returns the string or NULL if not available. The retsult must be deallocated | 
|  | 2234 | *     with xmlFree() | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2235 | */ | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2236 | xmlChar * | 
|  | 2237 | xmlTextReaderValue(xmlTextReaderPtr reader) { | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2238 | xmlNodePtr node; | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2239 | if (reader == NULL) | 
|  | 2240 | return(NULL); | 
|  | 2241 | if (reader->node == NULL) | 
|  | 2242 | return(NULL); | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2243 | if (reader->curnode != NULL) | 
|  | 2244 | node = reader->curnode; | 
|  | 2245 | else | 
|  | 2246 | node = reader->node; | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2247 |  | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2248 | switch (node->type) { | 
|  | 2249 | case XML_NAMESPACE_DECL: | 
|  | 2250 | return(xmlStrdup(((xmlNsPtr) node)->href)); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2251 | case XML_ATTRIBUTE_NODE:{ | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2252 | xmlAttrPtr attr = (xmlAttrPtr) node; | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2253 |  | 
|  | 2254 | if (attr->parent != NULL) | 
|  | 2255 | return (xmlNodeListGetString | 
|  | 2256 | (attr->parent->doc, attr->children, 1)); | 
|  | 2257 | else | 
|  | 2258 | return (xmlNodeListGetString(NULL, attr->children, 1)); | 
|  | 2259 | break; | 
|  | 2260 | } | 
|  | 2261 | case XML_TEXT_NODE: | 
|  | 2262 | case XML_CDATA_SECTION_NODE: | 
|  | 2263 | case XML_PI_NODE: | 
|  | 2264 | case XML_COMMENT_NODE: | 
| Daniel Veillard | da46d2d | 2002-12-15 23:36:49 +0000 | [diff] [blame] | 2265 | if (node->content != NULL) | 
|  | 2266 | return (xmlStrdup(node->content)); | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2267 | default: | 
| Daniel Veillard | 2cfd9df | 2003-03-22 22:39:16 +0000 | [diff] [blame] | 2268 | break; | 
| Daniel Veillard | 9b4bb4d | 2002-12-11 19:28:47 +0000 | [diff] [blame] | 2269 | } | 
|  | 2270 | return(NULL); | 
|  | 2271 | } | 
|  | 2272 |  | 
|  | 2273 | /** | 
|  | 2274 | * xmlTextReaderIsDefault: | 
|  | 2275 | * @reader:  the xmlTextReaderPtr used | 
|  | 2276 | * | 
|  | 2277 | * Whether an Attribute  node was generated from the default value | 
|  | 2278 | * defined in the DTD or schema. | 
|  | 2279 | * | 
|  | 2280 | * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error | 
|  | 2281 | */ | 
|  | 2282 | int | 
|  | 2283 | xmlTextReaderIsDefault(xmlTextReaderPtr reader) { | 
|  | 2284 | if (reader == NULL) | 
|  | 2285 | return(-1); | 
|  | 2286 | return(0); | 
|  | 2287 | } | 
|  | 2288 |  | 
|  | 2289 | /** | 
|  | 2290 | * xmlTextReaderQuoteChar: | 
|  | 2291 | * @reader:  the xmlTextReaderPtr used | 
|  | 2292 | * | 
|  | 2293 | * The quotation mark character used to enclose the value of an attribute. | 
|  | 2294 | * | 
|  | 2295 | * Returns " or ' and -1 in case of error | 
|  | 2296 | */ | 
|  | 2297 | int | 
|  | 2298 | xmlTextReaderQuoteChar(xmlTextReaderPtr reader) { | 
|  | 2299 | if (reader == NULL) | 
|  | 2300 | return(-1); | 
|  | 2301 | /* TODO maybe lookup the attribute value for " first */ | 
|  | 2302 | return((int) '"'); | 
|  | 2303 | } | 
| Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 2304 |  | 
|  | 2305 | /** | 
|  | 2306 | * xmlTextReaderXmlLang: | 
|  | 2307 | * @reader:  the xmlTextReaderPtr used | 
|  | 2308 | * | 
|  | 2309 | * The xml:lang scope within which the node resides. | 
|  | 2310 | * | 
|  | 2311 | * Returns the xml:lang value or NULL if none exists. | 
|  | 2312 | */ | 
|  | 2313 | xmlChar * | 
|  | 2314 | xmlTextReaderXmlLang(xmlTextReaderPtr reader) { | 
|  | 2315 | if (reader == NULL) | 
|  | 2316 | return(NULL); | 
|  | 2317 | if (reader->node == NULL) | 
|  | 2318 | return(NULL); | 
|  | 2319 | return(xmlNodeGetLang(reader->node)); | 
|  | 2320 | } | 
|  | 2321 |  | 
| Daniel Veillard | 67df809 | 2002-12-16 22:04:11 +0000 | [diff] [blame] | 2322 | /** | 
|  | 2323 | * xmlTextReaderNormalization: | 
|  | 2324 | * @reader:  the xmlTextReaderPtr used | 
|  | 2325 | * | 
|  | 2326 | * The value indicating whether to normalize white space and attribute values. | 
|  | 2327 | * Since attribute value and end of line normalizations are a MUST in the XML | 
|  | 2328 | * specification only the value true is accepted. The broken bahaviour of | 
|  | 2329 | * accepting out of range character entities like � is of course not | 
|  | 2330 | * supported either. | 
|  | 2331 | * | 
|  | 2332 | * Returns 1 or -1 in case of error. | 
|  | 2333 | */ | 
|  | 2334 | int | 
|  | 2335 | xmlTextReaderNormalization(xmlTextReaderPtr reader) { | 
|  | 2336 | if (reader == NULL) | 
|  | 2337 | return(-1); | 
|  | 2338 | return(1); | 
|  | 2339 | } | 
|  | 2340 |  | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2341 | /************************************************************************ | 
|  | 2342 | *									* | 
|  | 2343 | *			Extensions to the base APIs			* | 
|  | 2344 | *									* | 
|  | 2345 | ************************************************************************/ | 
|  | 2346 |  | 
|  | 2347 | /** | 
|  | 2348 | * xmlTextReaderSetParserProp: | 
|  | 2349 | * @reader:  the xmlTextReaderPtr used | 
|  | 2350 | * @prop:  the xmlParserProperties to set | 
|  | 2351 | * @value:  usually 0 or 1 to (de)activate it | 
|  | 2352 | * | 
|  | 2353 | * Change the parser processing behaviour by changing some of its internal | 
|  | 2354 | * properties. Note that some properties can only be changed before any | 
|  | 2355 | * read has been done. | 
|  | 2356 | * | 
|  | 2357 | * Returns 0 if the call was successful, or -1 in case of error | 
|  | 2358 | */ | 
|  | 2359 | int | 
|  | 2360 | xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) { | 
|  | 2361 | xmlParserProperties p = (xmlParserProperties) prop; | 
|  | 2362 | xmlParserCtxtPtr ctxt; | 
|  | 2363 |  | 
|  | 2364 | if ((reader == NULL) || (reader->ctxt == NULL)) | 
|  | 2365 | return(-1); | 
|  | 2366 | ctxt = reader->ctxt; | 
|  | 2367 |  | 
|  | 2368 | switch (p) { | 
|  | 2369 | case XML_PARSER_LOADDTD: | 
|  | 2370 | if (value != 0) { | 
|  | 2371 | if (ctxt->loadsubset == 0) { | 
|  | 2372 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) | 
|  | 2373 | return(-1); | 
|  | 2374 | ctxt->loadsubset = XML_DETECT_IDS; | 
|  | 2375 | } | 
|  | 2376 | } else { | 
|  | 2377 | ctxt->loadsubset = 0; | 
|  | 2378 | } | 
|  | 2379 | return(0); | 
|  | 2380 | case XML_PARSER_DEFAULTATTRS: | 
|  | 2381 | if (value != 0) { | 
|  | 2382 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; | 
|  | 2383 | } else { | 
|  | 2384 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) | 
|  | 2385 | ctxt->loadsubset -= XML_COMPLETE_ATTRS; | 
|  | 2386 | } | 
|  | 2387 | return(0); | 
|  | 2388 | case XML_PARSER_VALIDATE: | 
|  | 2389 | if (value != 0) { | 
|  | 2390 | ctxt->validate = 1; | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2391 | reader->validate = XML_TEXTREADER_VALIDATE_DTD; | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2392 | } else { | 
|  | 2393 | ctxt->validate = 0; | 
|  | 2394 | } | 
|  | 2395 | return(0); | 
| Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2396 | case XML_PARSER_SUBST_ENTITIES: | 
|  | 2397 | if (value != 0) { | 
|  | 2398 | ctxt->replaceEntities = 1; | 
|  | 2399 | } else { | 
|  | 2400 | ctxt->replaceEntities = 0; | 
|  | 2401 | } | 
|  | 2402 | return(0); | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2403 | } | 
|  | 2404 | return(-1); | 
|  | 2405 | } | 
|  | 2406 |  | 
|  | 2407 | /** | 
|  | 2408 | * xmlTextReaderGetParserProp: | 
|  | 2409 | * @reader:  the xmlTextReaderPtr used | 
|  | 2410 | * @prop:  the xmlParserProperties to get | 
|  | 2411 | * | 
|  | 2412 | * Read the parser internal property. | 
|  | 2413 | * | 
|  | 2414 | * Returns the value, usually 0 or 1, or -1 in case of error. | 
|  | 2415 | */ | 
|  | 2416 | int | 
|  | 2417 | xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) { | 
|  | 2418 | xmlParserProperties p = (xmlParserProperties) prop; | 
|  | 2419 | xmlParserCtxtPtr ctxt; | 
|  | 2420 |  | 
|  | 2421 | if ((reader == NULL) || (reader->ctxt == NULL)) | 
|  | 2422 | return(-1); | 
|  | 2423 | ctxt = reader->ctxt; | 
|  | 2424 |  | 
|  | 2425 | switch (p) { | 
|  | 2426 | case XML_PARSER_LOADDTD: | 
|  | 2427 | if ((ctxt->loadsubset != 0) || (ctxt->validate != 0)) | 
|  | 2428 | return(1); | 
|  | 2429 | return(0); | 
|  | 2430 | case XML_PARSER_DEFAULTATTRS: | 
|  | 2431 | if (ctxt->loadsubset & XML_COMPLETE_ATTRS) | 
|  | 2432 | return(1); | 
|  | 2433 | return(0); | 
|  | 2434 | case XML_PARSER_VALIDATE: | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2435 | return(reader->validate); | 
| Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2436 | case XML_PARSER_SUBST_ENTITIES: | 
|  | 2437 | return(ctxt->replaceEntities); | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2438 | } | 
|  | 2439 | return(-1); | 
|  | 2440 | } | 
|  | 2441 |  | 
| Daniel Veillard | e18fc18 | 2002-12-28 22:56:33 +0000 | [diff] [blame] | 2442 | /** | 
|  | 2443 | * xmlTextReaderCurrentNode: | 
|  | 2444 | * @reader:  the xmlTextReaderPtr used | 
|  | 2445 | * | 
|  | 2446 | * Hacking interface allowing to get the xmlNodePtr correponding to the | 
|  | 2447 | * current node being accessed by the xmlTextReader. This is dangerous | 
|  | 2448 | * because the underlying node may be destroyed on the next Reads. | 
|  | 2449 | * | 
|  | 2450 | * Returns the xmlNodePtr or NULL in case of error. | 
|  | 2451 | */ | 
|  | 2452 | xmlNodePtr | 
|  | 2453 | xmlTextReaderCurrentNode(xmlTextReaderPtr reader) { | 
|  | 2454 | if (reader == NULL) | 
|  | 2455 | return(NULL); | 
|  | 2456 |  | 
|  | 2457 | if (reader->curnode != NULL) | 
|  | 2458 | return(reader->curnode); | 
|  | 2459 | return(reader->node); | 
|  | 2460 | } | 
|  | 2461 |  | 
|  | 2462 | /** | 
|  | 2463 | * xmlTextReaderCurrentDoc: | 
|  | 2464 | * @reader:  the xmlTextReaderPtr used | 
|  | 2465 | * | 
|  | 2466 | * Hacking interface allowing to get the xmlDocPtr correponding to the | 
|  | 2467 | * current document being accessed by the xmlTextReader. This is dangerous | 
|  | 2468 | * because the associated node may be destroyed on the next Reads. | 
|  | 2469 | * | 
|  | 2470 | * Returns the xmlDocPtr or NULL in case of error. | 
|  | 2471 | */ | 
|  | 2472 | xmlDocPtr | 
|  | 2473 | xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) { | 
|  | 2474 | if ((reader == NULL) || (reader->ctxt == NULL)) | 
|  | 2475 | return(NULL); | 
|  | 2476 |  | 
|  | 2477 | return(reader->ctxt->myDoc); | 
|  | 2478 | } | 
|  | 2479 |  | 
| Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 2480 | #ifdef LIBXML_SCHEMAS_ENABLED | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2481 | /** | 
| Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 2482 | * xmlTextReaderRelaxNGSetSchema: | 
|  | 2483 | * @reader:  the xmlTextReaderPtr used | 
|  | 2484 | * @schema:  a precompiled RelaxNG schema | 
|  | 2485 | * | 
|  | 2486 | * Use RelaxNG to validate the document as it is processed. | 
|  | 2487 | * Activation is only possible before the first Read(). | 
|  | 2488 | * if @schema is NULL, then RelaxNG validation is desactivated. | 
|  | 2489 | @ The @schema should not be freed until the reader is deallocated | 
|  | 2490 | * or its use has been deactivated. | 
|  | 2491 | * | 
|  | 2492 | * Returns 0 in case the RelaxNG validation could be (des)activated and | 
|  | 2493 | *         -1 in case of error. | 
|  | 2494 | */ | 
|  | 2495 | int | 
|  | 2496 | xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) { | 
|  | 2497 | if (schema == NULL) { | 
|  | 2498 | if (reader->rngSchemas != NULL) { | 
|  | 2499 | xmlRelaxNGFree(reader->rngSchemas); | 
|  | 2500 | reader->rngSchemas = NULL; | 
|  | 2501 | } | 
|  | 2502 | if (reader->rngValidCtxt != NULL) { | 
|  | 2503 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); | 
|  | 2504 | reader->rngValidCtxt = NULL; | 
|  | 2505 | } | 
|  | 2506 | return(0); | 
|  | 2507 | } | 
|  | 2508 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) | 
|  | 2509 | return(-1); | 
|  | 2510 | if (reader->rngSchemas != NULL) { | 
|  | 2511 | xmlRelaxNGFree(reader->rngSchemas); | 
|  | 2512 | reader->rngSchemas = NULL; | 
|  | 2513 | } | 
|  | 2514 | if (reader->rngValidCtxt != NULL) { | 
|  | 2515 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); | 
|  | 2516 | reader->rngValidCtxt = NULL; | 
|  | 2517 | } | 
|  | 2518 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema); | 
|  | 2519 | if (reader->rngValidCtxt == NULL) | 
|  | 2520 | return(-1); | 
|  | 2521 | if (reader->errorFunc != NULL) { | 
|  | 2522 | xmlRelaxNGSetValidErrors(reader->rngValidCtxt, | 
|  | 2523 | (xmlRelaxNGValidityErrorFunc)reader->errorFunc, | 
|  | 2524 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, | 
|  | 2525 | reader->errorFuncArg); | 
|  | 2526 | } | 
|  | 2527 | reader->rngValidErrors = 0; | 
|  | 2528 | reader->rngFullNode = NULL; | 
|  | 2529 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; | 
|  | 2530 | return(0); | 
|  | 2531 | } | 
|  | 2532 |  | 
|  | 2533 | /** | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2534 | * xmlTextReaderRelaxNGValidate: | 
|  | 2535 | * @reader:  the xmlTextReaderPtr used | 
|  | 2536 | * @rng:  the path to a RelaxNG schema or NULL | 
|  | 2537 | * | 
|  | 2538 | * Use RelaxNG to validate the document as it is processed. | 
|  | 2539 | * Activation is only possible before the first Read(). | 
|  | 2540 | * if @rng is NULL, then RelaxNG validation is desactivated. | 
|  | 2541 | * | 
|  | 2542 | * Returns 0 in case the RelaxNG validation could be (des)activated and | 
|  | 2543 | *         -1 in case of error. | 
|  | 2544 | */ | 
|  | 2545 | int | 
|  | 2546 | xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) { | 
|  | 2547 | xmlRelaxNGParserCtxtPtr ctxt; | 
|  | 2548 |  | 
|  | 2549 | if (reader == NULL) | 
|  | 2550 | return(-1); | 
|  | 2551 |  | 
|  | 2552 | if (rng == NULL) { | 
|  | 2553 | if (reader->rngSchemas != NULL) { | 
|  | 2554 | xmlRelaxNGFree(reader->rngSchemas); | 
|  | 2555 | reader->rngSchemas = NULL; | 
|  | 2556 | } | 
|  | 2557 | if (reader->rngValidCtxt != NULL) { | 
|  | 2558 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); | 
|  | 2559 | reader->rngValidCtxt = NULL; | 
|  | 2560 | } | 
|  | 2561 | return(0); | 
|  | 2562 | } | 
|  | 2563 | if (reader->mode != XML_TEXTREADER_MODE_INITIAL) | 
|  | 2564 | return(-1); | 
| Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 2565 | if (reader->rngSchemas != NULL) { | 
|  | 2566 | xmlRelaxNGFree(reader->rngSchemas); | 
|  | 2567 | reader->rngSchemas = NULL; | 
|  | 2568 | } | 
|  | 2569 | if (reader->rngValidCtxt != NULL) { | 
|  | 2570 | xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); | 
|  | 2571 | reader->rngValidCtxt = NULL; | 
|  | 2572 | } | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2573 | ctxt = xmlRelaxNGNewParserCtxt(rng); | 
|  | 2574 | if (reader->errorFunc != NULL) { | 
|  | 2575 | xmlRelaxNGSetParserErrors(ctxt, | 
|  | 2576 | (xmlRelaxNGValidityErrorFunc) reader->errorFunc, | 
|  | 2577 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, | 
|  | 2578 | reader->errorFuncArg); | 
|  | 2579 | } | 
|  | 2580 | reader->rngSchemas = xmlRelaxNGParse(ctxt); | 
|  | 2581 | xmlRelaxNGFreeParserCtxt(ctxt); | 
|  | 2582 | if (reader->rngSchemas == NULL) | 
|  | 2583 | return(-1); | 
|  | 2584 | reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas); | 
|  | 2585 | if (reader->rngValidCtxt == NULL) | 
|  | 2586 | return(-1); | 
|  | 2587 | if (reader->errorFunc != NULL) { | 
|  | 2588 | xmlRelaxNGSetValidErrors(reader->rngValidCtxt, | 
|  | 2589 | (xmlRelaxNGValidityErrorFunc)reader->errorFunc, | 
|  | 2590 | (xmlRelaxNGValidityWarningFunc) reader->errorFunc, | 
|  | 2591 | reader->errorFuncArg); | 
|  | 2592 | } | 
|  | 2593 | reader->rngValidErrors = 0; | 
|  | 2594 | reader->rngFullNode = NULL; | 
|  | 2595 | reader->validate = XML_TEXTREADER_VALIDATE_RNG; | 
|  | 2596 | return(0); | 
|  | 2597 | } | 
| Daniel Veillard | 37fc84d | 2003-05-09 19:38:15 +0000 | [diff] [blame] | 2598 | #endif | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2599 |  | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2600 | /************************************************************************ | 
|  | 2601 | *									* | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2602 | *			Error Handling Extensions                       * | 
|  | 2603 | *									* | 
|  | 2604 | ************************************************************************/ | 
|  | 2605 |  | 
|  | 2606 | /* helper to build a xmlMalloc'ed string from a format and va_list */ | 
|  | 2607 | static char * | 
|  | 2608 | xmlTextReaderBuildMessage(const char *msg, va_list ap) { | 
|  | 2609 | int size; | 
|  | 2610 | int chars; | 
|  | 2611 | char *larger; | 
|  | 2612 | char *str; | 
|  | 2613 |  | 
| Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2614 | str = (char *) xmlMallocAtomic(150); | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2615 | if (str == NULL) { | 
|  | 2616 | xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n"); | 
|  | 2617 | return NULL; | 
|  | 2618 | } | 
|  | 2619 |  | 
|  | 2620 | size = 150; | 
|  | 2621 |  | 
|  | 2622 | while (1) { | 
|  | 2623 | chars = vsnprintf(str, size, msg, ap); | 
|  | 2624 | if ((chars > -1) && (chars < size)) | 
|  | 2625 | break; | 
|  | 2626 | if (chars > -1) | 
|  | 2627 | size += chars + 1; | 
|  | 2628 | else | 
|  | 2629 | size += 100; | 
|  | 2630 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) { | 
|  | 2631 | xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n"); | 
|  | 2632 | xmlFree(str); | 
|  | 2633 | return NULL; | 
|  | 2634 | } | 
|  | 2635 | str = larger; | 
|  | 2636 | } | 
|  | 2637 |  | 
|  | 2638 | return str; | 
|  | 2639 | } | 
|  | 2640 |  | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2641 | /** | 
| Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 2642 | * xmlTextReaderLocatorLineNumber: | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2643 | * @locator: the xmlTextReaderLocatorPtr used | 
|  | 2644 | * | 
|  | 2645 | * Obtain the line number for the given locator. | 
|  | 2646 | * | 
|  | 2647 | * Returns the line number or -1 in case of error. | 
|  | 2648 | */ | 
|  | 2649 | int | 
|  | 2650 | xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) { | 
|  | 2651 | /* we know that locator is a xmlParserCtxtPtr */ | 
|  | 2652 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; | 
|  | 2653 | int ret = -1; | 
|  | 2654 |  | 
|  | 2655 | if (ctx->node != NULL) { | 
|  | 2656 | ret = xmlGetLineNo(ctx->node); | 
|  | 2657 | } | 
|  | 2658 | else { | 
|  | 2659 | /* inspired from error.c */ | 
|  | 2660 | xmlParserInputPtr input; | 
|  | 2661 | input = ctx->input; | 
|  | 2662 | if ((input->filename == NULL) && (ctx->inputNr > 1)) | 
|  | 2663 | input = ctx->inputTab[ctx->inputNr - 2]; | 
|  | 2664 | if (input != NULL) { | 
|  | 2665 | ret = input->line; | 
|  | 2666 | } | 
|  | 2667 | else { | 
|  | 2668 | ret = -1; | 
|  | 2669 | } | 
|  | 2670 | } | 
|  | 2671 |  | 
|  | 2672 | return ret; | 
|  | 2673 | } | 
|  | 2674 |  | 
|  | 2675 | /** | 
| Daniel Veillard | 540a31a | 2003-01-21 11:21:07 +0000 | [diff] [blame] | 2676 | * xmlTextReaderLocatorBaseURI: | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2677 | * @locator: the xmlTextReaderLocatorPtr used | 
|  | 2678 | * | 
|  | 2679 | * Obtain the base URI for the given locator. | 
|  | 2680 | * | 
|  | 2681 | * Returns the base URI or NULL in case of error. | 
|  | 2682 | */ | 
|  | 2683 | xmlChar * | 
|  | 2684 | xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) { | 
|  | 2685 | /* we know that locator is a xmlParserCtxtPtr */ | 
|  | 2686 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator; | 
|  | 2687 | xmlChar *ret = NULL; | 
|  | 2688 |  | 
|  | 2689 | if (ctx->node != NULL) { | 
|  | 2690 | ret = xmlNodeGetBase(NULL,ctx->node); | 
|  | 2691 | } | 
|  | 2692 | else { | 
|  | 2693 | /* inspired from error.c */ | 
|  | 2694 | xmlParserInputPtr input; | 
|  | 2695 | input = ctx->input; | 
|  | 2696 | if ((input->filename == NULL) && (ctx->inputNr > 1)) | 
|  | 2697 | input = ctx->inputTab[ctx->inputNr - 2]; | 
|  | 2698 | if (input != NULL) { | 
| Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2699 | ret = xmlStrdup(BAD_CAST input->filename); | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2700 | } | 
|  | 2701 | else { | 
|  | 2702 | ret = NULL; | 
|  | 2703 | } | 
|  | 2704 | } | 
|  | 2705 |  | 
|  | 2706 | return ret; | 
|  | 2707 | } | 
|  | 2708 |  | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2709 | static void | 
|  | 2710 | xmlTextReaderGenericError(void *ctxt, int severity, char *str) { | 
|  | 2711 | xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)ctxt; | 
|  | 2712 | xmlTextReaderPtr reader = (xmlTextReaderPtr)ctx->_private; | 
|  | 2713 |  | 
|  | 2714 | if (str != NULL) { | 
|  | 2715 | reader->errorFunc(reader->errorFuncArg, | 
|  | 2716 | str, | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2717 | severity, | 
|  | 2718 | (xmlTextReaderLocatorPtr)ctx); | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2719 | xmlFree(str); | 
|  | 2720 | } | 
|  | 2721 | } | 
|  | 2722 |  | 
|  | 2723 | static void | 
|  | 2724 | xmlTextReaderError(void *ctxt, const char *msg, ...) { | 
|  | 2725 | va_list ap; | 
|  | 2726 |  | 
|  | 2727 | va_start(ap,msg); | 
|  | 2728 | xmlTextReaderGenericError(ctxt, | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2729 | XML_PARSER_SEVERITY_ERROR, | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2730 | xmlTextReaderBuildMessage(msg,ap)); | 
|  | 2731 | va_end(ap); | 
|  | 2732 |  | 
|  | 2733 | } | 
|  | 2734 |  | 
|  | 2735 | static void | 
|  | 2736 | xmlTextReaderWarning(void *ctxt, const char *msg, ...) { | 
|  | 2737 | va_list ap; | 
|  | 2738 |  | 
|  | 2739 | va_start(ap,msg); | 
|  | 2740 | xmlTextReaderGenericError(ctxt, | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2741 | XML_PARSER_SEVERITY_WARNING, | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2742 | xmlTextReaderBuildMessage(msg,ap)); | 
|  | 2743 | va_end(ap); | 
|  | 2744 | } | 
|  | 2745 |  | 
|  | 2746 | static void | 
|  | 2747 | xmlTextReaderValidityError(void *ctxt, const char *msg, ...) { | 
|  | 2748 | va_list ap; | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2749 | int len = xmlStrlen((const xmlChar *) msg); | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2750 |  | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2751 | if ((len > 1) && (msg[len - 2] != ':')) { | 
|  | 2752 | /* | 
|  | 2753 | * some callbacks only report locator information: | 
|  | 2754 | * skip them (mimicking behaviour in error.c) | 
|  | 2755 | */ | 
|  | 2756 | va_start(ap,msg); | 
|  | 2757 | xmlTextReaderGenericError(ctxt, | 
|  | 2758 | XML_PARSER_SEVERITY_VALIDITY_ERROR, | 
|  | 2759 | xmlTextReaderBuildMessage(msg,ap)); | 
|  | 2760 | va_end(ap); | 
|  | 2761 | } | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2762 | } | 
|  | 2763 |  | 
|  | 2764 | static void | 
|  | 2765 | xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...) { | 
|  | 2766 | va_list ap; | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2767 | int len = xmlStrlen((const xmlChar *) msg); | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2768 |  | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2769 | if ((len != 0) && (msg[len - 1] != ':')) { | 
|  | 2770 | /* | 
|  | 2771 | * some callbacks only report locator information: | 
|  | 2772 | * skip them (mimicking behaviour in error.c) | 
|  | 2773 | */ | 
|  | 2774 | va_start(ap,msg); | 
|  | 2775 | xmlTextReaderGenericError(ctxt, | 
|  | 2776 | XML_PARSER_SEVERITY_VALIDITY_WARNING, | 
|  | 2777 | xmlTextReaderBuildMessage(msg,ap)); | 
|  | 2778 | va_end(ap); | 
|  | 2779 | } | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2780 | } | 
|  | 2781 |  | 
|  | 2782 | /** | 
|  | 2783 | * xmlTextReaderSetErrorHandler: | 
|  | 2784 | * @reader:  the xmlTextReaderPtr used | 
|  | 2785 | * @f:	the callback function to call on error and warnings | 
|  | 2786 | * @arg:    a user argument to pass to the callback function | 
|  | 2787 | * | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2788 | * Register a callback function that will be called on error and warnings. | 
|  | 2789 | * | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2790 | * If @f is NULL, the default error and warning handlers are restored. | 
|  | 2791 | */ | 
|  | 2792 | void | 
|  | 2793 | xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader, | 
|  | 2794 | xmlTextReaderErrorFunc f, | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2795 | void *arg) { | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2796 | if (f != NULL) { | 
|  | 2797 | reader->ctxt->sax->error = xmlTextReaderError; | 
|  | 2798 | reader->ctxt->vctxt.error = xmlTextReaderValidityError; | 
|  | 2799 | reader->ctxt->sax->warning = xmlTextReaderWarning; | 
|  | 2800 | reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning; | 
|  | 2801 | reader->errorFunc = f; | 
|  | 2802 | reader->errorFuncArg = arg; | 
|  | 2803 | } | 
|  | 2804 | else { | 
|  | 2805 | /* restore defaults */ | 
|  | 2806 | reader->ctxt->sax->error = xmlParserError; | 
|  | 2807 | reader->ctxt->vctxt.error = xmlParserValidityError; | 
|  | 2808 | reader->ctxt->sax->warning = xmlParserWarning; | 
|  | 2809 | reader->ctxt->vctxt.warning = xmlParserValidityWarning; | 
|  | 2810 | reader->errorFunc = NULL; | 
|  | 2811 | reader->errorFuncArg = NULL; | 
|  | 2812 | } | 
|  | 2813 | } | 
|  | 2814 |  | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2815 | /** | 
| Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 2816 | * xmlTextReaderIsValid: | 
|  | 2817 | * @reader:  the xmlTextReaderPtr used | 
|  | 2818 | * | 
|  | 2819 | * Retrieve the validity status from the parser context | 
|  | 2820 | * | 
|  | 2821 | * Returns the flag value 1 if valid, 0 if no, and -1 in case of error | 
|  | 2822 | */ | 
|  | 2823 | int | 
|  | 2824 | xmlTextReaderIsValid(xmlTextReaderPtr reader) { | 
| Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 2825 | if (reader == NULL) return(-1); | 
|  | 2826 | #ifdef LIBXML_SCHEMAS_ENABLED | 
|  | 2827 | if (reader->validate == XML_TEXTREADER_VALIDATE_RNG) | 
|  | 2828 | return(reader->rngValidErrors == 0); | 
|  | 2829 | #endif | 
|  | 2830 | if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) && | 
|  | 2831 | (reader->ctxt != NULL)) | 
|  | 2832 | return(reader->ctxt->valid); | 
|  | 2833 | return(0); | 
| Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 2834 | } | 
|  | 2835 |  | 
|  | 2836 | /** | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2837 | * xmlTextReaderGetErrorHandler: | 
|  | 2838 | * @reader:  the xmlTextReaderPtr used | 
|  | 2839 | * @f:	the callback function or NULL is no callback has been registered | 
|  | 2840 | * @arg:    a user argument | 
|  | 2841 | * | 
|  | 2842 | * Retrieve the error callback function and user argument. | 
|  | 2843 | */ | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2844 | void | 
|  | 2845 | xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader, | 
|  | 2846 | xmlTextReaderErrorFunc *f, | 
| Daniel Veillard | 417be3a | 2003-01-20 21:26:34 +0000 | [diff] [blame] | 2847 | void **arg) { | 
| Daniel Veillard | 26f7026 | 2003-01-16 22:45:08 +0000 | [diff] [blame] | 2848 | *f = reader->errorFunc; | 
|  | 2849 | *arg = reader->errorFuncArg; | 
|  | 2850 | } | 
|  | 2851 |  | 
|  | 2852 | /************************************************************************ | 
|  | 2853 | *									* | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2854 | *			Utilities					* | 
|  | 2855 | *									* | 
|  | 2856 | ************************************************************************/ | 
|  | 2857 | /** | 
|  | 2858 | * xmlBase64Decode: | 
|  | 2859 | * @in:  the input buffer | 
|  | 2860 | * @inlen:  the size of the input (in), the size read from it (out) | 
|  | 2861 | * @to:  the output buffer | 
|  | 2862 | * @tolen:  the size of the output (in), the size written to (out) | 
|  | 2863 | * | 
|  | 2864 | * Base64 decoder, reads from @in and save in @to | 
| Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2865 | * TODO: tell jody when this is actually exported | 
| Daniel Veillard | beb70bd | 2002-12-18 14:53:54 +0000 | [diff] [blame] | 2866 | * | 
|  | 2867 | * Returns 0 if all the input was consumer, 1 if the Base64 end was reached, | 
|  | 2868 | *         2 if there wasn't enough space on the output or -1 in case of error. | 
|  | 2869 | */ | 
|  | 2870 | static int | 
|  | 2871 | xmlBase64Decode(const unsigned char *in, unsigned long *inlen, | 
|  | 2872 | unsigned char *to, unsigned long *tolen) { | 
|  | 2873 | unsigned long incur;		/* current index in in[] */ | 
|  | 2874 | unsigned long inblk;		/* last block index in in[] */ | 
|  | 2875 | unsigned long outcur;		/* current index in out[] */ | 
|  | 2876 | unsigned long inmax;		/* size of in[] */ | 
|  | 2877 | unsigned long outmax;		/* size of out[] */ | 
|  | 2878 | unsigned char cur;			/* the current value read from in[] */ | 
|  | 2879 | unsigned char intmp[3], outtmp[4];	/* temporary buffers for the convert */ | 
|  | 2880 | int nbintmp;			/* number of byte in intmp[] */ | 
|  | 2881 | int is_ignore;			/* cur should be ignored */ | 
|  | 2882 | int is_end = 0;			/* the end of the base64 was found */ | 
|  | 2883 | int retval = 1; | 
|  | 2884 | int i; | 
|  | 2885 |  | 
|  | 2886 | if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL)) | 
|  | 2887 | return(-1); | 
|  | 2888 |  | 
|  | 2889 | incur = 0; | 
|  | 2890 | inblk = 0; | 
|  | 2891 | outcur = 0; | 
|  | 2892 | inmax = *inlen; | 
|  | 2893 | outmax = *tolen; | 
|  | 2894 | nbintmp = 0; | 
|  | 2895 |  | 
|  | 2896 | while (1) { | 
|  | 2897 | if (incur >= inmax) | 
|  | 2898 | break; | 
|  | 2899 | cur = in[incur++]; | 
|  | 2900 | is_ignore = 0; | 
|  | 2901 | if ((cur >= 'A') && (cur <= 'Z')) | 
|  | 2902 | cur = cur - 'A'; | 
|  | 2903 | else if ((cur >= 'a') && (cur <= 'z')) | 
|  | 2904 | cur = cur - 'a' + 26; | 
|  | 2905 | else if ((cur >= '0') && (cur <= '9')) | 
|  | 2906 | cur = cur - '0' + 52; | 
|  | 2907 | else if (cur == '+') | 
|  | 2908 | cur = 62; | 
|  | 2909 | else if (cur == '/') | 
|  | 2910 | cur = 63; | 
|  | 2911 | else if (cur == '.') | 
|  | 2912 | cur = 0; | 
|  | 2913 | else if (cur == '=') /*no op , end of the base64 stream */ | 
|  | 2914 | is_end = 1; | 
|  | 2915 | else { | 
|  | 2916 | is_ignore = 1; | 
|  | 2917 | if (nbintmp == 0) | 
|  | 2918 | inblk = incur; | 
|  | 2919 | } | 
|  | 2920 |  | 
|  | 2921 | if (!is_ignore) { | 
|  | 2922 | int nbouttmp = 3; | 
|  | 2923 | int is_break = 0; | 
|  | 2924 |  | 
|  | 2925 | if (is_end) { | 
|  | 2926 | if (nbintmp == 0) | 
|  | 2927 | break; | 
|  | 2928 | if ((nbintmp == 1) || (nbintmp == 2)) | 
|  | 2929 | nbouttmp = 1; | 
|  | 2930 | else | 
|  | 2931 | nbouttmp = 2; | 
|  | 2932 | nbintmp = 3; | 
|  | 2933 | is_break = 1; | 
|  | 2934 | } | 
|  | 2935 | intmp[nbintmp++] = cur; | 
|  | 2936 | /* | 
|  | 2937 | * if intmp is full, push the 4byte sequence as a 3 byte | 
|  | 2938 | * sequence out | 
|  | 2939 | */ | 
|  | 2940 | if (nbintmp == 4) { | 
|  | 2941 | nbintmp = 0; | 
|  | 2942 | outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4); | 
|  | 2943 | outtmp[1] = | 
|  | 2944 | ((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2); | 
|  | 2945 | outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F); | 
|  | 2946 | if (outcur + 3 >= outmax) { | 
|  | 2947 | retval = 2; | 
|  | 2948 | break; | 
|  | 2949 | } | 
|  | 2950 |  | 
|  | 2951 | for (i = 0; i < nbouttmp; i++) | 
|  | 2952 | to[outcur++] = outtmp[i]; | 
|  | 2953 | inblk = incur; | 
|  | 2954 | } | 
|  | 2955 |  | 
|  | 2956 | if (is_break) { | 
|  | 2957 | retval = 0; | 
|  | 2958 | break; | 
|  | 2959 | } | 
|  | 2960 | } | 
|  | 2961 | } | 
|  | 2962 |  | 
|  | 2963 | *tolen = outcur; | 
|  | 2964 | *inlen = inblk; | 
|  | 2965 | return (retval); | 
|  | 2966 | } | 
|  | 2967 |  | 
|  | 2968 | /* | 
|  | 2969 | * Test routine for the xmlBase64Decode function | 
|  | 2970 | */ | 
|  | 2971 | #if 0 | 
|  | 2972 | int main(int argc, char **argv) { | 
|  | 2973 | char *input = "  VW4 gcGV0        \n      aXQgdGVzdCAuCg== "; | 
|  | 2974 | char output[100]; | 
|  | 2975 | char output2[100]; | 
|  | 2976 | char output3[100]; | 
|  | 2977 | unsigned long inlen = strlen(input); | 
|  | 2978 | unsigned long outlen = 100; | 
|  | 2979 | int ret; | 
|  | 2980 | unsigned long cons, tmp, tmp2, prod; | 
|  | 2981 |  | 
|  | 2982 | /* | 
|  | 2983 | * Direct | 
|  | 2984 | */ | 
|  | 2985 | ret = xmlBase64Decode(input, &inlen, output, &outlen); | 
|  | 2986 |  | 
|  | 2987 | output[outlen] = 0; | 
|  | 2988 | printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen, outlen, output); | 
|  | 2989 |  | 
|  | 2990 | /* | 
|  | 2991 | * output chunking | 
|  | 2992 | */ | 
|  | 2993 | cons = 0; | 
|  | 2994 | prod = 0; | 
|  | 2995 | while (cons < inlen) { | 
|  | 2996 | tmp = 5; | 
|  | 2997 | tmp2 = inlen - cons; | 
|  | 2998 |  | 
|  | 2999 | printf("%ld %ld\n", cons, prod); | 
|  | 3000 | ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp); | 
|  | 3001 | cons += tmp2; | 
|  | 3002 | prod += tmp; | 
|  | 3003 | printf("%ld %ld\n", cons, prod); | 
|  | 3004 | } | 
|  | 3005 | output2[outlen] = 0; | 
|  | 3006 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output2); | 
|  | 3007 |  | 
|  | 3008 | /* | 
|  | 3009 | * input chunking | 
|  | 3010 | */ | 
|  | 3011 | cons = 0; | 
|  | 3012 | prod = 0; | 
|  | 3013 | while (cons < inlen) { | 
|  | 3014 | tmp = 100 - prod; | 
|  | 3015 | tmp2 = inlen - cons; | 
|  | 3016 | if (tmp2 > 5) | 
|  | 3017 | tmp2 = 5; | 
|  | 3018 |  | 
|  | 3019 | printf("%ld %ld\n", cons, prod); | 
|  | 3020 | ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp); | 
|  | 3021 | cons += tmp2; | 
|  | 3022 | prod += tmp; | 
|  | 3023 | printf("%ld %ld\n", cons, prod); | 
|  | 3024 | } | 
|  | 3025 | output3[outlen] = 0; | 
|  | 3026 | printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons, prod, output3); | 
|  | 3027 | return(0); | 
|  | 3028 |  | 
|  | 3029 | } | 
|  | 3030 | #endif |