Search in sources :

Example 1 with SymbolsFromModule

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

the class BerClassWriter method parseObjectIdentifierValue.

private BerObjectIdentifier parseObjectIdentifierValue(String name, AsnModule module) {
    AsnValueAssignment valueAssignment = module.asnValueAssignmentsByName.get(name);
    if (valueAssignment == null || !(valueAssignment.type instanceof AsnObjectIdentifier)) {
        return null;
    // throw new IOException(
    // "no object identifier named \"" + name + "\" in module \"" + module.moduleIdentifier.name);
    }
    if (!valueAssignment.value.isValueInBraces) {
        throw new IllegalStateException("value of object identifier \"" + valueAssignment.name + "\" is not defined in braces.");
    }
    List<Integer> oidComponents = new ArrayList<>();
    List<String> tokens = valueAssignment.value.valueInBracesTokens;
    for (int i = 0; i < tokens.size(); i++) {
        String token = tokens.get(i);
        if (Character.isDigit(token.charAt(0))) {
            oidComponents.add(Integer.parseInt(token));
        } else if (Character.isLetter(token.charAt(0))) {
            if ((tokens.size() == i + 1) || !tokens.get(i + 1).equals("(")) {
                // this is either a value reference of another object identifier or a registered name
                if (!parseRegisteredOidComponentName(oidComponents, token)) {
                    BerObjectIdentifier oid = parseObjectIdentifierValue(token, module);
                    if (oid == null) {
                        for (SymbolsFromModule symbolsFromModule : module.importSymbolFromModuleList) {
                            for (String importedTypeName : symbolsFromModule.symbolList) {
                                if (token.equals(importedTypeName)) {
                                    oid = parseObjectIdentifierValue(token, modulesByName.get(symbolsFromModule.modref));
                                }
                            }
                        }
                    }
                    if (oid == null) {
                        throw new IllegalStateException("AsnValueAssignment \"" + token + "\" was not found in module \"" + module.moduleIdentifier.name + "\"");
                    }
                    for (int element : oid.value) {
                        oidComponents.add(element);
                    }
                }
            }
        }
    }
    return new BerObjectIdentifier(toIntArray(oidComponents));
}
Also used : AsnInteger(com.beanit.asn1bean.compiler.model.AsnInteger) BerObjectIdentifier(com.beanit.asn1bean.ber.types.BerObjectIdentifier) AsnObjectIdentifier(com.beanit.asn1bean.compiler.model.AsnObjectIdentifier) AsnValueAssignment(com.beanit.asn1bean.compiler.model.AsnValueAssignment) ArrayList(java.util.ArrayList) 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) SymbolsFromModule(com.beanit.asn1bean.compiler.model.SymbolsFromModule)

Example 2 with SymbolsFromModule

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

the class BerClassWriter method writeClassHeader.

private void writeClassHeader(String typeName, AsnModule module) throws IOException {
    // noinspection ResultOfMethodCallIgnored
    outputDirectory.mkdirs();
    Writer fileWriter = Files.newBufferedWriter(new File(outputDirectory, typeName + ".java").toPath(), UTF_8);
    out = new BufferedWriter(fileWriter);
    String versionString = "";
    if (insertVersion) {
        versionString = " v" + Compiler.VERSION;
    }
    write("/*");
    write(" * This class file was automatically generated by ASN1bean" + versionString + " (http://www.beanit.com)\n */\n");
    write("package " + basePackageName + moduleToPackageName(module.moduleIdentifier.name) + ";\n");
    write("import java.io.IOException;");
    write("import java.io.EOFException;");
    write("import java.io.InputStream;");
    write("import java.io.OutputStream;");
    write("import java.io.ByteArrayOutputStream;");
    write("import java.util.List;");
    write("import java.util.ArrayList;");
    write("import java.util.Iterator;");
    write("import java.io.UnsupportedEncodingException;");
    write("import java.math.BigInteger;");
    write("import java.io.Serializable;");
    write("import com.beanit.asn1bean.ber.*;");
    write("import com.beanit.asn1bean.ber.types.*;");
    write("import com.beanit.asn1bean.ber.types.string.*;\n");
    List<String> importedClassesFromOtherModules = new ArrayList<>();
    for (SymbolsFromModule symbolsFromModule : module.importSymbolFromModuleList) {
        AsnModule importedModule = modulesByName.get(symbolsFromModule.modref);
        for (String importedSymbol : symbolsFromModule.symbolList) {
            if (Character.isUpperCase(importedSymbol.charAt(0))) {
                if (importedModule.typesByName.get(importedSymbol) != null) {
                    importedClassesFromOtherModules.add(moduleToPackageName(importedModule.moduleIdentifier.name) + "." + cleanUpName(importedSymbol) + ";");
                }
            }
        }
    }
    Collections.sort(importedClassesFromOtherModules);
    for (String modulePackage : importedClassesFromOtherModules) {
        write("import " + basePackageName + modulePackage);
    }
    write("");
}
Also used : ArrayList(java.util.ArrayList) 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) File(java.io.File) AsnModule(com.beanit.asn1bean.compiler.model.AsnModule) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter) SymbolsFromModule(com.beanit.asn1bean.compiler.model.SymbolsFromModule)

Aggregations

AsnBitString (com.beanit.asn1bean.compiler.model.AsnBitString)2 AsnCharacterString (com.beanit.asn1bean.compiler.model.AsnCharacterString)2 AsnOctetString (com.beanit.asn1bean.compiler.model.AsnOctetString)2 SymbolsFromModule (com.beanit.asn1bean.compiler.model.SymbolsFromModule)2 HexString (com.beanit.asn1bean.util.HexString)2 ArrayList (java.util.ArrayList)2 BerObjectIdentifier (com.beanit.asn1bean.ber.types.BerObjectIdentifier)1 AsnInteger (com.beanit.asn1bean.compiler.model.AsnInteger)1 AsnModule (com.beanit.asn1bean.compiler.model.AsnModule)1 AsnObjectIdentifier (com.beanit.asn1bean.compiler.model.AsnObjectIdentifier)1 AsnValueAssignment (com.beanit.asn1bean.compiler.model.AsnValueAssignment)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 Writer (java.io.Writer)1