Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 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.utils.io.impl; |
| 18 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 19 | import java.io.BufferedReader; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 20 | import java.io.BufferedWriter; |
| 21 | import java.io.File; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 22 | import java.io.FileReader; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 23 | import java.io.FileWriter; |
| 24 | import java.io.IOException; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 25 | import java.util.ArrayList; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 26 | import java.util.Arrays; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 27 | import java.util.LinkedList; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 28 | import java.util.List; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 29 | import java.util.Stack; |
| 30 | import java.util.regex.Pattern; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 31 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 32 | import org.apache.commons.io.FileUtils; |
Shankara-Huawei | bdf24bb | 2016-08-02 18:13:13 +0530 | [diff] [blame^] | 33 | import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig; |
| 34 | import org.onosproject.yangutils.datamodel.javadatamodel.YangToJavaNamingConflictUtil; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 36 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.COLAN; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.HASH; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.utils.UtilConstants.ORG; |
| 45 | import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_DIGITS_WITH_SINGLE_LETTER; |
| 48 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT; |
| 49 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_HYPHEN; |
| 50 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_IDENTIFIER_SPECIAL_CHAR; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_PERIOD; |
| 52 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_SINGLE_LETTER; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_UNDERSCORE; |
| 54 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_ALL_SPECIAL_CHAR; |
| 55 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_DIGITS; |
| 56 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE; |
| 57 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES; |
| 58 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_UPPERCASE; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 60 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 61 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE; |
| 64 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 65 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents; |
| 66 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 69 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 70 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 71 | * Represents common utility functionalities for code generation. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 72 | */ |
| 73 | public final class YangIoUtils { |
| 74 | |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 75 | private static final int LINE_SIZE = 118; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 76 | private static final int SUB_LINE_SIZE = 112; |
| 77 | private static final int ZERO = 0; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 78 | |
| 79 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 80 | * Creates an instance of YANG io utils. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 81 | */ |
| 82 | private YangIoUtils() { |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Creates the directory structure. |
| 87 | * |
| 88 | * @param path directory path |
| 89 | * @return directory structure |
| 90 | */ |
| 91 | public static File createDirectories(String path) { |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 92 | File generatedDir = new File(path); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 93 | generatedDir.mkdirs(); |
| 94 | return generatedDir; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Adds package info file for the created directory. |
| 99 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 100 | * @param path directory path |
| 101 | * @param classInfo class info for the package |
| 102 | * @param pack package of the directory |
| 103 | * @param isChildNode is it a child node |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 104 | * @param pluginConfig plugin configurations |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 105 | * @throws IOException when fails to create package info file |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 106 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 107 | public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode, |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 108 | YangPluginConfig pluginConfig) |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 109 | throws IOException { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 110 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 111 | pack = parsePkg(pack); |
| 112 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 113 | try { |
| 114 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 115 | File packageInfo = new File(path + SLASH + "package-info.java"); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 116 | packageInfo.createNewFile(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 117 | |
| 118 | FileWriter fileWriter = new FileWriter(packageInfo); |
| 119 | BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); |
| 120 | |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 121 | bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 122 | bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode, pluginConfig)); |
| 123 | String pkg = PACKAGE + SPACE + pack + SEMI_COLAN; |
| 124 | if (pkg.length() > LINE_SIZE) { |
| 125 | pkg = whenDelimiterIsPersent(pkg, LINE_SIZE); |
| 126 | } |
| 127 | bufferedWriter.write(pkg); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 128 | bufferedWriter.close(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 129 | fileWriter.close(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 130 | } catch (IOException e) { |
| 131 | throw new IOException("Exception occured while creating package info file."); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 136 | * Parses package and returns updated package. |
| 137 | * |
| 138 | * @param pack package needs to be updated |
| 139 | * @return updated package |
| 140 | */ |
| 141 | public static String parsePkg(String pack) { |
| 142 | |
| 143 | if (pack.contains(ORG)) { |
| 144 | String[] strArray = pack.split(ORG); |
| 145 | if (strArray.length >= 3) { |
| 146 | for (int i = 1; i < strArray.length; i++) { |
| 147 | if (i == 1) { |
| 148 | pack = ORG + strArray[1]; |
| 149 | } else { |
| 150 | pack = pack + ORG + strArray[i]; |
| 151 | } |
| 152 | } |
| 153 | } else { |
| 154 | pack = ORG + strArray[1]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return pack; |
| 159 | } |
| 160 | |
| 161 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 162 | * Cleans the generated directory if already exist in source folder. |
| 163 | * |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 164 | * @param dir generated directory in previous build |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 165 | * @throws IOException when failed to delete directory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 166 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 167 | public static void deleteDirectory(String dir) |
| 168 | throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 169 | File generatedDirectory = new File(dir); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 170 | if (generatedDirectory.exists()) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 171 | try { |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 172 | FileUtils.deleteDirectory(generatedDirectory); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 173 | } catch (IOException e) { |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 174 | throw new IOException( |
| 175 | "Failed to delete the generated files in " + generatedDirectory + " directory"); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 176 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 181 | * Searches and deletes generated temporary directories. |
| 182 | * |
| 183 | * @param root root directory |
| 184 | * @throws IOException when fails to do IO operations. |
| 185 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 186 | public static void searchAndDeleteTempDir(String root) |
| 187 | throws IOException { |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 188 | List<File> store = new LinkedList<>(); |
| 189 | Stack<String> stack = new Stack<>(); |
| 190 | stack.push(root); |
| 191 | |
| 192 | while (!stack.empty()) { |
| 193 | root = stack.pop(); |
| 194 | File file = new File(root); |
| 195 | File[] filelist = file.listFiles(); |
| 196 | if (filelist == null || filelist.length == 0) { |
| 197 | continue; |
| 198 | } |
| 199 | for (File current : filelist) { |
| 200 | if (current.isDirectory()) { |
| 201 | stack.push(current.toString()); |
| 202 | if (current.getName().endsWith("-Temp")) { |
| 203 | store.add(current); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | for (File dir : store) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 210 | FileUtils.deleteDirectory(dir); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 215 | * Removes extra char from the string. |
| 216 | * |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 217 | * @param valueString string to be trimmed |
| 218 | * @param removalStirng extra chars |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 219 | * @return new string |
| 220 | */ |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 221 | public static String trimAtLast(String valueString, String removalStirng) { |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 222 | StringBuilder stringBuilder = new StringBuilder(valueString); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 223 | int index = valueString.lastIndexOf(removalStirng); |
| 224 | if (index != -1) { |
| 225 | stringBuilder.deleteCharAt(index); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 226 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 227 | return stringBuilder.toString(); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 228 | } |
| 229 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 230 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 231 | * Returns the directory path of the package in canonical form. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 232 | * |
| 233 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 234 | * put |
| 235 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 236 | * @return absolute path of the package in canonical form |
| 237 | */ |
| 238 | public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) { |
| 239 | |
| 240 | if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 241 | pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 242 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 243 | String[] strArray = pathOfJavaPkg.split(SLASH); |
| 244 | if (strArray[0].equals(EMPTY_STRING)) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 245 | return pathOfJavaPkg; |
| 246 | } else { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 247 | return baseCodeGenPath + SLASH + pathOfJavaPkg; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 252 | * Returns the absolute path of the package in canonical form. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 253 | * |
| 254 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 255 | * put |
| 256 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 257 | * @return absolute path of the package in canonical form |
| 258 | */ |
| 259 | public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 260 | return baseCodeGenPath + pathOfJavaPkg; |
| 261 | } |
| 262 | |
| 263 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 264 | * Merges the temp java files to main java files. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 265 | * |
| 266 | * @param appendFile temp file |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 267 | * @param srcFile main file |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 268 | * @throws IOException when fails to append contents |
| 269 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 270 | public static void mergeJavaFiles(File appendFile, File srcFile) |
| 271 | throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 272 | try { |
| 273 | appendFileContents(appendFile, srcFile); |
| 274 | } catch (IOException e) { |
| 275 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 280 | * Inserts data in the generated file. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 281 | * |
| 282 | * @param file file in which need to be inserted |
| 283 | * @param data data which need to be inserted |
| 284 | * @throws IOException when fails to insert into file |
| 285 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 286 | public static void insertDataIntoJavaFile(File file, String data) |
| 287 | throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 288 | try { |
| 289 | updateFileHandle(file, data, false); |
| 290 | } catch (IOException e) { |
| 291 | throw new IOException("Failed to insert in " + file + "file"); |
| 292 | } |
| 293 | } |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Validates a line size in given file whether it is having more then 120 characters. |
| 297 | * If yes it will update and give a new file. |
| 298 | * |
| 299 | * @param dataFile file in which need to verify all lines. |
| 300 | * @return updated file |
| 301 | * @throws IOException when fails to do IO operations. |
| 302 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 303 | public static File validateLineLength(File dataFile) |
| 304 | throws IOException { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 305 | File tempFile = dataFile; |
| 306 | FileReader fileReader = new FileReader(dataFile); |
| 307 | BufferedReader bufferReader = new BufferedReader(fileReader); |
| 308 | try { |
| 309 | StringBuilder stringBuilder = new StringBuilder(); |
| 310 | String line = bufferReader.readLine(); |
| 311 | |
| 312 | while (line != null) { |
| 313 | if (line.length() > LINE_SIZE) { |
| 314 | if (line.contains(PERIOD)) { |
| 315 | line = whenDelimiterIsPersent(line, LINE_SIZE); |
| 316 | } else if (line.contains(SPACE)) { |
| 317 | line = whenSpaceIsPresent(line, LINE_SIZE); |
| 318 | } |
| 319 | stringBuilder.append(line); |
| 320 | } else { |
| 321 | stringBuilder.append(line + NEW_LINE); |
| 322 | } |
| 323 | line = bufferReader.readLine(); |
| 324 | } |
| 325 | FileWriter writer = new FileWriter(tempFile); |
| 326 | writer.write(stringBuilder.toString()); |
| 327 | writer.close(); |
| 328 | return tempFile; |
| 329 | } finally { |
| 330 | fileReader.close(); |
| 331 | bufferReader.close(); |
| 332 | } |
| 333 | } |
| 334 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 335 | /* When delimiters are present in the given line. */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 336 | private static String whenDelimiterIsPersent(String line, int lineSize) { |
| 337 | StringBuilder stringBuilder = new StringBuilder(); |
| 338 | |
| 339 | if (line.length() > lineSize) { |
| 340 | String[] strArray = line.split(Pattern.quote(PERIOD)); |
| 341 | stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize); |
| 342 | } else { |
| 343 | stringBuilder.append(line + NEW_LINE); |
| 344 | } |
| 345 | String[] strArray = stringBuilder.toString().split(NEW_LINE); |
| 346 | StringBuilder tempBuilder = new StringBuilder(); |
| 347 | for (String str : strArray) { |
| 348 | if (str.length() > SUB_LINE_SIZE) { |
| 349 | if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) { |
| 350 | String[] strArr = str.split(Pattern.quote(PERIOD)); |
| 351 | tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE); |
| 352 | } else if (str.contains(SPACE)) { |
| 353 | tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE)); |
| 354 | } |
| 355 | } else { |
| 356 | tempBuilder.append(str + NEW_LINE); |
| 357 | } |
| 358 | } |
| 359 | return tempBuilder.toString(); |
| 360 | |
| 361 | } |
| 362 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 363 | /* When spaces are present in the given line. */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 364 | private static String whenSpaceIsPresent(String line, int lineSize) { |
| 365 | StringBuilder stringBuilder = new StringBuilder(); |
| 366 | if (line.length() > lineSize) { |
| 367 | String[] strArray = line.split(SPACE); |
| 368 | stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize); |
| 369 | } else { |
| 370 | stringBuilder.append(line + NEW_LINE); |
| 371 | } |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 372 | |
| 373 | String[] strArray = stringBuilder.toString().split(NEW_LINE); |
| 374 | StringBuilder tempBuilder = new StringBuilder(); |
| 375 | for (String str : strArray) { |
| 376 | if (str.length() > SUB_LINE_SIZE) { |
| 377 | if (str.contains(SPACE)) { |
| 378 | String[] strArr = str.split(SPACE); |
| 379 | tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE); |
| 380 | } |
| 381 | } else { |
| 382 | tempBuilder.append(str + NEW_LINE); |
| 383 | } |
| 384 | } |
| 385 | return tempBuilder.toString(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 386 | } |
| 387 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 388 | /* Updates the given line with the given size conditions. */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 389 | private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string, |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 390 | int lineSize) { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 391 | |
| 392 | StringBuilder tempBuilder = new StringBuilder(); |
| 393 | for (String str : strArray) { |
| 394 | tempBuilder.append(str + string); |
| 395 | if (tempBuilder.length() > lineSize) { |
| 396 | String tempString = stringBuilder.toString(); |
| 397 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 398 | tempString = trimAtLast(tempString, string); |
| 399 | stringBuilder.append(tempString); |
| 400 | if (string.equals(PERIOD)) { |
| 401 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string); |
| 402 | } else { |
| 403 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string); |
| 404 | } |
| 405 | tempBuilder.delete(ZERO, tempBuilder.length()); |
| 406 | tempBuilder.append(TWELVE_SPACE_INDENTATION); |
| 407 | } else { |
| 408 | stringBuilder.append(str + string); |
| 409 | } |
| 410 | } |
| 411 | String tempString = stringBuilder.toString(); |
| 412 | tempString = trimAtLast(tempString, string); |
| 413 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 414 | stringBuilder.append(tempString + NEW_LINE); |
| 415 | return stringBuilder; |
| 416 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 417 | |
| 418 | /** |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 419 | * Returns the java Package from package path. |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 420 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 421 | * @param packagePath package path |
| 422 | * @return java package |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 423 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 424 | public static String getJavaPackageFromPackagePath(String packagePath) { |
| 425 | return packagePath.replace(SLASH, PERIOD); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 429 | * Returns the directory path corresponding to java package. |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 430 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 431 | * @param packagePath package path |
| 432 | * @return java package |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 433 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 434 | public static String getPackageDirPathFromJavaJPackage(String packagePath) { |
| 435 | return packagePath.replace(PERIOD, SLASH); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 439 | * Returns the YANG identifier name as java identifier with first letter |
| 440 | * in small. |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 441 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 442 | * @param yangIdentifier identifier in YANG file. |
| 443 | * @return corresponding java identifier |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 444 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 445 | public static String getSmallCase(String yangIdentifier) { |
| 446 | return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 450 | * Returns the YANG identifier name as java identifier with first letter |
| 451 | * in capital. |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 452 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 453 | * @param yangIdentifier identifier in YANG file |
| 454 | * @return corresponding java identifier |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 455 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 456 | public static String getCapitalCase(String yangIdentifier) { |
| 457 | yangIdentifier = yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1); |
| 458 | return restrictConsecutiveCapitalCase(yangIdentifier); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 462 | * Restricts consecutive capital cased string as a rule in camel case. |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 463 | * |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 464 | * @param consecCapitalCaseRemover which requires the restriction of consecutive capital case |
| 465 | * @return string without consecutive capital case |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 466 | */ |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 467 | public static String restrictConsecutiveCapitalCase(String consecCapitalCaseRemover) { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 468 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 469 | for (int k = 0; k < consecCapitalCaseRemover.length(); k++) { |
| 470 | if (k + 1 < consecCapitalCaseRemover.length()) { |
| 471 | if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k))) { |
| 472 | if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k + 1))) { |
| 473 | consecCapitalCaseRemover = consecCapitalCaseRemover.substring(0, k + 1) |
| 474 | + consecCapitalCaseRemover.substring(k + 1, k + 2).toLowerCase() |
| 475 | + consecCapitalCaseRemover.substring(k + 2); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 476 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 477 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 478 | } |
| 479 | } |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 480 | return consecCapitalCaseRemover; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 481 | } |
| 482 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 483 | /** |
| 484 | * Adds prefix, if the string begins with digit or is a java key word. |
| 485 | * |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 486 | * @param camelCasePrefix string for adding prefix |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 487 | * @param conflictResolver object of YANG to java naming conflict util |
| 488 | * @return prefixed camel case string |
| 489 | */ |
| 490 | public static String addPrefix(String camelCasePrefix, YangToJavaNamingConflictUtil conflictResolver) { |
| 491 | |
| 492 | String prefix = getPrefixForIdentifier(conflictResolver); |
| 493 | if (camelCasePrefix.matches(REGEX_FOR_FIRST_DIGIT)) { |
| 494 | camelCasePrefix = prefix + camelCasePrefix; |
| 495 | } |
| 496 | if (JAVA_KEY_WORDS.contains(camelCasePrefix)) { |
| 497 | camelCasePrefix = prefix + camelCasePrefix.substring(0, 1).toUpperCase() |
| 498 | + camelCasePrefix.substring(1); |
| 499 | } |
| 500 | return camelCasePrefix; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Applies the rule that a string does not end with a capitalized letter and capitalizes |
| 505 | * the letter next to a number in an array. |
| 506 | * |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 507 | * @param stringArray containing strings for camel case separation |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 508 | * @param conflictResolver object of YANG to java naming conflict util |
| 509 | * @return camel case rule checked string |
| 510 | */ |
| 511 | public static String applyCamelCaseRule(String[] stringArray, YangToJavaNamingConflictUtil conflictResolver) { |
| 512 | |
| 513 | String ruleChecker = stringArray[0].toLowerCase(); |
| 514 | int i; |
| 515 | if (ruleChecker.matches(REGEX_FOR_FIRST_DIGIT)) { |
| 516 | i = 0; |
| 517 | ruleChecker = EMPTY_STRING; |
| 518 | } else { |
| 519 | i = 1; |
| 520 | } |
| 521 | for (; i < stringArray.length; i++) { |
| 522 | if (i + 1 == stringArray.length) { |
| 523 | if (stringArray[i].matches(REGEX_FOR_SINGLE_LETTER) |
| 524 | || stringArray[i].matches(REGEX_FOR_DIGITS_WITH_SINGLE_LETTER)) { |
| 525 | ruleChecker = ruleChecker + stringArray[i].toLowerCase(); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | if (stringArray[i].matches(REGEX_FOR_FIRST_DIGIT)) { |
| 530 | for (int j = 0; j < stringArray[i].length(); j++) { |
| 531 | char letterCheck = stringArray[i].charAt(j); |
| 532 | if (Character.isLetter(letterCheck)) { |
| 533 | stringArray[i] = stringArray[i].substring(0, j) |
| 534 | + stringArray[i].substring(j, j + 1).toUpperCase() + stringArray[i].substring(j + 1); |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | ruleChecker = ruleChecker + stringArray[i]; |
| 539 | } else { |
| 540 | ruleChecker = ruleChecker + stringArray[i].substring(0, 1).toUpperCase() + stringArray[i].substring(1); |
| 541 | } |
| 542 | } |
| 543 | String ruleCheckerWithPrefix = addPrefix(ruleChecker, conflictResolver); |
| 544 | return restrictConsecutiveCapitalCase(ruleCheckerWithPrefix); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Resolves the conflict when input has upper case. |
| 549 | * |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 550 | * @param stringArray containing strings for upper case conflict resolver |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 551 | * @param conflictResolver object of YANG to java naming conflict util |
| 552 | * @return camel cased string |
| 553 | */ |
| 554 | public static String upperCaseConflictResolver(String[] stringArray, |
| 555 | YangToJavaNamingConflictUtil conflictResolver) { |
| 556 | |
| 557 | for (int l = 0; l < stringArray.length; l++) { |
| 558 | String[] upperCaseSplitArray = stringArray[l].split(REGEX_WITH_UPPERCASE); |
| 559 | for (int m = 0; m < upperCaseSplitArray.length; m++) { |
| 560 | if (upperCaseSplitArray[m].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) { |
| 561 | int check = m; |
| 562 | while (check + 1 < upperCaseSplitArray.length) { |
| 563 | if (upperCaseSplitArray[check + 1].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) { |
| 564 | upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase(); |
| 565 | check = check + 1; |
| 566 | } else if (upperCaseSplitArray[check + 1] |
| 567 | .matches(REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES)) { |
| 568 | upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase(); |
| 569 | break; |
| 570 | } else { |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | StringBuilder strBuilder = new StringBuilder(); |
| 577 | for (String element : upperCaseSplitArray) { |
| 578 | strBuilder.append(element); |
| 579 | } |
| 580 | stringArray[l] = strBuilder.toString(); |
| 581 | } |
| 582 | List<String> result = new ArrayList<String>(); |
| 583 | for (String element : stringArray) { |
| 584 | String[] capitalCaseSplitArray = element.split(REGEX_WITH_UPPERCASE); |
| 585 | for (String letter : capitalCaseSplitArray) { |
| 586 | String[] arrayForAddition = letter.split(REGEX_WITH_DIGITS); |
| 587 | List<String> list = Arrays.asList(arrayForAddition); |
| 588 | for (String str : list) { |
| 589 | if (str != null && !str.isEmpty()) { |
| 590 | result.add(str); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | stringArray = result.toArray(new String[result.size()]); |
| 596 | return applyCamelCaseRule(stringArray, conflictResolver); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Returns the YANG identifier name as java identifier. |
| 601 | * |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 602 | * @param yangIdentifier identifier in YANG file |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 603 | * @param conflictResolver object of YANG to java naming conflict util |
| 604 | * @return corresponding java identifier |
| 605 | */ |
| 606 | public static String getCamelCase(String yangIdentifier, YangToJavaNamingConflictUtil conflictResolver) { |
| 607 | |
| 608 | if (conflictResolver != null) { |
| 609 | String replacementForHyphen = conflictResolver.getReplacementForHyphen(); |
| 610 | String replacementForPeriod = conflictResolver.getReplacementForPeriod(); |
| 611 | String replacementForUnderscore = conflictResolver.getReplacementForUnderscore(); |
| 612 | if (replacementForPeriod != null) { |
| 613 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_PERIOD, |
| 614 | PERIOD + replacementForPeriod.toLowerCase() + PERIOD); |
| 615 | } |
| 616 | if (replacementForUnderscore != null) { |
| 617 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_UNDERSCORE, |
| 618 | UNDER_SCORE + replacementForUnderscore.toLowerCase() + UNDER_SCORE); |
| 619 | } |
| 620 | if (replacementForHyphen != null) { |
| 621 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_HYPHEN, |
| 622 | HYPHEN + replacementForHyphen.toLowerCase() + HYPHEN); |
| 623 | } |
| 624 | } |
| 625 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_IDENTIFIER_SPECIAL_CHAR, COLAN); |
| 626 | String[] strArray = yangIdentifier.split(COLAN); |
| 627 | if (strArray[0].isEmpty()) { |
| 628 | List<String> stringArrangement = new ArrayList<String>(); |
| 629 | for (int i = 1; i < strArray.length; i++) { |
| 630 | stringArrangement.add(strArray[i]); |
| 631 | } |
| 632 | strArray = stringArrangement.toArray(new String[stringArrangement.size()]); |
| 633 | } |
| 634 | return upperCaseConflictResolver(strArray, conflictResolver); |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Prefix for adding with identifier and namespace, when it is a java keyword or starting with digits. |
| 639 | * |
| 640 | * @param conflictResolver object of YANG to java naming conflict util |
| 641 | * @return prefix which needs to be added |
| 642 | */ |
| 643 | public static String getPrefixForIdentifier(YangToJavaNamingConflictUtil conflictResolver) { |
| 644 | |
| 645 | String prefixForIdentifier = null; |
| 646 | if (conflictResolver != null) { |
| 647 | prefixForIdentifier = conflictResolver.getPrefixForIdentifier(); |
| 648 | } |
| 649 | if (prefixForIdentifier != null) { |
| 650 | prefixForIdentifier = prefixForIdentifier.replaceAll(REGEX_WITH_ALL_SPECIAL_CHAR, COLAN); |
| 651 | String[] strArray = prefixForIdentifier.split(COLAN); |
| 652 | try { |
| 653 | if (strArray[0].isEmpty()) { |
| 654 | List<String> stringArrangement = new ArrayList<String>(); |
| 655 | for (int i = 1; i < strArray.length; i++) { |
| 656 | stringArrangement.add(strArray[i]); |
| 657 | } |
| 658 | strArray = stringArrangement.toArray(new String[stringArrangement.size()]); |
| 659 | } |
| 660 | prefixForIdentifier = strArray[0]; |
| 661 | for (int j = 1; j < strArray.length; j++) { |
| 662 | prefixForIdentifier = prefixForIdentifier + strArray[j].substring(0, 1).toUpperCase() + |
| 663 | strArray[j].substring(1); |
| 664 | } |
| 665 | } catch (ArrayIndexOutOfBoundsException outOfBoundsException) { |
| 666 | throw new TranslatorException("The given prefix in pom.xml is invalid."); |
| 667 | } |
| 668 | } else { |
| 669 | prefixForIdentifier = YANG_AUTO_PREFIX; |
| 670 | } |
| 671 | return prefixForIdentifier; |
| 672 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 673 | } |