001: package com.teamkonzept.lib;
002:
003: import java.util.*;
004:
005: /**
006: * @author $Author: mischa $
007: * @version $Revision: 1.10 $
008: */
009: public class TKHashtableIteratorPlugin extends TKIteratorPlugin {
010:
011: private String itemName;
012: private boolean doSort;
013: private TKHashtable table;
014: private TKHashtable subPlugins = new TKHashtable();
015:
016: public TKHashtableIteratorPlugin(String listName, String itemName,
017: TKHashtable table, boolean doSort) {
018: super (
019: table,
020: listName,
021: doSort,
022: table instanceof TKSortedHashtable ? ((TKSortedHashtable) table)
023: .sortedForward()
024: : true);
025:
026: this .itemName = itemName;
027: this .doSort = doSort;
028: this .table = table;
029: }
030:
031: public boolean applyThis(Object item, TKTemplate template,
032: String path) {
033: Integer idx = (Integer) template.getEnumerationContext(name);
034: TKHashtable subs = (TKHashtable) subPlugins.get(idx);
035: if (subs == null) {
036: subs = new TKHashtable();
037: subPlugins.put(idx, subs);
038: }
039: if (table == null || item == null) {
040: return true;
041: }
042: Object value = table.get(item);
043: if (value == null) {
044: return true;
045: }
046: template.set("ITERATOR_PATH", path);
047: template.set(name + ".KEY", item.toString());
048: if (value instanceof TKHashtable) {
049: String nameDotSub = name + ".SUB";
050: applyTable(name, template, (TKHashtable) value, itemName,
051: subs, path);
052: subs.put(nameDotSub, new TKHashtableIteratorPlugin(
053: nameDotSub, itemName, (TKHashtable) value, true));
054: template.set(nameDotSub + ".LEN", new Integer(
055: ((TKHashtable) value).size()));
056: return true;
057: } else if (value instanceof TKVector) {
058: String nameDotSub = name + ".SUB";
059: subs.put(nameDotSub, new TKVectorIteratorPlugin(nameDotSub,
060: itemName, (TKVector) value, doSort));
061: template.set(nameDotSub + ".LEN", new Integer(
062: ((TKVector) value).size()));
063: return true;
064: } else if (value instanceof TKHashable) {
065: String nameDotSub = name + ".SUB";
066: applyTable(name, template, ((TKHashable) value)
067: .toHashtable(), itemName, subs, path);
068: TKHashtable sub = ((TKHashable) value).toHashtable();
069: subs.put(nameDotSub, new TKHashtableIteratorPlugin(
070: nameDotSub, itemName, sub, true));
071: template.set(nameDotSub + ".LEN", new Integer(sub.size()));
072: return true;
073: } else if (value instanceof TKVectorizable) {
074: String nameDotSub = name + ".SUB";
075: TKVector sub = ((TKVectorizable) value).toVector();
076: subs.put(nameDotSub, new TKVectorIteratorPlugin(nameDotSub,
077: itemName, sub, doSort));
078: template.set(nameDotSub + ".LEN", new Integer(sub.size()));
079: return true;
080: }
081: if (itemName == null) {
082: itemName = "ITEM_VALUE";
083: } else {
084: template.set(itemName, value.toString());
085: }
086: template.set("ITEM_NAME", itemName);
087: template.set("ITEM_VALUE", value.toString());
088: return true;
089: }
090:
091: public boolean applyChilds(TKTemplate template, int i,
092: String currListName, String path) {
093: if (subPlugins == null)
094: return false;
095: Integer idx = (Integer) template.getEnumerationContext(name);
096: if (idx == null)
097: return false;
098: TKHashtable subs = (TKHashtable) subPlugins.get(idx);
099: if (subs == null)
100: return false;
101:
102: Enumeration e = subs.elements();
103: while (e.hasMoreElements()) {
104:
105: TKIteratorPlugin plugin = (TKIteratorPlugin) e
106: .nextElement();
107: if (plugin.apply(template, i, currListName, null))
108: return true;
109: }
110:
111: return false;
112: }
113:
114: public static void applyTable(String listName, TKTemplate template,
115: TKHashtable table, String itemName, TKHashtable subPlugins,
116: String path) {
117: if (itemName != null) {
118: template.set("ITEM_NAME", itemName);
119: }
120: template.set("ITERATOR_PATH", path);
121:
122: Iterator it = table.entrySet().iterator();
123: while (it.hasNext()) {
124: Map.Entry entry = (Map.Entry) it.next();
125: Object value = entry.getValue();
126: String fieldName = entry.getKey().toString();
127:
128: if (value instanceof TKHashtable) {
129: String listNameDotFieldName = listName + '.'
130: + fieldName;
131: applyTable(listName, template, (TKHashtable) value,
132: itemName == null ? fieldName : itemName + '.'
133: + fieldName, subPlugins, path);
134: subPlugins.put(listNameDotFieldName,
135: new TKHashtableIteratorPlugin(
136: listNameDotFieldName, itemName,
137: (TKHashtable) value, true));
138: template.set(listNameDotFieldName + ".LEN",
139: new Integer(((TKHashtable) value).size()));
140: } else if (value instanceof TKVector) {
141: String listNameDotFieldName = listName + '.'
142: + fieldName;
143: subPlugins.put(listNameDotFieldName,
144: new TKVectorIteratorPlugin(
145: listNameDotFieldName, itemName,
146: (TKVector) value, false));
147: template.set(listNameDotFieldName + ".LEN",
148: new Integer(((TKVector) value).size()));
149: } else if (value instanceof TKHashable) {
150: String listNameDotFieldName = listName + '.'
151: + fieldName;
152: applyTable(listName, template, ((TKHashable) value)
153: .toHashtable(), itemName == null ? fieldName
154: : itemName + '.' + fieldName, subPlugins, path);
155: TKHashtable sub = ((TKHashable) value).toHashtable();
156: subPlugins.put(listNameDotFieldName,
157: new TKHashtableIteratorPlugin(
158: listNameDotFieldName, itemName, sub,
159: true));
160: template.set(listNameDotFieldName + ".LEN",
161: new Integer(sub.size()));
162: } else if (value instanceof TKVectorizable) {
163: String listNameDotFieldName = listName + '.'
164: + fieldName;
165: TKVector sub = ((TKVectorizable) value).toVector();
166: subPlugins.put(listNameDotFieldName,
167: new TKVectorIteratorPlugin(
168: listNameDotFieldName, itemName, sub,
169: false));
170: template.set(listNameDotFieldName + ".LEN",
171: new Integer(sub.size()));
172: } else {
173: if (itemName != null) {
174: template.set(itemName + '.' + fieldName, value);
175: }
176: template.set(fieldName, value);
177: }
178: }
179: }
180: }
|