1 package com.teamkonzept.lib; 2 3 import java.util.*; 4 import org.apache.log4j.Category; 5 6 public abstract class TKIteratorPlugin 7 { 8 private static final Category CATEGORY = Category.getInstance(TKIteratorPlugin.class); 9 10 private TKVector list; 11 private int index; 12 private boolean doSort; 13 private Object prevItem; 14 private boolean first; 15 private boolean initialized; 16 17 public String name; 18 19 22 public TKIteratorPlugin (TKHashtable hash, String name, 23 boolean doSort, boolean sortedForward) { 24 if ( hash != null ){ 25 this.list = new TKVector (hash.keySet()); 26 } 27 else{ 28 this.list = new TKVector (); 29 } 30 this.index = 0; 31 this.first = true; 32 this.name = name; 33 this.doSort = doSort; 34 this.prevItem = null; 35 this.initialized = true; 36 37 if ( doSort ){ 38 if ( sortedForward ){ 39 this.list = this.list.sort(); 40 } 41 else{ 42 this.list = this.list.rsort(); 43 } 44 } 45 init(); 46 } 47 48 49 public TKIteratorPlugin (Enumeration list, String name, 50 boolean doSort, boolean sortedForward) { 51 this.list = new TKVector (); 52 if ( list != null ){ 53 while ( list.hasMoreElements() ){ 54 this.list.addElement(list.nextElement()); 55 } 56 } 57 this.index = 0; 58 this.first = true; 59 this.name = name; 60 this.doSort = doSort; 61 this.prevItem = null; 62 this.initialized = true; 63 64 if ( doSort ){ 65 if ( sortedForward ){ 66 this.list = this.list.sort(); 67 } 68 else{ 69 this.list = this.list.rsort(); 70 } 71 } 72 init(); 73 } 74 75 public TKIteratorPlugin (Enumeration list, String name, boolean doSort) { 76 this (list,name,doSort,true); 77 } 78 79 public TKIteratorPlugin (Enumeration list, String name, boolean doSort, Object oneMore) { 80 81 this.list = new TKVector (); 82 83 if (oneMore != null) this.list.addElement(oneMore); 84 85 while ((list != null) && list.hasMoreElements()) 86 this.list.addElement(list.nextElement()); 87 88 this.index = 0; 89 this.first = true; 90 this.name = name; 91 this.doSort = doSort; 92 this.initialized = true; 93 94 if (doSort && (this.list != null)) this.list = this.list.sort(); 95 96 init(); 97 } 98 99 public TKIteratorPlugin (TKVector list, String name, boolean doSort) { 100 101 this.list = list; 102 this.index = 0; 103 this.first = true; 104 this.name = name; 105 this.doSort = doSort; 106 this.initialized = true; 107 108 if (doSort && (this.list != null)) this.list = this.list.sort(); 109 110 init(); 111 } 112 113 public TKIteratorPlugin (TKVector list, String name, boolean doSort, Object oneMore) { 114 115 if (oneMore == null) this.list = list; 116 else { 117 this.list = new TKVector(); 118 this.list.addElement(oneMore); 119 this.list.concat(list); 120 } 121 122 this.index = 0; 123 this.first = true; 124 this.name = name; 125 this.doSort = doSort; 126 this.initialized = true; 127 128 if (doSort && (this.list != null)) this.list = this.list.sort(); 129 130 init(); 131 } 132 133 public TKIteratorPlugin (TKHashtable list, String name, boolean doSort) { 134 135 Enumeration e = list == null ? null : list.keys(); 136 137 TKVector tmp = new TKVector (); 138 while ((e != null) && e.hasMoreElements()) 139 tmp.addElement(e.nextElement()); 140 141 tmp = doSort ? tmp.sort() : tmp; 142 143 this.list = new TKVector(); 144 e = tmp.elements(); 145 while (e.hasMoreElements()) 146 this.list.addElement(list.get(e.nextElement())); 147 148 this.index = 0; 149 this.first = true; 150 this.name = name; 151 this.doSort = doSort; 152 this.initialized = true; 153 154 init(); 155 } 156 157 public TKIteratorPlugin (TKHashtable list, String name, boolean doSort, Object oneMore) { 158 159 TKVector tmp = new TKVector (); 160 161 Enumeration e = list == null ? null : list.keys(); 162 while ((e != null) && e.hasMoreElements()) 163 tmp.addElement(e.nextElement()); 164 165 tmp = doSort ? tmp.sort() : tmp; 166 167 this.list = new TKVector(); 168 if (oneMore != null) this.list.addElement(oneMore); 169 170 e = tmp.elements(); 171 while (e.hasMoreElements()) 172 this.list.addElement(list.get(e.nextElement())); 173 174 this.index = 0; 175 this.first = true; 176 this.name = name; 177 this.doSort = doSort; 178 this.initialized = true; 179 180 init(); 181 } 182 183 public void init () {} 184 185 public abstract boolean applyThis (Object item, TKTemplate template, String path); 186 187 public void applyAfter (Object item, TKTemplate template, String path) {} 189 190 public boolean applyChilds (TKTemplate template, int i, String currListName, String path) { return false; } 191 192 public boolean apply (TKTemplate template, String currListName, String path) { 193 194 return apply (template, -1, currListName, path); 195 } 196 197 public boolean apply (TKTemplate template, int i, String currListName, String path) 198 { 199 path = path == null || path.length() == 0 ? name : path+'.'+name; 200 if (!currListName.equalsIgnoreCase(name)) return applyChilds (template,i,currListName,path); 201 202 template.setEnumerationContext( name, new Integer (i) ); 204 if (i >= 0 && index != i) 205 { 206 index = i; 207 if (index == 0) 208 { 209 first = true; 210 prevItem = null; 211 if (!initialized) init(); 212 initialized = true; 213 } 214 } 215 216 while ((list != null) && (index < list.size())) 217 { 218 try { 219 Object item = list.get(index++); 220 if (item == null) continue; 221 222 initialized = false; 223 if (prevItem != null) applyAfter (prevItem,template,path); 224 225 if (this.first) template.set(path+".FIRST_INDEX",new Boolean (true)); 226 this.first = false; 227 228 if (applyThis (item,template,path)) { 229 230 prevItem = item; 231 return true; 232 233 } else prevItem = null; 234 235 } catch ( Throwable th ) 236 { 237 CATEGORY.error("apply", th); 238 throw new Error ( th.getMessage() ); 239 } 240 } 241 242 if (prevItem != null) applyAfter (prevItem,template,path); 243 244 index = 0; 245 first = true; 246 prevItem = null; 247 if (!initialized) init(); 248 initialized = true; 249 250 return false; 251 } 252 } 253 | Popular Tags |