blob: b505111f6b5084af881c559c064e20ce18310dc7 [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.nio.file.Files;
26import java.nio.file.StandardCopyOption;
27import java.util.ArrayList;
Vidyashree Rama1db15562016-05-17 16:16:15 +053028import java.util.Iterator;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053029import java.util.LinkedList;
Bharat saraswal33dfa012016-05-17 19:59:16 +053030import java.util.List;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053031import java.util.Set;
Bharat saraswal33dfa012016-05-17 19:59:16 +053032import java.util.Stack;
33import java.util.regex.Pattern;
Bharat saraswal870c56f2016-02-20 21:57:16 +053034
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053035import org.apache.commons.io.FileUtils;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053036import org.apache.maven.model.Resource;
37import org.apache.maven.project.MavenProject;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053038import org.onosproject.yangutils.plugin.manager.YangFileInfo;
Bharat saraswal33dfa012016-05-17 19:59:16 +053039import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053040import org.slf4j.Logger;
41import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053042
Bharat saraswale2d51d62016-03-23 19:40:35 +053043import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
44import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053045import static org.onosproject.yangutils.utils.UtilConstants.HASH;
Bharat saraswale2d51d62016-03-23 19:40:35 +053046import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053047import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053048import static org.onosproject.yangutils.utils.UtilConstants.ORG;
49import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053050import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswale2d51d62016-03-23 19:40:35 +053051import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
52import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
53import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
54import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
55import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
56import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
57import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
58import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
Bharat saraswald72411a2016-04-19 01:00:16 +053059import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswale2d51d62016-03-23 19:40:35 +053060import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053061import static org.slf4j.LoggerFactory.getLogger;
62
Bharat saraswal870c56f2016-02-20 21:57:16 +053063/**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053065 */
66public final class YangIoUtils {
67
68 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053069 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
Bharat saraswal33dfa012016-05-17 19:59:16 +053070 private static final int LINE_SIZE = 116;
71 private static final int SUB_LINE_SIZE = 112;
72 private static final int ZERO = 0;
Bharat saraswal870c56f2016-02-20 21:57:16 +053073
74 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053075 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053076 */
77 private YangIoUtils() {
78 }
79
80 /**
81 * Creates the directory structure.
82 *
83 * @param path directory path
84 * @return directory structure
85 */
86 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +053087 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053088 generatedDir.mkdirs();
89 return generatedDir;
90 }
91
92 /**
93 * Adds package info file for the created directory.
94 *
Bharat saraswal33dfa012016-05-17 19:59:16 +053095 * @param path directory path
96 * @param classInfo class info for the package
97 * @param pack package of the directory
Bharat saraswalc0e04842016-05-12 13:16:57 +053098 * @param isChildNode is it a child node
Bharat saraswal33dfa012016-05-17 19:59:16 +053099 * @param pluginConfig plugin configurations
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530100 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530102 public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode,
103 YangPluginConfig pluginConfig)
Bharat saraswalc0e04842016-05-12 13:16:57 +0530104 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530105
Bharat saraswal33dfa012016-05-17 19:59:16 +0530106 pack = parsePkg(pack);
107
Bharat saraswal870c56f2016-02-20 21:57:16 +0530108 try {
109
Bharat saraswale2d51d62016-03-23 19:40:35 +0530110 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530111 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530112
113 FileWriter fileWriter = new FileWriter(packageInfo);
114 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
115
b.janani68c55e12016-02-24 12:23:03 +0530116 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530117 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode, pluginConfig));
118 String pkg = PACKAGE + SPACE + pack + SEMI_COLAN;
119 if (pkg.length() > LINE_SIZE) {
120 pkg = whenDelimiterIsPersent(pkg, LINE_SIZE);
121 }
122 bufferedWriter.write(pkg);
b.janani68c55e12016-02-24 12:23:03 +0530123 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530124 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 } catch (IOException e) {
126 throw new IOException("Exception occured while creating package info file.");
127 }
128 }
129
130 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530131 * Parses package and returns updated package.
132 *
133 * @param pack package needs to be updated
134 * @return updated package
135 */
136 public static String parsePkg(String pack) {
137
138 if (pack.contains(ORG)) {
139 String[] strArray = pack.split(ORG);
140 if (strArray.length >= 3) {
141 for (int i = 1; i < strArray.length; i++) {
142 if (i == 1) {
143 pack = ORG + strArray[1];
144 } else {
145 pack = pack + ORG + strArray[i];
146 }
147 }
148 } else {
149 pack = ORG + strArray[1];
150 }
151 }
152
153 return pack;
154 }
155
156 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530157 * Cleans the generated directory if already exist in source folder.
158 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530159 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530160 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530161 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530162 public static void deleteDirectory(String dir) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530163 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530164 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530165 try {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530167 } catch (IOException e) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530168 throw new IOException(
169 "Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530170 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530171 }
172 }
173
174 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530175 * Searches and deletes generated temporary directories.
176 *
177 * @param root root directory
178 * @throws IOException when fails to do IO operations.
179 */
180 public static void searchAndDeleteTempDir(String root) throws IOException {
181 List<File> store = new LinkedList<>();
182 Stack<String> stack = new Stack<>();
183 stack.push(root);
184
185 while (!stack.empty()) {
186 root = stack.pop();
187 File file = new File(root);
188 File[] filelist = file.listFiles();
189 if (filelist == null || filelist.length == 0) {
190 continue;
191 }
192 for (File current : filelist) {
193 if (current.isDirectory()) {
194 stack.push(current.toString());
195 if (current.getName().endsWith("-Temp")) {
196 store.add(current);
197 }
198 }
199 }
200 }
201
202 for (File dir : store) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530203 FileUtils.deleteDirectory(dir);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530204 }
205 }
206
207 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530208 * Adds generated source directory to the compilation root.
209 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530210 * @param source directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530211 * @param project current maven project
212 * @param context current build context
213 */
214 public static void addToSource(String source, MavenProject project, BuildContext context) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530215 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530216 context.refresh(project.getBasedir());
217 log.info("Source directory added to compilation root: " + source);
218 }
219
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530220 /**
221 * Removes extra char from the string.
222 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530223 * @param valueString string to be trimmed
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530224 * @param removealStirng extra chars
225 * @return new string
226 */
227 public static String trimAtLast(String valueString, String removealStirng) {
228 StringBuilder stringBuilder = new StringBuilder(valueString);
229 int index = valueString.lastIndexOf(removealStirng);
230 stringBuilder.deleteCharAt(index);
231 return stringBuilder.toString();
232 }
233
234 /**
235 * Returns new parted string.
236 *
237 * @param partString string to be parted
238 * @return parted string
239 */
240 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530241 String[] strArray = partString.split(COMMA);
242 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530243 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530244 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530245 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530246 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530247 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
248 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530249 }
250 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530251 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530252 }
253
Vinod Kumar S38046502016-03-23 15:30:27 +0530254 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530255 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530256 *
257 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswal33dfa012016-05-17 19:59:16 +0530258 * put
259 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530260 * @return absolute path of the package in canonical form
261 */
262 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
263
264 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530265 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530266 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530267 String[] strArray = pathOfJavaPkg.split(SLASH);
268 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 return pathOfJavaPkg;
270 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530271 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530272 }
273 }
274
275 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530276 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530277 *
278 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswal33dfa012016-05-17 19:59:16 +0530279 * put
280 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530281 * @return absolute path of the package in canonical form
282 */
283 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530284 return baseCodeGenPath + pathOfJavaPkg;
285 }
286
287 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530288 * Copies YANG files to the current project's output directory.
Vinod Kumar S38046502016-03-23 15:30:27 +0530289 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530290 * @param yangFileInfo list of YANG files
291 * @param outputDir project's output directory
292 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530293 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530294 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530295 public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
Vinod Kumar S38046502016-03-23 15:30:27 +0530296 throws IOException {
297
Vidyashree Rama1db15562016-05-17 16:16:15 +0530298 List<File> files = getListOfFile(yangFileInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530299
300 String path = outputDir + TARGET_RESOURCE_PATH;
301 File targetDir = new File(path);
302 targetDir.mkdirs();
303
304 for (File file : files) {
305 Files.copy(file.toPath(),
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530306 new File(path + file.getName()).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530307 StandardCopyOption.REPLACE_EXISTING);
308 }
309 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530310 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530311 project.addResource(rsc);
312 }
313
314 /**
315 * Provides a list of files from list of strings.
316 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530317 * @param yangFileInfo set of yang file information
Vinod Kumar S38046502016-03-23 15:30:27 +0530318 * @return list of files
319 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530320 private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530321 List<File> files = new ArrayList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530322 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
323 while (yangFileIterator.hasNext()) {
324 YangFileInfo yangFile = yangFileIterator.next();
325 files.add(new File(yangFile.getYangFileName()));
Vinod Kumar S38046502016-03-23 15:30:27 +0530326 }
327 return files;
328 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530329
330 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530331 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530332 *
333 * @param appendFile temp file
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530334 * @param srcFile main file
Bharat saraswale2d51d62016-03-23 19:40:35 +0530335 * @throws IOException when fails to append contents
336 */
337 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530338 try {
339 appendFileContents(appendFile, srcFile);
340 } catch (IOException e) {
341 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
342 }
343 }
344
345 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530346 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530347 *
348 * @param file file in which need to be inserted
349 * @param data data which need to be inserted
350 * @throws IOException when fails to insert into file
351 */
352 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530353 try {
354 updateFileHandle(file, data, false);
355 } catch (IOException e) {
356 throw new IOException("Failed to insert in " + file + "file");
357 }
358 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530359
360 /**
361 * Validates a line size in given file whether it is having more then 120 characters.
362 * If yes it will update and give a new file.
363 *
364 * @param dataFile file in which need to verify all lines.
365 * @return updated file
366 * @throws IOException when fails to do IO operations.
367 */
368 public static File validateLineLength(File dataFile) throws IOException {
369 File tempFile = dataFile;
370 FileReader fileReader = new FileReader(dataFile);
371 BufferedReader bufferReader = new BufferedReader(fileReader);
372 try {
373 StringBuilder stringBuilder = new StringBuilder();
374 String line = bufferReader.readLine();
375
376 while (line != null) {
377 if (line.length() > LINE_SIZE) {
378 if (line.contains(PERIOD)) {
379 line = whenDelimiterIsPersent(line, LINE_SIZE);
380 } else if (line.contains(SPACE)) {
381 line = whenSpaceIsPresent(line, LINE_SIZE);
382 }
383 stringBuilder.append(line);
384 } else {
385 stringBuilder.append(line + NEW_LINE);
386 }
387 line = bufferReader.readLine();
388 }
389 FileWriter writer = new FileWriter(tempFile);
390 writer.write(stringBuilder.toString());
391 writer.close();
392 return tempFile;
393 } finally {
394 fileReader.close();
395 bufferReader.close();
396 }
397 }
398
399 /*When delimiters are present in the given line.*/
400 private static String whenDelimiterIsPersent(String line, int lineSize) {
401 StringBuilder stringBuilder = new StringBuilder();
402
403 if (line.length() > lineSize) {
404 String[] strArray = line.split(Pattern.quote(PERIOD));
405 stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize);
406 } else {
407 stringBuilder.append(line + NEW_LINE);
408 }
409 String[] strArray = stringBuilder.toString().split(NEW_LINE);
410 StringBuilder tempBuilder = new StringBuilder();
411 for (String str : strArray) {
412 if (str.length() > SUB_LINE_SIZE) {
413 if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) {
414 String[] strArr = str.split(Pattern.quote(PERIOD));
415 tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE);
416 } else if (str.contains(SPACE)) {
417 tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE));
418 }
419 } else {
420 tempBuilder.append(str + NEW_LINE);
421 }
422 }
423 return tempBuilder.toString();
424
425 }
426
427 /*When spaces are present in the given line.*/
428 private static String whenSpaceIsPresent(String line, int lineSize) {
429 StringBuilder stringBuilder = new StringBuilder();
430 if (line.length() > lineSize) {
431 String[] strArray = line.split(SPACE);
432 stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize);
433 } else {
434 stringBuilder.append(line + NEW_LINE);
435 }
436 return stringBuilder.toString();
437 }
438
439 /*Updates the given line with the given size conditions.*/
440 private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string,
441 int lineSize) {
442
443 StringBuilder tempBuilder = new StringBuilder();
444 for (String str : strArray) {
445 tempBuilder.append(str + string);
446 if (tempBuilder.length() > lineSize) {
447 String tempString = stringBuilder.toString();
448 stringBuilder.delete(ZERO, stringBuilder.length());
449 tempString = trimAtLast(tempString, string);
450 stringBuilder.append(tempString);
451 if (string.equals(PERIOD)) {
452 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string);
453 } else {
454 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string);
455 }
456 tempBuilder.delete(ZERO, tempBuilder.length());
457 tempBuilder.append(TWELVE_SPACE_INDENTATION);
458 } else {
459 stringBuilder.append(str + string);
460 }
461 }
462 String tempString = stringBuilder.toString();
463 tempString = trimAtLast(tempString, string);
464 stringBuilder.delete(ZERO, stringBuilder.length());
465 stringBuilder.append(tempString + NEW_LINE);
466 return stringBuilder;
467 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530468}