KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > config > PropertiesTree


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 JavaDoc;
9 import java.util.*;
10
11
12 /**
13  * @version $Revision: 1.5 $
14  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
15  */

16 public class PropertiesTree
17 {
18
19     private static final Log LOG = new Log(PropertiesTree.class);
20     private Node root = null;
21
22     /**
23      * Constructor PropertiesTree
24      */

25     public PropertiesTree() throws ConfigurationException
26     {
27         initialize(new Properties());
28     }
29
30
31     /**
32      * Constructor PropertiesTree
33      */

34     public PropertiesTree(Properties properties) throws ConfigurationException
35     {
36         initialize(properties);
37     }
38
39
40     /**
41      * Method initialize
42      */

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 JavaDoc key = (String JavaDoc) keys.nextElement();
53             String JavaDoc value = properties.getProperty(key);
54
55             root.createNode(key, value);
56         }
57     }
58
59
60     /**
61      * Regenerates a Properties object based on the tree.
62      */

63     public Properties getProperties()
64     {
65
66         Properties properties = new Properties();
67
68         populateProperties(properties, root, "");
69
70         return properties;
71     }
72
73
74     /**
75      * Method populateProperties
76      */

77     private void populateProperties(Properties properties, Node node, String JavaDoc 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 JavaDoc key = prefix + child.getKey();
87             String JavaDoc 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     /**
100      * Method getRoot
101      *
102      * @return .
103      */

104     public Node getRoot()
105     {
106         return root;
107     }
108
109
110     /**
111      * Prints out the tree in a cheezy XML like format
112      */

113     public void print(PrintStream JavaDoc out)
114     {
115         root.print();
116     }
117
118
119     public static String JavaDoc[] tokenize(String JavaDoc text)
120     {
121         return tokenize(text, ".");
122     }
123
124
125     public static String JavaDoc[] tokenize(String JavaDoc text, String JavaDoc token)
126     {
127
128         StringTokenizer tokenizer = new StringTokenizer(text, token, false);
129         List list = new ArrayList(5);
130         String JavaDoc[] array;
131
132         while (tokenizer.hasMoreTokens())
133         {
134             list.add(tokenizer.nextToken());
135         }
136
137         array = new String JavaDoc[list.size()];
138         array = (String JavaDoc[]) list.toArray(array);
139
140         return array;
141     }
142 }
143
Popular Tags