All,

I posted this email to the xerces-j-user list earlier today, but haven't had any
joy as yet.  Since then, I've checked out the latest version from CVS and applied
the same tests with no joy:-

apologies if this is an FAQ, but I haven't been able to track down what the 
solution to this one is either in the docs or the mailing lists.

I have a small code snippet that tries to create a DOMImplementation like so:-

import org.apache.xerces.dom3.bootstrap.*;
import org.w3c.dom.*;

public class Test2
{
  public static void main(String[] args) 
    throws Exception
  {
    System.setProperty(DOMImplementationRegistry.PROPERTY,
                       "org.apache.xerces.dom.DOMImplementationSourceImpl");
    DOMImplementationRegistry registry 
      = DOMImplementationRegistry.newInstance();
    DOMImplementation impl 
      = (DOMImplementation) registry.getDOMImplementation("LS");
  }
}

Unfortunately this fails with the following:-
Exception in thread "main" java.lang.ClassCastException
        at 
org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown
 Source)
        at Test2.main(Test2.java:12)


I am using the following version of java:-
[EMAIL PROTECTED]:~/java/src> java -version
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

and am invoking the command utilising:-
java -Xbootclasspath/p:/home/alex/java/lib/dom3-xml-apis.jar Test2

If I run the application utilising the -verbose flag, and filter out all 
non-DOM related lines I get the following:-

[EMAIL PROTECTED]:~> java -Xbootclasspath/p:/home/alex/java/lib/dom3-xml-apis.jar 
-verbose Test2 | grep DOM
[Loaded org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry]
[Loaded org.apache.xerces.dom3.DOMImplementationList]
[Loaded org.w3c.dom.DOMImplementationSource from /home/alex/java/lib/dom3-xml-apis.jar]
[Loaded org.apache.xerces.dom.DOMImplementationSourceImpl]
[Loaded org.w3c.dom.DOMImplementationList from /home/alex/java/lib/dom3-xml-apis.jar]
[Loaded org.apache.xerces.dom3.DOMImplementationSource]
Exception in thread "main" java.lang.ClassCastException
        at 
org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown
 Source)
        at Test2.main(Test2.java:13)

and without the bootclasspath option gives me:-

[EMAIL PROTECTED]:~> java -verbose:class Test2 | grep DOM
[Loaded org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry]
[Loaded org.apache.xerces.dom3.DOMImplementationList]
[Loaded org.w3c.dom.DOMImplementationSource]
[Loaded org.apache.xerces.dom.DOMImplementationSourceImpl]
[Loaded org.w3c.dom.DOMImplementationList]
[Loaded org.apache.xerces.dom3.DOMImplementationSource]
Exception in thread "main" java.lang.ClassCastExceptionisnt instanceof 
DOMImplementationSource
        at 
org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown
 Source)
        at Test2.main(Test2.java:13)

which implies that the bootclasspath is forcing the bootstrap ClassLoader 
to load the DOMImplementationSource and the DOMImplementationList with all 
the other classes being loaded by some other ClassLoader (that doesn't 
specify which location they are being got from).  At least the org.w3c.dom 
classes are being got from the appropriate xerces jar which is a step in the 
right direction.

I then did a little bit of hacking around and added some debugging information 
into the DOMImplementationRegistry, to debug a bit what is going on, namely this:-

  public static void debug(Class c,
                           StringBuffer buf) 
  {
    buf.append("debug(")
      .append(c.getName())
      .append(")\n");
    buf.append("  .getSuperclass() [ ")
      .append(c.getSuperclass() == null
              ? "null"
              : c.getSuperclass().getName())
      .append(" ]\n");
    buf.append("  .getClassLoader() [ ")
      .append(c.getClassLoader())
      .append(" ]\n");
    buf.append("  .getInterfaces() [ ");
    Class[] classes = c.getInterfaces();
    for (int i = 0; i < classes.length; i++) {
      buf.append(classes[i].getName())
        .append(".getClassLoader()=")
        .append(classes[i].getClassLoader())
        .append(" ");
    }
    buf.append("]");
  }
  
  public static String debug(Class c) 
  {
    StringBuffer buf = new StringBuffer();
    debug(c,
          buf);
    return buf.toString();
  }

and then added this into the DOMImplementationRegistry.newInstance() method to
give some feedback on what is being created like so:-

  public static DOMImplementationRegistry newInstance()                 
    throws ClassNotFoundException, InstantiationException, IllegalAccessException
  {
    Vector _sources = new Vector();      
    // fetch system property:
    String p = System.getProperty(PROPERTY);
    if (p != null) {
      StringTokenizer st = new StringTokenizer(p);
      while (st.hasMoreTokens()) {
        String sourceName = st.nextToken();
        // Use context class loader, falling back to Class.forName
        // if and only if this fails...
        Object source = getClass(sourceName).newInstance();
        System.out.println(debug(source.getClass()));
        System.out.println((source instanceof DOMImplementationSource)
                           ? "is instanceof DOMImplementationSource"
                           : "isnt instanceof DOMImplementationSource");
        _sources.add(source);
      }
    }
    return new DOMImplementationRegistry(_sources);
  }

Now my both my bootclasspath option and default options output the following:-

debug(org.apache.xerces.dom.DOMImplementationSourceImpl)
  .getSuperclass() [ java.lang.Object ]
  .getClassLoader() [ [EMAIL PROTECTED] ]
  .getInterfaces() [ org.w3c.dom.DOMImplementationSource.getClassLoader()=null ]
isnt instanceof DOMImplementationSource
Exception in thread "main" java.lang.ClassCastException
        at 
org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry.getDOMImplementation(Unknown
 Source)
        at Test2.main(Test2.java:13)

The alarm bell for me was that although the DOMImplementationsSourceImpl had
been created with a sun.misc.Launcher$AppClassLoader ClassLoader, the
DOMImplementationSource interface had been instantiation by the bootstrap
ClassLoader.

Then when we say "source instanceof DOMImplementationSource", this will utilise
the default ClassLoader for the DOMImplementationRegistry (which is not the
bootstrap ClassLoader from the -verbose tests above) and create a seperate
instance of the DOMImplementationSource interface for the equivalent ClassLoader.
This is therefore not equal to the bootstrap provided instance of 
DOMImplementationSource
and therefore the result of "source instanceof DOMImplementationSource" is false.

This is all pretty new to me, but I am reasonably convinced that my steps in
getting to this point are relatively sane (and if they are not then would be
very grateful for anyones rapid enlightenment) and I am now in a position where
I do not know how to move forwards.  Is this a quirk of the JVM that I am using
and will it be necessary to downgrade to an earlier version or is there a more
complex underlying problem.

Many thanks for anyones help.

Alex

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to