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