1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.handlers;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27
28 import net.fckeditor.tool.Utils;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class ResourceType {
46
47
48 private String name;
49
50 private String path;
51
52 private Set<String> allowedEextensions;
53
54 private Set<String> deniedExtensions;
55
56
57 private static Map<String, ResourceType> types = new HashMap<String, ResourceType>(
58 4);
59
60
61 public static final ResourceType FILE = new ResourceType("File",
62 PropertiesLoader.getFileResourceTypePath(), Utils
63 .getSet(PropertiesLoader
64 .getFileResourceTypeAllowedExtensions()), Utils
65 .getSet(PropertiesLoader
66 .getFileResourceTypeDeniedExtensions()));
67
68 public static final ResourceType FLASH = new ResourceType("Flash",
69 PropertiesLoader.getFlashResourceTypePath(), Utils
70 .getSet(PropertiesLoader
71 .getFlashResourceTypeAllowedExtensions()), Utils
72 .getSet(PropertiesLoader
73 .getFlashResourceTypeDeniedExtensions()));
74
75 public static final ResourceType IMAGE = new ResourceType("Image",
76 PropertiesLoader.getImageResourceTypePath(), Utils
77 .getSet(PropertiesLoader
78 .getImageResourceTypeAllowedExtensions()), Utils
79 .getSet(PropertiesLoader
80 .getImageResourceTypeDeniedExtensions()));
81
82 public static final ResourceType MEDIA = new ResourceType("Media",
83 PropertiesLoader.getMediaResourceTypePath(), Utils
84 .getSet(PropertiesLoader
85 .getMediaResourceTypeAllowedExtensions()), Utils
86 .getSet(PropertiesLoader
87 .getMediaResourceTypeDeniedExtensions()));
88
89 static {
90 types.put(FILE.getName(), FILE);
91 types.put(FLASH.getName(), FLASH);
92 types.put(IMAGE.getName(), IMAGE);
93 types.put(MEDIA.getName(), MEDIA);
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 private ResourceType(final String name, final String path,
114 final Set<String> allowedEextensions,
115 final Set<String> deniedExtensions) {
116 this.name = name;
117 this.path = path;
118
119 if (allowedEextensions.isEmpty() && deniedExtensions.isEmpty())
120 throw new IllegalArgumentException(
121 "Both sets are empty, one has always to be filled");
122
123 if (!allowedEextensions.isEmpty() && !deniedExtensions.isEmpty())
124 throw new IllegalArgumentException(
125 "Both sets contain extensions, only one can be filled at the same time");
126
127 this.allowedEextensions = allowedEextensions;
128 this.deniedExtensions = deniedExtensions;
129 }
130
131
132
133
134
135
136 public String getName() {
137 return name;
138 }
139
140
141
142
143
144
145
146
147 public String getPath() {
148 return path;
149 }
150
151
152
153
154
155
156 public Set<String> getAllowedEextensions() {
157 return Collections.unmodifiableSet(allowedEextensions);
158 }
159
160
161
162
163
164
165 public Set<String> getDeniedExtensions() {
166 return Collections.unmodifiableSet(deniedExtensions);
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180 public static ResourceType valueOf(final String name) {
181 if (Utils.isEmpty(name))
182 throw new NullPointerException("Name is null or empty");
183
184 ResourceType rt = types.get(name);
185 if (rt == null)
186 throw new IllegalArgumentException("No resource type const " + name);
187 return rt;
188 }
189
190
191
192
193
194
195
196
197
198
199 public static boolean isValidType(final String name) {
200
201 return types.containsKey(name);
202 }
203
204
205
206
207
208
209
210
211
212
213
214 public static ResourceType getResourceType(final String name) {
215 try {
216 return ResourceType.valueOf(name);
217 } catch (Exception e) {
218 return null;
219 }
220 }
221
222
223
224
225
226
227
228
229
230
231
232 public static ResourceType getDefaultResourceType(final String name) {
233 ResourceType rt = getResourceType(name);
234 return rt == null ? FILE : rt;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248 public boolean isAllowedExtension(final String extension) {
249 if (Utils.isEmpty(extension))
250 return false;
251 String ext = extension.toLowerCase();
252 if (allowedEextensions.isEmpty())
253 return !deniedExtensions.contains(ext);
254 if (deniedExtensions.isEmpty())
255 return allowedEextensions.contains(ext);
256 return false;
257 }
258
259
260
261
262
263
264
265
266
267 public boolean isDeniedExtension(final String extension) {
268 return !isAllowedExtension(extension);
269 }
270
271
272
273
274
275
276
277
278
279
280 @Override
281 public boolean equals(Object obj) {
282 if (this == obj)
283 return true;
284
285 if (obj == null || this.getClass() != obj.getClass())
286 return false;
287
288 final ResourceType rt = (ResourceType) obj;
289 return name.equals(rt.getName());
290 }
291
292
293
294
295
296
297
298 @Override
299 public int hashCode() {
300 return name.hashCode();
301 }
302 }