Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @author ETSI / STF462 / Alexandre Berge
* @version $URL$
* $Id$
*/
package org.etsi.ttcn.codec.its.configtester;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.etsi.ttcn.codec.CodecBuffer;
import org.etsi.ttcn.codec.MainCodec;
import org.etsi.ttcn.codec.generic.Union;
import org.etsi.ttcn.tci.Type;
import org.etsi.ttcn.tci.UnionValue;
import org.etsi.ttcn.tci.Value;
public class CfUnion extends Union {
public CfUnion(MainCodec mainCodec) {
super(mainCodec);
}
@Override
public Value decode(CodecBuffer buf, Type decodingHypothesis) {
String variant = "";
Byte type = new Byte((byte)(0x00FF & buf.readBytes(1)[0]));
Pattern variantNamePattern = Pattern.compile(decodingHypothesis.getName() + "_(\\S+)");
System.out.println("CfUnion.decode: " + variantNamePattern + " / " + decodingHypothesis.getName());
Matcher matcher = variantNamePattern.matcher(CfPduId.name(type));
if (matcher.find()) {
variant = matcher.group(1);
}
else {
return null;
}
mainCodec.setHint(decodingHypothesis.getName(), variant);
return super.decode(buf, decodingHypothesis);
}
@Override
public CodecBuffer preEncode(Value value) {
UnionValue uv = (UnionValue)value;
System.out.println("CfUnion.preEncode: " + value.getType().getName() + "_" + uv.getPresentVariantName());
Byte id = CfPduId.value(value.getType().getName() + "_" + uv.getPresentVariantName());
if(id != null) {
return new CodecBuffer(new byte[] {id.byteValue()});
}
return null;
}
}