01: package org.conform.modifier;
02:
03: import org.conform.*;
04:
05: import java.util.*;
06:
07: public class ResourceBundleModifier implements Modifier {
08: private final static String KEY_MICRO_HELP_POSTFIX = ".help";
09: private ResourceBundle labelBundle;
10:
11: public ResourceBundleModifier(ResourceBundle labelBundle) {
12: this .labelBundle = labelBundle;
13: }
14:
15: public void modify(BeanMeta beanMeta) throws ModifierException {
16: beanMeta.setLabel(getLabel(beanMeta.getName()));
17: beanMeta.setMicroHelp(getMicroHelp(beanMeta.getName()));
18:
19: for (int i = 0; i < beanMeta.getProperties().length; i++) {
20: PropertyMeta propertyMeta = beanMeta.getProperties()[i];
21: localize(beanMeta.getName(), propertyMeta);
22: }
23: }
24:
25: private String getLabel(String key) {
26: try {
27: return labelBundle.getString(key);
28: } catch (MissingResourceException mre) {
29: return key;
30: }
31: }
32:
33: private String getMicroHelp(String key) {
34: try {
35: return labelBundle.getString(key + KEY_MICRO_HELP_POSTFIX);
36: } catch (MissingResourceException mre) {
37: return null;
38: }
39: }
40:
41: private void localize(String className, PropertyMeta propertyMeta) {
42: String key = buildKey(className, propertyMeta.getName());
43: propertyMeta.setLabel(getLabel(key));
44: propertyMeta.setMicroHelp(getMicroHelp(key));
45: }
46:
47: private String buildKey(String className, String propertyName) {
48: return new StringBuffer().append(className).append(".").append(
49: propertyName).toString();
50: }
51: }
|