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