KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RegisterProperties


1 import org.mozilla.javascript.Context;
2 import org.mozilla.javascript.Scriptable;
3 import org.mozilla.javascript.ScriptableObject;
4
5 import java.io.BufferedReader JavaDoc;
6 import java.io.FileReader JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 public class RegisterProperties {
12
13     private static String JavaDoc[] BROWSER_GLOBALS = {
14
15         "RegisteringGlobalProperties","console" , "window", "navigator", "document", "userAgent" ,"OpenAjax"
16     };
17
18     public static void main(String JavaDoc args[]) {
19         String JavaDoc libName = args[0];
20         String JavaDoc libUri = args[1];
21         String JavaDoc libVersion = args[2];
22
23         String JavaDoc src = args[3];
24
25         String JavaDoc dependenciesFile = null;
26         if(args.length == 5)
27             dependenciesFile = args[4];
28         if(dependenciesFile != null && dependenciesFile.trim().length()==0)
29                 dependenciesFile = null;
30
31         // Creates and enters a Context. The Context stores information
32
// about the execution environment of a script.
33
Context cx = Context.enter();
34         try {
35             // Initialize the standard objects (Object, Function, etc.)
36
// This must be done before scripts can be executed. Returns
37
// a scope object that we use in later calls.
38
Scriptable scope = cx.initStandardObjects();
39
40             // Collect the arguments into a single string.
41
String JavaDoc s = "";
42             for (int i = 0; i < BROWSER_GLOBALS.length; i++) {
43                 s += "var " + BROWSER_GLOBALS[i] + " = {};";
44             }
45             s += "console.log = function(s){};";
46             s += "navigator.appVersion='rhino';";
47             s += "document.getElementsByTagName = function(){return [];};";
48             s += "document.documentElement = {};";
49             s += "document.cookie = '';";
50             //s += "document.cookie.split = {};";
51
s += "Prototype = []; Prototype.Version='';";
52             cx.evaluateString(scope, s, "<cmd>", 1, null);
53             List JavaDoc dependencies = new ArrayList JavaDoc();
54             if(dependenciesFile != null){
55                 dependencies = getProperties(dependenciesFile, cx, scope);
56             }
57             List JavaDoc properties = getProperties(src, cx, scope);
58             Iterator JavaDoc iter = dependencies.iterator();
59             while(iter.hasNext()){
60                 Object JavaDoc o = iter.next();
61                 if(properties.contains(o))
62                     properties.remove(o);
63             }
64
65             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
66             sb.append("if (typeof OpenAjax!='undefined' && typeof OpenAjax.registerLibrary!='undefined' && typeof OpenAjax.registerGlobals!='undefined'){");
67             sb.append("OpenAjax.registerLibrary('").append(libName).append("','")
68                     .append(libUri).append("','").append(libVersion).append("');\n");
69             sb.append("OpenAjax.registerGlobals('").append(libName).append("', [");
70             boolean appended = false;
71             iter = properties.iterator();
72             while(iter.hasNext()){
73                 String JavaDoc name = iter.next().toString();
74                 if (!isBrowserGlobal(name)) {
75                     if (appended) sb.append(",");
76                     sb.append("'").append(name).append("'");
77                     appended = true;
78                 }
79             }
80             sb.append("]);}\n");
81             System.err.println(sb.toString());
82
83         } finally {
84             // Exit from the context.
85
Context.exit();
86         }
87     }
88
89     private static List JavaDoc getProperties(String JavaDoc fileName, Context context, Scriptable scope) {
90
91         String JavaDoc s = "";
92         try {
93             BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
94             while (in.ready()) {
95                 s += in.readLine();
96                 s += "\n";
97             }
98             in.close();
99         } catch (Exception JavaDoc e) {
100            // e.printStackTrace();
101
}
102
103         // Now evaluate the string we've colected.
104
context.evaluateString(scope, s, "<cmd>", 1, null);
105
106         Object JavaDoc[] o = scope.getIds();
107         List JavaDoc result = new ArrayList JavaDoc();
108         for(int i = 0;i<o.length;i++){
109             result.add(o[i].toString());
110         }
111         return result;
112     }
113
114     private static boolean isBrowserGlobal(String JavaDoc s) {
115         for (int i = 0; i < BROWSER_GLOBALS.length; i++) {
116             if (BROWSER_GLOBALS[i].equals(s)) return true;
117         }
118         return false;
119     }
120 }
121
Popular Tags