1 package freemarker.ext.rhino; 2 3 import org.mozilla.javascript.EvaluatorException; 4 import org.mozilla.javascript.Function; 5 import org.mozilla.javascript.NativeJavaObject; 6 import org.mozilla.javascript.Scriptable; 7 import org.mozilla.javascript.ScriptableObject; 8 9 import freemarker.ext.beans.BeansWrapper; 10 import freemarker.ext.util.ModelFactory; 11 import freemarker.template.AdapterTemplateModel; 12 import freemarker.template.ObjectWrapper; 13 import freemarker.template.TemplateCollectionModel; 14 import freemarker.template.TemplateHashModelEx; 15 import freemarker.template.TemplateModel; 16 import freemarker.template.TemplateModelException; 17 import freemarker.template.TemplateSequenceModel; 18 19 26 public class RhinoScriptableModel implements TemplateHashModelEx, 27 TemplateSequenceModel, AdapterTemplateModel 28 { 29 static final ModelFactory FACTORY = new ModelFactory() { 30 public TemplateModel create(Object object, ObjectWrapper wrapper) { 31 return new RhinoScriptableModel((Scriptable)object, 32 (BeansWrapper)wrapper); 33 } 34 }; 35 36 private final Scriptable scriptable; 37 private final BeansWrapper wrapper; 38 39 public RhinoScriptableModel(Scriptable scriptable, BeansWrapper wrapper) { 40 this.scriptable = scriptable; 41 this.wrapper = wrapper; 42 } 43 44 public TemplateModel get(String key) throws TemplateModelException { 45 Object retval = ScriptableObject.getProperty(scriptable, key); 46 if(retval instanceof Function) { 47 return new RhinoFunctionModel((Function)retval, scriptable, wrapper); 48 } 49 else { 50 return wrapper.wrap(retval); 51 } 52 } 53 54 public TemplateModel get(int index) throws TemplateModelException { 55 Object retval = ScriptableObject.getProperty(scriptable, index); 56 if(retval instanceof Function) { 57 return new RhinoFunctionModel((Function)retval, scriptable, wrapper); 58 } 59 else { 60 return wrapper.wrap(retval); 61 } 62 } 63 64 public boolean isEmpty() { 65 return scriptable.getIds().length == 0; 66 } 67 68 public TemplateCollectionModel keys() throws TemplateModelException { 69 return (TemplateCollectionModel)wrapper.wrap(scriptable.getIds()); 70 } 71 72 public int size() { 73 return scriptable.getIds().length; 74 } 75 76 public TemplateCollectionModel values() throws TemplateModelException { 77 Object [] ids = scriptable.getIds(); 78 Object [] values = new Object [ids.length]; 79 for (int i = 0; i < values.length; i++) { 80 Object id = ids[i]; 81 if(id instanceof Number ) { 82 values[i] = ScriptableObject.getProperty(scriptable, 83 ((Number )id).intValue()); 84 } 85 else { 86 values[i] = ScriptableObject.getProperty(scriptable, 87 String.valueOf(id)); 88 } 89 } 90 return (TemplateCollectionModel)wrapper.wrap(values); 91 } 92 93 Scriptable getScriptable() { 94 return scriptable; 95 } 96 97 BeansWrapper getWrapper() { 98 return wrapper; 99 } 100 101 public Object getAdaptedObject(Class hint) { 102 try { 103 return NativeJavaObject.coerceType(hint, scriptable); 104 } 105 catch(EvaluatorException e) { 106 return NativeJavaObject.coerceType(Object .class, scriptable); 107 } 108 } 109 } 110 | Popular Tags |