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