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 / string / SubstringFunction.java @ 47710

History | View | Annotate | Download (1.3 KB)

1
package org.gvsig.expressionevaluator.impl.function.string;
2

    
3
import org.apache.commons.lang3.Range;
4
import org.apache.commons.lang3.StringUtils;
5
import org.gvsig.expressionevaluator.Interpreter;
6
import org.gvsig.expressionevaluator.spi.AbstractFunction;
7

    
8
public class SubstringFunction extends AbstractFunction {
9

    
10
    public SubstringFunction() {
11
        super("String", "SUBSTRING", Range.between(2, 3));
12
        this.addAlias("SUBSTR");
13
    }
14

    
15

    
16
    @Override
17
    public boolean allowConstantFolding() {
18
        return true;
19
    }
20
    
21
    @Override
22
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
23
        /*
24
            http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
25
            section 4.2.2.1
26
        */
27
        
28
        String str = getStr(args,0);
29
        int start = getInt(args, 1);
30
        int len = 0;
31
        if( args.length == 3 ) {
32
            len = getInt(args, 2);
33
            if(len < 0) {
34
                throw new IllegalArgumentException("Length must be greater than or equal to 0");
35
            }
36
        }
37
        if(start < 1) {
38
            len = len + start - 1;
39
            start = 1;
40
        }
41
        String r = StringUtils.substring(str, start-1);
42
        if( args.length == 3 ) {
43
            r = StringUtils.left(r, len);
44
        }
45
        return r;
46
    }
47

    
48
}