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
9 package org.opendaylight.mdsal.binding.javav2.util;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Splitter;
15 import com.google.common.collect.ImmutableSet;
16 import java.text.SimpleDateFormat;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import org.opendaylight.yangtools.yang.model.api.Module;
23 * Standard Util class that provides generated Java related functionality
26 public final class BindingMapping {
28 public static final Set<String> JAVA_RESERVED_WORDS = ImmutableSet.of("abstract", "assert", "boolean", "break",
29 "byte", "case", "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum",
30 "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
31 "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
32 "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
33 "true", "try", "void", "volatile", "while");
35 public static final Set<String> WINDOWS_RESERVED_WORDS = ImmutableSet.of("CON", "PRN", "AUX", "CLOCK$", "NUL",
36 "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2",
37 "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9");
39 public static final String QNAME_STATIC_FIELD_NAME = "QNAME";
42 * Package prefix for Binding v2 generated Java code structures
44 public static final String PACKAGE_PREFIX = "org.opendaylight.mdsal.gen.javav2";
46 private static final Splitter CAMEL_SPLITTER = Splitter.on(CharMatcher.anyOf(" _.-/").precomputed())
47 .omitEmptyStrings().trimResults();
48 private static final Pattern COLON_SLASH_SLASH = Pattern.compile("://", Pattern.LITERAL);
49 private static final String QUOTED_DOT = Matcher.quoteReplacement(".");
50 public static final String MODULE_INFO_CLASS_NAME = "$YangModuleInfoImpl";
51 public static final String MODEL_BINDING_PROVIDER_CLASS_NAME = "$YangModelBindingProvider";
52 public static final String PATTERN_CONSTANT_NAME = "PATTERN_CONSTANTS";
53 public static final String MEMBER_PATTERN_LIST = "patterns";
55 private static final ThreadLocal<SimpleDateFormat> PACKAGE_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
58 protected SimpleDateFormat initialValue() {
59 return new SimpleDateFormat("yyMMdd");
63 public void set(final SimpleDateFormat value) {
64 throw new UnsupportedOperationException();
68 private BindingMapping() {
69 throw new UnsupportedOperationException("Utility class");
72 public static String getRootPackageName(final Module module) {
73 Preconditions.checkArgument(module != null, "Module must not be null");
74 Preconditions.checkArgument(module.getRevision() != null, "Revision must not be null");
75 Preconditions.checkArgument(module.getNamespace() != null, "Namespace must not be null");
77 final StringBuilder packageNameBuilder = new StringBuilder();
78 packageNameBuilder.append(PACKAGE_PREFIX);
79 packageNameBuilder.append('.');
81 String namespace = module.getNamespace().toString();
82 namespace = COLON_SLASH_SLASH.matcher(namespace).replaceAll(QUOTED_DOT);
84 final char[] chars = namespace.toCharArray();
85 for (int i = 0; i < chars.length; ++i) {
102 // no-op, any other character is kept as it is
106 packageNameBuilder.append(chars);
107 if (chars[chars.length - 1] != '.') {
108 packageNameBuilder.append('.');
111 //TODO: per yangtools dev, semantic version not used yet
112 // final SemVer semVer = module.getSemanticVersion();
113 // if (semVer != null) {
114 // packageNameBuilder.append(semVer.toString());
116 // packageNameBuilder.append("rev");
117 // packageNameBuilder.append(PACKAGE_DATE_FORMAT.get().format(module.getRevision()));
120 packageNameBuilder.append("rev");
121 packageNameBuilder.append(PACKAGE_DATE_FORMAT.get().format(module.getRevision()));
122 return packageNameBuilder.toString();
126 * Prepares valid Java class name
130 public static String getClassName(final String localName) {
131 Preconditions.checkArgument(localName != null, "Name should not be null.");
132 return toFirstUpper(toCamelCase(localName));
135 private static String toCamelCase(final String rawString) {
136 Preconditions.checkArgument(rawString != null, "String should not be null");
137 final Iterable<String> components = CAMEL_SPLITTER.split(rawString);
138 final StringBuilder builder = new StringBuilder();
139 for (final String comp : components) {
140 builder.append(toFirstUpper(comp));
142 return checkNumericPrefix(builder.toString());
145 private static String checkNumericPrefix(final String rawString) {
146 if ((rawString == null) || rawString.isEmpty()) {
149 if (Character.isDigit(rawString.charAt(0))) {
150 return "_" + rawString;
157 * Returns the {@link String} {@code s} with an
158 * {@link Character#isUpperCase(char) upper case} first character. This
159 * function is null-safe.
162 * the string that should get an upper case first character. May
163 * be <code>null</code>.
164 * @return the {@link String} {@code s} with an upper case first character
165 * or <code>null</code> if the input {@link String} {@code s} was
168 public static String toFirstUpper(final String s) {
169 if (s == null || s.length() == 0) {
172 if (Character.isUpperCase(s.charAt(0))) {
175 if (s.length() == 1) {
176 return s.toUpperCase();
178 return s.substring(0, 1).toUpperCase() + s.substring(1);
182 * Prepares Java property name for method getter code generation
183 * @param yangIdentifier given YANG element local name
184 * @return property name
186 public static String getPropertyName(final String yangIdentifier) {
187 final String potential = toFirstLower(toCamelCase(yangIdentifier));
188 if ("class".equals(potential)) {
195 * Returns the {@link String} {@code s} with an
196 * {@link Character#isLowerCase(char) lower case} first character. This
197 * function is null-safe.
200 * the string that should get an lower case first character. May
201 * be <code>null</code>.
202 * @return the {@link String} {@code s} with an lower case first character
203 * or <code>null</code> if the input {@link String} {@code s} was
206 private static String toFirstLower(final String s) {
207 if (s == null || s.length() == 0) {
210 if (Character.isLowerCase(s.charAt(0))) {
213 if (s.length() == 1) {
214 return s.toLowerCase();
216 return s.substring(0, 1).toLowerCase() + s.substring(1);
219 //TODO: further implementation of static util methods...