1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.search;
21
22 import java.util.*;
23 import java.util.Iterator;
24 import java.util.StringTokenizer;
25
26 import javax.swing.Icon;
27
28 /***
29 * This class is the default implementation for search results.
30 */
31 public abstract class AbstractSearchResult implements SearchResult {
32
33
34
35
36
37 private TreeMap valueByKey = new TreeMap();
38 private String separatorChars;
39
40
41
42 /***
43 * Constructs the search result object.
44 *
45 * @param separatorCharss the path separator chars
46 */
47 public AbstractSearchResult(String separatorChars)
48 {
49 this.separatorChars = separatorChars;
50 }
51
52 public AbstractSearchResult()
53 {
54 this("");
55 }
56
57
58
59 /***
60 * Returns a value. Uses a {@link Hashtable Hashtable}
61 * to retreive the value.
62 *
63 * @param key the key
64 */
65 public Object get(String key)
66 {
67 return valueByKey.get(key);
68 }
69
70 /***
71 * Returns null.
72 */
73 public Icon getIcon()
74 {
75 return null;
76 }
77
78 /***
79 * Returns the path. Uses a
80 * {@link StringTokenizer StringTokenizer} to split
81 * {@link #getFilename()} by
82 * <code>separatorCharss</code>.
83 */
84 public String[] getPath()
85 {
86 StringTokenizer t = new StringTokenizer(getFilename(), separatorChars);
87 if (t.countTokens() > 1) {
88 String[] path = new String[t.countTokens() - 1];
89 for (int i = 0; i < path.length; i++) {
90 path[i] = t.nextToken();
91 }
92
93 return path;
94 }
95 else {
96 return null;
97 }
98 }
99
100 /***
101 * Splits {@link #getFilename()} by the last
102 * occurence of <code>separatorChars</code> and returns the remainder.
103 */
104 public String getShortFilename()
105 {
106 if (separatorChars.length() > 0) {
107 String s = getFilename();
108
109 int index = -1;
110 for (int i = 0; i < separatorChars.length(); i++) {
111 index = Math.max
112 (index, s.lastIndexOf(separatorChars.charAt(i)));
113 }
114 return s.substring(index + 1);
115 }
116 else {
117 return getFilename();
118 }
119 }
120
121 /***
122 * Returns 1.
123 */
124 public int getSourcesCount()
125 {
126 return 1;
127 }
128
129 public Iterator keys()
130 {
131 return valueByKey.keySet().iterator();
132 }
133
134 /***
135 * Sets the value of <code>key</code> to <code>value</code>.
136 */
137 public void put(String key, Object value)
138 {
139 if (key != null && value != null) {
140 valueByKey.put(key, value);
141 }
142 }
143
144 }