Statistics
| Revision:

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 @ 44139

History | View | Annotate | Download (12.7 KB)

1
package org.gvsig.expressionevaluator.spi;
2

    
3
import java.io.InputStream;
4
import java.net.URL;
5
import java.util.ArrayList;
6
import java.util.List;
7
import java.util.Locale;
8
import java.util.Objects;
9
import org.apache.commons.io.IOUtils;
10
import org.apache.commons.lang3.BooleanUtils;
11
import org.apache.commons.lang3.Range;
12
import org.apache.commons.lang3.StringUtils;
13
import org.apache.commons.math.util.MathUtils;
14
import org.gvsig.expressionevaluator.Code;
15
import org.gvsig.expressionevaluator.Codes;
16
import org.gvsig.expressionevaluator.Function;
17
import org.gvsig.expressionevaluator.I18N;
18
import org.gvsig.expressionevaluator.Interpreter;
19
import org.gvsig.fmap.geom.Geometry;
20
import org.gvsig.fmap.geom.primitive.Point;
21
import org.json.JSONArray;
22
import org.json.JSONObject;
23

    
24
@SuppressWarnings("UseSpecificCatch")
25
public abstract class AbstractFunction implements Function {
26
 
27
    private final String name;
28
    private String group;
29
    private Range argc;
30
    private String description;
31
    private String[] descriptionArgs;
32
    private List<String> alias;
33
    private String template;
34
    private String returnType;
35
    private boolean sqlCompatible;
36

    
37
    protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType, boolean sqlCompatible) {
38
        this.name = name;
39
        this.group = group;
40
        this.argc = argc;
41
        this.description = description;
42
        this.template = template;
43
        this.descriptionArgs = descriptionArgs;
44
        this.returnType = returnType;
45
        this.sqlCompatible = sqlCompatible;
46
        load_from_resource();
47
    }
48
    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
    protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs) {
53
        this(group, name, argc, description, template, null, null);
54
    }
55

    
56
    protected AbstractFunction(String group, String name, Range argc, String description, String template) {
57
        this(group, name, argc, description, template, null, null);
58
    }
59

    
60
    protected AbstractFunction(String group, String name, Range argc) {
61
        this(group, name, argc, null, null, null, null);
62
    }
63

    
64
    @Override
65
    public String name() {
66
        return this.name;
67
    }
68

    
69
    @Override
70
    public String returnType() {
71
        return this.returnType;
72
    }
73

    
74
    @Override
75
    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
        if( StringUtils.isBlank(name) ) {
97
            return;
98
        }
99
        if( this.alias == null ) {
100
            this.alias = new ArrayList<>();
101
        }
102
        if( this.alias.contains(name) ) {
103
            return;
104
        }
105
        this.alias.add(name);
106
    }
107

    
108
    @Override
109
    public List<String> aliases() {
110
        return this.alias;
111
    }
112

    
113
    @Override
114
    public String template() {
115
        return this.template;
116
    }
117

    
118
    @Override
119
    public boolean isOperator() {
120
        return false;
121
    }
122

    
123
    @Override
124
    public boolean useArgumentsInsteadObjects() {
125
        return false;
126
    }
127

    
128
    @Override
129
    public boolean isSQLCompatible() {
130
        return sqlCompatible;
131
    }
132

    
133
    @Override
134
    public boolean allowConstantFolding() {
135
        return false;
136
    }
137
    
138
    @Override
139
    public Object call(Interpreter interpreter, Codes args) throws Exception {
140
        return null;
141
    }
142
    
143
    protected int getInt(Object args[], int n) {
144
        if( args.length < n  ) {
145
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
146
        }
147
        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
        }
151
        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
    }
160

    
161
    protected long getLong(Object args[], int n) {
162
        if( args.length < n  ) {
163
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
164
        }
165
        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
        }
169
        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
    }
178

    
179
    protected double getDouble(Object args[], int n) {
180
        if( args.length < n  ) {
181
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
182
        }
183
        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
        }
187
        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
    }
196
    
197
    protected String getStr(Object args[], int n) {
198
        if( args.length < n  ) {
199
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
200
        }
201
        return Objects.toString(args[n], "");
202
    }
203
    
204
    protected Object getObject(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 args[n];
209
    }
210
    
211
    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
        }
215
        Code arg = args.get(n);
216
        Object value = interpreter.run(arg);
217
        return value;
218
    }
219
    
220
    protected Geometry getGeom(Object[] args, int n) {
221
        return this.getGeom(args, n, false);
222
    }
223
    
224
    protected Geometry getGeom(Object[] args, int n, boolean allowNull) {
225
        if( args.length < n  ) {
226
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
227
        }
228
        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
        }
235
        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
    }
244

    
245
    protected Point getPoint(Object[] args, int n) {
246
        if( args.length < n  ) {
247
            throw new IllegalArgumentException(I18N.Required_argument_XargnX_and_only_found_XargcX_in_call_to_XIdentifierX(name(), args.length, n));
248
        }
249
        Object value = args[n];
250
        if( value == null ) {
251
            return null;
252
        }
253
        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
    }
262
    
263
    protected boolean getBoolean(Object args[], int n, Double accuracy) {
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
        return toBoolean(value, accuracy);
269
    }
270

    
271
    protected boolean getBoolean(Interpreter interpreter, Codes args, int n) {
272
        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

    
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
}