Search in sources :

Example 1 with AsnElementType

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

the class BerClassWriter method writeSequenceOrSetOfDecodeFunction.

private void writeSequenceOrSetOfDecodeFunction(AsnElementType componentType, String classNameOfSequenceOfElement, boolean hasExplicitTag, boolean isSequence) throws IOException {
    Tag componentTag = getTag(componentType);
    write("public int decode(InputStream is, boolean withTag) throws IOException {");
    write("int codeLength = 0;");
    write("int subCodeLength = 0;");
    if (componentTag != null || supportIndefiniteLength) {
        write("BerTag berTag = new BerTag();");
    }
    write("if (withTag) {");
    write("codeLength += tag.decodeAndCheck(is);");
    write("}\n");
    write("BerLength length = new BerLength();");
    write("codeLength += length.decode(is);");
    write("int totalLength = length.val;\n");
    if (supportIndefiniteLength) {
        writeSequenceOfDecodeIndefiniteLenghtPart(componentType, classNameOfSequenceOfElement, hasExplicitTag);
    }
    if (hasExplicitTag) {
        write("int nextByte = is.read();");
        write("if (nextByte == -1) {");
        write("throw new EOFException(\"Unexpected end of input stream.\");");
        write("}");
        if (isSequence) {
            write("if (nextByte != (0x30)) {");
        } else {
            write("if (nextByte != (0x31)) {");
        }
        write("throw new IOException(\"Tag does not match!\");");
        write("}");
        write("length.decode(is);");
        write("totalLength = length.val;\n");
    }
    write("while (subCodeLength < totalLength) {");
    write(classNameOfSequenceOfElement + " element = new " + classNameOfSequenceOfElement + "();");
    String explicitEncoding = getExplicitDecodingParameter(componentType);
    if (componentTag != null) {
        if (componentTag.type == TagType.EXPLICIT) {
            write("subCodeLength += berTag.decode(is);");
            write("subCodeLength += length.decode(is);");
            explicitEncoding = ", true";
        } else {
            write("subCodeLength += berTag.decode(is);");
            if (isDirectAnyOrChoice(componentType)) {
                explicitEncoding = ", berTag";
            } else {
                explicitEncoding = ", false";
            }
        }
        write("subCodeLength += element.decode(is" + explicitEncoding + ");");
    } else {
        if (isDirectAnyOrChoice(componentType)) {
            write("subCodeLength += element.decode(is, null);");
        } else {
            write("subCodeLength += element.decode(is, true);");
        }
    }
    write("seqOf.add(element);");
    write("}");
    write("if (subCodeLength != totalLength) {");
    write("throw new IOException(\"Decoded SequenceOf or SetOf has wrong length. Expected \" + totalLength + \" but has \" + subCodeLength);\n");
    write("}");
    write("codeLength += subCodeLength;\n");
    write("return codeLength;");
    write("}\n");
}
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 2 with AsnElementType

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

the class BerClassWriter method writeSequenceDecodeIndefiniteLenghtPart.

private void writeSequenceDecodeIndefiniteLenghtPart(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)

Example 3 with AsnElementType

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

the class BerClassWriter method writeChoiceClass.

private void writeChoiceClass(String className, AsnChoice asn1TypeElement, 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");
    write("public byte[] code = null;");
    if (tag != null) {
        write("public static final BerTag tag = new BerTag(" + getBerTagParametersString(tag) + ");\n");
    }
    List<AsnElementType> componentTypes = asn1TypeElement.componentTypes;
    addAutomaticTagsIfNeeded(componentTypes);
    if (asn1TypeElement.parameters != null) {
        List<AsnParameter> parameters = asn1TypeElement.parameters;
        replaceParamtersByAnyTypes(componentTypes, parameters);
    }
    for (AsnElementType componentType : componentTypes) {
        if (componentType.typeReference != null && (componentType.typeReference instanceof AsnConstructedType)) {
            listOfSubClassNames.add(getClassNameOfComponent(componentType));
        }
    }
    for (AsnElementType componentType : componentTypes) {
        if (isInnerType(componentType)) {
            String subClassName = getClassNameOfComponent(componentType);
            writeConstructedTypeClass(subClassName, componentType.typeReference, null, true, listOfSubClassNames);
        }
    }
    setClassNamesOfComponents(listOfSubClassNames, componentTypes);
    writePublicMembers(componentTypes);
    writeEmptyConstructor(className);
    if (!jaxbMode) {
        writeEncodeConstructor(className, componentTypes);
    }
    if (jaxbMode) {
        writeGetterAndSetter(componentTypes);
    }
    writeChoiceEncodeFunction(componentTypes, tag != null);
    writeChoiceDecodeFunction(componentTypes, tag != null);
    writeEncodeAndSaveFunction(tag == null);
    writeChoiceToStringFunction(componentTypes);
    write("}\n");
}
Also used : AsnConstructedType(org.openmuc.jasn1.compiler.model.AsnConstructedType) AsnParameter(org.openmuc.jasn1.compiler.model.AsnParameter) 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 4 with AsnElementType

use of org.openmuc.jasn1.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;
        replaceParamtersByAnyTypes(componentTypes, parameters);
    }
    for (AsnElementType componentType : componentTypes) {
        if (componentType.typeReference != null && (componentType.typeReference instanceof AsnConstructedType)) {
            listOfSubClassNames.add(getClassNameOfComponent(componentType));
        }
    }
    for (AsnElementType componentType : componentTypes) {
        if (isInnerType(componentType)) {
            String subClassName = getClassNameOfComponent(componentType);
            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("public byte[] code = null;");
    setClassNamesOfComponents(listOfSubClassNames, componentTypes);
    writePublicMembers(componentTypes);
    writeEmptyConstructor(className);
    if (!jaxbMode) {
        writeEncodeConstructor(className, componentTypes);
    }
    if (jaxbMode) {
        writeGetterAndSetter(componentTypes);
    }
    boolean hasExplicitTag = (tag != null) && (tag.type == TagType.EXPLICIT);
    writeSimpleEncodeFunction();
    writeSequenceOrSetEncodeFunction(componentTypes, hasExplicitTag, asnSequenceSet.isSequence);
    writeSimpleDecodeFunction("true");
    if (asnSequenceSet.isSequence) {
        writeSequenceDecodeFunction(componentTypes, hasExplicitTag);
    } else {
        writeSetDecodeFunction(componentTypes);
    }
    writeEncodeAndSaveFunction();
    writeSequenceOrSetToStringFunction(componentTypes);
    write("}\n");
}
Also used : AsnConstructedType(org.openmuc.jasn1.compiler.model.AsnConstructedType) AsnParameter(org.openmuc.jasn1.compiler.model.AsnParameter) 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 5 with AsnElementType

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

the class BerClassWriter method getConstructorParametersFromConstructedElement.

private String[] getConstructorParametersFromConstructedElement(AsnConstructedType assignedTypeDefinition) throws IOException {
    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] = getClassNameOfComponent(componentType);
        constructorParameters[j * 2 + 1] = cleanUpName(componentType.name);
    }
    return constructorParameters;
}
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) AsnSequenceSet(org.openmuc.jasn1.compiler.model.AsnSequenceSet)

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