annotate common/src/main/java/digilib/util/HashTree.java @ 903:7779b37d1d05

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