This source file includes following definitions.
- xmlFreeID
- xmlTextReaderRemoveID
- xmlTextReaderFreeProp
- xmlTextReaderFreePropList
- xmlTextReaderFreeNodeList
- xmlTextReaderFreeNode
- xmlTextReaderFreeIDTable
- xmlTextReaderFreeDoc
- xmlTextReaderDebug
- xmlTextReaderEntPush
- xmlTextReaderEntPop
- xmlTextReaderStartElement
- xmlTextReaderEndElement
- xmlTextReaderStartElementNs
- xmlTextReaderEndElementNs
- xmlTextReaderCharacters
- xmlTextReaderCDataBlock
- xmlTextReaderPushData
- xmlTextReaderValidatePush
- xmlTextReaderValidateCData
- xmlTextReaderValidatePop
- xmlTextReaderValidateEntity
- xmlTextReaderGetSuccessor
- xmlTextReaderDoExpand
- xmlTextReaderCollectSiblings
- xmlTextReaderRead
- xmlTextReaderReadState
- xmlTextReaderExpand
- xmlTextReaderNext
- xmlTextReaderReadInnerXml
- xmlTextReaderReadOuterXml
- xmlTextReaderReadString
- xmlTextReaderReadBase64
- xmlTextReaderReadBinHex
- xmlTextReaderNextTree
- xmlTextReaderReadTree
- xmlTextReaderNextSibling
- xmlNewTextReader
- xmlNewTextReaderFilename
- xmlFreeTextReader
- xmlTextReaderClose
- xmlTextReaderGetAttributeNo
- xmlTextReaderGetAttribute
- xmlTextReaderGetAttributeNs
- xmlTextReaderGetRemainder
- xmlTextReaderLookupNamespace
- xmlTextReaderMoveToAttributeNo
- xmlTextReaderMoveToAttribute
- xmlTextReaderMoveToAttributeNs
- xmlTextReaderMoveToFirstAttribute
- xmlTextReaderMoveToNextAttribute
- xmlTextReaderMoveToElement
- xmlTextReaderReadAttributeValue
- xmlTextReaderConstEncoding
- xmlTextReaderAttributeCount
- xmlTextReaderNodeType
- xmlTextReaderIsEmptyElement
- xmlTextReaderLocalName
- xmlTextReaderConstLocalName
- xmlTextReaderName
- xmlTextReaderConstName
- xmlTextReaderPrefix
- xmlTextReaderConstPrefix
- xmlTextReaderNamespaceUri
- xmlTextReaderConstNamespaceUri
- xmlTextReaderBaseUri
- xmlTextReaderConstBaseUri
- xmlTextReaderDepth
- xmlTextReaderHasAttributes
- xmlTextReaderHasValue
- xmlTextReaderValue
- xmlTextReaderConstValue
- xmlTextReaderIsDefault
- xmlTextReaderQuoteChar
- xmlTextReaderXmlLang
- xmlTextReaderConstXmlLang
- xmlTextReaderConstString
- xmlTextReaderNormalization
- xmlTextReaderSetParserProp
- xmlTextReaderGetParserProp
- xmlTextReaderGetParserLineNumber
- xmlTextReaderGetParserColumnNumber
- xmlTextReaderCurrentNode
- xmlTextReaderPreserve
- xmlTextReaderPreservePattern
- xmlTextReaderCurrentDoc
- xmlTextReaderValidityErrorRelay
- xmlTextReaderValidityWarningRelay
- xmlTextReaderValidityStructuredRelay
- xmlTextReaderRelaxNGSetSchema
- xmlTextReaderSetSchema
- xmlTextReaderRelaxNGValidate
- xmlTextReaderSchemaValidateInternal
- xmlTextReaderSchemaValidateCtxt
- xmlTextReaderSchemaValidate
- xmlTextReaderIsNamespaceDecl
- xmlTextReaderConstXmlVersion
- xmlTextReaderStandalone
- xmlTextReaderBuildMessage
- xmlTextReaderLocatorLineNumber
- xmlTextReaderLocatorBaseURI
- xmlTextReaderGenericError
- xmlTextReaderStructuredError
- xmlTextReaderError
- xmlTextReaderWarning
- xmlTextReaderValidityError
- xmlTextReaderValidityWarning
- xmlTextReaderSetErrorHandler
- xmlTextReaderSetStructuredErrorHandler
- xmlTextReaderIsValid
- xmlTextReaderGetErrorHandler
- xmlTextReaderSetup
- xmlTextReaderByteConsumed
- xmlReaderWalker
- xmlReaderForDoc
- xmlReaderForFile
- xmlReaderForMemory
- xmlReaderForFd
- xmlReaderForIO
- xmlReaderNewWalker
- xmlReaderNewDoc
- xmlReaderNewFile
- xmlReaderNewMemory
- xmlReaderNewFd
- xmlReaderNewIO
- xmlBase64Decode
- main
#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_READER_ENABLED
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <libxml/xmlmemory.h>
#include <libxml/xmlIO.h>
#include <libxml/xmlreader.h>
#include <libxml/parserInternals.h>
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
#endif
#include <libxml/uri.h>
#ifdef LIBXML_XINCLUDE_ENABLED
#include <libxml/xinclude.h>
#endif
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
#define MAX_ERR_MSG_SIZE 64000
#ifndef VA_COPY
#ifdef HAVE_VA_COPY
#define VA_COPY(dest, src) va_copy(dest, src)
#else
#ifdef HAVE___VA_COPY
#define VA_COPY(dest,src) __va_copy(dest, src)
#else
#define VA_COPY(dest,src) (dest) = (src)
#endif
#endif
#endif
#define TODO \
xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \
__FILE__, __LINE__);
#ifdef DEBUG_READER
#define DUMP_READER xmlTextReaderDebug(reader);
#else
#define DUMP_READER
#endif
#define CHUNK_SIZE 512
#define XML_TEXTREADER_INPUT 1
#define XML_TEXTREADER_CTXT 2
typedef enum {
XML_TEXTREADER_NONE = -1,
XML_TEXTREADER_START= 0,
XML_TEXTREADER_ELEMENT= 1,
XML_TEXTREADER_END= 2,
XML_TEXTREADER_EMPTY= 3,
XML_TEXTREADER_BACKTRACK= 4,
XML_TEXTREADER_DONE= 5,
XML_TEXTREADER_ERROR= 6
} xmlTextReaderState;
typedef enum {
XML_TEXTREADER_NOT_VALIDATE = 0,
XML_TEXTREADER_VALIDATE_DTD = 1,
XML_TEXTREADER_VALIDATE_RNG = 2,
XML_TEXTREADER_VALIDATE_XSD = 4
} xmlTextReaderValidate;
struct _xmlTextReader {
int mode;
xmlDocPtr doc;
xmlTextReaderValidate validate;
int allocs;
xmlTextReaderState state;
xmlParserCtxtPtr ctxt;
xmlSAXHandlerPtr sax;
xmlParserInputBufferPtr input;
startElementSAXFunc startElement;
endElementSAXFunc endElement;
startElementNsSAX2Func startElementNs;
endElementNsSAX2Func endElementNs;
charactersSAXFunc characters;
cdataBlockSAXFunc cdataBlock;
unsigned int base;
unsigned int cur;
xmlNodePtr node;
xmlNodePtr curnode;
int depth;
xmlNodePtr faketext;
int preserve;
xmlBufferPtr buffer;
xmlDictPtr dict;
xmlNodePtr ent;
int entNr;
int entMax;
xmlNodePtr *entTab;
xmlTextReaderErrorFunc errorFunc;
void *errorFuncArg;
#ifdef LIBXML_SCHEMAS_ENABLED
xmlRelaxNGPtr rngSchemas;
xmlRelaxNGValidCtxtPtr rngValidCtxt;
int rngValidErrors;
xmlNodePtr rngFullNode;
xmlSchemaPtr xsdSchemas;
xmlSchemaValidCtxtPtr xsdValidCtxt;
int xsdPreserveCtxt;
int xsdValidErrors;
xmlSchemaSAXPlugPtr xsdPlug;
#endif
#ifdef LIBXML_XINCLUDE_ENABLED
int xinclude;
const xmlChar * xinclude_name;
xmlXIncludeCtxtPtr xincctxt;
int in_xinclude;
#endif
#ifdef LIBXML_PATTERN_ENABLED
int patternNr;
int patternMax;
xmlPatternPtr *patternTab;
#endif
int preserves;
int parserFlags;
xmlStructuredErrorFunc sErrorFunc;
};
#define NODE_IS_EMPTY 0x1
#define NODE_IS_PRESERVED 0x2
#define NODE_IS_SPRESERVED 0x4
#define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
#define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
#define DICT_FREE(str) \
if ((str) && ((!dict) || \
(xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
xmlFree((char *)(str));
static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
static void
xmlFreeID(xmlIDPtr id) {
xmlDictPtr dict = NULL;
if (id == NULL) return;
if (id->doc != NULL)
dict = id->doc->dict;
if (id->value != NULL)
DICT_FREE(id->value)
xmlFree(id);
}
static int
xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
xmlIDTablePtr table;
xmlIDPtr id;
xmlChar *ID;
if (doc == NULL) return(-1);
if (attr == NULL) return(-1);
table = (xmlIDTablePtr) doc->ids;
if (table == NULL)
return(-1);
ID = xmlNodeListGetString(doc, attr->children, 1);
if (ID == NULL)
return(-1);
id = xmlHashLookup(table, ID);
xmlFree(ID);
if (id == NULL || id->attr != attr) {
return(-1);
}
id->name = attr->name;
id->attr = NULL;
return(0);
}
static void
xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
xmlDictPtr dict;
dict = reader->ctxt->dict;
if (cur == NULL) return;
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
((cur->parent->doc->intSubset != NULL) ||
(cur->parent->doc->extSubset != NULL))) {
if (xmlIsID(cur->parent->doc, cur->parent, cur))
xmlTextReaderRemoveID(cur->parent->doc, cur);
}
if (cur->children != NULL)
xmlTextReaderFreeNodeList(reader, cur->children);
DICT_FREE(cur->name);
if ((reader != NULL) && (reader->ctxt != NULL) &&
(reader->ctxt->freeAttrsNr < 100)) {
cur->next = reader->ctxt->freeAttrs;
reader->ctxt->freeAttrs = cur;
reader->ctxt->freeAttrsNr++;
} else {
xmlFree(cur);
}
}
static void
xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
xmlAttrPtr next;
if (cur == NULL) return;
while (cur != NULL) {
next = cur->next;
xmlTextReaderFreeProp(reader, cur);
cur = next;
}
}
static void
xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
xmlNodePtr next;
xmlDictPtr dict;
dict = reader->ctxt->dict;
if (cur == NULL) return;
if (cur->type == XML_NAMESPACE_DECL) {
xmlFreeNsList((xmlNsPtr) cur);
return;
}
if ((cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE)) {
xmlFreeDoc((xmlDocPtr) cur);
return;
}
while (cur != NULL) {
next = cur->next;
if (cur->type != XML_DTD_NODE) {
if ((cur->children != NULL) &&
(cur->type != XML_ENTITY_REF_NODE)) {
if (cur->children->parent == cur)
xmlTextReaderFreeNodeList(reader, cur->children);
cur->children = NULL;
}
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue(cur);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->properties != NULL))
xmlTextReaderFreePropList(reader, cur->properties);
if ((cur->content != (xmlChar *) &(cur->properties)) &&
(cur->type != XML_ELEMENT_NODE) &&
(cur->type != XML_XINCLUDE_START) &&
(cur->type != XML_XINCLUDE_END) &&
(cur->type != XML_ENTITY_REF_NODE)) {
DICT_FREE(cur->content);
}
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->nsDef != NULL))
xmlFreeNsList(cur->nsDef);
if ((cur->type != XML_TEXT_NODE) &&
(cur->type != XML_COMMENT_NODE))
DICT_FREE(cur->name);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_TEXT_NODE)) &&
(reader != NULL) && (reader->ctxt != NULL) &&
(reader->ctxt->freeElemsNr < 100)) {
cur->next = reader->ctxt->freeElems;
reader->ctxt->freeElems = cur;
reader->ctxt->freeElemsNr++;
} else {
xmlFree(cur);
}
}
cur = next;
}
}
static void
xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
xmlDictPtr dict;
dict = reader->ctxt->dict;
if (cur->type == XML_DTD_NODE) {
xmlFreeDtd((xmlDtdPtr) cur);
return;
}
if (cur->type == XML_NAMESPACE_DECL) {
xmlFreeNs((xmlNsPtr) cur);
return;
}
if (cur->type == XML_ATTRIBUTE_NODE) {
xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
return;
}
if ((cur->children != NULL) &&
(cur->type != XML_ENTITY_REF_NODE)) {
if (cur->children->parent == cur)
xmlTextReaderFreeNodeList(reader, cur->children);
cur->children = NULL;
}
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue(cur);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->properties != NULL))
xmlTextReaderFreePropList(reader, cur->properties);
if ((cur->content != (xmlChar *) &(cur->properties)) &&
(cur->type != XML_ELEMENT_NODE) &&
(cur->type != XML_XINCLUDE_START) &&
(cur->type != XML_XINCLUDE_END) &&
(cur->type != XML_ENTITY_REF_NODE)) {
DICT_FREE(cur->content);
}
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->nsDef != NULL))
xmlFreeNsList(cur->nsDef);
if ((cur->type != XML_TEXT_NODE) &&
(cur->type != XML_COMMENT_NODE))
DICT_FREE(cur->name);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_TEXT_NODE)) &&
(reader != NULL) && (reader->ctxt != NULL) &&
(reader->ctxt->freeElemsNr < 100)) {
cur->next = reader->ctxt->freeElems;
reader->ctxt->freeElems = cur;
reader->ctxt->freeElemsNr++;
} else {
xmlFree(cur);
}
}
static void
xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
}
static void
xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
xmlDtdPtr extSubset, intSubset;
if (cur == NULL) return;
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
if (cur->ids != NULL) xmlTextReaderFreeIDTable((xmlIDTablePtr) cur->ids);
cur->ids = NULL;
if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
cur->refs = NULL;
extSubset = cur->extSubset;
intSubset = cur->intSubset;
if (intSubset == extSubset)
extSubset = NULL;
if (extSubset != NULL) {
xmlUnlinkNode((xmlNodePtr) cur->extSubset);
cur->extSubset = NULL;
xmlFreeDtd(extSubset);
}
if (intSubset != NULL) {
xmlUnlinkNode((xmlNodePtr) cur->intSubset);
cur->intSubset = NULL;
xmlFreeDtd(intSubset);
}
if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
if (cur->version != NULL) xmlFree((char *) cur->version);
if (cur->name != NULL) xmlFree((char *) cur->name);
if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
if (cur->URL != NULL) xmlFree((char *) cur->URL);
if (cur->dict != NULL) xmlDictFree(cur->dict);
xmlFree(cur);
}
#ifdef DEBUG_READER
static void
xmlTextReaderDebug(xmlTextReaderPtr reader) {
if ((reader == NULL) || (reader->ctxt == NULL)) {
fprintf(stderr, "xmlTextReader NULL\n");
return;
}
fprintf(stderr, "xmlTextReader: state %d depth %d ",
reader->state, reader->depth);
if (reader->node == NULL) {
fprintf(stderr, "node = NULL\n");
} else {
fprintf(stderr, "node %s\n", reader->node->name);
}
fprintf(stderr, " input: base %d, cur %d, depth %d: ",
reader->base, reader->cur, reader->ctxt->nodeNr);
if (reader->input->buffer == NULL) {
fprintf(stderr, "buffer is NULL\n");
} else {
#ifdef LIBXML_DEBUG_ENABLED
xmlDebugDumpString(stderr,
&reader->input->buffer->content[reader->cur]);
#endif
fprintf(stderr, "\n");
}
}
#endif
static int
xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
{
if (reader->entMax <= 0) {
reader->entMax = 10;
reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
sizeof(reader->entTab[0]));
if (reader->entTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
return (0);
}
}
if (reader->entNr >= reader->entMax) {
reader->entMax *= 2;
reader->entTab =
(xmlNodePtr *) xmlRealloc(reader->entTab,
reader->entMax *
sizeof(reader->entTab[0]));
if (reader->entTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
return (0);
}
}
reader->entTab[reader->entNr] = value;
reader->ent = value;
return (reader->entNr++);
}
static xmlNodePtr
xmlTextReaderEntPop(xmlTextReaderPtr reader)
{
xmlNodePtr ret;
if (reader->entNr <= 0)
return (NULL);
reader->entNr--;
if (reader->entNr > 0)
reader->ent = reader->entTab[reader->entNr - 1];
else
reader->ent = NULL;
ret = reader->entTab[reader->entNr];
reader->entTab[reader->entNr] = NULL;
return (ret);
}
static void
xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
const xmlChar **atts) {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderStartElement(%s)\n", fullname);
#endif
if ((reader != NULL) && (reader->startElement != NULL)) {
reader->startElement(ctx, fullname, atts);
if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
(ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
(ctxt->input->cur[1] == '>'))
ctxt->node->extra = NODE_IS_EMPTY;
}
if (reader != NULL)
reader->state = XML_TEXTREADER_ELEMENT;
}
static void
xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderEndElement(%s)\n", fullname);
#endif
if ((reader != NULL) && (reader->endElement != NULL)) {
reader->endElement(ctx, fullname);
}
}
static void
xmlTextReaderStartElementNs(void *ctx,
const xmlChar *localname,
const xmlChar *prefix,
const xmlChar *URI,
int nb_namespaces,
const xmlChar **namespaces,
int nb_attributes,
int nb_defaulted,
const xmlChar **attributes)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderStartElementNs(%s)\n", localname);
#endif
if ((reader != NULL) && (reader->startElementNs != NULL)) {
reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
namespaces, nb_attributes, nb_defaulted,
attributes);
if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
(ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
(ctxt->input->cur[1] == '>'))
ctxt->node->extra = NODE_IS_EMPTY;
}
if (reader != NULL)
reader->state = XML_TEXTREADER_ELEMENT;
}
static void
xmlTextReaderEndElementNs(void *ctx,
const xmlChar * localname,
const xmlChar * prefix,
const xmlChar * URI)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderEndElementNs(%s)\n", localname);
#endif
if ((reader != NULL) && (reader->endElementNs != NULL)) {
reader->endElementNs(ctx, localname, prefix, URI);
}
}
static void
xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderCharacters()\n");
#endif
if ((reader != NULL) && (reader->characters != NULL)) {
reader->characters(ctx, ch, len);
}
}
static void
xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderCDataBlock()\n");
#endif
if ((reader != NULL) && (reader->cdataBlock != NULL)) {
reader->cdataBlock(ctx, ch, len);
}
}
static int
xmlTextReaderPushData(xmlTextReaderPtr reader) {
xmlBufferPtr inbuf;
int val, s;
xmlTextReaderState oldstate;
if ((reader->input == NULL) || (reader->input->buffer == NULL))
return(-1);
oldstate = reader->state;
reader->state = XML_TEXTREADER_NONE;
inbuf = reader->input->buffer;
while (reader->state == XML_TEXTREADER_NONE) {
if (inbuf->use < reader->cur + CHUNK_SIZE) {
if (reader->mode != XML_TEXTREADER_MODE_EOF) {
val = xmlParserInputBufferRead(reader->input, 4096);
if ((val == 0) &&
(inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
if (inbuf->use == reader->cur) {
reader->mode = XML_TEXTREADER_MODE_EOF;
reader->state = oldstate;
}
} else if (val < 0) {
reader->mode = XML_TEXTREADER_MODE_EOF;
reader->state = oldstate;
if ((oldstate != XML_TEXTREADER_START) ||
(reader->ctxt->myDoc != NULL))
return(val);
} else if (val == 0) {
reader->mode = XML_TEXTREADER_MODE_EOF;
break;
}
} else
break;
}
if (inbuf->use >= reader->cur + CHUNK_SIZE) {
val = xmlParseChunk(reader->ctxt,
(const char *) &inbuf->content[reader->cur],
CHUNK_SIZE, 0);
reader->cur += CHUNK_SIZE;
if ((val != 0) || (reader->ctxt->wellFormed == 0))
return(-1);
} else {
s = inbuf->use - reader->cur;
val = xmlParseChunk(reader->ctxt,
(const char *) &inbuf->content[reader->cur],
s, 0);
reader->cur += s;
if ((val != 0) || (reader->ctxt->wellFormed == 0))
return(-1);
break;
}
}
if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
if (inbuf->alloc != XML_BUFFER_ALLOC_IMMUTABLE) {
if ((reader->cur >= 4096) &&
(inbuf->use - reader->cur <= CHUNK_SIZE)) {
val = xmlBufferShrink(inbuf, reader->cur);
if (val >= 0) {
reader->cur -= val;
}
}
}
}
else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
if (reader->state != XML_TEXTREADER_DONE) {
s = inbuf->use - reader->cur;
val = xmlParseChunk(reader->ctxt,
(const char *) &inbuf->content[reader->cur],
s, 1);
reader->cur = inbuf->use;
reader->state = XML_TEXTREADER_DONE;
if ((val != 0) || (reader->ctxt->wellFormed == 0))
return(-1);
}
}
reader->state = oldstate;
return(0);
}
#ifdef LIBXML_REGEXP_ENABLED
static void
xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
xmlNodePtr node = reader->node;
#ifdef LIBXML_VALID_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
(reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
reader->ctxt->myDoc, node, node->name);
} else {
xmlChar *qname;
qname = xmlStrdup(node->ns->prefix);
qname = xmlStrcat(qname, BAD_CAST ":");
qname = xmlStrcat(qname, node->name);
reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
reader->ctxt->myDoc, node, qname);
if (qname != NULL)
xmlFree(qname);
}
}
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
(reader->rngValidCtxt != NULL)) {
int ret;
if (reader->rngFullNode != NULL) return;
ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
reader->ctxt->myDoc,
node);
if (ret == 0) {
node = xmlTextReaderExpand(reader);
if (node == NULL) {
printf("Expand failed !\n");
ret = -1;
} else {
ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
reader->ctxt->myDoc,
node);
reader->rngFullNode = node;
}
}
if (ret != 1)
reader->rngValidErrors++;
}
#endif
}
static void
xmlTextReaderValidateCData(xmlTextReaderPtr reader,
const xmlChar *data, int len) {
#ifdef LIBXML_VALID_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
(reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
data, len);
}
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
(reader->rngValidCtxt != NULL)) {
int ret;
if (reader->rngFullNode != NULL) return;
ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
if (ret != 1)
reader->rngValidErrors++;
}
#endif
}
static void
xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
xmlNodePtr node = reader->node;
#ifdef LIBXML_VALID_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
(reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
reader->ctxt->myDoc, node, node->name);
} else {
xmlChar *qname;
qname = xmlStrdup(node->ns->prefix);
qname = xmlStrcat(qname, BAD_CAST ":");
qname = xmlStrcat(qname, node->name);
reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
reader->ctxt->myDoc, node, qname);
if (qname != NULL)
xmlFree(qname);
}
}
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
(reader->rngValidCtxt != NULL)) {
int ret;
if (reader->rngFullNode != NULL) {
if (node == reader->rngFullNode)
reader->rngFullNode = NULL;
return;
}
ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
reader->ctxt->myDoc,
node);
if (ret != 1)
reader->rngValidErrors++;
}
#endif
}
static void
xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
xmlNodePtr oldnode = reader->node;
xmlNodePtr node = reader->node;
xmlParserCtxtPtr ctxt = reader->ctxt;
do {
if (node->type == XML_ENTITY_REF_NODE) {
if ((node->children == NULL) && (ctxt->sax != NULL) &&
(ctxt->sax->getEntity != NULL)) {
node->children = (xmlNodePtr)
ctxt->sax->getEntity(ctxt, node->name);
}
if ((node->children != NULL) &&
(node->children->type == XML_ENTITY_DECL) &&
(node->children->children != NULL)) {
xmlTextReaderEntPush(reader, node);
node = node->children->children;
continue;
} else {
if (node == oldnode)
break;
node = node->next;
}
#ifdef LIBXML_REGEXP_ENABLED
} else if (node->type == XML_ELEMENT_NODE) {
reader->node = node;
xmlTextReaderValidatePush(reader);
} else if ((node->type == XML_TEXT_NODE) ||
(node->type == XML_CDATA_SECTION_NODE)) {
xmlTextReaderValidateCData(reader, node->content,
xmlStrlen(node->content));
#endif
}
if (node->children != NULL) {
node = node->children;
continue;
} else if (node->type == XML_ELEMENT_NODE) {
xmlTextReaderValidatePop(reader);
}
if (node->next != NULL) {
node = node->next;
continue;
}
do {
node = node->parent;
if (node->type == XML_ELEMENT_NODE) {
xmlNodePtr tmp;
if (reader->entNr == 0) {
while ((tmp = node->last) != NULL) {
if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
xmlUnlinkNode(tmp);
xmlTextReaderFreeNode(reader, tmp);
} else
break;
}
}
reader->node = node;
xmlTextReaderValidatePop(reader);
}
if ((node->type == XML_ENTITY_DECL) &&
(reader->ent != NULL) && (reader->ent->children == node)) {
node = xmlTextReaderEntPop(reader);
}
if (node == oldnode)
break;
if (node->next != NULL) {
node = node->next;
break;
}
} while ((node != NULL) && (node != oldnode));
} while ((node != NULL) && (node != oldnode));
reader->node = oldnode;
}
#endif
static xmlNodePtr
xmlTextReaderGetSuccessor(xmlNodePtr cur) {
if (cur == NULL) return(NULL) ;
if (cur->next != NULL) return(cur->next) ;
do {
cur = cur->parent;
if (cur == NULL) break;
if (cur->next != NULL) return(cur->next);
} while (cur != NULL);
return(cur);
}
static int
xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
int val;
if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
return(-1);
do {
if (reader->ctxt->instate == XML_PARSER_EOF) return(1);
if (xmlTextReaderGetSuccessor(reader->node) != NULL)
return(1);
if (reader->ctxt->nodeNr < reader->depth)
return(1);
if (reader->mode == XML_TEXTREADER_MODE_EOF)
return(1);
val = xmlTextReaderPushData(reader);
if (val < 0){
reader->mode = XML_TEXTREADER_MODE_ERROR;
return(-1);
}
} while(reader->mode != XML_TEXTREADER_MODE_EOF);
return(1);
}
static xmlChar *
xmlTextReaderCollectSiblings(xmlNodePtr node)
{
xmlBufferPtr buffer;
xmlChar *ret;
buffer = xmlBufferCreate();
if (buffer == NULL)
return NULL;
for ( ; node != NULL; node = node->next) {
switch (node->type) {
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
xmlBufferCat(buffer, node->content);
break;
case XML_ELEMENT_NODE: {
xmlChar *tmp;
tmp = xmlTextReaderCollectSiblings(node->children);
xmlBufferCat(buffer, tmp);
xmlFree(tmp);
break;
}
default:
break;
}
}
ret = buffer->content;
buffer->content = NULL;
xmlBufferFree(buffer);
return(ret);
}
int
xmlTextReaderRead(xmlTextReaderPtr reader) {
int val, olddepth = 0;
xmlTextReaderState oldstate = XML_TEXTREADER_START;
xmlNodePtr oldnode = NULL;
if (reader == NULL)
return(-1);
reader->curnode = NULL;
if (reader->doc != NULL)
return(xmlTextReaderReadTree(reader));
if (reader->ctxt == NULL)
return(-1);
if (reader->ctxt->wellFormed != 1)
return(-1);
#ifdef DEBUG_READER
fprintf(stderr, "\nREAD ");
DUMP_READER
#endif
if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
do {
val = xmlTextReaderPushData(reader);
if (val < 0){
reader->mode = XML_TEXTREADER_MODE_ERROR;
reader->state = XML_TEXTREADER_ERROR;
return(-1);
}
} while ((reader->ctxt->node == NULL) &&
((reader->mode != XML_TEXTREADER_MODE_EOF) &&
(reader->state != XML_TEXTREADER_DONE)));
if (reader->ctxt->node == NULL) {
if (reader->ctxt->myDoc != NULL) {
reader->node = reader->ctxt->myDoc->children;
}
if (reader->node == NULL){
reader->mode = XML_TEXTREADER_MODE_ERROR;
reader->state = XML_TEXTREADER_ERROR;
return(-1);
}
reader->state = XML_TEXTREADER_ELEMENT;
} else {
if (reader->ctxt->myDoc != NULL) {
reader->node = reader->ctxt->myDoc->children;
}
if (reader->node == NULL)
reader->node = reader->ctxt->nodeTab[0];
reader->state = XML_TEXTREADER_ELEMENT;
}
reader->depth = 0;
reader->ctxt->parseMode = XML_PARSE_READER;
goto node_found;
}
oldstate = reader->state;
olddepth = reader->ctxt->nodeNr;
oldnode = reader->node;
get_next_node:
if (reader->node == NULL) {
if (reader->mode == XML_TEXTREADER_MODE_EOF)
return(0);
else
return(-1);
}
while ((reader->node != NULL) && (reader->node->next == NULL) &&
(reader->ctxt->nodeNr == olddepth) &&
((oldstate == XML_TEXTREADER_BACKTRACK) ||
(reader->node->children == NULL) ||
(reader->node->type == XML_ENTITY_REF_NODE) ||
((reader->node->children != NULL) &&
(reader->node->children->type == XML_TEXT_NODE) &&
(reader->node->children->next == NULL)) ||
(reader->node->type == XML_DTD_NODE) ||
(reader->node->type == XML_DOCUMENT_NODE) ||
(reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
((reader->ctxt->node == NULL) ||
(reader->ctxt->node == reader->node) ||
(reader->ctxt->node == reader->node->parent)) &&
(reader->ctxt->instate != XML_PARSER_EOF)) {
val = xmlTextReaderPushData(reader);
if (val < 0){
reader->mode = XML_TEXTREADER_MODE_ERROR;
reader->state = XML_TEXTREADER_ERROR;
return(-1);
}
if (reader->node == NULL)
goto node_end;
}
if (oldstate != XML_TEXTREADER_BACKTRACK) {
if ((reader->node->children != NULL) &&
(reader->node->type != XML_ENTITY_REF_NODE) &&
(reader->node->type != XML_XINCLUDE_START) &&
(reader->node->type != XML_DTD_NODE)) {
reader->node = reader->node->children;
reader->depth++;
reader->state = XML_TEXTREADER_ELEMENT;
goto node_found;
}
}
if (reader->node->next != NULL) {
if ((oldstate == XML_TEXTREADER_ELEMENT) &&
(reader->node->type == XML_ELEMENT_NODE) &&
(reader->node->children == NULL) &&
((reader->node->extra & NODE_IS_EMPTY) == 0)
#ifdef LIBXML_XINCLUDE_ENABLED
&& (reader->in_xinclude <= 0)
#endif
) {
reader->state = XML_TEXTREADER_END;
goto node_found;
}
#ifdef LIBXML_REGEXP_ENABLED
if ((reader->validate) &&
(reader->node->type == XML_ELEMENT_NODE))
xmlTextReaderValidatePop(reader);
#endif
if ((reader->preserves > 0) &&
(reader->node->extra & NODE_IS_SPRESERVED))
reader->preserves--;
reader->node = reader->node->next;
reader->state = XML_TEXTREADER_ELEMENT;
if ((reader->preserves == 0) &&
#ifdef LIBXML_XINCLUDE_ENABLED
(reader->in_xinclude == 0) &&
#endif
(reader->entNr == 0) &&
(reader->node->prev != NULL) &&
(reader->node->prev->type != XML_DTD_NODE)) {
xmlNodePtr tmp = reader->node->prev;
if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
xmlUnlinkNode(tmp);
xmlTextReaderFreeNode(reader, tmp);
}
}
goto node_found;
}
if ((oldstate == XML_TEXTREADER_ELEMENT) &&
(reader->node->type == XML_ELEMENT_NODE) &&
(reader->node->children == NULL) &&
((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
reader->state = XML_TEXTREADER_END;
goto node_found;
}
#ifdef LIBXML_REGEXP_ENABLED
if ((reader->validate) && (reader->node->type == XML_ELEMENT_NODE))
xmlTextReaderValidatePop(reader);
#endif
if ((reader->preserves > 0) &&
(reader->node->extra & NODE_IS_SPRESERVED))
reader->preserves--;
reader->node = reader->node->parent;
if ((reader->node == NULL) ||
(reader->node->type == XML_DOCUMENT_NODE) ||
#ifdef LIBXML_DOCB_ENABLED
(reader->node->type == XML_DOCB_DOCUMENT_NODE) ||
#endif
(reader->node->type == XML_HTML_DOCUMENT_NODE)) {
if (reader->mode != XML_TEXTREADER_MODE_EOF) {
val = xmlParseChunk(reader->ctxt, "", 0, 1);
reader->state = XML_TEXTREADER_DONE;
if (val != 0)
return(-1);
}
reader->node = NULL;
reader->depth = -1;
if ((oldnode != NULL) && (reader->preserves == 0) &&
#ifdef LIBXML_XINCLUDE_ENABLED
(reader->in_xinclude == 0) &&
#endif
(reader->entNr == 0) &&
(oldnode->type != XML_DTD_NODE) &&
((oldnode->extra & NODE_IS_PRESERVED) == 0)) {
xmlUnlinkNode(oldnode);
xmlTextReaderFreeNode(reader, oldnode);
}
goto node_end;
}
if ((reader->preserves == 0) &&
#ifdef LIBXML_XINCLUDE_ENABLED
(reader->in_xinclude == 0) &&
#endif
(reader->entNr == 0) &&
(reader->node->last != NULL) &&
((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
xmlNodePtr tmp = reader->node->last;
xmlUnlinkNode(tmp);
xmlTextReaderFreeNode(reader, tmp);
}
reader->depth--;
reader->state = XML_TEXTREADER_BACKTRACK;
node_found:
DUMP_READER
if ((reader->node != NULL) &&
(reader->node->next == NULL) &&
((reader->node->type == XML_TEXT_NODE) ||
(reader->node->type == XML_CDATA_SECTION_NODE))) {
if (xmlTextReaderExpand(reader) == NULL)
return -1;
}
#ifdef LIBXML_XINCLUDE_ENABLED
if ((reader->xinclude) && (reader->node != NULL) &&
(reader->node->type == XML_ELEMENT_NODE) &&
(reader->node->ns != NULL) &&
((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) ||
(xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) {
if (reader->xincctxt == NULL) {
reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
xmlXIncludeSetFlags(reader->xincctxt,
reader->parserFlags & (~XML_PARSE_NOXINCNODE));
}
if (xmlTextReaderExpand(reader) == NULL)
return -1;
xmlXIncludeProcessNode(reader->xincctxt, reader->node);
}
if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_START)) {
reader->in_xinclude++;
goto get_next_node;
}
if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_END)) {
reader->in_xinclude--;
goto get_next_node;
}
#endif
if ((reader->node != NULL) &&
(reader->node->type == XML_ENTITY_REF_NODE) &&
(reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
if ((reader->node->children == NULL) && (reader->ctxt->sax != NULL) &&
(reader->ctxt->sax->getEntity != NULL)) {
reader->node->children = (xmlNodePtr)
reader->ctxt->sax->getEntity(reader->ctxt, reader->node->name);
}
if ((reader->node->children != NULL) &&
(reader->node->children->type == XML_ENTITY_DECL) &&
(reader->node->children->children != NULL)) {
xmlTextReaderEntPush(reader, reader->node);
reader->node = reader->node->children->children;
}
#ifdef LIBXML_REGEXP_ENABLED
} else if ((reader->node != NULL) &&
(reader->node->type == XML_ENTITY_REF_NODE) &&
(reader->ctxt != NULL) && (reader->validate)) {
xmlTextReaderValidateEntity(reader);
#endif
}
if ((reader->node != NULL) &&
(reader->node->type == XML_ENTITY_DECL) &&
(reader->ent != NULL) && (reader->ent->children == reader->node)) {
reader->node = xmlTextReaderEntPop(reader);
reader->depth++;
goto get_next_node;
}
#ifdef LIBXML_REGEXP_ENABLED
if ((reader->validate) && (reader->node != NULL)) {
xmlNodePtr node = reader->node;
if ((node->type == XML_ELEMENT_NODE) &&
((reader->state != XML_TEXTREADER_END) &&
(reader->state != XML_TEXTREADER_BACKTRACK))) {
xmlTextReaderValidatePush(reader);
} else if ((node->type == XML_TEXT_NODE) ||
(node->type == XML_CDATA_SECTION_NODE)) {
xmlTextReaderValidateCData(reader, node->content,
xmlStrlen(node->content));
}
}
#endif
#ifdef LIBXML_PATTERN_ENABLED
if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
(reader->state != XML_TEXTREADER_BACKTRACK)) {
int i;
for (i = 0;i < reader->patternNr;i++) {
if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
xmlTextReaderPreserve(reader);
break;
}
}
}
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_XSD) &&
(reader->xsdValidErrors == 0) &&
(reader->xsdValidCtxt != NULL)) {
reader->xsdValidErrors = !xmlSchemaIsValid(reader->xsdValidCtxt);
}
#endif
return(1);
node_end:
reader->state = XML_TEXTREADER_DONE;
return(0);
}
int
xmlTextReaderReadState(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
return(reader->mode);
}
xmlNodePtr
xmlTextReaderExpand(xmlTextReaderPtr reader) {
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->doc != NULL)
return(reader->node);
if (reader->ctxt == NULL)
return(NULL);
if (xmlTextReaderDoExpand(reader) < 0)
return(NULL);
return(reader->node);
}
int
xmlTextReaderNext(xmlTextReaderPtr reader) {
int ret;
xmlNodePtr cur;
if (reader == NULL)
return(-1);
if (reader->doc != NULL)
return(xmlTextReaderNextTree(reader));
cur = reader->node;
if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
return(xmlTextReaderRead(reader));
if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK)
return(xmlTextReaderRead(reader));
if (cur->extra & NODE_IS_EMPTY)
return(xmlTextReaderRead(reader));
do {
ret = xmlTextReaderRead(reader);
if (ret != 1)
return(ret);
} while (reader->node != cur);
return(xmlTextReaderRead(reader));
}
#ifdef LIBXML_WRITER_ENABLED
xmlChar *
xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED)
{
xmlChar *resbuf;
xmlNodePtr node, cur_node;
xmlBufferPtr buff, buff2;
xmlDocPtr doc;
if (xmlTextReaderExpand(reader) == NULL) {
return NULL;
}
doc = reader->doc;
buff = xmlBufferCreate();
for (cur_node = reader->node->children; cur_node != NULL;
cur_node = cur_node->next) {
node = xmlDocCopyNode(cur_node, doc, 1);
buff2 = xmlBufferCreate();
if (xmlNodeDump(buff2, doc, node, 0, 0) == -1) {
xmlFreeNode(node);
xmlBufferFree(buff2);
xmlBufferFree(buff);
return NULL;
}
xmlBufferCat(buff, buff2->content);
xmlFreeNode(node);
xmlBufferFree(buff2);
}
resbuf = buff->content;
buff->content = NULL;
xmlBufferFree(buff);
return resbuf;
}
#endif
#ifdef LIBXML_WRITER_ENABLED
xmlChar *
xmlTextReaderReadOuterXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED)
{
xmlChar *resbuf;
xmlNodePtr node;
xmlBufferPtr buff;
xmlDocPtr doc;
node = reader->node;
doc = reader->doc;
if (xmlTextReaderExpand(reader) == NULL) {
return NULL;
}
node = xmlDocCopyNode(node, doc, 1);
buff = xmlBufferCreate();
if (xmlNodeDump(buff, doc, node, 0, 0) == -1) {
xmlFreeNode(node);
xmlBufferFree(buff);
return NULL;
}
resbuf = buff->content;
buff->content = NULL;
xmlFreeNode(node);
xmlBufferFree(buff);
return resbuf;
}
#endif
xmlChar *
xmlTextReaderReadString(xmlTextReaderPtr reader)
{
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
node = (reader->curnode != NULL) ? reader->curnode : reader->node;
switch (node->type) {
case XML_TEXT_NODE:
if (node->content != NULL)
return(xmlStrdup(node->content));
break;
case XML_ELEMENT_NODE:
if (xmlTextReaderDoExpand(reader) != -1) {
return xmlTextReaderCollectSiblings(node->children);
}
case XML_ATTRIBUTE_NODE:
TODO
break;
default:
break;
}
return(NULL);
}
#if 0
int
xmlTextReaderReadBase64(xmlTextReaderPtr reader,
unsigned char *array ATTRIBUTE_UNUSED,
int offset ATTRIBUTE_UNUSED,
int len ATTRIBUTE_UNUSED) {
if ((reader == NULL) || (reader->ctxt == NULL))
return(-1);
if (reader->ctxt->wellFormed != 1)
return(-1);
if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
return(0);
TODO
return(0);
}
int
xmlTextReaderReadBinHex(xmlTextReaderPtr reader,
unsigned char *array ATTRIBUTE_UNUSED,
int offset ATTRIBUTE_UNUSED,
int len ATTRIBUTE_UNUSED) {
if ((reader == NULL) || (reader->ctxt == NULL))
return(-1);
if (reader->ctxt->wellFormed != 1)
return(-1);
if ((reader->node == NULL) || (reader->node->type == XML_ELEMENT_NODE))
return(0);
TODO
return(0);
}
#endif
static int
xmlTextReaderNextTree(xmlTextReaderPtr reader)
{
if (reader == NULL)
return(-1);
if (reader->state == XML_TEXTREADER_END)
return(0);
if (reader->node == NULL) {
if (reader->doc->children == NULL) {
reader->state = XML_TEXTREADER_END;
return(0);
}
reader->node = reader->doc->children;
reader->state = XML_TEXTREADER_START;
return(1);
}
if (reader->state != XML_TEXTREADER_BACKTRACK) {
if (reader->node->next != 0) {
reader->node = reader->node->next;
reader->state = XML_TEXTREADER_START;
return(1);
}
if ((reader->node->type == XML_ELEMENT_NODE) ||
(reader->node->type == XML_ATTRIBUTE_NODE)) {
reader->state = XML_TEXTREADER_BACKTRACK;
xmlTextReaderRead(reader);
}
}
if (reader->node->next != 0) {
reader->node = reader->node->next;
reader->state = XML_TEXTREADER_START;
return(1);
}
if (reader->node->parent != 0) {
if (reader->node->parent->type == XML_DOCUMENT_NODE) {
reader->state = XML_TEXTREADER_END;
return(0);
}
reader->node = reader->node->parent;
reader->depth--;
reader->state = XML_TEXTREADER_BACKTRACK;
xmlTextReaderNextTree(reader);
}
reader->state = XML_TEXTREADER_END;
return(1);
}
static int
xmlTextReaderReadTree(xmlTextReaderPtr reader) {
if (reader->state == XML_TEXTREADER_END)
return(0);
next_node:
if (reader->node == NULL) {
if (reader->doc->children == NULL) {
reader->state = XML_TEXTREADER_END;
return(0);
}
reader->node = reader->doc->children;
reader->state = XML_TEXTREADER_START;
goto found_node;
}
if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
(reader->node->type != XML_DTD_NODE) &&
(reader->node->type != XML_XINCLUDE_START) &&
(reader->node->type != XML_ENTITY_REF_NODE)) {
if (reader->node->children != NULL) {
reader->node = reader->node->children;
reader->depth++;
reader->state = XML_TEXTREADER_START;
goto found_node;
}
if (reader->node->type == XML_ATTRIBUTE_NODE) {
reader->state = XML_TEXTREADER_BACKTRACK;
goto found_node;
}
}
if (reader->node->next != NULL) {
reader->node = reader->node->next;
reader->state = XML_TEXTREADER_START;
goto found_node;
}
if (reader->node->parent != NULL) {
if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
(reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
reader->state = XML_TEXTREADER_END;
return(0);
}
reader->node = reader->node->parent;
reader->depth--;
reader->state = XML_TEXTREADER_BACKTRACK;
goto found_node;
}
reader->state = XML_TEXTREADER_END;
found_node:
if ((reader->node->type == XML_XINCLUDE_START) ||
(reader->node->type == XML_XINCLUDE_END))
goto next_node;
return(1);
}
int
xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
if (reader->doc == NULL) {
return(-1);
}
if (reader->state == XML_TEXTREADER_END)
return(0);
if (reader->node == NULL)
return(xmlTextReaderNextTree(reader));
if (reader->node->next != NULL) {
reader->node = reader->node->next;
reader->state = XML_TEXTREADER_START;
return(1);
}
return(0);
}
xmlTextReaderPtr
xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
xmlTextReaderPtr ret;
if (input == NULL)
return(NULL);
ret = xmlMalloc(sizeof(xmlTextReader));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlNewTextReader : malloc failed\n");
return(NULL);
}
memset(ret, 0, sizeof(xmlTextReader));
ret->doc = NULL;
ret->entTab = NULL;
ret->entMax = 0;
ret->entNr = 0;
ret->input = input;
ret->buffer = xmlBufferCreateSize(100);
if (ret->buffer == NULL) {
xmlFree(ret);
xmlGenericError(xmlGenericErrorContext,
"xmlNewTextReader : malloc failed\n");
return(NULL);
}
ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
if (ret->sax == NULL) {
xmlBufferFree(ret->buffer);
xmlFree(ret);
xmlGenericError(xmlGenericErrorContext,
"xmlNewTextReader : malloc failed\n");
return(NULL);
}
xmlSAXVersion(ret->sax, 2);
ret->startElement = ret->sax->startElement;
ret->sax->startElement = xmlTextReaderStartElement;
ret->endElement = ret->sax->endElement;
ret->sax->endElement = xmlTextReaderEndElement;
#ifdef LIBXML_SAX1_ENABLED
if (ret->sax->initialized == XML_SAX2_MAGIC) {
#endif
ret->startElementNs = ret->sax->startElementNs;
ret->sax->startElementNs = xmlTextReaderStartElementNs;
ret->endElementNs = ret->sax->endElementNs;
ret->sax->endElementNs = xmlTextReaderEndElementNs;
#ifdef LIBXML_SAX1_ENABLED
} else {
ret->startElementNs = NULL;
ret->endElementNs = NULL;
}
#endif
ret->characters = ret->sax->characters;
ret->sax->characters = xmlTextReaderCharacters;
ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
ret->cdataBlock = ret->sax->cdataBlock;
ret->sax->cdataBlock = xmlTextReaderCDataBlock;
ret->mode = XML_TEXTREADER_MODE_INITIAL;
ret->node = NULL;
ret->curnode = NULL;
if (ret->input->buffer->use < 4) {
xmlParserInputBufferRead(input, 4);
}
if (ret->input->buffer->use >= 4) {
ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
(const char *) ret->input->buffer->content, 4, URI);
ret->base = 0;
ret->cur = 4;
} else {
ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
ret->base = 0;
ret->cur = 0;
}
if (ret->ctxt == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlNewTextReader : malloc failed\n");
xmlBufferFree(ret->buffer);
xmlFree(ret->sax);
xmlFree(ret);
return(NULL);
}
ret->ctxt->parseMode = XML_PARSE_READER;
ret->ctxt->_private = ret;
ret->ctxt->linenumbers = 1;
ret->ctxt->dictNames = 1;
ret->allocs = XML_TEXTREADER_CTXT;
ret->ctxt->docdict = 1;
ret->dict = ret->ctxt->dict;
#ifdef LIBXML_XINCLUDE_ENABLED
ret->xinclude = 0;
#endif
#ifdef LIBXML_PATTERN_ENABLED
ret->patternMax = 0;
ret->patternTab = NULL;
#endif
return(ret);
}
xmlTextReaderPtr
xmlNewTextReaderFilename(const char *URI) {
xmlParserInputBufferPtr input;
xmlTextReaderPtr ret;
char *directory = NULL;
input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
if (input == NULL)
return(NULL);
ret = xmlNewTextReader(input, URI);
if (ret == NULL) {
xmlFreeParserInputBuffer(input);
return(NULL);
}
ret->allocs |= XML_TEXTREADER_INPUT;
if (ret->ctxt->directory == NULL)
directory = xmlParserGetDirectory(URI);
if ((ret->ctxt->directory == NULL) && (directory != NULL))
ret->ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
if (directory != NULL)
xmlFree(directory);
return(ret);
}
void
xmlFreeTextReader(xmlTextReaderPtr reader) {
if (reader == NULL)
return;
#ifdef LIBXML_SCHEMAS_ENABLED
if (reader->rngSchemas != NULL) {
xmlRelaxNGFree(reader->rngSchemas);
reader->rngSchemas = NULL;
}
if (reader->rngValidCtxt != NULL) {
xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
reader->rngValidCtxt = NULL;
}
if (reader->xsdPlug != NULL) {
xmlSchemaSAXUnplug(reader->xsdPlug);
reader->xsdPlug = NULL;
}
if (reader->xsdValidCtxt != NULL) {
if (! reader->xsdPreserveCtxt)
xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
reader->xsdValidCtxt = NULL;
}
if (reader->xsdSchemas != NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
}
#endif
#ifdef LIBXML_XINCLUDE_ENABLED
if (reader->xincctxt != NULL)
xmlXIncludeFreeContext(reader->xincctxt);
#endif
#ifdef LIBXML_PATTERN_ENABLED
if (reader->patternTab != NULL) {
int i;
for (i = 0;i < reader->patternNr;i++) {
if (reader->patternTab[i] != NULL)
xmlFreePattern(reader->patternTab[i]);
}
xmlFree(reader->patternTab);
}
#endif
if (reader->faketext != NULL) {
xmlFreeNode(reader->faketext);
}
if (reader->ctxt != NULL) {
if (reader->dict == reader->ctxt->dict)
reader->dict = NULL;
if (reader->ctxt->myDoc != NULL) {
if (reader->preserve == 0)
xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
reader->ctxt->myDoc = NULL;
}
if ((reader->ctxt->vctxt.vstateTab != NULL) &&
(reader->ctxt->vctxt.vstateMax > 0)){
xmlFree(reader->ctxt->vctxt.vstateTab);
reader->ctxt->vctxt.vstateTab = NULL;
reader->ctxt->vctxt.vstateMax = 0;
}
if (reader->allocs & XML_TEXTREADER_CTXT)
xmlFreeParserCtxt(reader->ctxt);
}
if (reader->sax != NULL)
xmlFree(reader->sax);
if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT))
xmlFreeParserInputBuffer(reader->input);
if (reader->buffer != NULL)
xmlBufferFree(reader->buffer);
if (reader->entTab != NULL)
xmlFree(reader->entTab);
if (reader->dict != NULL)
xmlDictFree(reader->dict);
xmlFree(reader);
}
int
xmlTextReaderClose(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
reader->node = NULL;
reader->curnode = NULL;
reader->mode = XML_TEXTREADER_MODE_CLOSED;
if (reader->ctxt != NULL) {
xmlStopParser(reader->ctxt);
if (reader->ctxt->myDoc != NULL) {
if (reader->preserve == 0)
xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
reader->ctxt->myDoc = NULL;
}
}
if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) {
xmlFreeParserInputBuffer(reader->input);
reader->allocs -= XML_TEXTREADER_INPUT;
}
return(0);
}
xmlChar *
xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
xmlChar *ret;
int i;
xmlAttrPtr cur;
xmlNsPtr ns;
if (reader == NULL)
return(NULL);
if (reader->node == NULL)
return(NULL);
if (reader->curnode != NULL)
return(NULL);
if (reader->node->type != XML_ELEMENT_NODE)
return(NULL);
ns = reader->node->nsDef;
for (i = 0;(i < no) && (ns != NULL);i++) {
ns = ns->next;
}
if (ns != NULL)
return(xmlStrdup(ns->href));
cur = reader->node->properties;
if (cur == NULL)
return(NULL);
for (;i < no;i++) {
cur = cur->next;
if (cur == NULL)
return(NULL);
}
ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
if (ret == NULL) return(xmlStrdup((xmlChar *)""));
return(ret);
}
xmlChar *
xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
xmlChar *prefix = NULL;
xmlChar *localname;
xmlNsPtr ns;
xmlChar *ret = NULL;
if ((reader == NULL) || (name == NULL))
return(NULL);
if (reader->node == NULL)
return(NULL);
if (reader->curnode != NULL)
return(NULL);
if (reader->node->type != XML_ELEMENT_NODE)
return(NULL);
localname = xmlSplitQName2(name, &prefix);
if (localname == NULL) {
if (xmlStrEqual(name, BAD_CAST "xmlns")) {
ns = reader->node->nsDef;
while (ns != NULL) {
if (ns->prefix == NULL) {
return(xmlStrdup(ns->href));
}
ns = ns->next;
}
return NULL;
}
return(xmlGetNoNsProp(reader->node, name));
}
if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
ns = reader->node->nsDef;
while (ns != NULL) {
if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
ret = xmlStrdup(ns->href);
break;
}
ns = ns->next;
}
} else {
ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
if (ns != NULL)
ret = xmlGetNsProp(reader->node, localname, ns->href);
}
xmlFree(localname);
if (prefix != NULL)
xmlFree(prefix);
return(ret);
}
xmlChar *
xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
const xmlChar *namespaceURI) {
xmlChar *prefix = NULL;
xmlNsPtr ns;
if ((reader == NULL) || (localName == NULL))
return(NULL);
if (reader->node == NULL)
return(NULL);
if (reader->curnode != NULL)
return(NULL);
if (reader->node->type != XML_ELEMENT_NODE)
return(NULL);
if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
prefix = BAD_CAST localName;
}
ns = reader->node->nsDef;
while (ns != NULL) {
if ((prefix == NULL && ns->prefix == NULL) ||
((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
return xmlStrdup(ns->href);
}
ns = ns->next;
}
return NULL;
}
return(xmlGetNsProp(reader->node, localName, namespaceURI));
}
xmlParserInputBufferPtr
xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
xmlParserInputBufferPtr ret = NULL;
if (reader == NULL)
return(NULL);
if (reader->node == NULL)
return(NULL);
reader->node = NULL;
reader->curnode = NULL;
reader->mode = XML_TEXTREADER_MODE_EOF;
if (reader->ctxt != NULL) {
xmlStopParser(reader->ctxt);
if (reader->ctxt->myDoc != NULL) {
if (reader->preserve == 0)
xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
reader->ctxt->myDoc = NULL;
}
}
if (reader->allocs & XML_TEXTREADER_INPUT) {
ret = reader->input;
reader->input = NULL;
reader->allocs -= XML_TEXTREADER_INPUT;
} else {
TODO
return(NULL);
}
return(ret);
}
xmlChar *
xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
xmlNsPtr ns;
if (reader == NULL)
return(NULL);
if (reader->node == NULL)
return(NULL);
ns = xmlSearchNs(reader->node->doc, reader->node, prefix);
if (ns == NULL)
return(NULL);
return(xmlStrdup(ns->href));
}
int
xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
int i;
xmlAttrPtr cur;
xmlNsPtr ns;
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->node->type != XML_ELEMENT_NODE)
return(-1);
reader->curnode = NULL;
ns = reader->node->nsDef;
for (i = 0;(i < no) && (ns != NULL);i++) {
ns = ns->next;
}
if (ns != NULL) {
reader->curnode = (xmlNodePtr) ns;
return(1);
}
cur = reader->node->properties;
if (cur == NULL)
return(0);
for (;i < no;i++) {
cur = cur->next;
if (cur == NULL)
return(0);
}
reader->curnode = (xmlNodePtr) cur;
return(1);
}
int
xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
xmlChar *prefix = NULL;
xmlChar *localname;
xmlNsPtr ns;
xmlAttrPtr prop;
if ((reader == NULL) || (name == NULL))
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->node->type != XML_ELEMENT_NODE)
return(0);
localname = xmlSplitQName2(name, &prefix);
if (localname == NULL) {
if (xmlStrEqual(name, BAD_CAST "xmlns")) {
ns = reader->node->nsDef;
while (ns != NULL) {
if (ns->prefix == NULL) {
reader->curnode = (xmlNodePtr) ns;
return(1);
}
ns = ns->next;
}
return(0);
}
prop = reader->node->properties;
while (prop != NULL) {
if ((xmlStrEqual(prop->name, name)) &&
((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
reader->curnode = (xmlNodePtr) prop;
return(1);
}
prop = prop->next;
}
return(0);
}
if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
ns = reader->node->nsDef;
while (ns != NULL) {
if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
reader->curnode = (xmlNodePtr) ns;
goto found;
}
ns = ns->next;
}
goto not_found;
}
prop = reader->node->properties;
while (prop != NULL) {
if ((xmlStrEqual(prop->name, localname)) &&
(prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
reader->curnode = (xmlNodePtr) prop;
goto found;
}
prop = prop->next;
}
not_found:
if (localname != NULL)
xmlFree(localname);
if (prefix != NULL)
xmlFree(prefix);
return(0);
found:
if (localname != NULL)
xmlFree(localname);
if (prefix != NULL)
xmlFree(prefix);
return(1);
}
int
xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
const xmlChar *localName, const xmlChar *namespaceURI) {
xmlAttrPtr prop;
xmlNodePtr node;
xmlNsPtr ns;
xmlChar *prefix = NULL;
if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->node->type != XML_ELEMENT_NODE)
return(0);
node = reader->node;
if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
prefix = BAD_CAST localName;
}
ns = reader->node->nsDef;
while (ns != NULL) {
if ((prefix == NULL && ns->prefix == NULL) ||
((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
reader->curnode = (xmlNodePtr) ns;
return(1);
}
ns = ns->next;
}
return(0);
}
prop = node->properties;
while (prop != NULL) {
if (xmlStrEqual(prop->name, localName) &&
((prop->ns != NULL) &&
(xmlStrEqual(prop->ns->href, namespaceURI)))) {
reader->curnode = (xmlNodePtr) prop;
return(1);
}
prop = prop->next;
}
return(0);
}
int
xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->node->type != XML_ELEMENT_NODE)
return(0);
if (reader->node->nsDef != NULL) {
reader->curnode = (xmlNodePtr) reader->node->nsDef;
return(1);
}
if (reader->node->properties != NULL) {
reader->curnode = (xmlNodePtr) reader->node->properties;
return(1);
}
return(0);
}
int
xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->node->type != XML_ELEMENT_NODE)
return(0);
if (reader->curnode == NULL)
return(xmlTextReaderMoveToFirstAttribute(reader));
if (reader->curnode->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) reader->curnode;
if (ns->next != NULL) {
reader->curnode = (xmlNodePtr) ns->next;
return(1);
}
if (reader->node->properties != NULL) {
reader->curnode = (xmlNodePtr) reader->node->properties;
return(1);
}
return(0);
} else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
(reader->curnode->next != NULL)) {
reader->curnode = reader->curnode->next;
return(1);
}
return(0);
}
int
xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->node->type != XML_ELEMENT_NODE)
return(0);
if (reader->curnode != NULL) {
reader->curnode = NULL;
return(1);
}
return(0);
}
int
xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->curnode == NULL)
return(0);
if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
if (reader->curnode->children == NULL)
return(0);
reader->curnode = reader->curnode->children;
} else if (reader->curnode->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) reader->curnode;
if (reader->faketext == NULL) {
reader->faketext = xmlNewDocText(reader->node->doc,
ns->href);
} else {
if ((reader->faketext->content != NULL) &&
(reader->faketext->content !=
(xmlChar *) &(reader->faketext->properties)))
xmlFree(reader->faketext->content);
reader->faketext->content = xmlStrdup(ns->href);
}
reader->curnode = reader->faketext;
} else {
if (reader->curnode->next == NULL)
return(0);
reader->curnode = reader->curnode->next;
}
return(1);
}
const xmlChar *
xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
xmlDocPtr doc = NULL;
if (reader == NULL)
return(NULL);
if (reader->doc != NULL)
doc = reader->doc;
else if (reader->ctxt != NULL)
doc = reader->ctxt->myDoc;
if (doc == NULL)
return(NULL);
if (doc->encoding == NULL)
return(NULL);
else
return(CONSTSTR(doc->encoding));
}
int
xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
int ret;
xmlAttrPtr attr;
xmlNsPtr ns;
xmlNodePtr node;
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(0);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (node->type != XML_ELEMENT_NODE)
return(0);
if ((reader->state == XML_TEXTREADER_END) ||
(reader->state == XML_TEXTREADER_BACKTRACK))
return(0);
ret = 0;
attr = node->properties;
while (attr != NULL) {
ret++;
attr = attr->next;
}
ns = node->nsDef;
while (ns != NULL) {
ret++;
ns = ns->next;
}
return(ret);
}
int
xmlTextReaderNodeType(xmlTextReaderPtr reader) {
xmlNodePtr node;
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(XML_READER_TYPE_NONE);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
switch (node->type) {
case XML_ELEMENT_NODE:
if ((reader->state == XML_TEXTREADER_END) ||
(reader->state == XML_TEXTREADER_BACKTRACK))
return(XML_READER_TYPE_END_ELEMENT);
return(XML_READER_TYPE_ELEMENT);
case XML_NAMESPACE_DECL:
case XML_ATTRIBUTE_NODE:
return(XML_READER_TYPE_ATTRIBUTE);
case XML_TEXT_NODE:
if (xmlIsBlankNode(reader->node)) {
if (xmlNodeGetSpacePreserve(reader->node))
return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
else
return(XML_READER_TYPE_WHITESPACE);
} else {
return(XML_READER_TYPE_TEXT);
}
case XML_CDATA_SECTION_NODE:
return(XML_READER_TYPE_CDATA);
case XML_ENTITY_REF_NODE:
return(XML_READER_TYPE_ENTITY_REFERENCE);
case XML_ENTITY_NODE:
return(XML_READER_TYPE_ENTITY);
case XML_PI_NODE:
return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
case XML_COMMENT_NODE:
return(XML_READER_TYPE_COMMENT);
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
return(XML_READER_TYPE_DOCUMENT);
case XML_DOCUMENT_FRAG_NODE:
return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
case XML_NOTATION_NODE:
return(XML_READER_TYPE_NOTATION);
case XML_DOCUMENT_TYPE_NODE:
case XML_DTD_NODE:
return(XML_READER_TYPE_DOCUMENT_TYPE);
case XML_ELEMENT_DECL:
case XML_ATTRIBUTE_DECL:
case XML_ENTITY_DECL:
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
return(XML_READER_TYPE_NONE);
}
return(-1);
}
int
xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
if ((reader == NULL) || (reader->node == NULL))
return(-1);
if (reader->node->type != XML_ELEMENT_NODE)
return(0);
if (reader->curnode != NULL)
return(0);
if (reader->node->children != NULL)
return(0);
if (reader->state == XML_TEXTREADER_END)
return(0);
if (reader->doc != NULL)
return(1);
#ifdef LIBXML_XINCLUDE_ENABLED
if (reader->in_xinclude > 0)
return(1);
#endif
return((reader->node->extra & NODE_IS_EMPTY) != 0);
}
xmlChar *
xmlTextReaderLocalName(xmlTextReaderPtr reader) {
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (node->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) node;
if (ns->prefix == NULL)
return(xmlStrdup(BAD_CAST "xmlns"));
else
return(xmlStrdup(ns->prefix));
}
if ((node->type != XML_ELEMENT_NODE) &&
(node->type != XML_ATTRIBUTE_NODE))
return(xmlTextReaderName(reader));
return(xmlStrdup(node->name));
}
const xmlChar *
xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (node->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) node;
if (ns->prefix == NULL)
return(CONSTSTR(BAD_CAST "xmlns"));
else
return(ns->prefix);
}
if ((node->type != XML_ELEMENT_NODE) &&
(node->type != XML_ATTRIBUTE_NODE))
return(xmlTextReaderConstName(reader));
return(node->name);
}
xmlChar *
xmlTextReaderName(xmlTextReaderPtr reader) {
xmlNodePtr node;
xmlChar *ret;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
switch (node->type) {
case XML_ELEMENT_NODE:
case XML_ATTRIBUTE_NODE:
if ((node->ns == NULL) ||
(node->ns->prefix == NULL))
return(xmlStrdup(node->name));
ret = xmlStrdup(node->ns->prefix);
ret = xmlStrcat(ret, BAD_CAST ":");
ret = xmlStrcat(ret, node->name);
return(ret);
case XML_TEXT_NODE:
return(xmlStrdup(BAD_CAST "#text"));
case XML_CDATA_SECTION_NODE:
return(xmlStrdup(BAD_CAST "#cdata-section"));
case XML_ENTITY_NODE:
case XML_ENTITY_REF_NODE:
return(xmlStrdup(node->name));
case XML_PI_NODE:
return(xmlStrdup(node->name));
case XML_COMMENT_NODE:
return(xmlStrdup(BAD_CAST "#comment"));
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
return(xmlStrdup(BAD_CAST "#document"));
case XML_DOCUMENT_FRAG_NODE:
return(xmlStrdup(BAD_CAST "#document-fragment"));
case XML_NOTATION_NODE:
return(xmlStrdup(node->name));
case XML_DOCUMENT_TYPE_NODE:
case XML_DTD_NODE:
return(xmlStrdup(node->name));
case XML_NAMESPACE_DECL: {
xmlNsPtr ns = (xmlNsPtr) node;
ret = xmlStrdup(BAD_CAST "xmlns");
if (ns->prefix == NULL)
return(ret);
ret = xmlStrcat(ret, BAD_CAST ":");
ret = xmlStrcat(ret, ns->prefix);
return(ret);
}
case XML_ELEMENT_DECL:
case XML_ATTRIBUTE_DECL:
case XML_ENTITY_DECL:
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
return(NULL);
}
return(NULL);
}
const xmlChar *
xmlTextReaderConstName(xmlTextReaderPtr reader) {
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
switch (node->type) {
case XML_ELEMENT_NODE:
case XML_ATTRIBUTE_NODE:
if ((node->ns == NULL) ||
(node->ns->prefix == NULL))
return(node->name);
return(CONSTQSTR(node->ns->prefix, node->name));
case XML_TEXT_NODE:
return(CONSTSTR(BAD_CAST "#text"));
case XML_CDATA_SECTION_NODE:
return(CONSTSTR(BAD_CAST "#cdata-section"));
case XML_ENTITY_NODE:
case XML_ENTITY_REF_NODE:
return(CONSTSTR(node->name));
case XML_PI_NODE:
return(CONSTSTR(node->name));
case XML_COMMENT_NODE:
return(CONSTSTR(BAD_CAST "#comment"));
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
return(CONSTSTR(BAD_CAST "#document"));
case XML_DOCUMENT_FRAG_NODE:
return(CONSTSTR(BAD_CAST "#document-fragment"));
case XML_NOTATION_NODE:
return(CONSTSTR(node->name));
case XML_DOCUMENT_TYPE_NODE:
case XML_DTD_NODE:
return(CONSTSTR(node->name));
case XML_NAMESPACE_DECL: {
xmlNsPtr ns = (xmlNsPtr) node;
if (ns->prefix == NULL)
return(CONSTSTR(BAD_CAST "xmlns"));
return(CONSTQSTR(BAD_CAST "xmlns", ns->prefix));
}
case XML_ELEMENT_DECL:
case XML_ATTRIBUTE_DECL:
case XML_ENTITY_DECL:
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
return(NULL);
}
return(NULL);
}
xmlChar *
xmlTextReaderPrefix(xmlTextReaderPtr reader) {
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (node->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) node;
if (ns->prefix == NULL)
return(NULL);
return(xmlStrdup(BAD_CAST "xmlns"));
}
if ((node->type != XML_ELEMENT_NODE) &&
(node->type != XML_ATTRIBUTE_NODE))
return(NULL);
if ((node->ns != NULL) && (node->ns->prefix != NULL))
return(xmlStrdup(node->ns->prefix));
return(NULL);
}
const xmlChar *
xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (node->type == XML_NAMESPACE_DECL) {
xmlNsPtr ns = (xmlNsPtr) node;
if (ns->prefix == NULL)
return(NULL);
return(CONSTSTR(BAD_CAST "xmlns"));
}
if ((node->type != XML_ELEMENT_NODE) &&
(node->type != XML_ATTRIBUTE_NODE))
return(NULL);
if ((node->ns != NULL) && (node->ns->prefix != NULL))
return(CONSTSTR(node->ns->prefix));
return(NULL);
}
xmlChar *
xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (node->type == XML_NAMESPACE_DECL)
return(xmlStrdup(BAD_CAST "http://www.w3.org/2000/xmlns/"));
if ((node->type != XML_ELEMENT_NODE) &&
(node->type != XML_ATTRIBUTE_NODE))
return(NULL);
if (node->ns != NULL)
return(xmlStrdup(node->ns->href));
return(NULL);
}
const xmlChar *
xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
xmlNodePtr node;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (node->type == XML_NAMESPACE_DECL)
return(CONSTSTR(BAD_CAST "http://www.w3.org/2000/xmlns/"));
if ((node->type != XML_ELEMENT_NODE) &&
(node->type != XML_ATTRIBUTE_NODE))
return(NULL);
if (node->ns != NULL)
return(CONSTSTR(node->ns->href));
return(NULL);
}
xmlChar *
xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
return(xmlNodeGetBase(NULL, reader->node));
}
const xmlChar *
xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
xmlChar *tmp;
const xmlChar *ret;
if ((reader == NULL) || (reader->node == NULL))
return(NULL);
tmp = xmlNodeGetBase(NULL, reader->node);
if (tmp == NULL)
return(NULL);
ret = CONSTSTR(tmp);
xmlFree(tmp);
return(ret);
}
int
xmlTextReaderDepth(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(0);
if (reader->curnode != NULL) {
if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
(reader->curnode->type == XML_NAMESPACE_DECL))
return(reader->depth + 1);
return(reader->depth + 2);
}
return(reader->depth);
}
int
xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
xmlNodePtr node;
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(0);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if ((node->type == XML_ELEMENT_NODE) &&
((node->properties != NULL) || (node->nsDef != NULL)))
return(1);
return(0);
}
int
xmlTextReaderHasValue(xmlTextReaderPtr reader) {
xmlNodePtr node;
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(0);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
switch (node->type) {
case XML_ATTRIBUTE_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
case XML_NAMESPACE_DECL:
return(1);
default:
break;
}
return(0);
}
xmlChar *
xmlTextReaderValue(xmlTextReaderPtr reader) {
xmlNodePtr node;
if (reader == NULL)
return(NULL);
if (reader->node == NULL)
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
switch (node->type) {
case XML_NAMESPACE_DECL:
return(xmlStrdup(((xmlNsPtr) node)->href));
case XML_ATTRIBUTE_NODE:{
xmlAttrPtr attr = (xmlAttrPtr) node;
if (attr->parent != NULL)
return (xmlNodeListGetString
(attr->parent->doc, attr->children, 1));
else
return (xmlNodeListGetString(NULL, attr->children, 1));
break;
}
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
if (node->content != NULL)
return (xmlStrdup(node->content));
default:
break;
}
return(NULL);
}
const xmlChar *
xmlTextReaderConstValue(xmlTextReaderPtr reader) {
xmlNodePtr node;
if (reader == NULL)
return(NULL);
if (reader->node == NULL)
return(NULL);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
switch (node->type) {
case XML_NAMESPACE_DECL:
return(((xmlNsPtr) node)->href);
case XML_ATTRIBUTE_NODE:{
xmlAttrPtr attr = (xmlAttrPtr) node;
if ((attr->children != NULL) &&
(attr->children->type == XML_TEXT_NODE) &&
(attr->children->next == NULL))
return(attr->children->content);
else {
if (reader->buffer == NULL)
reader->buffer = xmlBufferCreateSize(100);
if (reader->buffer == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlTextReaderSetup : malloc failed\n");
return (NULL);
}
reader->buffer->use = 0;
xmlNodeBufGetContent(reader->buffer, node);
return(reader->buffer->content);
}
break;
}
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
return(node->content);
default:
break;
}
return(NULL);
}
int
xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
return(0);
}
int
xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
return((int) '"');
}
xmlChar *
xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
if (reader == NULL)
return(NULL);
if (reader->node == NULL)
return(NULL);
return(xmlNodeGetLang(reader->node));
}
const xmlChar *
xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
xmlChar *tmp;
const xmlChar *ret;
if (reader == NULL)
return(NULL);
if (reader->node == NULL)
return(NULL);
tmp = xmlNodeGetLang(reader->node);
if (tmp == NULL)
return(NULL);
ret = CONSTSTR(tmp);
xmlFree(tmp);
return(ret);
}
const xmlChar *
xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
if (reader == NULL)
return(NULL);
return(CONSTSTR(str));
}
int
xmlTextReaderNormalization(xmlTextReaderPtr reader) {
if (reader == NULL)
return(-1);
return(1);
}
int
xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
xmlParserProperties p = (xmlParserProperties) prop;
xmlParserCtxtPtr ctxt;
if ((reader == NULL) || (reader->ctxt == NULL))
return(-1);
ctxt = reader->ctxt;
switch (p) {
case XML_PARSER_LOADDTD:
if (value != 0) {
if (ctxt->loadsubset == 0) {
if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
return(-1);
ctxt->loadsubset = XML_DETECT_IDS;
}
} else {
ctxt->loadsubset = 0;
}
return(0);
case XML_PARSER_DEFAULTATTRS:
if (value != 0) {
ctxt->loadsubset |= XML_COMPLETE_ATTRS;
} else {
if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
ctxt->loadsubset -= XML_COMPLETE_ATTRS;
}
return(0);
case XML_PARSER_VALIDATE:
if (value != 0) {
ctxt->validate = 1;
reader->validate = XML_TEXTREADER_VALIDATE_DTD;
} else {
ctxt->validate = 0;
}
return(0);
case XML_PARSER_SUBST_ENTITIES:
if (value != 0) {
ctxt->replaceEntities = 1;
} else {
ctxt->replaceEntities = 0;
}
return(0);
}
return(-1);
}
int
xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
xmlParserProperties p = (xmlParserProperties) prop;
xmlParserCtxtPtr ctxt;
if ((reader == NULL) || (reader->ctxt == NULL))
return(-1);
ctxt = reader->ctxt;
switch (p) {
case XML_PARSER_LOADDTD:
if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
return(1);
return(0);
case XML_PARSER_DEFAULTATTRS:
if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
return(1);
return(0);
case XML_PARSER_VALIDATE:
return(reader->validate);
case XML_PARSER_SUBST_ENTITIES:
return(ctxt->replaceEntities);
}
return(-1);
}
int
xmlTextReaderGetParserLineNumber(xmlTextReaderPtr reader)
{
if ((reader == NULL) || (reader->ctxt == NULL) ||
(reader->ctxt->input == NULL)) {
return (0);
}
return (reader->ctxt->input->line);
}
int
xmlTextReaderGetParserColumnNumber(xmlTextReaderPtr reader)
{
if ((reader == NULL) || (reader->ctxt == NULL) ||
(reader->ctxt->input == NULL)) {
return (0);
}
return (reader->ctxt->input->col);
}
xmlNodePtr
xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
if (reader == NULL)
return(NULL);
if (reader->curnode != NULL)
return(reader->curnode);
return(reader->node);
}
xmlNodePtr
xmlTextReaderPreserve(xmlTextReaderPtr reader) {
xmlNodePtr cur, parent;
if (reader == NULL)
return(NULL);
if (reader->curnode != NULL)
cur = reader->curnode;
else
cur = reader->node;
if (cur == NULL)
return(NULL);
if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) {
cur->extra |= NODE_IS_PRESERVED;
cur->extra |= NODE_IS_SPRESERVED;
}
reader->preserves++;
parent = cur->parent;;
while (parent != NULL) {
if (parent->type == XML_ELEMENT_NODE)
parent->extra |= NODE_IS_PRESERVED;
parent = parent->parent;
}
return(cur);
}
#ifdef LIBXML_PATTERN_ENABLED
int
xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern,
const xmlChar **namespaces)
{
xmlPatternPtr comp;
if ((reader == NULL) || (pattern == NULL))
return(-1);
comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
if (comp == NULL)
return(-1);
if (reader->patternMax <= 0) {
reader->patternMax = 4;
reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax *
sizeof(reader->patternTab[0]));
if (reader->patternTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
return (-1);
}
}
if (reader->patternNr >= reader->patternMax) {
xmlPatternPtr *tmp;
reader->patternMax *= 2;
tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab,
reader->patternMax *
sizeof(reader->patternTab[0]));
if (tmp == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
reader->patternMax /= 2;
return (-1);
}
reader->patternTab = tmp;
}
reader->patternTab[reader->patternNr] = comp;
return(reader->patternNr++);
}
#endif
xmlDocPtr
xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
if (reader == NULL)
return(NULL);
if (reader->doc != NULL)
return(reader->doc);
if ((reader->ctxt == NULL) || (reader->ctxt->myDoc == NULL))
return(NULL);
reader->preserve = 1;
return(reader->ctxt->myDoc);
}
#ifdef LIBXML_SCHEMAS_ENABLED
static char *xmlTextReaderBuildMessage(const char *msg, va_list ap);
static void XMLCDECL
xmlTextReaderValidityError(void *ctxt, const char *msg, ...);
static void XMLCDECL
xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...);
static void XMLCDECL
xmlTextReaderValidityErrorRelay(void *ctx, const char *msg, ...)
{
xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx;
char *str;
va_list ap;
va_start(ap, msg);
str = xmlTextReaderBuildMessage(msg, ap);
if (!reader->errorFunc) {
xmlTextReaderValidityError(ctx, "%s", str);
} else {
reader->errorFunc(reader->errorFuncArg, str,
XML_PARSER_SEVERITY_VALIDITY_ERROR,
NULL );
}
if (str != NULL)
xmlFree(str);
va_end(ap);
}
static void XMLCDECL
xmlTextReaderValidityWarningRelay(void *ctx, const char *msg, ...)
{
xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx;
char *str;
va_list ap;
va_start(ap, msg);
str = xmlTextReaderBuildMessage(msg, ap);
if (!reader->errorFunc) {
xmlTextReaderValidityWarning(ctx, "%s", str);
} else {
reader->errorFunc(reader->errorFuncArg, str,
XML_PARSER_SEVERITY_VALIDITY_WARNING,
NULL );
}
if (str != NULL)
xmlFree(str);
va_end(ap);
}
static void
xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error);
static void
xmlTextReaderValidityStructuredRelay(void *userData, xmlErrorPtr error)
{
xmlTextReaderPtr reader = (xmlTextReaderPtr) userData;
if (reader->sErrorFunc) {
reader->sErrorFunc(reader->errorFuncArg, error);
} else {
xmlTextReaderStructuredError(reader, error);
}
}
int
xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
if (reader == NULL)
return(-1);
if (schema == NULL) {
if (reader->rngSchemas != NULL) {
xmlRelaxNGFree(reader->rngSchemas);
reader->rngSchemas = NULL;
}
if (reader->rngValidCtxt != NULL) {
xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
reader->rngValidCtxt = NULL;
}
return(0);
}
if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
return(-1);
if (reader->rngSchemas != NULL) {
xmlRelaxNGFree(reader->rngSchemas);
reader->rngSchemas = NULL;
}
if (reader->rngValidCtxt != NULL) {
xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
reader->rngValidCtxt = NULL;
}
reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
if (reader->rngValidCtxt == NULL)
return(-1);
if (reader->errorFunc != NULL) {
xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
}
if (reader->sErrorFunc != NULL) {
xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
xmlTextReaderValidityStructuredRelay,
reader);
}
reader->rngValidErrors = 0;
reader->rngFullNode = NULL;
reader->validate = XML_TEXTREADER_VALIDATE_RNG;
return(0);
}
int
xmlTextReaderSetSchema(xmlTextReaderPtr reader, xmlSchemaPtr schema) {
if (reader == NULL)
return(-1);
if (schema == NULL) {
if (reader->xsdPlug != NULL) {
xmlSchemaSAXUnplug(reader->xsdPlug);
reader->xsdPlug = NULL;
}
if (reader->xsdValidCtxt != NULL) {
if (! reader->xsdPreserveCtxt)
xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
reader->xsdValidCtxt = NULL;
}
reader->xsdPreserveCtxt = 0;
if (reader->xsdSchemas != NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
}
return(0);
}
if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
return(-1);
if (reader->xsdPlug != NULL) {
xmlSchemaSAXUnplug(reader->xsdPlug);
reader->xsdPlug = NULL;
}
if (reader->xsdValidCtxt != NULL) {
if (! reader->xsdPreserveCtxt)
xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
reader->xsdValidCtxt = NULL;
}
reader->xsdPreserveCtxt = 0;
if (reader->xsdSchemas != NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
}
reader->xsdValidCtxt = xmlSchemaNewValidCtxt(schema);
if (reader->xsdValidCtxt == NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
return(-1);
}
reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
&(reader->ctxt->sax),
&(reader->ctxt->userData));
if (reader->xsdPlug == NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
reader->xsdValidCtxt = NULL;
return(-1);
}
if (reader->errorFunc != NULL) {
xmlSchemaSetValidErrors(reader->xsdValidCtxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
}
if (reader->sErrorFunc != NULL) {
xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
xmlTextReaderValidityStructuredRelay,
reader);
}
reader->xsdValidErrors = 0;
reader->validate = XML_TEXTREADER_VALIDATE_XSD;
return(0);
}
int
xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) {
xmlRelaxNGParserCtxtPtr ctxt;
if (reader == NULL)
return(-1);
if (rng == NULL) {
if (reader->rngValidCtxt != NULL) {
xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
reader->rngValidCtxt = NULL;
}
if (reader->rngSchemas != NULL) {
xmlRelaxNGFree(reader->rngSchemas);
reader->rngSchemas = NULL;
}
return(0);
}
if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
return(-1);
if (reader->rngSchemas != NULL) {
xmlRelaxNGFree(reader->rngSchemas);
reader->rngSchemas = NULL;
}
if (reader->rngValidCtxt != NULL) {
xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
reader->rngValidCtxt = NULL;
}
ctxt = xmlRelaxNGNewParserCtxt(rng);
if (reader->errorFunc != NULL) {
xmlRelaxNGSetParserErrors(ctxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
}
if (reader->sErrorFunc != NULL) {
xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
xmlTextReaderValidityStructuredRelay,
reader);
}
reader->rngSchemas = xmlRelaxNGParse(ctxt);
xmlRelaxNGFreeParserCtxt(ctxt);
if (reader->rngSchemas == NULL)
return(-1);
reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
if (reader->rngValidCtxt == NULL) {
xmlRelaxNGFree(reader->rngSchemas);
reader->rngSchemas = NULL;
return(-1);
}
if (reader->errorFunc != NULL) {
xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
}
if (reader->sErrorFunc != NULL) {
xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
xmlTextReaderValidityStructuredRelay,
reader);
}
reader->rngValidErrors = 0;
reader->rngFullNode = NULL;
reader->validate = XML_TEXTREADER_VALIDATE_RNG;
return(0);
}
static int
xmlTextReaderSchemaValidateInternal(xmlTextReaderPtr reader,
const char *xsd,
xmlSchemaValidCtxtPtr ctxt,
int options ATTRIBUTE_UNUSED)
{
if (reader == NULL)
return(-1);
if ((xsd != NULL) && (ctxt != NULL))
return(-1);
if (((xsd != NULL) || (ctxt != NULL)) &&
((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
(reader->ctxt == NULL)))
return(-1);
if (reader->xsdPlug != NULL) {
xmlSchemaSAXUnplug(reader->xsdPlug);
reader->xsdPlug = NULL;
}
if (reader->xsdValidCtxt != NULL) {
if (! reader->xsdPreserveCtxt)
xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
reader->xsdValidCtxt = NULL;
}
reader->xsdPreserveCtxt = 0;
if (reader->xsdSchemas != NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
}
if ((xsd == NULL) && (ctxt == NULL)) {
return(0);
}
if (xsd != NULL) {
xmlSchemaParserCtxtPtr pctxt;
pctxt = xmlSchemaNewParserCtxt(xsd);
if (reader->errorFunc != NULL) {
xmlSchemaSetParserErrors(pctxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
}
reader->xsdSchemas = xmlSchemaParse(pctxt);
xmlSchemaFreeParserCtxt(pctxt);
if (reader->xsdSchemas == NULL)
return(-1);
reader->xsdValidCtxt = xmlSchemaNewValidCtxt(reader->xsdSchemas);
if (reader->xsdValidCtxt == NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
return(-1);
}
reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
&(reader->ctxt->sax),
&(reader->ctxt->userData));
if (reader->xsdPlug == NULL) {
xmlSchemaFree(reader->xsdSchemas);
reader->xsdSchemas = NULL;
xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
reader->xsdValidCtxt = NULL;
return(-1);
}
} else {
reader->xsdValidCtxt = ctxt;
reader->xsdPreserveCtxt = 1;
reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
&(reader->ctxt->sax),
&(reader->ctxt->userData));
if (reader->xsdPlug == NULL) {
reader->xsdValidCtxt = NULL;
reader->xsdPreserveCtxt = 0;
return(-1);
}
}
if (reader->errorFunc != NULL) {
xmlSchemaSetValidErrors(reader->xsdValidCtxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
}
if (reader->sErrorFunc != NULL) {
xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
xmlTextReaderValidityStructuredRelay,
reader);
}
reader->xsdValidErrors = 0;
reader->validate = XML_TEXTREADER_VALIDATE_XSD;
return(0);
}
int
xmlTextReaderSchemaValidateCtxt(xmlTextReaderPtr reader,
xmlSchemaValidCtxtPtr ctxt,
int options)
{
return(xmlTextReaderSchemaValidateInternal(reader, NULL, ctxt, options));
}
int
xmlTextReaderSchemaValidate(xmlTextReaderPtr reader, const char *xsd)
{
return(xmlTextReaderSchemaValidateInternal(reader, xsd, NULL, 0));
}
#endif
int
xmlTextReaderIsNamespaceDecl(xmlTextReaderPtr reader) {
xmlNodePtr node;
if (reader == NULL)
return(-1);
if (reader->node == NULL)
return(-1);
if (reader->curnode != NULL)
node = reader->curnode;
else
node = reader->node;
if (XML_NAMESPACE_DECL == node->type)
return(1);
else
return(0);
}
const xmlChar *
xmlTextReaderConstXmlVersion(xmlTextReaderPtr reader) {
xmlDocPtr doc = NULL;
if (reader == NULL)
return(NULL);
if (reader->doc != NULL)
doc = reader->doc;
else if (reader->ctxt != NULL)
doc = reader->ctxt->myDoc;
if (doc == NULL)
return(NULL);
if (doc->version == NULL)
return(NULL);
else
return(CONSTSTR(doc->version));
}
int
xmlTextReaderStandalone(xmlTextReaderPtr reader) {
xmlDocPtr doc = NULL;
if (reader == NULL)
return(-1);
if (reader->doc != NULL)
doc = reader->doc;
else if (reader->ctxt != NULL)
doc = reader->ctxt->myDoc;
if (doc == NULL)
return(-1);
return(doc->standalone);
}
static char *
xmlTextReaderBuildMessage(const char *msg, va_list ap) {
int size = 0;
int chars;
char *larger;
char *str = NULL;
va_list aq;
while (1) {
VA_COPY(aq, ap);
chars = vsnprintf(str, size, msg, aq);
va_end(aq);
if (chars < 0) {
xmlGenericError(xmlGenericErrorContext, "vsnprintf failed !\n");
if (str)
xmlFree(str);
return NULL;
}
if ((chars < size) || (size == MAX_ERR_MSG_SIZE))
break;
if (chars < MAX_ERR_MSG_SIZE)
size = chars + 1;
else
size = MAX_ERR_MSG_SIZE;
if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
if (str)
xmlFree(str);
return NULL;
}
str = larger;
}
return str;
}
int
xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
int ret = -1;
if (locator == NULL)
return(-1);
if (ctx->node != NULL) {
ret = xmlGetLineNo(ctx->node);
}
else {
xmlParserInputPtr input;
input = ctx->input;
if ((input->filename == NULL) && (ctx->inputNr > 1))
input = ctx->inputTab[ctx->inputNr - 2];
if (input != NULL) {
ret = input->line;
}
else {
ret = -1;
}
}
return ret;
}
xmlChar *
xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
xmlChar *ret = NULL;
if (locator == NULL)
return(NULL);
if (ctx->node != NULL) {
ret = xmlNodeGetBase(NULL,ctx->node);
}
else {
xmlParserInputPtr input;
input = ctx->input;
if ((input->filename == NULL) && (ctx->inputNr > 1))
input = ctx->inputTab[ctx->inputNr - 2];
if (input != NULL) {
ret = xmlStrdup(BAD_CAST input->filename);
}
else {
ret = NULL;
}
}
return ret;
}
static void
xmlTextReaderGenericError(void *ctxt, xmlParserSeverities severity,
char *str)
{
xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt;
xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private;
if (str != NULL) {
if (reader->errorFunc)
reader->errorFunc(reader->errorFuncArg, str, severity,
(xmlTextReaderLocatorPtr) ctx);
xmlFree(str);
}
}
static void
xmlTextReaderStructuredError(void *ctxt, xmlErrorPtr error)
{
xmlParserCtxtPtr ctx = (xmlParserCtxtPtr) ctxt;
xmlTextReaderPtr reader = (xmlTextReaderPtr) ctx->_private;
if (error && reader->sErrorFunc) {
reader->sErrorFunc(reader->errorFuncArg, (xmlErrorPtr) error);
}
}
static void XMLCDECL
xmlTextReaderError(void *ctxt, const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
xmlTextReaderGenericError(ctxt,
XML_PARSER_SEVERITY_ERROR,
xmlTextReaderBuildMessage(msg, ap));
va_end(ap);
}
static void XMLCDECL
xmlTextReaderWarning(void *ctxt, const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
xmlTextReaderGenericError(ctxt,
XML_PARSER_SEVERITY_WARNING,
xmlTextReaderBuildMessage(msg, ap));
va_end(ap);
}
static void XMLCDECL
xmlTextReaderValidityError(void *ctxt, const char *msg, ...)
{
va_list ap;
int len = xmlStrlen((const xmlChar *) msg);
if ((len > 1) && (msg[len - 2] != ':')) {
va_start(ap, msg);
xmlTextReaderGenericError(ctxt,
XML_PARSER_SEVERITY_VALIDITY_ERROR,
xmlTextReaderBuildMessage(msg, ap));
va_end(ap);
}
}
static void XMLCDECL
xmlTextReaderValidityWarning(void *ctxt, const char *msg, ...)
{
va_list ap;
int len = xmlStrlen((const xmlChar *) msg);
if ((len != 0) && (msg[len - 1] != ':')) {
va_start(ap, msg);
xmlTextReaderGenericError(ctxt,
XML_PARSER_SEVERITY_VALIDITY_WARNING,
xmlTextReaderBuildMessage(msg, ap));
va_end(ap);
}
}
void
xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
xmlTextReaderErrorFunc f, void *arg)
{
if (f != NULL) {
reader->ctxt->sax->error = xmlTextReaderError;
reader->ctxt->sax->serror = NULL;
reader->ctxt->vctxt.error = xmlTextReaderValidityError;
reader->ctxt->sax->warning = xmlTextReaderWarning;
reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
reader->errorFunc = f;
reader->sErrorFunc = NULL;
reader->errorFuncArg = arg;
#ifdef LIBXML_SCHEMAS_ENABLED
if (reader->rngValidCtxt) {
xmlRelaxNGSetValidErrors(reader->rngValidCtxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
reader);
}
if (reader->xsdValidCtxt) {
xmlSchemaSetValidErrors(reader->xsdValidCtxt,
xmlTextReaderValidityErrorRelay,
xmlTextReaderValidityWarningRelay,
reader);
xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
reader);
}
#endif
} else {
reader->ctxt->sax->error = xmlParserError;
reader->ctxt->vctxt.error = xmlParserValidityError;
reader->ctxt->sax->warning = xmlParserWarning;
reader->ctxt->vctxt.warning = xmlParserValidityWarning;
reader->errorFunc = NULL;
reader->sErrorFunc = NULL;
reader->errorFuncArg = NULL;
#ifdef LIBXML_SCHEMAS_ENABLED
if (reader->rngValidCtxt) {
xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
reader);
xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
reader);
}
if (reader->xsdValidCtxt) {
xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
reader);
xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
reader);
}
#endif
}
}
void
xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader,
xmlStructuredErrorFunc f, void *arg)
{
if (f != NULL) {
reader->ctxt->sax->error = NULL;
reader->ctxt->sax->serror = xmlTextReaderStructuredError;
reader->ctxt->vctxt.error = xmlTextReaderValidityError;
reader->ctxt->sax->warning = xmlTextReaderWarning;
reader->ctxt->vctxt.warning = xmlTextReaderValidityWarning;
reader->sErrorFunc = f;
reader->errorFunc = NULL;
reader->errorFuncArg = arg;
#ifdef LIBXML_SCHEMAS_ENABLED
if (reader->rngValidCtxt) {
xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
reader);
xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
xmlTextReaderValidityStructuredRelay,
reader);
}
if (reader->xsdValidCtxt) {
xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
reader);
xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
xmlTextReaderValidityStructuredRelay,
reader);
}
#endif
} else {
reader->ctxt->sax->error = xmlParserError;
reader->ctxt->sax->serror = NULL;
reader->ctxt->vctxt.error = xmlParserValidityError;
reader->ctxt->sax->warning = xmlParserWarning;
reader->ctxt->vctxt.warning = xmlParserValidityWarning;
reader->errorFunc = NULL;
reader->sErrorFunc = NULL;
reader->errorFuncArg = NULL;
#ifdef LIBXML_SCHEMAS_ENABLED
if (reader->rngValidCtxt) {
xmlRelaxNGSetValidErrors(reader->rngValidCtxt, NULL, NULL,
reader);
xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
reader);
}
if (reader->xsdValidCtxt) {
xmlSchemaSetValidErrors(reader->xsdValidCtxt, NULL, NULL,
reader);
xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
reader);
}
#endif
}
}
int
xmlTextReaderIsValid(xmlTextReaderPtr reader)
{
if (reader == NULL)
return (-1);
#ifdef LIBXML_SCHEMAS_ENABLED
if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
return (reader->rngValidErrors == 0);
if (reader->validate == XML_TEXTREADER_VALIDATE_XSD)
return (reader->xsdValidErrors == 0);
#endif
if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
return (reader->ctxt->valid);
return (0);
}
void
xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
xmlTextReaderErrorFunc * f, void **arg)
{
if (f != NULL)
*f = reader->errorFunc;
if (arg != NULL)
*arg = reader->errorFuncArg;
}
int
xmlTextReaderSetup(xmlTextReaderPtr reader,
xmlParserInputBufferPtr input, const char *URL,
const char *encoding, int options)
{
if (reader == NULL) {
if (input != NULL)
xmlFreeParserInputBuffer(input);
return (-1);
}
options |= XML_PARSE_COMPACT;
reader->doc = NULL;
reader->entNr = 0;
reader->parserFlags = options;
reader->validate = XML_TEXTREADER_NOT_VALIDATE;
if ((input != NULL) && (reader->input != NULL) &&
(reader->allocs & XML_TEXTREADER_INPUT)) {
xmlFreeParserInputBuffer(reader->input);
reader->input = NULL;
reader->allocs -= XML_TEXTREADER_INPUT;
}
if (input != NULL) {
reader->input = input;
reader->allocs |= XML_TEXTREADER_INPUT;
}
if (reader->buffer == NULL)
reader->buffer = xmlBufferCreateSize(100);
if (reader->buffer == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlTextReaderSetup : malloc failed\n");
return (-1);
}
if (reader->sax == NULL)
reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
if (reader->sax == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlTextReaderSetup : malloc failed\n");
return (-1);
}
xmlSAXVersion(reader->sax, 2);
reader->startElement = reader->sax->startElement;
reader->sax->startElement = xmlTextReaderStartElement;
reader->endElement = reader->sax->endElement;
reader->sax->endElement = xmlTextReaderEndElement;
#ifdef LIBXML_SAX1_ENABLED
if (reader->sax->initialized == XML_SAX2_MAGIC) {
#endif
reader->startElementNs = reader->sax->startElementNs;
reader->sax->startElementNs = xmlTextReaderStartElementNs;
reader->endElementNs = reader->sax->endElementNs;
reader->sax->endElementNs = xmlTextReaderEndElementNs;
#ifdef LIBXML_SAX1_ENABLED
} else {
reader->startElementNs = NULL;
reader->endElementNs = NULL;
}
#endif
reader->characters = reader->sax->characters;
reader->sax->characters = xmlTextReaderCharacters;
reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
reader->cdataBlock = reader->sax->cdataBlock;
reader->sax->cdataBlock = xmlTextReaderCDataBlock;
reader->mode = XML_TEXTREADER_MODE_INITIAL;
reader->node = NULL;
reader->curnode = NULL;
if (input != NULL) {
if (reader->input->buffer->use < 4) {
xmlParserInputBufferRead(input, 4);
}
if (reader->ctxt == NULL) {
if (reader->input->buffer->use >= 4) {
reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
(const char *) reader->input->buffer->content, 4, URL);
reader->base = 0;
reader->cur = 4;
} else {
reader->ctxt =
xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
reader->base = 0;
reader->cur = 0;
}
} else {
xmlParserInputPtr inputStream;
xmlParserInputBufferPtr buf;
xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
xmlCtxtReset(reader->ctxt);
buf = xmlAllocParserInputBuffer(enc);
if (buf == NULL) return(-1);
inputStream = xmlNewInputStream(reader->ctxt);
if (inputStream == NULL) {
xmlFreeParserInputBuffer(buf);
return(-1);
}
if (URL == NULL)
inputStream->filename = NULL;
else
inputStream->filename = (char *)
xmlCanonicPath((const xmlChar *) URL);
inputStream->buf = buf;
inputStream->base = inputStream->buf->buffer->content;
inputStream->cur = inputStream->buf->buffer->content;
inputStream->end =
&inputStream->buf->buffer->content[inputStream->buf->buffer->use];
inputPush(reader->ctxt, inputStream);
reader->cur = 0;
}
if (reader->ctxt == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlTextReaderSetup : malloc failed\n");
return (-1);
}
}
if (reader->dict != NULL) {
if (reader->ctxt->dict != NULL) {
if (reader->dict != reader->ctxt->dict) {
xmlDictFree(reader->dict);
reader->dict = reader->ctxt->dict;
}
} else {
reader->ctxt->dict = reader->dict;
}
} else {
if (reader->ctxt->dict == NULL)
reader->ctxt->dict = xmlDictCreate();
reader->dict = reader->ctxt->dict;
}
reader->ctxt->_private = reader;
reader->ctxt->linenumbers = 1;
reader->ctxt->dictNames = 1;
reader->ctxt->docdict = 1;
reader->ctxt->parseMode = XML_PARSE_READER;
#ifdef LIBXML_XINCLUDE_ENABLED
if (reader->xincctxt != NULL) {
xmlXIncludeFreeContext(reader->xincctxt);
reader->xincctxt = NULL;
}
if (options & XML_PARSE_XINCLUDE) {
reader->xinclude = 1;
reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
options -= XML_PARSE_XINCLUDE;
} else
reader->xinclude = 0;
reader->in_xinclude = 0;
#endif
#ifdef LIBXML_PATTERN_ENABLED
if (reader->patternTab == NULL) {
reader->patternNr = 0;
reader->patternMax = 0;
}
while (reader->patternNr > 0) {
reader->patternNr--;
if (reader->patternTab[reader->patternNr] != NULL) {
xmlFreePattern(reader->patternTab[reader->patternNr]);
reader->patternTab[reader->patternNr] = NULL;
}
}
#endif
if (options & XML_PARSE_DTDVALID)
reader->validate = XML_TEXTREADER_VALIDATE_DTD;
xmlCtxtUseOptions(reader->ctxt, options);
if (encoding != NULL) {
xmlCharEncodingHandlerPtr hdlr;
hdlr = xmlFindCharEncodingHandler(encoding);
if (hdlr != NULL)
xmlSwitchToEncoding(reader->ctxt, hdlr);
}
if ((URL != NULL) && (reader->ctxt->input != NULL) &&
(reader->ctxt->input->filename == NULL))
reader->ctxt->input->filename = (char *)
xmlStrdup((const xmlChar *) URL);
reader->doc = NULL;
return (0);
}
long
xmlTextReaderByteConsumed(xmlTextReaderPtr reader) {
if ((reader == NULL) || (reader->ctxt == NULL))
return(-1);
return(xmlByteConsumed(reader->ctxt));
}
xmlTextReaderPtr
xmlReaderWalker(xmlDocPtr doc)
{
xmlTextReaderPtr ret;
if (doc == NULL)
return(NULL);
ret = xmlMalloc(sizeof(xmlTextReader));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlNewTextReader : malloc failed\n");
return(NULL);
}
memset(ret, 0, sizeof(xmlTextReader));
ret->entNr = 0;
ret->input = NULL;
ret->mode = XML_TEXTREADER_MODE_INITIAL;
ret->node = NULL;
ret->curnode = NULL;
ret->base = 0;
ret->cur = 0;
ret->allocs = XML_TEXTREADER_CTXT;
ret->doc = doc;
ret->state = XML_TEXTREADER_START;
ret->dict = xmlDictCreate();
return(ret);
}
xmlTextReaderPtr
xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
int options)
{
int len;
if (cur == NULL)
return (NULL);
len = xmlStrlen(cur);
return (xmlReaderForMemory
((const char *) cur, len, URL, encoding, options));
}
xmlTextReaderPtr
xmlReaderForFile(const char *filename, const char *encoding, int options)
{
xmlTextReaderPtr reader;
reader = xmlNewTextReaderFilename(filename);
if (reader == NULL)
return (NULL);
xmlTextReaderSetup(reader, NULL, NULL, encoding, options);
return (reader);
}
xmlTextReaderPtr
xmlReaderForMemory(const char *buffer, int size, const char *URL,
const char *encoding, int options)
{
xmlTextReaderPtr reader;
xmlParserInputBufferPtr buf;
buf = xmlParserInputBufferCreateStatic(buffer, size,
XML_CHAR_ENCODING_NONE);
if (buf == NULL) {
return (NULL);
}
reader = xmlNewTextReader(buf, URL);
if (reader == NULL) {
xmlFreeParserInputBuffer(buf);
return (NULL);
}
reader->allocs |= XML_TEXTREADER_INPUT;
xmlTextReaderSetup(reader, NULL, URL, encoding, options);
return (reader);
}
xmlTextReaderPtr
xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
{
xmlTextReaderPtr reader;
xmlParserInputBufferPtr input;
if (fd < 0)
return (NULL);
input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
if (input == NULL)
return (NULL);
input->closecallback = NULL;
reader = xmlNewTextReader(input, URL);
if (reader == NULL) {
xmlFreeParserInputBuffer(input);
return (NULL);
}
reader->allocs |= XML_TEXTREADER_INPUT;
xmlTextReaderSetup(reader, NULL, URL, encoding, options);
return (reader);
}
xmlTextReaderPtr
xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
void *ioctx, const char *URL, const char *encoding,
int options)
{
xmlTextReaderPtr reader;
xmlParserInputBufferPtr input;
if (ioread == NULL)
return (NULL);
input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
XML_CHAR_ENCODING_NONE);
if (input == NULL)
return (NULL);
reader = xmlNewTextReader(input, URL);
if (reader == NULL) {
xmlFreeParserInputBuffer(input);
return (NULL);
}
reader->allocs |= XML_TEXTREADER_INPUT;
xmlTextReaderSetup(reader, NULL, URL, encoding, options);
return (reader);
}
int
xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
{
if (doc == NULL)
return (-1);
if (reader == NULL)
return (-1);
if (reader->input != NULL) {
xmlFreeParserInputBuffer(reader->input);
}
if (reader->ctxt != NULL) {
xmlCtxtReset(reader->ctxt);
}
reader->entNr = 0;
reader->input = NULL;
reader->mode = XML_TEXTREADER_MODE_INITIAL;
reader->node = NULL;
reader->curnode = NULL;
reader->base = 0;
reader->cur = 0;
reader->allocs = XML_TEXTREADER_CTXT;
reader->doc = doc;
reader->state = XML_TEXTREADER_START;
if (reader->dict == NULL) {
if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
reader->dict = reader->ctxt->dict;
else
reader->dict = xmlDictCreate();
}
return(0);
}
int
xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
const char *URL, const char *encoding, int options)
{
int len;
if (cur == NULL)
return (-1);
if (reader == NULL)
return (-1);
len = xmlStrlen(cur);
return (xmlReaderNewMemory(reader, (const char *)cur, len,
URL, encoding, options));
}
int
xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
const char *encoding, int options)
{
xmlParserInputBufferPtr input;
if (filename == NULL)
return (-1);
if (reader == NULL)
return (-1);
input =
xmlParserInputBufferCreateFilename(filename,
XML_CHAR_ENCODING_NONE);
if (input == NULL)
return (-1);
return (xmlTextReaderSetup(reader, input, filename, encoding, options));
}
int
xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
const char *URL, const char *encoding, int options)
{
xmlParserInputBufferPtr input;
if (reader == NULL)
return (-1);
if (buffer == NULL)
return (-1);
input = xmlParserInputBufferCreateStatic(buffer, size,
XML_CHAR_ENCODING_NONE);
if (input == NULL) {
return (-1);
}
return (xmlTextReaderSetup(reader, input, URL, encoding, options));
}
int
xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
const char *URL, const char *encoding, int options)
{
xmlParserInputBufferPtr input;
if (fd < 0)
return (-1);
if (reader == NULL)
return (-1);
input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
if (input == NULL)
return (-1);
input->closecallback = NULL;
return (xmlTextReaderSetup(reader, input, URL, encoding, options));
}
int
xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
xmlInputCloseCallback ioclose, void *ioctx,
const char *URL, const char *encoding, int options)
{
xmlParserInputBufferPtr input;
if (ioread == NULL)
return (-1);
if (reader == NULL)
return (-1);
input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
XML_CHAR_ENCODING_NONE);
if (input == NULL)
return (-1);
return (xmlTextReaderSetup(reader, input, URL, encoding, options));
}
#ifdef NOT_USED_YET
static int
xmlBase64Decode(const unsigned char *in, unsigned long *inlen,
unsigned char *to, unsigned long *tolen)
{
unsigned long incur;
unsigned long inblk;
unsigned long outcur;
unsigned long inmax;
unsigned long outmax;
unsigned char cur;
unsigned char intmp[4], outtmp[4];
int nbintmp;
int is_ignore;
int is_end = 0;
int retval = 1;
int i;
if ((in == NULL) || (inlen == NULL) || (to == NULL) || (tolen == NULL))
return (-1);
incur = 0;
inblk = 0;
outcur = 0;
inmax = *inlen;
outmax = *tolen;
nbintmp = 0;
while (1) {
if (incur >= inmax)
break;
cur = in[incur++];
is_ignore = 0;
if ((cur >= 'A') && (cur <= 'Z'))
cur = cur - 'A';
else if ((cur >= 'a') && (cur <= 'z'))
cur = cur - 'a' + 26;
else if ((cur >= '0') && (cur <= '9'))
cur = cur - '0' + 52;
else if (cur == '+')
cur = 62;
else if (cur == '/')
cur = 63;
else if (cur == '.')
cur = 0;
else if (cur == '=')
is_end = 1;
else {
is_ignore = 1;
if (nbintmp == 0)
inblk = incur;
}
if (!is_ignore) {
int nbouttmp = 3;
int is_break = 0;
if (is_end) {
if (nbintmp == 0)
break;
if ((nbintmp == 1) || (nbintmp == 2))
nbouttmp = 1;
else
nbouttmp = 2;
nbintmp = 3;
is_break = 1;
}
intmp[nbintmp++] = cur;
if (nbintmp == 4) {
nbintmp = 0;
outtmp[0] = (intmp[0] << 2) | ((intmp[1] & 0x30) >> 4);
outtmp[1] =
((intmp[1] & 0x0F) << 4) | ((intmp[2] & 0x3C) >> 2);
outtmp[2] = ((intmp[2] & 0x03) << 6) | (intmp[3] & 0x3F);
if (outcur + 3 >= outmax) {
retval = 2;
break;
}
for (i = 0; i < nbouttmp; i++)
to[outcur++] = outtmp[i];
inblk = incur;
}
if (is_break) {
retval = 0;
break;
}
}
}
*tolen = outcur;
*inlen = inblk;
return (retval);
}
#if 0
int
main(int argc, char **argv)
{
char *input = " VW4 gcGV0 \n aXQgdGVzdCAuCg== ";
char output[100];
char output2[100];
char output3[100];
unsigned long inlen = strlen(input);
unsigned long outlen = 100;
int ret;
unsigned long cons, tmp, tmp2, prod;
ret = xmlBase64Decode(input, &inlen, output, &outlen);
output[outlen] = 0;
printf("ret: %d, inlen: %ld , outlen: %ld, output: '%s'\n", ret, inlen,
outlen, output)indent: Standard input:179: Error:Unmatched #endif
;
cons = 0;
prod = 0;
while (cons < inlen) {
tmp = 5;
tmp2 = inlen - cons;
printf("%ld %ld\n", cons, prod);
ret = xmlBase64Decode(&input[cons], &tmp2, &output2[prod], &tmp);
cons += tmp2;
prod += tmp;
printf("%ld %ld\n", cons, prod);
}
output2[outlen] = 0;
printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons,
prod, output2);
cons = 0;
prod = 0;
while (cons < inlen) {
tmp = 100 - prod;
tmp2 = inlen - cons;
if (tmp2 > 5)
tmp2 = 5;
printf("%ld %ld\n", cons, prod);
ret = xmlBase64Decode(&input[cons], &tmp2, &output3[prod], &tmp);
cons += tmp2;
prod += tmp;
printf("%ld %ld\n", cons, prod);
}
output3[outlen] = 0;
printf("ret: %d, cons: %ld , prod: %ld, output: '%s'\n", ret, cons,
prod, output3);
return (0);
}
#endif
#endif
#define bottom_xmlreader
#include "elfgcchack.h"
#endif