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;


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

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

        Object param = inStack.pop();

        byte[] value = null;

        if (param instanceof String) {
            value = ((String) param).getBytes();
        } else if (param instanceof byte[]) {
            value = (byte[]) param;
        } else {
            throw new ParseException("Function의 파라미터 인수가 byte[], String이 아닙니다. : " + param);
        }

        try {
        	byte[] result = IConv.asciiToEBCDIC(new String(value));
        	if( result.length == (value.length+2) ) {
        		byte[] result_nososi = new byte[value.length];
        		System.arraycopy(result, 1, result_nososi, 0, result_nososi.length);        		
        		inStack.push(result_nososi);
        	}
        	else {
        		throw new Exception("Invalid source data, not Seal["+new String(value)+"]");
        	}
        } catch (Exception e) {
            //throw new ExecutionException(e.getMessage(), parameters, e);
            throw new ParseException(e.getMessage(), e);
        }
    }
}
