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