1 34 35 36 package com.micronova.util.codec; 37 38 import java.util.*; 39 import com.micronova.util.*; 40 import org.mozilla.javascript.*; 41 42 public class CodecJavascript extends Codec 43 { 44 45 46 public static final Object eval(Object object, Object environment) throws Exception 47 { 48 if (object != null) 49 { 50 NestedMap envMap = TypeUtil.isNestedMap(environment); 51 Context cx = Context.enter(); 52 53 try 54 { 55 Scriptable scope = cx.initStandardObjects(); 56 57 if (envMap != null) 58 { 59 Iterator iterator = envMap.entrySet().iterator(); 60 61 while (iterator.hasNext()) 62 { 63 Map.Entry entry = (Map.Entry)iterator.next(); 64 ScriptableObject.putProperty(scope, entry.getKey().toString(), Context.javaToJS(entry.getValue(), scope)); 65 } 66 } 67 68 if (object instanceof Script) 69 { 70 object = ((Script)object).exec(cx, scope); 71 } 72 else 73 { 74 object = cx.evaluateString(scope, object.toString(), "<jsp>", 1, null); 75 } 76 77 object = cx.jsToJava(object, java.lang.Object .class); 78 } 79 catch (Exception e) 80 { 81 throw e; 82 } 83 finally 84 { 85 Context.exit(); 86 } 87 } 88 89 return object; 90 } 91 92 public static final Object eval(Object object) throws Exception 93 { 94 return eval(object, null); 95 } 96 97 98 99 public static final Object compile(Object object) throws Exception 100 { 101 if (object != null) 102 { 103 Context cx = Context.enter(); 104 try 105 { 106 Scriptable scope = cx.initStandardObjects(); 107 108 object = cx.compileString(object.toString(), "<jsp>", 1, null); 109 } 110 catch (Exception e) 111 { 112 throw e; 113 } 114 finally 115 { 116 cx.exit(); 117 } 118 } 119 return object; 120 } 121 } 122 | Popular Tags |