1 16 17 package org.apache.commons.configuration; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 44 abstract class HierarchicalConfigurationConverter 45 { 46 55 public void process(Configuration config) 56 { 57 if (config != null) 58 { 59 ConfigurationKey keyEmpty = new ConfigurationKey(); 60 ConfigurationKey keyLast = keyEmpty; 61 Set keySet = new HashSet (); 62 63 for (Iterator it = config.getKeys(); it.hasNext();) 64 { 65 String key = (String ) it.next(); 66 if (keySet.contains(key)) 67 { 68 continue; 70 } 71 ConfigurationKey keyAct = new ConfigurationKey(key); 72 closeElements(keyLast, keyAct); 73 String elem = openElements(keyLast, keyAct, config, keySet); 74 fireValue(elem, config.getProperty(key)); 75 keyLast = keyAct; 76 } 77 78 closeElements(keyLast, keyEmpty); 80 } 81 } 82 83 92 protected abstract void elementStart(String name, Object value); 93 94 102 protected abstract void elementEnd(String name); 103 104 114 protected void closeElements(ConfigurationKey keyLast, ConfigurationKey keyAct) 115 { 116 ConfigurationKey keyDiff = keyAct.differenceKey(keyLast); 117 Iterator it = reverseIterator(keyDiff); 118 if (it.hasNext()) 119 { 120 it.next(); 122 } 123 124 while (it.hasNext()) 125 { 126 elementEnd((String ) it.next()); 127 } 128 } 129 130 138 protected Iterator reverseIterator(ConfigurationKey key) 139 { 140 List list = new ArrayList (); 141 for (ConfigurationKey.KeyIterator it = key.iterator(); it.hasNext();) 142 { 143 list.add(it.nextKey()); 144 } 145 146 Collections.reverse(list); 147 return list.iterator(); 148 } 149 150 162 protected String openElements(ConfigurationKey keyLast, ConfigurationKey keyAct, Configuration config, Set keySet) 163 { 164 ConfigurationKey.KeyIterator it = keyLast.differenceKey(keyAct).iterator(); 165 ConfigurationKey k = keyLast.commonKey(keyAct); 166 for (it.nextKey(); it.hasNext(); it.nextKey()) 167 { 168 k.append(it.currentKey(true)); 169 elementStart(it.currentKey(true), config.getProperty(k.toString())); 170 keySet.add(k.toString()); 171 } 172 return it.currentKey(true); 173 } 174 175 184 protected void fireValue(String name, Object value) 185 { 186 if (value != null && value instanceof Collection ) 187 { 188 for (Iterator it = ((Collection ) value).iterator(); it.hasNext();) 189 { 190 fireValue(name, it.next()); 191 } 192 } 193 else 194 { 195 elementStart(name, value); 196 elementEnd(name); 197 } 198 } 199 } 200 | Popular Tags |