1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.rapiddweller.benerator.engine.parser.xml;
28
29 import com.rapiddweller.benerator.engine.expression.ScriptExpression;
30 import com.rapiddweller.benerator.engine.expression.ScriptableExpression;
31 import com.rapiddweller.benerator.engine.expression.TypedScriptExpression;
32 import com.rapiddweller.common.StringUtil;
33 import com.rapiddweller.common.xml.XMLUtil;
34 import com.rapiddweller.format.text.SplitStringConverter;
35 import com.rapiddweller.script.Expression;
36 import com.rapiddweller.script.expression.ConstantExpression;
37 import com.rapiddweller.script.expression.ConvertingExpression;
38 import com.rapiddweller.script.expression.StringExpression;
39 import com.rapiddweller.script.expression.TypeConvertingExpression;
40 import com.rapiddweller.script.expression.UnescapeExpression;
41 import org.w3c.dom.Element;
42
43
44
45
46
47
48
49
50 public class DescriptorParserUtil {
51
52
53
54
55
56
57
58
59
60
61 public static String getAttribute(String name, Element element) {
62 return (element.hasAttribute(name) ? element.getAttribute(name) : null);
63 }
64
65
66
67
68
69
70
71 public static String getElementText(Element element) {
72 return XMLUtil.getText(element);
73 }
74
75
76
77
78
79
80
81
82
83
84 public static Expression<String> parseScriptableElementText(Element element, boolean unescape) {
85 Expression<String> result = new StringExpression(new ScriptableExpression(XMLUtil.getText(element), null));
86 if (unescape) {
87 result = new UnescapeExpression(result);
88 }
89 return result;
90 }
91
92
93
94
95
96
97
98
99 public static Expression<String> parseScriptableStringAttribute(String name, Element element) {
100 return parseScriptableStringAttribute(name, element, true);
101 }
102
103
104
105
106
107
108
109
110
111 public static Expression<String> parseScriptableStringAttribute(String name, Element element, boolean unescape) {
112 String attribute = getAttribute(name, element);
113 if (attribute == null) {
114 return null;
115 }
116 Expression<String> result = new StringExpression(new ScriptableExpression(attribute, null));
117 if (unescape) {
118 result = new UnescapeExpression(result);
119 }
120 return result;
121 }
122
123
124
125
126
127
128
129
130 public static Expression<String[]> parseScriptableStringArrayAttribute(String name, Element element) {
131 String attribute = getAttribute(name, element);
132 if (attribute == null) {
133 return null;
134 }
135 Expression<String> rawEx = new TypeConvertingExpression<>(
136 new ScriptableExpression(attribute, null), String.class);
137 return new ConvertingExpression<>(rawEx, new SplitStringConverter(','));
138 }
139
140
141
142
143
144
145
146
147 public static Expression<Integer> parseIntAttribute(String name, Element element) {
148 return new TypedScriptExpression<>(getAttribute(name, element), Integer.class);
149 }
150
151
152
153
154
155
156
157
158
159 public static Expression<Integer> parseIntAttribute(String name, Element element, int defaultValue) {
160 return parseIntAttribute(name, element, new ConstantExpression<>(defaultValue));
161 }
162
163
164
165
166
167
168
169
170
171 public static Expression<Integer> parseIntAttribute(String name, Element element, Expression<Integer> defaultValue) {
172 String attribute = getAttribute(name, element);
173 if (StringUtil.isEmpty(attribute)) {
174 return defaultValue;
175 } else {
176 return new TypedScriptExpression<>(attribute, Integer.class);
177 }
178 }
179
180
181
182
183
184
185
186
187
188 public static Expression<Long> parseLongAttribute(String name, Element element, long defaultValue) {
189 return parseLongAttribute(name, element, new ConstantExpression<>(defaultValue));
190 }
191
192
193
194
195
196
197
198
199
200 public static Expression<Long> parseLongAttribute(String name, Element element, Expression<Long> defaultValue) {
201 String attribute = getAttribute(name, element);
202 if (StringUtil.isEmpty(attribute)) {
203 return defaultValue;
204 } else {
205 return new TypedScriptExpression<>(attribute, Long.class);
206 }
207 }
208
209
210
211
212
213
214
215
216 public static Expression<Boolean> parseBooleanExpressionAttribute(String name, Element element) {
217 return parseBooleanExpressionAttribute(name, element, null);
218 }
219
220
221
222
223
224
225
226
227
228 public static Expression<Boolean> parseBooleanExpressionAttribute(String name, Element element, Boolean defaultValue) {
229 String attribute = getAttribute(name, element);
230 if (StringUtil.isEmpty(attribute)) {
231 return new ConstantExpression<>(defaultValue);
232 } else {
233 return new TypedScriptExpression<>(attribute, Boolean.class);
234 }
235 }
236
237
238
239
240
241
242
243
244 public static ConstantExpression<String> parseAttribute(String name, Element element) {
245 String attribute = getAttribute(name, element);
246 return (attribute != null ? new ConstantExpression<>(attribute) : null);
247 }
248
249
250
251
252
253
254
255
256 public static Expression<?> parseScriptAttribute(String name, Element element) {
257 String rawAttribute = getAttribute(name, element);
258 if (StringUtil.isEmpty(rawAttribute)) {
259 return null;
260 } else {
261 return new ScriptExpression<>(rawAttribute);
262 }
263 }
264
265 }