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 / typeconversion / DecodeFunction.java @ 44198

History | View | Annotate | Download (1.67 KB)

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

    
3
import org.apache.commons.codec.binary.Base64;
4
import org.apache.commons.codec.binary.Hex;
5
import org.apache.commons.lang3.Range;
6
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DECODE;
7
import org.gvsig.expressionevaluator.Interpreter;
8
import org.gvsig.expressionevaluator.spi.AbstractFunction;
9

    
10
public class DecodeFunction extends AbstractFunction {
11

    
12
    public DecodeFunction() {
13
        super(
14
            GROUP_CONVERSION, FUNCTION_DECODE, Range.is(2),
15
            "Decode binary data from textual representation in string.\nSupported formats are: base64, hex.",
16
            FUNCTION_DECODE+"({{data}}, 'hex')",
17
            new String[]{
18
                "data - A string value with the data to be converted",
19
                "format - A string value indicating the format of the input data. The default value is 'hex'."
20
            },
21
            "Byte array",
22
            true
23
        );
24
    }
25

    
26
    @Override
27
    public boolean allowConstantFolding() {
28
        return true;
29
    }
30
    
31
    @Override
32
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
33
        String data_s = getStr(args,0);
34
        String type = getStr(args,1);
35
        if( "hex".equalsIgnoreCase(type) ) {
36
            byte[] data = Hex.decodeHex(data_s.toCharArray());
37
            return data;
38
        } else if( "base64".equalsIgnoreCase(type) ) {
39
            byte[] data = Base64.decodeBase64(data_s);
40
            return data;
41
        } else if( "escape".equalsIgnoreCase(type) ) {
42
            // Not supported
43
        }
44
        throw new IllegalArgumentException("Unsupported format type '"+type+"'.");
45
    }
46

    
47
}