001: package org.shiftone.cache.config;
002:
003: import org.shiftone.cache.ConfigurationException;
004: import org.shiftone.cache.util.Log;
005:
006: import java.io.PrintStream;
007: import java.util.Collection;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.Map;
011:
012: /**
013: * @version $Revision: 1.5 $
014: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
015: */
016: public class Node {
017:
018: private static final Log LOG = new Log(Node.class);
019: private Map children = new HashMap();
020: private String key = null;
021: private String value = null;
022:
023: /**
024: * Constructor StackNode
025: */
026: public Node(String key, String value) {
027: this .value = value;
028: this .key = key;
029: }
030:
031: /**
032: * Method getKey
033: */
034: public String getKey() {
035: return key;
036: }
037:
038: /**
039: * Method getValue
040: */
041: public String getValue() {
042: return value;
043: }
044:
045: /**
046: * Method getChildren
047: */
048: public Collection getChildren() {
049: return children.values();
050: }
051:
052: public boolean hasNode(String key) {
053: return getNode(key) != null;
054: }
055:
056: public Node getRequiredNode(String key)
057: throws ConfigurationException {
058:
059: Node node = getNode(key);
060:
061: if (node == null) {
062: throw new ConfigurationException("node not found : " + key);
063: }
064:
065: return node;
066: }
067:
068: public Node getNode(String key) {
069: return getNodeInternal(PropertiesTree.tokenize(key), 0);
070: }
071:
072: /**
073: * Method getNode
074: */
075: private Node getNodeInternal(String[] keyParts, int partIndex) {
076:
077: if (keyParts.length == partIndex) {
078: return this ;
079: } else {
080: String part = keyParts[partIndex];
081: Node nextChild = (Node) children.get(part);
082:
083: if (nextChild == null) {
084: LOG.debug("node not found : "
085: + fullKey(keyParts, partIndex));
086:
087: return null;
088: }
089:
090: return nextChild.getNodeInternal(keyParts, partIndex + 1);
091: }
092: }
093:
094: /**
095: * Method fullKey
096: */
097: private String fullKey(String[] keyParts, int partIndex) {
098:
099: String fullKey = keyParts[0];
100:
101: for (int i = 1; i < partIndex; i++) {
102: fullKey += ("." + keyParts[i]);
103: }
104:
105: return fullKey;
106: }
107:
108: /**
109: * Method createNode
110: */
111: public void createNode(String key, String value) {
112: createNode(PropertiesTree.tokenize(key, "."), 0, value);
113: }
114:
115: /**
116: * Method createNode
117: */
118: private void createNode(String[] keyParts, int partIndex,
119: String value) {
120:
121: if (keyParts.length == partIndex) {
122: this .value = value;
123: } else {
124: Node nextChild = null;
125: String part = keyParts[partIndex];
126:
127: if (children.containsKey(part)) {
128: nextChild = (Node) children.get(part);
129: } else {
130: nextChild = new Node(part, null);
131:
132: children.put(part, nextChild);
133: }
134:
135: nextChild.createNode(keyParts, partIndex + 1, value);
136: }
137: }
138:
139: public void print() {
140: print(System.out, 0);
141: }
142:
143: /**
144: * Method print
145: */
146: private void print(PrintStream out, int indentLevel) {
147:
148: Collection children = getChildren();
149:
150: if (children.size() == 0) {
151: out.println(bufferString(indentLevel, '\t') + "<"
152: + getKey() + " value=\"" + getValue() + "\"/>");
153: } else {
154: out.println(bufferString(indentLevel, '\t') + "<"
155: + getKey() + " value=\"" + getValue() + "\">");
156:
157: Iterator i = children.iterator();
158:
159: while (i.hasNext()) {
160: ((Node) i.next()).print(out, indentLevel + 1);
161: }
162:
163: out.println(bufferString(indentLevel, '\t') + "</"
164: + getKey() + ">");
165: }
166: }
167:
168: private static String bufferString(int indentLevel, char c) {
169:
170: StringBuffer sb = new StringBuffer(indentLevel);
171:
172: for (int i = 0; i < indentLevel; i++) {
173: sb.append(c);
174: }
175:
176: return sb.toString();
177: }
178: }
|