1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.report.resource;
17
18 import java.net.URL;
19 import java.util.Locale;
20 import java.util.MissingResourceException;
21 import java.util.ResourceBundle;
22
23 import org.apache.commons.lang.NullArgumentException;
24 import org.apache.commons.lang.StringUtils;
25
26
27
28
29
30
31
32 public abstract class AbstractClassPathCategoryResourceProvider implements
33 CategoryResourceProvider
34 {
35
36
37
38
39
40
41
42
43
44 private final String resourceLocationName;
45
46
47
48
49 private final String baseBundleName;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 protected AbstractClassPathCategoryResourceProvider(
66 final String resourcePackageName)
67 {
68 this.resourceLocationName = resourcePackageName.replace('.', '/');
69 this.baseBundleName = resourcePackageName + '.' + "CategoryResourceBundle";
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 @Override
88 public CategoryResource getResource(final Locale locale,
89 final Class<?> categoryType) throws NullArgumentException,
90 IconNotFoundException
91 {
92 checkArguments(locale, categoryType);
93
94 final ResourceBundle bundle =
95 ResourceBundle.getBundle(baseBundleName, locale);
96 final String key = categoryType.getName();
97 try
98 {
99 final String label = bundle.getString(key);
100 final URL iconLocation = calcIconLocation(bundle, key);
101 return new CategoryResource(label, iconLocation);
102 }
103 catch (final MissingResourceException e)
104 {
105 return null;
106 }
107 }
108
109 private static void checkArguments(final Locale locale,
110 final Class<?> categoryType)
111 {
112 if (locale == null)
113 {
114 throw new NullArgumentException("locale");
115 }
116 if (categoryType == null)
117 {
118 throw new NullArgumentException("categoryType");
119 }
120 }
121
122
123
124 private URL calcIconLocation(final ResourceBundle bundle, final String key)
125 throws IconNotFoundException
126 {
127 try
128 {
129 final String resourceId = bundle.getString(key + ".icon");
130 if (StringUtils.isNotBlank(resourceId))
131 {
132 final String resourceLocation = resourceLocationName + '/' + resourceId;
133 final ClassLoader loader =
134 Thread.currentThread().getContextClassLoader();
135 final URL url = loader.getResource(resourceLocation);
136 if(url == null)
137 {
138 throw new IconNotFoundException(resourceId);
139 }
140 return url;
141 }
142 }
143 catch (final MissingResourceException e)
144 {
145
146 }
147 return null;
148 }
149 }