Added generation of Transfer Objects from Type Definitions.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / BindingGeneratorUtil.java
1 package org.opendaylight.controller.binding.generator.util;
2
3 import java.net.URI;
4 import java.util.Arrays;
5 import java.util.Calendar;
6 import java.util.GregorianCalendar;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.opendaylight.controller.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
12 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;
13 import org.opendaylight.controller.yang.common.QName;
14 import org.opendaylight.controller.yang.model.api.SchemaNode;
15 import org.opendaylight.controller.yang.model.api.SchemaPath;
16
17 public class BindingGeneratorUtil {
18
19     private static final String[] SET_VALUES = new String[] { "abstract",
20             "assert", "boolean", "break", "byte", "case", "catch", "char",
21             "class", "const", "continue", "default", "double", "do", "else",
22             "enum", "extends", "false", "final", "finally", "float", "for",
23             "goto", "if", "implements", "import", "instanceof", "int",
24             "interface", "long", "native", "new", "null", "package", "private",
25             "protected", "public", "return", "short", "static", "strictfp",
26             "super", "switch", "synchronized", "this", "throw", "throws",
27             "transient", "true", "try", "void", "volatile", "while" };
28
29     private static Calendar calendar = new GregorianCalendar();
30
31     private BindingGeneratorUtil() {
32     }
33
34     public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(
35             Arrays.asList(SET_VALUES));
36
37     public static String validateJavaPackage(final String packageName) {
38         if (packageName != null) {
39             final String[] packNameParts = packageName.split("\\.");
40             if (packNameParts != null) {
41                 final StringBuilder builder = new StringBuilder();
42                 for (int i = 0; i < packNameParts.length; ++i) {
43                     if (JAVA_RESERVED_WORDS.contains(packNameParts[i])) {
44                         packNameParts[i] = "_" + packNameParts[i];
45                     }
46                     if (i > 0) {
47                         builder.append(".");
48                     }
49                     builder.append(packNameParts[i]);
50                 }
51                 return builder.toString();
52             }
53         }
54         return packageName;
55     }
56     
57     public static String validateParameterName(final String parameterName) {
58         if (parameterName != null) {
59             if (JAVA_RESERVED_WORDS.contains(parameterName)) {
60                 return "_" + parameterName;
61             }
62         }
63         return parameterName;
64     }
65     
66     public static GeneratedTOBuilder schemaNodeToTransferObjectBuilder(
67             final String basePackageName, final SchemaNode schemaNode, final String transObjectName) {
68         if (basePackageName != null && schemaNode != null && transObjectName != null) {
69             final String packageName = packageNameForGeneratedType(basePackageName,
70                     schemaNode.getPath());
71
72             if (packageName != null) {
73                 final String genTOName = BindingGeneratorUtil
74                         .parseToClassName(transObjectName);
75                 final GeneratedTOBuilder newType = new GeneratedTOBuilderImpl(
76                         packageName, genTOName);
77
78                 return newType;
79             }
80         }
81         return null;
82     }
83
84     public static String moduleNamespaceToPackageName(
85             final URI moduleNamespace, final String yangVersion) {
86         final StringBuilder packageNameBuilder = new StringBuilder();
87
88         packageNameBuilder.append("org.opendaylight.yang.gen.v");
89         packageNameBuilder.append(yangVersion);
90         packageNameBuilder.append(".rev");
91         packageNameBuilder.append(calendar.get(Calendar.YEAR));
92         packageNameBuilder.append((calendar.get(Calendar.MONTH) + 1));
93         packageNameBuilder.append(calendar.get(Calendar.DAY_OF_MONTH));
94         packageNameBuilder.append(".");
95
96         String namespace = moduleNamespace.toString();
97         namespace = namespace.replace("://", ".");
98         namespace = namespace.replace("/", ".");
99         namespace = namespace.replace(":", ".");
100         namespace = namespace.replace("-", ".");
101         namespace = namespace.replace("@", ".");
102         namespace = namespace.replace("$", ".");
103         namespace = namespace.replace("#", ".");
104         namespace = namespace.replace("'", ".");
105         namespace = namespace.replace("*", ".");
106         namespace = namespace.replace("+", ".");
107         namespace = namespace.replace(",", ".");
108         namespace = namespace.replace(";", ".");
109         namespace = namespace.replace("=", ".");
110
111         packageNameBuilder.append(namespace);
112
113         return packageNameBuilder.toString();
114     }
115
116     public static String packageNameForGeneratedType(
117             final String basePackageName, final SchemaPath schemaPath) {
118         final StringBuilder builder = new StringBuilder();
119         builder.append(basePackageName);
120         if ((schemaPath != null) && (schemaPath.getPath() != null)) {
121             final List<QName> pathToNode = schemaPath.getPath();
122             final int traversalSteps = (pathToNode.size() - 1);
123             for (int i = 0; i < traversalSteps; ++i) {
124                 builder.append(".");
125                 String nodeLocalName = pathToNode.get(i).getLocalName();
126
127                 nodeLocalName = nodeLocalName.replace(":", ".");
128                 nodeLocalName = nodeLocalName.replace("-", ".");
129                 builder.append(nodeLocalName);
130             }
131             return validateJavaPackage(builder.toString());
132         }
133         return null;
134     }
135     
136     public static String parseToClassName(String token) {
137         token = token.replace(".", "");
138         String correctStr = parseToCamelCase(token);
139
140         // make first char upper-case
141         char first = Character.toUpperCase(correctStr.charAt(0));
142         correctStr = first + correctStr.substring(1);
143         return correctStr;
144     }
145     
146     public static String parseToValidParamName(final String token) {
147         final String validToken = token.replace(".", "");
148         String correctStr = parseToCamelCase(validToken);
149
150         // make first char lower-case
151         char first = Character.toLowerCase(correctStr.charAt(0));
152         correctStr = first + correctStr.substring(1);
153         return validateParameterName(correctStr);
154     }
155     
156     private static String parseToCamelCase(String token) {
157         if (token == null) {
158             throw new NullPointerException("Name can not be null");
159         }
160
161         String correctStr = token.trim();
162         if (correctStr.length() == 0) {
163             throw new IllegalArgumentException("Name can not be emty");
164         }
165
166         correctStr = replaceWithCamelCase(correctStr, ' ');
167         correctStr = replaceWithCamelCase(correctStr, '-');
168         correctStr = replaceWithCamelCase(correctStr, '_');
169         return correctStr;
170     }
171     
172     private static String replaceWithCamelCase(String text, char removalChar) {
173         StringBuilder sb = new StringBuilder(text);
174         String toBeRemoved = String.valueOf(removalChar);
175
176         int toBeRemovedPos = sb.indexOf(toBeRemoved);
177         while (toBeRemovedPos != -1) {
178             sb.replace(toBeRemovedPos, toBeRemovedPos + 1, "");
179             // check if 'toBeRemoved' character is not the only character in
180             // 'text'
181             if (sb.length() == 0) {
182                 throw new IllegalArgumentException("Name can not be '"
183                         + toBeRemoved + "'");
184             }
185             String replacement = String.valueOf(sb.charAt(toBeRemovedPos))
186                     .toUpperCase();
187             sb.setCharAt(toBeRemovedPos, replacement.charAt(0));
188             toBeRemovedPos = sb.indexOf(toBeRemoved);
189         }
190         return sb.toString();
191     }
192 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.