1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.resource.names;
18
19 import java.util.Dictionary;
20 import java.util.Hashtable;
21
22 import org.apache.commons.discovery.ResourceNameDiscover;
23 import org.apache.commons.discovery.ResourceNameIterator;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28
29
30
31
32
33
34 public class DiscoverNamesInDictionary extends ResourceNameDiscoverImpl implements ResourceNameDiscover {
35
36 private static Log log = LogFactory.getLog(DiscoverNamesInDictionary.class);
37
38
39
40
41
42
43
44 @Deprecated
45 public static void setLog(Log _log) {
46 log = _log;
47 }
48
49 private Dictionary<String, String[]> dictionary;
50
51
52
53
54 public DiscoverNamesInDictionary() {
55 setDictionary(new Hashtable<String, String[]>());
56 }
57
58
59
60
61
62
63 public DiscoverNamesInDictionary(Dictionary<String, String[]> dictionary) {
64 setDictionary(dictionary);
65 }
66
67
68
69
70
71
72 protected Dictionary<String, String[]> getDictionary() {
73 return dictionary;
74 }
75
76
77
78
79
80
81 public void setDictionary(Dictionary<String, String[]> table) {
82 this.dictionary = table;
83 }
84
85
86
87
88
89
90
91 public void addResource(String resourceName, String resource) {
92 addResource(resourceName, new String[]{ resource });
93 }
94
95
96
97
98
99
100
101 public void addResource(String resourceName, String[] resources) {
102 dictionary.put(resourceName, resources);
103 }
104
105
106
107
108 @Override
109 public ResourceNameIterator findResourceNames(final String resourceName) {
110 if (log.isDebugEnabled()) {
111 log.debug("find: resourceName='" + resourceName + "'");
112 }
113
114 final String[] resources = dictionary.get(resourceName);
115
116 return new ResourceNameIterator() {
117 private int idx = 0;
118
119 public boolean hasNext() {
120 if (resources != null) {
121 while (idx < resources.length && resources[idx] == null) {
122 idx++;
123 }
124 return idx < resources.length;
125 }
126 return false;
127 }
128
129 public String nextResourceName() {
130 return hasNext() ? resources[idx++] : null;
131 }
132 };
133 }
134
135 }