Search in sources :

Example 16 with AsnElementType

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

the class BerClassWriter method replaceParamtersByAnyTypes.

private void replaceParamtersByAnyTypes(List<AsnElementType> componentTypes, List<AsnParameter> parameters) {
    for (AsnParameter parameter : parameters) {
        if (parameter.paramGovernor == null) {
            for (AsnElementType componentType : componentTypes) {
                if (componentType.definedType != null && componentType.definedType.typeName.equals(parameter.dummyReference)) {
                    componentType.typeReference = new AsnAny();
                    componentType.definedType = null;
                    componentType.isDefinedType = false;
                }
            }
        }
    }
}
Also used : AsnAny(org.openmuc.jasn1.compiler.model.AsnAny) AsnParameter(org.openmuc.jasn1.compiler.model.AsnParameter) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 17 with AsnElementType

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

the class BerClassWriter method writeSetDecodeFunction.

private void writeSetDecodeFunction(List<AsnElementType> componentTypes) 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) {
        writeSetDecodeIndefiniteLenghtPart(componentTypes);
    }
    write("while (subCodeLength < totalLength) {");
    write("subCodeLength += berTag.decode(is);");
    for (int j = 0; j < componentTypes.size(); j++) {
        AsnElementType componentType = componentTypes.get(j);
        Tag componentTag = getTag(componentType);
        String explicitEncoding = ", false";
        String elseString = "";
        if (j != 0) {
            elseString = "else ";
        }
        if (isDirectAnyOrChoice(componentType)) {
            if (!isExplicit(componentTag)) {
                throw new IOException("choice or ANY within set has no explicit tag.");
            }
            write(elseString + "if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
            write("subCodeLength += new BerLength().decode(is);");
            explicitEncoding = ", null";
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            write("}");
        } else {
            if (componentTag != null) {
                if (isExplicit(componentTag)) {
                    write(elseString + "if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                } else {
                    write(elseString + "if (berTag.equals(" + getBerTagParametersString(componentTag) + ")) {");
                }
                if (isExplicit(componentTag)) {
                    write("subCodeLength += new BerLength().decode(is);");
                    explicitEncoding = ", true";
                }
            } else {
                write(elseString + "if (berTag.equals(" + getClassNameOfComponent(componentType) + ".tag)) {");
            }
            write(getName(componentType) + " = new " + getClassNameOfComponent(componentType) + "();");
            if (", null".equals(explicitEncoding)) {
                write("BerLength length2 = new BerLength();");
                write("subCodeLength += length2.decode(is);");
            }
            write("subCodeLength += " + getName(componentType) + ".decode(is" + explicitEncoding + ");");
            write("}");
        }
    }
    write("}");
    write("if (subCodeLength != totalLength) {");
    write("throw new IOException(\"Length of set does not match length tag, length tag: \" + totalLength + \", actual set length: \" + 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) IOException(java.io.IOException) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 18 with AsnElementType

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

the class BerClassWriter method writeSequenceOfClass.

private void writeSequenceOfClass(String className, AsnSequenceOf asnSequenceOf, 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");
    AsnElementType componentType = asnSequenceOf.componentType;
    String referencedTypeName = getClassNameOfSequenceOfElement(componentType, listOfSubClassNames);
    if (isInnerType(componentType)) {
        writeConstructedTypeClass(referencedTypeName, componentType.typeReference, null, true, listOfSubClassNames);
    }
    Tag mainTag;
    if (tag == null) {
        if (asnSequenceOf.isSequenceOf) {
            mainTag = stdSeqTag;
        } else {
            mainTag = stdSetTag;
        }
    } else {
        mainTag = tag;
    }
    write("public static final BerTag tag = new BerTag(" + getBerTagParametersString(mainTag) + ");");
    write("public byte[] code = null;");
    if (jaxbMode) {
        write("private List<" + referencedTypeName + "> seqOf = null;\n");
    } else {
        write("public List<" + referencedTypeName + "> seqOf = null;\n");
    }
    write("public " + className + "() {");
    write("seqOf = new ArrayList<" + referencedTypeName + ">();");
    write("}\n");
    write("public " + className + "(byte[] code) {");
    write("this.code = code;");
    write("}\n");
    if (!jaxbMode) {
        write("public " + className + "(List<" + referencedTypeName + "> seqOf) {");
        write("this.seqOf = seqOf;");
        write("}\n");
    }
    if (jaxbMode) {
        writeGetterForSeqOf(referencedTypeName);
    }
    boolean hasExplicitTag = (tag != null) && (tag.type == TagType.EXPLICIT);
    writeSimpleEncodeFunction();
    writeSequenceOfEncodeFunction(componentType, hasExplicitTag, asnSequenceOf.isSequenceOf);
    writeSimpleDecodeFunction("true");
    writeSequenceOrSetOfDecodeFunction(componentType, referencedTypeName, hasExplicitTag, asnSequenceOf.isSequenceOf);
    writeEncodeAndSaveFunction();
    writeSequenceOrSetOfToStringFunction(referencedTypeName, componentType);
    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 19 with AsnElementType

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

the class BerClassWriter method writeSequenceOrSetEncodeFunction.

private void writeSequenceOrSetEncodeFunction(List<AsnElementType> componentTypes, 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;");
    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);
        if (isOptional(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 (isOptional(componentType)) {
            write("}");
        }
        write("");
    }
    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;\n");
    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) AsnElementType(org.openmuc.jasn1.compiler.model.AsnElementType)

Example 20 with AsnElementType

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

the class BerClassWriter method writeChoiceToStringFunction.

private void writeChoiceToStringFunction(List<AsnElementType> componentTypes) throws IOException {
    writeToStringFunction();
    write("public void appendAsString(StringBuilder sb, int indentLevel) {\n");
    for (int j = 0; j < componentTypes.size(); j++) {
        AsnElementType componentType = componentTypes.get(j);
        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) + ");");
        }
        write("return;");
        write("}\n");
    }
    write("sb.append(\"<none>\");");
    write("}\n");
}
Also used : 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