You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Chuck Simpson <ch...@adhesive.com> on 2000/01/05 02:19:57 UTC

ClassCastException in org.apache.xerces.dom.DeferredDocumentImpl

I am getting a ClassCastException every time I call getIdentifiers() on my
Document. The exception occurs at line 1176 in the DeferredDocumentImpl
class' synchronizeData() method. The code at that line is:

Element element = (Element)place;

A few lines above that line, the variable place is declared and initialized as:

Node place = this;

I stuck some code in to print out the inheritance tree for the variable place
just before the line where the exception occurs. A printout is included below.
Sure enough, there is no mention of the org.w3c.dom.Element interface in
the inheritance tree. So the widening cast from DeferredDocumentImpl to
Node works, but a narrowing cast from DeferredDocumentImpl to Element
does not work. I am sure the writer thought this was a narrowing cast from
Node to Element as I did when I first looked at it. It just doesn't work that
way though.

I replaced

Element element = (Element)place;

with

Element element;
if( place instanceof Document )
    element = this.getDocumentElement();
else
    element = (Element)place;

and I no longer get the exception. I think this should work, but I would prefer
someone with more Xerces history check it and commit it.

Sorry to be so longwinded, but this was just not that obvious and I wanted to
describe the issue and resolution clearly.

Thanks,
Chuck Simpson
chuckls@adhesive.com


Here is the inheritance printout:

    place is of type: org.apache.xerces.dom.DeferredDocumentImpl
    The superclass of org.apache.xerces.dom.DeferredDocumentImpl is:
                                                        org.apache.xerces.dom.DocumentImpl
    Class org.apache.xerces.dom.DocumentImpl implements: org.w3c.dom.Document
    Class org.apache.xerces.dom.DocumentImpl implements:    
                                                        org.apache.xerces.domx.traversal.DocumentTraversal
    Class org.apache.xerces.dom.DocumentImpl implements: org.apache.xerces.domx.events.DocumentEvent
    The superclass of org.apache.xerces.dom.DocumentImpl is: org.apache.xerces.dom.NodeImpl
    Class org.apache.xerces.dom.NodeImpl implements: org.w3c.dom.Node
    Class org.apache.xerces.dom.NodeImpl implements: org.w3c.dom.NodeList
    Class org.apache.xerces.dom.NodeImpl implements: org.apache.xerces.domx.events.EventTarget
    Class org.apache.xerces.dom.NodeImpl implements: java.lang.Cloneable
    Class org.apache.xerces.dom.NodeImpl implements: java.io.Serializable
    The superclass of org.apache.xerces.dom.NodeImpl is: java.lang.Object
    Class org.apache.xerces.dom.DeferredDocumentImpl implements: org.apache.xerces.dom.DeferredNode

java.lang.ClassCastException
        at org.apache.xerces.dom.DeferredDocumentImpl.synchronizeData(DeferredDocumentImpl.java, Compiled Code)
        at org.apache.xerces.dom.DocumentImpl.getIdentifier(DocumentImpl.java:827)
        at com.adhesive.glyph.TestXml.processChild(TestXml.java, Compiled Code)
        at com.adhesive.glyph.TestXml.processChild(TestXml.java, Compiled Code)
        at com.adhesive.glyph.TestXml.processChild(TestXml.java, Compiled Code)
        at com.adhesive.glyph.TestXml.processChild(TestXml.java, Compiled Code)
        at com.adhesive.glyph.TestXml.init(TestXml.java, Compiled Code)
        at com.adhesive.glyph.TestXml.main(TestXml.java:179)

Re: ClassCastException in org.apache.xerces.dom.DeferredDocumentImpl

Posted by Andy Clark <an...@apache.org>.
Chuck,

> I am getting a ClassCastException every time I call getIdentifiers()
> on my Document. The exception occurs at line 1176 in the
> DeferredDocumentImpl class' synchronizeData() method. The code at 
> that line is:

I am not able to reproduce your bug with the latest code in
CVS. Please verify with the latest source code and if you 
still see the error, send me a sample program.

> Element element;
> if( place instanceof Document )
>     element = this.getDocumentElement();
> else
>     element = (Element)place;

This is not a good fix. The purpose of the previous loop
is to arrive at the element which contains the ID attribute.
This is not always the document element. If place is 
becoming set to the Document reference, then there is a
bug in the algorithm that needs to be fixed. But since I
can't duplicate, I'll have to wait until I get a sample
program.

-- 
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org