1 16 package org.apache.cocoon.forms.util; 17 18 import java.io.IOException ; 19 import java.io.StringReader ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.CascadingRuntimeException; 24 import org.apache.cocoon.components.flow.FlowHelper; 25 import org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptFlowHelper; 26 import org.mozilla.javascript.Context; 27 import org.mozilla.javascript.Function; 28 import org.mozilla.javascript.JavaScriptException; 29 import org.mozilla.javascript.Script; 30 import org.mozilla.javascript.Scriptable; 31 import org.w3c.dom.Element ; 32 33 40 public class JavaScriptHelper { 41 42 45 private static Scriptable _rootScope = null; 46 47 54 public static Script buildScript(Element element) throws IOException { 55 String jsText = DomHelper.getElementText(element); 56 String sourceName = DomHelper.getSystemIdLocation(element); 57 58 Context ctx = Context.enter(); 59 Script script; 60 try { 61 script = ctx.compileReader( 62 getRootScope(), new StringReader (jsText), sourceName == null ? "<unknown>" : sourceName, DomHelper.getLineLocation(element), null ); 68 } finally { 69 Context.exit(); 70 } 71 return script; 72 } 73 74 83 public static Function buildFunction(Element element, String name, String [] argumentNames) throws IOException { 84 StringBuffer buffer = new StringBuffer ("function ").append(name).append("("); 86 for (int i = 0; i < argumentNames.length; i++) { 87 if (i > 0) { 88 buffer.append(','); 89 } 90 buffer.append(argumentNames[i]); 91 } 92 buffer.append(") {\n").append(DomHelper.getElementText(element)).append("\n}"); 93 94 String jsText = buffer.toString(); 95 String sourceName = DomHelper.getSystemIdLocation(element); 96 97 Context ctx = Context.enter(); 98 Function func; 99 try { 100 func = ctx.compileFunction( 101 getRootScope(), jsText, sourceName == null ? "<unknown>" : sourceName, DomHelper.getLineLocation(element) - 1, null ); 107 } finally { 108 Context.exit(); 109 } 110 return func; 111 } 112 113 118 public static Scriptable getRootScope() { 119 122 123 if (_rootScope == null) { 124 Context ctx = Context.enter(); 126 try { 127 _rootScope = ctx.initStandardObjects(null); 128 } finally { 129 Context.exit(); 130 } 131 } 132 return _rootScope; 133 } 134 135 143 public static Scriptable getParentScope(Map objectModel) { 144 Scriptable parentScope = null; 146 if (objectModel != null) { 147 parentScope = FOM_JavaScriptFlowHelper.getFOM_FlowScope(objectModel); 148 } 149 150 if (parentScope != null) { 151 return parentScope; 152 } else { 153 return getRootScope(); 154 } 155 } 156 157 public static Object execScript(Script script, Map values, Map objectModel) throws JavaScriptException { 158 Context ctx = Context.enter(); 159 try { 160 Scriptable parentScope = getParentScope(objectModel); 161 162 Scriptable scope; 164 try { 165 scope = ctx.newObject(parentScope); 166 } catch (Exception e) { 167 throw new CascadingRuntimeException("Cannont create script scope", e); 169 } 170 scope.setParentScope(parentScope); 171 172 Iterator iter = values.entrySet().iterator(); 174 while(iter.hasNext()) { 175 Map.Entry entry = (Map.Entry )iter.next(); 176 String key = (String )entry.getKey(); 177 Object value = entry.getValue(); 178 scope.put(key, scope, Context.toObject(value, scope)); 179 } 180 181 if (objectModel != null) { 182 Object viewData = FlowHelper.getContextObject(objectModel); 183 if (viewData != null) { 184 scope.put("viewData", scope, Context.toObject(viewData, scope)); 185 } 186 } 187 188 Object result = script.exec(ctx, scope); 189 return FlowHelper.unwrap(result); 190 } finally { 191 Context.exit(); 192 } 193 } 194 195 public static Object callFunction(Function func, Object thisObject, Object [] arguments, Map objectModel) throws JavaScriptException { 196 Context ctx = Context.enter(); 197 try { 198 Scriptable scope = getParentScope(objectModel); 199 200 if (objectModel != null) { 201 Object viewData = FlowHelper.getContextObject(objectModel); 202 if (viewData != null) { 203 Scriptable newScope; 205 try { 206 newScope = ctx.newObject(scope); 207 } catch (Exception e) { 208 throw new CascadingRuntimeException("Cannont create function scope", e); 210 } 211 newScope.setParentScope(scope); 212 scope = newScope; 213 214 scope.put("viewData", scope, Context.toObject(viewData, scope)); 215 } 216 } 217 Object result = func.call(ctx, scope, thisObject == null? null: Context.toObject(thisObject, scope), arguments); 218 return FlowHelper.unwrap(result); 219 } finally { 220 Context.exit(); 221 } 222 } 223 } 224 | Popular Tags |