annotate servlet/src/digilib/auth/HashTree.java @ 87:5d44cd2481a5

New version 1.8b4. Utils without static debuglevel.
author robcast
date Mon, 17 Mar 2003 15:23:17 +0100
parents 0ff3ede32060
children 398d39ee1014 9cedd170b581
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
1 /* HashTree -- Tree in a Hashtable
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
2
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
3 Digital Image Library servlet components
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
4
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
6
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify it
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
9 Free Software Foundation; either version 2 of the License, or (at your
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
10 option) any later version.
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
11
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
12 Please read license.txt for the full details. A copy of the GPL
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
13 may be found at http://www.gnu.org/copyleft/lgpl.html
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
14
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
16 along with this program; if not, write to the Free Software
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
18
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
19 */
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
20
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
21 package digilib.auth;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
22
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
23 import java.util.*;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
24
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
25 public class HashTree {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
26
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
27 private Hashtable table;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
28 private String twigSep = "/";
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
29 private String leafSep = ",";
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
30
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
31 public HashTree() {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
32 table = new Hashtable();
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
33 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
34
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
35 public HashTree(Hashtable t, String twig_separator, String leaf_separator) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
36 table = t;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
37 twigSep = twig_separator;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
38 leafSep = leaf_separator;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
39 optimizeTable();
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
40 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
41
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
42 void optimizeTable() {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
43 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
44
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
45 List match(String branch) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
46 String b = "";
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
47 String m;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
48 LinkedList matches = new LinkedList();
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
49
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
50 // split branch
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
51 StringTokenizer twig = new StringTokenizer(branch, twigSep);
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
52 // walk branch and check with tree
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
53 while (twig.hasMoreTokens()) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
54 if (b.length() == 0) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
55 b = twig.nextToken();
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
56 } else {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
57 b += twigSep + twig.nextToken();
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
58 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
59 m = (String)table.get(b);
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
60 //System.out.println("CHECK: "+b+" = "+m);
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
61 if (m != null) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
62 if (m.indexOf(leafSep) < 0) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
63 // single leaf
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
64 matches.add(m);
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
65 } else {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
66 // split leaves
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
67 StringTokenizer leaf = new StringTokenizer(m, leafSep);
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
68 while (leaf.hasMoreTokens()) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
69 matches.add(leaf.nextToken());
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
70 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
71 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
72 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
73 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
74 if (matches.size() > 0) {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
75 return matches;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
76 } else {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
77 return null;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
78 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
79 }
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
80 }