Wolfhart Bauer wrote:
>
> I have discovered a bug in the class org.apache.xerces.dom.DeepNodeListImpl.
> The
> attached code demonstrates it, just try switching the order of the two println
> lines. It seems that you have to call the getLength() method at least once
> before accessing any of the items directly. I don't feel that this behavior is
> intentional.
>
> public class XMLTest
> {
> public static void main (String[] args)
> throws Exception
> {
> org.apache.xerces.parsers.DOMParser parser = new
> org.apache.xerces.parsers.DOMParser();
> parser.parse("file:///d:/testing/xercestest/test.xml");
> org.w3c.dom.Node d = parser.getDocument();
> org.w3c.dom.NodeList nl = d.getChildNodes();
> System.out.println("Node 0 is an element?
> "+(nl.item(0).getNodeType()==org.w3c.dom.Node.ELEMENT_NODE));
> System.out.println("Length of nodelist: "+nl.getLength());
> }
> }
>
> Executed on the file test.xml:
> <?xml version="1.0"?>
> <!DOCTYPE TEST SYSTEM "test.dtd">
> <TEST/>
First, this code does not actually use the DeepNodeListImpl class at
all. getChildNodes() is implemented by the NodeContainer class.
Second, the first node of your document is not an element. It's a
doctype.
This can easily be seen in changing your first test with the following:
System.out.println("Node 0 is a doctype? "
+(nl.item(0).getNodeType()==org.w3c.dom.Node.DOCUMENT_TYPE_NODE));
System.out.println("Node 1 is an element? "
+(nl.item(1).getNodeType()==org.w3c.dom.Node.ELEMENT_NODE));
This said, you're right about getLength(). The cache is wrong. The patch
is attached.
--
Arnaud Le Hors - IBM Cupertino, XML Technology Group
Index: NodeContainer.java
===================================================================
RCS file:
/home/cvs/xml-xerces/java/src/org/apache/xerces/dom/NodeContainer.java,v
retrieving revision 1.6
diff -c -r1.6 NodeContainer.java
*** NodeContainer.java 2000/02/22 00:14:56 1.6
--- NodeContainer.java 2000/02/24 02:08:05
***************
*** 97,103 ****
protected transient int nodeListChanges = -1;
/** Cached node list length. */
! protected transient int nodeListLength;
/** Last requested node. */
protected transient NodeImpl nodeListNode;
--- 97,103 ----
protected transient int nodeListChanges = -1;
/** Cached node list length. */
! protected transient int nodeListLength = -1;
/** Last requested node. */
protected transient NodeImpl nodeListNode;
***************
*** 740,746 ****
*/
public int getLength() {
! if (nodeListChanges != changes) {
nodeListChanges = changes;
nodeListLength = 0;
nodeListIndex = 0;
--- 740,746 ----
*/
public int getLength() {
! if (nodeListChanges != changes || nodeListLength == -1) {
nodeListChanges = changes;
nodeListLength = 0;
nodeListIndex = 0;