1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.resteasy.hypermedia.renderer.i18n;
17
18 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.ACCESS_KEY;
19 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.CONTEXT_MENU;
20 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.CSS_CLASS;
21 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.DIR;
22 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.HELP;
23 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.HIDDEN;
24 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.ID;
25 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.LABEL;
26 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.MEDIA;
27 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.SHORT_LABEL;
28 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.TAB_INDEX;
29 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.TARGET;
30 import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.TITLE;
31
32 import java.util.Locale;
33 import java.util.MissingResourceException;
34 import java.util.ResourceBundle;
35
36 import javax.enterprise.context.ApplicationScoped;
37
38 import de.smartics.resteasy.hypermedia.renderer.I18nProvider;
39 import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
40 import de.smartics.resteasy.hypermedia.renderer.LocalizationProvider;
41
42
43
44
45 @ApplicationScoped
46 public class ResourceBundleI18nProvider implements I18nProvider
47 {
48
49
50
51
52
53
54
55
56
57
58
59 public static final String SYSTEM_KEY = "de.smartics.hypermedia.link";
60
61
62
63
64
65
66
67 public static final String DEFAULT_LOCATION = "de/smartics/hypermedia/link";
68
69
70
71
72
73
74
75
76
77
78
79
80 private static final class ResourceBundleLocalizationProvider implements
81 LocalizationProvider
82 {
83
84
85
86
87
88
89
90
91
92 private final Locale locale;
93
94
95
96
97
98
99
100
101
102
103 private ResourceBundleLocalizationProvider(final Locale locale)
104 {
105 this.locale = locale;
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 @Override
122 public Locale getLocale()
123 {
124 return locale;
125 }
126
127
128
129 @Override
130 public LinkMetadata getLinkMetadata(final String id)
131 {
132 final BundleAccessor accessor = new BundleAccessor(locale, id);
133 final LinkMetadata metadata = accessor.getLinkMetadata();
134 return metadata;
135 }
136
137
138
139 }
140
141
142
143
144 private static final class BundleAccessor
145 {
146
147
148
149
150
151
152
153
154
155 private final ResourceBundle bundle;
156
157
158
159
160
161
162
163
164
165
166
167 private BundleAccessor(final Locale locale, final String id)
168 {
169 final String baseName = calcBaseName(id);
170 this.bundle = ResourceBundle.getBundle(baseName, locale);
171 }
172
173
174
175
176
177
178
179 private static String calcBaseName(final String id)
180 {
181 final String prefix = System.getProperty(SYSTEM_KEY, DEFAULT_LOCATION);
182 final String baseName = prefix + '/' + id;
183 return baseName;
184 }
185
186
187
188
189
190 private LinkMetadata getLinkMetadata()
191 {
192 final LinkMetadata metadata = new LinkMetadata();
193
194 metadata.setAccessKey(getValue(ACCESS_KEY));
195 metadata.setContextMenu(getValue(CONTEXT_MENU));
196 metadata.setCssClass(getValue(CSS_CLASS));
197 metadata.setDir(getValue(DIR));
198 metadata.setHelp(getValue(HELP));
199 metadata.setHidden(getValueAsBoolean(HIDDEN));
200 metadata.setId(getValue(ID));
201 metadata.setLabel(getValue(LABEL));
202 metadata.setMedia(getValue(MEDIA));
203 metadata.setShortLabel(getValue(SHORT_LABEL));
204 metadata.setTabIndex(getValueAsInt(TAB_INDEX));
205 metadata.setTarget(getValue(TARGET));
206 metadata.setTitle(getValue(TITLE));
207
208 return metadata;
209 }
210
211 private Integer getValueAsInt(final String key)
212 {
213 try
214 {
215 final String string = bundle.getString(key);
216 final Integer value = Integer.parseInt(string);
217 return value;
218 }
219 catch (final MissingResourceException e)
220 {
221 return null;
222 }
223 }
224
225 private Boolean getValueAsBoolean(final String key)
226 {
227 try
228 {
229 final String string = bundle.getString(key);
230 final boolean value = Boolean.parseBoolean(string);
231 return value;
232 }
233 catch (final MissingResourceException e)
234 {
235 return null;
236 }
237 }
238
239 private String getValue(final String key)
240 {
241 try
242 {
243 final String value = bundle.getString(key);
244 return value;
245 }
246 catch (final MissingResourceException e)
247 {
248 return null;
249 }
250 }
251
252
253 }
254
255
256
257
258
259
260
261 @Override
262 public LocalizationProvider getProvider(final Locale locale)
263 throws NullPointerException
264 {
265 final ResourceBundleLocalizationProvider provider =
266 new ResourceBundleLocalizationProvider(locale);
267 return provider;
268 }
269
270
271
272
273
274 }