2 * Copyright (c) 2014 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.binding.generator.util;
10 import java.io.ByteArrayOutputStream;
11 import java.io.DataOutputStream;
12 import java.io.IOException;
13 import java.security.MessageDigest;
14 import java.security.NoSuchAlgorithmException;
15 import java.text.SimpleDateFormat;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.HashSet;
21 import java.util.List;
24 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
25 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
26 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
27 import org.opendaylight.yangtools.sal.binding.model.api.Type;
28 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
29 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
30 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.TypeMemberBuilder;
31 import org.opendaylight.yangtools.yang.binding.BindingMapping;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
39 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
40 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
41 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
45 * Contains the methods for converting strings to valid JAVA language strings
46 * (package names, class names, attribute names).
50 public final class BindingGeneratorUtil {
52 private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
55 protected SimpleDateFormat initialValue() {
56 return new SimpleDateFormat("yyMMdd");
60 public void set(SimpleDateFormat value) {
61 throw new UnsupportedOperationException();
66 * Array of strings values which represents JAVA reserved words.
69 private static final String[] SET_VALUES = new String[] { "abstract", "assert", "boolean", "break", "byte", "case",
70 "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum", "extends",
71 "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int",
72 "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
73 "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient",
74 "true", "try", "void", "volatile", "while" };
77 * Impossible to instantiate this class. All of the methods or attributes
80 private BindingGeneratorUtil() {
84 * Hash set of words which are reserved in JAVA language.
87 private static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(Arrays.asList(SET_VALUES));
90 * Converts string <code>packageName</code> to valid JAVA package name.
92 * If some words of package name are digits of JAVA reserved words they are
93 * prefixed with underscore character.
96 * string which contains words separated by point.
97 * @return package name which contains words separated by point.
99 private static String validateJavaPackage(final String packageName) {
100 if (packageName != null) {
101 final String[] packNameParts = packageName.toLowerCase().split("\\.");
102 if (packNameParts != null) {
103 final StringBuilder builder = new StringBuilder();
104 for (int i = 0; i < packNameParts.length; ++i) {
105 final String packNamePart = packNameParts[i];
106 if (Character.isDigit(packNamePart.charAt(0))) {
107 packNameParts[i] = "_" + packNamePart;
108 } else if (JAVA_RESERVED_WORDS.contains(packNamePart)) {
109 packNameParts[i] = "_" + packNamePart;
114 builder.append(packNameParts[i]);
116 return builder.toString();
123 * Converts <code>parameterName</code> to valid JAVA parameter name.
125 * If the <code>parameterName</code> is one of the JAVA reserved words then
126 * it is prefixed with underscore character.
128 * @param parameterName
129 * string with the parameter name
130 * @return string with the admissible parameter name
132 public static String resolveJavaReservedWordEquivalency(final String parameterName) {
133 if (parameterName != null && JAVA_RESERVED_WORDS.contains(parameterName)) {
134 return "_" + parameterName;
136 return parameterName;
140 * Converts module name to valid JAVA package name.
142 * The package name consists of:
144 * <li>prefix - <i>org.opendaylight.yang.gen.v</i></li>
145 * <li>module YANG version - <i>org.opendaylight.yang.gen.v</i></li>
146 * <li>module namespace - invalid characters are replaced with dots</li>
147 * <li>revision prefix - <i>.rev</i></li>
148 * <li>revision - YYYYMMDD (MM and DD aren't spread to the whole length)</li>
152 * module which contains data about namespace and revision date
153 * @return string with the valid JAVA package name
154 * @throws IllegalArgumentException
155 * if the revision date of the <code>module</code> equals
158 public static String moduleNamespaceToPackageName(final Module module) {
159 final StringBuilder packageNameBuilder = new StringBuilder();
161 if (module.getRevision() == null) {
162 throw new IllegalArgumentException("Module " + module.getName() + " does not specify revision date!");
164 packageNameBuilder.append("org.opendaylight.yang.gen.v");
165 packageNameBuilder.append(module.getYangVersion());
166 packageNameBuilder.append(".");
168 String namespace = module.getNamespace().toString();
169 namespace = namespace.replace("://", ".");
170 namespace = namespace.replace("/", ".");
171 namespace = namespace.replace(":", ".");
172 namespace = namespace.replace("-", ".");
173 namespace = namespace.replace("@", ".");
174 namespace = namespace.replace("$", ".");
175 namespace = namespace.replace("#", ".");
176 namespace = namespace.replace("'", ".");
177 namespace = namespace.replace("*", ".");
178 namespace = namespace.replace("+", ".");
179 namespace = namespace.replace(",", ".");
180 namespace = namespace.replace(";", ".");
181 namespace = namespace.replace("=", ".");
183 packageNameBuilder.append(namespace);
184 packageNameBuilder.append(".rev");
185 packageNameBuilder.append(DATE_FORMAT.get().format(module.getRevision()));
187 return validateJavaPackage(packageNameBuilder.toString());
191 * Creates package name from specified <code>basePackageName</code> (package
192 * name for module) and <code>schemaPath</code>.
194 * Resulting package name is concatenation of <code>basePackageName</code>
195 * and all local names of YANG nodes which are parents of some node for
196 * which <code>schemaPath</code> is specified.
198 * @param basePackageName
199 * string with package name of the module
201 * list of names of YANG nodes which are parents of some node +
203 * @return string with valid JAVA package name
205 public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
206 if (basePackageName == null) {
207 throw new IllegalArgumentException("Base Package Name cannot be NULL!");
209 if (schemaPath == null) {
210 throw new IllegalArgumentException("Schema Path cannot be NULL!");
213 final StringBuilder builder = new StringBuilder();
214 builder.append(basePackageName);
215 final List<QName> pathToNode = schemaPath.getPath();
216 final int traversalSteps = (pathToNode.size() - 1);
217 for (int i = 0; i < traversalSteps; ++i) {
219 String nodeLocalName = pathToNode.get(i).getLocalName();
221 nodeLocalName = nodeLocalName.replace(":", ".");
222 nodeLocalName = nodeLocalName.replace("-", ".");
223 builder.append(nodeLocalName);
225 return validateJavaPackage(builder.toString());
229 * Generates the package name for type definition from
230 * <code>typeDefinition</code> and <code>basePackageName</code>.
232 * @param basePackageName
233 * string with the package name of the module
234 * @param typeDefinition
235 * type definition for which the package name will be generated *
236 * @return string with valid JAVA package name
237 * @throws IllegalArgumentException
239 * <li>if <code>basePackageName</code> equals <code>null</code></li>
240 * <li>if <code>typeDefinition</code> equals <code>null</code></li>
243 public static String packageNameForTypeDefinition(final String basePackageName,
244 final TypeDefinition<?> typeDefinition) {
245 if (basePackageName == null) {
246 throw new IllegalArgumentException("Base Package Name cannot be NULL!");
248 if (typeDefinition == null) {
249 throw new IllegalArgumentException("Type Definition reference cannot be NULL!");
252 final StringBuilder builder = new StringBuilder();
253 builder.append(basePackageName);
254 return validateJavaPackage(builder.toString());
258 * Converts <code>token</code> to string which is in accordance with best
259 * practices for JAVA class names.
262 * string which contains characters which should be converted to
264 * @return string which is in accordance with best practices for JAVA class
267 * @deprecated Use {@link BindingMapping#getClassName(QName)} instead.
270 public static String parseToClassName(String token) {
271 return parseToCamelCase(token, true);
275 * Converts <code>token</code> to string which is in accordance with best
276 * practices for JAVA parameter names.
279 * string which contains characters which should be converted to
280 * JAVA parameter name
281 * @return string which is in accordance with best practices for JAVA
284 public static String parseToValidParamName(final String token) {
285 return resolveJavaReservedWordEquivalency(parseToCamelCase(token, false));
290 * Converts string <code>token</code> to the cammel case format.
293 * string which should be converted to the cammel case format
295 * boolean value which says whether the first character of the
296 * <code>token</code> should|shuldn't be uppercased
297 * @return string in the cammel case format
298 * @throws IllegalArgumentException
300 * <li>if <code>token</code> without white spaces is empty</li>
301 * <li>if <code>token</code> equals null</li>
305 private static String parseToCamelCase(final String token, final boolean uppercase) {
307 throw new IllegalArgumentException("Name can not be null");
310 String correctStr = token.trim();
311 correctStr = correctStr.replace(".", "");
313 if (correctStr.isEmpty()) {
314 throw new IllegalArgumentException("Name can not be emty");
317 correctStr = replaceWithCamelCase(correctStr, ' ');
318 correctStr = replaceWithCamelCase(correctStr, '-');
319 correctStr = replaceWithCamelCase(correctStr, '_');
321 String firstChar = correctStr.substring(0, 1);
323 firstChar = firstChar.toUpperCase();
325 firstChar = firstChar.toLowerCase();
328 if (firstChar.matches("[0-9]")) {
329 correctStr = "_" + correctStr;
331 correctStr = firstChar + correctStr.substring(1);
337 * Replaces all the occurances of the <code>removalChar</code> in the
338 * <code>text</code> with empty string and converts following character to
342 * string with source text which should be converted
344 * character which is sought in the <code>text</code>
345 * @return string which doesn't contain <code>removalChar</code> and has
346 * following characters converted to upper case
347 * @throws IllegalArgumentException
348 * if the length of the returning string has length 0
350 private static String replaceWithCamelCase(String text, char removalChar) {
351 StringBuilder sb = new StringBuilder(text);
352 String toBeRemoved = String.valueOf(removalChar);
354 int toBeRemovedPos = sb.indexOf(toBeRemoved);
355 while (toBeRemovedPos != -1) {
356 sb.replace(toBeRemovedPos, toBeRemovedPos + 1, "");
357 // check if 'toBeRemoved' character is not the only character in
359 if (sb.length() == 0) {
360 throw new IllegalArgumentException("The resulting string can not be empty");
362 String replacement = String.valueOf(sb.charAt(toBeRemovedPos)).toUpperCase();
363 sb.setCharAt(toBeRemovedPos, replacement.charAt(0));
364 toBeRemovedPos = sb.indexOf(toBeRemoved);
366 return sb.toString();
369 public static long computeDefaultSUID(GeneratedTOBuilderImpl to) {
371 ByteArrayOutputStream bout = new ByteArrayOutputStream();
372 DataOutputStream dout = new DataOutputStream(bout);
374 dout.writeUTF(to.getName());
375 dout.writeInt(to.isAbstract() ? 3 : 7);
377 List<Type> impl = to.getImplementsTypes();
378 Collections.sort(impl, new Comparator<Type>() {
380 public int compare(Type o1, Type o2) {
381 return o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName());
384 for (Type ifc : impl) {
385 dout.writeUTF(ifc.getFullyQualifiedName());
388 Comparator<TypeMemberBuilder<?>> comparator = new Comparator<TypeMemberBuilder<?>>() {
390 public int compare(TypeMemberBuilder<?> o1, TypeMemberBuilder<?> o2) {
391 return o1.getName().compareTo(o2.getName());
395 List<GeneratedPropertyBuilder> props = to.getProperties();
396 Collections.sort(props, comparator);
397 for (GeneratedPropertyBuilder gp : props) {
398 dout.writeUTF(gp.getName());
401 List<MethodSignatureBuilder> methods = to.getMethodDefinitions();
402 Collections.sort(methods, comparator);
403 for (MethodSignatureBuilder m : methods) {
404 if (!(m.getAccessModifier().equals(AccessModifier.PRIVATE))) {
405 dout.writeUTF(m.getName());
406 dout.write(m.getAccessModifier().ordinal());
412 MessageDigest md = MessageDigest.getInstance("SHA");
413 byte[] hashBytes = md.digest(bout.toByteArray());
415 for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
416 hash = (hash << 8) | (hashBytes[i] & 0xFF);
419 } catch (IOException ex) {
420 throw new InternalError();
421 } catch (NoSuchAlgorithmException ex) {
422 throw new SecurityException(ex.getMessage());
426 public static Restrictions getRestrictions(TypeDefinition<?> type) {
427 final List<LengthConstraint> length = new ArrayList<>();
428 final List<PatternConstraint> pattern = new ArrayList<>();
429 final List<RangeConstraint> range = new ArrayList<>();
431 if (type instanceof ExtendedType) {
432 ExtendedType ext = (ExtendedType)type;
433 TypeDefinition<?> base = ext.getBaseType();
434 length.addAll(ext.getLengthConstraints());
435 pattern.addAll(ext.getPatternConstraints());
436 range.addAll(ext.getRangeConstraints());
438 if (base instanceof IntegerTypeDefinition && range.isEmpty()) {
439 range.addAll(((IntegerTypeDefinition)base).getRangeConstraints());
440 } else if (base instanceof UnsignedIntegerTypeDefinition && range.isEmpty()) {
441 range.addAll(((UnsignedIntegerTypeDefinition)base).getRangeConstraints());
442 } else if (base instanceof DecimalTypeDefinition && range.isEmpty()) {
443 range.addAll(((DecimalTypeDefinition)base).getRangeConstraints());
448 return new Restrictions() {
450 public List<RangeConstraint> getRangeConstraints() {
454 public List<PatternConstraint> getPatternConstraints() {
458 public List<LengthConstraint> getLengthConstraints() {
462 public boolean isEmpty() {
463 return range.isEmpty() && pattern.isEmpty() && length.isEmpty();