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 ; 11 12 public class load extends Procedure1 { 13 boolean relative; 14 15 public load (String 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 25 29 public final static void loadClassFile (String name, Environment env) 30 { 31 Environment orig_env = Environment.getCurrent(); 32 try 33 { 34 if (env != orig_env) 35 Environment.setCurrent(env); 36 Class clas = Class.forName (name); 37 Object inst = clas.newInstance (); 38 gnu.kawa.reflect.ClassMemberLocation.defineAll(inst, env); 39 if (inst instanceof Runnable ) 40 ((Runnable )inst).run(); 41 } 42 catch (Throwable 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 name, Environment env) 55 throws Throwable 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 ("load: "+name+" - not found"); 65 if (!zfile.canRead ()) 66 throw new RuntimeException ("load: "+name+" - not readable"); 67 ZipLoader loader = new ZipLoader (name); 68 Class clas = loader.loadAllClasses(); 69 Object proc = clas == null ? null : clas.newInstance(); 70 if (! (proc instanceof ModuleBody)) 71 throw new RuntimeException ("load: "+name+" - no module in archive"); 72 gnu.kawa.reflect.ClassMemberLocation.defineAll(proc, env); 73 ((ModuleBody) proc).run(); 74 } 75 catch (java.io.IOException ex) 76 { 77 throw new WrappedException ("load: "+name+" - "+ex.toString (), ex); 78 } 79 catch (InstantiationException ex) 80 { 81 throw new WrappedException ("class not instantiable: in load", ex); 82 } 83 catch (IllegalAccessException 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 url) 95 throws SyntaxException, Throwable 96 { 97 boolean print = ModuleBody.getMainPrintValues(); 98 Language language = Language.getDefaultLanguage(); 99 Consumer out = (print ? kawa.Shell.getOutputConsumer(OutPort.outDefault()) 100 : new VoidConsumer()); 101 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 apply1 (Object arg1) 133 throws Throwable 134 { 135 return apply2 (arg1, Environment.getCurrent ()); 136 } 137 138 public final Object apply2 (Object arg1, Object arg2) 139 throws Throwable 140 { 141 String 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 e) 149 { 150 throw new RuntimeException ("load: file not readable: " + name); 151 } 152 catch (SyntaxException ex) 153 { 154 throw new RuntimeException ("load: errors while compiling '"+ 155 name+"':\n"+ex.getMessages().toString(20)); 156 } 157 } 158 159 public static final void apply (String name, Environment env, 160 boolean relative, int skipLines) 161 throws Throwable 162 { 163 CallContext ctx = CallContext.getInstance(); 164 boolean isUri = InPort.uriSchemeSpecified(name); 165 String resolved = name; 169 if (! isUri) 170 { 171 String baseUri = ctx.getBaseUri(); 172 Object 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 url = new URL (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; 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 |