1 52 53 package freemarker.ext.beans; 54 55 import java.util.Collection ; 56 import java.util.List ; 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 73 public class CollectionModel 74 extends 75 StringModel 76 implements 77 TemplateCollectionModel, 78 TemplateSequenceModel 79 { 80 static final ModelFactory FACTORY = 81 new ModelFactory() 82 { 83 public TemplateModel create(Object object, ObjectWrapper wrapper) 84 { 85 return new CollectionModel((Collection )object, (BeansWrapper)wrapper); 86 } 87 }; 88 89 90 98 public CollectionModel(Collection collection, BeansWrapper wrapper) 99 { 100 super(collection, wrapper); 101 } 102 103 108 public TemplateModel get(int index) 109 throws 110 TemplateModelException 111 { 112 if(object instanceof List ) 113 { 114 try 115 { 116 return wrap(((List )object).get(index)); 117 } 118 catch(IndexOutOfBoundsException e) 119 { 120 throw new TemplateModelException("Index out of bounds: " + index); 121 } 122 } 123 else 124 { 125 throw new TemplateModelException("Underlying collection is not a list, it's " + object.getClass().getName()); 126 } 127 } 128 129 public TemplateModelIterator iterator() 130 { 131 return new IteratorModel(((Collection )object).iterator(), wrapper); 132 } 133 134 public int size() 135 { 136 return ((Collection )object).size(); 137 } 138 } 139 | Popular Tags |