1 52 53 package freemarker.ext.jython; 54 55 import org.python.core.Py; 56 import org.python.core.PyException; 57 import org.python.core.PyObject; 58 59 import freemarker.ext.util.ModelFactory; 60 import freemarker.template.ObjectWrapper; 61 import freemarker.template.TemplateModel; 62 import freemarker.template.TemplateModelException; 63 import freemarker.template.TemplateNumberModel; 64 65 71 public class JythonNumberModel 72 extends 73 JythonModel 74 implements 75 TemplateNumberModel 76 { 77 static final ModelFactory FACTORY = 78 new ModelFactory() 79 { 80 public TemplateModel create(Object object, ObjectWrapper wrapper) 81 { 82 return new JythonNumberModel((PyObject)object, (JythonWrapper)wrapper); 83 } 84 }; 85 86 public JythonNumberModel(PyObject object, JythonWrapper wrapper) 87 { 88 super(object, wrapper); 89 } 90 91 96 public Number getAsNumber() throws TemplateModelException 97 { 98 try 99 { 100 Object value = object.__tojava__(java.lang.Number .class); 101 if(value == null || value == Py.NoConversion) 102 { 103 return new Double (object.__float__().getValue()); 104 } 105 return (Number )value; 106 } 107 catch(PyException e) 108 { 109 throw new TemplateModelException(e); 110 } 111 } 112 } 113 | Popular Tags |