use of com.beanit.asn1bean.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method writeChoiceEncodeFunction.
private void writeChoiceEncodeFunction(List<AsnElementType> componentTypes, boolean hasExplicitTag) throws IOException {
if (hasExplicitTag) {
writeSimpleEncodeFunction();
write("public int encode(OutputStream reverseOS, boolean withTag) throws IOException {\n");
} else {
write("@Override public int encode(OutputStream reverseOS) throws IOException {\n");
}
write("if (code != null) {");
write("reverseOS.write(code);");
if (hasExplicitTag) {
write("if (withTag) {");
write("return tag.encode(reverseOS) + code.length;");
write("}");
}
write("return code.length;");
write("}\n");
write("int codeLength = 0;");
for (int j = componentTypes.size() - 1; j >= 0; j--) {
if (isExplicit(getTag(componentTypes.get(j)))) {
write("int sublength;\n");
break;
}
}
for (int j = componentTypes.size() - 1; j >= 0; j--) {
AsnElementType componentType = componentTypes.get(j);
Tag componentTag = getTag(componentType);
write("if (" + getVariableName(componentType) + " != null) {");
String explicitEncoding = getExplicitEncodingParameter(componentType);
if (isExplicit(componentTag)) {
write("sublength = " + getVariableName(componentType) + ".encode(reverseOS" + explicitEncoding + ");");
write("codeLength += sublength;");
write("codeLength += BerLength.encodeLength(reverseOS, sublength);");
} else {
write("codeLength += " + getVariableName(componentType) + ".encode(reverseOS" + explicitEncoding + ");");
}
if (componentTag != null) {
writeEncodeTag(componentTag);
}
if (hasExplicitTag) {
write("codeLength += BerLength.encodeLength(reverseOS, codeLength);");
write("if (withTag) {");
write("codeLength += tag.encode(reverseOS);");
write("}");
}
write("return codeLength;");
write("}");
write("");
}
write("throw new IOException(\"Error encoding CHOICE: No element of CHOICE was selected.\");");
write("}\n");
}
use of com.beanit.asn1bean.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method writeSequenceOrSetClass.
private void writeSequenceOrSetClass(String className, AsnSequenceSet asnSequenceSet, Tag tag, String isStaticStr, List<String> listOfSubClassNames) throws IOException {
write("public" + isStaticStr + " class " + className + " implements " + berTypeInterfaceString + "Serializable {\n");
write("private static final long serialVersionUID = 1L;\n");
List<AsnElementType> componentTypes = asnSequenceSet.componentTypes;
addAutomaticTagsIfNeeded(componentTypes);
if (asnSequenceSet.parameters != null) {
List<AsnParameter> parameters = asnSequenceSet.parameters;
replaceParametersByAnyTypes(componentTypes, parameters);
}
for (AsnElementType componentType : componentTypes) {
if ((componentType.typeReference instanceof AsnConstructedType)) {
listOfSubClassNames.add(getClassName(componentType));
}
}
for (AsnElementType componentType : componentTypes) {
if (isInnerType(componentType)) {
String subClassName = getClassName(componentType, className);
writeConstructedTypeClass(subClassName, componentType.typeReference, null, true, listOfSubClassNames);
}
}
Tag mainTag;
if (tag == null) {
if (asnSequenceSet.isSequence) {
mainTag = stdSeqTag;
} else {
mainTag = stdSetTag;
}
} else {
mainTag = tag;
}
write("public static final BerTag tag = new BerTag(" + getBerTagParametersString(mainTag) + ");\n");
write("private byte[] code = null;");
setClassNamesOfComponents(listOfSubClassNames, componentTypes, className);
writeMembers(componentTypes);
if (asnSequenceSet.isSequence && accessExtended && extensibilityImplied) {
String accessModifierString = jaxbMode ? "private" : "public";
write(accessModifierString + " byte[] extensionBytes = null;");
}
writeEmptyConstructor(className);
if (jaxbMode) {
writeGetterAndSetter(componentTypes);
if (asnSequenceSet.isSequence && accessExtended && extensibilityImplied) {
write("public byte[] getExtensionBytes() {");
write("return extensionBytes;");
write("}\n");
write("public void setExtensionBytes(byte[] bytes) {");
write("this.extensionBytes = bytes;");
write("}\n");
}
} else {
writeEncodeConstructor(className, componentTypes);
}
boolean hasExplicitTag = (tag != null) && (tag.type == TagType.EXPLICIT);
writeSimpleEncodeFunction();
writeSequenceOrSetEncodeFunction(componentTypes, hasExplicitTag, asnSequenceSet.isSequence);
writeSimpleDecodeFunction("true");
if (asnSequenceSet.isSequence) {
writeSequenceDecodeMethod(convertToComponentInfos(componentTypes), hasExplicitTag);
} else {
writeSetDecodeFunction(convertToComponentInfos(componentTypes), hasExplicitTag);
}
writeEncodeAndSaveFunction();
writeSequenceOrSetToStringFunction(componentTypes);
write("}\n");
}
use of com.beanit.asn1bean.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method writeEncodeConstructor.
private void writeEncodeConstructor(String className, List<AsnElementType> componentTypes) throws IOException {
if (componentTypes.isEmpty()) {
return;
}
StringBuilder line = new StringBuilder("public " + className + "(");
int j = 0;
for (AsnElementType componentType : componentTypes) {
if (j != 0) {
line.append(", ");
}
j++;
line.append(getClassName(componentType)).append(" ").append(cleanUpName(componentType.name));
}
write(line + ") {");
for (AsnElementType componentType : componentTypes) {
String elementName = cleanUpName(componentType.name);
write("this." + elementName + " = " + elementName + ";");
}
write("}\n");
}
use of com.beanit.asn1bean.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method getConstructorParametersFromConstructedElement.
private String[] getConstructorParametersFromConstructedElement(AsnConstructedType assignedTypeDefinition) {
List<AsnElementType> componentTypes;
if (assignedTypeDefinition instanceof AsnSequenceSet) {
componentTypes = ((AsnSequenceSet) assignedTypeDefinition).componentTypes;
} else {
componentTypes = ((AsnChoice) assignedTypeDefinition).componentTypes;
}
String[] constructorParameters = new String[componentTypes.size() * 2];
for (int j = 0; j < componentTypes.size(); j++) {
AsnElementType componentType = componentTypes.get(j);
constructorParameters[j * 2] = getClassName(componentType);
constructorParameters[j * 2 + 1] = cleanUpName(componentType.name);
}
return constructorParameters;
}
use of com.beanit.asn1bean.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method convertToComponentInfos.
private List<ComponentInfo> convertToComponentInfos(List<AsnElementType> asnElementTypes) {
int lastRequiredComponentIndex = getLastRequiredComponentIndex(asnElementTypes);
List<ComponentInfo> componentInfos = new ArrayList<>(asnElementTypes.size());
int i = 0;
for (AsnElementType asnElementType : asnElementTypes) {
boolean mayBeLastComponent = lastRequiredComponentIndex <= i;
componentInfos.add(convertToComponentInfo(asnElementType, mayBeLastComponent, null));
i++;
}
return componentInfos;
}
Aggregations