KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > rhino > RhinoFunctionModel


1 package freemarker.ext.rhino;
2
3 import java.util.List JavaDoc;
4
5 import org.mozilla.javascript.Context;
6 import org.mozilla.javascript.Function;
7 import org.mozilla.javascript.Scriptable;
8 import org.mozilla.javascript.ScriptableObject;
9
10 import freemarker.ext.beans.BeansWrapper;
11 import freemarker.template.TemplateMethodModelEx;
12 import freemarker.template.TemplateModel;
13 import freemarker.template.TemplateModelException;
14
15 /**
16  * <p><font color="red">Experimental: no backward compatibility guarantees</font>;
17  * any feedback is highly welcome!</p>
18  *
19  * @author Attila Szegedi
20  * @version $Id: RhinoFunctionModel.java,v 1.2 2005/06/22 10:52:52 ddekany Exp $
21  */

22 public class RhinoFunctionModel extends RhinoScriptableModel
23 implements TemplateMethodModelEx {
24
25     private final Scriptable fnThis;
26     
27     public RhinoFunctionModel(Function function, Scriptable fnThis,
28             BeansWrapper wrapper) {
29         super(function, wrapper);
30         this.fnThis = fnThis;
31     }
32     
33     public Object JavaDoc exec(List JavaDoc arguments) throws TemplateModelException {
34         Context cx = Context.getCurrentContext();
35         Object JavaDoc[] args = arguments.toArray();
36         BeansWrapper wrapper = getWrapper();
37         for (int i = 0; i < args.length; i++) {
38             args[i] = wrapper.unwrap((TemplateModel)args[i], Scriptable.class);
39         }
40         return wrapper.wrap(((Function)getScriptable()).call(cx,
41                 ScriptableObject.getTopLevelScope(fnThis), fnThis, args));
42     }
43 }
44
Popular Tags