Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 19 | import java.io.File; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNode; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 25 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.utils.UtilConstants; |
| 27 | |
| 28 | /** |
| 29 | * Utility Class for translating the name from YANG to java convention. |
| 30 | */ |
| 31 | public final class JavaIdentifierSyntax { |
| 32 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 33 | private static final int MAX_MONTHS = 12; |
| 34 | private static final int MAX_DAYS = 31; |
| 35 | private static final int INDEX_ZERO = 0; |
| 36 | private static final int INDEX_ONE = 1; |
| 37 | private static final int INDEX_TWO = 2; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 38 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 39 | /** |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 40 | * Default constructor. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 41 | */ |
| 42 | private JavaIdentifierSyntax() { |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get the root package string. |
| 47 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 48 | * @param version YANG version |
| 49 | * @param nameSpace name space of the module |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 50 | * @param revision revision of the module defined |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 51 | * @return returns the root package string |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 52 | */ |
| 53 | public static String getRootPackage(byte version, String nameSpace, String revision) { |
| 54 | |
| 55 | String pkg; |
| 56 | pkg = UtilConstants.DEFAULT_BASE_PKG; |
| 57 | pkg = pkg + UtilConstants.PERIOD; |
| 58 | pkg = pkg + getYangVersion(version); |
| 59 | pkg = pkg + UtilConstants.PERIOD; |
| 60 | pkg = pkg + getPkgFromNameSpace(nameSpace); |
| 61 | pkg = pkg + UtilConstants.PERIOD; |
| 62 | pkg = pkg + getYangRevisionStr(revision); |
| 63 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 64 | return pkg.toLowerCase(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 68 | * Get the contained data model parent node. |
| 69 | * |
| 70 | * @param currentNode current node which parent contained node is required |
| 71 | * @return parent node in which the current node is an attribute |
| 72 | */ |
| 73 | public static YangNode getParentNodeInGenCode(YangNode currentNode) { |
| 74 | |
| 75 | /* |
| 76 | * TODO: recursive parent lookup to support choice/augment/uses. TODO: |
| 77 | * need to check if this needs to be updated for |
| 78 | * choice/case/augment/grouping |
| 79 | */ |
| 80 | return currentNode.getParent(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the node package string. |
| 85 | * |
| 86 | * @param curNode current java node whose package string needs to be set |
| 87 | * @return returns the root package string |
| 88 | */ |
| 89 | public static String getCurNodePackage(YangNode curNode) { |
| 90 | |
| 91 | String pkg; |
| 92 | if (!(curNode instanceof HasJavaFileInfo) |
| 93 | || curNode.getParent() == null) { |
| 94 | throw new RuntimeException("missing parent node to get current node's package"); |
| 95 | } |
| 96 | |
| 97 | YangNode parentNode = getParentNodeInGenCode(curNode); |
| 98 | if (!(parentNode instanceof HasJavaFileInfo)) { |
| 99 | throw new RuntimeException("missing parent java node to get current node's package"); |
| 100 | } |
| 101 | JavaFileInfo parentJavaFileHandle = ((HasJavaFileInfo) parentNode).getJavaFileInfo(); |
| 102 | pkg = parentJavaFileHandle.getPackage() + UtilConstants.PERIOD + parentJavaFileHandle.getJavaName(); |
| 103 | return pkg.toLowerCase(); |
| 104 | } |
| 105 | |
| 106 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 107 | * Returns version. |
| 108 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 109 | * @param ver YANG version |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 110 | * @return version |
| 111 | */ |
| 112 | private static String getYangVersion(byte ver) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 113 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 114 | return "v" + ver; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get package name from name space. |
| 119 | * |
| 120 | * @param nameSpace name space of YANG module |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 121 | * @return java package name as per java rules |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 122 | */ |
| 123 | public static String getPkgFromNameSpace(String nameSpace) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 124 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 125 | ArrayList<String> pkgArr = new ArrayList<String>(); |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 126 | nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING); |
| 127 | String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN); |
| 128 | String[] nameSpaceArr = properNameSpace.split(UtilConstants.COLAN); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 129 | |
| 130 | for (String nameSpaceString : nameSpaceArr) { |
| 131 | pkgArr.add(nameSpaceString); |
| 132 | } |
| 133 | return getPkgFrmArr(pkgArr); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns revision string array. |
| 138 | * |
| 139 | * @param date YANG module revision |
| 140 | * @return revision string |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 141 | * @throws TranslatorException when date is invalid. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 142 | */ |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 143 | public static String getYangRevisionStr(String date) throws TranslatorException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 144 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 145 | String[] revisionArr = date.split(UtilConstants.HYPHEN); |
| 146 | |
| 147 | String rev = "rev"; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 148 | rev = rev + revisionArr[INDEX_ZERO]; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 149 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 150 | if (Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 151 | && Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) { |
| 152 | for (int i = INDEX_ONE; i < revisionArr.length; i++) { |
| 153 | |
| 154 | Integer val = Integer.parseInt(revisionArr[i]); |
| 155 | if (val < 10) { |
| 156 | rev = rev + "0"; |
| 157 | } |
| 158 | rev = rev + val; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 159 | } |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 160 | |
| 161 | return rev; |
| 162 | } else { |
| 163 | throw new TranslatorException("Date in revision is not proper: " + date); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 164 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the package string. |
| 169 | * |
| 170 | * @param pkgArr package array |
| 171 | * @return package string |
| 172 | */ |
| 173 | public static String getPkgFrmArr(ArrayList<String> pkgArr) { |
| 174 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 175 | String pkg = UtilConstants.EMPTY_STRING; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 176 | int size = pkgArr.size(); |
| 177 | int i = 0; |
| 178 | for (String member : pkgArr) { |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 179 | boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 180 | if (presenceOfKeyword || member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT)) { |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 181 | member = UtilConstants.UNDER_SCORE + member; |
| 182 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 183 | pkg = pkg + member; |
| 184 | if (i != size - 1) { |
| 185 | pkg = pkg + UtilConstants.PERIOD; |
| 186 | } |
| 187 | i++; |
| 188 | } |
| 189 | return pkg; |
| 190 | } |
| 191 | |
| 192 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 193 | * Get package sub name from YANG identifier name. |
| 194 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 195 | * @param name YANG identifier name |
| 196 | * @return java package sub name as per java rules |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 197 | */ |
| 198 | public static String getSubPkgFromName(String name) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 199 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 200 | ArrayList<String> pkgArr = new ArrayList<String>(); |
| 201 | String[] nameArr = name.split(UtilConstants.COLAN); |
| 202 | |
| 203 | for (String nameString : nameArr) { |
| 204 | pkgArr.add(nameString); |
| 205 | } |
| 206 | return getPkgFrmArr(pkgArr); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Translate the YANG identifier name to java identifier. |
| 211 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 212 | * @param yangIdentifier identifier in YANG file |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 213 | * @return corresponding java identifier |
| 214 | */ |
| 215 | public static String getCamelCase(String yangIdentifier) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 216 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 217 | String[] strArray = yangIdentifier.split(UtilConstants.HYPHEN); |
| 218 | String camelCase = strArray[0]; |
| 219 | for (int i = 1; i < strArray.length; i++) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 220 | camelCase = camelCase + strArray[i].substring(0, 1).toUpperCase() + strArray[i].substring(1); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 221 | } |
| 222 | return camelCase; |
| 223 | } |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 224 | |
| 225 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 226 | * Translate the YANG identifier name to java identifier with first letter |
| 227 | * in caps. |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 228 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 229 | * @param yangIdentifier identifier in YANG file |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 230 | * @return corresponding java identifier |
| 231 | */ |
| 232 | public static String getCaptialCase(String yangIdentifier) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 233 | |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 234 | return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1); |
| 235 | } |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 236 | |
| 237 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 238 | * Translate the YANG identifier name to java identifier with first letter |
| 239 | * in small. |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 240 | * |
| 241 | * @param yangIdentifier identifier in YANG file. |
| 242 | * @return corresponding java identifier |
| 243 | */ |
| 244 | public static String getLowerCase(String yangIdentifier) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 245 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 246 | return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1); |
| 247 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Get the java Package from package path. |
| 251 | * |
| 252 | * @param packagePath package path |
| 253 | * @return java package |
| 254 | */ |
| 255 | public static String getJavaPackageFromPackagePath(String packagePath) { |
| 256 | |
| 257 | return packagePath.replace(File.separator, UtilConstants.PERIOD); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get the directory path corresponding to java package. |
| 262 | * |
| 263 | * @param packagePath package path |
| 264 | * @return java package |
| 265 | */ |
| 266 | public static String getPackageDirPathFromJavaJPackage(String packagePath) { |
| 267 | |
| 268 | return packagePath.replace(UtilConstants.PERIOD, File.separator); |
| 269 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 270 | } |