package com.eactive.eai.transformer.function.convert;

import java.util.Stack;

import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

import com.eactive.eai.transformer.util.IConv;

/**
 * SO-SI없는 EBCDIC(Graphic) 값을 전각 ASCII으로 변환하는 함수
 * 입력 값은 byte[], 결과는 byte[]임.
 */
public class EBCDIC2ASCIINOSOSISPACEPAD extends PostfixMathCommand {
    public EBCDIC2ASCIINOSOSISPACEPAD() {
        numberOfParameters = 1;
    }

    public void run(Stack inStack) throws ParseException {
        checkStack(inStack);

        Object param = inStack.pop();
        byte[] value = null;
        try {
            if (param instanceof String) {
                value = ((String) param).getBytes();
            } else if (param instanceof byte[]) {
                value = (byte[]) param;
            } else {
                throw new ParseException("Function의 파라미터 인수가 byte[], String이 아닙니다. : " + param);
            }
            byte[] ebcdic = new byte[value.length + 2];
            
            System.arraycopy(value, 0, ebcdic, 1, value.length);
            ebcdic[0] = (byte)0x0E;
            ebcdic[ebcdic.length-1] = (byte)0x0F;
            
            inStack.push( IConv.ebcdicToASCIISingleSpace(ebcdic) );
        } catch (Exception e) {
            //throw new ExecutionException(e.getMessage(), parameters, e);
            throw new ParseException(e.getMessage(), e);
        }
    }
}
