1 16 package org.apache.cocoon.woody.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 82 public static Function buildFunction(Element element, String [] argumentNames) throws IOException { 83 StringBuffer buffer = new StringBuffer ("function foo("); 85 for (int i = 0; i < argumentNames.length; i++) { 86 if (i > 0) { 87 buffer.append(','); 88 } 89 buffer.append(argumentNames[i]); 90 } 91 buffer.append(") {\n").append(DomHelper.getElementText(element)).append("\n}"); 92 93 String jsText = buffer.toString(); 94 String sourceName = DomHelper.getSystemIdLocation(element); 95 96 Context ctx = Context.enter(); 97 Function func; 98 try { 99 func = ctx.compileFunction( 100 getRootScope(), jsText, sourceName == null ? "<unknown>" : sourceName, DomHelper.getLineLocation(element) - 1, null ); 106 } finally { 107 Context.exit(); 108 } 109 return func; 110 } 111 112 117 public static Scriptable getRootScope() { 118 if (_rootScope == null) { 119 Context ctx = Context.enter(); 121 try { 122 _rootScope = ctx.initStandardObjects(null); 123 } finally { 124 Context.exit(); 125 } 126 } 127 return _rootScope; 128 } 129 130 138 public static Scriptable getParentScope(Map objectModel) { 139 Scriptable parentScope = null; 141 if (objectModel != null) { 142 parentScope = FOM_JavaScriptFlowHelper.getFOM_FlowScope(objectModel); 143 } 144 145 if (parentScope != null) { 146 return parentScope; 147 } else { 148 return getRootScope(); 149 } 150 } 151 152 public static Object execScript(Script script, Map values, Map objectModel) throws JavaScriptException { 153 Context ctx = Context.enter(); 154 try { 155 Scriptable parentScope = getParentScope(objectModel); 156 157 Scriptable scope; 159 try { 160 scope = ctx.newObject(parentScope); 161 } catch (Exception e) { 162 throw new CascadingRuntimeException("Cannont create script scope", e); 164 } 165 scope.setParentScope(parentScope); 166 167 Iterator iter = values.entrySet().iterator(); 169 while(iter.hasNext()) { 170 Map.Entry entry = (Map.Entry )iter.next(); 171 String key = (String )entry.getKey(); 172 Object value = entry.getValue(); 173 scope.put(key, scope, Context.toObject(value, scope)); 174 } 175 176 if (objectModel != null) { 177 Object viewData = FlowHelper.getContextObject(objectModel); 178 if (viewData != null) { 179 scope.put("viewData", scope, Context.toObject(viewData, scope)); 180 } 181 } 182 183 Object result = script.exec(ctx, scope); 184 return FlowHelper.unwrap(result); 185 } finally { 186 Context.exit(); 187 } 188 } 189 190 public static Object callFunction(Function func, Object thisObject, Object [] arguments, Map objectModel) throws JavaScriptException { 191 Context ctx = Context.enter(); 192 try { 193 Scriptable scope = getParentScope(objectModel); 194 195 if (objectModel != null) { 196 Object viewData = FlowHelper.getContextObject(objectModel); 197 if (viewData != null) { 198 Scriptable newScope; 200 try { 201 newScope = ctx.newObject(scope); 202 } catch (Exception e) { 203 throw new CascadingRuntimeException("Cannont create function scope", e); 205 } 206 newScope.setParentScope(scope); 207 scope = newScope; 208 209 scope.put("viewData", scope, Context.toObject(viewData, scope)); 210 } 211 } 212 Object result = func.call(ctx, scope, thisObject == null? null: Context.toObject(thisObject, scope), arguments); 213 return FlowHelper.unwrap(result); 214 } finally { 215 Context.exit(); 216 } 217 } 218 } 219 | Popular Tags |