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

History | View | Annotate | Download (1.56 KB)

1
package org.gvsig.expressionevaluator.impl.function.string;
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 org.gvsig.expressionevaluator.Interpreter;
7
import org.gvsig.expressionevaluator.spi.AbstractFunction;
8

    
9
public class DecodeFunction extends AbstractFunction {
10

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

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

    
46
}