KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > standard > load


1 package kawa.standard;
2 import java.io.*;
3 import gnu.bytecode.ZipLoader;
4 import gnu.mapping.*;
5 import gnu.expr.*;
6 import gnu.text.SourceMessages;
7 import gnu.text.SyntaxException;
8 import gnu.lists.*;
9 import gnu.text.URI_utils;
10 import java.net.URL JavaDoc;
11
12 public class load extends Procedure1 {
13   boolean relative;
14
15   public load (String JavaDoc name, boolean relative)
16   {
17     super(name);
18     this.relative = relative;
19   }
20
21   public static final load load = new load("load", false);
22   public static final load loadRelative = new load("load-relative", true);
23
24   /** Load using the name of a compile .class file. */
25   /* This should probably be re-written to use a ClassLoader, unless '.'
26    * is in the CLASSPATH, since it a bit ugly that load of a source file
27    * or .zip file reads a file (using a relative or absolute file name),
28    * while load of a compiled .class uses the classpath. */

29   public final static void loadClassFile (String JavaDoc name, Environment env)
30   {
31     Environment orig_env = Environment.getCurrent();
32     try
33       {
34     if (env != orig_env)
35       Environment.setCurrent(env);
36     Class JavaDoc clas = Class.forName (name);
37     Object JavaDoc inst = clas.newInstance ();
38     gnu.kawa.reflect.ClassMemberLocation.defineAll(inst, env);
39     if (inst instanceof Runnable JavaDoc)
40       ((Runnable JavaDoc)inst).run();
41       }
42     catch (Throwable JavaDoc ex)
43       {
44         throw new WrappedException("exception during load of \""+name+'\"',
45                                    ex);
46       }
47     finally
48       {
49     if (env != orig_env)
50       Environment.setCurrent(orig_env);
51       }
52   }
53
54   public final static void loadCompiled (String JavaDoc name, Environment env)
55     throws Throwable JavaDoc
56   {
57     Environment orig_env = Environment.getCurrent();
58     try
59       {
60     if (env != orig_env)
61       Environment.setCurrent(env);
62     File zfile = new File (name);
63     if (!zfile.exists ())
64       throw new RuntimeException JavaDoc ("load: "+name+" - not found");
65     if (!zfile.canRead ())
66       throw new RuntimeException JavaDoc ("load: "+name+" - not readable");
67     ZipLoader loader = new ZipLoader (name);
68         Class JavaDoc clas = loader.loadAllClasses();
69         Object JavaDoc proc = clas == null ? null : clas.newInstance();
70         if (! (proc instanceof ModuleBody))
71           throw new RuntimeException JavaDoc("load: "+name+" - no module in archive");
72     gnu.kawa.reflect.ClassMemberLocation.defineAll(proc, env);
73     ((ModuleBody) proc).run();
74       }
75     catch (java.io.IOException JavaDoc ex)
76       {
77     throw new WrappedException ("load: "+name+" - "+ex.toString (), ex);
78       }
79     catch (InstantiationException JavaDoc ex)
80       {
81     throw new WrappedException ("class not instantiable: in load", ex);
82       }
83     catch (IllegalAccessException JavaDoc ex)
84       {
85     throw new WrappedException ("class illegal access: in load", ex);
86       }
87     finally
88       {
89     if (env != orig_env)
90       Environment.setCurrent(orig_env);
91       }
92   }
93
94   public final static void loadSource (InPort port, Environment env, URL JavaDoc url)
95     throws SyntaxException, Throwable JavaDoc
96   {
97     boolean print = ModuleBody.getMainPrintValues();
98     Language language = Language.getDefaultLanguage();
99     Consumer out = (print ? kawa.Shell.getOutputConsumer(OutPort.outDefault())
100             : new VoidConsumer());
101     // Reading the entire file and evaluting it as a unit is more
102
// consistent with compiled code, and more efficient.
103
// Unfortunately, it is difficult to get macros to work properly.
104
// So instead, we read and evaluate each line individually.
105
if (true)
106       {
107     kawa.Shell.run(language, env, port, out, OutPort.errDefault(), url);
108       }
109     else
110       {
111     SourceMessages messages = new SourceMessages();
112     Compilation comp
113           = language.parse(port, messages, Language.PARSE_IMMEDIATE);
114     ModuleExp mexp = comp.getModule();
115     CallContext ctx = CallContext.getInstance();
116     Consumer save = ctx.consumer;
117     try
118       {
119         ctx.consumer = out;
120         ctx.values = Values.noArgs;
121         ModuleExp.evalModule(env, ctx, comp, url, null);
122         if (messages.seenErrors())
123           throw new SyntaxException(messages);
124       }
125     finally
126       {
127         ctx.consumer = save;
128       }
129       }
130   }
131
132   public final Object JavaDoc apply1 (Object JavaDoc arg1)
133     throws Throwable JavaDoc
134   {
135     return apply2 (arg1, Environment.getCurrent ());
136   }
137
138   public final Object JavaDoc apply2 (Object JavaDoc arg1, Object JavaDoc arg2)
139     throws Throwable JavaDoc
140   {
141     String JavaDoc name = arg1.toString();
142     try
143       {
144     Environment env = (Environment) arg2;
145     apply (name, env, relative, 0);
146     return Values.empty;
147       }
148     catch (java.io.FileNotFoundException JavaDoc e)
149       {
150     throw new RuntimeException JavaDoc ("load: file not readable: " + name);
151       }
152     catch (SyntaxException ex)
153       {
154     throw new RuntimeException JavaDoc("load: errors while compiling '"+
155                    name+"':\n"+ex.getMessages().toString(20));
156       }
157   }
158
159   public static final void apply (String JavaDoc name, Environment env,
160                   boolean relative, int skipLines)
161     throws Throwable JavaDoc
162   {
163     CallContext ctx = CallContext.getInstance();
164     boolean isUri = InPort.uriSchemeSpecified(name);
165     // Resolve a relative URI. However, if the base uri matches the
166
// default base uri (i.e. the current directory) just leave it as
167
// a filename, rather than coercing it to a URI.
168
String JavaDoc resolved = name;
169     if (! isUri)
170       {
171         String JavaDoc baseUri = ctx.getBaseUri();
172         Object JavaDoc base = baseUri;
173         if (! URI_utils.isAbsolute(base))
174           base = URI_utils.resolve(base, ctx.getBaseUriDefault());
175         resolved = URI_utils.resolve(name, base).toString();
176         if (relative && ! baseUri.equals(ctx.getBaseUriDefault()))
177           name = resolved;
178       }
179     if (name.endsWith (".zip") || name.endsWith(".jar"))
180       {
181         loadCompiled (name, env);
182         return;
183       }
184     URL JavaDoc url = new URL JavaDoc(resolved);
185     char file_separator = System.getProperty ("file.separator").charAt(0);
186
187     if (name.endsWith (".class"))
188       {
189         name = name.substring (0, name.length () - 6);
190         name = name.replace ('/', '.');
191         if (file_separator != '/')
192           name = name.replace (file_separator, '.');
193         loadClassFile (name, env);
194         return;
195       }
196     InputStream fs = new BufferedInputStream(URI_utils.getInputStream(url));
197     fs.mark(5);
198     int char0 = fs.read ();
199     if (char0 == -1)
200       return; // Sequence.eofValue;
201
if (char0 == 'P')
202       {
203         int char1 = fs.read ();
204         if (char1 == 'K')
205           {
206             int char2 = fs.read ();
207             if (char2 == '\003')
208               {
209                 int char3 = fs.read ();
210                 if (char3 == '\004')
211                   {
212                     fs.close ();
213                     loadCompiled (name, env);
214                     return;
215                   }
216               }
217           }
218       }
219     fs.reset();
220     InPort src = InPort.openFile(fs, name);
221     while (--skipLines >= 0)
222       src.skipRestOfLine();
223     loadSource (src, env, url);
224     src.close();
225   }
226 }
227
Popular Tags