1 52 53 package freemarker.ext.beans; 54 55 import java.util.Iterator ; 56 import java.util.NoSuchElementException ; 57 58 import freemarker.template.TemplateCollectionModel; 59 import freemarker.template.TemplateModel; 60 import freemarker.template.TemplateModelException; 61 import freemarker.template.TemplateModelIterator; 62 63 78 79 public class IteratorModel 80 extends 81 BeanModel 82 implements 83 TemplateModelIterator, 84 TemplateCollectionModel 85 { 86 private boolean accessed = false; 87 88 96 public IteratorModel(Iterator iterator, BeansWrapper wrapper) 97 { 98 super(iterator, wrapper); 99 } 100 101 105 public TemplateModelIterator iterator() throws TemplateModelException 106 { 107 synchronized(this) { 108 if(accessed) { 109 throw new TemplateModelException( 110 "This collection is stateful and can not be iterated over the" + 111 " second time."); 112 } 113 accessed = true; 114 } 115 return this; 116 } 117 118 121 public boolean hasNext() { 122 return ((Iterator )object).hasNext(); 123 } 124 125 126 129 public TemplateModel next() 130 throws 131 TemplateModelException 132 { 133 try { 134 return wrap(((Iterator )object).next()); 135 } 136 catch(NoSuchElementException e) { 137 throw new TemplateModelException( 138 "No more elements in the iterator."); 139 } 140 } 141 142 147 public boolean getAsBoolean() { 148 return hasNext(); 149 } 150 } 151 | Popular Tags |