Added support for discovering YangModuleInfo via ServiceLoader.
[yangtools.git] / yang / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / BindingMapping.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.binding;
9
10 import java.util.Set;
11
12 import org.opendaylight.yangtools.yang.common.QName;
13
14 import com.google.common.base.Splitter;
15 import com.google.common.collect.ImmutableSet;
16
17 import static com.google.common.base.Preconditions.*;
18
19 public final class BindingMapping {
20
21     public static final String VERSION = "0.6";
22
23     public static final Set<String> JAVA_RESERVED_WORDS = ImmutableSet.of("abstract", "assert", "boolean", "break",
24             "byte", "case", "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum",
25             "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
26             "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
27             "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
28             "true", "try", "void", "volatile", "while");
29
30     public static final String DATA_ROOT_SUFFIX = "Data";
31     public static final String RPC_SERVICE_SUFFIX = "Service";
32     public static final String NOTIFICATION_LISTENER_SUFFIX = "Listener";
33     public static final String QNAME_STATIC_FIELD_NAME = "QNAME";
34     public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v1";
35
36     private static final Splitter SPACE_SPLITTER = Splitter.on(" ").omitEmptyStrings().trimResults();
37
38     public static final String MODULE_INFO_CLASS_NAME = "$YangModuleInfoImpl";
39     public static final String MODEL_BINDING_PROVIDER_CLASS_NAME = "$YangModelBindingProvider";
40
41     public static final String getMethodName(QName name) {
42         checkArgument(name != null, "Name should not be null.");
43         return getMethodName(name.getLocalName());
44     }
45
46     public static final String getClassName(String localName) {
47         checkArgument(localName != null, "Name should not be null.");
48         return toFirstUpper(toCamelCase(localName));
49     }
50
51     public static final String getMethodName(String yangIdentifier) {
52         checkArgument(yangIdentifier != null,"Identifier should not be null");
53         return toFirstLower(toCamelCase(yangIdentifier));
54     }
55
56     public static final String getClassName(QName name) {
57         checkArgument(name != null, "Name should not be null.");
58         return toFirstUpper(toCamelCase(name.getLocalName()));
59     }
60
61     public static String getPropertyName(String yangIdentifier) {
62         final String potential = toFirstLower(toCamelCase(yangIdentifier));
63         if("class".equals(potential)) {
64             return "xmlClass";
65         }
66         return potential;
67     }
68
69     private static final String toCamelCase(String rawString) {
70         checkArgument(rawString != null, "String should not be null");
71         Iterable<String> components = SPACE_SPLITTER.split(rawString.replace('-', ' ').replace('_', ' '));
72         StringBuilder builder = new StringBuilder();
73         for (String comp : components) {
74             builder.append(toFirstUpper(comp));
75         }
76         return builder.toString();
77     }
78
79     /**
80      * Returns the {@link String} {@code s} with an
81      * {@link Character#isUpperCase(char) upper case} first character. This
82      * function is null-safe.
83      * 
84      * @param s
85      *            the string that should get an upper case first character. May
86      *            be <code>null</code>.
87      * @return the {@link String} {@code s} with an upper case first character
88      *         or <code>null</code> if the input {@link String} {@code s} was
89      *         <code>null</code>.
90      */
91     private static String toFirstUpper(String s) {
92         if (s == null || s.length() == 0)
93             return s;
94         if (Character.isUpperCase(s.charAt(0)))
95             return s;
96         if (s.length() == 1)
97             return s.toUpperCase();
98         return s.substring(0, 1).toUpperCase() + s.substring(1);
99     }
100
101     /**
102      * Returns the {@link String} {@code s} with an
103      * {@link Character#isLowerCase(char) lower case} first character. This
104      * function is null-safe.
105      * 
106      * @param s
107      *            the string that should get an lower case first character. May
108      *            be <code>null</code>.
109      * @return the {@link String} {@code s} with an lower case first character
110      *         or <code>null</code> if the input {@link String} {@code s} was
111      *         <code>null</code>.
112      */
113     private static String toFirstLower(String s) {
114         if (s == null || s.length() == 0)
115             return s;
116         if (Character.isLowerCase(s.charAt(0)))
117             return s;
118         if (s.length() == 1)
119             return s.toLowerCase();
120         return s.substring(0, 1).toLowerCase() + s.substring(1);
121     }
122 }