blob: 15051cbfdc83cdd8a43d75bcef9ee5f467a96f38 [file] [log] [blame]
Bharat saraswal97459962016-02-20 21:57:16 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal97459962016-02-20 21:57:16 +05303 *
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
17package org.onosproject.yangutils.utils.io.impl;
18
Bharat saraswal9fab16b2016-09-23 23:27:24 +053019import org.apache.commons.io.FileUtils;
20import org.onosproject.yangutils.translator.exception.TranslatorException;
21import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil;
22
Bharat saraswal715d3fc2016-05-17 19:59:16 +053023import java.io.BufferedReader;
Bharat saraswal97459962016-02-20 21:57:16 +053024import java.io.BufferedWriter;
25import java.io.File;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053026import java.io.FileReader;
Bharat saraswal97459962016-02-20 21:57:16 +053027import java.io.FileWriter;
28import java.io.IOException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053029import java.util.ArrayList;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053030import java.util.Arrays;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053031import java.util.Iterator;
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +053032import java.util.LinkedList;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053033import java.util.List;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053034import java.util.Stack;
35import java.util.regex.Pattern;
Bharat saraswale304c252016-08-16 20:56:20 +053036
Bharat saraswal0663aff2016-10-18 23:16:14 +053037import static java.lang.Integer.parseInt;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053038import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053039import static org.onosproject.yangutils.utils.UtilConstants.COLON;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053040import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
Bharat saraswal84366c52016-03-23 19:40:35 +053041import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053042import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053043import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS;
Bharat saraswal84366c52016-03-23 19:40:35 +053044import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053045import static org.onosproject.yangutils.utils.UtilConstants.ONE;
Bharat saraswale3175d32016-08-31 17:50:11 +053046import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053047import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
Bharat saraswal84366c52016-03-23 19:40:35 +053048import static org.onosproject.yangutils.utils.UtilConstants.ORG;
49import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053050import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053051import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_DIGITS_WITH_SINGLE_LETTER;
52import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
53import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_HYPHEN;
54import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_IDENTIFIER_SPECIAL_CHAR;
55import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_PERIOD;
56import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_SINGLE_LETTER;
57import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_UNDERSCORE;
58import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_ALL_SPECIAL_CHAR;
59import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_DIGITS;
60import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE;
61import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES;
62import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_UPPERCASE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053063import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLON;
Bharat saraswal84366c52016-03-23 19:40:35 +053064import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
65import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053066import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
Bharat saraswal84366c52016-03-23 19:40:35 +053067import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053068import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053069import static org.onosproject.yangutils.utils.UtilConstants.UNUSED;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053070import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
Vidyashree Rama17992c12016-11-30 11:49:27 +053071import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.parseCopyrightHeader;
Bharat saraswal84366c52016-03-23 19:40:35 +053072import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
73import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053074import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053075import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053076
Bharat saraswal97459962016-02-20 21:57:16 +053077/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053078 * Represents common utility functionalities for code generation.
Bharat saraswal97459962016-02-20 21:57:16 +053079 */
80public final class YangIoUtils {
81
Bharat saraswale3175d32016-08-31 17:50:11 +053082 private static final int LINE_SIZE = 118;
83 private static final int SUB_LINE_SIZE = 116;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053084 private static final int SUB_SIZE = 60;
Bharat saraswal97459962016-02-20 21:57:16 +053085
86 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053087 * Creates an instance of YANG io utils.
Bharat saraswal97459962016-02-20 21:57:16 +053088 */
89 private YangIoUtils() {
90 }
91
92 /**
93 * Creates the directory structure.
94 *
95 * @param path directory path
96 * @return directory structure
Bharat saraswal8beac342016-08-04 02:00:03 +053097 * @throws IOException when fails to do IO operations
Bharat saraswal97459962016-02-20 21:57:16 +053098 */
Bharat saraswal1edde622016-09-06 10:18:04 +053099 public static File createDirectories(String path)
100 throws IOException {
b.janani66b749c2016-02-24 12:23:03 +0530101 File generatedDir = new File(path);
Bharat saraswal8beac342016-08-04 02:00:03 +0530102 if (!generatedDir.exists()) {
103 boolean isGenerated = generatedDir.mkdirs();
104 if (!isGenerated) {
105 throw new IOException("failed to generated directory " + path);
106 }
107 }
Bharat saraswal97459962016-02-20 21:57:16 +0530108 return generatedDir;
109 }
110
111 /**
112 * Adds package info file for the created directory.
113 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530114 * @param path directory path
115 * @param classInfo class info for the package
116 * @param pack package of the directory
117 * @param isChildNode is it a child node
Vinod Kumar S08710982016-03-03 19:55:30 +0530118 * @throws IOException when fails to create package info file
Bharat saraswal97459962016-02-20 21:57:16 +0530119 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530120 public static void addPackageInfo(File path, String classInfo, String pack,
121 boolean isChildNode) throws IOException {
Bharat saraswal97459962016-02-20 21:57:16 +0530122
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530123 pack = parsePkg(pack);
Bharat saraswal97459962016-02-20 21:57:16 +0530124 try {
125
Bharat saraswal84366c52016-03-23 19:40:35 +0530126 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal8beac342016-08-04 02:00:03 +0530127 if (!packageInfo.exists()) {
128 boolean isGenerated = packageInfo.createNewFile();
129 if (!isGenerated) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530130 throw new IOException("failed to generated package-info " +
131 path);
Bharat saraswal8beac342016-08-04 02:00:03 +0530132 }
133 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530134 FileWriter fileWriter = new FileWriter(packageInfo);
135 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
136
Vidyashree Rama17992c12016-11-30 11:49:27 +0530137 bufferedWriter.write(parseCopyrightHeader());
Vidyashree Ramab3670472016-08-06 15:49:56 +0530138 //TODO: get the compiler annotations and pass the info
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530139 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode,
140 null));
141 String pkg = PACKAGE + SPACE + pack + SEMI_COLON;
Bharat saraswal94844d62016-10-13 13:28:03 +0530142 if (pkg.length() >= LINE_SIZE) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530143 pkg = processModifications(pkg, LINE_SIZE);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530144 }
145 bufferedWriter.write(pkg);
b.janani66b749c2016-02-24 12:23:03 +0530146 bufferedWriter.close();
Bharat saraswal84366c52016-03-23 19:40:35 +0530147 fileWriter.close();
Bharat saraswal97459962016-02-20 21:57:16 +0530148 } catch (IOException e) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530149 throw new IOException("Exception occurred while creating package info" +
150 " file.");
Bharat saraswal97459962016-02-20 21:57:16 +0530151 }
152 }
153
154 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530155 * Parses package and returns updated package.
156 *
157 * @param pack package needs to be updated
158 * @return updated package
159 */
160 public static String parsePkg(String pack) {
161
162 if (pack.contains(ORG)) {
163 String[] strArray = pack.split(ORG);
164 if (strArray.length >= 3) {
165 for (int i = 1; i < strArray.length; i++) {
166 if (i == 1) {
167 pack = ORG + strArray[1];
168 } else {
169 pack = pack + ORG + strArray[i];
170 }
171 }
172 } else {
173 pack = ORG + strArray[1];
174 }
175 }
176
177 return pack;
178 }
179
180 /**
Bharat saraswal97459962016-02-20 21:57:16 +0530181 * Cleans the generated directory if already exist in source folder.
182 *
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530183 * @param dir generated directory in previous build
Bharat saraswal84366c52016-03-23 19:40:35 +0530184 * @throws IOException when failed to delete directory
Bharat saraswal97459962016-02-20 21:57:16 +0530185 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530186 public static void deleteDirectory(String dir)
187 throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530188 File generatedDirectory = new File(dir);
Bharat saraswal97459962016-02-20 21:57:16 +0530189 if (generatedDirectory.exists()) {
Bharat saraswal97459962016-02-20 21:57:16 +0530190 try {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530191 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal97459962016-02-20 21:57:16 +0530192 } catch (IOException e) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530193 throw new IOException(
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530194 "Failed to delete the generated files in " +
195 generatedDirectory + " directory");
Bharat saraswal97459962016-02-20 21:57:16 +0530196 }
Bharat saraswal97459962016-02-20 21:57:16 +0530197 }
198 }
199
200 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530201 * Searches and deletes generated temporary directories.
202 *
203 * @param root root directory
204 * @throws IOException when fails to do IO operations.
205 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530206 public static void searchAndDeleteTempDir(String root)
207 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530208 List<File> store = new LinkedList<>();
209 Stack<String> stack = new Stack<>();
210 stack.push(root);
211
212 while (!stack.empty()) {
213 root = stack.pop();
214 File file = new File(root);
Bharat saraswal8beac342016-08-04 02:00:03 +0530215 File[] fileList = file.listFiles();
216 if (fileList == null || fileList.length == 0) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530217 continue;
218 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530219 for (File current : fileList) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530220 if (current.isDirectory()) {
221 stack.push(current.toString());
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530222 if (current.getName().endsWith(HYPHEN + TEMP)) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530223 store.add(current);
224 }
225 }
226 }
227 }
228
229 for (File dir : store) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530230 FileUtils.deleteDirectory(dir);
Bharat saraswal250a7472016-05-12 13:16:57 +0530231 }
232 }
233
234 /**
Bharat saraswal022dae92016-03-04 20:08:09 +0530235 * Removes extra char from the string.
236 *
Bharat saraswald14cbe82016-07-14 13:26:18 +0530237 * @param valueString string to be trimmed
Bharat saraswal8beac342016-08-04 02:00:03 +0530238 * @param removalString extra chars
Bharat saraswal022dae92016-03-04 20:08:09 +0530239 * @return new string
240 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530241 public static String trimAtLast(String valueString, String...
242 removalString) {
Bharat saraswal022dae92016-03-04 20:08:09 +0530243 StringBuilder stringBuilder = new StringBuilder(valueString);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530244 String midString;
245 int index;
246 for (String remove : removalString) {
247 midString = stringBuilder.toString();
248 index = midString.lastIndexOf(remove);
249 if (index != -1) {
250 stringBuilder.deleteCharAt(index);
251 }
Bharat saraswal022dae92016-03-04 20:08:09 +0530252 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530253 return stringBuilder.toString();
Bharat saraswal022dae92016-03-04 20:08:09 +0530254 }
255
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530256 /**
Gaurav Agrawal8a147522016-08-10 13:43:01 +0530257 * Replaces the last occurrence of a string with a given string.
258 *
259 * @param valueString string under operation
260 * @param removalString string to be replaced
261 * @param replacingString string with which replacement is to be done
262 * @return new string
263 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530264 public static String replaceLast(String valueString, String removalString,
265 String replacingString) {
Gaurav Agrawal8a147522016-08-10 13:43:01 +0530266 StringBuilder stringBuilder = new StringBuilder(valueString);
267 int index = valueString.lastIndexOf(removalString);
268 if (index != -1) {
269 stringBuilder.replace(index, index + 1, replacingString);
270 } else {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530271 stringBuilder.append(NEW_LINE + EIGHT_SPACE_INDENTATION + UNUSED +
272 OPEN_PARENTHESIS + ONE +
273 CLOSE_PARENTHESIS + SEMI_COLON);
Gaurav Agrawal8a147522016-08-10 13:43:01 +0530274 }
275 return stringBuilder.toString();
276
277 // TODO remove generation of ENUM if there is no leaf node.
278 }
279
280
281 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530282 * Returns the directory path of the package in canonical form.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530283 *
284 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530285 * put
286 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530287 * @return absolute path of the package in canonical form
288 */
289 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
290
291 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswal84366c52016-03-23 19:40:35 +0530292 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530293 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530294 String[] strArray = pathOfJavaPkg.split(SLASH);
295 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530296 return pathOfJavaPkg;
297 } else {
Bharat saraswal84366c52016-03-23 19:40:35 +0530298 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530299 }
300 }
301
302 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530303 * Returns the absolute path of the package in canonical form.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530304 *
305 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530306 * put
307 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530308 * @return absolute path of the package in canonical form
309 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530310 public static String getAbsolutePackagePath(String baseCodeGenPath,
311 String pathOfJavaPkg) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530312 return baseCodeGenPath + pathOfJavaPkg;
313 }
314
315 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530316 * Merges the temp java files to main java files.
Bharat saraswal84366c52016-03-23 19:40:35 +0530317 *
318 * @param appendFile temp file
Gaurav Agrawalab7c4bd2016-05-17 18:06:38 +0530319 * @param srcFile main file
Bharat saraswal84366c52016-03-23 19:40:35 +0530320 * @throws IOException when fails to append contents
321 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530322 public static void mergeJavaFiles(File appendFile, File srcFile)
323 throws IOException {
Bharat saraswal84366c52016-03-23 19:40:35 +0530324 try {
325 appendFileContents(appendFile, srcFile);
326 } catch (IOException e) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530327 throw new IOException("Failed to merge " + appendFile + " in " +
328 srcFile);
Bharat saraswal84366c52016-03-23 19:40:35 +0530329 }
330 }
331
332 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530333 * Inserts data in the generated file.
Bharat saraswal84366c52016-03-23 19:40:35 +0530334 *
335 * @param file file in which need to be inserted
336 * @param data data which need to be inserted
337 * @throws IOException when fails to insert into file
338 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530339 public static void insertDataIntoJavaFile(File file, String data)
340 throws IOException {
Bharat saraswal84366c52016-03-23 19:40:35 +0530341 try {
342 updateFileHandle(file, data, false);
343 } catch (IOException e) {
344 throw new IOException("Failed to insert in " + file + "file");
345 }
346 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530347
348 /**
349 * Validates a line size in given file whether it is having more then 120 characters.
350 * If yes it will update and give a new file.
351 *
352 * @param dataFile file in which need to verify all lines.
353 * @return updated file
354 * @throws IOException when fails to do IO operations.
355 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530356 public static File validateLineLength(File dataFile)
357 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530358 FileReader fileReader = new FileReader(dataFile);
359 BufferedReader bufferReader = new BufferedReader(fileReader);
360 try {
361 StringBuilder stringBuilder = new StringBuilder();
362 String line = bufferReader.readLine();
363
364 while (line != null) {
Bharat saraswal94844d62016-10-13 13:28:03 +0530365 if (line.length() >= LINE_SIZE) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530366 line = processModifications(line, LINE_SIZE);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530367 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530368 stringBuilder.append(line);
369 stringBuilder.append(NEW_LINE);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530370 line = bufferReader.readLine();
371 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530372 FileWriter writer = new FileWriter(dataFile);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530373 writer.write(stringBuilder.toString());
374 writer.close();
Bharat saraswal8beac342016-08-04 02:00:03 +0530375 return dataFile;
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530376 } finally {
377 fileReader.close();
378 bufferReader.close();
379 }
380 }
381
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530382 /**
383 * Resolves validation of line length by modifying the string.
384 *
385 * @param line current line string
386 * @param lineSize line size for change
387 * @return modified line string
388 */
389 private static String processModifications(String line, int lineSize) {
390 int period = getArrayLength(line, PERIOD);
391 int space = getArrayLength(line, SPACE);
392 if (period > space) {
393 return merge(getForPeriod(line), PERIOD, lineSize);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530394 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530395 return merge(getForSpace(line), SPACE, lineSize);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530396 }
397
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530398 /**
399 * Returns count of pattern in line.
400 *
401 * @param line line string
402 * @param pattern pattern followed in line
403 * @return count of pattern in line
404 */
405 private static int getArrayLength(String line, String pattern) {
406 String[] array = line.split(Pattern.quote(pattern));
407 int len = array.length;
408 if (pattern.equals(SPACE)) {
409 for (String str : array) {
410 if (str.equals(EMPTY_STRING)) {
411 len--;
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530412 }
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530413 }
414 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530415 return len - 1;
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530416 }
417
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530418 /**
419 * Returns array list of string in case of period.
420 *
421 * @param line line string
422 * @return array list of string in case of period
423 */
424 private static ArrayList<String> getForPeriod(String line) {
425 String[] array = line.split(Pattern.quote(PERIOD));
426 return getSplitArray(array, PERIOD);
427 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530428
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530429 /**
430 * Returns array list of string in case of space.
431 *
432 * @param line line string
433 * @return array list of string in case of space
434 */
435 private static ArrayList<String> getForSpace(String line) {
436 String[] array = line.split(SPACE);
437 return getSplitArray(array, SPACE);
438 }
439
440 /**
441 * Merges strings to form a new string.
442 *
443 * @param list list of strings
444 * @param pattern pattern
445 * @param lineSize line size
446 * @return merged string
447 */
448 private static String merge(ArrayList<String> list, String pattern, int lineSize) {
449 StringBuilder builder = new StringBuilder();
450 StringBuilder fine = new StringBuilder();
Bharat saraswal8beac342016-08-04 02:00:03 +0530451 String append;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530452 String pre;
453 String present = EMPTY_STRING;
454 //Add one blank string in list to handle border limit cases.
455 list.add(EMPTY_STRING);
456 Iterator<String> listIt = list.iterator();
457 ArrayList<String> arrayList = new ArrayList<>();
458 int length;
459 StringBuilder spaces = new StringBuilder();
460 while (listIt.hasNext()) {
461 pre = present;
462 present = listIt.next();
463 //check is present string is more than 80 char.
464 if (present.length() > SUB_SIZE) {
465 int period = getArrayLength(present, PERIOD);
466 int space = getArrayLength(present, SPACE);
467 if (period > space) {
468 // in such case present string should be resolved.
469 present = processModifications(present, SUB_SIZE);
470 builder.append(present);
Bharat saraswale3175d32016-08-31 17:50:11 +0530471 }
Bharat saraswale3175d32016-08-31 17:50:11 +0530472 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530473 length = builder.length();
474 //If length of builder is less than the given length then append
475 // it to builder.
476 if (length <= lineSize) {
477 //fill the space builder to provide proper indentation.
478 if (present.equals(EMPTY_STRING)) {
479 spaces.append(SPACE);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530480 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530481 //append to builder
482 builder.append(present);
483 builder.append(pattern);
484 fine.append(pre);
485 //do not append pattern in case of empty strings.
486 if (!pre.equals(EMPTY_STRING)) {
487 fine.append(pattern);
488 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530489 } else {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530490 // now the length is more than given size so trim the pattern
491 // for the string and add it to list,
492 fine = getReplacedString(fine, pattern);
493 arrayList.add(fine.toString());
494 // clear all.
495 builder.delete(0, length);
496 fine.delete(0, fine.length());
497 // append indentation
498 if (pattern.contains(PERIOD)) {
499 append = NEW_LINE + spaces +
500 TWELVE_SPACE_INDENTATION +
501 PERIOD;
502 } else {
503 append = NEW_LINE + spaces + TWELVE_SPACE_INDENTATION;
504 }
505 // builder needs to move one step forward to fine builder so
506 // append present and pre strings to builder with pattern.
507 builder.append(append);
508 builder.append(pre);
509 builder.append(pattern);
510 builder.append(present);
511 builder.append(pattern);
512 fine.append(append);
513 fine.append(pre);
514 if (!pre.equals(EMPTY_STRING)) {
515 fine.append(pattern);
516 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530517 }
518 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530519
520 builder = getReplacedString(builder, pattern);
521
522 //need to remove extra string added from the builder.
523 if (builder.toString().lastIndexOf(pattern) == builder.length() - 1) {
524 builder = getReplacedString(builder, pattern);
525 }
526 arrayList.add(builder.toString());
527 fine.delete(0, fine.length());
528 for (String str : arrayList) {
529 fine.append(str);
530 }
531 //No need to append extra spaces.
532 if (pattern.equals(PERIOD)) {
533 return fine.toString();
534 }
535 return spaces + fine.toString();
536 }
537
538 /**
539 * Trims extra pattern strings for builder string.
540 *
541 * @param builder builder
542 * @param pattern pattern
543 * @return modified string
544 */
545 private static StringBuilder getReplacedString(StringBuilder builder, String
546 pattern) {
547 String temp = builder.toString();
548 temp = trimAtLast(temp, pattern);
549 int length = builder.length();
550 builder.delete(0, length);
551 builder.append(temp);
552 return builder;
553 }
554
555 /**
556 * Creates array list to process line string modification.
557 *
558 * @param array array of strings
559 * @param pattern pattern
560 * @return list to process line string modification
561 */
562 private static ArrayList<String> getSplitArray(String[] array, String pattern) {
563 ArrayList<String> newArray = new ArrayList<>();
564 int count = 0;
565 String temp;
566 for (String str : array) {
567 if (!str.contains(OPEN_CURLY_BRACKET)) {
Bharat saraswal94844d62016-10-13 13:28:03 +0530568 if (str.length() >= SUB_LINE_SIZE) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530569 count = getSplitString(str, newArray, count);
570 } else {
571 newArray.add(str);
572 count++;
573 }
574 } else {
575 if (newArray.isEmpty()) {
576 newArray.add(str);
577 } else {
578 temp = newArray.get(count - 1);
579 newArray.remove(count - 1);
580 newArray.add(count - 1, temp + pattern + str);
581 }
582 }
583 }
584
585 return newArray;
586 }
587
588 private static int getSplitString(String str,
589 ArrayList<String> newArray, int count) {
590 String[] array = str.split(SPACE);
591 for (String st : array) {
592 newArray.add(st + SPACE);
593 count++;
594 }
595 return count;
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530596 }
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530597
598 /**
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530599 * Returns the java Package from package path.
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530600 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530601 * @param packagePath package path
602 * @return java package
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530603 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530604 public static String getJavaPackageFromPackagePath(String packagePath) {
605 return packagePath.replace(SLASH, PERIOD);
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530606 }
607
608 /**
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530609 * Returns the directory path corresponding to java package.
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530610 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530611 * @param packagePath package path
612 * @return java package
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530613 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530614 public static String getPackageDirPathFromJavaJPackage(String packagePath) {
615 return packagePath.replace(PERIOD, SLASH);
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530616 }
617
618 /**
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530619 * Returns the YANG identifier name as java identifier with first letter
620 * in small.
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530621 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530622 * @param yangIdentifier identifier in YANG file.
623 * @return corresponding java identifier
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530624 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530625 public static String getSmallCase(String yangIdentifier) {
626 return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530627 }
628
629 /**
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530630 * Returns the YANG identifier name as java identifier with first letter
631 * in capital.
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530632 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530633 * @param yangIdentifier identifier in YANG file
634 * @return corresponding java identifier
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530635 */
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530636 public static String getCapitalCase(String yangIdentifier) {
637 yangIdentifier = yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
638 return restrictConsecutiveCapitalCase(yangIdentifier);
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530639 }
640
641 /**
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530642 * Restricts consecutive capital cased string as a rule in camel case.
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530643 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530644 * @param consecCapitalCaseRemover which requires the restriction of consecutive capital case
645 * @return string without consecutive capital case
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530646 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530647 private static String restrictConsecutiveCapitalCase(String consecCapitalCaseRemover) {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530648
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530649 for (int k = 0; k < consecCapitalCaseRemover.length(); k++) {
650 if (k + 1 < consecCapitalCaseRemover.length()) {
651 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k))) {
652 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k + 1))) {
653 consecCapitalCaseRemover = consecCapitalCaseRemover.substring(0, k + 1)
654 + consecCapitalCaseRemover.substring(k + 1, k + 2).toLowerCase()
655 + consecCapitalCaseRemover.substring(k + 2);
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530656 }
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530657 }
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530658 }
659 }
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530660 return consecCapitalCaseRemover;
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530661 }
662
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530663 /**
664 * Adds prefix, if the string begins with digit or is a java key word.
665 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530666 * @param camelCasePrefix string for adding prefix
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530667 * @param conflictResolver object of YANG to java naming conflict util
668 * @return prefixed camel case string
669 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530670 private static String addPrefix(String camelCasePrefix, YangToJavaNamingConflictUtil conflictResolver) {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530671
672 String prefix = getPrefixForIdentifier(conflictResolver);
673 if (camelCasePrefix.matches(REGEX_FOR_FIRST_DIGIT)) {
674 camelCasePrefix = prefix + camelCasePrefix;
675 }
676 if (JAVA_KEY_WORDS.contains(camelCasePrefix)) {
677 camelCasePrefix = prefix + camelCasePrefix.substring(0, 1).toUpperCase()
678 + camelCasePrefix.substring(1);
679 }
680 return camelCasePrefix;
681 }
682
683 /**
684 * Applies the rule that a string does not end with a capitalized letter and capitalizes
685 * the letter next to a number in an array.
686 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530687 * @param stringArray containing strings for camel case separation
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530688 * @param conflictResolver object of YANG to java naming conflict util
689 * @return camel case rule checked string
690 */
Vidyashree Rama17992c12016-11-30 11:49:27 +0530691 private static String applyCamelCaseRule(String[] stringArray,
692 YangToJavaNamingConflictUtil conflictResolver) {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530693
694 String ruleChecker = stringArray[0].toLowerCase();
695 int i;
696 if (ruleChecker.matches(REGEX_FOR_FIRST_DIGIT)) {
697 i = 0;
698 ruleChecker = EMPTY_STRING;
699 } else {
700 i = 1;
701 }
702 for (; i < stringArray.length; i++) {
703 if (i + 1 == stringArray.length) {
704 if (stringArray[i].matches(REGEX_FOR_SINGLE_LETTER)
705 || stringArray[i].matches(REGEX_FOR_DIGITS_WITH_SINGLE_LETTER)) {
706 ruleChecker = ruleChecker + stringArray[i].toLowerCase();
707 break;
708 }
709 }
710 if (stringArray[i].matches(REGEX_FOR_FIRST_DIGIT)) {
711 for (int j = 0; j < stringArray[i].length(); j++) {
712 char letterCheck = stringArray[i].charAt(j);
713 if (Character.isLetter(letterCheck)) {
714 stringArray[i] = stringArray[i].substring(0, j)
715 + stringArray[i].substring(j, j + 1).toUpperCase() + stringArray[i].substring(j + 1);
716 break;
717 }
718 }
719 ruleChecker = ruleChecker + stringArray[i];
720 } else {
721 ruleChecker = ruleChecker + stringArray[i].substring(0, 1).toUpperCase() + stringArray[i].substring(1);
722 }
723 }
724 String ruleCheckerWithPrefix = addPrefix(ruleChecker, conflictResolver);
725 return restrictConsecutiveCapitalCase(ruleCheckerWithPrefix);
726 }
727
728 /**
729 * Resolves the conflict when input has upper case.
730 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530731 * @param stringArray containing strings for upper case conflict resolver
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530732 * @param conflictResolver object of YANG to java naming conflict util
733 * @return camel cased string
734 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530735 private static String upperCaseConflictResolver(String[] stringArray,
736 YangToJavaNamingConflictUtil conflictResolver) {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530737
738 for (int l = 0; l < stringArray.length; l++) {
739 String[] upperCaseSplitArray = stringArray[l].split(REGEX_WITH_UPPERCASE);
740 for (int m = 0; m < upperCaseSplitArray.length; m++) {
741 if (upperCaseSplitArray[m].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
742 int check = m;
743 while (check + 1 < upperCaseSplitArray.length) {
744 if (upperCaseSplitArray[check + 1].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
745 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
746 check = check + 1;
747 } else if (upperCaseSplitArray[check + 1]
748 .matches(REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES)) {
749 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
750 break;
751 } else {
752 break;
753 }
754 }
755 }
756 }
757 StringBuilder strBuilder = new StringBuilder();
758 for (String element : upperCaseSplitArray) {
759 strBuilder.append(element);
760 }
761 stringArray[l] = strBuilder.toString();
762 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530763 List<String> result = new ArrayList<>();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530764 for (String element : stringArray) {
765 String[] capitalCaseSplitArray = element.split(REGEX_WITH_UPPERCASE);
766 for (String letter : capitalCaseSplitArray) {
767 String[] arrayForAddition = letter.split(REGEX_WITH_DIGITS);
768 List<String> list = Arrays.asList(arrayForAddition);
769 for (String str : list) {
770 if (str != null && !str.isEmpty()) {
771 result.add(str);
772 }
773 }
774 }
775 }
776 stringArray = result.toArray(new String[result.size()]);
777 return applyCamelCaseRule(stringArray, conflictResolver);
778 }
779
780 /**
781 * Returns the YANG identifier name as java identifier.
782 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530783 * @param yangIdentifier identifier in YANG file
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530784 * @param conflictResolver object of YANG to java naming conflict util
785 * @return corresponding java identifier
786 */
787 public static String getCamelCase(String yangIdentifier, YangToJavaNamingConflictUtil conflictResolver) {
788
789 if (conflictResolver != null) {
790 String replacementForHyphen = conflictResolver.getReplacementForHyphen();
791 String replacementForPeriod = conflictResolver.getReplacementForPeriod();
792 String replacementForUnderscore = conflictResolver.getReplacementForUnderscore();
793 if (replacementForPeriod != null) {
794 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_PERIOD,
Bharat saraswal1edde622016-09-06 10:18:04 +0530795 PERIOD + replacementForPeriod.toLowerCase() + PERIOD);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530796 }
797 if (replacementForUnderscore != null) {
798 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_UNDERSCORE,
Bharat saraswal1edde622016-09-06 10:18:04 +0530799 UNDER_SCORE + replacementForUnderscore.toLowerCase() +
800 UNDER_SCORE);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530801 }
802 if (replacementForHyphen != null) {
803 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_HYPHEN,
Bharat saraswal1edde622016-09-06 10:18:04 +0530804 HYPHEN + replacementForHyphen.toLowerCase() + HYPHEN);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530805 }
806 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530807 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_IDENTIFIER_SPECIAL_CHAR, COLON);
808 String[] strArray = yangIdentifier.split(COLON);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530809 if (strArray[0].isEmpty()) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530810 List<String> stringArrangement = new ArrayList<>();
811 stringArrangement.addAll(Arrays.asList(strArray).subList(1, strArray.length));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530812 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
813 }
814 return upperCaseConflictResolver(strArray, conflictResolver);
815 }
816
817 /**
818 * Prefix for adding with identifier and namespace, when it is a java keyword or starting with digits.
819 *
820 * @param conflictResolver object of YANG to java naming conflict util
821 * @return prefix which needs to be added
822 */
823 public static String getPrefixForIdentifier(YangToJavaNamingConflictUtil conflictResolver) {
824
825 String prefixForIdentifier = null;
826 if (conflictResolver != null) {
827 prefixForIdentifier = conflictResolver.getPrefixForIdentifier();
828 }
829 if (prefixForIdentifier != null) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530830 prefixForIdentifier = prefixForIdentifier.replaceAll
831 (REGEX_WITH_ALL_SPECIAL_CHAR, COLON);
832 String[] strArray = prefixForIdentifier.split(COLON);
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530833 try {
834 if (strArray[0].isEmpty()) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530835 List<String> stringArrangement = new ArrayList<>();
836 stringArrangement.addAll(Arrays.asList(strArray).subList(1, strArray.length));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530837 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
838 }
839 prefixForIdentifier = strArray[0];
840 for (int j = 1; j < strArray.length; j++) {
841 prefixForIdentifier = prefixForIdentifier + strArray[j].substring(0, 1).toUpperCase() +
842 strArray[j].substring(1);
843 }
844 } catch (ArrayIndexOutOfBoundsException outOfBoundsException) {
845 throw new TranslatorException("The given prefix in pom.xml is invalid.");
846 }
847 } else {
848 prefixForIdentifier = YANG_AUTO_PREFIX;
849 }
850 return prefixForIdentifier;
851 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530852
853 /**
854 * Removes empty directory.
855 *
856 * @param path path to be checked
857 */
858 public static void removeEmptyDirectory(String path) {
859 int index;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530860 while (path != null && !path.isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530861 if (!removeDirectory(path)) {
862 break;
863 } else {
864 index = path.lastIndexOf(SLASH);
865 path = path.substring(0, index);
866 }
867 }
868 }
869
870 private static boolean removeDirectory(String path) {
871 File dir = new File(path);
872 boolean isDeleted = false;
873 if (dir.isDirectory()) {
874 File[] files = dir.listFiles();
875 if (files != null && files.length == 0) {
876 isDeleted = dir.delete();
877 } else if (files != null && files.length == 1) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530878 if ("package-info.java".equals(files[0].getName())
Bharat saraswal1edde622016-09-06 10:18:04 +0530879 || files[0].getName().endsWith("-temp")) {
880 isDeleted = dir.delete();
881 }
882 }
883 }
884 return isDeleted;
885 }
Bharat saraswal0663aff2016-10-18 23:16:14 +0530886
887 /**
888 * Converts string to integer number for maven version.
889 *
890 * @param ver version
891 * @return int value of version
892 */
893 public static int getVersionValue(String ver) {
894 String[] array = ver.split(Pattern.quote(PERIOD));
895 StringBuilder builder = new StringBuilder();
896 for (String str : array) {
897 builder.append(str);
898 }
899 return parseInt(builder.toString());
900 }
Bharat saraswal97459962016-02-20 21:57:16 +0530901}