1 package com.teamkonzept.lib; 2 3 import java.util.*; 4 5 6 10 public class TKHashtableIteratorPlugin extends TKIteratorPlugin 11 { 12 13 private String itemName; 14 private boolean doSort; 15 private TKHashtable table; 16 private TKHashtable subPlugins = new TKHashtable(); 17 18 public TKHashtableIteratorPlugin (String listName, String itemName, TKHashtable table, boolean doSort) 19 { 20 super (table, listName, doSort, 21 table instanceof TKSortedHashtable 22 ? ((TKSortedHashtable) table).sortedForward() 23 : true); 24 25 this.itemName = itemName; 26 this.doSort = doSort; 27 this.table = table; 28 } 29 30 public boolean applyThis (Object item, TKTemplate template, String path){ 31 Integer idx = (Integer )template.getEnumerationContext(name); 32 TKHashtable subs = (TKHashtable) subPlugins.get(idx); 33 if (subs == null){ 34 subs = new TKHashtable(); 35 subPlugins.put(idx, subs); 36 } 37 if (table == null || item == null){ 38 return true; 39 } 40 Object value = table.get(item); 41 if (value == null){ 42 return true; 43 } 44 template.set("ITERATOR_PATH", path); 45 template.set(name+".KEY", item.toString()); 46 if (value instanceof TKHashtable){ 47 String nameDotSub = name + ".SUB"; 48 applyTable (name, template, (TKHashtable) value, itemName,subs,path); 49 subs.put (nameDotSub, 50 new TKHashtableIteratorPlugin(nameDotSub, itemName, 51 (TKHashtable)value, true)); 52 template.set(nameDotSub + ".LEN",new Integer (((TKHashtable) value).size())); 53 return true; 54 } 55 else if (value instanceof TKVector){ 56 String nameDotSub = name + ".SUB"; 57 subs.put (nameDotSub, 58 new TKVectorIteratorPlugin(nameDotSub, itemName, 59 (TKVector)value, doSort)); 60 template.set(nameDotSub + ".LEN", new Integer (((TKVector) value).size())); 61 return true; 62 } 63 else if (value instanceof TKHashable){ 64 String nameDotSub = name + ".SUB"; 65 applyTable (name, template, 66 ((TKHashable) value).toHashtable(),itemName,subs,path); 67 TKHashtable sub = ((TKHashable) value).toHashtable(); 68 subs.put (nameDotSub, 69 new TKHashtableIteratorPlugin (nameDotSub, itemName, sub, true)); 70 template.set(nameDotSub + ".LEN", new Integer (sub.size())); 71 return true; 72 } 73 else if (value instanceof TKVectorizable) { 74 String nameDotSub = name + ".SUB"; 75 TKVector sub = ((TKVectorizable) value).toVector(); 76 subs.put (nameDotSub, 77 new TKVectorIteratorPlugin (nameDotSub,itemName,sub,doSort)); 78 template.set(nameDotSub + ".LEN",new Integer (sub.size())); 79 return true; 80 } 81 if (itemName == null){ 82 itemName = "ITEM_VALUE"; 83 } 84 else{ 85 template.set(itemName, value.toString()); 86 } 87 template.set("ITEM_NAME", itemName); 88 template.set("ITEM_VALUE", value.toString()); 89 return true; 90 } 91 92 public boolean applyChilds (TKTemplate template, int i, String currListName, String path) 93 { 94 if (subPlugins == null) return false; 95 Integer idx = (Integer )template.getEnumerationContext(name); 96 if (idx == null) 97 return false; 98 TKHashtable subs = (TKHashtable) subPlugins.get(idx); 99 if (subs == null) 100 return false; 101 102 Enumeration e = subs.elements(); 103 while (e.hasMoreElements()) 104 { 105 106 TKIteratorPlugin plugin = (TKIteratorPlugin) e.nextElement(); 107 if (plugin.apply (template,i,currListName,null)) return true; 108 } 109 110 return false; 111 } 112 113 public static void applyTable (String listName, TKTemplate template, 114 TKHashtable table, String itemName, 115 TKHashtable subPlugins, String path) { 116 if (itemName != null){ 117 template.set("ITEM_NAME",itemName); 118 } 119 template.set("ITERATOR_PATH",path); 120 121 Iterator it = table.entrySet().iterator(); 122 while (it.hasNext()) { 123 Map.Entry entry = (Map.Entry)it.next(); 124 Object value = entry.getValue(); 125 String fieldName = entry.getKey().toString(); 126 127 if (value instanceof TKHashtable) { 128 String listNameDotFieldName = listName + '.' + fieldName; 129 applyTable (listName,template, (TKHashtable) value, 130 itemName == null ? fieldName : itemName + '.' + fieldName, 131 subPlugins, path); 132 subPlugins.put (listNameDotFieldName, 133 new TKHashtableIteratorPlugin(listNameDotFieldName, 134 itemName, 135 (TKHashtable)value, true)); 136 template.set(listNameDotFieldName + ".LEN", 137 new Integer (((TKHashtable) value).size())); 138 } 139 else if (value instanceof TKVector) { 140 String listNameDotFieldName = listName + '.' + fieldName; 141 subPlugins.put (listNameDotFieldName, 142 new TKVectorIteratorPlugin(listNameDotFieldName, 143 itemName, 144 (TKVector)value, false)); 145 template.set(listNameDotFieldName + ".LEN", 146 new Integer (((TKVector)value).size())); 147 } 148 else if (value instanceof TKHashable) { 149 String listNameDotFieldName = listName + '.' + fieldName; 150 applyTable(listName, template, ((TKHashable)value).toHashtable(), 151 itemName == null ? fieldName : itemName + '.' + fieldName, 152 subPlugins, path); 153 TKHashtable sub = ((TKHashable)value).toHashtable(); 154 subPlugins.put(listNameDotFieldName, 155 new TKHashtableIteratorPlugin(listNameDotFieldName, itemName, sub, true)); 156 template.set(listNameDotFieldName + ".LEN", new Integer (sub.size())); 157 } 158 else if (value instanceof TKVectorizable) { 159 String listNameDotFieldName = listName + '.' + fieldName; 160 TKVector sub = ((TKVectorizable) value).toVector(); 161 subPlugins.put (listNameDotFieldName, 162 new TKVectorIteratorPlugin(listNameDotFieldName, itemName, sub, false)); 163 template.set(listNameDotFieldName + ".LEN", new Integer (sub.size())); 164 } 165 else { 166 if (itemName != null) { 167 template.set (itemName + '.' + fieldName, value); 168 } 169 template.set (fieldName, value); 170 } 171 } 172 } 173 } 174 | Popular Tags |