2003/09/23 21:16:10
[org.ibex.core.git] / src / org / xwt / Main.java
index 590d126..9febf7c 100644 (file)
@@ -14,6 +14,7 @@ import org.xwt.translators.*;
 public class Main {
 
     /**
+     *  FEATURE: this should be implemented using self-emulation
      *  Used for security checks. If this is null, it means that only
      *  scripts originating from the local filesystem are loaded in
      *  the engine (maximum permissions). If scripts have been loaded
@@ -22,120 +23,68 @@ public class Main {
      */
     public static java.net.InetAddress originAddr = null;
     public static String originHost = null;
-
-    /** the URL where the initial xwar came from. */
     public static String origin = null;
 
-    /** true iff the user asked for rendered regions to be shown with a red rectangle */
-    public static boolean showRenders = false;
-
-    /** don't check if all surfaces are gone (and quit) until this is true */
-    public static boolean doneInitializing = false;
+    public static Res builtin = null;
+
+    public static void printUsage() {
+        System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
+        System.err.println("");
+        System.err.println("Options:");
+        // FEATURE: reintroduce
+        //System.err.println("    -s show rendering activity with red rectangles");
+        System.err.println("    -v verbose logging");
+        // FEATURE: reintroduce
+        //System.err.println("    -l serve logs via HTTP on 127.0.0.1:<port>/<url>");
+        System.err.println("");
+        System.err.println("Source-location is one of the following:");
+        System.err.println("    - the path to an xwar file");
+        System.err.println("    - the path to a directory to be used as an xwar");
+        System.err.println("    - the http url of an xwar");
+        System.err.println("");
+        System.err.println("Initial-template is the resource name of the template to load; defaults to 'main'");
+        Runtime.getRuntime().exit(-1);
+    }
 
     /** common entry point */
-    public static void main(String[] args) {
-        try {
-            int startargs = 0;
-            while (true) {
-                if (startargs > args.length - 1) {
-                    System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
-                    System.err.println("");
-                    System.err.println("Options:");
-                    System.err.println("    -s show rendering activity with red rectangles");
-                    System.err.println("    -v verbose logging");
-                    System.err.println("    -l serve logs via HTTP on 127.0.0.1:<port>/<url>");
-                    System.err.println("");
-                    System.err.println("Source-location is one of the following:");
-                    System.err.println("    - the path to an xwar file");
-                    System.err.println("    - the path to a directory to be used as an xwar");
-                    System.err.println("    - the http url of an xwar");
-                    System.err.println("");
-                    System.err.println("Initial-template is the resource name of the template to load; defaults to 'main'");
-                    Runtime.getRuntime().exit(-1);
-                }
-                else if (args[startargs].equals("-s")) showRenders = true;
-                else if (args[startargs].equals("-v")) Log.verbose = true;
-                else if (args[startargs].equals("-l")) startargs++;
-                else break;
-                startargs++;
-            }
-            /* FIXME
-            final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
-
-            Platform.forceLoad();
-            if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
-
-            InputStream is = Platform.getBuiltinInputStream();
-            if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
-           
-            if (Log.on) Log.log(Main.class, "loading scar image");
-            PNG png = PNG.decode(new ByteArrayInputStream(Resources.getResource("org.xwt.builtin.scar.png")), "bundled scar image");
-            Surface.scarPicture = Platform.createPicture(png.getData(), png.getWidth(), png.getHeight());
-
-            String initialTemplate = "main";
-            Res initialRR = null;
-
-            // FIXME: after applying initial template, check numsurfaces; if zero, exit
-            if (args.length > startargs) {
-                if (args[startargs].startsWith("http://")) {
-                    if (Log.on) Log.log(Main.class, "downloading xwar");
-                    initialRR = Res.stringToRes("file:" + args[startargs] + "!");
-                    origin = args[startargs];
-                    initialTemplate = "org.xwt.builtin.splash";
+    public static void main(String[] args) throws Exception {
+        int startargs = 0;
+        while (true) {
+            if (startargs > args.length - 1) printUsage();
+            else if (args[startargs].equals("-v")) Log.verbose = true;
+            else if (args[startargs].equals("-l")) startargs++;
+            else break;
+            startargs++;
+        }
 
-                } else {
-                    if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
+        Platform.forceLoad();
+        if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
+
+        builtin = new Res.Zip(new Res.IS(Platform.getBuiltinInputStream()));
+
+        String initialTemplateName = args.length > startargs + 1 ? args[startargs + 1] : "main";
+        initialTemplateName = initialTemplateName.replace('/', '.');
+        origin = args[startargs];
+
+        if (origin.startsWith("http://") || origin.startsWith("https://")) {
+            originHost = origin.substring(origin.indexOf('/') + 2);
+            originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
+            if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
+            originAddr = InetAddress.getByName(originHost);
+        } else {
+            // HACK because MSIE turns \'s into /'s in file URLs... argh!!
+            if (Platform.platform.getClass().getName().endsWith("Win32")) origin = origin.replace('/', '\\');
+            if (!new File(origin).isDirectory()) origin += "!";
+            origin = "file:" + origin;
+        }
 
-                    // HACK because MSIE turns \'s into /'s in URLs... argh!!
-                    if (Platform.platform.getClass().getName().endsWith("Win32"))
-                        args[startargs] = args[startargs].replace('/', '\\');
+        // FIXME put the splash screen back in
+        if (Log.on) Log.log(Main.class, "loading xwar");
 
-                    File f = new File(args[startargs]);
-                    origin = "file:" + f.getAbsolutePath();
-                    if (f.isDirectory()) {
-                        initialRR = Res.stringToRes("file:" + args[startargs]);
-                        Surface.scarAllSurfacesFromNowOn = true;
-                    } else {
-                        initialRR = Res.stringToRes("file:" + args[startargs] + "!");
-                        initialTemplate = "org.xwt.builtin.splash";
-                    }
+        Res rr = Res.stringToRes(origin);
+        Template.getTemplate(((Res)rr.get(initialTemplateName)).addExtension(".xwt")).apply(new Box(), null, 0, 0, rr);
 
-                    if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
-                }
-            }
-            
-            if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
-            final String initialTemplate_f = initialTemplate;
-            final Res initialRR_f = initialRR;
-            ThreadMessage.newthread(new JS.Callable() {
-                    public Object call(JS.Array args) throws JS.Exn {
-                        Box b = new Box();
-                        try {
-                            Template t = Template.buildTemplate(Res.stringToRes("file:.").getInputStream(), "RESOURCE");
-                            t.apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f);
-                            //FIXME
-                            //Template.getTemplate(initialTemplate_f, null).apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f);
-                            doneInitializing = true;
-                            if (Surface.allSurfaces.size() == 0) {
-                                Log.log(this, "exiting because all surfaces are gone");
-                                Platform.exit();
-                            }
-                        } catch (Exception e) {
-                            Log.log(Main.class, e);
-                        }
-                        return null;
-                    }
-                });
-           
-            // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
-            //if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
-            //new org.xwt.util.Semaphore().block();
-            Platform.running();
-            */
-        } catch (Throwable e) {
-            e.printStackTrace();
-            Platform.criticalAbort("exception in Main.main(): " + e);
-        }
+        Message.Q.startQ();
     }
 
 }