/*
* Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.mdsal.binding.javav2.generator.util;
import com.google.common.annotations.Beta;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ListMultimap;
import java.util.List;
import java.util.Set;
import org.opendaylight.mdsal.binding.javav2.model.api.Enumeration;
import org.opendaylight.mdsal.binding.javav2.model.api.Enumeration.Pair;
import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
/**
* This util class converts every non-java char in identifier to java char by
* its unicode name (JAVA SE
* SPECIFICATIONS - Identifiers). There are special types of mapping
* non-java chars to original identifiers according to specific
* {@linkplain JavaIdentifier java type}:
*
* - class, enum, interface
* -
*
* - without special separator
* - the first character of identifier, any other first character of
* identifier part mapped by non-Java char name from unicode and char in
* identifier behind non-java char name are converting to upper case
* - examples:
* -
*
* - example* - ExampleAsterisk
* - example*example - ExampleAserisksExample
* - \example - ReverseSolidusExample
* - 1example - DigitOneExample
* - example1 - Example1
* - int - IntReservedKeyword
* - con - ConReservedKeyword
*
*
*
*
* - enum value, constant
* -
*
* - used underscore as special separator
* - converted identifier to upper case
* - examples:
* -
*
* - example* - EXAMPLE_ASTERISK
* - example*example - EXAMPLE_ASTERISK_EXAMPLE
* - \example - REVERSE_SOLIDUS_EXAMPLE
* - 1example - DIGIT_ONE_EXAMPLE
* - example1 - EXAMPLE1
* - int - INT_RESERVED_KEYWORD
* - con - CON_RESERVED_KEYWORD
*
*
*
*
* - method, variable
* -
*
-
*
* - without special separator
* - the first character of identifier is converting to lower case
* - any other first character of identifier part mapped by non-Java char name
* from unicode and char in identifier behind non-java char name are converting
* to upper case
* - examples:
* -
*
* - example* - exampleAsterisk
* - example*example - exampleAserisksExample
* - \example - reverseSolidusExample
* - 1example - digitOneExample
* - example1 - example1
* - int - intReservedKeyword
* - con - conReservedKeyword
*
*
*
*
* - package - full package name (
* Naming a package)
* -
*
-
*
* - parts of package name are separated by dots
* - parts of package name are converting to lower case
* - if parts of package name are reserved Java or Windows keywords, such as
* 'int' the suggested convention is to add an underscore to keyword
* - dash is parsed as underscore according to
* Naming a package
* - examples:
* -
*
* - org.example* - org.exampleasterisk
* - org.example*example - org.exampleasteriskexample
* - org.\example - org.reversesolidusexample
* - org.1example - org.digitoneexample
* - org.example1 - org.example1
* - org.int - org.int_
* - org.con - org.con_
* - org.foo-cont - org.foo_cont
*
*
*
*
*
*
* There is special case in CLASS, INTERFACE, ENUM, ENUM VALUE, CONSTANT, METHOD
* and VARIABLE if identifier contains single dash - then the converter ignores
* the single dash in the way of the non-java chars. In other way, if dash is
* the first or the last char in the identifier or there is more dashes in a row
* in the identifier, then these dashes are converted as non-java chars.
* Example:
*
* - class, enum, interface
* -
*
* - foo-cont - FooCont
* - foo--cont - FooHyphenMinusHyphenMinusCont
* - -foo - HyphenMinusFoo
* - foo- - FooHyphenMinus
*
*
* - enum value, constant
*
-
*
* - foo-cont - FOO_CONT
* - foo--cont - FOO_HYPHEN_MINUS_HYPHEN_MINUS_CONT
* - -foo - HYPHEN_MINUS_FOO
* - foo- - FOO_HYPHEN_MINUS
*
*
* - method, variable
* -
*
* - foo-cont - fooCont
* - foo--cont - fooHyphenMinusHyphenMinusCont
* - -foo - hyphenMinusFoo
* - foo- - fooHyphenMinus
*
*
*
*
* Next special case talks about normalizing class name which already exists in
* package - but with different camel cases (foo, Foo, fOo, ...). To every next
* classes with same names will by added their actual rank (serial number),
* except the first one. This working for CLASS, ENUM and INTEFACE java
* identifiers. If there exist the same ENUM VALUES in ENUM (with different
* camel cases), then it's parsed with same logic like CLASSES, ENUMS and
* INTERFACES but according to list of pairs of their ENUM parent. Example:
*
*
* - class, enum, interface
* -
*
* - package name org.example, class (or interface or enum) Foo - normalized
* to Foo
*
- package name org.example, class (or interface or enum) fOo - normalized
* to Foo1
*
*
* - enum value
* -
*
*
*
*/
@Beta
public final class JavaIdentifierNormalizer {
private static final int FIRST_CHAR = 0;
private static final int FIRST_INDEX = 1;
private static final char UNDERSCORE = '_';
private static final char DASH = '-';
private static final String EMPTY_STRING = "";
private static final String RESERVED_KEYWORD = "reserved_keyword";
private static final ListMultimap PACKAGES_MAP = ArrayListMultimap.create();
public static final Set SPECIAL_RESERVED_PATHS =
ImmutableSet.of("org.opendaylight.yangtools.yang.common", "org.opendaylight.mdsal.binding.javav2.spec",
"java", "com");
private JavaIdentifierNormalizer() {
throw new UnsupportedOperationException("Util class");
}
/**
*
* According to YANG RFC 7950,
* all assigned names in an enumeration MUST be unique. Created names are contained in the list
* of {@link Enumeration.Pair}. This method adds actual index with underscore behind name of new
* enum value only if this name already exists in one of the list of {@link Enumeration.Pair}.
* Then, the name will be converted to java chars according to {@link JavaIdentifier#ENUM_VALUE}
* and returned.
*
* Example:
*
*
* type enumeration {
* enum foo;
* enum Foo;
* }
*
*
* YANG enum values will be mapped to 'FOO' and 'FOO_1' Java enum values.
*
* @param name
* - name of new enum value
* @param values
* - list of all actual enum values
* @return converted and fixed name of new enum value
*/
public static String normalizeEnumValueIdentifier(final String name, final List values) {
return convertIdentifierEnumValue(name, name, values, FIRST_INDEX);
}
/**
* Normalizing full package name by non java chars and reserved keywords.
*
* @param fullPackageName
* - full package name
* @return normalized name
*/
public static String normalizeFullPackageName(final String fullPackageName) {
final String[] packageNameParts = fullPackageName.split("\\.");
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < packageNameParts.length; i++) {
sb.append(normalizePartialPackageName(packageNameParts[i]));
if (i != (packageNameParts.length - 1)) {
sb.append(".");
}
}
return sb.toString();
}
/**
* Normalizing part of package name by non java chars.
*
* @param packageNamePart
* - part of package name
* @return normalized name
*/
public static String normalizePartialPackageName(final String packageNamePart) {
// if part of package name consist from java or windows reserved word, return it with
// underscore at the end and in lower case
if (BindingMapping.JAVA_RESERVED_WORDS.contains(packageNamePart.toLowerCase())
|| BindingMapping.WINDOWS_RESERVED_WORDS.contains(packageNamePart.toUpperCase())) {
return new StringBuilder(packageNamePart).append(UNDERSCORE).toString().toLowerCase();
}
String normalizedPackageNamePart = packageNamePart;
if (packageNamePart.contains(String.valueOf(DASH))) {
normalizedPackageNamePart = packageNamePart.replaceAll(String.valueOf(DASH), String.valueOf(UNDERSCORE));
}
final StringBuilder sb = new StringBuilder();
StringBuilder innserSb = new StringBuilder();
for (int i = 0; i < normalizedPackageNamePart.length(); i++) {
if (normalizedPackageNamePart.charAt(i) == UNDERSCORE) {
if (!innserSb.toString().isEmpty()) {
sb.append(normalizeSpecificIdentifier(innserSb.toString(), JavaIdentifier.PACKAGE));
innserSb = new StringBuilder();
}
sb.append(UNDERSCORE);
} else {
innserSb.append(normalizedPackageNamePart.charAt(i));
}
}
if (!innserSb.toString().isEmpty()) {
sb.append(normalizeSpecificIdentifier(innserSb.toString(), JavaIdentifier.PACKAGE));
}
// returned normalized part of package name
return sb.toString();
}
/**
* Find and convert non Java chars in identifiers of generated transfer objects, initially
* derived from corresponding YANG according to
* Java
* Specifications - Identifiers. If there is more same class names at the same package, then
* append rank (serial number) to the end of them. Works for class, enum, interface.
*
* @param packageName
* - package of identifier
* @param className
* - name of identifier
* @return - java acceptable identifier
*/
public static String normalizeClassIdentifier(final String packageName, final String className) {
for (final String reservedPath : SPECIAL_RESERVED_PATHS) {
if (packageName.startsWith(reservedPath)) {
return className;
}
}
final String convertedClassName = normalizeSpecificIdentifier(className, JavaIdentifier.CLASS);
return normalizeClassIdentifier(packageName, convertedClassName, convertedClassName, FIRST_INDEX);
}
/**
* Find and convert non Java chars in identifiers of generated transfer objects, initially
* derived from corresponding YANG.
*
* @param identifier
* - name of identifier
* @param javaIdentifier
* - java type of identifier
* @return - java acceptable identifier
*/
public static String normalizeSpecificIdentifier(final String identifier, final JavaIdentifier javaIdentifier) {
final StringBuilder sb = new StringBuilder();
// if identifier isn't PACKAGE type then check it by reserved keywords
if(javaIdentifier != JavaIdentifier.PACKAGE) {
if (BindingMapping.JAVA_RESERVED_WORDS.contains(identifier.toLowerCase())
|| BindingMapping.WINDOWS_RESERVED_WORDS.contains(identifier.toUpperCase())) {
return fixCasesByJavaType(
sb.append(identifier).append(UNDERSCORE).append(RESERVED_KEYWORD).toString().toLowerCase(),
javaIdentifier);
}
}
// check and convert first char in identifier if there is non-java char
final char firstChar = identifier.charAt(FIRST_CHAR);
if (!Character.isJavaIdentifierStart(firstChar)) {
// converting first char of identifier
sb.append(convertFirst(firstChar, existNext(identifier, FIRST_CHAR)));
} else {
sb.append(firstChar);
}
// check and convert other chars in identifier, if there is non-java char
for (int i = 1; i < identifier.length(); i++) {
final char actualChar = identifier.charAt(i);
// ignore single dash as non java char - if there is more dashes in a row or dash is as
// the last char in identifier then parse these dashes as non java chars
if ((actualChar == '-') && existNext(identifier, i)) {
if ((identifier.charAt(i - 1) != DASH) && (identifier.charAt(i + 1) != DASH)) {
sb.append(UNDERSCORE);
continue;
}
}
if (!Character.isJavaIdentifierPart(actualChar)) {
// prepare actual string of sb for checking if underscore exist on position of the
// last char
final String partialConvertedIdentifier = sb.toString();
sb.append(convert(actualChar, existNext(identifier, i),
partialConvertedIdentifier.charAt(partialConvertedIdentifier.length() - 1)));
} else {
sb.append(actualChar);
}
}
// apply camel case in appropriate way
return fixCasesByJavaType(sb.toString().replace("__", "_").toLowerCase(), javaIdentifier);
}
/**
* Checking while there doesn't exist any class name with the same name
* (regardless of camel cases) in package.
*
* @param packageName
* - package of class name
* @param origClassName
* - original class name
* @param actualClassName
* - actual class name with rank (serial number)
* @param rank
* - actual rank (serial number)
* @return converted identifier
*/
private static String normalizeClassIdentifier(final String packageName, final String origClassName,
final String actualClassName, final int rank) {
if (PACKAGES_MAP.containsKey(packageName)) {
for (final String existingName : PACKAGES_MAP.get(packageName)) {
if (existingName.toLowerCase().equals(actualClassName.toLowerCase())) {
final int nextRank = rank + 1;
return normalizeClassIdentifier(packageName, origClassName,
new StringBuilder(origClassName).append(rank).toString(), nextRank);
}
}
}
PACKAGES_MAP.put(packageName, actualClassName);
return actualClassName;
}
/**
* Fix cases of converted identifiers by Java type
*
* @param string
* - converted identifier
* @param javaIdentifier
* - java type of identifier
* @return converted identifier with right cases according to java type
*/
private static String fixCasesByJavaType(final String convertedIdentifier, final JavaIdentifier javaIdentifier) {
switch (javaIdentifier) {
case CLASS:
case ENUM:
case INTERFACE:
return capitalize(fixCases(convertedIdentifier));
case ENUM_VALUE:
case CONSTANT:
return convertedIdentifier.toUpperCase();
case METHOD:
case VARIABLE:
return fixCases(convertedIdentifier);
case PACKAGE:
return convertedIdentifier.replaceAll(String.valueOf(UNDERSCORE), EMPTY_STRING);
default:
throw new IllegalArgumentException("Unknown java type of identifier : " + javaIdentifier.toString());
}
}
/**
* Delete unnecessary chars in converted identifier and apply camel case in appropriate way.
*
* @param convertedIdentifier
* - original converted identifier
* @return resolved identifier
*/
private static String fixCases(final String convertedIdentifier) {
final StringBuilder sb = new StringBuilder();
if (convertedIdentifier.contains(String.valueOf(UNDERSCORE))) {
boolean isFirst = true;
for (final String part : convertedIdentifier.split(String.valueOf(UNDERSCORE))) {
if (isFirst) {
isFirst = false;
sb.append(part);
} else {
sb.append(capitalize(part));
}
}
} else {
sb.append(convertedIdentifier);
}
return sb.toString();
}
/**
* Check if there exist next char in identifier behind actual char position
*
* @param identifier
* - original identifier
* @param actual
* - actual char position
* @return true if there is another char, false otherwise
*/
private static boolean existNext(final String identifier, final int actual) {
return (identifier.length() - 1) < (actual + 1) ? false : true;
}
/**
* Converting first char of identifier. This happen only if this char is
* non-java char
*
* @param c
* - first char
* @param existNext
* - existing of next char behind actual char
* @return converted char
*/
private static String convertFirst(final char c, final boolean existNext) {
String name = Character.getName(c);
if (name.contains(String.valueOf(DASH))) {
name = name.replaceAll(String.valueOf(DASH), String.valueOf(UNDERSCORE));
}
name = existNext ? (name + "_") : name;
return name.contains(" ") ? name.replaceAll(" ", "_") : name;
}
/**
* Converting any char in java identifier, This happen only if this char is
* non-java char
*
* @param c
* - actual char
* @param existNext
* - existing of next char behind actual char
* @param partialLastChar
* - last char of partial converted identifier
* @return converted char
*/
private static String convert(final char c, final boolean existNext, final char partialLastChar) {
return partialLastChar == '_' ? convertFirst(c, existNext) : "_" + convertFirst(c, existNext);
}
/**
* Capitalize input string
*
* @param identifier
* - string to be capitalized
*/
private static String capitalize(final String identifier) {
return identifier.substring(FIRST_CHAR, FIRST_CHAR + 1).toUpperCase() + identifier.substring(1);
}
private static String convertIdentifierEnumValue(final String name, final String origName, final List values,
final int rank) {
String newName = name;
for (final Pair pair : values) {
if (pair.getName().toLowerCase().equals(name.toLowerCase())
|| pair.getMappedName().toLowerCase().equals(name.toLowerCase())) {
int actualRank = rank;
final StringBuilder actualNameBuilder =
new StringBuilder(origName).append(UNDERSCORE).append(actualRank);
newName = convertIdentifierEnumValue(actualNameBuilder.toString(), origName, values, ++actualRank);
}
}
return normalizeSpecificIdentifier(newName, JavaIdentifier.ENUM_VALUE);
}
}