use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method writeChoiceDecodeFunction.
private void writeChoiceDecodeFunction(List<AsnElementType> componentTypes, boolean hasExplicitTag) throws IOException {
if (hasExplicitTag) {
writeSimpleDecodeFunction("true");
write("public int decode(InputStream is, boolean withTag) throws IOException {");
write("int codeLength = 0;");
write("BerLength length = new BerLength();");
write("BerTag berTag = new BerTag();\n");
write("if (withTag) {");
write("codeLength += tag.decodeAndCheck(is);");
write("}\n");
write("codeLength += length.decode(is);");
write("codeLength += berTag.decode(is);\n");
} else {
writeSimpleDecodeFunction("null");
write("public int decode(InputStream is, BerTag berTag) throws IOException {\n");
write("int codeLength = 0;");
write("BerTag passedTag = berTag;\n");
write("if (berTag == null) {");
write("berTag = new BerTag();");
write("codeLength += berTag.decode(is);");
write("}\n");
}
// TODO rename "choiceDecodeLength" because it could also be of type ANY
String initChoiceDecodeLength = "int ";
for (int j = 0; j < componentTypes.size(); j++) {
AsnElementType componentType = componentTypes.get(j);
String explicitEncoding = getExplicitDecodingParameter(componentType);
Tag componentTag = getTag(componentType);
if (componentTag != null) {
write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
if (componentTag.type == TagType.EXPLICIT) {
write("codeLength += BerLength.skip(is);");
}
write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
write("codeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
write("return codeLength;");
write("}\n");
} else {
if (isDirectAnyOrChoice(componentType)) {
write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
write(initChoiceDecodeLength + "choiceDecodeLength = " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
initChoiceDecodeLength = "";
write("if (choiceDecodeLength != 0) {");
write("return codeLength + choiceDecodeLength;");
write("}");
write("else {");
write(getName(componentType) + " = null;");
write("}\n");
} else {
write("if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
write("codeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
write("return codeLength;");
write("}\n");
}
}
}
if (!hasExplicitTag) {
write("if (passedTag != null) {");
write("return 0;");
write("}\n");
}
write("throw new IOException(\"Error decoding CHOICE: Tag \" + berTag + \" matched to no item.\");");
write("}\n");
}
use of org.openmuc.jasn1.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("public int encode(OutputStream reverseOS) throws IOException {\n");
}
write("if (code != null) {");
write("for (int i = code.length - 1; i >= 0; i--) {");
write("reverseOS.write(code[i]);");
write("}");
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 (" + getName(componentType) + " != null) {");
String explicitEncoding = getExplicitEncodingParameter(componentType);
if (isExplicit(componentTag)) {
write("sublength = " + getName(componentType) + ".encode(reverseOS" + explicitEncoding + ");");
write("codeLength += sublength;");
write("codeLength += BerLength.encodeLength(reverseOS, sublength);");
} else {
write("codeLength += " + getName(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 org.openmuc.jasn1.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;
}
String line = "public " + className + "(";
int j = 0;
for (AsnElementType componentType : componentTypes) {
if (j != 0) {
line += ", ";
}
j++;
line += (getClassNameOfComponent(componentType) + " " + cleanUpName(componentType.name));
}
write(line + ") {");
for (AsnElementType componentType : componentTypes) {
String elementName = cleanUpName(componentType.name);
write("this." + elementName + " = " + elementName + ";");
}
write("}\n");
}
use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method writeSequenceOfEncodeFunction.
private void writeSequenceOfEncodeFunction(AsnElementType componentType, boolean hasExplicitTag, boolean isSequence) throws IOException {
write("public int encode(OutputStream reverseOS, boolean withTag) throws IOException {\n");
write("if (code != null) {");
write("for (int i = code.length - 1; i >= 0; i--) {");
write("reverseOS.write(code[i]);");
write("}");
write("if (withTag) {");
write("return tag.encode(reverseOS) + code.length;");
write("}");
write("return code.length;");
write("}\n");
write("int codeLength = 0;");
write("for (int i = (seqOf.size() - 1); i >= 0; i--) {");
Tag componentTag = getTag(componentType);
String explicitEncoding = getExplicitEncodingParameter(componentType);
if (componentTag != null) {
if (componentTag.type == TagType.EXPLICIT) {
write("int sublength = seqOf.get(i).encode(reverseOS" + explicitEncoding + ");");
write("codeLength += sublength;");
write("codeLength += BerLength.encodeLength(reverseOS, sublength);");
} else {
write("codeLength += seqOf.get(i).encode(reverseOS" + explicitEncoding + ");");
}
if (componentTag != null) {
writeEncodeTag(componentTag);
}
} else {
if (isDirectAnyOrChoice(componentType)) {
write("codeLength += seqOf.get(i).encode(reverseOS);");
} else {
write("codeLength += seqOf.get(i).encode(reverseOS, true);");
}
}
write("}\n");
if (hasExplicitTag) {
write("codeLength += BerLength.encodeLength(reverseOS, codeLength);");
if (isSequence) {
write("reverseOS.write(0x30);");
} else {
write("reverseOS.write(0x31);");
}
write("codeLength++;\n");
}
write("codeLength += BerLength.encodeLength(reverseOS, codeLength);\n");
write("if (withTag) {");
write("codeLength += tag.encode(reverseOS);");
write("}\n");
write("return codeLength;");
write("}\n");
}
use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.
the class BerClassWriter method writeSequenceOrSetToStringFunction.
private void writeSequenceOrSetToStringFunction(List<AsnElementType> componentTypes) throws IOException {
writeToStringFunction();
write("public void appendAsString(StringBuilder sb, int indentLevel) {\n");
write("sb.append(\"{\");");
boolean checkIfFirstSelectedElement = componentTypes.size() > 1;
int j = 0;
for (AsnElementType componentType : componentTypes) {
if (isOptional(componentType)) {
if (j == 0 && componentTypes.size() > 1) {
write("boolean firstSelectedElement = true;");
}
write("if (" + getName(componentType) + " != null) {");
}
if (j != 0) {
if (checkIfFirstSelectedElement) {
write("if (!firstSelectedElement) {");
}
write("sb.append(\",\\n\");");
if (checkIfFirstSelectedElement) {
write("}");
}
} else {
write("sb.append(\"\\n\");");
}
write("for (int i = 0; i < indentLevel + 1; i++) {");
write("sb.append(\"\\t\");");
write("}");
if (!isOptional(componentType)) {
write("if (" + getName(componentType) + " != null) {");
}
if (!isPrimitive(getUniversalType(componentType))) {
write("sb.append(\"" + getName(componentType) + ": \");");
write(getName(componentType) + ".appendAsString(sb, indentLevel + 1);");
} else {
write("sb.append(\"" + getName(componentType) + ": \").append(" + getName(componentType) + ");");
}
if (!isOptional(componentType)) {
write("}");
write("else {");
write("sb.append(\"" + getName(componentType) + ": <empty-required-field>\");");
write("}");
}
if (isOptional(componentType)) {
if (checkIfFirstSelectedElement) {
write("firstSelectedElement = false;");
}
write("}");
} else {
checkIfFirstSelectedElement = false;
}
write("");
j++;
}
write("sb.append(\"\\n\");");
write("for (int i = 0; i < indentLevel; i++) {");
write("sb.append(\"\\t\");");
write("}");
write("sb.append(\"}\");");
write("}\n");
}
Aggregations