1
2
3
4 package net.sf.tomp.xtcl;
5
6 import java.util.StringTokenizer;
7
8 /***
9 * Simple CompilerFactory, creates Compilers, either the standard ones or
10 * of the specified class
11 *
12 * @author tomp
13 */
14 public class CompilerFactory {
15
16 /***
17 * Create the default Compiler. Class of the default Compiler may
18 * be specified via System.property "net.sf.tomp.xtcl.Compiler"
19 *
20 * @param context Context to be set to the new Compiler
21 * @throws InstantiationException
22 * @throws IllegalAccessException
23 * @throws ClassNotFoundException if Compiler class initialization fails
24 */
25 public static void createDefaultCompilers(Context context)
26 throws InstantiationException, IllegalAccessException,
27 ClassNotFoundException {
28 String compClassNames = System.getProperty("net.sf.tomp.xtcl.Compiler",
29 "net.sf.tomp.xtcl.XTCompiler");
30 StringTokenizer st = new StringTokenizer(compClassNames, " :;,\t\n\r\f");
31
32 while (st.hasMoreTokens()) {
33 createCompiler(st.nextToken(), context);
34 }
35 }
36
37 /***
38 * Similar to createDefaultCompilers but creates Compiler
39 * with the specified classname compClassName
40 *
41 * @param compClassName Compiler class name
42 * @param context Context to be set to the new Compiler
43 * @return the created Compiler
44 *
45 * @throws InstantiationException
46 * @throws IllegalAccessException
47 * @throws ClassNotFoundException if Compiler class initialization fails
48 */
49 public static Compiler createCompiler(String compClassName, Context context)
50 throws InstantiationException, IllegalAccessException,
51 ClassNotFoundException {
52 Class compClass = Class.forName(compClassName);
53
54 return createCompiler(compClass, context);
55 }
56
57 /***
58 * Similar to createCompiler but creates Compiler
59 * with the specified Class compClass
60
61 * @param compClass the Compiler class
62 * @param context
63 * @return the created Compiler
64 *
65 * @throws InstantiationException
66 * @throws IllegalAccessException
67 * @throws ClassNotFoundException if Compiler class initialization fails
68 */
69 public static Compiler createCompiler(Class compClass, Context context)
70 throws InstantiationException, IllegalAccessException,
71 ClassNotFoundException {
72 Object compObj = compClass.newInstance();
73
74 if (compObj instanceof Compiler) {
75 Compiler compiler = (Compiler) compObj;
76
77 context.addCompiler(compiler);
78
79 return compiler;
80 } else {
81 throw new IllegalArgumentException("Not a Compiler class: "
82 + compClass.getName());
83 }
84 }
85
86 /***
87 * Clones all compilers to a target Context
88 *
89 * @param srcContext the source Context
90 * @param targetContext the target Context
91 *
92 * @throws InstantiationException
93 * @throws IllegalAccessException
94 * @throws ClassNotFoundException if Compiler class initialization fails
95 */
96 public static void cloneCompilers(Context srcContext, Context targetContext)
97 throws InstantiationException, IllegalAccessException,
98 ClassNotFoundException {
99 int compCount = srcContext.getCompilerCount();
100
101 for (int i = 0; i < compCount; i++) {
102 Class compClass = srcContext.getCompiler(i).getClass();
103
104 createCompiler(compClass, targetContext);
105 }
106 }
107 }