1 import org.mozilla.javascript.Context; 2 import org.mozilla.javascript.Scriptable; 3 import org.mozilla.javascript.ScriptableObject; 4 5 import java.io.BufferedReader ; 6 import java.io.FileReader ; 7 import java.util.List ; 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 11 public class RegisterProperties { 12 13 private static String [] BROWSER_GLOBALS = { 14 15 "RegisteringGlobalProperties","console" , "window", "navigator", "document", "userAgent" ,"OpenAjax" 16 }; 17 18 public static void main(String args[]) { 19 String libName = args[0]; 20 String libUri = args[1]; 21 String libVersion = args[2]; 22 23 String src = args[3]; 24 25 String dependenciesFile = null; 26 if(args.length == 5) 27 dependenciesFile = args[4]; 28 if(dependenciesFile != null && dependenciesFile.trim().length()==0) 29 dependenciesFile = null; 30 31 Context cx = Context.enter(); 34 try { 35 Scriptable scope = cx.initStandardObjects(); 39 40 String 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 += "Prototype = []; Prototype.Version='';"; 52 cx.evaluateString(scope, s, "<cmd>", 1, null); 53 List dependencies = new ArrayList (); 54 if(dependenciesFile != null){ 55 dependencies = getProperties(dependenciesFile, cx, scope); 56 } 57 List properties = getProperties(src, cx, scope); 58 Iterator iter = dependencies.iterator(); 59 while(iter.hasNext()){ 60 Object o = iter.next(); 61 if(properties.contains(o)) 62 properties.remove(o); 63 } 64 65 StringBuffer sb = new StringBuffer (); 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 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 Context.exit(); 86 } 87 } 88 89 private static List getProperties(String fileName, Context context, Scriptable scope) { 90 91 String s = ""; 92 try { 93 BufferedReader in = new BufferedReader (new FileReader (fileName)); 94 while (in.ready()) { 95 s += in.readLine(); 96 s += "\n"; 97 } 98 in.close(); 99 } catch (Exception e) { 100 } 102 103 context.evaluateString(scope, s, "<cmd>", 1, null); 105 106 Object [] o = scope.getIds(); 107 List result = new ArrayList (); 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 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 |