Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.utils.io.impl; |
| 18 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 19 | import java.io.BufferedReader; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 20 | import java.io.BufferedWriter; |
| 21 | import java.io.File; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 22 | import java.io.FileReader; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 23 | import java.io.FileWriter; |
| 24 | import java.io.IOException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 25 | import java.nio.file.Files; |
| 26 | import java.nio.file.StandardCopyOption; |
| 27 | import java.util.ArrayList; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 28 | import java.util.Iterator; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 29 | import java.util.LinkedList; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 30 | import java.util.List; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 31 | import java.util.Set; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 32 | import java.util.Stack; |
| 33 | import java.util.regex.Pattern; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 34 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 35 | import org.apache.commons.io.FileUtils; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 36 | import org.apache.maven.model.Resource; |
| 37 | import org.apache.maven.project.MavenProject; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.sonatype.plexus.build.incremental.BuildContext; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 42 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.UtilConstants.HASH; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.ORG; |
| 49 | import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 52 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
| 54 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
| 55 | import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION; |
| 56 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES; |
| 57 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents; |
| 58 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 61 | import static org.slf4j.LoggerFactory.getLogger; |
| 62 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 63 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 64 | * Represents common utility functionalities for code generation. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 65 | */ |
| 66 | public final class YangIoUtils { |
| 67 | |
| 68 | private static final Logger log = getLogger(YangIoUtils.class); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 69 | private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 70 | private static final int LINE_SIZE = 116; |
| 71 | private static final int SUB_LINE_SIZE = 112; |
| 72 | private static final int ZERO = 0; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | |
| 74 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 75 | * Creates an instance of YANG io utils. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 76 | */ |
| 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.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 87 | File generatedDir = new File(path); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 88 | generatedDir.mkdirs(); |
| 89 | return generatedDir; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Adds package info file for the created directory. |
| 94 | * |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 95 | * @param path directory path |
| 96 | * @param classInfo class info for the package |
| 97 | * @param pack package of the directory |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 98 | * @param isChildNode is it a child node |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 99 | * @param pluginConfig plugin configurations |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 100 | * @throws IOException when fails to create package info file |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 101 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 102 | public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode, |
| 103 | YangPluginConfig pluginConfig) |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 104 | throws IOException { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 105 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 106 | pack = parsePkg(pack); |
| 107 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 108 | try { |
| 109 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 110 | File packageInfo = new File(path + SLASH + "package-info.java"); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 111 | packageInfo.createNewFile(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 112 | |
| 113 | FileWriter fileWriter = new FileWriter(packageInfo); |
| 114 | BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); |
| 115 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 116 | bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 117 | 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.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 123 | bufferedWriter.close(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 124 | fileWriter.close(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 125 | } catch (IOException e) { |
| 126 | throw new IOException("Exception occured while creating package info file."); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 131 | * 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 saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 157 | * Cleans the generated directory if already exist in source folder. |
| 158 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 159 | * @param dir generated directory in previous build |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 160 | * @throws IOException when failed to delete directory |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 161 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 162 | public static void deleteDirectory(String dir) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 163 | File generatedDirectory = new File(dir); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 164 | if (generatedDirectory.exists()) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 165 | try { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 166 | FileUtils.deleteDirectory(generatedDirectory); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 167 | } catch (IOException e) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 168 | throw new IOException( |
| 169 | "Failed to delete the generated files in " + generatedDirectory + " directory"); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 170 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 175 | * 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 saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 203 | FileUtils.deleteDirectory(dir); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 208 | * Adds generated source directory to the compilation root. |
| 209 | * |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 210 | * @param source directory |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 211 | * @param project current maven project |
| 212 | * @param context current build context |
| 213 | */ |
| 214 | public static void addToSource(String source, MavenProject project, BuildContext context) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 215 | project.addCompileSourceRoot(source); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 216 | context.refresh(project.getBasedir()); |
| 217 | log.info("Source directory added to compilation root: " + source); |
| 218 | } |
| 219 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 220 | /** |
| 221 | * Removes extra char from the string. |
| 222 | * |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 223 | * @param valueString string to be trimmed |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 224 | * @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 saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 241 | String[] strArray = partString.split(COMMA); |
| 242 | String newString = EMPTY_STRING; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 243 | for (int i = 0; i < strArray.length; i++) { |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 244 | if (i % 4 != 0 || i == 0) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 245 | newString = newString + strArray[i] + COMMA; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 246 | } else { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 247 | newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION |
| 248 | + strArray[i] + COMMA; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 249 | } |
| 250 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 251 | return trimAtLast(newString, COMMA); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 252 | } |
| 253 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 254 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 255 | * Returns the directory path of the package in canonical form. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 256 | * |
| 257 | * @param baseCodeGenPath base path where the generated files needs to be |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 258 | * put |
| 259 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 260 | * @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 saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 265 | pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 266 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 267 | String[] strArray = pathOfJavaPkg.split(SLASH); |
| 268 | if (strArray[0].equals(EMPTY_STRING)) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 269 | return pathOfJavaPkg; |
| 270 | } else { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 271 | return baseCodeGenPath + SLASH + pathOfJavaPkg; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 276 | * Returns the absolute path of the package in canonical form. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 277 | * |
| 278 | * @param baseCodeGenPath base path where the generated files needs to be |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 279 | * put |
| 280 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 281 | * @return absolute path of the package in canonical form |
| 282 | */ |
| 283 | public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 284 | return baseCodeGenPath + pathOfJavaPkg; |
| 285 | } |
| 286 | |
| 287 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 288 | * Copies YANG files to the current project's output directory. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 289 | * |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 290 | * @param yangFileInfo list of YANG files |
| 291 | * @param outputDir project's output directory |
| 292 | * @param project maven project |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 293 | * @throws IOException when fails to copy files to destination resource directory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 294 | */ |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 295 | public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project) |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 296 | throws IOException { |
| 297 | |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 298 | List<File> files = getListOfFile(yangFileInfo); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 299 | |
| 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 Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 306 | new File(path + file.getName()).toPath(), |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 307 | StandardCopyOption.REPLACE_EXISTING); |
| 308 | } |
| 309 | Resource rsc = new Resource(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 310 | rsc.setDirectory(outputDir + SLASH + TEMP + SLASH); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 311 | project.addResource(rsc); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Provides a list of files from list of strings. |
| 316 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 317 | * @param yangFileInfo set of yang file information |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 318 | * @return list of files |
| 319 | */ |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 320 | private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 321 | List<File> files = new ArrayList<>(); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 322 | Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator(); |
| 323 | while (yangFileIterator.hasNext()) { |
| 324 | YangFileInfo yangFile = yangFileIterator.next(); |
| 325 | files.add(new File(yangFile.getYangFileName())); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 326 | } |
| 327 | return files; |
| 328 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 329 | |
| 330 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 331 | * Merges the temp java files to main java files. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 332 | * |
| 333 | * @param appendFile temp file |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 334 | * @param srcFile main file |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 335 | * @throws IOException when fails to append contents |
| 336 | */ |
| 337 | public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 338 | try { |
| 339 | appendFileContents(appendFile, srcFile); |
| 340 | } catch (IOException e) { |
| 341 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 346 | * Inserts data in the generated file. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 347 | * |
| 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 saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 353 | try { |
| 354 | updateFileHandle(file, data, false); |
| 355 | } catch (IOException e) { |
| 356 | throw new IOException("Failed to insert in " + file + "file"); |
| 357 | } |
| 358 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 359 | |
| 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 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 436 | |
| 437 | String[] strArray = stringBuilder.toString().split(NEW_LINE); |
| 438 | StringBuilder tempBuilder = new StringBuilder(); |
| 439 | for (String str : strArray) { |
| 440 | if (str.length() > SUB_LINE_SIZE) { |
| 441 | if (str.contains(SPACE)) { |
| 442 | String[] strArr = str.split(SPACE); |
| 443 | tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE); |
| 444 | } |
| 445 | } else { |
| 446 | tempBuilder.append(str + NEW_LINE); |
| 447 | } |
| 448 | } |
| 449 | return tempBuilder.toString(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /*Updates the given line with the given size conditions.*/ |
| 453 | private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string, |
| 454 | int lineSize) { |
| 455 | |
| 456 | StringBuilder tempBuilder = new StringBuilder(); |
| 457 | for (String str : strArray) { |
| 458 | tempBuilder.append(str + string); |
| 459 | if (tempBuilder.length() > lineSize) { |
| 460 | String tempString = stringBuilder.toString(); |
| 461 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 462 | tempString = trimAtLast(tempString, string); |
| 463 | stringBuilder.append(tempString); |
| 464 | if (string.equals(PERIOD)) { |
| 465 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string); |
| 466 | } else { |
| 467 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string); |
| 468 | } |
| 469 | tempBuilder.delete(ZERO, tempBuilder.length()); |
| 470 | tempBuilder.append(TWELVE_SPACE_INDENTATION); |
| 471 | } else { |
| 472 | stringBuilder.append(str + string); |
| 473 | } |
| 474 | } |
| 475 | String tempString = stringBuilder.toString(); |
| 476 | tempString = trimAtLast(tempString, string); |
| 477 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 478 | stringBuilder.append(tempString + NEW_LINE); |
| 479 | return stringBuilder; |
| 480 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 481 | } |