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.*; 10 11 12 16 public class PropertiesTree 17 { 18 19 private static final Log LOG = new Log(PropertiesTree.class); 20 private Node root = null; 21 22 25 public PropertiesTree() throws ConfigurationException 26 { 27 initialize(new Properties()); 28 } 29 30 31 34 public PropertiesTree(Properties properties) throws ConfigurationException 35 { 36 initialize(properties); 37 } 38 39 40 43 private void initialize(Properties properties) throws ConfigurationException 44 { 45 46 this.root = new Node("ROOT", null); 47 48 Enumeration keys = properties.keys(); 49 50 while (keys.hasMoreElements()) 51 { 52 String key = (String ) keys.nextElement(); 53 String value = properties.getProperty(key); 54 55 root.createNode(key, value); 56 } 57 } 58 59 60 63 public Properties getProperties() 64 { 65 66 Properties properties = new Properties(); 67 68 populateProperties(properties, root, ""); 69 70 return properties; 71 } 72 73 74 77 private void populateProperties(Properties properties, Node node, String prefix) 78 { 79 80 Collection children = node.getChildren(); 81 Iterator i = children.iterator(); 82 83 while (i.hasNext()) 84 { 85 Node child = (Node) i.next(); 86 String key = prefix + child.getKey(); 87 String value = child.getValue(); 88 89 if (value != null) 90 { 91 properties.setProperty(key, value); 92 } 93 94 populateProperties(properties, child, key + "."); 95 } 96 } 97 98 99 104 public Node getRoot() 105 { 106 return root; 107 } 108 109 110 113 public void print(PrintStream out) 114 { 115 root.print(); 116 } 117 118 119 public static String [] tokenize(String text) 120 { 121 return tokenize(text, "."); 122 } 123 124 125 public static String [] tokenize(String text, String token) 126 { 127 128 StringTokenizer tokenizer = new StringTokenizer(text, token, false); 129 List list = new ArrayList(5); 130 String [] array; 131 132 while (tokenizer.hasMoreTokens()) 133 { 134 list.add(tokenizer.nextToken()); 135 } 136 137 array = new String [list.size()]; 138 array = (String []) list.toArray(array); 139 140 return array; 141 } 142 } 143 | Popular Tags |