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 | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 19 | import org.apache.commons.io.FileUtils; |
| 20 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 21 | import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil; |
| 22 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 23 | import java.io.BufferedReader; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 24 | import java.io.BufferedWriter; |
| 25 | import java.io.File; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 26 | import java.io.FileReader; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 27 | import java.io.FileWriter; |
| 28 | import java.io.IOException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 29 | import java.util.ArrayList; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 30 | import java.util.Arrays; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 31 | import java.util.Iterator; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 32 | import java.util.LinkedList; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 33 | import java.util.List; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 34 | import java.util.Stack; |
| 35 | import java.util.regex.Pattern; |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 36 | |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 37 | import static java.lang.Integer.parseInt; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.COLON; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
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 | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLON; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 65 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 66 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.utils.UtilConstants.UNUSED; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents; |
| 73 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 77 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 78 | * Represents common utility functionalities for code generation. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 79 | */ |
| 80 | public final class YangIoUtils { |
| 81 | |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 82 | private static final int LINE_SIZE = 118; |
| 83 | private static final int SUB_LINE_SIZE = 116; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 84 | private static final int SUB_SIZE = 60; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 85 | |
| 86 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 87 | * Creates an instance of YANG io utils. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 88 | */ |
| 89 | private YangIoUtils() { |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates the directory structure. |
| 94 | * |
| 95 | * @param path directory path |
| 96 | * @return directory structure |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 97 | * @throws IOException when fails to do IO operations |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 98 | */ |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 99 | public static File createDirectories(String path) |
| 100 | throws IOException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 101 | File generatedDir = new File(path); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 102 | if (!generatedDir.exists()) { |
| 103 | boolean isGenerated = generatedDir.mkdirs(); |
| 104 | if (!isGenerated) { |
| 105 | throw new IOException("failed to generated directory " + path); |
| 106 | } |
| 107 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 108 | return generatedDir; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Adds package info file for the created directory. |
| 113 | * |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 114 | * @param path directory path |
| 115 | * @param classInfo class info for the package |
| 116 | * @param pack package of the directory |
| 117 | * @param isChildNode is it a child node |
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 | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 120 | public static void addPackageInfo(File path, String classInfo, String pack, |
| 121 | boolean isChildNode) 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); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 124 | try { |
| 125 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 126 | File packageInfo = new File(path + SLASH + "package-info.java"); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 127 | if (!packageInfo.exists()) { |
| 128 | boolean isGenerated = packageInfo.createNewFile(); |
| 129 | if (!isGenerated) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 130 | throw new IOException("failed to generated package-info " + |
| 131 | path); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 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 |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 139 | bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode, |
| 140 | null)); |
| 141 | String pkg = PACKAGE + SPACE + pack + SEMI_COLON; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 142 | if (pkg.length() >= LINE_SIZE) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 143 | pkg = processModifications(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 | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 149 | throw new IOException("Exception occurred while creating package info" + |
| 150 | " file."); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 155 | * Parses package and returns updated package. |
| 156 | * |
| 157 | * @param pack package needs to be updated |
| 158 | * @return updated package |
| 159 | */ |
| 160 | public static String parsePkg(String pack) { |
| 161 | |
| 162 | if (pack.contains(ORG)) { |
| 163 | String[] strArray = pack.split(ORG); |
| 164 | if (strArray.length >= 3) { |
| 165 | for (int i = 1; i < strArray.length; i++) { |
| 166 | if (i == 1) { |
| 167 | pack = ORG + strArray[1]; |
| 168 | } else { |
| 169 | pack = pack + ORG + strArray[i]; |
| 170 | } |
| 171 | } |
| 172 | } else { |
| 173 | pack = ORG + strArray[1]; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return pack; |
| 178 | } |
| 179 | |
| 180 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 181 | * Cleans the generated directory if already exist in source folder. |
| 182 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 183 | * @param dir generated directory in previous build |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 184 | * @throws IOException when failed to delete directory |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 185 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 186 | public static void deleteDirectory(String dir) |
| 187 | throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 188 | File generatedDirectory = new File(dir); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 189 | if (generatedDirectory.exists()) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 190 | try { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 191 | FileUtils.deleteDirectory(generatedDirectory); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 192 | } catch (IOException e) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 193 | throw new IOException( |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 194 | "Failed to delete the generated files in " + |
| 195 | generatedDirectory + " directory"); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 196 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 201 | * Searches and deletes generated temporary directories. |
| 202 | * |
| 203 | * @param root root directory |
| 204 | * @throws IOException when fails to do IO operations. |
| 205 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 206 | public static void searchAndDeleteTempDir(String root) |
| 207 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 208 | List<File> store = new LinkedList<>(); |
| 209 | Stack<String> stack = new Stack<>(); |
| 210 | stack.push(root); |
| 211 | |
| 212 | while (!stack.empty()) { |
| 213 | root = stack.pop(); |
| 214 | File file = new File(root); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 215 | File[] fileList = file.listFiles(); |
| 216 | if (fileList == null || fileList.length == 0) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 217 | continue; |
| 218 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 219 | for (File current : fileList) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 220 | if (current.isDirectory()) { |
| 221 | stack.push(current.toString()); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 222 | if (current.getName().endsWith(HYPHEN + TEMP)) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 223 | store.add(current); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | for (File dir : store) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 230 | FileUtils.deleteDirectory(dir); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 235 | * Removes extra char from the string. |
| 236 | * |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 237 | * @param valueString string to be trimmed |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 238 | * @param removalString extra chars |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 239 | * @return new string |
| 240 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 241 | public static String trimAtLast(String valueString, String... |
| 242 | removalString) { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 243 | StringBuilder stringBuilder = new StringBuilder(valueString); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 244 | String midString; |
| 245 | int index; |
| 246 | for (String remove : removalString) { |
| 247 | midString = stringBuilder.toString(); |
| 248 | index = midString.lastIndexOf(remove); |
| 249 | if (index != -1) { |
| 250 | stringBuilder.deleteCharAt(index); |
| 251 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 252 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 253 | return stringBuilder.toString(); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 254 | } |
| 255 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 256 | /** |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 257 | * Replaces the last occurrence of a string with a given string. |
| 258 | * |
| 259 | * @param valueString string under operation |
| 260 | * @param removalString string to be replaced |
| 261 | * @param replacingString string with which replacement is to be done |
| 262 | * @return new string |
| 263 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 264 | public static String replaceLast(String valueString, String removalString, |
| 265 | String replacingString) { |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 266 | StringBuilder stringBuilder = new StringBuilder(valueString); |
| 267 | int index = valueString.lastIndexOf(removalString); |
| 268 | if (index != -1) { |
| 269 | stringBuilder.replace(index, index + 1, replacingString); |
| 270 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 271 | stringBuilder.append(NEW_LINE + EIGHT_SPACE_INDENTATION + UNUSED + |
| 272 | OPEN_PARENTHESIS + ONE + |
| 273 | CLOSE_PARENTHESIS + SEMI_COLON); |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 274 | } |
| 275 | return stringBuilder.toString(); |
| 276 | |
| 277 | // TODO remove generation of ENUM if there is no leaf node. |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 282 | * Returns the directory path of the package in canonical form. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 283 | * |
| 284 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 285 | * put |
| 286 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 287 | * @return absolute path of the package in canonical form |
| 288 | */ |
| 289 | public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) { |
| 290 | |
| 291 | if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 292 | pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 293 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 294 | String[] strArray = pathOfJavaPkg.split(SLASH); |
| 295 | if (strArray[0].equals(EMPTY_STRING)) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 296 | return pathOfJavaPkg; |
| 297 | } else { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 298 | return baseCodeGenPath + SLASH + pathOfJavaPkg; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 303 | * Returns the absolute path of the package in canonical form. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 304 | * |
| 305 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 306 | * put |
| 307 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 308 | * @return absolute path of the package in canonical form |
| 309 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 310 | public static String getAbsolutePackagePath(String baseCodeGenPath, |
| 311 | String pathOfJavaPkg) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 312 | return baseCodeGenPath + pathOfJavaPkg; |
| 313 | } |
| 314 | |
| 315 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 316 | * Merges the temp java files to main java files. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 317 | * |
| 318 | * @param appendFile temp file |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 319 | * @param srcFile main file |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 320 | * @throws IOException when fails to append contents |
| 321 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 322 | public static void mergeJavaFiles(File appendFile, File srcFile) |
| 323 | throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 324 | try { |
| 325 | appendFileContents(appendFile, srcFile); |
| 326 | } catch (IOException e) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 327 | throw new IOException("Failed to merge " + appendFile + " in " + |
| 328 | srcFile); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 333 | * Inserts data in the generated file. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 334 | * |
| 335 | * @param file file in which need to be inserted |
| 336 | * @param data data which need to be inserted |
| 337 | * @throws IOException when fails to insert into file |
| 338 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 339 | public static void insertDataIntoJavaFile(File file, String data) |
| 340 | throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 341 | try { |
| 342 | updateFileHandle(file, data, false); |
| 343 | } catch (IOException e) { |
| 344 | throw new IOException("Failed to insert in " + file + "file"); |
| 345 | } |
| 346 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 347 | |
| 348 | /** |
| 349 | * Validates a line size in given file whether it is having more then 120 characters. |
| 350 | * If yes it will update and give a new file. |
| 351 | * |
| 352 | * @param dataFile file in which need to verify all lines. |
| 353 | * @return updated file |
| 354 | * @throws IOException when fails to do IO operations. |
| 355 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 356 | public static File validateLineLength(File dataFile) |
| 357 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 358 | FileReader fileReader = new FileReader(dataFile); |
| 359 | BufferedReader bufferReader = new BufferedReader(fileReader); |
| 360 | try { |
| 361 | StringBuilder stringBuilder = new StringBuilder(); |
| 362 | String line = bufferReader.readLine(); |
| 363 | |
| 364 | while (line != null) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 365 | if (line.length() >= LINE_SIZE) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 366 | line = processModifications(line, LINE_SIZE); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 367 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 368 | stringBuilder.append(line); |
| 369 | stringBuilder.append(NEW_LINE); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 370 | line = bufferReader.readLine(); |
| 371 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 372 | FileWriter writer = new FileWriter(dataFile); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 373 | writer.write(stringBuilder.toString()); |
| 374 | writer.close(); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 375 | return dataFile; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 376 | } finally { |
| 377 | fileReader.close(); |
| 378 | bufferReader.close(); |
| 379 | } |
| 380 | } |
| 381 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 382 | /** |
| 383 | * Resolves validation of line length by modifying the string. |
| 384 | * |
| 385 | * @param line current line string |
| 386 | * @param lineSize line size for change |
| 387 | * @return modified line string |
| 388 | */ |
| 389 | private static String processModifications(String line, int lineSize) { |
| 390 | int period = getArrayLength(line, PERIOD); |
| 391 | int space = getArrayLength(line, SPACE); |
| 392 | if (period > space) { |
| 393 | return merge(getForPeriod(line), PERIOD, lineSize); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 394 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 395 | return merge(getForSpace(line), SPACE, lineSize); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 396 | } |
| 397 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 398 | /** |
| 399 | * Returns count of pattern in line. |
| 400 | * |
| 401 | * @param line line string |
| 402 | * @param pattern pattern followed in line |
| 403 | * @return count of pattern in line |
| 404 | */ |
| 405 | private static int getArrayLength(String line, String pattern) { |
| 406 | String[] array = line.split(Pattern.quote(pattern)); |
| 407 | int len = array.length; |
| 408 | if (pattern.equals(SPACE)) { |
| 409 | for (String str : array) { |
| 410 | if (str.equals(EMPTY_STRING)) { |
| 411 | len--; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 412 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 413 | } |
| 414 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 415 | return len - 1; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 416 | } |
| 417 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 418 | /** |
| 419 | * Returns array list of string in case of period. |
| 420 | * |
| 421 | * @param line line string |
| 422 | * @return array list of string in case of period |
| 423 | */ |
| 424 | private static ArrayList<String> getForPeriod(String line) { |
| 425 | String[] array = line.split(Pattern.quote(PERIOD)); |
| 426 | return getSplitArray(array, PERIOD); |
| 427 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 428 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 429 | /** |
| 430 | * Returns array list of string in case of space. |
| 431 | * |
| 432 | * @param line line string |
| 433 | * @return array list of string in case of space |
| 434 | */ |
| 435 | private static ArrayList<String> getForSpace(String line) { |
| 436 | String[] array = line.split(SPACE); |
| 437 | return getSplitArray(array, SPACE); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Merges strings to form a new string. |
| 442 | * |
| 443 | * @param list list of strings |
| 444 | * @param pattern pattern |
| 445 | * @param lineSize line size |
| 446 | * @return merged string |
| 447 | */ |
| 448 | private static String merge(ArrayList<String> list, String pattern, int lineSize) { |
| 449 | StringBuilder builder = new StringBuilder(); |
| 450 | StringBuilder fine = new StringBuilder(); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 451 | String append; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 452 | String pre; |
| 453 | String present = EMPTY_STRING; |
| 454 | //Add one blank string in list to handle border limit cases. |
| 455 | list.add(EMPTY_STRING); |
| 456 | Iterator<String> listIt = list.iterator(); |
| 457 | ArrayList<String> arrayList = new ArrayList<>(); |
| 458 | int length; |
| 459 | StringBuilder spaces = new StringBuilder(); |
| 460 | while (listIt.hasNext()) { |
| 461 | pre = present; |
| 462 | present = listIt.next(); |
| 463 | //check is present string is more than 80 char. |
| 464 | if (present.length() > SUB_SIZE) { |
| 465 | int period = getArrayLength(present, PERIOD); |
| 466 | int space = getArrayLength(present, SPACE); |
| 467 | if (period > space) { |
| 468 | // in such case present string should be resolved. |
| 469 | present = processModifications(present, SUB_SIZE); |
| 470 | builder.append(present); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 471 | } |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 472 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 473 | length = builder.length(); |
| 474 | //If length of builder is less than the given length then append |
| 475 | // it to builder. |
| 476 | if (length <= lineSize) { |
| 477 | //fill the space builder to provide proper indentation. |
| 478 | if (present.equals(EMPTY_STRING)) { |
| 479 | spaces.append(SPACE); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 480 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 481 | //append to builder |
| 482 | builder.append(present); |
| 483 | builder.append(pattern); |
| 484 | fine.append(pre); |
| 485 | //do not append pattern in case of empty strings. |
| 486 | if (!pre.equals(EMPTY_STRING)) { |
| 487 | fine.append(pattern); |
| 488 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 489 | } else { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 490 | // now the length is more than given size so trim the pattern |
| 491 | // for the string and add it to list, |
| 492 | fine = getReplacedString(fine, pattern); |
| 493 | arrayList.add(fine.toString()); |
| 494 | // clear all. |
| 495 | builder.delete(0, length); |
| 496 | fine.delete(0, fine.length()); |
| 497 | // append indentation |
| 498 | if (pattern.contains(PERIOD)) { |
| 499 | append = NEW_LINE + spaces + |
| 500 | TWELVE_SPACE_INDENTATION + |
| 501 | PERIOD; |
| 502 | } else { |
| 503 | append = NEW_LINE + spaces + TWELVE_SPACE_INDENTATION; |
| 504 | } |
| 505 | // builder needs to move one step forward to fine builder so |
| 506 | // append present and pre strings to builder with pattern. |
| 507 | builder.append(append); |
| 508 | builder.append(pre); |
| 509 | builder.append(pattern); |
| 510 | builder.append(present); |
| 511 | builder.append(pattern); |
| 512 | fine.append(append); |
| 513 | fine.append(pre); |
| 514 | if (!pre.equals(EMPTY_STRING)) { |
| 515 | fine.append(pattern); |
| 516 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 517 | } |
| 518 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 519 | |
| 520 | builder = getReplacedString(builder, pattern); |
| 521 | |
| 522 | //need to remove extra string added from the builder. |
| 523 | if (builder.toString().lastIndexOf(pattern) == builder.length() - 1) { |
| 524 | builder = getReplacedString(builder, pattern); |
| 525 | } |
| 526 | arrayList.add(builder.toString()); |
| 527 | fine.delete(0, fine.length()); |
| 528 | for (String str : arrayList) { |
| 529 | fine.append(str); |
| 530 | } |
| 531 | //No need to append extra spaces. |
| 532 | if (pattern.equals(PERIOD)) { |
| 533 | return fine.toString(); |
| 534 | } |
| 535 | return spaces + fine.toString(); |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Trims extra pattern strings for builder string. |
| 540 | * |
| 541 | * @param builder builder |
| 542 | * @param pattern pattern |
| 543 | * @return modified string |
| 544 | */ |
| 545 | private static StringBuilder getReplacedString(StringBuilder builder, String |
| 546 | pattern) { |
| 547 | String temp = builder.toString(); |
| 548 | temp = trimAtLast(temp, pattern); |
| 549 | int length = builder.length(); |
| 550 | builder.delete(0, length); |
| 551 | builder.append(temp); |
| 552 | return builder; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Creates array list to process line string modification. |
| 557 | * |
| 558 | * @param array array of strings |
| 559 | * @param pattern pattern |
| 560 | * @return list to process line string modification |
| 561 | */ |
| 562 | private static ArrayList<String> getSplitArray(String[] array, String pattern) { |
| 563 | ArrayList<String> newArray = new ArrayList<>(); |
| 564 | int count = 0; |
| 565 | String temp; |
| 566 | for (String str : array) { |
| 567 | if (!str.contains(OPEN_CURLY_BRACKET)) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 568 | if (str.length() >= SUB_LINE_SIZE) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 569 | count = getSplitString(str, newArray, count); |
| 570 | } else { |
| 571 | newArray.add(str); |
| 572 | count++; |
| 573 | } |
| 574 | } else { |
| 575 | if (newArray.isEmpty()) { |
| 576 | newArray.add(str); |
| 577 | } else { |
| 578 | temp = newArray.get(count - 1); |
| 579 | newArray.remove(count - 1); |
| 580 | newArray.add(count - 1, temp + pattern + str); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | return newArray; |
| 586 | } |
| 587 | |
| 588 | private static int getSplitString(String str, |
| 589 | ArrayList<String> newArray, int count) { |
| 590 | String[] array = str.split(SPACE); |
| 591 | for (String st : array) { |
| 592 | newArray.add(st + SPACE); |
| 593 | count++; |
| 594 | } |
| 595 | return count; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 596 | } |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 597 | |
| 598 | /** |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 599 | * Returns the java Package from package path. |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 600 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 601 | * @param packagePath package path |
| 602 | * @return java package |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 603 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 604 | public static String getJavaPackageFromPackagePath(String packagePath) { |
| 605 | return packagePath.replace(SLASH, PERIOD); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /** |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 609 | * Returns the directory path corresponding to java package. |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 610 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 611 | * @param packagePath package path |
| 612 | * @return java package |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 613 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 614 | public static String getPackageDirPathFromJavaJPackage(String packagePath) { |
| 615 | return packagePath.replace(PERIOD, SLASH); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /** |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 619 | * Returns the YANG identifier name as java identifier with first letter |
| 620 | * in small. |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 621 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 622 | * @param yangIdentifier identifier in YANG file. |
| 623 | * @return corresponding java identifier |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 624 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 625 | public static String getSmallCase(String yangIdentifier) { |
| 626 | return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 630 | * Returns the YANG identifier name as java identifier with first letter |
| 631 | * in capital. |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 632 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 633 | * @param yangIdentifier identifier in YANG file |
| 634 | * @return corresponding java identifier |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 635 | */ |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 636 | public static String getCapitalCase(String yangIdentifier) { |
| 637 | yangIdentifier = yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1); |
| 638 | return restrictConsecutiveCapitalCase(yangIdentifier); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /** |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 642 | * Restricts consecutive capital cased string as a rule in camel case. |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 643 | * |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 644 | * @param consecCapitalCaseRemover which requires the restriction of consecutive capital case |
| 645 | * @return string without consecutive capital case |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 646 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 647 | private static String restrictConsecutiveCapitalCase(String consecCapitalCaseRemover) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 648 | |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 649 | for (int k = 0; k < consecCapitalCaseRemover.length(); k++) { |
| 650 | if (k + 1 < consecCapitalCaseRemover.length()) { |
| 651 | if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k))) { |
| 652 | if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k + 1))) { |
| 653 | consecCapitalCaseRemover = consecCapitalCaseRemover.substring(0, k + 1) |
| 654 | + consecCapitalCaseRemover.substring(k + 1, k + 2).toLowerCase() |
| 655 | + consecCapitalCaseRemover.substring(k + 2); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 656 | } |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 657 | } |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 658 | } |
| 659 | } |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 660 | return consecCapitalCaseRemover; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 661 | } |
| 662 | |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 663 | /** |
| 664 | * Adds prefix, if the string begins with digit or is a java key word. |
| 665 | * |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 666 | * @param camelCasePrefix string for adding prefix |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 667 | * @param conflictResolver object of YANG to java naming conflict util |
| 668 | * @return prefixed camel case string |
| 669 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 670 | private static String addPrefix(String camelCasePrefix, YangToJavaNamingConflictUtil conflictResolver) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 671 | |
| 672 | String prefix = getPrefixForIdentifier(conflictResolver); |
| 673 | if (camelCasePrefix.matches(REGEX_FOR_FIRST_DIGIT)) { |
| 674 | camelCasePrefix = prefix + camelCasePrefix; |
| 675 | } |
| 676 | if (JAVA_KEY_WORDS.contains(camelCasePrefix)) { |
| 677 | camelCasePrefix = prefix + camelCasePrefix.substring(0, 1).toUpperCase() |
| 678 | + camelCasePrefix.substring(1); |
| 679 | } |
| 680 | return camelCasePrefix; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Applies the rule that a string does not end with a capitalized letter and capitalizes |
| 685 | * the letter next to a number in an array. |
| 686 | * |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 687 | * @param stringArray containing strings for camel case separation |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 688 | * @param conflictResolver object of YANG to java naming conflict util |
| 689 | * @return camel case rule checked string |
| 690 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 691 | private static String applyCamelCaseRule(String[] stringArray, YangToJavaNamingConflictUtil conflictResolver) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 692 | |
| 693 | String ruleChecker = stringArray[0].toLowerCase(); |
| 694 | int i; |
| 695 | if (ruleChecker.matches(REGEX_FOR_FIRST_DIGIT)) { |
| 696 | i = 0; |
| 697 | ruleChecker = EMPTY_STRING; |
| 698 | } else { |
| 699 | i = 1; |
| 700 | } |
| 701 | for (; i < stringArray.length; i++) { |
| 702 | if (i + 1 == stringArray.length) { |
| 703 | if (stringArray[i].matches(REGEX_FOR_SINGLE_LETTER) |
| 704 | || stringArray[i].matches(REGEX_FOR_DIGITS_WITH_SINGLE_LETTER)) { |
| 705 | ruleChecker = ruleChecker + stringArray[i].toLowerCase(); |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | if (stringArray[i].matches(REGEX_FOR_FIRST_DIGIT)) { |
| 710 | for (int j = 0; j < stringArray[i].length(); j++) { |
| 711 | char letterCheck = stringArray[i].charAt(j); |
| 712 | if (Character.isLetter(letterCheck)) { |
| 713 | stringArray[i] = stringArray[i].substring(0, j) |
| 714 | + stringArray[i].substring(j, j + 1).toUpperCase() + stringArray[i].substring(j + 1); |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | ruleChecker = ruleChecker + stringArray[i]; |
| 719 | } else { |
| 720 | ruleChecker = ruleChecker + stringArray[i].substring(0, 1).toUpperCase() + stringArray[i].substring(1); |
| 721 | } |
| 722 | } |
| 723 | String ruleCheckerWithPrefix = addPrefix(ruleChecker, conflictResolver); |
| 724 | return restrictConsecutiveCapitalCase(ruleCheckerWithPrefix); |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Resolves the conflict when input has upper case. |
| 729 | * |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 730 | * @param stringArray containing strings for upper case conflict resolver |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 731 | * @param conflictResolver object of YANG to java naming conflict util |
| 732 | * @return camel cased string |
| 733 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 734 | private static String upperCaseConflictResolver(String[] stringArray, |
| 735 | YangToJavaNamingConflictUtil conflictResolver) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 736 | |
| 737 | for (int l = 0; l < stringArray.length; l++) { |
| 738 | String[] upperCaseSplitArray = stringArray[l].split(REGEX_WITH_UPPERCASE); |
| 739 | for (int m = 0; m < upperCaseSplitArray.length; m++) { |
| 740 | if (upperCaseSplitArray[m].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) { |
| 741 | int check = m; |
| 742 | while (check + 1 < upperCaseSplitArray.length) { |
| 743 | if (upperCaseSplitArray[check + 1].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) { |
| 744 | upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase(); |
| 745 | check = check + 1; |
| 746 | } else if (upperCaseSplitArray[check + 1] |
| 747 | .matches(REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES)) { |
| 748 | upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase(); |
| 749 | break; |
| 750 | } else { |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | StringBuilder strBuilder = new StringBuilder(); |
| 757 | for (String element : upperCaseSplitArray) { |
| 758 | strBuilder.append(element); |
| 759 | } |
| 760 | stringArray[l] = strBuilder.toString(); |
| 761 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 762 | List<String> result = new ArrayList<>(); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 763 | for (String element : stringArray) { |
| 764 | String[] capitalCaseSplitArray = element.split(REGEX_WITH_UPPERCASE); |
| 765 | for (String letter : capitalCaseSplitArray) { |
| 766 | String[] arrayForAddition = letter.split(REGEX_WITH_DIGITS); |
| 767 | List<String> list = Arrays.asList(arrayForAddition); |
| 768 | for (String str : list) { |
| 769 | if (str != null && !str.isEmpty()) { |
| 770 | result.add(str); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | stringArray = result.toArray(new String[result.size()]); |
| 776 | return applyCamelCaseRule(stringArray, conflictResolver); |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Returns the YANG identifier name as java identifier. |
| 781 | * |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 782 | * @param yangIdentifier identifier in YANG file |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 783 | * @param conflictResolver object of YANG to java naming conflict util |
| 784 | * @return corresponding java identifier |
| 785 | */ |
| 786 | public static String getCamelCase(String yangIdentifier, YangToJavaNamingConflictUtil conflictResolver) { |
| 787 | |
| 788 | if (conflictResolver != null) { |
| 789 | String replacementForHyphen = conflictResolver.getReplacementForHyphen(); |
| 790 | String replacementForPeriod = conflictResolver.getReplacementForPeriod(); |
| 791 | String replacementForUnderscore = conflictResolver.getReplacementForUnderscore(); |
| 792 | if (replacementForPeriod != null) { |
| 793 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_PERIOD, |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 794 | PERIOD + replacementForPeriod.toLowerCase() + PERIOD); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 795 | } |
| 796 | if (replacementForUnderscore != null) { |
| 797 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_UNDERSCORE, |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 798 | UNDER_SCORE + replacementForUnderscore.toLowerCase() + |
| 799 | UNDER_SCORE); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 800 | } |
| 801 | if (replacementForHyphen != null) { |
| 802 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_HYPHEN, |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 803 | HYPHEN + replacementForHyphen.toLowerCase() + HYPHEN); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 804 | } |
| 805 | } |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 806 | yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_IDENTIFIER_SPECIAL_CHAR, COLON); |
| 807 | String[] strArray = yangIdentifier.split(COLON); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 808 | if (strArray[0].isEmpty()) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 809 | List<String> stringArrangement = new ArrayList<>(); |
| 810 | stringArrangement.addAll(Arrays.asList(strArray).subList(1, strArray.length)); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 811 | strArray = stringArrangement.toArray(new String[stringArrangement.size()]); |
| 812 | } |
| 813 | return upperCaseConflictResolver(strArray, conflictResolver); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Prefix for adding with identifier and namespace, when it is a java keyword or starting with digits. |
| 818 | * |
| 819 | * @param conflictResolver object of YANG to java naming conflict util |
| 820 | * @return prefix which needs to be added |
| 821 | */ |
| 822 | public static String getPrefixForIdentifier(YangToJavaNamingConflictUtil conflictResolver) { |
| 823 | |
| 824 | String prefixForIdentifier = null; |
| 825 | if (conflictResolver != null) { |
| 826 | prefixForIdentifier = conflictResolver.getPrefixForIdentifier(); |
| 827 | } |
| 828 | if (prefixForIdentifier != null) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 829 | prefixForIdentifier = prefixForIdentifier.replaceAll |
| 830 | (REGEX_WITH_ALL_SPECIAL_CHAR, COLON); |
| 831 | String[] strArray = prefixForIdentifier.split(COLON); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 832 | try { |
| 833 | if (strArray[0].isEmpty()) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 834 | List<String> stringArrangement = new ArrayList<>(); |
| 835 | stringArrangement.addAll(Arrays.asList(strArray).subList(1, strArray.length)); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 836 | strArray = stringArrangement.toArray(new String[stringArrangement.size()]); |
| 837 | } |
| 838 | prefixForIdentifier = strArray[0]; |
| 839 | for (int j = 1; j < strArray.length; j++) { |
| 840 | prefixForIdentifier = prefixForIdentifier + strArray[j].substring(0, 1).toUpperCase() + |
| 841 | strArray[j].substring(1); |
| 842 | } |
| 843 | } catch (ArrayIndexOutOfBoundsException outOfBoundsException) { |
| 844 | throw new TranslatorException("The given prefix in pom.xml is invalid."); |
| 845 | } |
| 846 | } else { |
| 847 | prefixForIdentifier = YANG_AUTO_PREFIX; |
| 848 | } |
| 849 | return prefixForIdentifier; |
| 850 | } |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 851 | |
| 852 | /** |
| 853 | * Removes empty directory. |
| 854 | * |
| 855 | * @param path path to be checked |
| 856 | */ |
| 857 | public static void removeEmptyDirectory(String path) { |
| 858 | int index; |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 859 | while (path != null && !path.isEmpty()) { |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 860 | if (!removeDirectory(path)) { |
| 861 | break; |
| 862 | } else { |
| 863 | index = path.lastIndexOf(SLASH); |
| 864 | path = path.substring(0, index); |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | private static boolean removeDirectory(String path) { |
| 870 | File dir = new File(path); |
| 871 | boolean isDeleted = false; |
| 872 | if (dir.isDirectory()) { |
| 873 | File[] files = dir.listFiles(); |
| 874 | if (files != null && files.length == 0) { |
| 875 | isDeleted = dir.delete(); |
| 876 | } else if (files != null && files.length == 1) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 877 | if ("package-info.java".equals(files[0].getName()) |
Bharat saraswal | 1edde62 | 2016-09-06 10:18:04 +0530 | [diff] [blame] | 878 | || files[0].getName().endsWith("-temp")) { |
| 879 | isDeleted = dir.delete(); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | return isDeleted; |
| 884 | } |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 885 | |
| 886 | /** |
| 887 | * Converts string to integer number for maven version. |
| 888 | * |
| 889 | * @param ver version |
| 890 | * @return int value of version |
| 891 | */ |
| 892 | public static int getVersionValue(String ver) { |
| 893 | String[] array = ver.split(Pattern.quote(PERIOD)); |
| 894 | StringBuilder builder = new StringBuilder(); |
| 895 | for (String str : array) { |
| 896 | builder.append(str); |
| 897 | } |
| 898 | return parseInt(builder.toString()); |
| 899 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 900 | } |