2 * Copyright (c) 2013 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.yangtools.yang.binding;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.base.Splitter;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Interner;
16 import com.google.common.collect.Interners;
17 import java.text.SimpleDateFormat;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
24 public final class BindingMapping {
26 public static final String VERSION = "0.6";
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 String DATA_ROOT_SUFFIX = "Data";
36 public static final String RPC_SERVICE_SUFFIX = "Service";
37 public static final String NOTIFICATION_LISTENER_SUFFIX = "Listener";
38 public static final String QNAME_STATIC_FIELD_NAME = "QNAME";
39 public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v1";
40 public static final String AUGMENTATION_FIELD = "augmentation";
42 private static final Splitter CAMEL_SPLITTER = Splitter.on(CharMatcher.anyOf(" _.-/").precomputed())
43 .omitEmptyStrings().trimResults();
44 private static final Pattern COLON_SLASH_SLASH = Pattern.compile("://", Pattern.LITERAL);
45 private static final String QUOTED_DOT = Matcher.quoteReplacement(".");
46 private static final Splitter DOT_SPLITTER = Splitter.on('.');
48 public static final String MODULE_INFO_CLASS_NAME = "$YangModuleInfoImpl";
49 public static final String MODEL_BINDING_PROVIDER_CLASS_NAME = "$YangModelBindingProvider";
51 public static final String RPC_INPUT_SUFFIX = "Input";
52 public static final String RPC_OUTPUT_SUFFIX = "Output";
54 private static final ThreadLocal<SimpleDateFormat> PACKAGE_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
57 protected SimpleDateFormat initialValue() {
58 return new SimpleDateFormat("yyMMdd");
62 public void set(final SimpleDateFormat value) {
63 throw new UnsupportedOperationException();
67 private static final Interner<String> PACKAGE_INTERNER = Interners.newWeakInterner();
69 private BindingMapping() {
70 throw new UnsupportedOperationException("Utility class should not be instantiated");
73 public static String getRootPackageName(final QName module) {
74 return getRootPackageName(module.getModule());
77 public static String getRootPackageName(final QNameModule module) {
78 checkArgument(module != null, "Module must not be null");
79 checkArgument(module.getRevision() != null, "Revision must not be null");
80 checkArgument(module.getNamespace() != null, "Namespace must not be null");
81 final StringBuilder packageNameBuilder = new StringBuilder();
83 packageNameBuilder.append(BindingMapping.PACKAGE_PREFIX);
84 packageNameBuilder.append('.');
86 String namespace = module.getNamespace().toString();
87 namespace = COLON_SLASH_SLASH.matcher(namespace).replaceAll(QUOTED_DOT);
89 final char[] chars = namespace.toCharArray();
90 for (int i = 0; i < chars.length; ++i) {
108 packageNameBuilder.append(chars);
109 if (chars[chars.length - 1] != '.') {
110 packageNameBuilder.append('.');
112 packageNameBuilder.append("rev");
113 packageNameBuilder.append(PACKAGE_DATE_FORMAT.get().format(module.getRevision()));
114 return normalizePackageName(packageNameBuilder.toString());
117 public static String normalizePackageName(final String packageName) {
118 if (packageName == null) {
122 final StringBuilder builder = new StringBuilder();
123 boolean first = true;
125 for (String p : DOT_SPLITTER.split(packageName.toLowerCase())) {
132 if (Character.isDigit(p.charAt(0)) || BindingMapping.JAVA_RESERVED_WORDS.contains(p)) {
138 // Prevent duplication of input string
139 return PACKAGE_INTERNER.intern(builder.toString());
142 public static String getMethodName(final QName name) {
143 checkArgument(name != null, "Name should not be null.");
144 return getMethodName(name.getLocalName());
147 public static String getClassName(final String localName) {
148 checkArgument(localName != null, "Name should not be null.");
149 return toFirstUpper(toCamelCase(localName));
152 public static String getMethodName(final String yangIdentifier) {
153 checkArgument(yangIdentifier != null,"Identifier should not be null");
154 return toFirstLower(toCamelCase(yangIdentifier));
157 public static String getClassName(final QName name) {
158 checkArgument(name != null, "Name should not be null.");
159 return toFirstUpper(toCamelCase(name.getLocalName()));
162 public static String getGetterSuffix(final QName name) {
163 checkArgument(name != null, "Name should not be null.");
164 final String candidate = toFirstUpper(toCamelCase(name.getLocalName()));
165 return ("Class".equals(candidate) ? "XmlClass" : candidate);
168 public static String getPropertyName(final String yangIdentifier) {
169 final String potential = toFirstLower(toCamelCase(yangIdentifier));
170 if ("class".equals(potential)) {
176 private static String toCamelCase(final String rawString) {
177 checkArgument(rawString != null, "String should not be null");
178 Iterable<String> components = CAMEL_SPLITTER.split(rawString);
179 StringBuilder builder = new StringBuilder();
180 for (String comp : components) {
181 builder.append(toFirstUpper(comp));
183 return checkNumericPrefix(builder.toString());
186 private static String checkNumericPrefix(final String rawString) {
187 if (rawString == null || rawString.isEmpty()) {
190 char firstChar = rawString.charAt(0);
191 if (firstChar >= '0' && firstChar <= '9') {
192 return "_" + rawString;
199 * Returns the {@link String} {@code s} with an
200 * {@link Character#isUpperCase(char) upper case} first character. This
201 * function is null-safe.
204 * the string that should get an upper case first character. May
205 * be <code>null</code>.
206 * @return the {@link String} {@code s} with an upper case first character
207 * or <code>null</code> if the input {@link String} {@code s} was
210 public static String toFirstUpper(final String s) {
211 if (s == null || s.length() == 0) {
214 if (Character.isUpperCase(s.charAt(0))) {
217 if (s.length() == 1) {
218 return s.toUpperCase();
220 return s.substring(0, 1).toUpperCase() + s.substring(1);
224 * Returns the {@link String} {@code s} with an
225 * {@link Character#isLowerCase(char) lower case} first character. This
226 * function is null-safe.
229 * the string that should get an lower case first character. May
230 * be <code>null</code>.
231 * @return the {@link String} {@code s} with an lower case first character
232 * or <code>null</code> if the input {@link String} {@code s} was
235 private static String toFirstLower(final String s) {
236 if (s == null || s.length() == 0) {
239 if (Character.isLowerCase(s.charAt(0))) {
242 if (s.length() == 1) {
243 return s.toLowerCase();
245 return s.substring(0, 1).toLowerCase() + s.substring(1);