1 52 53 package freemarker.ext.jython; 54 55 import java.util.Iterator ; 56 import java.util.List ; 57 58 import org.python.core.Py; 59 import org.python.core.PyException; 60 import org.python.core.PyObject; 61 62 import freemarker.ext.util.ModelFactory; 63 import freemarker.ext.util.WrapperTemplateModel; 64 import freemarker.template.AdapterTemplateModel; 65 import freemarker.template.ObjectWrapper; 66 import freemarker.template.TemplateBooleanModel; 67 import freemarker.template.TemplateHashModel; 68 import freemarker.template.TemplateMethodModelEx; 69 import freemarker.template.TemplateModel; 70 import freemarker.template.TemplateModelException; 71 import freemarker.template.TemplateScalarModel; 72 73 78 public class JythonModel 79 implements TemplateBooleanModel, TemplateScalarModel, TemplateHashModel, 80 TemplateMethodModelEx, AdapterTemplateModel, WrapperTemplateModel 81 { 82 protected final PyObject object; 83 protected final JythonWrapper wrapper; 84 85 static final ModelFactory FACTORY = 86 new ModelFactory() 87 { 88 public TemplateModel create(Object object, ObjectWrapper wrapper) 89 { 90 return new JythonModel((PyObject)object, (JythonWrapper)wrapper); 91 } 92 }; 93 94 public JythonModel(PyObject object, JythonWrapper wrapper) 95 { 96 this.object = object; 97 this.wrapper = wrapper; 98 } 99 100 103 public boolean getAsBoolean() throws TemplateModelException 104 { 105 try 106 { 107 return object.__nonzero__(); 108 } 109 catch(PyException e) 110 { 111 throw new TemplateModelException(e); 112 } 113 } 114 115 118 public String getAsString() throws TemplateModelException 119 { 120 try 121 { 122 return object.toString(); 123 } 124 catch(PyException e) 125 { 126 throw new TemplateModelException(e); 127 } 128 } 129 130 137 public TemplateModel get(String key) 138 throws 139 TemplateModelException 140 { 141 if(key != null) 142 { 143 key = key.intern(); 144 } 145 146 PyObject obj = null; 147 148 try 149 { 150 if(wrapper.isAttributesShadowItems()) 151 { 152 obj = object.__findattr__(key); 153 if(obj == null) 154 { 155 obj = object.__finditem__(key); 156 } 157 } 158 else 159 { 160 obj = object.__finditem__(key); 161 if(obj == null) 162 { 163 obj = object.__findattr__(key); 164 } 165 } 166 } 167 catch(PyException e) 168 { 169 throw new TemplateModelException(e); 170 } 171 172 return wrapper.wrap(obj); 173 } 174 175 178 public boolean isEmpty() throws TemplateModelException 179 { 180 try 181 { 182 return object.__len__() == 0; 183 } 184 catch(PyException e) 185 { 186 throw new TemplateModelException(e); 187 } 188 } 189 190 193 public Object exec(List arguments) throws TemplateModelException 194 { 195 int size = arguments.size(); 196 try 197 { 198 switch(size) 199 { 200 case 0: 201 { 202 return wrapper.wrap(object.__call__()); 203 } 204 case 1: 205 { 206 return wrapper.wrap(object.__call__(wrapper.unwrap( 207 (TemplateModel)arguments.get(0)))); 208 } 209 default: 210 { 211 PyObject[] pyargs = new PyObject[size]; 212 int i = 0; 213 for (Iterator arg = arguments.iterator(); arg.hasNext();) 214 { 215 pyargs[i++] = wrapper.unwrap( 216 (TemplateModel) arg.next()); 217 } 218 return wrapper.wrap(object.__call__(pyargs)); 219 } 220 } 221 } 222 catch(PyException e) 223 { 224 throw new TemplateModelException(e); 225 } 226 } 227 228 public Object getAdaptedObject(Class hint) { 229 if(object == null) { 230 return null; 231 } 232 Object view = object.__tojava__(hint); 233 if(view == Py.NoConversion) { 234 view = object.__tojava__(Object .class); 235 } 236 return view; 237 } 238 239 public Object getWrappedObject() { 240 return object == null ? null : object.__tojava__(Object .class); 241 } 242 } 243 | Popular Tags |