[jdom-interest] ClassCastException

Elliotte Rusty Harold elharo at metalab.unc.edu
Wed Jun 26 20:17:17 PDT 2002


At 8:13 PM -0400 6/26/02, Elliotte Rusty Harold wrote:

>FYI, I've just been pulling my hair out trying to debug something 
>similar, also in JDK 1.3.1 on Linux. I'm going to try to track it 
>down. It would be helpful to know which version of xerces.jar is 
>currently bundled with JDOM.


I've spent a little time tracking this down and I still don't 
understand it, but I do think it's a bug, and I do think I have a 
fix. The problem, IMHO, is in SAXBuilder.createParser(), specifically 
this line:


// This next line sometimes throws a ClassCastException; I'm not
// sure why.
parser = (XMLReader) getXMLReader.invoke(jaxpParser, null);

The work around is to catch that exception:

             } catch (ClassNotFoundException e) {
                 //e.printStackTrace();
             } catch (InvocationTargetException e) {
                 //e.printStackTrace();
             } catch (NoSuchMethodException e) {
                 //e.printStackTrace();
             } catch (IllegalAccessException e) {
                 //e.printStackTrace();
             }
             catch (ClassCastException e) {
                 // This fixes the problem with the ClassCastException
                 //e.printStackTrace();
             }


Alterantely we could just catch any exception:

             catch (Exception e) {
                 //e.printStackTrace();
             }

Either case would result in falling back to the default parser.

Also, all the reflection in this method offends my sense of code 
aesthetics. Do we really want this code to compile or run in an 
environment where JAXP is not available? If so, I would prefer to 
remove the references to JAXP completely, and rely exclusively on 
XMLReaderFactory. One thing we do not currently attempt to do is load 
the parser default XMLReader. i.e. we should change

         if (parser == null) {
             try {
                 parser = XMLReaderFactory.createXMLReader(DEFAULT_SAX_DRIVER);
                 // System.out.println("using default " + DEFAULT_SAX_DRIVER);
                 saxDriverClass = parser.getClass().getName();
             }
             catch (SAXException e) {
                 throw new JDOMException("Could not load default SAX parser: "
                   + DEFAULT_SAX_DRIVER, e);
             }
         }


to

         if (parser == null) {
             try {
                 parser = XMLReaderFactory.createXMLReader(DEFAULT_SAX_DRIVER);
                 // System.out.println("using default " + DEFAULT_SAX_DRIVER);
                 saxDriverClass = parser.getClass().getName();
             }
             catch (SAXException e) {
               try {
                  parser = XMLReaderFactory.createXMLReader();
               }
               catch (SAXException ee) {
                 throw new JDOMException("Could not load default SAX parser: "
                   + DEFAULT_SAX_DRIVER, e);
               }
             }
         }


We could even store a list of well-known parser classes and try to 
load them up in a certain order (e.g. Look for Xerces, Crimson, 
oracle, Piccolo, AElfred, etc. until we find one or give up.)
-- 

+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
|          XML in a  Nutshell, 2nd Edition (O'Reilly, 2002)          |
|              http://www.cafeconleche.org/books/xian2/              |
|  http://www.amazon.com/exec/obidos/ISBN%3D0596002920/cafeaulaitA/  |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://www.cafeaulait.org/      |
|  Read Cafe con Leche for XML News: http://www.cafeconleche.org/    |
+----------------------------------+---------------------------------+



More information about the jdom-interest mailing list