extended the XmlTextReader API a bit, addding accessors for the current

* xmlreader.c include/libxml/xmlreader.h doc/libxml2-api.xml:
  extended the XmlTextReader API a bit, addding accessors for
  the current doc and node, and an entity substitution mode for
  the parser.
* python/libxml.py python/libxml2class.txt: related updates
* python/tests/Makefile.am python/tests/reader.py
  python/tests/reader2.py python/tests/reader3.py: updated a bit
  the old tests and added a new one to test the entities handling
Daniel
This commit is contained in:
Daniel Veillard 2002-12-28 22:56:33 +00:00
parent aba976d825
commit e18fc185fa
10 changed files with 194 additions and 4 deletions

View File

@ -1,3 +1,14 @@
Sat Dec 28 23:49:12 CET 2002 Daniel Veillard <daniel@veillard.com>
* xmlreader.c include/libxml/xmlreader.h doc/libxml2-api.xml:
extended the XmlTextReader API a bit, addding accessors for
the current doc and node, and an entity substitution mode for
the parser.
* python/libxml.py python/libxml2class.txt: related updates
* python/tests/Makefile.am python/tests/reader.py
python/tests/reader2.py python/tests/reader3.py: updated a bit
the old tests and added a new one to test the entities handling
Sat Dec 28 22:11:57 CET 2002 Daniel Veillard <daniel@veillard.com>
* python/generator.py python/libxml2class.txt

View File

@ -1092,6 +1092,7 @@
<file name='xmlreader'>
<exports symbol='XML_PARSER_DEFAULTATTRS'/>
<exports symbol='XML_PARSER_LOADDTD'/>
<exports symbol='XML_PARSER_SUBST_ENTITIES'/>
<exports symbol='XML_PARSER_VALIDATE'/>
<exports symbol='xmlFreeTextReader'/>
<exports symbol='xmlNewTextReader'/>
@ -1101,6 +1102,8 @@
<exports symbol='xmlTextReaderAttributeCount'/>
<exports symbol='xmlTextReaderBaseUri'/>
<exports symbol='xmlTextReaderClose'/>
<exports symbol='xmlTextReaderCurrentDoc'/>
<exports symbol='xmlTextReaderCurrentNode'/>
<exports symbol='xmlTextReaderDepth'/>
<exports symbol='xmlTextReaderGetAttribute'/>
<exports symbol='xmlTextReaderGetAttributeNo'/>
@ -2382,6 +2385,7 @@
<enum name='XML_PARSER_PUBLIC_LITERAL' file='parser' value='16' type='xmlParserInputState' info=' within a PUBLIC value'/>
<enum name='XML_PARSER_START' file='parser' value='0' type='xmlParserInputState' info='nothing has been parsed'/>
<enum name='XML_PARSER_START_TAG' file='parser' value='6' type='xmlParserInputState' info='within a start tag'/>
<enum name='XML_PARSER_SUBST_ENTITIES' file='xmlreader' value='4' type='xmlParserProperties'/>
<enum name='XML_PARSER_SYSTEM_LITERAL' file='parser' value='13' type='xmlParserInputState' info='within a SYSTEM value'/>
<enum name='XML_PARSER_VALIDATE' file='xmlreader' value='3' type='xmlParserProperties'/>
<enum name='XML_PI_NODE' file='tree' value='7' type='xmlElementType'/>
@ -7917,6 +7921,16 @@ actually an xmlCharEncoding'/>
<return type='int' info='0 or -1 in case of error'/>
<arg name='reader' type='xmlTextReaderPtr' info='the xmlTextReaderPtr used'/>
</function>
<function name='xmlTextReaderCurrentDoc' file='xmlreader'>
<info>Hacking interface allowing to get the xmlDocPtr correponding to the current document being accessed by the xmlTextReader. This is dangerous because the associated node may be destroyed on the next Reads.</info>
<return type='xmlDocPtr' info='the xmlDocPtr or NULL in case of error.'/>
<arg name='reader' type='xmlTextReaderPtr' info='the xmlTextReaderPtr used'/>
</function>
<function name='xmlTextReaderCurrentNode' file='xmlreader'>
<info>Hacking interface allowing to get the xmlNodePtr correponding to the current node being accessed by the xmlTextReader. This is dangerous because the underlying node may be destroyed on the next Reads.</info>
<return type='xmlNodePtr' info='the xmlNodePtr or NULL in case of error.'/>
<arg name='reader' type='xmlTextReaderPtr' info='the xmlTextReaderPtr used'/>
</function>
<function name='xmlTextReaderDepth' file='xmlreader'>
<info>The depth of the node in the tree.</info>
<return type='int' info='the depth or -1 in case of error'/>

View File

@ -18,8 +18,9 @@ extern "C" {
typedef enum {
XML_PARSER_LOADDTD = 1,
XML_PARSER_DEFAULTATTRS,
XML_PARSER_VALIDATE
XML_PARSER_DEFAULTATTRS = 2,
XML_PARSER_VALIDATE = 3,
XML_PARSER_SUBST_ENTITIES = 4
} xmlParserProperties;
typedef struct _xmlTextReader xmlTextReader;
@ -96,6 +97,8 @@ int xmlTextReaderSetParserProp (xmlTextReaderPtr reader,
int value);
int xmlTextReaderGetParserProp (xmlTextReaderPtr reader,
int prop);
xmlNodePtr xmlTextReaderCurrentNode (xmlTextReaderPtr reader);
xmlDocPtr xmlTextReaderCurrentDoc (xmlTextReaderPtr reader);
#ifdef __cplusplus
}
#endif

