KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 public class Node
20 {
21
22     private static final Log LOG = new Log(Node.class);
23     private Map JavaDoc children = new HashMap JavaDoc();
24     private String JavaDoc key = null;
25     private String JavaDoc value = null;
26
27     /**
28      * Constructor StackNode
29      */

30     public Node(String JavaDoc key, String JavaDoc value)
31     {
32         this.value = value;
33         this.key = key;
34     }
35
36
37     /**
38      * Method getKey
39      */

40     public String JavaDoc getKey()
41     {
42         return key;
43     }
44
45
46     /**
47      * Method getValue
48      */

49     public String JavaDoc getValue()
50     {
51         return value;
52     }
53
54
55     /**
56      * Method getChildren
57      */

58     public Collection JavaDoc getChildren()
59     {
60         return children.values();
61     }
62
63
64     public boolean hasNode(String JavaDoc key)
65     {
66         return getNode(key) != null;
67     }
68
69
70     public Node getRequiredNode(String JavaDoc 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 JavaDoc key)
85     {
86         return getNodeInternal(PropertiesTree.tokenize(key), 0);
87     }
88
89
90     /**
91      * Method getNode
92      */

93     private Node getNodeInternal(String JavaDoc[] keyParts, int partIndex)
94     {
95
96         if (keyParts.length == partIndex)
97         {
98             return this;
99         }
100         else
101         {
102             String JavaDoc 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     /**
118      * Method fullKey
119      */

120     private String JavaDoc fullKey(String JavaDoc[] keyParts, int partIndex)
121     {
122
123         String JavaDoc 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     /**
135      * Method createNode
136      */

137     public void createNode(String JavaDoc key, String JavaDoc value)
138     {
139         createNode(PropertiesTree.tokenize(key, "."), 0, value);
140     }
141
142
143     /**
144      * Method createNode
145      */

146     private void createNode(String JavaDoc[] keyParts, int partIndex, String JavaDoc value)
147     {
148
149         if (keyParts.length == partIndex)
150         {
151             this.value = value;
152         }
153         else
154         {
155             Node nextChild = null;
156             String JavaDoc 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     /**
181         * Method print
182         */

183     private void print(PrintStream JavaDoc out, int indentLevel)
184     {
185
186         Collection JavaDoc 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 JavaDoc 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 JavaDoc bufferString(int indentLevel, char c)
209     {
210
211         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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