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