2003/10/13 01:00:41
[org.ibex.core.git] / src / org / xwt / Main.java
1 // Copyright 2003 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.bouncycastle.util.encoders.Base64;
9 import org.xwt.js.*;
10 import org.xwt.util.*;
11 import org.xwt.translators.*;
12
13 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
14 public class Main {
15
16     /**
17      *  FEATURE: this should be implemented using self-emulation
18      *  Used for security checks. If this is null, it means that only
19      *  scripts originating from the local filesystem are loaded in
20      *  the engine (maximum permissions). If scripts have been loaded
21      *  from multiple locations, this will be 0.0.0.0 (the invalid
22      *  IP).
23      */
24     public static java.net.InetAddress originAddr = null;
25     public static String originHost = null;
26     public static String origin = null;
27
28     public static Res builtin = null;
29
30     public static void printUsage() {
31         System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
32         System.err.println("");
33         System.err.println("Options:");
34         // FEATURE: reintroduce
35         //System.err.println("    -s show rendering activity with red rectangles");
36         System.err.println("    -v verbose logging");
37         // FEATURE: reintroduce
38         //System.err.println("    -l serve logs via HTTP on 127.0.0.1:<port>/<url>");
39         System.err.println("");
40         System.err.println("Source-location is one of the following:");
41         System.err.println("    - the path to an xwar file");
42         System.err.println("    - the path to a directory to be used as an xwar");
43         System.err.println("    - the http url of an xwar");
44         System.err.println("");
45         System.err.println("Initial-template is the resource name of the template to load; defaults to 'main'");
46         Runtime.getRuntime().exit(-1);
47     }
48
49     /** common entry point */
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 if (args[startargs].equals("-l")) startargs++;
56             else break;
57             startargs++;
58         }
59
60         Platform.forceLoad();
61         if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
62
63         builtin = new Res.Zip(new Res.IS(Platform.getBuiltinInputStream()));
64
65         String initialTemplateName = args.length > startargs + 1 ? args[startargs + 1] : "main";
66         initialTemplateName = initialTemplateName.replace('/', '.');
67         origin = args[startargs];
68
69         if (origin.startsWith("http://") || origin.startsWith("https://")) {
70             originHost = origin.substring(origin.indexOf('/') + 2);
71             originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
72             if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
73             originAddr = InetAddress.getByName(originHost);
74         } else {
75             // HACK because MSIE turns \'s into /'s in file URLs... argh!!
76             if (Platform.platform.getClass().getName().endsWith("Win32")) origin = origin.replace('/', '\\');
77             if (!new File(origin).isDirectory()) origin += "!";
78             origin = "file:" + origin;
79         }
80
81         // FIXME put the splash screen back in
82         if (Log.on) Log.log(Main.class, "loading xwar");
83
84         final Res rr = Res.stringToRes(origin, true);
85         final XWT xwt = new XWT(rr);
86         final String initialTemplateName_ = initialTemplateName;
87
88         new Thread(new Runnable() { 
89             public void run() {
90                 Template.getTemplate(((Res)rr.get(initialTemplateName_)).addExtension(".xwt")).apply(new Box(), null, xwt);
91                 Message.Q.startQ();
92             }
93         }).start();
94         
95         Platform.running();
96     }
97
98 }