001: package com.teamkonzept.lib;
002:
003: import java.util.*;
004: import org.apache.log4j.Category;
005:
006: public abstract class TKIteratorPlugin {
007: private static final Category CATEGORY = Category
008: .getInstance(TKIteratorPlugin.class);
009:
010: private TKVector list;
011: private int index;
012: private boolean doSort;
013: private Object prevItem;
014: private boolean first;
015: private boolean initialized;
016:
017: public String name;
018:
019: /**
020: * konstruktor, der Vector.init(Set) nutzt
021: */
022: public TKIteratorPlugin(TKHashtable hash, String name,
023: boolean doSort, boolean sortedForward) {
024: if (hash != null) {
025: this .list = new TKVector(hash.keySet());
026: } else {
027: this .list = new TKVector();
028: }
029: this .index = 0;
030: this .first = true;
031: this .name = name;
032: this .doSort = doSort;
033: this .prevItem = null;
034: this .initialized = true;
035:
036: if (doSort) {
037: if (sortedForward) {
038: this .list = this .list.sort();
039: } else {
040: this .list = this .list.rsort();
041: }
042: }
043: init();
044: }
045:
046: public TKIteratorPlugin(Enumeration list, String name,
047: boolean doSort, boolean sortedForward) {
048: this .list = new TKVector();
049: if (list != null) {
050: while (list.hasMoreElements()) {
051: this .list.addElement(list.nextElement());
052: }
053: }
054: this .index = 0;
055: this .first = true;
056: this .name = name;
057: this .doSort = doSort;
058: this .prevItem = null;
059: this .initialized = true;
060:
061: if (doSort) {
062: if (sortedForward) {
063: this .list = this .list.sort();
064: } else {
065: this .list = this .list.rsort();
066: }
067: }
068: init();
069: }
070:
071: public TKIteratorPlugin(Enumeration list, String name,
072: boolean doSort) {
073: this (list, name, doSort, true);
074: }
075:
076: public TKIteratorPlugin(Enumeration list, String name,
077: boolean doSort, Object oneMore) {
078:
079: this .list = new TKVector();
080:
081: if (oneMore != null)
082: this .list.addElement(oneMore);
083:
084: while ((list != null) && list.hasMoreElements())
085: this .list.addElement(list.nextElement());
086:
087: this .index = 0;
088: this .first = true;
089: this .name = name;
090: this .doSort = doSort;
091: this .initialized = true;
092:
093: if (doSort && (this .list != null))
094: this .list = this .list.sort();
095:
096: init();
097: }
098:
099: 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))
109: this .list = this .list.sort();
110:
111: init();
112: }
113:
114: public TKIteratorPlugin(TKVector list, String name, boolean doSort,
115: Object oneMore) {
116:
117: if (oneMore == null)
118: this .list = list;
119: else {
120: this .list = new TKVector();
121: this .list.addElement(oneMore);
122: this .list.concat(list);
123: }
124:
125: this .index = 0;
126: this .first = true;
127: this .name = name;
128: this .doSort = doSort;
129: this .initialized = true;
130:
131: if (doSort && (this .list != null))
132: this .list = this .list.sort();
133:
134: init();
135: }
136:
137: public TKIteratorPlugin(TKHashtable list, String name,
138: boolean doSort) {
139:
140: Enumeration e = list == null ? null : list.keys();
141:
142: TKVector tmp = new TKVector();
143: while ((e != null) && e.hasMoreElements())
144: tmp.addElement(e.nextElement());
145:
146: tmp = doSort ? tmp.sort() : tmp;
147:
148: this .list = new TKVector();
149: e = tmp.elements();
150: while (e.hasMoreElements())
151: this .list.addElement(list.get(e.nextElement()));
152:
153: this .index = 0;
154: this .first = true;
155: this .name = name;
156: this .doSort = doSort;
157: this .initialized = true;
158:
159: init();
160: }
161:
162: public TKIteratorPlugin(TKHashtable list, String name,
163: boolean doSort, Object oneMore) {
164:
165: TKVector tmp = new TKVector();
166:
167: Enumeration e = list == null ? null : list.keys();
168: while ((e != null) && e.hasMoreElements())
169: tmp.addElement(e.nextElement());
170:
171: tmp = doSort ? tmp.sort() : tmp;
172:
173: this .list = new TKVector();
174: if (oneMore != null)
175: this .list.addElement(oneMore);
176:
177: e = tmp.elements();
178: while (e.hasMoreElements())
179: this .list.addElement(list.get(e.nextElement()));
180:
181: this .index = 0;
182: this .first = true;
183: this .name = name;
184: this .doSort = doSort;
185: this .initialized = true;
186:
187: init();
188: }
189:
190: public void init() {
191: }
192:
193: public abstract boolean applyThis(Object item, TKTemplate template,
194: String path);
195:
196: // wird nirgends benutzt !!!
197: public void applyAfter(Object item, TKTemplate template, String path) {
198: }
199:
200: public boolean applyChilds(TKTemplate template, int i,
201: String currListName, String path) {
202: return false;
203: }
204:
205: public boolean apply(TKTemplate template, String currListName,
206: String path) {
207:
208: return apply(template, -1, currListName, path);
209: }
210:
211: public boolean apply(TKTemplate template, int i,
212: String currListName, String path) {
213: path = path == null || path.length() == 0 ? name : path + '.'
214: + name;
215: if (!currListName.equalsIgnoreCase(name))
216: return applyChilds(template, i, currListName, path);
217:
218: // Kontext merken
219: template.setEnumerationContext(name, new Integer(i));
220: if (i >= 0 && index != i) {
221: index = i;
222: if (index == 0) {
223: first = true;
224: prevItem = null;
225: if (!initialized)
226: init();
227: initialized = true;
228: }
229: }
230:
231: while ((list != null) && (index < list.size())) {
232: try {
233: Object item = list.get(index++);
234: if (item == null)
235: continue;
236:
237: initialized = false;
238: if (prevItem != null)
239: applyAfter(prevItem, template, path);
240:
241: if (this .first)
242: template.set(path + ".FIRST_INDEX", new Boolean(
243: true));
244: this .first = false;
245:
246: if (applyThis(item, template, path)) {
247:
248: prevItem = item;
249: return true;
250:
251: } else
252: prevItem = null;
253:
254: } catch (Throwable th) {
255: CATEGORY.error("apply", th);
256: throw new Error(th.getMessage());
257: }
258: }
259:
260: if (prevItem != null)
261: applyAfter(prevItem, template, path);
262:
263: index = 0;
264: first = true;
265: prevItem = null;
266: if (!initialized)
267: init();
268: initialized = true;
269:
270: return false;
271: }
272: }
|