| 1 package org.shiftone.cache.config; 2 3 4 5 import org.shiftone.cache.ConfigurationException; 6 import org.shiftone.cache.util.Log; 7 8 import java.io.PrintStream ; 9 import java.util.Collection ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 14 15 19 public class Node 20 { 21 22 private static final Log LOG = new Log(Node.class); 23 private Map children = new HashMap (); 24 private String key = null; 25 private String value = null; 26 27 30 public Node(String key, String value) 31 { 32 this.value = value; 33 this.key = key; 34 } 35 36 37 40 public String getKey() 41 { 42 return key; 43 } 44 45 46 49 public String getValue() 50 { 51 return value; 52 } 53 54 55 58 public Collection getChildren() 59 { 60 return children.values(); 61 } 62 63 64 public boolean hasNode(String key) 65 { 66 return getNode(key) != null; 67 } 68 69 70 public Node getRequiredNode(String key) throws ConfigurationException 71 { 72 73 Node node = getNode(key); 74 75 if (node == null) 76 { 77 throw new ConfigurationException("node not found : " + key); 78 } 79 80 return node; 81 } 82 83 84 public Node getNode(String key) 85 { 86 return getNodeInternal(PropertiesTree.tokenize(key), 0); 87 } 88 89 90 93 private Node getNodeInternal(String [] keyParts, int partIndex) 94 { 95 96 if (keyParts.length == partIndex) 97 { 98 return this; 99 } 100 else 101 { 102 String part = keyParts[partIndex]; 103 Node nextChild = (Node) children.get(part); 104 105 if (nextChild == null) 106 { 107 LOG.debug("node not found : " + fullKey(keyParts, partIndex)); 108 109 return null; 110 } 111 112 return nextChild.getNodeInternal(keyParts, partIndex + 1); 113 } 114 } 115 116 117 120 private String fullKey(String [] keyParts, int partIndex) 121 { 122 123 String fullKey = keyParts[0]; 124 125 for (int i = 1; i < partIndex; i++) 126 { 127 fullKey += ("." + keyParts[i]); 128 } 129 130 return fullKey; 131 } 132 133 134 137 public void createNode(String key, String value) 138 { 139 createNode(PropertiesTree.tokenize(key, "."), 0, value); 140 } 141 142 143 146 private void createNode(String [] keyParts, int partIndex, String value) 147 { 148 149 if (keyParts.length == partIndex) 150 { 151 this.value = value; 152 } 153 else 154 { 155 Node nextChild = null; 156 String part = keyParts[partIndex]; 157 158 if (children.containsKey(part)) 159 { 160 nextChild = (Node) children.get(part); 161 } 162 else 163 { 164 nextChild = new Node(part, null); 165 166 children.put(part, nextChild); 167 } 168 169 nextChild.createNode(keyParts, partIndex + 1, value); 170 } 171 } 172 173 174 public void print() 175 { 176 print(System.out, 0); 177 } 178 179 180 183 private void print(PrintStream out, int indentLevel) 184 { 185 186 Collection children = getChildren(); 187 188 if (children.size() == 0) 189 { 190 out.println(bufferString(indentLevel, '\t') + "<" + getKey() + " value=\"" + getValue() + "\"/>"); 191 } 192 else 193 { 194 out.println(bufferString(indentLevel, '\t') + "<" + getKey() + " value=\"" + getValue() + "\">"); 195 196 Iterator i = children.iterator(); 197 198 while (i.hasNext()) 199 { 200 ((Node) i.next()).print(out, indentLevel + 1); 201 } 202 203 out.println(bufferString(indentLevel, '\t') + "</" + getKey() + ">"); 204 } 205 } 206 207 208 private static String bufferString(int indentLevel, char c) 209 { 210 211 StringBuffer sb = new StringBuffer (indentLevel); 212 213 for (int i = 0; i < indentLevel; i++) 214 { 215 sb.append(c); 216 } 217 218 return sb.toString(); 219 } 220 } 221 | Popular Tags |