Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

PathTreeDictionary.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
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; // last match
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 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4