1 52 53 package freemarker.ext.jython; 54 55 import org.python.core.PyException; 56 import org.python.core.PyObject; 57 58 import freemarker.ext.util.ModelFactory; 59 import freemarker.template.ObjectWrapper; 60 import freemarker.template.TemplateCollectionModel; 61 import freemarker.template.TemplateModel; 62 import freemarker.template.TemplateModelException; 63 import freemarker.template.TemplateModelIterator; 64 import freemarker.template.TemplateSequenceModel; 65 66 71 public class JythonSequenceModel 72 extends 73 JythonModel 74 implements 75 TemplateSequenceModel, 76 TemplateCollectionModel 77 { 78 static final ModelFactory FACTORY = 79 new ModelFactory() 80 { 81 public TemplateModel create(Object object, ObjectWrapper wrapper) 82 { 83 return new JythonSequenceModel((PyObject)object, (JythonWrapper)wrapper); 84 } 85 }; 86 87 public JythonSequenceModel(PyObject object, JythonWrapper wrapper) 88 { 89 super(object, wrapper); 90 } 91 92 95 public TemplateModel get(int index) throws TemplateModelException 96 { 97 try 98 { 99 return wrapper.wrap(object.__finditem__(index)); 100 } 101 catch(PyException e) 102 { 103 throw new TemplateModelException(e); 104 } 105 } 106 107 110 public int size() throws TemplateModelException 111 { 112 try 113 { 114 return object.__len__(); 115 } 116 catch(PyException e) 117 { 118 throw new TemplateModelException(e); 119 } 120 } 121 122 public TemplateModelIterator iterator() 123 { 124 return new TemplateModelIterator() 125 { 126 int i = 0; 127 128 public boolean hasNext() throws TemplateModelException 129 { 130 return i < size(); 131 } 132 133 public TemplateModel next() throws TemplateModelException 134 { 135 return get(i++); 136 } 137 }; 138 } 139 } 140 | Popular Tags |