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