1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.util.ArrayList ; 58 import java.util.Collection ; 59 import java.util.Collections ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 63 80 abstract class HierarchicalConfigurationConverter 81 { 82 90 public void process(Configuration config) 91 { 92 if (config != null) 93 { 94 ConfigurationKey keyEmpty = new ConfigurationKey(); 95 ConfigurationKey keyLast = keyEmpty; 96 97 for (Iterator it = config.getKeys(); it.hasNext();) 98 { 99 String key = (String ) it.next(); 100 ConfigurationKey keyAct = new ConfigurationKey(key); 101 closeElements(keyLast, keyAct); 102 String elem = openElements(keyLast, keyAct); 103 fireValue(elem, config.getProperty(key)); 104 keyLast = keyAct; 105 } 106 107 closeElements(keyLast, keyEmpty); } 109 } 110 111 119 protected abstract void elementStart(String name, Object value); 120 121 128 protected abstract void elementEnd(String name); 129 130 139 protected void closeElements( 140 ConfigurationKey keyLast, 141 ConfigurationKey keyAct) 142 { 143 ConfigurationKey keyDiff = keyAct.differenceKey(keyLast); 144 Iterator it = reverseIterator(keyDiff); 145 if (it.hasNext()) 146 { 147 it.next(); 149 } 150 151 while (it.hasNext()) 152 { 153 elementEnd((String ) it.next()); 154 } 155 } 156 157 164 protected Iterator reverseIterator(ConfigurationKey key) 165 { 166 List list = new ArrayList (); 167 for (ConfigurationKey.KeyIterator it = key.iterator(); it.hasNext();) 168 { 169 list.add(it.nextKey()); 170 } 171 172 Collections.reverse(list); 173 return list.iterator(); 174 } 175 176 185 protected String openElements( 186 ConfigurationKey keyLast, 187 ConfigurationKey keyAct) 188 { 189 ConfigurationKey.KeyIterator it = 190 keyLast.differenceKey(keyAct).iterator(); 191 192 for (it.nextKey(); it.hasNext(); it.nextKey()) 193 { 194 elementStart(it.currentKey(), null); 195 } 196 197 return it.currentKey(); 198 } 199 200 208 protected void fireValue(String name, Object value) 209 { 210 if (value != null && value instanceof Collection ) 211 { 212 for (Iterator it = ((Collection ) value).iterator(); it.hasNext();) 213 { 214 fireValue(name, it.next()); 215 } 216 } 217 218 else 219 { 220 elementStart(name, value); 221 elementEnd(name); 222 } 223 } 224 } 225 | Popular Tags |