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