svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / spi / AbstractFunction.java @ 44533
History | View | Annotate | Download (15.5 KB)
1 |
package org.gvsig.expressionevaluator.spi; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.InputStream; |
5 |
import java.net.URI; |
6 |
import java.net.URISyntaxException; |
7 |
import java.net.URL; |
8 |
import java.time.LocalDateTime; |
9 |
import java.time.ZoneId; |
10 |
import java.time.temporal.TemporalAccessor; |
11 |
import java.util.ArrayList; |
12 |
import java.util.Date; |
13 |
import java.util.List; |
14 |
import java.util.Locale; |
15 |
import java.util.Objects; |
16 |
import org.apache.commons.io.IOUtils; |
17 |
import org.apache.commons.lang3.BooleanUtils; |
18 |
import org.apache.commons.lang3.Range; |
19 |
import org.apache.commons.lang3.StringUtils; |
20 |
import org.apache.commons.math.util.MathUtils; |
21 |
import org.gvsig.expressionevaluator.Code; |
22 |
import org.gvsig.expressionevaluator.Codes; |
23 |
import org.gvsig.expressionevaluator.Function; |
24 |
import org.gvsig.expressionevaluator.I18N; |
25 |
import org.gvsig.expressionevaluator.Interpreter; |
26 |
import org.gvsig.fmap.geom.Geometry; |
27 |
import org.gvsig.fmap.geom.primitive.Point; |
28 |
import org.json.JSONArray; |
29 |
import org.json.JSONObject; |
30 |
|
31 |
@SuppressWarnings("UseSpecificCatch") |
32 |
public abstract class AbstractFunction implements Function { |
33 |
|
34 |
private final String name; |
35 |
private String group; |
36 |
private Range argc;
|
37 |
private String description; |
38 |
private String[] descriptionArgs; |
39 |
private List<String> alias; |
40 |
private String template; |
41 |
private String returnType; |
42 |
private boolean sqlCompatible; |
43 |
|
44 |
protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType, boolean sqlCompatible) { |
45 |
this.name = name;
|
46 |
this.group = group;
|
47 |
this.argc = argc;
|
48 |
this.description = description;
|
49 |
this.template = template;
|
50 |
this.descriptionArgs = descriptionArgs;
|
51 |
this.returnType = returnType;
|
52 |
this.sqlCompatible = sqlCompatible;
|
53 |
load_from_resource(); |
54 |
} |
55 |
protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType) { |
56 |
this(group, name, argc, description, template, descriptionArgs, returnType, false); |
57 |
} |
58 |
|
59 |
protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs) { |
60 |
this(group, name, argc, description, template, null, null); |
61 |
} |
62 |
|
63 |
protected AbstractFunction(String group, String name, Range argc, String description, String template) { |
64 |
this(group, name, argc, description, template, null, null); |
65 |
} |
66 |
|
67 |
protected AbstractFunction(String group, String name, Range argc) { |
68 |
this(group, name, argc, null, null, null, null); |
69 |
} |
70 |
|
71 |
@Override
|
72 |
public String name() { |
73 |
return this.name; |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public String returnType() { |
78 |
return this.returnType; |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public String group() { |
83 |
return this.group; |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public Range argc() {
|
88 |
return argc;
|
89 |
} |
90 |
|
91 |
@Override
|
92 |
public String description() { |
93 |
return description;
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public String[] descriptionArgs() { |
98 |
return descriptionArgs;
|
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void addAlias(String name) { |
103 |
if( StringUtils.isBlank(name) ) {
|
104 |
return;
|
105 |
} |
106 |
if( this.alias == null ) { |
107 |
this.alias = new ArrayList<>(); |
108 |
} |
109 |
if( this.alias.contains(name) ) { |
110 |
return;
|
111 |
} |
112 |
this.alias.add(name);
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public List<String> aliases() { |
117 |
return this.alias; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public String template() { |
122 |
return this.template; |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public boolean isOperator() { |
127 |
return false; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public boolean useArgumentsInsteadObjects() { |
132 |
return false; |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public boolean isSQLCompatible() { |
137 |
return sqlCompatible;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public boolean allowConstantFolding() { |
142 |
return false; |
143 |
} |
144 |
|
145 |
@Override
|
146 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
147 |
return null; |
148 |
} |
149 |
|
150 |
protected int getInt(Object args[], int n) { |
151 |
if( args.length < n ) {
|
152 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
153 |
} |
154 |
Object value = args[n];
|
155 |
if( value == null ) { |
156 |
throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n)); |
157 |
} |
158 |
if( !(value instanceof Number) ) { |
159 |
String type = value.getClass().getCanonicalName();
|
160 |
throw new IllegalArgumentException( |
161 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
162 |
I18N.Expected_XexpectedX_and_found_XfoundX("Number",type)
|
163 |
); |
164 |
} |
165 |
return ((Number)value).intValue(); |
166 |
} |
167 |
|
168 |
protected long getLong(Object args[], int n) { |
169 |
if( args.length < n ) {
|
170 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
171 |
} |
172 |
Object value = args[n];
|
173 |
if( value == null ) { |
174 |
throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n)); |
175 |
} |
176 |
if( !(value instanceof Number) ) { |
177 |
String type = value.getClass().getCanonicalName();
|
178 |
throw new IllegalArgumentException( |
179 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
180 |
I18N.Expected_XexpectedX_and_found_XfoundX("Number",type)
|
181 |
); |
182 |
} |
183 |
return ((Number)value).longValue(); |
184 |
} |
185 |
|
186 |
protected double getDouble(Object args[], int n) { |
187 |
if( args.length < n ) {
|
188 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
189 |
} |
190 |
Object value = args[n];
|
191 |
if( value == null ) { |
192 |
throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n)); |
193 |
} |
194 |
if( !(value instanceof Number) ) { |
195 |
String type = value.getClass().getCanonicalName();
|
196 |
throw new IllegalArgumentException( |
197 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
198 |
I18N.Expected_XexpectedX_and_found_XfoundX("Number",type)
|
199 |
); |
200 |
} |
201 |
return ((Number)value).doubleValue(); |
202 |
} |
203 |
|
204 |
protected String getStr(Object args[], int n) { |
205 |
if( args.length < n ) {
|
206 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
207 |
} |
208 |
return Objects.toString(args[n], ""); |
209 |
} |
210 |
|
211 |
protected File getFile(Object args[], int n) { |
212 |
if( args.length < n ) {
|
213 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
214 |
} |
215 |
Object arg = args[n];
|
216 |
if( arg == null ) { |
217 |
return null; |
218 |
} |
219 |
if( arg instanceof File ) { |
220 |
return (File)arg; |
221 |
} |
222 |
if( arg instanceof URL ) { |
223 |
try {
|
224 |
return new File(((URL)arg).toURI()); |
225 |
} catch (URISyntaxException ex) { |
226 |
return null; |
227 |
} |
228 |
} |
229 |
if( arg instanceof URI ) { |
230 |
return new File(((URI)arg)); |
231 |
} |
232 |
String s = Objects.toString(arg, null); |
233 |
if( s == null ) { |
234 |
return null; |
235 |
} |
236 |
File f = new File(s); |
237 |
return f;
|
238 |
} |
239 |
|
240 |
protected Object getObject(Object args[], int n) { |
241 |
if( args.length < n ) {
|
242 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
243 |
} |
244 |
return args[n];
|
245 |
} |
246 |
|
247 |
protected Object getObject(Interpreter interpreter, Codes args, int n) { |
248 |
if( args.size() < n ) {
|
249 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.size(), n)); |
250 |
} |
251 |
Code arg = args.get(n); |
252 |
Object value = interpreter.run(arg);
|
253 |
return value;
|
254 |
} |
255 |
|
256 |
protected Geometry getGeom(Object[] args, int n) { |
257 |
return this.getGeom(args, n, false); |
258 |
} |
259 |
|
260 |
protected Geometry getGeom(Object[] args, int n, boolean allowNull) { |
261 |
if( args.length < n ) {
|
262 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
263 |
} |
264 |
Object value = args[n];
|
265 |
if( value == null ) { |
266 |
if( allowNull ) {
|
267 |
return null; |
268 |
} |
269 |
throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n)); |
270 |
} |
271 |
if( !(value instanceof Geometry) ) { |
272 |
String type = value.getClass().getCanonicalName();
|
273 |
throw new IllegalArgumentException( |
274 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
275 |
I18N.Expected_XexpectedX_and_found_XfoundX("Geometry",type)
|
276 |
); |
277 |
} |
278 |
return (Geometry)value;
|
279 |
} |
280 |
|
281 |
protected Point getPoint(Object[] args, int n) { |
282 |
if( args.length < n ) {
|
283 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
284 |
} |
285 |
Object value = args[n];
|
286 |
if( value == null ) { |
287 |
return null; |
288 |
} |
289 |
if( !(value instanceof Point) ) { |
290 |
String type = value.getClass().getCanonicalName();
|
291 |
throw new IllegalArgumentException( |
292 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
293 |
I18N.Expected_XexpectedX_and_found_XfoundX("Point",type)
|
294 |
); |
295 |
} |
296 |
return (Point)value; |
297 |
} |
298 |
|
299 |
protected Date getDate(Object[] args, int n) { |
300 |
if( args.length < n ) {
|
301 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
302 |
} |
303 |
Object value = args[n];
|
304 |
if( value == null ) { |
305 |
return null; |
306 |
} |
307 |
if( !(value instanceof Date) ) { |
308 |
String type = value.getClass().getCanonicalName();
|
309 |
throw new IllegalArgumentException( |
310 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
311 |
I18N.Expected_XexpectedX_and_found_XfoundX("Date",type)
|
312 |
); |
313 |
} |
314 |
return (Date)value; |
315 |
} |
316 |
|
317 |
protected LocalDateTime getLocalDateTime(Object[] args, int n) { |
318 |
if( args.length < n ) {
|
319 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
320 |
} |
321 |
Object value = args[n];
|
322 |
if( value == null ) { |
323 |
return null; |
324 |
} |
325 |
if( value instanceof Date ) { |
326 |
Date date = ((Date)value); |
327 |
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
328 |
} |
329 |
if( value instanceof LocalDateTime ) { |
330 |
return (LocalDateTime) value;
|
331 |
} |
332 |
if( value instanceof TemporalAccessor ) { |
333 |
return LocalDateTime.from(((TemporalAccessor)value));
|
334 |
} |
335 |
String type = value.getClass().getCanonicalName();
|
336 |
throw new IllegalArgumentException( |
337 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
338 |
I18N.Expected_XexpectedX_and_found_XfoundX("Temporal/Date",type)
|
339 |
); |
340 |
} |
341 |
|
342 |
protected boolean getBoolean(Object args[], int n, Double accuracy) { |
343 |
if( args.length < n ) {
|
344 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
345 |
} |
346 |
Object value = args[n];
|
347 |
return toBoolean(value, accuracy);
|
348 |
} |
349 |
|
350 |
protected boolean getBoolean(Interpreter interpreter, Codes args, int n) { |
351 |
Object value = getObject(interpreter, args, n);
|
352 |
return toBoolean(value, interpreter.getAccuracy());
|
353 |
} |
354 |
|
355 |
protected boolean toBoolean(Object value, Double accuracy) { |
356 |
if( value == null ) { |
357 |
return false; |
358 |
} |
359 |
if( value instanceof Boolean ) { |
360 |
return (Boolean)value; |
361 |
} |
362 |
if( value instanceof Number ) { |
363 |
return MathUtils.compareTo(
|
364 |
((Number) value).doubleValue(),
|
365 |
0,
|
366 |
accuracy==null? MathUtils.EPSILON:accuracy
|
367 |
) == 0;
|
368 |
} |
369 |
return BooleanUtils.toBoolean(value.toString());
|
370 |
} |
371 |
|
372 |
private void load_from_resource() { |
373 |
String lang = Locale.getDefault().getLanguage(); |
374 |
URL url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/"+lang+"/"+this.name()+".json"); |
375 |
if( url == null ) { |
376 |
url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/en/"+this.name()+".json"); |
377 |
if( url == null ) { |
378 |
return;
|
379 |
} |
380 |
} |
381 |
InputStream is = null; |
382 |
JSONObject json; |
383 |
try {
|
384 |
is = url.openStream(); |
385 |
List<String> lines = IOUtils.readLines(is); |
386 |
json = new JSONObject(StringUtils.join(lines, "\n")); |
387 |
} catch (Exception ex) { |
388 |
return;
|
389 |
} finally {
|
390 |
IOUtils.closeQuietly(is); |
391 |
} |
392 |
|
393 |
if( json.has("group") ) { |
394 |
this.group = json.getString("group"); |
395 |
} |
396 |
if( json.has("description") ) { |
397 |
Object x = json.get("description"); |
398 |
if( x instanceof String ) { |
399 |
this.description = (String) x; |
400 |
} else if( x instanceof JSONArray ) { |
401 |
StringBuilder builder = new StringBuilder(); |
402 |
for (int i = 0; i < ((JSONArray)x).length(); i++) { |
403 |
if( i>0 ) { |
404 |
builder.append(" ");
|
405 |
} |
406 |
builder.append(((JSONArray)x).getString(i)); |
407 |
} |
408 |
this.description = builder.toString();
|
409 |
} else {
|
410 |
this.description = x.toString();
|
411 |
} |
412 |
this.description = StringUtils.replace(
|
413 |
this.description,
|
414 |
"@@@",
|
415 |
url.toString() |
416 |
); |
417 |
} |
418 |
if( json.has("template") ) { |
419 |
this.template = json.getString("template"); |
420 |
} |
421 |
if( json.has("returnType") ) { |
422 |
this.returnType = json.getString("returnType"); |
423 |
} |
424 |
if( json.has("sqlCompatible") ) { |
425 |
this.sqlCompatible = json.getBoolean("sqlCompatible"); |
426 |
} |
427 |
if( json.has("args") ) { |
428 |
JSONArray x = json.getJSONArray("args");
|
429 |
String[] args = new String[x.length()]; |
430 |
for (int i = 0; i < x.length(); i++) { |
431 |
args[i] = x.getString(i); |
432 |
} |
433 |
this.descriptionArgs = args;
|
434 |
} |
435 |
if( json.has("alias") ) { |
436 |
JSONArray x = json.getJSONArray("alias");
|
437 |
for (int i = 0; i < x.length(); i++) { |
438 |
this.addAlias(x.getString(i));
|
439 |
} |
440 |
} |
441 |
} |
442 |
} |