MainCodec.java 4.42 KB
Newer Older
filatov's avatar
filatov committed
/**
 * @author      ETSI / STF462 / Alexandre Berge
 * @version     $URL$
 *              $Id$
 */
package org.etsi.ttcn.codec;

import java.util.Map;
import java.util.TreeMap;

import org.etsi.codec.ITciCDWrapper;
filatov's avatar
filatov committed
import org.etsi.ttcn.tci.TciCDProvided;
import org.etsi.ttcn.tci.Type;
import org.etsi.ttcn.tci.Value;
import org.etsi.ttcn.tri.TriMessage;
filatov's avatar
filatov committed

public class MainCodec extends ICodec {

    public MainCodec(ITciCDWrapper _cdReq) {
filatov's avatar
filatov committed
        super(null);
        this.cdReq = _cdReq;
filatov's avatar
filatov committed
    }

    public Value triDecode(TriMessage message, Type decodingHypothesis) {
garciay's avatar
garciay committed
//        TERFactory.getInstance().logDebug("############################################################################################");
filatov's avatar
filatov committed
        return decode(new CodecBuffer(message.getEncodedMessage()), decodingHypothesis);
    }

    public TriMessage triEncode(Value value) {
garciay's avatar
garciay committed
//        TERFactory.getInstance().logDebug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
filatov's avatar
filatov committed
        CodecBuffer encoded = encode(value);
        encoded.runCallbacks();
        return new TriMessageImpl(encoded.getBytes());
    }

    @Override
    public Value decode(CodecBuffer buf, Type decodingHypothesis) {
garciay's avatar
garciay committed
//        TERFactory.getInstance().logDebug(">>> MainCodec.decode: " + decodingHypothesis.getName());
//        ByteHelper.dump(">>> MainCodec.decode: ", buf.getBytes());
filatov's avatar
filatov committed
        
        CodecFactory cf = CodecFactory.getInstance();
        try {
            TciCDProvided extCodec = cf.getExternalCodec(decodingHypothesis.getTypeEncoding());
            if(extCodec != null) {
filatov's avatar
filatov committed
                return extCodec.decode(new TriMessageImpl(buf.getBytes()), decodingHypothesis);
            } else {
                ICodec codec = cf.getCodec(
                        this,
                        decodingHypothesis.getTypeClass(),
                        decodingHypothesis.getTypeEncoding(),
                        decodingHypothesis.getName()
                        );
                codec.preDecode(buf, decodingHypothesis);
                
                // TODO To be removed, for debug purpose only
//                Value decValue = codec.decode(buf, decodingHypothesis);
garciay's avatar
garciay committed
//                TERFactory.getInstance().logDebug("<<< MainCodec.decode: " + decValue);
//                return decValue;
                return codec.decode(buf, decodingHypothesis);
        } catch(Throwable e) {
            e.printStackTrace();
            return null;
filatov's avatar
filatov committed
        }
    }

    @Override
    public CodecBuffer encode(Value value) {
filatov's avatar
filatov committed
        CodecFactory cf = CodecFactory.getInstance();
        TciCDProvided extCodec = cf.getExternalCodec(value.getValueEncoding());
        if(extCodec != null) {
            return new CodecBuffer(extCodec.encode(value).getEncodedMessage());
filatov's avatar
filatov committed
        }
        else {
            ICodec codec = CodecFactory.getInstance().getCodec(
                    this,
                    value.getType().getTypeClass(),
                    value.getValueEncoding(),
                    value.getType().getName()
                    );
            CodecBuffer preBuf = codec.preEncode(value);
            CodecBuffer buf = codec.encode(value);
            if(preBuf != null) {
                preBuf.append(buf);
                buf = preBuf;
            }
//            ByteHelper.dump("<<< MainCodec.encode: ", buf.getBytes());
filatov's avatar
filatov committed
            return buf;
        }
    }
filatov's avatar
filatov committed
    public String getHint(String key) {
garciay's avatar
garciay committed
//        TERFactory.getInstance().logDebug("getHint: " + key + ": " + hints.get(key));
filatov's avatar
filatov committed
        return hints.get(key);
    }
filatov's avatar
filatov committed
    public void setHint(String key, String value) {
garciay's avatar
garciay committed
//        TERFactory.getInstance().logDebug("setHint: " + key + ", " + value);
filatov's avatar
filatov committed
        hints.put(key, value);
    }
filatov's avatar
filatov committed
    public java.lang.Boolean getPresenceHint(String key) {
garciay's avatar
garciay committed
//        TERFactory.getInstance().logDebug("getPresenceHint: " + key + ": " + presenceHints.get(key));
filatov's avatar
filatov committed
        return presenceHints.get(key);
    }
filatov's avatar
filatov committed
    public void setPresenceHint(String key, java.lang.Boolean value) {
garciay's avatar
garciay committed
//        TERFactory.getInstance().logDebug("setPresenceHint: " + key + ", " + value);
filatov's avatar
filatov committed
        presenceHints.put(key, value);
    }
    
    public ITciCDWrapper getTciCDRequired() {
filatov's avatar
filatov committed
        return cdReq;
    }
filatov's avatar
filatov committed
    protected Map<String, String> hints = new TreeMap<String, String>();
    protected Map<String, java.lang.Boolean> presenceHints = new TreeMap<String, java.lang.Boolean>();
    protected ITciCDWrapper cdReq;