Search in sources :

Example 6 with Name

use of org.openmuc.jasn1.compiler.x690_ber_example.Name 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)

Example 7 with Name

use of org.openmuc.jasn1.compiler.x690_ber_example.Name in project jasn1 by openmuc.

the class Compiler method main.

public static void main(String[] args) throws Exception {
    StringCliParameter outputBaseDir = new CliParameterBuilder("-o").setDescription("The base directory for the generated Java classes. The class files will be saved in subfolders of the base directory corresponding to the name of the defined modules.").buildStringParameter("output_base_dir", "./");
    StringCliParameter basePackageName = new CliParameterBuilder("-p").setDescription("The base package name. Added to this will be a name generated from the module name.").buildStringParameter("package_base_name", "");
    FlagCliParameter supportIndefiniteLength = new CliParameterBuilder("-il").setDescription("Add support for decoding indefinite length in generated classes. This feature is not yet fully implemented and should be used with caution.").buildFlagParameter();
    FlagCliParameter disableBerTypeInterface = new CliParameterBuilder("-di").setDescription("By default generated classes implement the BerType interface. Using this flag this behavior can be disabled.").buildFlagParameter();
    FlagCliParameter legacyMode = new CliParameterBuilder("-l").setDescription("Enable legacy mode. Earlier versions of the jASN1 compiler generated classes that had public member variables instead of getters and setters. This flag enables the old kind of classes.").buildFlagParameter();
    StringListCliParameter asn1Files = new CliParameterBuilder("-f").setMandatory().setDescription("ASN.1 files defining one or more modules.").buildStringListParameter("file");
    List<CliParameter> cliParameters = new ArrayList<>();
    cliParameters.add(asn1Files);
    cliParameters.add(outputBaseDir);
    cliParameters.add(basePackageName);
    cliParameters.add(supportIndefiniteLength);
    cliParameters.add(disableBerTypeInterface);
    cliParameters.add(legacyMode);
    CliParser cliParser = new CliParser("jasn1-compiler", "The compiler reads the ASN.1 definitions from the given files and generates corresponding Java classes that can be used to conveniently encode and decode BER data. v" + VERSION);
    cliParser.addParameters(cliParameters);
    try {
        cliParser.parseArguments(args);
    } catch (CliParseException e1) {
        System.err.println("Error parsing command line parameters: " + e1.getMessage());
        System.out.println(cliParser.getUsageString());
        System.exit(1);
    }
    System.out.println("Generated code will be saved in " + outputBaseDir.getValue());
    if (supportIndefiniteLength.isSelected()) {
        System.out.println("Java classes will support decoding indefinite length.");
    }
    HashMap<String, AsnModule> modulesByName = new HashMap<>();
    for (String asn1File : asn1Files.getValue()) {
        System.out.println("Parsing \"" + asn1File + "\"");
        AsnModel model = getJavaModelFromAsn1File(asn1File);
        modulesByName.putAll(model.modulesByName);
    }
    BerClassWriter classWriter = new BerClassWriter(modulesByName, outputBaseDir.getValue(), basePackageName.getValue(), !legacyMode.isSelected(), supportIndefiniteLength.isSelected(), disableBerTypeInterface.isSelected());
    classWriter.translate();
    System.out.println("done");
}
Also used : StringListCliParameter(org.openmuc.jasn1.compiler.cli.StringListCliParameter) StringCliParameter(org.openmuc.jasn1.compiler.cli.StringCliParameter) FlagCliParameter(org.openmuc.jasn1.compiler.cli.FlagCliParameter) CliParameter(org.openmuc.jasn1.compiler.cli.CliParameter) StringListCliParameter(org.openmuc.jasn1.compiler.cli.StringListCliParameter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CliParameterBuilder(org.openmuc.jasn1.compiler.cli.CliParameterBuilder) FlagCliParameter(org.openmuc.jasn1.compiler.cli.FlagCliParameter) AsnModule(org.openmuc.jasn1.compiler.model.AsnModule) AsnModel(org.openmuc.jasn1.compiler.model.AsnModel) CliParser(org.openmuc.jasn1.compiler.cli.CliParser) CliParseException(org.openmuc.jasn1.compiler.cli.CliParseException) StringCliParameter(org.openmuc.jasn1.compiler.cli.StringCliParameter)

Example 8 with Name

use of org.openmuc.jasn1.compiler.x690_ber_example.Name in project jasn1 by openmuc.

the class Jasn1Sample method main.

public static void main(String[] args) throws IOException {
    ReverseByteArrayOutputStream os = new ReverseByteArrayOutputStream(1000);
    // Name name = new Name(new BerVisibleString("John"), new
    // BerVisibleString("P"), new BerVisibleString("Smith"));
    // instead of creating the Name object as in previous statement you can
    // assign the byte code directly as in the following statement. The
    // encode function of the name object will then simply insert this byte
    // array in the BerOutputStream. This can speed up things if the code
    // for certain structures is known and does not change.
    Name name = new Name();
    name.code = new byte[] { (byte) 0x10, (byte) 0x1A, (byte) 0x04, (byte) 0x4a, (byte) 0x6f, (byte) 0x68, (byte) 0x6e, (byte) 0x1A, (byte) 0x01, (byte) 0x50, (byte) 0x1A, (byte) 0x05, (byte) 0x53, (byte) 0x6d, (byte) 0x69, (byte) 0x74, (byte) 0x68 };
    BerVisibleString title = new BerVisibleString("Director".getBytes());
    EmployeeNumber number = new EmployeeNumber(51);
    Date dateOfHire = new Date("19710917".getBytes());
    Name nameOfSpouse = new Name();
    nameOfSpouse.setGivenName(new BerVisibleString("Mary"));
    nameOfSpouse.setInitial(new BerVisibleString("T"));
    nameOfSpouse.setFamilyName(new BerVisibleString("Smith"));
    Name child1Name = new Name();
    child1Name.setGivenName(new BerVisibleString("Ralph"));
    child1Name.setInitial(new BerVisibleString("T"));
    child1Name.setFamilyName(new BerVisibleString("Smith"));
    ChildInformation child1 = new ChildInformation();
    child1.setName(child1Name);
    child1.setDateOfBirth(new Date("19571111".getBytes()));
    // encodeAndSave will start the encoding and save the result in
    // child1.code. This is useful if the same structure will have to be
    // encoded several times as part of different structures. Using this
    // function will make sure that the real encoding is only done once.
    child1.encodeAndSave(80);
    Name child2Name = new Name();
    child2Name.setGivenName(new BerVisibleString("Susan"));
    child2Name.setInitial(new BerVisibleString("B"));
    child2Name.setFamilyName(new BerVisibleString("Jones"));
    ChildInformation child2 = new ChildInformation();
    child2.setName(child2Name);
    child2.setDateOfBirth(new Date("19590717".getBytes()));
    PersonnelRecord.Children childrenSeq = new PersonnelRecord.Children();
    List<ChildInformation> childList = childrenSeq.getChildInformation();
    childList.add(child1);
    childList.add(child2);
    PersonnelRecord personnelRecord = new PersonnelRecord();
    personnelRecord.setName(name);
    personnelRecord.setTitle(title);
    personnelRecord.setNumber(number);
    personnelRecord.setDateOfHire(dateOfHire);
    personnelRecord.setNameOfSpouse(nameOfSpouse);
    personnelRecord.setChildren(childrenSeq);
    personnelRecord.encode(os);
    System.out.println("Encoded bytes:");
    System.out.println(DatatypeConverter.printHexBinary(os.getArray()));
    InputStream is = new ByteArrayInputStream(os.getArray());
    PersonnelRecord personnelRecord_decoded = new PersonnelRecord();
    personnelRecord_decoded.decode(is);
    System.out.println("\nDecoded structure:");
    System.out.println(personnelRecord_decoded);
    System.out.println("Given name = " + personnelRecord_decoded.getName().getGivenName());
}
Also used : ChildInformation(org.openmuc.jasn1.compiler.x690_ber_example.ChildInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EmployeeNumber(org.openmuc.jasn1.compiler.x690_ber_example.EmployeeNumber) BerVisibleString(org.openmuc.jasn1.ber.types.string.BerVisibleString) PersonnelRecord(org.openmuc.jasn1.compiler.x690_ber_example.PersonnelRecord) Date(org.openmuc.jasn1.compiler.x690_ber_example.Date) ReverseByteArrayOutputStream(org.openmuc.jasn1.ber.ReverseByteArrayOutputStream) Name(org.openmuc.jasn1.compiler.x690_ber_example.Name)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)3 ReverseByteArrayOutputStream (org.openmuc.jasn1.ber.ReverseByteArrayOutputStream)3 BerVisibleString (org.openmuc.jasn1.ber.types.string.BerVisibleString)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)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 ChildInformation (org.openmuc.jasn1.compiler.x690_ber_example.ChildInformation)2 Date (org.openmuc.jasn1.compiler.x690_ber_example.Date)2 EmployeeNumber (org.openmuc.jasn1.compiler.x690_ber_example.EmployeeNumber)2 Name (org.openmuc.jasn1.compiler.x690_ber_example.Name)2 PersonnelRecord (org.openmuc.jasn1.compiler.x690_ber_example.PersonnelRecord)2 EOFException (java.io.EOFException)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 BerEmbeddedPdv (org.openmuc.jasn1.ber.types.BerEmbeddedPdv)1 BerInteger (org.openmuc.jasn1.ber.types.BerInteger)1