1 52 53 package freemarker.ext.beans; 54 55 import java.util.Enumeration ; 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 74 75 public class EnumerationModel 76 extends 77 BeanModel 78 implements 79 TemplateModelIterator, 80 TemplateCollectionModel 81 { 82 private boolean accessed = false; 83 84 92 public EnumerationModel(Enumeration enumeration, BeansWrapper wrapper) 93 { 94 super(enumeration, wrapper); 95 } 96 97 101 public TemplateModelIterator iterator() throws TemplateModelException 102 { 103 synchronized(this) { 104 if(accessed) { 105 throw new TemplateModelException( 106 "This collection is stateful and can not be iterated over the" + 107 " second time."); 108 } 109 accessed = true; 110 } 111 return this; 112 } 113 114 117 public boolean hasNext() { 118 return ((Enumeration )object).hasMoreElements(); 119 } 120 121 122 125 public TemplateModel next() 126 throws 127 TemplateModelException 128 { 129 try { 130 return wrap(((Enumeration )object).nextElement()); 131 } 132 catch(NoSuchElementException e) { 133 throw new TemplateModelException( 134 "No more elements in the enumeration."); 135 } 136 } 137 138 143 public boolean getAsBoolean() { 144 return hasNext(); 145 } 146 } 147 | Popular Tags |