Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / programming / URLFunction.java @ 44207

History | View | Annotate | Download (1.63 KB)

1 44138 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.programming;
2 43512 jjdelcerro
3 44181 jjdelcerro
import java.io.File;
4
import java.net.URI;
5
import java.net.URL;
6 43512 jjdelcerro
import org.apache.commons.lang3.Range;
7 44138 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
8 43939 jjdelcerro
import org.gvsig.expressionevaluator.Function;
9 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
10 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
11 44181 jjdelcerro
import static org.gvsig.tools.dataTypes.DataTypes.URL;
12 43512 jjdelcerro
13 44181 jjdelcerro
public class URLFunction extends AbstractFunction {
14 43512 jjdelcerro
15 44181 jjdelcerro
    public static final String NAME = "URL";
16 44138 jjdelcerro
17 44181 jjdelcerro
    public URLFunction() {
18 44138 jjdelcerro
        super(Function.GROUP_PROGRAMMING,
19
            NAME,
20 44181 jjdelcerro
            Range.is(1),
21 44138 jjdelcerro
            null,
22
            null,
23
            null,
24 44098 jjdelcerro
            "Object",
25
            false
26
        );
27 43512 jjdelcerro
    }
28 44138 jjdelcerro
29 43512 jjdelcerro
    @Override
30 44138 jjdelcerro
    public boolean allowConstantFolding() {
31
        return false;
32
    }
33
34
    @Override
35 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
36 44138 jjdelcerro
        Object value;
37
        Object obj = getObject(args, 0);
38
        if( obj == null ) {
39 44181 jjdelcerro
            return null;
40
        }
41
        if( obj instanceof URL ) {
42
            return obj;
43 44138 jjdelcerro
        } else if( obj instanceof CharSequence ) {
44 44181 jjdelcerro
            value = new URL(((CharSequence)obj).toString());
45
        } else if( obj instanceof File ) {
46
            value = ((File)obj).toURI().toURL();
47
        } else if( obj instanceof URI ) {
48
            value = ((URI)obj).toURL();
49 43939 jjdelcerro
        } else {
50 44181 jjdelcerro
            throw new ExpressionRuntimeException("The "+NAME+" function require a File, URI or a String and a received a '"+obj.getClass().getSimpleName()+"'.");
51 43939 jjdelcerro
        }
52 44138 jjdelcerro
        return value;
53 43512 jjdelcerro
    }
54
55
}