Search in sources :

Example 1 with SymbolsFromModule

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

the class BerClassWriter method writeClassHeader.

private void writeClassHeader(String typeName, AsnModule module) throws IOException {
    outputDirectory.mkdirs();
    FileWriter fstream = new FileWriter(new File(outputDirectory, typeName + ".java"));
    out = new BufferedWriter(fstream);
    write("/**");
    write(" * This class file was automatically generated by jASN1 v" + Compiler.VERSION + " (http://www.openmuc.org)\n */\n");
    write("package " + basePackageName + sanitizeModuleName(module.moduleIdentifier.name).replace('-', '.').toLowerCase() + ";\n");
    write("import java.io.IOException;");
    write("import java.io.EOFException;");
    write("import java.io.InputStream;");
    write("import java.io.OutputStream;");
    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 org.openmuc.jasn1.ber.*;");
    write("import org.openmuc.jasn1.ber.types.*;");
    write("import org.openmuc.jasn1.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(sanitizeModuleName(importedModule.moduleIdentifier.name).replace('-', '.').toLowerCase() + "." + cleanUpName(importedSymbol) + ";");
                }
            }
        }
    }
    Collections.sort(importedClassesFromOtherModules);
    for (String modulePackage : importedClassesFromOtherModules) {
        write("import " + basePackageName + modulePackage);
    }
    write("");
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) File(java.io.File) AsnModule(org.openmuc.jasn1.compiler.model.AsnModule) BufferedWriter(java.io.BufferedWriter) SymbolsFromModule(org.openmuc.jasn1.compiler.model.SymbolsFromModule)

Example 2 with SymbolsFromModule

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

the class BerClassWriter method parseObjectIdentfierValue.

private BerObjectIdentifier parseObjectIdentfierValue(String name, AsnModule module) throws IOException {
    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 = parseObjectIdentfierValue(token, module);
                    if (oid == null) {
                        for (SymbolsFromModule symbolsFromModule : module.importSymbolFromModuleList) {
                            for (String importedTypeName : symbolsFromModule.symbolList) {
                                if (token.equals(importedTypeName)) {
                                    oid = parseObjectIdentfierValue(token, modulesByName.get(symbolsFromModule.modref));
                                }
                            }
                        }
                    }
                    if (oid == null) {
                        throw new IllegalStateException("AsnValueAssigment \"" + 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(org.openmuc.jasn1.compiler.model.AsnInteger) BerObjectIdentifier(org.openmuc.jasn1.ber.types.BerObjectIdentifier) AsnObjectIdentifier(org.openmuc.jasn1.compiler.model.AsnObjectIdentifier) AsnValueAssignment(org.openmuc.jasn1.compiler.model.AsnValueAssignment) ArrayList(java.util.ArrayList) AsnBitString(org.openmuc.jasn1.compiler.model.AsnBitString) AsnCharacterString(org.openmuc.jasn1.compiler.model.AsnCharacterString) AsnOctetString(org.openmuc.jasn1.compiler.model.AsnOctetString) SymbolsFromModule(org.openmuc.jasn1.compiler.model.SymbolsFromModule)

Aggregations

ArrayList (java.util.ArrayList)2 AsnBitString (org.openmuc.jasn1.compiler.model.AsnBitString)2 AsnCharacterString (org.openmuc.jasn1.compiler.model.AsnCharacterString)2 AsnOctetString (org.openmuc.jasn1.compiler.model.AsnOctetString)2 SymbolsFromModule (org.openmuc.jasn1.compiler.model.SymbolsFromModule)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 BerObjectIdentifier (org.openmuc.jasn1.ber.types.BerObjectIdentifier)1 AsnInteger (org.openmuc.jasn1.compiler.model.AsnInteger)1 AsnModule (org.openmuc.jasn1.compiler.model.AsnModule)1 AsnObjectIdentifier (org.openmuc.jasn1.compiler.model.AsnObjectIdentifier)1 AsnValueAssignment (org.openmuc.jasn1.compiler.model.AsnValueAssignment)1