KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > Loader


1 package com.icl.saxon;
2
3 import java.lang.ClassNotFoundException JavaDoc;
4 import java.lang.IllegalAccessException JavaDoc;
5 import java.lang.InstantiationException JavaDoc;
6 import java.lang.SecurityException JavaDoc;
7 import java.lang.ClassCastException JavaDoc;
8
9 import javax.xml.transform.TransformerException JavaDoc;
10
11
12 /**
13   * Loader is used to load a class given its name.
14   * The implementation varies in different Java environments.
15   *
16   * @author Michael H. Kay (mhkay@iclway.co.uk)
17   */

18
19  
20 public class Loader {
21
22     private static boolean tracing = false;
23
24     /**
25     * Switch tracing on or off
26     */

27
28     public synchronized static void setTracing(boolean onOrOff) {
29         tracing = onOrOff;
30     }
31       
32     /**
33     * Load a class using the class name provided.<br>
34     * Note that the method does not check that the object is of the right class.
35     * @param className A string containing the name of the
36     * class, for example "com.microstar.sax.LarkDriver"
37     * @return an instance of the class named, or null if it is not
38     * loadable.
39     * @throw an exception if the class cannot be loaded.
40     *
41     */

42     
43     public static Class JavaDoc getClass(String JavaDoc className) throws TransformerException JavaDoc
44     {
45         if (tracing) {
46             System.err.println("Loading " + className);
47         }
48         if (Version.isPreJDK12()) {
49             try {
50                 return Class.forName(className);
51             }
52             catch (Exception JavaDoc e) {
53                 throw new TransformerException JavaDoc("Failed to load " + className, e );
54             }
55         } else {
56
57             try {
58                 ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
59                 if (loader!=null) {
60                     try {
61                         return loader.loadClass(className);
62                     } catch (Exception JavaDoc ex) {
63                         return Class.forName(className);
64                     }
65                 } else {
66                     return Class.forName(className);
67                 }
68             }
69             catch (Exception JavaDoc e) {
70                 throw new TransformerException JavaDoc("Failed to load " + className, e );
71             }
72
73         }
74
75     }
76
77   /**
78     * Instantiate a class using the class name provided.<br>
79     * Note that the method does not check that the object is of the right class.
80     * @param className A string containing the name of the
81     * class, for example "com.microstar.sax.LarkDriver"
82     * @return an instance of the class named, or null if it is not
83     * loadable.
84     * @throw an exception if the class cannot be loaded.
85     *
86     */

87     
88     public static Object JavaDoc getInstance(String JavaDoc className) throws TransformerException JavaDoc {
89         Class JavaDoc theclass = getClass(className);
90         try {
91             return theclass.newInstance();
92         } catch (Exception JavaDoc err) {
93             throw new TransformerException JavaDoc("Failed to instantiate class " + className, err);
94         }
95     }
96
97
98 }
99
100 //
101
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
102
// you may not use this file except in compliance with the License. You may obtain a copy of the
103
// License at http://www.mozilla.org/MPL/
104
//
105
// Software distributed under the License is distributed on an "AS IS" basis,
106
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
107
// See the License for the specific language governing rights and limitations under the License.
108
//
109
// The Original Code is: all this file, other than fragments copied from the SAX distribution
110
// made available by David Megginson, and the line marked PB-SYNC.
111
//
112
// The Initial Developer of the Original Code is
113
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
114
//
115
// The line marked PB-SYNC is by Peter Bryant (pbryant@bigfoot.com). All Rights Reserved.
116
//
117
// Contributor(s): Michael Kay, Peter Bryant, David Megginson
118
//
119
Popular Tags