001package com.ganteater.ae.processor;
002
003import java.io.UnsupportedEncodingException;
004import java.util.Base64;
005
006import org.apache.commons.lang.StringEscapeUtils;
007
008import com.ganteater.ae.processor.annotation.CommandExamples;
009import com.ganteater.ae.util.xml.easyparser.Node;
010
011public class Coder extends BaseProcessor {
012
013        @CommandExamples({ "<Base64Encode name='type:property' source='type:property'/>" })
014        public void runCommandBase64Encode(Node action) throws UnsupportedEncodingException {
015                String name = attr(action, "name");
016                Object value = attrValue(action, "source");
017                if (value == null) {
018                        value = attr(action, "value");
019                }
020                if (value instanceof String) {
021                        value = Base64.getEncoder().encodeToString(((String) value).getBytes("UTF-8"));
022                } else if (value instanceof byte[]) {
023                        value = Base64.getEncoder().encodeToString((byte[]) value);
024                }
025                setVariableValue(name, value);
026        }
027
028        @CommandExamples({ "<EscapeXml name='type:property' source='type:property'/>" })
029        public void runCommandEscapeXml(Node action) {
030                String name = attr(action, "name");
031                String value = (String) attrValue(action, "source");
032                if (value instanceof String) {
033                        value = StringEscapeUtils.escapeXml(value);
034                }
035                setVariableValue(name, value);
036        }
037
038}