1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.schema.rules; |
16 |
| |
17 |
| import java.lang.reflect.Field; |
18 |
| import java.util.Map; |
19 |
| |
20 |
| import org.apache.hivemind.ApplicationRuntimeException; |
21 |
| import org.apache.hivemind.HiveMind; |
22 |
| import org.apache.hivemind.Location; |
23 |
| import org.apache.hivemind.internal.Module; |
24 |
| import org.apache.hivemind.schema.Translator; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class EnumerationTranslator implements Translator |
35 |
| { |
36 |
| private Map _mappings; |
37 |
| private String _className; |
38 |
| private Class _class; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
5
| public EnumerationTranslator(String initializer)
|
46 |
| { |
47 |
5
| int commax = initializer.indexOf(',');
|
48 |
| |
49 |
5
| _className = initializer.substring(0, commax);
|
50 |
| |
51 |
5
| _mappings = RuleUtils.convertInitializer(initializer.substring(commax + 1));
|
52 |
| } |
53 |
| |
54 |
5
| private synchronized Class getClass(Module contributingModule)
|
55 |
| { |
56 |
5
| if (_class == null)
|
57 |
4
| _class = contributingModule.resolveType(_className);
|
58 |
| |
59 |
4
| return _class;
|
60 |
| } |
61 |
| |
62 |
6
| public Object translate(
|
63 |
| Module contributingModule, |
64 |
| Class propertyType, |
65 |
| String inputValue, |
66 |
| Location location) |
67 |
| { |
68 |
6
| if (HiveMind.isBlank(inputValue))
|
69 |
1
| return null;
|
70 |
| |
71 |
5
| Class c = getClass(contributingModule);
|
72 |
| |
73 |
4
| String fieldName = (String) _mappings.get(inputValue);
|
74 |
| |
75 |
4
| if (fieldName == null)
|
76 |
1
| throw new ApplicationRuntimeException(
|
77 |
| RulesMessages.enumNotRecognized(inputValue), |
78 |
| location, |
79 |
| null); |
80 |
| |
81 |
3
| try
|
82 |
| { |
83 |
3
| Field f = c.getField(fieldName);
|
84 |
| |
85 |
2
| return f.get(null);
|
86 |
| } |
87 |
| catch (Exception ex) |
88 |
| { |
89 |
1
| throw new ApplicationRuntimeException(
|
90 |
| RulesMessages.enumError(c, fieldName, ex), |
91 |
| location, |
92 |
| ex); |
93 |
| } |
94 |
| } |
95 |
| |
96 |
| } |