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