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 / FileFunction.java @ 44924
History | View | Annotate | Download (1.67 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 |
|
12 |
public class FileFunction extends AbstractFunction { |
13 |
|
14 |
public static final String NAME = "FILE"; |
15 |
|
16 |
public FileFunction() {
|
17 |
super(Function.GROUP_PROGRAMMING,
|
18 |
NAME, |
19 |
Range.is(1),
|
20 |
null,
|
21 |
NAME+"({{expression}})",
|
22 |
null,
|
23 |
"Object",
|
24 |
false
|
25 |
); |
26 |
} |
27 |
|
28 |
@Override
|
29 |
public boolean allowConstantFolding() { |
30 |
return false; |
31 |
} |
32 |
|
33 |
@Override
|
34 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
35 |
Object obj = getObject(args, 0); |
36 |
File value = toFile(obj);
|
37 |
return value;
|
38 |
} |
39 |
|
40 |
public static File toFile(Object obj) { |
41 |
if( obj == null ) { |
42 |
return null; |
43 |
} |
44 |
if( obj instanceof File ) { |
45 |
return (File) obj; |
46 |
} else if( obj instanceof CharSequence ) { |
47 |
return new File(((CharSequence)obj).toString()); |
48 |
} else if( obj instanceof URI ) { |
49 |
return new File(((URI)obj)); |
50 |
} else if( obj instanceof URL ) { |
51 |
return new File(((URL)obj).getPath()); |
52 |
} else {
|
53 |
throw new ExpressionRuntimeException("The "+NAME+" function require a File or a String and a received a '"+obj.getClass().getSimpleName()+"'."); |
54 |
} |
55 |
} |
56 |
|
57 |
} |