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 43512 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.string;
2
3 44006 jjdelcerro
import org.apache.commons.codec.binary.Base64;
4
import org.apache.commons.codec.binary.Hex;
5 43512 jjdelcerro
import org.apache.commons.lang3.Range;
6 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
7 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
8
9 44006 jjdelcerro
public class DecodeFunction extends AbstractFunction {
10 43512 jjdelcerro
11 44006 jjdelcerro
    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 43512 jjdelcerro
    }
24
25
    @Override
26 44009 jjdelcerro
    public boolean allowConstantFolding() {
27
        return true;
28
    }
29
30
    @Override
31 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
32 44006 jjdelcerro
        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 43512 jjdelcerro
    }
45
46
}