1. package org.xml.sax.helpers;
  2. /**
  3. * This code is designed to run on JDK version 1.1 and later including JVMs
  4. * that perform early linking like the Microsoft JVM in IE 5. Note however
  5. * that it must be compiled on a JDK version 1.2 or later system since it
  6. * calls Thread#getContextClassLoader(). The code also runs both as part
  7. * of an unbundled jar file and when bundled as part of the JDK.
  8. */
  9. class NewInstance {
  10. /**
  11. * Creates a new instance of the specified class name
  12. *
  13. * Package private so this code is not exposed at the API level.
  14. */
  15. static Object newInstance(String className)
  16. throws ClassNotFoundException, IllegalAccessException,
  17. InstantiationException
  18. {
  19. ClassLoader classLoader;
  20. try {
  21. // Construct the name of the concrete class to instantiate
  22. Class clazz = Class.forName(NewInstance.class.getName()
  23. + "$ClassLoaderFinderConcrete");
  24. ClassLoaderFinder clf = (ClassLoaderFinder) clazz.newInstance();
  25. classLoader = clf.getContextClassLoader();
  26. } catch (LinkageError le) {
  27. // Assume that we are running JDK 1.1, use the current ClassLoader
  28. classLoader = NewInstance.class.getClassLoader();
  29. } catch (ClassNotFoundException x) {
  30. // This case should not normally happen. MS IE can throw this
  31. // instead of a LinkageError the second time Class.forName() is
  32. // called so assume that we are running JDK 1.1 and use the
  33. // current ClassLoader
  34. classLoader = NewInstance.class.getClassLoader();
  35. }
  36. Class driverClass;
  37. if (classLoader == null) {
  38. driverClass = Class.forName(className);
  39. } else {
  40. driverClass = classLoader.loadClass(className);
  41. }
  42. return driverClass.newInstance();
  43. }
  44. /*
  45. * The following nested classes allow getContextClassLoader() to be
  46. * called only on JDK 1.2 and yet run in older JDK 1.1 JVMs
  47. */
  48. private static abstract class ClassLoaderFinder {
  49. abstract ClassLoader getContextClassLoader();
  50. }
  51. static class ClassLoaderFinderConcrete extends ClassLoaderFinder {
  52. ClassLoader getContextClassLoader() {
  53. return Thread.currentThread().getContextClassLoader();
  54. }
  55. }
  56. }