View File

@ -378,6 +378,7 @@ def registerXPathFunction(ctxt, name, ns_uri, f):
PARSER_LOADDTD=1
PARSER_DEFAULTATTRS=2
PARSER_VALIDATE=3
PARSER_SUBST_ENTITIES=4
#
# Everything below this point is automatically generated

View File

@ -565,6 +565,8 @@ Class xmlTextReader()
AttributeCount()
BaseUri()
Close()
CurrentDoc()
CurrentNode()
Depth()
GetAttribute()
GetAttributeNo()

View File

@ -21,7 +21,9 @@ PYTESTS= \
resolver.py \
regexp.py \
reader.py \
reader2.py
reader2.py \
reader3.py
XMLS= \
tst.xml \

View File

@ -1,4 +1,7 @@
#!/usr/bin/python -u
#
# this tests the basic APIs of the XmlTextReader interface
#
import libxml2
import StringIO
import sys

View File

@ -1,6 +1,6 @@
#!/usr/bin/python -u
#
# this tests the validation with the XmlTextReader interface
# this tests the DTD validation with the XmlTextReader interface
#
import sys
import glob

107
python/tests/reader3.py Executable file
View File

@ -0,0 +1,107 @@
#!/usr/bin/python -u
#
# this tests the validation with the XmlTextReader interface
#
import sys
import StringIO
import libxml2
docstr="""<?xml version='1.0'?>
<!DOCTYPE doc [
<!ENTITY tst "<p>test</p>">
]>
<doc>&tst;</doc>"""
# Memory debug specific
libxml2.debugMemory(1)
#
# First test, normal don't substitute entities.
#
f = StringIO.StringIO(docstr)
input = libxml2.inputBuffer(f)
reader = input.newTextReader("test_noent")
ret = reader.Read()
if ret != 1:
print "Error reading to root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 1:
print "test_normal: Error reading the root element"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_normal: Error reading to the entity"
sys.exit(1)
if reader.Name() != "tst" or reader.NodeType() != 5:
print "test_normal: Error reading the entity"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_normal: Error reading to the end of root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 15:
print "test_normal: Error reading the end of the root element"
sys.exit(1)
ret = reader.Read()
if ret != 0:
print "test_normal: Error detecting the end"
sys.exit(1)
#
# Second test, completely substitute the entities.
#
f = StringIO.StringIO(docstr)
input = libxml2.inputBuffer(f)
reader = input.newTextReader("test_noent")
reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
ret = reader.Read()
if ret != 1:
print "Error reading to root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 1:
print "test_noent: Error reading the root element"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the entity content"
sys.exit(1)
if reader.Name() != "p" or reader.NodeType() != 1:
print "test_noent: Error reading the p element from entity"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the text node"
sys.exit(1)
if reader.NodeType() != 3 or reader.Value() != "test":
print "test_noent: Error reading the text node"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the end of p element"
sys.exit(1)
if reader.Name() != "p" or reader.NodeType() != 15:
print "test_noent: Error reading the end of the p element"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the end of root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 15:
print "test_noent: Error reading the end of the root element"
sys.exit(1)
ret = reader.Read()
if ret != 0:
print "test_noent: Error detecting the end"
sys.exit(1)
del f
del input
del reader
# Memory debug specific
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
print "OK"
else:
print "Memory leak %d bytes" % (libxml2.debugMemory(1))
libxml2.dumpMemory()

View File

@ -1864,6 +1864,13 @@ xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
ctxt->validate = 0;
}
return(0);
case XML_PARSER_SUBST_ENTITIES:
if (value != 0) {
ctxt->replaceEntities = 1;
} else {
ctxt->replaceEntities = 0;
}
return(0);
}
return(-1);
}
@ -1897,10 +1904,50 @@ xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
return(0);
case XML_PARSER_VALIDATE:
return(ctxt->validate);
case XML_PARSER_SUBST_ENTITIES:
return(ctxt->replaceEntities);
}
return(-1);
}
/**
* xmlTextReaderCurrentNode:
* @reader: the xmlTextReaderPtr used
*
* Hacking interface allowing to get the xmlNodePtr correponding to the
* current node being accessed by the xmlTextReader. This is dangerous
* because the underlying node may be destroyed on the next Reads.
*
* Returns the xmlNodePtr or NULL in case of error.
*/
xmlNodePtr
xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
if (reader == NULL)
return(NULL);
if (reader->curnode != NULL)
return(reader->curnode);
return(reader->node);
}
/**
* xmlTextReaderCurrentDoc:
* @reader: the xmlTextReaderPtr used
*
* Hacking interface allowing to get the xmlDocPtr correponding to the
* current document being accessed by the xmlTextReader. This is dangerous
* because the associated node may be destroyed on the next Reads.
*
* Returns the xmlDocPtr or NULL in case of error.
*/
xmlDocPtr
xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
if ((reader == NULL) || (reader->ctxt == NULL))
return(NULL);
return(reader->ctxt->myDoc);
}
/************************************************************************
* *
* Utilities *