2 * Copyright (c) 2016 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.binding2.generator.util;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.CharMatcher;
13 import com.google.common.collect.Iterables;
14 import java.util.Iterator;
15 import org.opendaylight.mdsal.binding2.util.BindingMapping;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 * Standard Util class that contains various method for converting
21 * input strings to valid JAVA language strings e.g. package names,
22 * class names, attribute names and/or valid JavaDoc comments.
25 public final class BindingGeneratorUtil {
27 private static final CharMatcher GT_MATCHER = CharMatcher.is('>');
28 private static final CharMatcher LT_MATCHER = CharMatcher.is('<');
30 private BindingGeneratorUtil() {
31 throw new UnsupportedOperationException("Utility class");
35 * Creates package name from specified <code>basePackageName</code> (package
36 * name for module) and <code>schemaPath</code>.
38 * Resulting package name is concatenation of <code>basePackageName</code>
39 * and all local names of YANG nodes which are parents of some node for
40 * which <code>schemaPath</code> is specified.
42 * Based on type of node, there is also possible suffix added in order
43 * to prevent package name conflicts.
45 * @param basePackageName
46 * string with package name of the module, MUST be normalized,
47 * otherwise this method may return an invalid string.
49 * list of names of YANG nodes which are parents of some node +
51 * @return string with valid JAVA package name
52 * @throws NullPointerException if any of the arguments are null
54 public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
55 final Iterable<QName> pathTowardsRoot = schemaPath.getPathTowardsRoot();
56 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
57 final int size = Iterables.size(pathTowardsRoot) - 1;
59 return basePackageName;
62 return generateNormalizedPackageName(basePackageName, pathFromRoot, size);
66 * Creates package name from specified <code>basePackageName</code> (package
67 * name for module) and <code>schemaPath</code> which crosses an augmentation.
69 * Resulting package name is concatenation of <code>basePackageName</code>
70 * and all local names of YANG nodes which are parents of some node for
71 * which <code>schemaPath</code> is specified.
73 * Based on type of node, there is also possible suffix added in order
74 * to prevent package name conflicts.
76 * @param basePackageName
77 * string with package name of the module, MUST be normalized,
78 * otherwise this method may return an invalid string.
80 * list of names of YANG nodes which are parents of some node +
82 * @return string with valid JAVA package name
83 * @throws NullPointerException if any of the arguments are null
85 public static String packageNameForAugmentedGeneratedType(final String basePackageName, final SchemaPath schemaPath) {
86 final Iterable<QName> pathTowardsRoot = schemaPath.getPathTowardsRoot();
87 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
88 final int size = Iterables.size(pathTowardsRoot);
90 return basePackageName;
93 return generateNormalizedPackageName(basePackageName, pathFromRoot, size);
97 private static String generateNormalizedPackageName(final String base, final Iterable<QName> path, final int size) {
98 final StringBuilder builder = new StringBuilder(base);
99 final Iterator<QName> iterator = path.iterator();
100 for (int i = 0; i < size; ++i) {
102 String nodeLocalName = iterator.next().getLocalName();
103 //FIXME: colon or dash in identifier?
104 builder.append(nodeLocalName);
106 return BindingMapping.normalizePackageName(builder.toString());
110 * Encodes angle brackets in yang statement description
111 * @param description description of a yang statement which is used to generate javadoc comments
112 * @return string with encoded angle brackets
114 public static String encodeAngleBrackets(String description) {
115 String newDesc = description;
116 if (newDesc != null) {
117 newDesc = LT_MATCHER.replaceFrom(newDesc, "<");
118 newDesc = GT_MATCHER.replaceFrom(newDesc, ">");
123 //TODO: further implementation of static util methods...