use of org.openmuc.jasn1.compiler.model.AsnModule in project jasn1 by openmuc.
the class BerClassWriter method writeOidValues.
private void writeOidValues(AsnModule module) throws IOException {
boolean first = true;
List<String> values = new ArrayList<>(module.asnValueAssignmentsByName.keySet());
Collections.sort(values);
for (String valueName : values) {
if (module.asnValueAssignmentsByName.get(valueName).type instanceof AsnObjectIdentifier) {
BerObjectIdentifier oid;
try {
oid = parseObjectIdentfierValue(valueName, module);
} catch (IllegalStateException e) {
System.out.println("Warning: could not parse object identifier value: " + e.getMessage());
continue;
}
StringBuilder sb = new StringBuilder("public static final BerObjectIdentifier " + cleanUpName(valueName) + " = new BerObjectIdentifier(new int[]{");
if (first == true) {
first = false;
writeClassHeader("OidValues", module);
write("public final class OidValues {");
}
boolean firstOidComponent = true;
for (int i : oid.value) {
if (firstOidComponent) {
firstOidComponent = false;
} else {
sb.append(", ");
}
sb.append(i);
}
sb.append("});");
write(sb.toString());
}
}
if (first == false) {
write("}");
out.close();
}
}
use of org.openmuc.jasn1.compiler.model.AsnModule 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("");
}
use of org.openmuc.jasn1.compiler.model.AsnModule 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));
}
use of org.openmuc.jasn1.compiler.model.AsnModule in project jasn1 by openmuc.
the class BerClassWriter method translateModule.
public void translateModule(AsnModule module) throws IOException {
System.out.println("Generating classes for module \"" + module.moduleIdentifier.name + "\"");
outputDirectory = new File(outputBaseDir, sanitizeModuleName(module.moduleIdentifier.name).replace('-', '/').toLowerCase());
this.module = module;
tagDefault = module.tagDefault;
for (AsnType typeDefinition : module.typesByName.values()) {
if (typeDefinition instanceof AsnDefinedType) {
if (getInformationObjectClass(((AsnDefinedType) typeDefinition).typeName, module) != null) {
continue;
}
}
String typeName = cleanUpName(typeDefinition.name);
writeClassHeader(typeName, module);
if (typeDefinition instanceof AsnTaggedType) {
AsnTaggedType asnTaggedType = (AsnTaggedType) typeDefinition;
Tag tag = getTag(asnTaggedType);
if (asnTaggedType.definedType != null) {
writeRetaggingTypeClass(typeName, asnTaggedType.definedType.typeName, typeDefinition, tag);
} else {
AsnType assignedAsnType = asnTaggedType.typeReference;
if (assignedAsnType instanceof AsnConstructedType) {
writeConstructedTypeClass(typeName, assignedAsnType, tag, false, null);
} else {
writeRetaggingTypeClass(typeName, getBerType(assignedAsnType), typeDefinition, tag);
}
}
} else if (typeDefinition instanceof AsnDefinedType) {
writeRetaggingTypeClass(typeName, ((AsnDefinedType) typeDefinition).typeName, typeDefinition, null);
} else if (typeDefinition instanceof AsnConstructedType) {
writeConstructedTypeClass(typeName, typeDefinition, null, false, null);
} else {
writeRetaggingTypeClass(typeName, getBerType(typeDefinition), typeDefinition, null);
}
out.close();
}
writeOidValues(module);
}
use of org.openmuc.jasn1.compiler.model.AsnModule 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");
}
Aggregations