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 @ 44592
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 |
if( arg==null ) { |
253 |
return null; |
254 |
} |
255 |
Object value = interpreter.run(arg);
|
256 |
return value;
|
257 |
} |
258 |
|
259 |
protected Geometry getGeom(Object[] args, int n) { |
260 |
return this.getGeom(args, n, false); |
261 |
} |
262 |
|
263 |
protected Geometry getGeom(Object[] args, int n, boolean allowNull) { |
264 |
if( args.length < n ) {
|
265 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
266 |
} |
267 |
Object value = args[n];
|
268 |
if( value == null ) { |
269 |
if( allowNull ) {
|
270 |
return null; |
271 |
} |
272 |
throw new IllegalArgumentException(I18N.Illegal_null_value_for_argument_XargnX_of_XIdentifierX_function(name(), n)); |
273 |
} |
274 |
if( !(value instanceof Geometry) ) { |
275 |
String type = value.getClass().getCanonicalName();
|
276 |
throw new IllegalArgumentException( |
277 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
278 |
I18N.Expected_XexpectedX_and_found_XfoundX("Geometry",type)
|
279 |
); |
280 |
} |
281 |
return (Geometry)value;
|
282 |
} |
283 |
|
284 |
protected Point getPoint(Object[] args, int n) { |
285 |
if( args.length < n ) {
|
286 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
287 |
} |
288 |
Object value = args[n];
|
289 |
if( value == null ) { |
290 |
return null; |
291 |
} |
292 |
if( !(value instanceof Point) ) { |
293 |
String type = value.getClass().getCanonicalName();
|
294 |
throw new IllegalArgumentException( |
295 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
296 |
I18N.Expected_XexpectedX_and_found_XfoundX("Point",type)
|
297 |
); |
298 |
} |
299 |
return (Point)value; |
300 |
} |
301 |
|
302 |
protected Date getDate(Object[] args, int n) { |
303 |
if( args.length < n ) {
|
304 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
305 |
} |
306 |
Object value = args[n];
|
307 |
if( value == null ) { |
308 |
return null; |
309 |
} |
310 |
if( !(value instanceof Date) ) { |
311 |
String type = value.getClass().getCanonicalName();
|
312 |
throw new IllegalArgumentException( |
313 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
314 |
I18N.Expected_XexpectedX_and_found_XfoundX("Date",type)
|
315 |
); |
316 |
} |
317 |
return (Date)value; |
318 |
} |
319 |
|
320 |
protected LocalDateTime getLocalDateTime(Object[] args, int n) { |
321 |
if( args.length < n ) {
|
322 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
323 |
} |
324 |
Object value = args[n];
|
325 |
if( value == null ) { |
326 |
return null; |
327 |
} |
328 |
if( value instanceof Date ) { |
329 |
Date date = ((Date)value); |
330 |
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
331 |
} |
332 |
if( value instanceof LocalDateTime ) { |
333 |
return (LocalDateTime) value;
|
334 |
} |
335 |
if( value instanceof TemporalAccessor ) { |
336 |
return LocalDateTime.from(((TemporalAccessor)value));
|
337 |
} |
338 |
String type = value.getClass().getCanonicalName();
|
339 |
throw new IllegalArgumentException( |
340 |
I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
|
341 |
I18N.Expected_XexpectedX_and_found_XfoundX("Temporal/Date",type)
|
342 |
); |
343 |
} |
344 |
|
345 |
protected boolean getBoolean(Object args[], int n, Double accuracy) { |
346 |
if( args.length < n ) {
|
347 |
throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n)); |
348 |
} |
349 |
Object value = args[n];
|
350 |
return toBoolean(value, accuracy);
|
351 |
} |
352 |
|
353 |
protected boolean getBoolean(Interpreter interpreter, Codes args, int n) { |
354 |
Object value = getObject(interpreter, args, n);
|
355 |
return toBoolean(value, interpreter.getAccuracy());
|
356 |
} |
357 |
|
358 |
protected boolean toBoolean(Object value, Double accuracy) { |
359 |
if( value == null ) { |
360 |
return false; |
361 |
} |
362 |
if( value instanceof Boolean ) { |
363 |
return (Boolean)value; |
364 |
} |
365 |
if( value instanceof Number ) { |
366 |
return MathUtils.compareTo(
|
367 |
((Number) value).doubleValue(),
|
368 |
0,
|
369 |
accuracy==null? MathUtils.EPSILON:accuracy
|
370 |
) == 0;
|
371 |
} |
372 |
return BooleanUtils.toBoolean(value.toString());
|
373 |
} |
374 |
|
375 |
private void load_from_resource() { |
376 |
String lang = Locale.getDefault().getLanguage(); |
377 |
URL url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/"+lang+"/"+this.name()+".json"); |
378 |
if( url == null ) { |
379 |
url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/en/"+this.name()+".json"); |
380 |
if( url == null ) { |
381 |
return;
|
382 |
} |
383 |
} |
384 |
InputStream is = null; |
385 |
JSONObject json; |
386 |
try {
|
387 |
is = url.openStream(); |
388 |
List<String> lines = IOUtils.readLines(is); |
389 |
json = new JSONObject(StringUtils.join(lines, "\n")); |
390 |
} catch (Exception ex) { |
391 |
return;
|
392 |
} finally {
|
393 |
IOUtils.closeQuietly(is); |
394 |
} |
395 |
|
396 |
if( json.has("group") ) { |
397 |
this.group = json.getString("group"); |
398 |
} |
399 |
if( json.has("description") ) { |
400 |
Object x = json.get("description"); |
401 |
if( x instanceof String ) { |
402 |
this.description = (String) x; |
403 |
} else if( x instanceof JSONArray ) { |
404 |
StringBuilder builder = new StringBuilder(); |
405 |
for (int i = 0; i < ((JSONArray)x).length(); i++) { |
406 |
if( i>0 ) { |
407 |
builder.append(" ");
|
408 |
} |
409 |
builder.append(((JSONArray)x).getString(i)); |
410 |
} |
411 |
this.description = builder.toString();
|
412 |
} else {
|
413 |
this.description = x.toString();
|
414 |
} |
415 |
this.description = StringUtils.replace(
|
416 |
this.description,
|
417 |
"@@@",
|
418 |
url.toString() |
419 |
); |
420 |
} |
421 |
if( json.has("template") ) { |
422 |
this.template = json.getString("template"); |
423 |
} |
424 |
if( json.has("returnType") ) { |
425 |
this.returnType = json.getString("returnType"); |
426 |
} |
427 |
if( json.has("sqlCompatible") ) { |
428 |
this.sqlCompatible = json.getBoolean("sqlCompatible"); |
429 |
} |
430 |
if( json.has("args") ) { |
431 |
JSONArray x = json.getJSONArray("args");
|
432 |
String[] args = new String[x.length()]; |
433 |
for (int i = 0; i < x.length(); i++) { |
434 |
args[i] = x.getString(i); |
435 |
} |
436 |
this.descriptionArgs = args;
|
437 |
} |
438 |
if( json.has("alias") ) { |
439 |
JSONArray x = json.getJSONArray("alias");
|
440 |
for (int i = 0; i < x.length(); i++) { |
441 |
this.addAlias(x.getString(i));
|
442 |
} |
443 |
} |
444 |
} |
445 |
} |