Search in sources :

Example 6 with AsnElementType

use of com.beanit.asn1bean.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("@Override public int encode(OutputStream reverseOS) throws IOException {\n");
    }
    write("if (code != null) {");
    write("reverseOS.write(code);");
    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 (" + getVariableName(componentType) + " != null) {");
        String explicitEncoding = getExplicitEncodingParameter(componentType);
        if (isExplicit(componentTag)) {
            write("sublength = " + getVariableName(componentType) + ".encode(reverseOS" + explicitEncoding + ");");
            write("codeLength += sublength;");
            write("codeLength += BerLength.encodeLength(reverseOS, sublength);");
        } else {
            write("codeLength += " + getVariableName(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");
}
Also used : BerTag(com.beanit.asn1bean.ber.BerTag) AsnTag(com.beanit.asn1bean.compiler.model.AsnTag) AsnCharacterString(com.beanit.asn1bean.compiler.model.AsnCharacterString) AsnOctetString(com.beanit.asn1bean.compiler.model.AsnOctetString) HexString(com.beanit.asn1bean.util.HexString) AsnBitString(com.beanit.asn1bean.compiler.model.AsnBitString) AsnElementType(com.beanit.asn1bean.compiler.model.AsnElementType)

Example 7 with AsnElementType

use of com.beanit.asn1bean.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;
        replaceParametersByAnyTypes(componentTypes, parameters);
    }
    for (AsnElementType componentType : componentTypes) {
        if ((componentType.typeReference instanceof AsnConstructedType)) {
            listOfSubClassNames.add(getClassName(componentType));
        }
    }
    for (AsnElementType componentType : componentTypes) {
        if (isInnerType(componentType)) {
            String subClassName = getClassName(componentType, className);
            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("private byte[] code = null;");
    setClassNamesOfComponents(listOfSubClassNames, componentTypes, className);
    writeMembers(componentTypes);
    if (asnSequenceSet.isSequence && accessExtended && extensibilityImplied) {
        String accessModifierString = jaxbMode ? "private" : "public";
        write(accessModifierString + " byte[] extensionBytes = null;");
    }
    writeEmptyConstructor(className);
    if (jaxbMode) {
        writeGetterAndSetter(componentTypes);
        if (asnSequenceSet.isSequence && accessExtended && extensibilityImplied) {
            write("public byte[] getExtensionBytes() {");
            write("return extensionBytes;");
            write("}\n");
            write("public void setExtensionBytes(byte[] bytes) {");
            write("this.extensionBytes = bytes;");
            write("}\n");
        }
    } else {
        writeEncodeConstructor(className, componentTypes);
    }
    boolean hasExplicitTag = (tag != null) && (tag.type == TagType.EXPLICIT);
    writeSimpleEncodeFunction();
    writeSequenceOrSetEncodeFunction(componentTypes, hasExplicitTag, asnSequenceSet.isSequence);
    writeSimpleDecodeFunction("true");
    if (asnSequenceSet.isSequence) {
        writeSequenceDecodeMethod(convertToComponentInfos(componentTypes), hasExplicitTag);
    } else {
        writeSetDecodeFunction(convertToComponentInfos(componentTypes), hasExplicitTag);
    }
    writeEncodeAndSaveFunction();
    writeSequenceOrSetToStringFunction(componentTypes);
    write("}\n");
}
Also used : AsnConstructedType(com.beanit.asn1bean.compiler.model.AsnConstructedType) AsnParameter(com.beanit.asn1bean.compiler.model.AsnParameter) AsnCharacterString(com.beanit.asn1bean.compiler.model.AsnCharacterString) AsnOctetString(com.beanit.asn1bean.compiler.model.AsnOctetString) HexString(com.beanit.asn1bean.util.HexString) AsnBitString(com.beanit.asn1bean.compiler.model.AsnBitString) BerTag(com.beanit.asn1bean.ber.BerTag) AsnTag(com.beanit.asn1bean.compiler.model.AsnTag) AsnElementType(com.beanit.asn1bean.compiler.model.AsnElementType)

Example 8 with AsnElementType

use of com.beanit.asn1bean.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;
    }
    StringBuilder line = new StringBuilder("public " + className + "(");
    int j = 0;
    for (AsnElementType componentType : componentTypes) {
        if (j != 0) {
            line.append(", ");
        }
        j++;
        line.append(getClassName(componentType)).append(" ").append(cleanUpName(componentType.name));
    }
    write(line + ") {");
    for (AsnElementType componentType : componentTypes) {
        String elementName = cleanUpName(componentType.name);
        write("this." + elementName + " = " + elementName + ";");
    }
    write("}\n");
}
Also used : AsnCharacterString(com.beanit.asn1bean.compiler.model.AsnCharacterString) AsnOctetString(com.beanit.asn1bean.compiler.model.AsnOctetString) HexString(com.beanit.asn1bean.util.HexString) AsnBitString(com.beanit.asn1bean.compiler.model.AsnBitString) AsnElementType(com.beanit.asn1bean.compiler.model.AsnElementType)

Example 9 with AsnElementType

use of com.beanit.asn1bean.compiler.model.AsnElementType in project jasn1 by openmuc.

the class BerClassWriter method getConstructorParametersFromConstructedElement.

private String[] getConstructorParametersFromConstructedElement(AsnConstructedType assignedTypeDefinition) {
    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] = getClassName(componentType);
        constructorParameters[j * 2 + 1] = cleanUpName(componentType.name);
    }
    return constructorParameters;
}
Also used : AsnCharacterString(com.beanit.asn1bean.compiler.model.AsnCharacterString) AsnOctetString(com.beanit.asn1bean.compiler.model.AsnOctetString) HexString(com.beanit.asn1bean.util.HexString) AsnBitString(com.beanit.asn1bean.compiler.model.AsnBitString) AsnElementType(com.beanit.asn1bean.compiler.model.AsnElementType) AsnSequenceSet(com.beanit.asn1bean.compiler.model.AsnSequenceSet)

