1
|
1 /* HashTree -- Tree in a Hashtable
|
|
2
|
205
|
3 Digital Image Library servlet components
|
1
|
4
|
205
|
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
|
1
|
6
|
205
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
1
|
14
|
205
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with this program; if not, write to the Free Software
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1
|
18
|
205
|
19 */
|
1
|
20
|
|
21 package digilib.auth;
|
|
22
|
|
23 import java.util.*;
|
|
24
|
205
|
25 /**
|
|
26 * Tree representation wrapper for a HashMap.
|
|
27 *
|
|
28 * The HashTree is constructed from a HashMap filled with 'branches' with
|
|
29 * 'leaves'. The branches are stored as String keys in the HashMap. The String
|
|
30 * values are leaves.
|
|
31 *
|
|
32 * Branches are matched in 'twigs' separated by 'twig separator' Strings. The
|
|
33 * return values for a match are leaf values separated by 'leaf separator'
|
|
34 * Strings.
|
|
35 *
|
|
36 * @author casties
|
|
37 */
|
1
|
38 public class HashTree {
|
|
39
|
269
|
40 private Map table;
|
1
|
41
|
205
|
42 private String twigSep = "/";
|
|
43
|
|
44 private String leafSep = ",";
|
1
|
45
|
205
|
46 /**
|
|
47 * Constructor of a HashTree.
|
|
48 *
|
|
49 * Creates a HashTree wrapper around a given HashMap, using the given twig
|
|
50 * separator and leaf separator.
|
|
51 *
|
|
52 * @param t
|
|
53 * @param twig_separator
|
|
54 * @param leaf_separator
|
|
55 */
|
269
|
56 public HashTree(Map t, String twig_separator, String leaf_separator) {
|
205
|
57 table = t;
|
|
58 twigSep = twig_separator;
|
|
59 leafSep = leaf_separator;
|
|
60 optimizeTable();
|
|
61 }
|
1
|
62
|
205
|
63 void optimizeTable() {
|
|
64 }
|
1
|
65
|
205
|
66 /**
|
|
67 * Matches the given branch against the HashTree.
|
|
68 *
|
|
69 * Returns a LinkedList of all leaves on all matching branches in the tree.
|
|
70 * Branches in the tree match if they are substrings starting at the same
|
|
71 * root.
|
|
72 *
|
|
73 * @param branch
|
|
74 * @return
|
|
75 */
|
|
76 List match(String branch) {
|
|
77 String b = "";
|
|
78 String m;
|
|
79 LinkedList matches = new LinkedList();
|
|
80
|
|
81 // split branch
|
|
82 StringTokenizer twig = new StringTokenizer(branch, twigSep);
|
|
83 // walk branch and check with tree
|
|
84 while (twig.hasMoreTokens()) {
|
|
85 if (b.length() == 0) {
|
|
86 b = twig.nextToken();
|
|
87 } else {
|
|
88 b += twigSep + twig.nextToken();
|
|
89 }
|
|
90 m = (String) table.get(b);
|
|
91 if (m != null) {
|
|
92 if (m.indexOf(leafSep) < 0) {
|
|
93 // single leaf
|
|
94 matches.add(m);
|
|
95 } else {
|
|
96 // split leaves
|
|
97 StringTokenizer leaf = new StringTokenizer(m, leafSep);
|
|
98 while (leaf.hasMoreTokens()) {
|
|
99 matches.add(leaf.nextToken());
|
|
100 }
|
|
101 }
|
|
102 }
|
|
103 }
|
|
104 if (matches.size() > 0) {
|
|
105 return matches;
|
1
|
106 } else {
|
205
|
107 return null;
|
1
|
108 }
|
|
109 }
|
|
110 }
|