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.util.Collections; |
18 |
| import java.util.HashMap; |
19 |
| import java.util.Map; |
20 |
| |
21 |
| import org.apache.commons.logging.Log; |
22 |
| import org.apache.commons.logging.LogFactory; |
23 |
| import org.apache.hivemind.ApplicationRuntimeException; |
24 |
| import org.apache.hivemind.Element; |
25 |
| import org.apache.hivemind.ErrorHandler; |
26 |
| import org.apache.hivemind.HiveMind; |
27 |
| import org.apache.hivemind.internal.Module; |
28 |
| import org.apache.hivemind.schema.SchemaProcessor; |
29 |
| import org.apache.hivemind.schema.Translator; |
30 |
| import org.apache.hivemind.util.PropertyUtils; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class RuleUtils |
39 |
| { |
40 |
| private static final Log LOG = LogFactory.getLog(RuleUtils.class); |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
147
| public static Map convertInitializer(String initializer)
|
48 |
| { |
49 |
147
| if (HiveMind.isBlank(initializer))
|
50 |
2
| return Collections.EMPTY_MAP;
|
51 |
| |
52 |
145
| Map result = new HashMap();
|
53 |
| |
54 |
145
| int lastCommax = -1;
|
55 |
145
| int inputLength = initializer.length();
|
56 |
| |
57 |
145
| while (lastCommax < inputLength)
|
58 |
| { |
59 |
160
| int nextCommax = initializer.indexOf(',', lastCommax + 1);
|
60 |
| |
61 |
160
| if (nextCommax < 0)
|
62 |
145
| nextCommax = inputLength;
|
63 |
| |
64 |
160
| String term = initializer.substring(lastCommax + 1, nextCommax);
|
65 |
| |
66 |
160
| int equalsx = term.indexOf('=');
|
67 |
| |
68 |
160
| if (equalsx <= 0)
|
69 |
1
| throw new ApplicationRuntimeException(RulesMessages.invalidInitializer(initializer));
|
70 |
| |
71 |
159
| String key = term.substring(0, equalsx);
|
72 |
159
| String value = term.substring(equalsx + 1);
|
73 |
| |
74 |
159
| result.put(key, value);
|
75 |
| |
76 |
159
| lastCommax = nextCommax;
|
77 |
| } |
78 |
| |
79 |
144
| return result;
|
80 |
| } |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
7023
| public static String processText(SchemaProcessor processor, Element element, String inputValue)
|
97 |
| { |
98 |
7023
| if (inputValue == null)
|
99 |
480
| return null;
|
100 |
| |
101 |
6543
| Module contributingModule = processor.getContributingModule();
|
102 |
| |
103 |
6543
| if (inputValue.startsWith("%"))
|
104 |
| { |
105 |
1
| String key = inputValue.substring(1);
|
106 |
| |
107 |
1
| return contributingModule.getMessages().getMessage(key);
|
108 |
| } |
109 |
| |
110 |
6542
| return contributingModule.expandSymbols(inputValue, element.getLocation());
|
111 |
| } |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
5
| public static void setProperty(SchemaProcessor processor, Element element, String propertyName,
|
117 |
| Object target, Object value) |
118 |
| { |
119 |
5
| try
|
120 |
| { |
121 |
5
| PropertyUtils.write(target, propertyName, value);
|
122 |
| } |
123 |
| catch (Exception ex) |
124 |
| { |
125 |
| |
126 |
| |
127 |
| |
128 |
0
| ErrorHandler errorHandler = processor.getContributingModule().getErrorHandler();
|
129 |
0
| errorHandler.error(LOG, RulesMessages.unableToSetElementProperty(
|
130 |
| propertyName, |
131 |
| target, |
132 |
| processor, |
133 |
| element, |
134 |
| ex), element.getLocation(), ex); |
135 |
| } |
136 |
| } |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
158
| public static Translator getTranslator(SchemaProcessor processor, String translator)
|
149 |
| { |
150 |
158
| if (translator == null)
|
151 |
0
| return new NullTranslator();
|
152 |
| |
153 |
158
| return processor.getContributingModule().getTranslator(translator);
|
154 |
| } |
155 |
| } |