Example 10 with AsnElementType

use of com.beanit.asn1bean.compiler.model.AsnElementType in project jasn1 by openmuc.

the class BerClassWriter method convertToComponentInfos.

private List<ComponentInfo> convertToComponentInfos(List<AsnElementType> asnElementTypes) {
    int lastRequiredComponentIndex = getLastRequiredComponentIndex(asnElementTypes);
    List<ComponentInfo> componentInfos = new ArrayList<>(asnElementTypes.size());
    int i = 0;
    for (AsnElementType asnElementType : asnElementTypes) {
        boolean mayBeLastComponent = lastRequiredComponentIndex <= i;
        componentInfos.add(convertToComponentInfo(asnElementType, mayBeLastComponent, null));
        i++;
    }
    return componentInfos;
}
Also used : ArrayList(java.util.ArrayList) AsnElementType(com.beanit.asn1bean.compiler.model.AsnElementType)

Aggregations

AsnElementType (com.beanit.asn1bean.compiler.model.AsnElementType)14 AsnBitString (com.beanit.asn1bean.compiler.model.AsnBitString)9 AsnCharacterString (com.beanit.asn1bean.compiler.model.AsnCharacterString)9 AsnOctetString (com.beanit.asn1bean.compiler.model.AsnOctetString)9 HexString (com.beanit.asn1bean.util.HexString)9 AsnTag (com.beanit.asn1bean.compiler.model.AsnTag)5 BerTag (com.beanit.asn1bean.ber.BerTag)4 AsnParameter (com.beanit.asn1bean.compiler.model.AsnParameter)3 AsnConstructedType (com.beanit.asn1bean.compiler.model.AsnConstructedType)2 AsnAny (com.beanit.asn1bean.compiler.model.AsnAny)1 AsnClassNumber (com.beanit.asn1bean.compiler.model.AsnClassNumber)1 AsnSequenceSet (com.beanit.asn1bean.compiler.model.AsnSequenceSet)1 ArrayList (java.util.ArrayList)1