Search in sources :

Example 6 with AsnElementType

use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.

the class BerClassWriter method writeGetterAndSetter.

private void writeGetterAndSetter(List<AsnElementType> componentTypes) throws IOException {
    for (AsnElementType element : componentTypes) {
        String typeName = getClassNameOfComponent(element);
        String getterName = cleanUpName("get" + capitalizeFirstCharacter(element.name));
        String setterName = cleanUpName("set" + capitalizeFirstCharacter(element.name));
        String variableName = cleanUpName(element.name);
        write("public void " + setterName + "(" + typeName + " " + variableName + ") {");
        write("this." + variableName + " = " + variableName + ";");
        write("}\n");
        write("public " + typeName + " " + getterName + "() {");
        write("return " + variableName + ";");
        write("}\n");
    }
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 7 with AsnElementType

use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.

the class BerClassWriter method writeSequenceDecodeFunction.

private void writeSequenceDecodeFunction(List<AsnElementType> componentTypes, boolean hasExplicitTag) throws IOException {
    write("public int decode(InputStream is, boolean withTag) throws IOException {");
    write("int codeLength = 0;");
    write("int subCodeLength = 0;");
    write("BerTag berTag = new BerTag();\n");
    write("if (withTag) {");
    write("codeLength += tag.decodeAndCheck(is);");
    write("}\n");
    write("BerLength length = new BerLength();");
    write("codeLength += length.decode(is);\n");
    write("int totalLength = length.val;");
    if (supportIndefiniteLength == true) {
        writeSequenceDecodeIndefiniteLenghtPart(componentTypes);
    }
    write("codeLength += totalLength;\n");
    if (hasExplicitTag) {
        write("int nextByte = is.read();");
        write("if (nextByte == -1) {");
        write("throw new EOFException(\"Unexpected end of input stream.\");");
        write("}");
        write("if (nextByte != (0x30)) {");
        write("throw new IOException(\"Tag does not match!\");");
        write("}");
        write("length.decode(is);");
        write("totalLength = length.val;\n");
    }
    int lastNoneOptionalFieldIndex = -1;
    for (int j = 0; j < componentTypes.size(); j++) {
        AsnElementType componentType = componentTypes.get(componentTypes.size() - 1 - j);
        if (!isOptional(componentType)) {
            lastNoneOptionalFieldIndex = componentTypes.size() - 1 - j;
            break;
        }
    }
    if (lastNoneOptionalFieldIndex == -1) {
        write("if (totalLength == 0) {");
        write("return codeLength;");
        write("}");
    }
    write("subCodeLength += berTag.decode(is);");
    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 (isExplicit(componentTag)) {
                write("subCodeLength += length.decode(is);");
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            if (lastNoneOptionalFieldIndex <= j) {
                write("if (subCodeLength == totalLength) {");
                write("return codeLength;");
                write("}");
            }
            if (j != (componentTypes.size() - 1)) {
                write("subCodeLength += berTag.decode(is);");
            }
            write("}");
            if (j == (componentTypes.size() - 1)) {
                write("throw new IOException(\"Unexpected end of sequence, length tag: \" + totalLength + \", actual sequence length: \" + subCodeLength);\n");
            } else if (!isOptional(componentType)) {
                write("else {");
                write("throw new IOException(\"Tag does not match the mandatory sequence element tag.\");");
                write("}");
            }
        } else {
            if (isDirectAnyOrChoice(componentType)) {
                write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
                if (isOptional(componentType)) {
                    write(initChoiceDecodeLength + "choiceDecodeLength = " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
                    initChoiceDecodeLength = "";
                    if (j != (componentTypes.size() - 1)) {
                        write("if (choiceDecodeLength != 0) {");
                        write("subCodeLength += choiceDecodeLength;");
                        if (lastNoneOptionalFieldIndex <= j) {
                            write("if (subCodeLength == totalLength) {");
                            write("return codeLength;");
                            write("}");
                        }
                        write("subCodeLength += berTag.decode(is);");
                        write("}");
                        write("else {");
                        write(getName(componentType) + " = null;");
                        write("}");
                    } else {
                        // if last sequence element
                        write("subCodeLength += choiceDecodeLength;");
                        write("if (subCodeLength == totalLength) {");
                        write("return codeLength;");
                        write("}");
                    }
                } else {
                    write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
                    if (j != (componentTypes.size() - 1)) {
                        if (lastNoneOptionalFieldIndex <= j) {
                            write("if (subCodeLength == totalLength) {");
                            write("return codeLength;");
                            write("}");
                        }
                        write("subCodeLength += berTag.decode(is);");
                    } else {
                        write("if (subCodeLength == totalLength) {");
                        write("return codeLength;");
                        write("}");
                    }
                }
                if (j == (componentTypes.size() - 1)) {
                    write("throw new IOException(\"Unexpected end of sequence, length tag: \" + totalLength + \", actual sequence length: \" + subCodeLength);\n");
                }
            } else {
                write("if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
                write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
                write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
                if (lastNoneOptionalFieldIndex <= j) {
                    write("if (subCodeLength == totalLength) {");
                    write("return codeLength;");
                    write("}");
                }
                if (j != (componentTypes.size() - 1)) {
                    write("subCodeLength += berTag.decode(is);");
                }
                write("}");
                if (j == (componentTypes.size() - 1)) {
                    write("throw new IOException(\"Unexpected end of sequence, length tag: \" + totalLength + \", actual sequence length: \" + subCodeLength);\n");
                } else if (!isOptional(componentType)) {
                    write("else {");
                    write("throw new IOException(\"Tag does not match the mandatory sequence element tag.\");");
                    write("}");
                }
            }
        }
        write("");
    }
    if (componentTypes.isEmpty()) {
        write("return subCodeLength;");
    }
    write("}\n");
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 8 with AsnElementType

use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.

the class BerClassWriter method addAutomaticTagsIfNeeded.

private void addAutomaticTagsIfNeeded(List<AsnElementType> componentTypes) throws IOException {
    if (tagDefault != TagDefault.AUTOMATIC) {
        return;
    }
    for (AsnElementType element : componentTypes) {
        if (getTag(element) != null) {
            return;
        }
    }
    int i = 0;
    for (AsnElementType element : componentTypes) {
        element.tag = new AsnTag();
        element.tag.classNumber = new AsnClassNumber();
        element.tag.classNumber.num = i;
        i++;
    }
}
Also used : AsnClassNumber(org.openmuc.jasn1.compiler.model.AsnClassNumber) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag)

Example 9 with AsnElementType

use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.

the class BerClassWriter method getExplicitDecodingParameter.

private String getExplicitDecodingParameter(AsnElementType componentType) throws IOException {
    Tag tag = getTag(componentType);
    String explicitEncoding;
    if (tag != null && tag.type == TagType.EXPLICIT) {
        if (isDirectAnyOrChoice(componentType)) {
            explicitEncoding = ", null";
        } else {
            explicitEncoding = ", true";
        }
    } else {
        if (isDirectAnyOrChoice(componentType)) {
            explicitEncoding = ", berTag";
        } else {
            explicitEncoding = ", false";
        }
    }
    return explicitEncoding;
}
Also used : BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString)

Example 10 with AsnElementType

use of org.openmuc.jasn1.compiler.model.AsnElementType in project jasn1 by openmuc.

the class BerClassWriter method writeSetDecodeIndefiniteLenghtPart.

private void writeSetDecodeIndefiniteLenghtPart(List<AsnElementType> componentTypes) throws IOException {
    write("if (totalLength == -1) {");
    write("subCodeLength += berTag.decode(is);\n");
    String initChoiceDecodeLength = "int ";
    for (AsnElementType componentType : componentTypes) {
        Tag componentTag = getTag(componentType);
        write("if (berTag.tagNumber == 0 && berTag.tagClass == 0 && berTag.primitive == 0) {");
        write("int nextByte = is.read();");
        write("if (nextByte != 0) {");
        write("if (nextByte == -1) {");
        write("throw new EOFException(\"Unexpected end of input stream.\");");
        write("}");
        write("throw new IOException(\"Decoded sequence has wrong end of contents octets\");");
        write("}");
        write("codeLength += subCodeLength + 1;");
        write("return codeLength;");
        write("}");
        String explicitEncoding;
        if (isDirectAnyOrChoice(componentType)) {
            if (isExplicit(componentTag)) {
                write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                write("subCodeLength += length.decode(is);");
                explicitEncoding = "null";
            } else {
                explicitEncoding = "berTag";
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write(initChoiceDecodeLength + "choiceDecodeLength = " + getName(componentType) + ".decode(is, " + explicitEncoding + ");");
            if (!isExplicit(componentTag)) {
                initChoiceDecodeLength = "";
            }
            write("if (choiceDecodeLength != 0) {");
            write("subCodeLength += choiceDecodeLength;");
            write("subCodeLength += berTag.decode(is);");
            write("}");
            write("else {");
            write(getName(componentType) + " = null;");
            write("}\n");
            if (isExplicit(componentTag)) {
                write("}");
            }
        } else {
            explicitEncoding = ", false";
            if (componentTag != null) {
                write("if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                if (isExplicit(componentTag)) {
                    write("codeLength += length.decode(is);");
                    explicitEncoding = ", true";
                }
            } else {
                write("if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            write("subCodeLength += berTag.decode(is);");
            write("}");
        }
    }
    write("int nextByte = is.read();");
    write("if (berTag.tagNumber != 0 || berTag.tagClass != 0 || berTag.primitive != 0");
    write("|| nextByte != 0) {");
    write("if (nextByte == -1) {");
    write("throw new EOFException(\"Unexpected end of input stream.\");");
    write("}");
    write("throw new IOException(\"Decoded sequence has wrong end of contents octets\");");
    write("}");
    write("codeLength += subCodeLength + 1;");
    write("return codeLength;");
    write("}\n");
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) BerTag(org.openmuc.jasn1.ber.BerTag) AsnTag(org.openmuc.jasn1.compiler.model.AsnTag) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Aggregations

AsnElementType (org.openmuc.jasn1.compiler.model.AsnElementType)17 AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)16 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)16 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)16 AsnTag (org.openmuc.jasn1.compiler.model.AsnTag)13 BerTag (org.openmuc.jasn1.ber.BerTag)12 AsnParameter (org.openmuc.jasn1.compiler.model.AsnParameter)3 AsnConstructedType (org.openmuc.jasn1.compiler.model.AsnConstructedType)2 IOException (java.io.IOException)1 AsnAny (org.openmuc.jasn1.compiler.model.AsnAny)1 AsnClassNumber (org.openmuc.jasn1.compiler.model.AsnClassNumber)1 AsnSequenceSet (org.openmuc.jasn1.compiler.model.AsnSequenceSet)1