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