blob: baea6eacb45be067c504305bf2bab107b79c8161 [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;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031import org.apache.commons.io.FileUtils;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053033
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053034import static org.onosproject.yangutils.utils.UtilConstants.COLAN;
Bharat saraswale2d51d62016-03-23 19:40:35 +053035import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
36import 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,
106 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 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530215 * @param valueString string to be trimmed
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530216 * @param removealStirng extra chars
217 * @return new string
218 */
219 public static String trimAtLast(String valueString, String removealStirng) {
220 StringBuilder stringBuilder = new StringBuilder(valueString);
221 int index = valueString.lastIndexOf(removealStirng);
222 stringBuilder.deleteCharAt(index);
223 return stringBuilder.toString();
224 }
225
226 /**
227 * Returns new parted string.
228 *
229 * @param partString string to be parted
230 * @return parted string
231 */
232 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530233 String[] strArray = partString.split(COMMA);
234 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530235 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530236 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530237 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530238 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530239 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
240 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530241 }
242 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530243 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530244 }
245
Vinod Kumar S38046502016-03-23 15:30:27 +0530246 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530247 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530248 *
249 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530250 * put
251 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530252 * @return absolute path of the package in canonical form
253 */
254 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
255
256 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530257 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530258 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530259 String[] strArray = pathOfJavaPkg.split(SLASH);
260 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530261 return pathOfJavaPkg;
262 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530263 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530264 }
265 }
266
267 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530268 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 *
270 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530271 * put
272 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530273 * @return absolute path of the package in canonical form
274 */
275 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530276 return baseCodeGenPath + pathOfJavaPkg;
277 }
278
279 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530280 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530281 *
282 * @param appendFile temp file
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530283 * @param srcFile main file
Bharat saraswale2d51d62016-03-23 19:40:35 +0530284 * @throws IOException when fails to append contents
285 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530286 public static void mergeJavaFiles(File appendFile, File srcFile)
287 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530288 try {
289 appendFileContents(appendFile, srcFile);
290 } catch (IOException e) {
291 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
292 }
293 }
294
295 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530296 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530297 *
298 * @param file file in which need to be inserted
299 * @param data data which need to be inserted
300 * @throws IOException when fails to insert into file
301 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530302 public static void insertDataIntoJavaFile(File file, String data)
303 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530304 try {
305 updateFileHandle(file, data, false);
306 } catch (IOException e) {
307 throw new IOException("Failed to insert in " + file + "file");
308 }
309 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530310
311 /**
312 * Validates a line size in given file whether it is having more then 120 characters.
313 * If yes it will update and give a new file.
314 *
315 * @param dataFile file in which need to verify all lines.
316 * @return updated file
317 * @throws IOException when fails to do IO operations.
318 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530319 public static File validateLineLength(File dataFile)
320 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530321 File tempFile = dataFile;
322 FileReader fileReader = new FileReader(dataFile);
323 BufferedReader bufferReader = new BufferedReader(fileReader);
324 try {
325 StringBuilder stringBuilder = new StringBuilder();
326 String line = bufferReader.readLine();
327
328 while (line != null) {
329 if (line.length() > LINE_SIZE) {
330 if (line.contains(PERIOD)) {
331 line = whenDelimiterIsPersent(line, LINE_SIZE);
332 } else if (line.contains(SPACE)) {
333 line = whenSpaceIsPresent(line, LINE_SIZE);
334 }
335 stringBuilder.append(line);
336 } else {
337 stringBuilder.append(line + NEW_LINE);
338 }
339 line = bufferReader.readLine();
340 }
341 FileWriter writer = new FileWriter(tempFile);
342 writer.write(stringBuilder.toString());
343 writer.close();
344 return tempFile;
345 } finally {
346 fileReader.close();
347 bufferReader.close();
348 }
349 }
350
Bharat saraswal96dfef02016-06-16 00:29:12 +0530351 /* When delimiters are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530352 private static String whenDelimiterIsPersent(String line, int lineSize) {
353 StringBuilder stringBuilder = new StringBuilder();
354
355 if (line.length() > lineSize) {
356 String[] strArray = line.split(Pattern.quote(PERIOD));
357 stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize);
358 } else {
359 stringBuilder.append(line + NEW_LINE);
360 }
361 String[] strArray = stringBuilder.toString().split(NEW_LINE);
362 StringBuilder tempBuilder = new StringBuilder();
363 for (String str : strArray) {
364 if (str.length() > SUB_LINE_SIZE) {
365 if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) {
366 String[] strArr = str.split(Pattern.quote(PERIOD));
367 tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE);
368 } else if (str.contains(SPACE)) {
369 tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE));
370 }
371 } else {
372 tempBuilder.append(str + NEW_LINE);
373 }
374 }
375 return tempBuilder.toString();
376
377 }
378
Bharat saraswal96dfef02016-06-16 00:29:12 +0530379 /* When spaces are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530380 private static String whenSpaceIsPresent(String line, int lineSize) {
381 StringBuilder stringBuilder = new StringBuilder();
382 if (line.length() > lineSize) {
383 String[] strArray = line.split(SPACE);
384 stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize);
385 } else {
386 stringBuilder.append(line + NEW_LINE);
387 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530388
389 String[] strArray = stringBuilder.toString().split(NEW_LINE);
390 StringBuilder tempBuilder = new StringBuilder();
391 for (String str : strArray) {
392 if (str.length() > SUB_LINE_SIZE) {
393 if (str.contains(SPACE)) {
394 String[] strArr = str.split(SPACE);
395 tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE);
396 }
397 } else {
398 tempBuilder.append(str + NEW_LINE);
399 }
400 }
401 return tempBuilder.toString();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530402 }
403
Bharat saraswal96dfef02016-06-16 00:29:12 +0530404 /* Updates the given line with the given size conditions. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530405 private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string,
406 int lineSize) {
407
408 StringBuilder tempBuilder = new StringBuilder();
409 for (String str : strArray) {
410 tempBuilder.append(str + string);
411 if (tempBuilder.length() > lineSize) {
412 String tempString = stringBuilder.toString();
413 stringBuilder.delete(ZERO, stringBuilder.length());
414 tempString = trimAtLast(tempString, string);
415 stringBuilder.append(tempString);
416 if (string.equals(PERIOD)) {
417 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string);
418 } else {
419 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string);
420 }
421 tempBuilder.delete(ZERO, tempBuilder.length());
422 tempBuilder.append(TWELVE_SPACE_INDENTATION);
423 } else {
424 stringBuilder.append(str + string);
425 }
426 }
427 String tempString = stringBuilder.toString();
428 tempString = trimAtLast(tempString, string);
429 stringBuilder.delete(ZERO, stringBuilder.length());
430 stringBuilder.append(tempString + NEW_LINE);
431 return stringBuilder;
432 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530433
434 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530435 * Returns the java Package from package path.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530436 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530437 * @param packagePath package path
438 * @return java package
Bharat saraswal96dfef02016-06-16 00:29:12 +0530439 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530440 public static String getJavaPackageFromPackagePath(String packagePath) {
441 return packagePath.replace(SLASH, PERIOD);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530442 }
443
444 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530445 * Returns the directory path corresponding to java package.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530446 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530447 * @param packagePath package path
448 * @return java package
Bharat saraswal96dfef02016-06-16 00:29:12 +0530449 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530450 public static String getPackageDirPathFromJavaJPackage(String packagePath) {
451 return packagePath.replace(PERIOD, SLASH);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530452 }
453
454 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530455 * Returns the YANG identifier name as java identifier with first letter
456 * in small.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530457 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530458 * @param yangIdentifier identifier in YANG file.
459 * @return corresponding java identifier
Bharat saraswal96dfef02016-06-16 00:29:12 +0530460 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530461 public static String getSmallCase(String yangIdentifier) {
462 return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530463 }
464
465 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530466 * Returns the YANG identifier name as java identifier with first letter
467 * in capital.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530468 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530469 * @param yangIdentifier identifier in YANG file
470 * @return corresponding java identifier
Bharat saraswal96dfef02016-06-16 00:29:12 +0530471 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530472 public static String getCapitalCase(String yangIdentifier) {
473 yangIdentifier = yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
474 return restrictConsecutiveCapitalCase(yangIdentifier);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530475 }
476
477 /**
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530478 * Restricts consecutive capital cased string as a rule in camel case.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530479 *
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530480 * @param consecCapitalCaseRemover which requires the restriction of consecutive capital case
481 * @return string without consecutive capital case
Bharat saraswal96dfef02016-06-16 00:29:12 +0530482 */
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530483 public static String restrictConsecutiveCapitalCase(String consecCapitalCaseRemover) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530484
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530485 for (int k = 0; k < consecCapitalCaseRemover.length(); k++) {
486 if (k + 1 < consecCapitalCaseRemover.length()) {
487 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k))) {
488 if (Character.isUpperCase(consecCapitalCaseRemover.charAt(k + 1))) {
489 consecCapitalCaseRemover = consecCapitalCaseRemover.substring(0, k + 1)
490 + consecCapitalCaseRemover.substring(k + 1, k + 2).toLowerCase()
491 + consecCapitalCaseRemover.substring(k + 2);
Bharat saraswal96dfef02016-06-16 00:29:12 +0530492 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530493 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530494 }
495 }
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530496 return consecCapitalCaseRemover;
Bharat saraswal96dfef02016-06-16 00:29:12 +0530497 }
498
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530499 /**
500 * Adds prefix, if the string begins with digit or is a java key word.
501 *
502 * @param camelCasePrefix string for adding prefix
503 * @param conflictResolver object of YANG to java naming conflict util
504 * @return prefixed camel case string
505 */
506 public static String addPrefix(String camelCasePrefix, YangToJavaNamingConflictUtil conflictResolver) {
507
508 String prefix = getPrefixForIdentifier(conflictResolver);
509 if (camelCasePrefix.matches(REGEX_FOR_FIRST_DIGIT)) {
510 camelCasePrefix = prefix + camelCasePrefix;
511 }
512 if (JAVA_KEY_WORDS.contains(camelCasePrefix)) {
513 camelCasePrefix = prefix + camelCasePrefix.substring(0, 1).toUpperCase()
514 + camelCasePrefix.substring(1);
515 }
516 return camelCasePrefix;
517 }
518
519 /**
520 * Applies the rule that a string does not end with a capitalized letter and capitalizes
521 * the letter next to a number in an array.
522 *
523 * @param stringArray containing strings for camel case separation
524 * @param conflictResolver object of YANG to java naming conflict util
525 * @return camel case rule checked string
526 */
527 public static String applyCamelCaseRule(String[] stringArray, YangToJavaNamingConflictUtil conflictResolver) {
528
529 String ruleChecker = stringArray[0].toLowerCase();
530 int i;
531 if (ruleChecker.matches(REGEX_FOR_FIRST_DIGIT)) {
532 i = 0;
533 ruleChecker = EMPTY_STRING;
534 } else {
535 i = 1;
536 }
537 for (; i < stringArray.length; i++) {
538 if (i + 1 == stringArray.length) {
539 if (stringArray[i].matches(REGEX_FOR_SINGLE_LETTER)
540 || stringArray[i].matches(REGEX_FOR_DIGITS_WITH_SINGLE_LETTER)) {
541 ruleChecker = ruleChecker + stringArray[i].toLowerCase();
542 break;
543 }
544 }
545 if (stringArray[i].matches(REGEX_FOR_FIRST_DIGIT)) {
546 for (int j = 0; j < stringArray[i].length(); j++) {
547 char letterCheck = stringArray[i].charAt(j);
548 if (Character.isLetter(letterCheck)) {
549 stringArray[i] = stringArray[i].substring(0, j)
550 + stringArray[i].substring(j, j + 1).toUpperCase() + stringArray[i].substring(j + 1);
551 break;
552 }
553 }
554 ruleChecker = ruleChecker + stringArray[i];
555 } else {
556 ruleChecker = ruleChecker + stringArray[i].substring(0, 1).toUpperCase() + stringArray[i].substring(1);
557 }
558 }
559 String ruleCheckerWithPrefix = addPrefix(ruleChecker, conflictResolver);
560 return restrictConsecutiveCapitalCase(ruleCheckerWithPrefix);
561 }
562
563 /**
564 * Resolves the conflict when input has upper case.
565 *
566 * @param stringArray containing strings for upper case conflict resolver
567 * @param conflictResolver object of YANG to java naming conflict util
568 * @return camel cased string
569 */
570 public static String upperCaseConflictResolver(String[] stringArray,
571 YangToJavaNamingConflictUtil conflictResolver) {
572
573 for (int l = 0; l < stringArray.length; l++) {
574 String[] upperCaseSplitArray = stringArray[l].split(REGEX_WITH_UPPERCASE);
575 for (int m = 0; m < upperCaseSplitArray.length; m++) {
576 if (upperCaseSplitArray[m].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
577 int check = m;
578 while (check + 1 < upperCaseSplitArray.length) {
579 if (upperCaseSplitArray[check + 1].matches(REGEX_WITH_SINGLE_CAPITAL_CASE)) {
580 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
581 check = check + 1;
582 } else if (upperCaseSplitArray[check + 1]
583 .matches(REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES)) {
584 upperCaseSplitArray[check + 1] = upperCaseSplitArray[check + 1].toLowerCase();
585 break;
586 } else {
587 break;
588 }
589 }
590 }
591 }
592 StringBuilder strBuilder = new StringBuilder();
593 for (String element : upperCaseSplitArray) {
594 strBuilder.append(element);
595 }
596 stringArray[l] = strBuilder.toString();
597 }
598 List<String> result = new ArrayList<String>();
599 for (String element : stringArray) {
600 String[] capitalCaseSplitArray = element.split(REGEX_WITH_UPPERCASE);
601 for (String letter : capitalCaseSplitArray) {
602 String[] arrayForAddition = letter.split(REGEX_WITH_DIGITS);
603 List<String> list = Arrays.asList(arrayForAddition);
604 for (String str : list) {
605 if (str != null && !str.isEmpty()) {
606 result.add(str);
607 }
608 }
609 }
610 }
611 stringArray = result.toArray(new String[result.size()]);
612 return applyCamelCaseRule(stringArray, conflictResolver);
613 }
614
615 /**
616 * Returns the YANG identifier name as java identifier.
617 *
618 * @param yangIdentifier identifier in YANG file
619 * @param conflictResolver object of YANG to java naming conflict util
620 * @return corresponding java identifier
621 */
622 public static String getCamelCase(String yangIdentifier, YangToJavaNamingConflictUtil conflictResolver) {
623
624 if (conflictResolver != null) {
625 String replacementForHyphen = conflictResolver.getReplacementForHyphen();
626 String replacementForPeriod = conflictResolver.getReplacementForPeriod();
627 String replacementForUnderscore = conflictResolver.getReplacementForUnderscore();
628 if (replacementForPeriod != null) {
629 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_PERIOD,
630 PERIOD + replacementForPeriod.toLowerCase() + PERIOD);
631 }
632 if (replacementForUnderscore != null) {
633 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_UNDERSCORE,
634 UNDER_SCORE + replacementForUnderscore.toLowerCase() + UNDER_SCORE);
635 }
636 if (replacementForHyphen != null) {
637 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_HYPHEN,
638 HYPHEN + replacementForHyphen.toLowerCase() + HYPHEN);
639 }
640 }
641 yangIdentifier = yangIdentifier.replaceAll(REGEX_FOR_IDENTIFIER_SPECIAL_CHAR, COLAN);
642 String[] strArray = yangIdentifier.split(COLAN);
643 if (strArray[0].isEmpty()) {
644 List<String> stringArrangement = new ArrayList<String>();
645 for (int i = 1; i < strArray.length; i++) {
646 stringArrangement.add(strArray[i]);
647 }
648 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
649 }
650 return upperCaseConflictResolver(strArray, conflictResolver);
651 }
652
653 /**
654 * Prefix for adding with identifier and namespace, when it is a java keyword or starting with digits.
655 *
656 * @param conflictResolver object of YANG to java naming conflict util
657 * @return prefix which needs to be added
658 */
659 public static String getPrefixForIdentifier(YangToJavaNamingConflictUtil conflictResolver) {
660
661 String prefixForIdentifier = null;
662 if (conflictResolver != null) {
663 prefixForIdentifier = conflictResolver.getPrefixForIdentifier();
664 }
665 if (prefixForIdentifier != null) {
666 prefixForIdentifier = prefixForIdentifier.replaceAll(REGEX_WITH_ALL_SPECIAL_CHAR, COLAN);
667 String[] strArray = prefixForIdentifier.split(COLAN);
668 try {
669 if (strArray[0].isEmpty()) {
670 List<String> stringArrangement = new ArrayList<String>();
671 for (int i = 1; i < strArray.length; i++) {
672 stringArrangement.add(strArray[i]);
673 }
674 strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
675 }
676 prefixForIdentifier = strArray[0];
677 for (int j = 1; j < strArray.length; j++) {
678 prefixForIdentifier = prefixForIdentifier + strArray[j].substring(0, 1).toUpperCase() +
679 strArray[j].substring(1);
680 }
681 } catch (ArrayIndexOutOfBoundsException outOfBoundsException) {
682 throw new TranslatorException("The given prefix in pom.xml is invalid.");
683 }
684 } else {
685 prefixForIdentifier = YANG_AUTO_PREFIX;
686 }
687 return prefixForIdentifier;
688 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530689}