blob: 566e9ce9a9dbccbb9f928daa9d941fcce126c479 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-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 saraswal33dfa012016-05-17 19:59:16 +053019import java.io.BufferedReader;
Bharat saraswal870c56f2016-02-20 21:57:16 +053020import java.io.BufferedWriter;
21import java.io.File;
Bharat saraswal33dfa012016-05-17 19:59:16 +053022import java.io.FileReader;
Bharat saraswal870c56f2016-02-20 21:57:16 +053023import java.io.FileWriter;
24import java.io.IOException;
Vinod Kumar S38046502016-03-23 15:30:27 +053025import java.util.ArrayList;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053026import java.util.Arrays;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053027import java.util.LinkedList;
Bharat saraswal33dfa012016-05-17 19:59:16 +053028import java.util.List;
Bharat saraswal33dfa012016-05-17 19:59:16 +053029import java.util.Stack;
30import java.util.regex.Pattern;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053031
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import org.apache.commons.io.FileUtils;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053033import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053034
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053035import static org.onosproject.yangutils.utils.UtilConstants.COLAN;
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053037import static org.onosproject.yangutils.utils.UtilConstants.HASH;
Bharat saraswal96dfef02016-06-16 00:29:12 +053038import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053039import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053040import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053041import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053042import static org.onosproject.yangutils.utils.UtilConstants.ORG;
43import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053044import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053045import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_DIGITS_WITH_SINGLE_LETTER;
46import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
47import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_HYPHEN;
48import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_IDENTIFIER_SPECIAL_CHAR;
49import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_PERIOD;
50import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_SINGLE_LETTER;
51import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_UNDERSCORE;
52import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_ALL_SPECIAL_CHAR;
53import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_DIGITS;
54import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE;
55import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES;
56import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_UPPERCASE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053057import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
58import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
59import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053060import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053061import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
62import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
Bharat saraswale2d51d62016-03-23 19:40:35 +053063import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
64import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
Bharat saraswal96dfef02016-06-16 00:29:12 +053065import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053066import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar S38046502016-03-23 15:30:27 +053067
Bharat saraswal870c56f2016-02-20 21:57:16 +053068/**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053070 */
71public final class YangIoUtils {
72
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053073 private static final int LINE_SIZE = 118;
Bharat saraswal33dfa012016-05-17 19:59:16 +053074 private static final int SUB_LINE_SIZE = 112;
75 private static final int ZERO = 0;
Bharat saraswal870c56f2016-02-20 21:57:16 +053076
77 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053078 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 */
80 private YangIoUtils() {
81 }
82
83 /**
84 * Creates the directory structure.
85 *
86 * @param path directory path
87 * @return directory structure
88 */
89 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +053090 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053091 generatedDir.mkdirs();
92 return generatedDir;
93 }
94
95 /**
96 * Adds package info file for the created directory.
97 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053098 * @param path directory path
99 * @param classInfo class info for the package
100 * @param pack package of the directory
101 * @param isChildNode is it a child node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530102 * @param pluginConfig plugin configurations
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530103 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530105 public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode,
Bharat saraswalb551aae2016-07-14 15:18:20 +0530106 YangPluginConfig pluginConfig)
Bharat saraswalc0e04842016-05-12 13:16:57 +0530107 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530108
Bharat saraswal33dfa012016-05-17 19:59:16 +0530109 pack = parsePkg(pack);
110
Bharat saraswal870c56f2016-02-20 21:57:16 +0530111 try {
112
Bharat saraswale2d51d62016-03-23 19:40:35 +0530113 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530114 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530115
116 FileWriter fileWriter = new FileWriter(packageInfo);
117 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
118
b.janani68c55e12016-02-24 12:23:03 +0530119 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530120 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode, pluginConfig));
121 String pkg = PACKAGE + SPACE + pack + SEMI_COLAN;
122 if (pkg.length() > LINE_SIZE) {
123 pkg = whenDelimiterIsPersent(pkg, LINE_SIZE);
124 }
125 bufferedWriter.write(pkg);
b.janani68c55e12016-02-24 12:23:03 +0530126 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530127 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530128 } catch (IOException e) {
129 throw new IOException("Exception occured while creating package info file.");
130 }
131 }
132
133 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530134 * Parses package and returns updated package.
135 *
136 * @param pack package needs to be updated
137 * @return updated package
138 */
139 public static String parsePkg(String pack) {
140
141 if (pack.contains(ORG)) {
142 String[] strArray = pack.split(ORG);
143 if (strArray.length >= 3) {
144 for (int i = 1; i < strArray.length; i++) {
145 if (i == 1) {
146 pack = ORG + strArray[1];
147 } else {
148 pack = pack + ORG + strArray[i];
149 }
150 }
151 } else {
152 pack = ORG + strArray[1];
153 }
154 }
155
156 return pack;
157 }
158
159 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530160 * Cleans the generated directory if already exist in source folder.
161 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530162 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530163 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530164 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530165 public static void deleteDirectory(String dir)
166 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530167 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530168 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530169 try {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530170 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530171 } catch (IOException e) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530172 throw new IOException(
173 "Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530174 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530175 }
176 }
177
178 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530179 * Searches and deletes generated temporary directories.
180 *
181 * @param root root directory
182 * @throws IOException when fails to do IO operations.
183 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530184 public static void searchAndDeleteTempDir(String root)
185 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186 List<File> store = new LinkedList<>();
187 Stack<String> stack = new Stack<>();
188 stack.push(root);
189
190 while (!stack.empty()) {
191 root = stack.pop();
192 File file = new File(root);
193 File[] filelist = file.listFiles();
194 if (filelist == null || filelist.length == 0) {
195 continue;
196 }
197 for (File current : filelist) {
198 if (current.isDirectory()) {
199 stack.push(current.toString());
200 if (current.getName().endsWith("-Temp")) {
201 store.add(current);
202 }
203 }
204 }
205 }
206
207 for (File dir : store) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530208 FileUtils.deleteDirectory(dir);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530209 }
210 }
211
212 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530213 * Removes extra char from the string.
214 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530215 * @param valueString string to be trimmed
216 * @param removalStirng extra chars
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530217 * @return new string
218 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530219 public static String trimAtLast(String valueString, String removalStirng) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530220 StringBuilder stringBuilder = new StringBuilder(valueString);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530221 int index = valueString.lastIndexOf(removalStirng);
222 if (index != -1) {
223 stringBuilder.deleteCharAt(index);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530224 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530225 return stringBuilder.toString();
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530226 }
227
Vinod Kumar S38046502016-03-23 15:30:27 +0530228 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530229 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530230 *
231 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530232 * put
233 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530234 * @return absolute path of the package in canonical form
235 */
236 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
237
238 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530239 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530240 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530241 String[] strArray = pathOfJavaPkg.split(SLASH);
242 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530243 return pathOfJavaPkg;
244 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530245 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530246 }
247 }
248
249 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530250 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530251 *
252 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530253 * put
254 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530255 * @return absolute path of the package in canonical form
256 */
257 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530258 return baseCodeGenPath + pathOfJavaPkg;
259 }
260
261 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530262 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530263 *
264 * @param appendFile temp file
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530265 * @param srcFile main file
Bharat saraswale2d51d62016-03-23 19:40:35 +0530266 * @throws IOException when fails to append contents
267 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530268 public static void mergeJavaFiles(File appendFile, File srcFile)
269 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530270 try {
271 appendFileContents(appendFile, srcFile);
272 } catch (IOException e) {
273 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
274 }
275 }
276
277 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530278 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530279 *
280 * @param file file in which need to be inserted
281 * @param data data which need to be inserted
282 * @throws IOException when fails to insert into file
283 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530284 public static void insertDataIntoJavaFile(File file, String data)
285 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530286 try {
287 updateFileHandle(file, data, false);
288 } catch (IOException e) {
289 throw new IOException("Failed to insert in " + file + "file");
290 }
291 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530292
293 /**
294 * Validates a line size in given file whether it is having more then 120 characters.
295 * If yes it will update and give a new file.
296 *
297 * @param dataFile file in which need to verify all lines.
298 * @return updated file
299 * @throws IOException when fails to do IO operations.
300 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530301 public static File validateLineLength(File dataFile)
302 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530303 File tempFile = dataFile;
304 FileReader fileReader = new FileReader(dataFile);
305 BufferedReader bufferReader = new BufferedReader(fileReader);
306 try {
307 StringBuilder stringBuilder = new StringBuilder();
308 String line = bufferReader.readLine();
309
310 while (line != null) {
311 if (line.length() > LINE_SIZE) {
312 if (line.contains(PERIOD)) {
313 line = whenDelimiterIsPersent(line, LINE_SIZE);
314 } else if (line.contains(SPACE)) {
315 line = whenSpaceIsPresent(line, LINE_SIZE);
316 }
317 stringBuilder.append(line);
318 } else {
319 stringBuilder.append(line + NEW_LINE);
320 }
321 line = bufferReader.readLine();
322 }
323 FileWriter writer = new FileWriter(tempFile);
324 writer.write(stringBuilder.toString());
325 writer.close();
326 return tempFile;
327 } finally {
328 fileReader.close();
329 bufferReader.close();
330 }
331 }
332
Bharat saraswal96dfef02016-06-16 00:29:12 +0530333 /* When delimiters are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530334 private static String whenDelimiterIsPersent(String line, int lineSize) {
335 StringBuilder stringBuilder = new StringBuilder();
336
337 if (line.length() > lineSize) {
338 String[] strArray = line.split(Pattern.quote(PERIOD));
339 stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize);
340 } else {
341 stringBuilder.append(line + NEW_LINE);
342 }
343 String[] strArray = stringBuilder.toString().split(NEW_LINE);
344 StringBuilder tempBuilder = new StringBuilder();
345 for (String str : strArray) {
346 if (str.length() > SUB_LINE_SIZE) {
347 if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) {
348 String[] strArr = str.split(Pattern.quote(PERIOD));
349 tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE);
350 } else if (str.contains(SPACE)) {
351 tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE));
352 }
353 } else {
354 tempBuilder.append(str + NEW_LINE);
355 }
356 }
357 return tempBuilder.toString();
358
359 }
360
Bharat saraswal96dfef02016-06-16 00:29:12 +0530361 /* When spaces are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530362 private static String whenSpaceIsPresent(String line, int lineSize) {
363 StringBuilder stringBuilder = new StringBuilder();
364 if (line.length() > lineSize) {
365 String[] strArray = line.split(SPACE);
366 stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize);
367 } else {
368 stringBuilder.append(line + NEW_LINE);
369 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530370
371 String[] strArray = stringBuilder.toString().split(NEW_LINE);
372 StringBuilder tempBuilder = new StringBuilder();
373 for (String str : strArray) {
374 if (str.length() > SUB_LINE_SIZE) {
375 if (str.contains(SPACE)) {
376 String[] strArr = str.split(SPACE);
377 tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE);
378 }
379 } else {
380 tempBuilder.append(str + NEW_LINE);
381 }
382 }
383 return tempBuilder.toString();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530384 }
385
Bharat saraswal96dfef02016-06-16 00:29:12 +0530386 /* Updates the given line with the given size conditions. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530387 private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string,
Bharat saraswalb551aae2016-07-14 15:18:20 +0530388 int lineSize) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530389
390 StringBuilder tempBuilder = new StringBuilder();
391 for (String str : strArray) {
392 tempBuilder.append(str + string);
393 if (tempBuilder.length() > lineSize) {
394 String tempString = stringBuilder.toString();
395 stringBuilder.delete(ZERO, stringBuilder.length());
396 tempString = trimAtLast(tempString, string);
397 stringBuilder.append(tempString);
398 if (string.equals(PERIOD)) {
399 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string);
400 } else {
401 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string);
402 }
403 tempBuilder.delete(ZERO, tempBuilder.length());
404 tempBuilder.append(TWELVE_SPACE_INDENTATION);
405 } else {
406 stringBuilder.append(str + string);
407 }
408 }
409 String tempString = stringBuilder.toString();
410 tempString = trimAtLast(tempString, string);
411 stringBuilder.delete(ZERO, stringBuilder.length());
412 stringBuilder.append(tempString + NEW_LINE);
413 return stringBuilder;
414 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530415
416 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530417 * Returns the java Package from package path.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530418 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530419 * @param packagePath package path
420 * @return java package
Bharat saraswal96dfef02016-06-16 00:29:12 +0530421 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530422 public static String getJavaPackageFromPackagePath(String packagePath) {
423 return packagePath.replace(SLASH, PERIOD);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530424 }
425
426 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530427 * Returns the directory path corresponding to java package.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530428 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530429 * @param packagePath package path
430 * @return java package
Bharat saraswal96dfef02016-06-16 00:29:12 +0530431 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530432 public static String getPackageDirPathFromJavaJPackage(String packagePath) {
433 return packagePath.replace(PERIOD, SLASH);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530434 }
435
436 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530437 * Returns the YANG identifier name as java identifier with first letter
438 * in small.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530439 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530440 * @param yangIdentifier identifier in YANG file.
441 * @return corresponding java identifier
Bharat saraswal96dfef02016-06-16 00:29:12 +0530442 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530443 public static String getSmallCase(String yangIdentifier) {
444 return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530445 }
446
447 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530448 * Returns the YANG identifier name as java identifier with first letter
449 * in capital.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530450 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530451 * @param yangIdentifier identifier in YANG file
452 * @return corresponding java identifier
Bharat saraswal96dfef02016-06-16 00:29:12 +0530453 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530454 public static String getCapitalCase(String yangIdentifier) {
455 yangIdentifier = yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
456 return restrictConsecutiveCapitalCase(yangIdentifier);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530457 }
458
459 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530460 * Restricts consecutive capital cased string as a rule in camel case.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530461 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530462 * @param consecCapitalCaseRemover which requires the restriction of consecutive capital case
463 * @return string without consecutive capital case
Bharat saraswal96dfef02016-06-16 00:29:12 +0530464 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530465 public static String restrictConsecutiveCapitalCase(String consecCapitalCaseRemover) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530466
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530467 for (int k = 0; k < consecCapitalCaseRemover.length(); k++) {
468 if (k + 1 < consecCapitalCaseRemover.length()) {
469 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k))) {
470 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k + 1))) {
471 consecCapitalCaseRemover = consecCapitalCaseRemover.substring(0, k + 1)
472 + consecCapitalCaseRemover.substring(k + 1, k + 2).toLowerCase()
473 + consecCapitalCaseRemover.substring(k + 2);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530474 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530475 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530476 }
477 }
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530478 return consecCapitalCaseRemover;
Bharat saraswal96dfef02016-06-16 00:29:12 +0530479 }
480
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530481 /**
482 * Adds prefix, if the string begins with digit or is a java key word.
483 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530484 * @param camelCasePrefix string for adding prefix
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530485 * @param conflictResolver object of YANG to java naming conflict util
486 * @return prefixed camel case string
487 */
488 public static String addPrefix(String camelCasePrefix, YangToJavaNamingConflictUtil conflictResolver) {
489
490 String prefix = getPrefixForIdentifier(conflictResolver);
491 if (camelCasePrefix.matches(REGEX_FOR_FIRST_DIGIT)) {
492 camelCasePrefix = prefix + camelCasePrefix;
493 }
494 if (JAVA_KEY_WORDS.contains(camelCasePrefix)) {
495 camelCasePrefix = prefix + camelCasePrefix.substring(0, 1).toUpperCase()
496 + camelCasePrefix.substring(1);
497 }
498 return camelCasePrefix;
499 }
500
501 /**
502 * Applies the rule that a string does not end with a capitalized letter and capitalizes
503 * the letter next to a number in an array.
504 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530505 * @param stringArray containing strings for camel case separation
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530506 * @param conflictResolver object of YANG to java naming conflict util
507 * @return camel case rule checked string
508 */
509 public static String applyCamelCaseRule(String[] stringArray, YangToJavaNamingConflictUtil conflictResolver) {
510
511 String ruleChecker = stringArray[0].toLowerCase();
512 int i;
513 if (ruleChecker.matches(REGEX_FOR_FIRST_DIGIT)) {
514 i = 0;
515 ruleChecker = EMPTY_STRING;
516 } else {
517 i = 1;
518 }
519 for (; i < stringArray.length; i++) {
520 if (i + 1 == stringArray.length) {
521 if (stringArray[i].matches(REGEX_FOR_SINGLE_LETTER)
522 || stringArray[i].matches(REGEX_FOR_DIGITS_WITH_SINGLE_LETTER)) {
523 ruleChecker = ruleChecker + stringArray[i].toLowerCase();
524 break;
525 }
526 }
527 if (stringArray[i].matches(REGEX_FOR_FIRST_DIGIT)) {
528 for (int j = 0; j < stringArray[i].length(); j++) {
529 char letterCheck = stringArray[i].charAt(j);
530 if (Character.isLetter(letterCheck)) {
531 stringArray[i] = stringArray[i].substring(0, j)
532 + stringArray[i].substring(j, j + 1).toUpperCase() + stringArray[i].substring(j + 1);
533 break;
534 }
535 }
536 ruleChecker = ruleChecker + stringArray[i];
537 } else {
538 ruleChecker = ruleChecker + stringArray[i].substring(0, 1).toUpperCase() + stringArray[i].substring(1);
539 }
540 }
541 String ruleCheckerWithPrefix = addPrefix(ruleChecker, conflictResolver);
542 return restrictConsecutiveCapitalCase(ruleCheckerWithPrefix);
543 }
544
545 /**
546 * Resolves the conflict when input has upper case.
547 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530548 * @param stringArray containing strings for upper case conflict resolver
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530549 * @param conflictResolver object of YANG to java naming conflict util
550 * @return camel cased string
551 */
552 public static String upperCaseConflictResolver(String[] stringArray,
553 YangToJavaNamingConflictUtil conflictResolver) {
554
555 for (int l = 0; l < stringArray.length; l++) {
556 String[] upperCaseSplitArray = stringArray[l].split(REGEX_WITH_UPPERCASE);
557 for (int m = 0; m < upperCaseSplitArray.length; m++) {
558 if (upperCaseSplitArray[m].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
559 int check = m;
560 while (check + 1 < upperCaseSplitArray.length) {
561 if (upperCaseSplitArray[check + 1].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
562 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
563 check = check + 1;
564 } else if (upperCaseSplitArray[check + 1]
565 .matches(REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES)) {
566 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
567 break;
568 } else {
569 break;
570 }
571 }
572 }
573 }
574 StringBuilder strBuilder = new StringBuilder();
575 for (String element : upperCaseSplitArray) {
576 strBuilder.append(element);
577 }
578 stringArray[l] = strBuilder.toString();
579 }
580 List<String> result = new ArrayList<String>();
581 for (String element : stringArray) {
582 String[] capitalCaseSplitArray = element.split(REGEX_WITH_UPPERCASE);
583 for (String letter : capitalCaseSplitArray) {
584 String[] arrayForAddition = letter.split(REGEX_WITH_DIGITS);
585 List<String> list = Arrays.asList(arrayForAddition);
586 for (String str : list) {
587 if (str != null && !str.isEmpty()) {
588 result.add(str);
589 }
590 }
591 }
592 }
593 stringArray = result.toArray(new String[result.size()]);
594 return applyCamelCaseRule(stringArray, conflictResolver);
595 }
596
597 /**
598 * Returns the YANG identifier name as java identifier.
599 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530600 * @param yangIdentifier identifier in YANG file
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530601 * @param conflictResolver object of YANG to java naming conflict util
602 * @return corresponding java identifier
603 */
604 public static String getCamelCase(String yangIdentifier, YangToJavaNamingConflictUtil conflictResolver) {
605
606 if (conflictResolver != null) {
607 String replacementForHyphen = conflictResolver.getReplacementForHyphen();
608 String replacementForPeriod = conflictResolver.getReplacementForPeriod();
609 String replacementForUnderscore = conflictResolver.getReplacementForUnderscore();
610 if (replacementForPeriod != null) {
611 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_PERIOD,
612 PERIOD + replacementForPeriod.toLowerCase() + PERIOD);
613 }
614 if (replacementForUnderscore != null) {
615 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_UNDERSCORE,
616 UNDER_SCORE + replacementForUnderscore.toLowerCase() + UNDER_SCORE);
617 }
618 if (replacementForHyphen != null) {
619 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_HYPHEN,
620 HYPHEN + replacementForHyphen.toLowerCase() + HYPHEN);
621 }
622 }
623 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_IDENTIFIER_SPECIAL_CHAR, COLAN);
624 String[] strArray = yangIdentifier.split(COLAN);
625 if (strArray[0].isEmpty()) {
626 List<String> stringArrangement = new ArrayList<String>();
627 for (int i = 1; i < strArray.length; i++) {
628 stringArrangement.add(strArray[i]);
629 }
630 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
631 }
632 return upperCaseConflictResolver(strArray, conflictResolver);
633 }
634
635 /**
636 * Prefix for adding with identifier and namespace, when it is a java keyword or starting with digits.
637 *
638 * @param conflictResolver object of YANG to java naming conflict util
639 * @return prefix which needs to be added
640 */
641 public static String getPrefixForIdentifier(YangToJavaNamingConflictUtil conflictResolver) {
642
643 String prefixForIdentifier = null;
644 if (conflictResolver != null) {
645 prefixForIdentifier = conflictResolver.getPrefixForIdentifier();
646 }
647 if (prefixForIdentifier != null) {
648 prefixForIdentifier = prefixForIdentifier.replaceAll(REGEX_WITH_ALL_SPECIAL_CHAR, COLAN);
649 String[] strArray = prefixForIdentifier.split(COLAN);
650 try {
651 if (strArray[0].isEmpty()) {
652 List<String> stringArrangement = new ArrayList<String>();
653 for (int i = 1; i < strArray.length; i++) {
654 stringArrangement.add(strArray[i]);
655 }
656 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
657 }
658 prefixForIdentifier = strArray[0];
659 for (int j = 1; j < strArray.length; j++) {
660 prefixForIdentifier = prefixForIdentifier + strArray[j].substring(0, 1).toUpperCase() +
661 strArray[j].substring(1);
662 }
663 } catch (ArrayIndexOutOfBoundsException outOfBoundsException) {
664 throw new TranslatorException("The given prefix in pom.xml is invalid.");
665 }
666 } else {
667 prefixForIdentifier = YANG_AUTO_PREFIX;
668 }
669 return prefixForIdentifier;
670 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530671}