00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package org.openmobileis.embedded.webserver;
00031
00032 import java.util.Enumeration;
00033 import java.util.Hashtable;
00034 import java.util.StringTokenizer;
00035 import java.util.Vector;
00036
00037 public class PathTreeDictionary {
00038
00039 Node root_node;
00040
00041 PathTreeDictionary() {
00042 root_node = new Node();
00043 }
00044
00045 void put(String path, Object value) {
00046 StringTokenizer st = new StringTokenizer(path, "\\/");
00047 Node cur_node = root_node;
00048 while (st.hasMoreTokens()) {
00049 String nodename = st.nextToken();
00050 Node node = (Node) cur_node.get(nodename);
00051 if (node == null) {
00052 node = new Node();
00053 cur_node.put(nodename, node);
00054 }
00055 cur_node = node;
00056 }
00057 cur_node.object = value;
00058 }
00059
00065 Object[] get(String path) {
00066 Object[] result = new Object[2];
00067 if (path == null)
00068 return result;
00069 char[] ps = path.toCharArray();
00070 Node cur_node = root_node;
00071 int p0 = 0, lm = 0;
00072 result[0] = cur_node.object;
00073 boolean div_state = true;
00074 for (int i = 0; i < ps.length; i++) {
00075 if (ps[i] == '/' || ps[i] == '\\') {
00076 if (div_state)
00077 continue;
00078 Node node = (Node) cur_node.get(new String(ps, p0, i - p0));
00079 if (node == null) {
00080 result[1] = new Integer(lm);
00081 return result;
00082 }
00083 if (node.object != null) {
00084 result[0] = node.object;
00085 lm = i;
00086 }
00087 cur_node = node;
00088 div_state = true;
00089 } else {
00090 if (div_state) {
00091 p0 = i;
00092 div_state = false;
00093 }
00094 }
00095 }
00096 cur_node = (Node) cur_node.get(new String(ps, p0, ps.length - p0));
00097 if (cur_node != null && cur_node.object != null) {
00098 result[0] = cur_node.object;
00099 lm = ps.length;
00100 }
00101 result[1] = new Integer(lm);
00102 return result;
00103 }
00104
00105 Enumeration keys() {
00106 Vector result = new Vector();
00107 addSiblingNames(root_node, result, "");
00108 return result.elements();
00109 }
00110
00111 void addSiblingNames(Node node, Vector result, String path) {
00112 Enumeration e = node.keys();
00113 while (e.hasMoreElements()) {
00114 String pc = (String) e.nextElement();
00115 Node childNode = (Node) node.get(pc);
00116 pc = path + '/' + pc;
00117 if (childNode.object != null)
00118 result.addElement(pc);
00119 addSiblingNames(childNode, result, pc);
00120 }
00121 }
00122
00123 Enumeration elements() {
00124 Vector result = new Vector();
00125 addSiblingObjects(root_node, result);
00126 return result.elements();
00127 }
00128
00129 void addSiblingObjects(Node node, Vector result) {
00130 Enumeration e = node.keys();
00131 while (e.hasMoreElements()) {
00132 Node childNode = (Node) node.get(e.nextElement());
00133 if (childNode.object != null)
00134 result.addElement(childNode.object);
00135 addSiblingObjects(childNode, result);
00136 }
00137 }
00138
00139 class Node extends Hashtable {
00140 Object object;
00141 }
00142
00143 }