|
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.beans.PropertyEditor; |
|
18 |
| import java.beans.PropertyEditorManager; |
|
19 |
| import java.util.Map; |
|
20 |
| |
|
21 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
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 |
| public class SmartTranslator implements Translator |
|
33 |
| { |
|
34 |
| private String _default; |
|
35 |
| |
|
36 |
154
| public SmartTranslator()
|
|
37 |
| { |
|
38 |
| } |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
1
| public SmartTranslator(String initializer)
|
|
47 |
| { |
|
48 |
1
| Map m = RuleUtils.convertInitializer(initializer);
|
|
49 |
| |
|
50 |
1
| _default = (String) m.get("default");
|
|
51 |
| } |
|
52 |
| |
|
53 |
5545
| public Object translate(Module contributingModule, Class propertyType, String inputValue,
|
|
54 |
| Location location) |
|
55 |
| { |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
5545
| if (inputValue == null)
|
|
60 |
| { |
|
61 |
3
| if (_default == null)
|
|
62 |
2
| return null;
|
|
63 |
| |
|
64 |
1
| inputValue = _default;
|
|
65 |
| } |
|
66 |
| |
|
67 |
5543
| if (propertyType.equals(String.class) || propertyType.equals(Object.class))
|
|
68 |
5531
| return inputValue;
|
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
12
| try
|
|
73 |
| { |
|
74 |
12
| PropertyEditor e = PropertyEditorManager.findEditor(propertyType);
|
|
75 |
| |
|
76 |
12
| if (e == null)
|
|
77 |
1
| throw new ApplicationRuntimeException(RulesMessages.noPropertyEditor(propertyType),
|
|
78 |
| location, null); |
|
79 |
| |
|
80 |
11
| e.setAsText(inputValue);
|
|
81 |
| |
|
82 |
11
| return e.getValue();
|
|
83 |
| } |
|
84 |
| catch (Exception ex) |
|
85 |
| { |
|
86 |
1
| throw new ApplicationRuntimeException(RulesMessages.smartTranslatorError(
|
|
87 |
| inputValue, |
|
88 |
| propertyType, |
|
89 |
| ex), location, ex); |
|
90 |
| |
|
91 |
| } |
|
92 |
| } |
|
93 |
| |
|
94 |
| } |