Revision 44006 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

View differences:

AbstractFunction.java
1 1
package org.gvsig.expressionevaluator.spi;
2 2

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

  
23
@SuppressWarnings("UseSpecificCatch")
16 24
public abstract class AbstractFunction implements Function {
17 25
 
18 26
    private final String name;
19
    private final String group;
20
    private final Range argc;
21
    private final String description;
22
    private final String[] descriptionArgs;
27
    private String group;
28
    private Range argc;
29
    private String description;
30
    private String[] descriptionArgs;
23 31
    private List<String> alias;
24 32
    private String template;
25 33
    private String returnType;
......
34 42
        this.descriptionArgs = descriptionArgs;
35 43
        this.returnType = returnType;
36 44
        this.sqlCompatible = sqlCompatible;
45
        load_from_resource();
37 46
    }
38 47
    protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType) {
39 48
        this(group, name, argc, description, template, descriptionArgs, returnType, false);
......
83 92

  
84 93
    @Override
85 94
    public void addAlias(String name) {
95
        if( StringUtils.isBlank(name) ) {
96
            return;
97
        }
86 98
        if( this.alias == null ) {
87 99
            this.alias = new ArrayList<>();
88 100
        }
101
        if( this.alias.contains(name) ) {
102
            return;
103
        }
89 104
        this.alias.add(name);
90 105
    }
91 106

  
......
226 241
        }
227 242
        return BooleanUtils.toBoolean(value.toString());
228 243
    } 
244

  
245
    private void load_from_resource() {
246
        String lang = Locale.getDefault().getLanguage();
247
        URL url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/"+lang+"/"+this.name()+".json");
248
        if( url == null ) {
249
            url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/en/"+this.name()+".json");
250
            if( url == null ) {
251
                return;
252
            }
253
        }
254
        InputStream is = null;
255
        JSONObject json;
256
        try {
257
            is = url.openStream();
258
            List<String> lines = IOUtils.readLines(is);
259
            json = new JSONObject(StringUtils.join(lines,  "\n"));
260
        } catch (Exception ex) {
261
            return;
262
        } finally {
263
            IOUtils.closeQuietly(is);
264
        }
265
        
266
        if( json.has("group") ) {
267
            this.group = json.getString("group");
268
        }
269
        if( json.has("description") ) {
270
            Object x = json.get("description");
271
            if( x instanceof String ) {
272
                this.description = (String) x;
273
            } else if( x instanceof JSONArray ) {
274
                StringBuilder builder = new StringBuilder();
275
                for (int i = 0; i < ((JSONArray)x).length(); i++) {
276
                    if( i>0 ) {
277
                        builder.append(" ");
278
                    }
279
                    builder.append(((JSONArray)x).getString(i));
280
                }
281
                this.description = builder.toString();
282
            } else {
283
                this.description = x.toString();
284
            }
285
            this.description = StringUtils.replace(
286
                    this.description, 
287
                    "@@@", 
288
                    url.toString()
289
            );
290
        }
291
        if( json.has("template") ) {
292
            this.template = json.getString("template");
293
        }
294
        if( json.has("returnType") ) {
295
            this.returnType = json.getString("returnType");
296
        }
297
        if( json.has("sqlCompatible") ) {
298
            this.sqlCompatible = json.getBoolean("sqlCompatible");
299
        }
300
        if( json.has("args") ) {
301
            JSONArray x = json.getJSONArray("args");
302
            String[] args = new String[x.length()];
303
            for (int i = 0; i < x.length(); i++) {
304
                args[i] = x.getString(i);
305
            }
306
            this.descriptionArgs = args;
307
        }
308
        if( json.has("alias") ) {
309
            JSONArray x = json.getJSONArray("alias");
310
            for (int i = 0; i < x.length(); i++) {
311
                this.addAlias(x.getString(i));
312
            }
313
        }
314
    }
229 315
}

Also available in: Unified diff