propose-patch
[org.ibex.core.git] / src / org / xwt / Main.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.net.*;
5 import java.util.*;
6 import java.io.*;
7 import java.awt.*;
8 import org.xwt.js.*;
9 import org.xwt.util.*;
10 import org.xwt.translators.*;
11 import org.xwt.plat.*;
12 import org.bouncycastle.util.encoders.Base64;
13
14 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
15 public class Main {
16
17     /**
18      *  FEATURE: this should be implemented using self-emulation
19      *  Used for security checks. If this is null, it means that only
20      *  scripts originating from the local filesystem are loaded in
21      *  the engine (maximum permissions). If scripts have been loaded
22      *  from multiple locations, this will be 0.0.0.0 (the invalid
23      *  IP).
24      */
25     public static java.net.InetAddress originAddr = null;
26     public static String originHost = null;
27     public static String origin = null;
28     public static String initialTemplate = null;
29     
30     public static final Stream builtin = new Stream.Zip(new Stream.Builtin());
31     public static Picture scarImage = null;
32
33     public static void printUsage() {
34         System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
35         System.err.println("");
36         System.err.println("Options:");
37         System.err.println("    -v verbose logging (required for logging on Win32)");
38         System.err.println("    -s [not yet supported]");
39         System.err.println("    -l [not yet supported]");
40         System.err.println("");
41         System.err.println("Source-location is one of the following:");
42         System.err.println("    - the path to an xwar file");
43         System.err.println("    - the path to a directory to be used as an xwar");
44         System.err.println("    - the http url of an xwar");
45         System.err.println("");
46         System.err.println("Initial-template is the path of the template to load; defaults to 'main'");
47         Runtime.getRuntime().exit(-1);
48     }
49
50     public static void main(String[] args) throws Exception {
51         int startargs = 0;
52         while (true) {
53             if (startargs > args.length - 1) printUsage();
54             else if (args[startargs].equals("-v")) Log.verbose = true;
55             else break;
56             startargs++;
57         }
58
59         Platform.forceLoad();
60         if (Log.on) for(int i=0; i<args.length; i++) Log.info(Main.class, "argument " + i + ": " + args[i]);
61
62         initialTemplate = args.length > startargs + 1 ? args[startargs + 1] : "main";
63         origin = args[startargs];
64
65         Stream rr;
66         final String startupTemplate;
67         if (origin.startsWith("http://") || origin.startsWith("https://")) {
68             originHost = origin.substring(origin.indexOf('/') + 2);
69             originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
70             if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
71             originAddr = InetAddress.getByName(originHost);
72             rr = builtin;
73             startupTemplate = "org.xwt.builtin.splash";
74         } else {
75             rr = new Stream.File(origin);
76             if (!new File(origin).isDirectory()) rr = new Stream.Zip(rr);
77             startupTemplate = initialTemplate;
78         }
79
80         if (Log.on) Log.info(Main.class, "loading xwar");
81         final XWT xwt = new XWT(rr);
82         final JS final_rr = (JS)xwt.get("");
83
84         scarImage =
85             Picture.load((Stream)Main.builtin.get("org/xwt/builtin/scar.png"),
86                          new Scheduler.Task() { public void perform() throws Exception {
87                              xwt.resolveString(startupTemplate, false).call(new Box(), null, null, null, 1);
88                          } });
89
90         Scheduler.init();
91     }
92 }