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