Search in sources :

Example 1 with AsnType

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

the class BerClassWriter method getBerType.

private String getBerType(AsnType asnType) {
    String fullClassName = asnType.getClass().getName();
    String className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
    if (className.equals("AsnCharacterString")) {
        AsnCharacterString asnCharacterString = (AsnCharacterString) asnType;
        if (asnCharacterString.stringtype.equals("ISO646String")) {
            return "BerVisibleString";
        } else if (asnCharacterString.stringtype.equals("T61String")) {
            return "BerTeletexString";
        }
        return "Ber" + ((AsnCharacterString) asnType).stringtype;
    }
    return "Ber" + className.substring(3);
}
Also used : AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString)

Example 2 with AsnType

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

the class BerClassWriter method writeRetaggingTypeClass.

private void writeRetaggingTypeClass(String typeName, String assignedTypeName, AsnType typeDefinition, Tag tag) throws IOException {
    write("public class " + typeName + " extends " + cleanUpName(assignedTypeName) + " {\n");
    write("private static final long serialVersionUID = 1L;\n");
    if (tag != null) {
        write("public static final BerTag tag = new BerTag(" + getBerTagParametersString(tag) + ");\n");
        if (tag.type == TagType.EXPLICIT) {
            write("public byte[] code = null;\n");
        }
    }
    write("public " + typeName + "() {");
    write("}\n");
    String[] constructorParameters = getConstructorParameters(getUniversalType(typeDefinition));
    if (constructorParameters.length != 2 || constructorParameters[0] != "byte[]") {
        write("public " + typeName + "(byte[] code) {");
        write("super(code);");
        write("}\n");
    }
    if ((!jaxbMode || isPrimitiveOrRetaggedPrimitive(typeDefinition)) && (constructorParameters.length != 0)) {
        String constructorParameterString = "";
        String superCallParameterString = "";
        for (int i = 0; i < constructorParameters.length; i += 2) {
            if (i > 0) {
                constructorParameterString += ", ";
                superCallParameterString += ", ";
            }
            constructorParameterString += constructorParameters[i] + " " + constructorParameters[i + 1];
            superCallParameterString += constructorParameters[i + 1];
        }
        write("public " + typeName + "(" + constructorParameterString + ") {");
        write("super(" + superCallParameterString + ");");
        write("}\n");
        if (constructorParameters[0].equals("BigInteger")) {
            write("public " + typeName + "(long value) {");
            write("super(value);");
            write("}\n");
        } else if (constructorParameters.length == 4 && constructorParameters[3].equals("numBits")) {
            write("public " + typeName + "(boolean[] value) {");
            write("super(value);");
            write("}\n");
        }
    }
    if (tag != null) {
        if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
            writeSimpleEncodeFunction();
        }
        write("public int encode(OutputStream reverseOS, boolean withTag) throws IOException {\n");
        if (constructorParameters.length != 2 || constructorParameters[0] != "byte[]") {
            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;\n");
        if (tag.type == TagType.EXPLICIT) {
            if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
                write("codeLength = super.encode(reverseOS);");
            } else {
                write("codeLength = super.encode(reverseOS, true);");
            }
            write("codeLength += BerLength.encodeLength(reverseOS, codeLength);");
        } else {
            write("codeLength = super.encode(reverseOS, false);");
        }
        write("if (withTag) {");
        write("codeLength += tag.encode(reverseOS);");
        write("}\n");
        write("return codeLength;");
        write("}\n");
        if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
            writeSimpleDecodeFunction("true");
        }
        write("public int decode(InputStream is, boolean withTag) throws IOException {\n");
        write("int codeLength = 0;\n");
        write("if (withTag) {");
        write("codeLength += tag.decodeAndCheck(is);");
        write("}\n");
        if (tag.type == TagType.EXPLICIT) {
            write("BerLength length = new BerLength();");
            write("codeLength += length.decode(is);\n");
            if (isDirectAnyOrChoice((AsnTaggedType) typeDefinition)) {
                write("codeLength += super.decode(is, null);\n");
            } else {
                write("codeLength += super.decode(is, true);\n");
            }
        } else {
            write("codeLength += super.decode(is, false);\n");
        }
        write("return codeLength;");
        write("}\n");
    }
    write("}");
}
Also used : AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString)

Example 3 with AsnType

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

the class BerClassWriter method translateModule.

public void translateModule(AsnModule module) throws IOException {
    System.out.println("Generating classes for module \"" + module.moduleIdentifier.name + "\"");
    outputDirectory = new File(outputBaseDir, sanitizeModuleName(module.moduleIdentifier.name).replace('-', '/').toLowerCase());
    this.module = module;
    tagDefault = module.tagDefault;
    for (AsnType typeDefinition : module.typesByName.values()) {
        if (typeDefinition instanceof AsnDefinedType) {
            if (getInformationObjectClass(((AsnDefinedType) typeDefinition).typeName, module) != null) {
                continue;
            }
        }
        String typeName = cleanUpName(typeDefinition.name);
        writeClassHeader(typeName, module);
        if (typeDefinition instanceof AsnTaggedType) {
            AsnTaggedType asnTaggedType = (AsnTaggedType) typeDefinition;
            Tag tag = getTag(asnTaggedType);
            if (asnTaggedType.definedType != null) {
                writeRetaggingTypeClass(typeName, asnTaggedType.definedType.typeName, typeDefinition, tag);
            } else {
                AsnType assignedAsnType = asnTaggedType.typeReference;
                if (assignedAsnType instanceof AsnConstructedType) {
                    writeConstructedTypeClass(typeName, assignedAsnType, tag, false, null);
                } else {
                    writeRetaggingTypeClass(typeName, getBerType(assignedAsnType), typeDefinition, tag);
                }
            }
        } else if (typeDefinition instanceof AsnDefinedType) {
            writeRetaggingTypeClass(typeName, ((AsnDefinedType) typeDefinition).typeName, typeDefinition, null);
        } else if (typeDefinition instanceof AsnConstructedType) {
            writeConstructedTypeClass(typeName, typeDefinition, null, false, null);
        } else {
            writeRetaggingTypeClass(typeName, getBerType(typeDefinition), typeDefinition, null);
        }
        out.close();
    }
    writeOidValues(module);
}
Also used : AsnTaggedType(org.openmuc.jasn1.compiler.model.AsnTaggedType) AsnConstructedType(org.openmuc.jasn1.compiler.model.AsnConstructedType) 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) File(java.io.File) AsnDefinedType(org.openmuc.jasn1.compiler.model.AsnDefinedType) AsnType(org.openmuc.jasn1.compiler.model.AsnType)

Aggregations

AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)3 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)3 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)3 File (java.io.File)1 BerTag (org.openmuc.jasn1.ber.BerTag)1 AsnConstructedType (org.openmuc.jasn1.compiler.model.AsnConstructedType)1 AsnDefinedType (org.openmuc.jasn1.compiler.model.AsnDefinedType)1 AsnTag (org.openmuc.jasn1.compiler.model.AsnTag)1 AsnTaggedType (org.openmuc.jasn1.compiler.model.AsnTaggedType)1 AsnType (org.openmuc.jasn1.compiler.model.AsnType)1