X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fbinding-generator-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fbinding%2Fgenerator%2Futil%2FBindingGeneratorUtil.java;h=a394edd906bc50de5f71d8a09eb54b393b3c42d4;hp=1fb862291f19700b673a153112bf6f4352d02553;hb=a954bf75193626833a2bd8b7395514a8b1aec032;hpb=b571c5f3e6e9b2dcf2cff2314537c7b1c61533b4 diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java index 1fb862291f..a394edd906 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-util/src/main/java/org/opendaylight/controller/binding/generator/util/BindingGeneratorUtil.java @@ -1,28 +1,28 @@ package org.opendaylight.controller.binding.generator.util; +import java.util.*; + import org.opendaylight.controller.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl; import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder; import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.model.api.*; +import org.opendaylight.controller.yang.model.api.Module; +import org.opendaylight.controller.yang.model.api.SchemaNode; +import org.opendaylight.controller.yang.model.api.SchemaPath; +import org.opendaylight.controller.yang.model.api.TypeDefinition; -import java.util.*; +public final class BindingGeneratorUtil { -public class BindingGeneratorUtil { + private static final String[] SET_VALUES = new String[] { "abstract", "assert", "boolean", "break", "byte", "case", + "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum", "extends", + "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", + "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", + "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", + "true", "try", "void", "volatile", "while" }; - private static final String[] SET_VALUES = new String[]{"abstract", - "assert", "boolean", "break", "byte", "case", "catch", "char", - "class", "const", "continue", "default", "double", "do", "else", - "enum", "extends", "false", "final", "finally", "float", "for", - "goto", "if", "implements", "import", "instanceof", "int", - "interface", "long", "native", "new", "null", "package", "private", - "protected", "public", "return", "short", "static", "strictfp", - "super", "switch", "synchronized", "this", "throw", "throws", - "transient", "true", "try", "void", "volatile", "while"}; - - private BindingGeneratorUtil() {} + private BindingGeneratorUtil() { + } - public static final Set JAVA_RESERVED_WORDS = new HashSet( - Arrays.asList(SET_VALUES)); + public static final Set JAVA_RESERVED_WORDS = new HashSet(Arrays.asList(SET_VALUES)); public static String validateJavaPackage(final String packageName) { if (packageName != null) { @@ -53,16 +53,12 @@ public class BindingGeneratorUtil { return parameterName; } - public static GeneratedTOBuilder schemaNodeToTransferObjectBuilder( - final String packageName, final SchemaNode schemaNode, - final String transObjectName) { - if (packageName != null && schemaNode != null - && transObjectName != null) { + public static GeneratedTOBuilder schemaNodeToTransferObjectBuilder(final String packageName, + final SchemaNode schemaNode, final String transObjectName) { + if (packageName != null && schemaNode != null && transObjectName != null) { - final String genTOName = BindingGeneratorUtil - .parseToClassName(transObjectName); - final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl( - packageName, genTOName); + final String genTOName = BindingGeneratorUtil.parseToClassName(transObjectName); + final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(packageName, genTOName); return newType; @@ -70,14 +66,12 @@ public class BindingGeneratorUtil { return null; } - public static String moduleNamespaceToPackageName( - final Module module) { + public static String moduleNamespaceToPackageName(final Module module) { final StringBuilder packageNameBuilder = new StringBuilder(); final Calendar calendar = Calendar.getInstance(); if (module.getRevision() == null) { - throw new IllegalArgumentException("Module " + module.getName() - + " does not specify revision date!"); + throw new IllegalArgumentException("Module " + module.getName() + " does not specify revision date!"); } packageNameBuilder.append("org.opendaylight.yang.gen.v"); packageNameBuilder.append(module.getYangVersion()); @@ -108,24 +102,41 @@ public class BindingGeneratorUtil { return validateJavaPackage(packageNameBuilder.toString()); } - public static String packageNameForGeneratedType( - final String basePackageName, final SchemaPath schemaPath) { + public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) { + if (basePackageName == null) { + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); + } + if (schemaPath == null) { + throw new IllegalArgumentException("Schema Path cannot be NULL!"); + } + final StringBuilder builder = new StringBuilder(); builder.append(basePackageName); - if ((schemaPath != null) && (schemaPath.getPath() != null)) { - final List pathToNode = schemaPath.getPath(); - final int traversalSteps = (pathToNode.size() - 1); - for (int i = 0; i < traversalSteps; ++i) { - builder.append("."); - String nodeLocalName = pathToNode.get(i).getLocalName(); - - nodeLocalName = nodeLocalName.replace(":", "."); - nodeLocalName = nodeLocalName.replace("-", "."); - builder.append(nodeLocalName); - } - return validateJavaPackage(builder.toString()); + final List pathToNode = schemaPath.getPath(); + final int traversalSteps = (pathToNode.size() - 1); + for (int i = 0; i < traversalSteps; ++i) { + builder.append("."); + String nodeLocalName = pathToNode.get(i).getLocalName(); + + nodeLocalName = nodeLocalName.replace(":", "."); + nodeLocalName = nodeLocalName.replace("-", "."); + builder.append(nodeLocalName); } - return null; + return validateJavaPackage(builder.toString()); + } + + public static String packageNameForTypeDefinition(final String basePackageName, + final TypeDefinition typeDefinition) { + if (basePackageName == null) { + throw new IllegalArgumentException("Base Package Name cannot be NULL!"); + } + if (typeDefinition == null) { + throw new IllegalArgumentException("Type Definition reference cannot be NULL!"); + } + + final StringBuilder builder = new StringBuilder(); + builder.append(basePackageName); + return validateJavaPackage(builder.toString()); } public static String parseToClassName(String token) { @@ -148,6 +159,13 @@ public class BindingGeneratorUtil { return validateParameterName(correctStr); } + public static String convertToCapitalLetters(final String token) { + String convertedStr = token.replace(" ", "_"); + convertedStr = convertedStr.replace(".", "_"); + convertedStr = convertedStr.toUpperCase(); + return convertedStr; + } + private static String parseToCamelCase(String token) { if (token == null) { throw new NullPointerException("Name can not be null"); @@ -174,11 +192,9 @@ public class BindingGeneratorUtil { // check if 'toBeRemoved' character is not the only character in // 'text' if (sb.length() == 0) { - throw new IllegalArgumentException("Name can not be '" - + toBeRemoved + "'"); + throw new IllegalArgumentException("Name can not be '" + toBeRemoved + "'"); } - String replacement = String.valueOf(sb.charAt(toBeRemovedPos)) - .toUpperCase(); + String replacement = String.valueOf(sb.charAt(toBeRemovedPos)).toUpperCase(); sb.setCharAt(toBeRemovedPos, replacement.charAt(0)); toBeRemovedPos = sb.indexOf(toBeRemoved); }