blob: 0c6b51e2f9971f18f865b270b851e82f5a642a5c [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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053033import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
34import org.onosproject.yangutils.datamodel.javadatamodel.YangToJavaNamingConflictUtil;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053035import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053036
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053037import static org.onosproject.yangutils.utils.UtilConstants.COLAN;
Bharat saraswale2d51d62016-03-23 19:40:35 +053038import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053039import static org.onosproject.yangutils.utils.UtilConstants.HASH;
Bharat saraswal96dfef02016-06-16 00:29:12 +053040import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053041import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053042import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053043import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053044import static org.onosproject.yangutils.utils.UtilConstants.ORG;
45import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053046import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053047import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_DIGITS_WITH_SINGLE_LETTER;
48import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
49import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_HYPHEN;
50import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_IDENTIFIER_SPECIAL_CHAR;
51import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_PERIOD;
52import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_SINGLE_LETTER;
53import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_UNDERSCORE;
54import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_ALL_SPECIAL_CHAR;
55import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_DIGITS;
56import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE;
57import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES;
58import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_UPPERCASE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053059import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
60import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
61import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053062import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053063import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
64import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
Bharat saraswale2d51d62016-03-23 19:40:35 +053065import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
66import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
Bharat saraswal96dfef02016-06-16 00:29:12 +053067import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053068import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar S38046502016-03-23 15:30:27 +053069
Bharat saraswal870c56f2016-02-20 21:57:16 +053070/**
Bharat saraswald9822e92016-04-05 15:13:44 +053071 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 */
73public final class YangIoUtils {
74
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053075 private static final int LINE_SIZE = 118;
Bharat saraswal33dfa012016-05-17 19:59:16 +053076 private static final int SUB_LINE_SIZE = 112;
77 private static final int ZERO = 0;
Bharat saraswal870c56f2016-02-20 21:57:16 +053078
79 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053080 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053081 */
82 private YangIoUtils() {
83 }
84
85 /**
86 * Creates the directory structure.
87 *
88 * @param path directory path
89 * @return directory structure
90 */
91 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +053092 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053093 generatedDir.mkdirs();
94 return generatedDir;
95 }
96
97 /**
98 * Adds package info file for the created directory.
99 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530100 * @param path directory path
101 * @param classInfo class info for the package
102 * @param pack package of the directory
103 * @param isChildNode is it a child node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530104 * @param pluginConfig plugin configurations
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530105 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530106 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530107 public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode,
Bharat saraswalb551aae2016-07-14 15:18:20 +0530108 YangPluginConfig pluginConfig)
Bharat saraswalc0e04842016-05-12 13:16:57 +0530109 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530110
Bharat saraswal33dfa012016-05-17 19:59:16 +0530111 pack = parsePkg(pack);
112
Bharat saraswal870c56f2016-02-20 21:57:16 +0530113 try {
114
Bharat saraswale2d51d62016-03-23 19:40:35 +0530115 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530116 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530117
118 FileWriter fileWriter = new FileWriter(packageInfo);
119 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
120
b.janani68c55e12016-02-24 12:23:03 +0530121 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530122 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode, pluginConfig));
123 String pkg = PACKAGE + SPACE + pack + SEMI_COLAN;
124 if (pkg.length() > LINE_SIZE) {
125 pkg = whenDelimiterIsPersent(pkg, LINE_SIZE);
126 }
127 bufferedWriter.write(pkg);
b.janani68c55e12016-02-24 12:23:03 +0530128 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530129 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530130 } catch (IOException e) {
131 throw new IOException("Exception occured while creating package info file.");
132 }
133 }
134
135 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530136 * Parses package and returns updated package.
137 *
138 * @param pack package needs to be updated
139 * @return updated package
140 */
141 public static String parsePkg(String pack) {
142
143 if (pack.contains(ORG)) {
144 String[] strArray = pack.split(ORG);
145 if (strArray.length >= 3) {
146 for (int i = 1; i < strArray.length; i++) {
147 if (i == 1) {
148 pack = ORG + strArray[1];
149 } else {
150 pack = pack + ORG + strArray[i];
151 }
152 }
153 } else {
154 pack = ORG + strArray[1];
155 }
156 }
157
158 return pack;
159 }
160
161 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530162 * Cleans the generated directory if already exist in source folder.
163 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530164 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530165 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530166 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530167 public static void deleteDirectory(String dir)
168 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530169 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530170 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530171 try {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530172 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530173 } catch (IOException e) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530174 throw new IOException(
175 "Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530177 }
178 }
179
180 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530181 * Searches and deletes generated temporary directories.
182 *
183 * @param root root directory
184 * @throws IOException when fails to do IO operations.
185 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530186 public static void searchAndDeleteTempDir(String root)
187 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530188 List<File> store = new LinkedList<>();
189 Stack<String> stack = new Stack<>();
190 stack.push(root);
191
192 while (!stack.empty()) {
193 root = stack.pop();
194 File file = new File(root);
195 File[] filelist = file.listFiles();
196 if (filelist == null || filelist.length == 0) {
197 continue;
198 }
199 for (File current : filelist) {
200 if (current.isDirectory()) {
201 stack.push(current.toString());
202 if (current.getName().endsWith("-Temp")) {
203 store.add(current);
204 }
205 }
206 }
207 }
208
209 for (File dir : store) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530210 FileUtils.deleteDirectory(dir);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530211 }
212 }
213
214 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530215 * Removes extra char from the string.
216 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530217 * @param valueString string to be trimmed
218 * @param removalStirng extra chars
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530219 * @return new string
220 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530221 public static String trimAtLast(String valueString, String removalStirng) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530222 StringBuilder stringBuilder = new StringBuilder(valueString);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530223 int index = valueString.lastIndexOf(removalStirng);
224 if (index != -1) {
225 stringBuilder.deleteCharAt(index);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530226 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530227 return stringBuilder.toString();
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530228 }
229
Vinod Kumar S38046502016-03-23 15:30:27 +0530230 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530231 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530232 *
233 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530234 * put
235 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530236 * @return absolute path of the package in canonical form
237 */
238 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
239
240 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530241 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530242 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530243 String[] strArray = pathOfJavaPkg.split(SLASH);
244 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530245 return pathOfJavaPkg;
246 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530247 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530248 }
249 }
250
251 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530252 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530253 *
254 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530255 * put
256 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530257 * @return absolute path of the package in canonical form
258 */
259 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530260 return baseCodeGenPath + pathOfJavaPkg;
261 }
262
263 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530264 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530265 *
266 * @param appendFile temp file
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530267 * @param srcFile main file
Bharat saraswale2d51d62016-03-23 19:40:35 +0530268 * @throws IOException when fails to append contents
269 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530270 public static void mergeJavaFiles(File appendFile, File srcFile)
271 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530272 try {
273 appendFileContents(appendFile, srcFile);
274 } catch (IOException e) {
275 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
276 }
277 }
278
279 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530280 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530281 *
282 * @param file file in which need to be inserted
283 * @param data data which need to be inserted
284 * @throws IOException when fails to insert into file
285 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530286 public static void insertDataIntoJavaFile(File file, String data)
287 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530288 try {
289 updateFileHandle(file, data, false);
290 } catch (IOException e) {
291 throw new IOException("Failed to insert in " + file + "file");
292 }
293 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530294
295 /**
296 * Validates a line size in given file whether it is having more then 120 characters.
297 * If yes it will update and give a new file.
298 *
299 * @param dataFile file in which need to verify all lines.
300 * @return updated file
301 * @throws IOException when fails to do IO operations.
302 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530303 public static File validateLineLength(File dataFile)
304 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530305 File tempFile = dataFile;
306 FileReader fileReader = new FileReader(dataFile);
307 BufferedReader bufferReader = new BufferedReader(fileReader);
308 try {
309 StringBuilder stringBuilder = new StringBuilder();
310 String line = bufferReader.readLine();
311
312 while (line != null) {
313 if (line.length() > LINE_SIZE) {
314 if (line.contains(PERIOD)) {
315 line = whenDelimiterIsPersent(line, LINE_SIZE);
316 } else if (line.contains(SPACE)) {
317 line = whenSpaceIsPresent(line, LINE_SIZE);
318 }
319 stringBuilder.append(line);
320 } else {
321 stringBuilder.append(line + NEW_LINE);
322 }
323 line = bufferReader.readLine();
324 }
325 FileWriter writer = new FileWriter(tempFile);
326 writer.write(stringBuilder.toString());
327 writer.close();
328 return tempFile;
329 } finally {
330 fileReader.close();
331 bufferReader.close();
332 }
333 }
334
Bharat saraswal96dfef02016-06-16 00:29:12 +0530335 /* When delimiters are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530336 private static String whenDelimiterIsPersent(String line, int lineSize) {
337 StringBuilder stringBuilder = new StringBuilder();
338
339 if (line.length() > lineSize) {
340 String[] strArray = line.split(Pattern.quote(PERIOD));
341 stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize);
342 } else {
343 stringBuilder.append(line + NEW_LINE);
344 }
345 String[] strArray = stringBuilder.toString().split(NEW_LINE);
346 StringBuilder tempBuilder = new StringBuilder();
347 for (String str : strArray) {
348 if (str.length() > SUB_LINE_SIZE) {
349 if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) {
350 String[] strArr = str.split(Pattern.quote(PERIOD));
351 tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE);
352 } else if (str.contains(SPACE)) {
353 tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE));
354 }
355 } else {
356 tempBuilder.append(str + NEW_LINE);
357 }
358 }
359 return tempBuilder.toString();
360
361 }
362
Bharat saraswal96dfef02016-06-16 00:29:12 +0530363 /* When spaces are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530364 private static String whenSpaceIsPresent(String line, int lineSize) {
365 StringBuilder stringBuilder = new StringBuilder();
366 if (line.length() > lineSize) {
367 String[] strArray = line.split(SPACE);
368 stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize);
369 } else {
370 stringBuilder.append(line + NEW_LINE);
371 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530372
373 String[] strArray = stringBuilder.toString().split(NEW_LINE);
374 StringBuilder tempBuilder = new StringBuilder();
375 for (String str : strArray) {
376 if (str.length() > SUB_LINE_SIZE) {
377 if (str.contains(SPACE)) {
378 String[] strArr = str.split(SPACE);
379 tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE);
380 }
381 } else {
382 tempBuilder.append(str + NEW_LINE);
383 }
384 }
385 return tempBuilder.toString();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530386 }
387
Bharat saraswal96dfef02016-06-16 00:29:12 +0530388 /* Updates the given line with the given size conditions. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530389 private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string,
Bharat saraswalb551aae2016-07-14 15:18:20 +0530390 int lineSize) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530391
392 StringBuilder tempBuilder = new StringBuilder();
393 for (String str : strArray) {
394 tempBuilder.append(str + string);
395 if (tempBuilder.length() > lineSize) {
396 String tempString = stringBuilder.toString();
397 stringBuilder.delete(ZERO, stringBuilder.length());
398 tempString = trimAtLast(tempString, string);
399 stringBuilder.append(tempString);
400 if (string.equals(PERIOD)) {
401 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string);
402 } else {
403 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string);
404 }
405 tempBuilder.delete(ZERO, tempBuilder.length());
406 tempBuilder.append(TWELVE_SPACE_INDENTATION);
407 } else {
408 stringBuilder.append(str + string);
409 }
410 }
411 String tempString = stringBuilder.toString();
412 tempString = trimAtLast(tempString, string);
413 stringBuilder.delete(ZERO, stringBuilder.length());
414 stringBuilder.append(tempString + NEW_LINE);
415 return stringBuilder;
416 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530417
418 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530419 * Returns the java Package from package path.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530420 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530421 * @param packagePath package path
422 * @return java package
Bharat saraswal96dfef02016-06-16 00:29:12 +0530423 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530424 public static String getJavaPackageFromPackagePath(String packagePath) {
425 return packagePath.replace(SLASH, PERIOD);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530426 }
427
428 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530429 * Returns the directory path corresponding to java package.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530430 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530431 * @param packagePath package path
432 * @return java package
Bharat saraswal96dfef02016-06-16 00:29:12 +0530433 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530434 public static String getPackageDirPathFromJavaJPackage(String packagePath) {
435 return packagePath.replace(PERIOD, SLASH);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530436 }
437
438 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530439 * Returns the YANG identifier name as java identifier with first letter
440 * in small.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530441 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530442 * @param yangIdentifier identifier in YANG file.
443 * @return corresponding java identifier
Bharat saraswal96dfef02016-06-16 00:29:12 +0530444 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530445 public static String getSmallCase(String yangIdentifier) {
446 return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530447 }
448
449 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530450 * Returns the YANG identifier name as java identifier with first letter
451 * in capital.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530452 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530453 * @param yangIdentifier identifier in YANG file
454 * @return corresponding java identifier
Bharat saraswal96dfef02016-06-16 00:29:12 +0530455 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530456 public static String getCapitalCase(String yangIdentifier) {
457 yangIdentifier = yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
458 return restrictConsecutiveCapitalCase(yangIdentifier);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530459 }
460
461 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530462 * Restricts consecutive capital cased string as a rule in camel case.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530463 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530464 * @param consecCapitalCaseRemover which requires the restriction of consecutive capital case
465 * @return string without consecutive capital case
Bharat saraswal96dfef02016-06-16 00:29:12 +0530466 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530467 public static String restrictConsecutiveCapitalCase(String consecCapitalCaseRemover) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530468
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530469 for (int k = 0; k < consecCapitalCaseRemover.length(); k++) {
470 if (k + 1 < consecCapitalCaseRemover.length()) {
471 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k))) {
472 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k + 1))) {
473 consecCapitalCaseRemover = consecCapitalCaseRemover.substring(0, k + 1)
474 + consecCapitalCaseRemover.substring(k + 1, k + 2).toLowerCase()
475 + consecCapitalCaseRemover.substring(k + 2);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530476 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530477 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530478 }
479 }
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530480 return consecCapitalCaseRemover;
Bharat saraswal96dfef02016-06-16 00:29:12 +0530481 }
482
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530483 /**
484 * Adds prefix, if the string begins with digit or is a java key word.
485 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530486 * @param camelCasePrefix string for adding prefix
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530487 * @param conflictResolver object of YANG to java naming conflict util
488 * @return prefixed camel case string
489 */
490 public static String addPrefix(String camelCasePrefix, YangToJavaNamingConflictUtil conflictResolver) {
491
492 String prefix = getPrefixForIdentifier(conflictResolver);
493 if (camelCasePrefix.matches(REGEX_FOR_FIRST_DIGIT)) {
494 camelCasePrefix = prefix + camelCasePrefix;
495 }
496 if (JAVA_KEY_WORDS.contains(camelCasePrefix)) {
497 camelCasePrefix = prefix + camelCasePrefix.substring(0, 1).toUpperCase()
498 + camelCasePrefix.substring(1);
499 }
500 return camelCasePrefix;
501 }
502
503 /**
504 * Applies the rule that a string does not end with a capitalized letter and capitalizes
505 * the letter next to a number in an array.
506 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530507 * @param stringArray containing strings for camel case separation
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530508 * @param conflictResolver object of YANG to java naming conflict util
509 * @return camel case rule checked string
510 */
511 public static String applyCamelCaseRule(String[] stringArray, YangToJavaNamingConflictUtil conflictResolver) {
512
513 String ruleChecker = stringArray[0].toLowerCase();
514 int i;
515 if (ruleChecker.matches(REGEX_FOR_FIRST_DIGIT)) {
516 i = 0;
517 ruleChecker = EMPTY_STRING;
518 } else {
519 i = 1;
520 }
521 for (; i < stringArray.length; i++) {
522 if (i + 1 == stringArray.length) {
523 if (stringArray[i].matches(REGEX_FOR_SINGLE_LETTER)
524 || stringArray[i].matches(REGEX_FOR_DIGITS_WITH_SINGLE_LETTER)) {
525 ruleChecker = ruleChecker + stringArray[i].toLowerCase();
526 break;
527 }
528 }
529 if (stringArray[i].matches(REGEX_FOR_FIRST_DIGIT)) {
530 for (int j = 0; j < stringArray[i].length(); j++) {
531 char letterCheck = stringArray[i].charAt(j);
532 if (Character.isLetter(letterCheck)) {
533 stringArray[i] = stringArray[i].substring(0, j)
534 + stringArray[i].substring(j, j + 1).toUpperCase() + stringArray[i].substring(j + 1);
535 break;
536 }
537 }
538 ruleChecker = ruleChecker + stringArray[i];
539 } else {
540 ruleChecker = ruleChecker + stringArray[i].substring(0, 1).toUpperCase() + stringArray[i].substring(1);
541 }
542 }
543 String ruleCheckerWithPrefix = addPrefix(ruleChecker, conflictResolver);
544 return restrictConsecutiveCapitalCase(ruleCheckerWithPrefix);
545 }
546
547 /**
548 * Resolves the conflict when input has upper case.
549 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530550 * @param stringArray containing strings for upper case conflict resolver
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530551 * @param conflictResolver object of YANG to java naming conflict util
552 * @return camel cased string
553 */
554 public static String upperCaseConflictResolver(String[] stringArray,
555 YangToJavaNamingConflictUtil conflictResolver) {
556
557 for (int l = 0; l < stringArray.length; l++) {
558 String[] upperCaseSplitArray = stringArray[l].split(REGEX_WITH_UPPERCASE);
559 for (int m = 0; m < upperCaseSplitArray.length; m++) {
560 if (upperCaseSplitArray[m].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
561 int check = m;
562 while (check + 1 < upperCaseSplitArray.length) {
563 if (upperCaseSplitArray[check + 1].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
564 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
565 check = check + 1;
566 } else if (upperCaseSplitArray[check + 1]
567 .matches(REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES)) {
568 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
569 break;
570 } else {
571 break;
572 }
573 }
574 }
575 }
576 StringBuilder strBuilder = new StringBuilder();
577 for (String element : upperCaseSplitArray) {
578 strBuilder.append(element);
579 }
580 stringArray[l] = strBuilder.toString();
581 }
582 List<String> result = new ArrayList<String>();
583 for (String element : stringArray) {
584 String[] capitalCaseSplitArray = element.split(REGEX_WITH_UPPERCASE);
585 for (String letter : capitalCaseSplitArray) {
586 String[] arrayForAddition = letter.split(REGEX_WITH_DIGITS);
587 List<String> list = Arrays.asList(arrayForAddition);
588 for (String str : list) {
589 if (str != null && !str.isEmpty()) {
590 result.add(str);
591 }
592 }
593 }
594 }
595 stringArray = result.toArray(new String[result.size()]);
596 return applyCamelCaseRule(stringArray, conflictResolver);
597 }
598
599 /**
600 * Returns the YANG identifier name as java identifier.
601 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530602 * @param yangIdentifier identifier in YANG file
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530603 * @param conflictResolver object of YANG to java naming conflict util
604 * @return corresponding java identifier
605 */
606 public static String getCamelCase(String yangIdentifier, YangToJavaNamingConflictUtil conflictResolver) {
607
608 if (conflictResolver != null) {
609 String replacementForHyphen = conflictResolver.getReplacementForHyphen();
610 String replacementForPeriod = conflictResolver.getReplacementForPeriod();
611 String replacementForUnderscore = conflictResolver.getReplacementForUnderscore();
612 if (replacementForPeriod != null) {
613 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_PERIOD,
614 PERIOD + replacementForPeriod.toLowerCase() + PERIOD);
615 }
616 if (replacementForUnderscore != null) {
617 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_UNDERSCORE,
618 UNDER_SCORE + replacementForUnderscore.toLowerCase() + UNDER_SCORE);
619 }
620 if (replacementForHyphen != null) {
621 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_HYPHEN,
622 HYPHEN + replacementForHyphen.toLowerCase() + HYPHEN);
623 }
624 }
625 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_IDENTIFIER_SPECIAL_CHAR, COLAN);
626 String[] strArray = yangIdentifier.split(COLAN);
627 if (strArray[0].isEmpty()) {
628 List<String> stringArrangement = new ArrayList<String>();
629 for (int i = 1; i < strArray.length; i++) {
630 stringArrangement.add(strArray[i]);
631 }
632 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
633 }
634 return upperCaseConflictResolver(strArray, conflictResolver);
635 }
636
637 /**
638 * Prefix for adding with identifier and namespace, when it is a java keyword or starting with digits.
639 *
640 * @param conflictResolver object of YANG to java naming conflict util
641 * @return prefix which needs to be added
642 */
643 public static String getPrefixForIdentifier(YangToJavaNamingConflictUtil conflictResolver) {
644
645 String prefixForIdentifier = null;
646 if (conflictResolver != null) {
647 prefixForIdentifier = conflictResolver.getPrefixForIdentifier();
648 }
649 if (prefixForIdentifier != null) {
650 prefixForIdentifier = prefixForIdentifier.replaceAll(REGEX_WITH_ALL_SPECIAL_CHAR, COLAN);
651 String[] strArray = prefixForIdentifier.split(COLAN);
652 try {
653 if (strArray[0].isEmpty()) {
654 List<String> stringArrangement = new ArrayList<String>();
655 for (int i = 1; i < strArray.length; i++) {
656 stringArrangement.add(strArray[i]);
657 }
658 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
659 }
660 prefixForIdentifier = strArray[0];
661 for (int j = 1; j < strArray.length; j++) {
662 prefixForIdentifier = prefixForIdentifier + strArray[j].substring(0, 1).toUpperCase() +
663 strArray[j].substring(1);
664 }
665 } catch (ArrayIndexOutOfBoundsException outOfBoundsException) {
666 throw new TranslatorException("The given prefix in pom.xml is invalid.");
667 }
668 } else {
669 prefixForIdentifier = YANG_AUTO_PREFIX;
670 }
671 return prefixForIdentifier;
672 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530673}