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 44198 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.typeconversion;
2 43512 jjdelcerro
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 44198 jjdelcerro
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DECODE;
7 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
8 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
9
10 44006 jjdelcerro
public class DecodeFunction extends AbstractFunction {
11 43512 jjdelcerro
12 44006 jjdelcerro
    public DecodeFunction() {
13
        super(
14 44198 jjdelcerro
            GROUP_CONVERSION, FUNCTION_DECODE, Range.is(2),
15 44006 jjdelcerro
            "Decode binary data from textual representation in string.\nSupported formats are: base64, hex.",
16 44198 jjdelcerro
            FUNCTION_DECODE+"({{data}}, 'hex')",
17 44006 jjdelcerro
            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 43512 jjdelcerro
    }
25
26
    @Override
27 44009 jjdelcerro
    public boolean allowConstantFolding() {
28
        return true;
29
    }
30
31
    @Override
32 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
33 44006 jjdelcerro
        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 43512 jjdelcerro
    }
46
47
}