From e18fc185fa2604ba73f2b259e34796c106ad5545 Mon Sep 17 00:00:00 2001
From: Daniel Veillard
Date: Sat, 28 Dec 2002 22:56:33 +0000
Subject: [PATCH] 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
---
ChangeLog | 11 ++++
doc/libxml2-api.xml | 14 +++++
include/libxml/xmlreader.h | 7 ++-
python/libxml.py | 1 +
python/libxml2class.txt | 2 +
python/tests/Makefile.am | 4 +-
python/tests/reader.py | 3 ++
python/tests/reader2.py | 2 +-
python/tests/reader3.py | 107 +++++++++++++++++++++++++++++++++++++
xmlreader.c | 47 ++++++++++++++++
10 files changed, 194 insertions(+), 4 deletions(-)
create mode 100755 python/tests/reader3.py
diff --git a/ChangeLog b/ChangeLog
index 0f24c4ad..f7edb258 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+Sat Dec 28 23:49:12 CET 2002 Daniel Veillard
+
+ * 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
* python/generator.py python/libxml2class.txt
diff --git a/doc/libxml2-api.xml b/doc/libxml2-api.xml
index d92e9ea4..b680aceb 100644
--- a/doc/libxml2-api.xml
+++ b/doc/libxml2-api.xml
@@ -1092,6 +1092,7 @@
+
@@ -1101,6 +1102,8 @@
+
+
@@ -2382,6 +2385,7 @@
+
@@ -7917,6 +7921,16 @@ actually an xmlCharEncoding'/>
+
+ 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.
+
+
+
+
+ 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.
+
+
+
The depth of the node in the tree.
diff --git a/include/libxml/xmlreader.h b/include/libxml/xmlreader.h
index fbe1de0f..457dcd52 100644
--- a/include/libxml/xmlreader.h
+++ b/include/libxml/xmlreader.h
@@ -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
diff --git a/python/libxml.py b/python/libxml.py
index 8e5ba2a2..a1b5a7bb 100644
--- a/python/libxml.py
+++ b/python/libxml.py
@@ -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
diff --git a/python/libxml2class.txt b/python/libxml2class.txt
index 93c2e9d7..2fbc7077 100644
--- a/python/libxml2class.txt
+++ b/python/libxml2class.txt
@@ -565,6 +565,8 @@ Class xmlTextReader()
AttributeCount()
BaseUri()
Close()
+ CurrentDoc()
+ CurrentNode()
Depth()
GetAttribute()
GetAttributeNo()
diff --git a/python/tests/Makefile.am b/python/tests/Makefile.am
index de1ef3be..6ac6ab17 100644
--- a/python/tests/Makefile.am
+++ b/python/tests/Makefile.am
@@ -21,7 +21,9 @@ PYTESTS= \
resolver.py \
regexp.py \
reader.py \
- reader2.py
+ reader2.py \
+ reader3.py
+
XMLS= \
tst.xml \
diff --git a/python/tests/reader.py b/python/tests/reader.py
index b9e351ca..7b7dcc85 100755
--- a/python/tests/reader.py
+++ b/python/tests/reader.py
@@ -1,4 +1,7 @@
#!/usr/bin/python -u
+#
+# this tests the basic APIs of the XmlTextReader interface
+#
import libxml2
import StringIO
import sys
diff --git a/python/tests/reader2.py b/python/tests/reader2.py
index 3a49b50a..82e46bd6 100755
--- a/python/tests/reader2.py
+++ b/python/tests/reader2.py
@@ -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
diff --git a/python/tests/reader3.py b/python/tests/reader3.py
new file mode 100755
index 00000000..1593afb6
--- /dev/null
+++ b/python/tests/reader3.py
@@ -0,0 +1,107 @@
+#!/usr/bin/python -u
+#
+# this tests the validation with the XmlTextReader interface
+#
+import sys
+import StringIO
+import libxml2
+
+docstr="""
+test
">
+]>
+&tst;"""
+
+# 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()
diff --git a/xmlreader.c b/xmlreader.c
index e36bbf35..9640414a 100644
--- a/xmlreader.c
+++ b/xmlreader.c
@@ -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 *