2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.javav2.generator.util;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.CharMatcher;
12 import com.google.common.base.Splitter;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ListMultimap;
15 import java.util.Iterator;
16 import java.util.List;
18 import java.util.stream.Collectors;
19 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
20 import org.opendaylight.mdsal.binding.javav2.model.api.Enumeration;
21 import org.opendaylight.mdsal.binding.javav2.model.api.Enumeration.Pair;
22 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
25 * This util class converts every non-java char in identifier to java char by
26 * its unicode name (<a href=
27 * "http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.8">JAVA SE
28 * SPECIFICATIONS - Identifiers</a>). There are special types of mapping
29 * non-java chars to original identifiers according to specific
30 * {@linkplain JavaIdentifier java type}:
32 * <li>class, enum, interface</li>
35 * <li>without special separator</li>
36 * <li>the first character of identifier, any other first character of
37 * identifier part mapped by non-Java char name from unicode and char in
38 * identifier behind non-java char name are converting to upper case</li>
42 * <li>example* - ExampleAsterisk</li>
43 * <li>example*example - ExampleAserisksExample</li>
44 * <li>\example - ReverseSolidusExample</li>
45 * <li>1example - DigitOneExample</li>
46 * <li>example1 - Example1</li>
47 * <li>int - IntReservedKeyword</li>
48 * <li>con - ConReservedKeyword</li>
53 * <li>enum value, constant</li>
56 * <li>used underscore as special separator</li>
57 * <li>converted identifier to upper case</li>
61 * <li>example* - EXAMPLE_ASTERISK</li>
62 * <li>example*example - EXAMPLE_ASTERISK_EXAMPLE</li>
63 * <li>\example - REVERSE_SOLIDUS_EXAMPLE</li>
64 * <li>1example - DIGIT_ONE_EXAMPLE</li>
65 * <li>example1 - EXAMPLE1</li>
66 * <li>int - INT_RESERVED_KEYWORD</li>
67 * <li>con - CON_RESERVED_KEYWORD</li>
72 * <li>method, variable</li>
76 * <li>without special separator</li>
77 * <li>the first character of identifier is converting to lower case</li>
78 * <li>any other first character of identifier part mapped by non-Java char name
79 * from unicode and char in identifier behind non-java char name are converting
84 * <li>example* - exampleAsterisk</li>
85 * <li>example*example - exampleAserisksExample</li>
86 * <li>\example - reverseSolidusExample</li>
87 * <li>1example - digitOneExample</li>
88 * <li>example1 - example1</li>
89 * <li>int - intReservedKeyword</li>
90 * <li>con - conReservedKeyword</li>
95 * <li>package - full package name (<a href=
96 * "https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html">
97 * Naming a package</a>)</li>
101 * <li>parts of package name are separated by dots</li>
102 * <li>parts of package name are converting to lower case</li>
103 * <li>if parts of package name are reserved Java or Windows keywords, such as
104 * 'int' the suggested convention is to add an underscore to keyword</li>
105 * <li>dash is parsed as underscore according to <a href=
106 * "https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html">
107 * Naming a package</a></li>
111 * <li>org.example* - org.exampleasterisk</li>
112 * <li>org.example*example - org.exampleasteriskexample</li>
113 * <li>org.\example - org.reversesolidusexample</li>
114 * <li>org.1example - org.digitoneexample</li>
115 * <li>org.example1 - org.example1</li>
116 * <li>org.int - org.int_</li>
117 * <li>org.con - org.con_</li>
118 * <li>org.foo-cont - org.foo_cont</li>
125 * There is special case in CLASS, INTERFACE, ENUM, ENUM VALUE, CONSTANT, METHOD
126 * and VARIABLE if identifier contains single dash - then the converter ignores
127 * the single dash in the way of the non-java chars. In other way, if dash is
128 * the first or the last char in the identifier or there is more dashes in a row
129 * in the identifier, then these dashes are converted as non-java chars.
132 * <li>class, enum, interface</li>
135 * <li>foo-cont - FooCont</li>
136 * <li>foo--cont - FooHyphenMinusHyphenMinusCont</li>
137 * <li>-foo - HyphenMinusFoo</li>
138 * <li>foo- - FooHyphenMinus</li>
141 * <li>enum value, constant
144 * <li>foo-cont - FOO_CONT</li>
145 * <li>foo--cont - FOO_HYPHEN_MINUS_HYPHEN_MINUS_CONT</li>
146 * <li>-foo - HYPHEN_MINUS_FOO</li>
147 * <li>foo- - FOO_HYPHEN_MINUS</li>
150 * <li>method, variable</li>
153 * <li>foo-cont - fooCont</li>
154 * <li>foo--cont - fooHyphenMinusHyphenMinusCont</li>
155 * <li>-foo - hyphenMinusFoo</li>
156 * <li>foo- - fooHyphenMinus</li>
161 * Next special case talks about normalizing class name which already exists in
162 * package - but with different camel cases (foo, Foo, fOo, ...). To every next
163 * classes with same names will by added their actual rank (serial number),
164 * except the first one. This working for CLASS, ENUM and INTEFACE java
165 * identifiers. If there exist the same ENUM VALUES in ENUM (with different
166 * camel cases), then it's parsed with same logic like CLASSES, ENUMS and
167 * INTERFACES but according to list of pairs of their ENUM parent. Example:
170 * <li>class, enum, interface</li>
173 * <li>package name org.example, class (or interface or enum) Foo - normalized
175 * <li>package name org.example, class (or interface or enum) fOo - normalized
179 * <li>enum value</li>
192 * <li>YANG enum values will be mapped to 'FOO' and 'FOO_1' Java enum
199 public final class JavaIdentifierNormalizer {
201 public static final Set<String> SPECIAL_RESERVED_PATHS = ImmutableSet.of(
202 "org.opendaylight.yangtools.concepts",
203 "org.opendaylight.yangtools.yang.common",
204 "org.opendaylight.yangtools.yang.model",
205 "org.opendaylight.mdsal.binding.javav2.spec",
209 private static final char UNDERSCORE = '_';
210 private static final char DASH = '-';
211 private static final String RESERVED_KEYWORD = "reserved_keyword";
212 private static final Set<String> PRIMITIVE_TYPES = ImmutableSet.of("char[]", "byte[]");
214 private static final CharMatcher DASH_MATCHER = CharMatcher.is(DASH);
215 private static final CharMatcher DASH_OR_SPACE_MATCHER = CharMatcher.anyOf(" -");
216 private static final CharMatcher UNDERSCORE_MATCHER = CharMatcher.is(UNDERSCORE);
217 private static final Splitter DOT_SPLITTER = Splitter.on('.');
218 private static final Splitter UNDERSCORE_SPLITTER = Splitter.on(UNDERSCORE);
220 // Converted to lower case
221 private static final Set<String> WINDOWS_RESERVED_WORDS = BindingMapping.WINDOWS_RESERVED_WORDS.stream()
222 .map(String::toLowerCase).collect(Collectors.collectingAndThen(Collectors.toSet(), ImmutableSet::copyOf));
224 private JavaIdentifierNormalizer() {
225 throw new UnsupportedOperationException("Util class");
230 * According to <a href="https://tools.ietf.org/html/rfc7950#section-9.6.4">YANG RFC 7950</a>,
231 * all assigned names in an enumeration MUST be unique. Created names are contained in the list
232 * of {@link Enumeration.Pair}. This method adds actual index with underscore behind name of new
233 * enum value only if this name already exists in one of the list of {@link Enumeration.Pair}.
234 * Then, the name will be converted to java chars according to {@link JavaIdentifier#ENUM_VALUE}
246 * YANG enum values will be mapped to 'FOO' and 'FOO_1' Java enum values.
249 * - name of new enum value
251 * - list of all actual enum values
252 * @return converted and fixed name of new enum value
254 public static String normalizeEnumValueIdentifier(final String name, final List<Pair> values) {
255 return convertIdentifierEnumValue(name, name, values, 1);
259 * Normalizing full package name by non java chars and reserved keywords.
261 * @param fullPackageName
262 * - full package name
263 * @return normalized name
265 public static String normalizeFullPackageName(final String fullPackageName) {
266 final Iterator<String> it = DOT_SPLITTER.split(fullPackageName).iterator();
268 return fullPackageName;
271 final StringBuilder sb = new StringBuilder(fullPackageName.length());
273 sb.append(normalizePartialPackageName(it.next()));
275 return sb.toString();
282 * Normalizing part of package name by non java chars.
284 * @param packageNamePart
285 * - part of package name
286 * @return normalized name
288 static String normalizePartialPackageName(final String packageNamePart) {
289 // if part of package name consist from java or windows reserved word, return it with
290 // underscore at the end and in lower case
291 final String lowerPart = packageNamePart.toLowerCase();
292 if (BindingMapping.JAVA_RESERVED_WORDS.contains(lowerPart) || WINDOWS_RESERVED_WORDS.contains(lowerPart)) {
293 return lowerPart + UNDERSCORE;
296 final String normalizedPart = DASH_MATCHER.replaceFrom(packageNamePart, UNDERSCORE);
298 final StringBuilder sb = new StringBuilder();
299 final StringBuilder innerSb = new StringBuilder();
300 for (int i = 0; i < normalizedPart.length(); i++) {
301 final char c = normalizedPart.charAt(i);
302 if (c == UNDERSCORE) {
303 if (innerSb.length() != 0) {
304 sb.append(normalizeSpecificIdentifier(innerSb.toString(), JavaIdentifier.PACKAGE));
305 innerSb.setLength(0);
307 sb.append(UNDERSCORE);
312 if (innerSb.length() != 0) {
313 sb.append(normalizeSpecificIdentifier(innerSb.toString(), JavaIdentifier.PACKAGE));
315 // returned normalized part of package name
316 return sb.toString();
320 * Find and convert non Java chars in identifiers of generated transfer objects, initially
321 * derived from corresponding YANG according to
322 * <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.8"> Java
323 * Specifications - Identifiers</a>. If there is more same class names at the same package, then
324 * append rank (serial number) to the end of them. Works for class, enum, interface.
327 * - package of identifier
329 * - name of identifier
330 * @return - java acceptable identifier
332 static String normalizeClassIdentifier(final String packageName, final String className,
333 ModuleContext context) {
334 if (packageName.isEmpty() && PRIMITIVE_TYPES.contains(className)) {
337 for (final String reservedPath : SPECIAL_RESERVED_PATHS) {
338 if (packageName.startsWith(reservedPath)) {
342 final String convertedClassName = normalizeSpecificIdentifier(className, JavaIdentifier.CLASS);
344 // if packageName contains class name at the end, then the className is name of inner class
345 final String basePackageName;
346 final int lastDot = packageName.lastIndexOf('.');
347 if (lastDot != -1 && Character.isUpperCase(packageName.charAt(lastDot + 1))) {
348 // ignore class name in package name - inner class name has to be normalized according to original package
350 basePackageName = packageName.substring(0, lastDot);
352 basePackageName = packageName;
355 return normalizeClassIdentifier(basePackageName, convertedClassName, convertedClassName, 1, context);
359 * Find and convert non Java chars in identifiers of generated transfer objects, initially
360 * derived from corresponding YANG.
363 * - name of identifier
364 * @param javaIdentifier
365 * - java type of identifier
366 * @return - java acceptable identifier
368 public static String normalizeSpecificIdentifier(final String identifier, final JavaIdentifier javaIdentifier) {
369 // if identifier isn't PACKAGE type then check it by reserved keywords
370 if (javaIdentifier != JavaIdentifier.PACKAGE) {
371 final String lower = identifier.toLowerCase();
372 if (BindingMapping.JAVA_RESERVED_WORDS.contains(lower) || WINDOWS_RESERVED_WORDS.contains(lower)) {
373 return fixCasesByJavaType(lower + UNDERSCORE + RESERVED_KEYWORD, javaIdentifier);
377 // check and convert first char in identifier if there is non-java char
378 final StringBuilder sb = new StringBuilder();
379 final char firstChar = identifier.charAt(0);
380 if (!Character.isJavaIdentifierStart(firstChar)) {
381 // converting first char of identifier
382 sb.append(convertFirst(firstChar, existNext(identifier, 0)));
384 sb.append(firstChar);
386 // check and convert other chars in identifier, if there is non-java char
387 for (int i = 1; i < identifier.length(); i++) {
388 final char actualChar = identifier.charAt(i);
389 // ignore single dash as non java char - if there is more dashes in a row or dash is as
390 // the last char in identifier then parse these dashes as non java chars
391 if (actualChar == DASH && existNext(identifier, i)) {
392 if (identifier.charAt(i - 1) != DASH && identifier.charAt(i + 1) != DASH) {
393 sb.append(UNDERSCORE);
397 if (!Character.isJavaIdentifierPart(actualChar)) {
398 // prepare actual string of sb for checking if underscore exist on position of the last char
399 sb.append(convert(actualChar, existNext(identifier, i), sb.charAt(sb.length() - 1)));
401 sb.append(actualChar);
404 // apply camel case in appropriate way
405 return fixCasesByJavaType(sb.toString().replace("__", "_").toLowerCase(), javaIdentifier);
409 * Checking while there doesn't exist any class name with the same name
410 * (regardless of camel cases) in package.
413 * - package of class name
414 * @param origClassName
415 * - original class name
416 * @param actualClassName
417 * - actual class name with rank (serial number)
419 * - actual rank (serial number)
420 * @return converted identifier
422 private static String normalizeClassIdentifier(final String packageName, final String origClassName,
423 final String actualClassName, final int rank, ModuleContext context) {
425 final ListMultimap<String, String> packagesMap = context.getPackagesMap();
427 synchronized (packagesMap) {
428 if (packagesMap.containsKey(packageName)) {
429 for (final String existingName : packagesMap.get(packageName)) {
430 if (actualClassName.equalsIgnoreCase(existingName)) {
431 return normalizeClassIdentifier(packageName, origClassName, origClassName + rank,
436 context.putToPackagesMap(packageName, actualClassName);
437 return actualClassName;
442 * Fix cases of converted identifiers by Java type
444 * @param convertedIdentifier
445 * - converted identifier
446 * @param javaIdentifier
447 * - java type of identifier
448 * @return converted identifier with right cases according to java type
450 private static String fixCasesByJavaType(final String convertedIdentifier, final JavaIdentifier javaIdentifier) {
451 switch (javaIdentifier) {
455 return capitalize(fixCases(convertedIdentifier));
458 return convertedIdentifier.toUpperCase();
461 return fixCases(convertedIdentifier);
463 return UNDERSCORE_MATCHER.removeFrom(convertedIdentifier);
465 throw new IllegalArgumentException("Unknown java type of identifier : " + javaIdentifier.toString());
470 * Delete unnecessary chars in converted identifier and apply camel case in appropriate way.
472 * @param convertedIdentifier
473 * - original converted identifier
474 * @return resolved identifier
476 private static String fixCases(final String convertedIdentifier) {
477 if (convertedIdentifier.indexOf(UNDERSCORE) == -1) {
478 return convertedIdentifier;
481 final StringBuilder sb = new StringBuilder(convertedIdentifier.length());
482 final Iterator<String> it = UNDERSCORE_SPLITTER.split(convertedIdentifier).iterator();
483 sb.append(it.next());
484 while (it.hasNext()) {
485 sb.append(capitalize(it.next()));
487 return sb.toString();
491 * Check if there exist next char in identifier behind actual char position
494 * - original identifier
496 * - actual char position
497 * @return true if there is another char, false otherwise
499 private static boolean existNext(final String identifier, final int actual) {
500 return identifier.length() > actual + 1;
504 * Converting first char of identifier. This happen only if this char is
510 * - existing of next char behind actual char
511 * @return converted char
513 private static String convertFirst(final char c, final boolean existNext) {
514 final String name = DASH_OR_SPACE_MATCHER.replaceFrom(Character.getName(c), UNDERSCORE);
515 return existNext ? name + '_' : name;
519 * Converting any char in java identifier, This happen only if this char is
525 * - existing of next char behind actual char
526 * @param partialLastChar
527 * - last char of partial converted identifier
528 * @return converted char
530 private static String convert(final char c, final boolean existNext, final char partialLastChar) {
531 return partialLastChar == '_' ? convertFirst(c, existNext) : "_" + convertFirst(c, existNext);
535 * Capitalize input string
538 * - string to be capitalized
540 private static String capitalize(final String identifier) {
541 return identifier.substring(0, 1).toUpperCase() + identifier.substring(1);
544 private static String convertIdentifierEnumValue(final String name, final String origName, final List<Pair> values,
546 String newName = name;
547 for (final Pair pair : values) {
548 if (name.equalsIgnoreCase(pair.getName()) || name.equalsIgnoreCase(pair.getMappedName())) {
549 int actualRank = rank;
550 final String actualName = origName + UNDERSCORE + actualRank;
551 newName = convertIdentifierEnumValue(actualName, origName, values, ++actualRank);
554 return normalizeSpecificIdentifier(newName, JavaIdentifier.ENUM_VALUE);