Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 870c56f | 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 | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 19 | import java.io.BufferedReader; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 20 | import java.io.BufferedWriter; |
| 21 | import java.io.File; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 22 | import java.io.FileReader; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 23 | import java.io.FileWriter; |
| 24 | import java.io.IOException; |
Vinod Kumar S | 3804650 | 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 | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 28 | import java.util.Iterator; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 29 | import java.util.LinkedList; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 30 | import java.util.List; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 31 | import java.util.Set; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 32 | import java.util.Stack; |
| 33 | import java.util.regex.Pattern; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 34 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 35 | import org.apache.commons.io.FileUtils; |
Vinod Kumar S | c421600 | 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 | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.sonatype.plexus.build.incremental.BuildContext; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 42 | |
Bharat saraswal | e2d51d6 | 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 | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.UtilConstants.HASH; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
Bharat saraswal | e2d51d6 | 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 | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Bharat saraswal | e2d51d6 | 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; |
| 59 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 60 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 61 | import static org.slf4j.LoggerFactory.getLogger; |
| 62 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 63 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 64 | * Represents common utility functionalities for code generation. |
Bharat saraswal | 870c56f | 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 | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 69 | private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 70 | private static final int LINE_SIZE = 118; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 71 | private static final int SUB_LINE_SIZE = 112; |
| 72 | private static final int ZERO = 0; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | |
| 74 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 75 | * Creates an instance of YANG io utils. |
Bharat saraswal | 870c56f | 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 | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 87 | File generatedDir = new File(path); |
Bharat saraswal | 870c56f | 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 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 95 | * @param path directory path |
| 96 | * @param classInfo class info for the package |
| 97 | * @param pack package of the directory |
| 98 | * @param isChildNode is it a child node |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 99 | * @param pluginConfig plugin configurations |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 100 | * @throws IOException when fails to create package info file |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 101 | */ |
Bharat saraswal | 33dfa01 | 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 | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 104 | throws IOException { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 105 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 106 | pack = parsePkg(pack); |
| 107 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 108 | try { |
| 109 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 110 | File packageInfo = new File(path + SLASH + "package-info.java"); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 111 | packageInfo.createNewFile(); |
Bharat saraswal | e2d51d6 | 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 | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 116 | bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); |
Bharat saraswal | 33dfa01 | 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 | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 123 | bufferedWriter.close(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 124 | fileWriter.close(); |
Bharat saraswal | 870c56f | 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 | 33dfa01 | 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 | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 157 | * Cleans the generated directory if already exist in source folder. |
| 158 | * |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 159 | * @param dir generated directory in previous build |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 160 | * @throws IOException when failed to delete directory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 161 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 162 | public static void deleteDirectory(String dir) |
| 163 | throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 164 | File generatedDirectory = new File(dir); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 165 | if (generatedDirectory.exists()) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 166 | try { |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 167 | FileUtils.deleteDirectory(generatedDirectory); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 168 | } catch (IOException e) { |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 169 | throw new IOException( |
| 170 | "Failed to delete the generated files in " + generatedDirectory + " directory"); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 171 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 176 | * Searches and deletes generated temporary directories. |
| 177 | * |
| 178 | * @param root root directory |
| 179 | * @throws IOException when fails to do IO operations. |
| 180 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 181 | public static void searchAndDeleteTempDir(String root) |
| 182 | throws IOException { |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 183 | List<File> store = new LinkedList<>(); |
| 184 | Stack<String> stack = new Stack<>(); |
| 185 | stack.push(root); |
| 186 | |
| 187 | while (!stack.empty()) { |
| 188 | root = stack.pop(); |
| 189 | File file = new File(root); |
| 190 | File[] filelist = file.listFiles(); |
| 191 | if (filelist == null || filelist.length == 0) { |
| 192 | continue; |
| 193 | } |
| 194 | for (File current : filelist) { |
| 195 | if (current.isDirectory()) { |
| 196 | stack.push(current.toString()); |
| 197 | if (current.getName().endsWith("-Temp")) { |
| 198 | store.add(current); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | for (File dir : store) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 205 | FileUtils.deleteDirectory(dir); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 210 | * Adds generated source directory to the compilation root. |
| 211 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 212 | * @param source directory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 213 | * @param project current maven project |
| 214 | * @param context current build context |
| 215 | */ |
| 216 | public static void addToSource(String source, MavenProject project, BuildContext context) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 217 | project.addCompileSourceRoot(source); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 218 | context.refresh(project.getBasedir()); |
| 219 | log.info("Source directory added to compilation root: " + source); |
| 220 | } |
| 221 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 222 | /** |
| 223 | * Removes extra char from the string. |
| 224 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 225 | * @param valueString string to be trimmed |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 226 | * @param removealStirng extra chars |
| 227 | * @return new string |
| 228 | */ |
| 229 | public static String trimAtLast(String valueString, String removealStirng) { |
| 230 | StringBuilder stringBuilder = new StringBuilder(valueString); |
| 231 | int index = valueString.lastIndexOf(removealStirng); |
| 232 | stringBuilder.deleteCharAt(index); |
| 233 | return stringBuilder.toString(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns new parted string. |
| 238 | * |
| 239 | * @param partString string to be parted |
| 240 | * @return parted string |
| 241 | */ |
| 242 | public static String partString(String partString) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 243 | String[] strArray = partString.split(COMMA); |
| 244 | String newString = EMPTY_STRING; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 245 | for (int i = 0; i < strArray.length; i++) { |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 246 | if (i % 4 != 0 || i == 0) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 247 | newString = newString + strArray[i] + COMMA; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 248 | } else { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 249 | newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION |
| 250 | + strArray[i] + COMMA; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 251 | } |
| 252 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 253 | return trimAtLast(newString, COMMA); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 254 | } |
| 255 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 256 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 257 | * Returns the directory path of the package in canonical form. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 258 | * |
| 259 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 260 | * put |
| 261 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 262 | * @return absolute path of the package in canonical form |
| 263 | */ |
| 264 | public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) { |
| 265 | |
| 266 | if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 267 | pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 268 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 269 | String[] strArray = pathOfJavaPkg.split(SLASH); |
| 270 | if (strArray[0].equals(EMPTY_STRING)) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 271 | return pathOfJavaPkg; |
| 272 | } else { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 273 | return baseCodeGenPath + SLASH + pathOfJavaPkg; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 278 | * Returns the absolute path of the package in canonical form. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 279 | * |
| 280 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 281 | * put |
| 282 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 283 | * @return absolute path of the package in canonical form |
| 284 | */ |
| 285 | public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 286 | return baseCodeGenPath + pathOfJavaPkg; |
| 287 | } |
| 288 | |
| 289 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 290 | * Copies YANG files to the current project's output directory. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 291 | * |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 292 | * @param yangFileInfo list of YANG files |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 293 | * @param outputDir project's output directory |
| 294 | * @param project maven project |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 295 | * @throws IOException when fails to copy files to destination resource directory |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 296 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 297 | public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project) |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 298 | throws IOException { |
| 299 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 300 | List<File> files = getListOfFile(yangFileInfo); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 301 | |
| 302 | String path = outputDir + TARGET_RESOURCE_PATH; |
| 303 | File targetDir = new File(path); |
| 304 | targetDir.mkdirs(); |
| 305 | |
| 306 | for (File file : files) { |
| 307 | Files.copy(file.toPath(), |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 308 | new File(path + file.getName()).toPath(), |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 309 | StandardCopyOption.REPLACE_EXISTING); |
| 310 | } |
| 311 | Resource rsc = new Resource(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 312 | rsc.setDirectory(outputDir + SLASH + TEMP + SLASH); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 313 | project.addResource(rsc); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Provides a list of files from list of strings. |
| 318 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 319 | * @param yangFileInfo set of yang file information |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 320 | * @return list of files |
| 321 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 322 | private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 323 | List<File> files = new ArrayList<>(); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 324 | Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator(); |
| 325 | while (yangFileIterator.hasNext()) { |
| 326 | YangFileInfo yangFile = yangFileIterator.next(); |
| 327 | files.add(new File(yangFile.getYangFileName())); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 328 | } |
| 329 | return files; |
| 330 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 331 | |
| 332 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 333 | * Merges the temp java files to main java files. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 334 | * |
| 335 | * @param appendFile temp file |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 336 | * @param srcFile main file |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 337 | * @throws IOException when fails to append contents |
| 338 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 339 | public static void mergeJavaFiles(File appendFile, File srcFile) |
| 340 | throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 341 | try { |
| 342 | appendFileContents(appendFile, srcFile); |
| 343 | } catch (IOException e) { |
| 344 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 349 | * Inserts data in the generated file. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 350 | * |
| 351 | * @param file file in which need to be inserted |
| 352 | * @param data data which need to be inserted |
| 353 | * @throws IOException when fails to insert into file |
| 354 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 355 | public static void insertDataIntoJavaFile(File file, String data) |
| 356 | throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 357 | try { |
| 358 | updateFileHandle(file, data, false); |
| 359 | } catch (IOException e) { |
| 360 | throw new IOException("Failed to insert in " + file + "file"); |
| 361 | } |
| 362 | } |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * Validates a line size in given file whether it is having more then 120 characters. |
| 366 | * If yes it will update and give a new file. |
| 367 | * |
| 368 | * @param dataFile file in which need to verify all lines. |
| 369 | * @return updated file |
| 370 | * @throws IOException when fails to do IO operations. |
| 371 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 372 | public static File validateLineLength(File dataFile) |
| 373 | throws IOException { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 374 | File tempFile = dataFile; |
| 375 | FileReader fileReader = new FileReader(dataFile); |
| 376 | BufferedReader bufferReader = new BufferedReader(fileReader); |
| 377 | try { |
| 378 | StringBuilder stringBuilder = new StringBuilder(); |
| 379 | String line = bufferReader.readLine(); |
| 380 | |
| 381 | while (line != null) { |
| 382 | if (line.length() > LINE_SIZE) { |
| 383 | if (line.contains(PERIOD)) { |
| 384 | line = whenDelimiterIsPersent(line, LINE_SIZE); |
| 385 | } else if (line.contains(SPACE)) { |
| 386 | line = whenSpaceIsPresent(line, LINE_SIZE); |
| 387 | } |
| 388 | stringBuilder.append(line); |
| 389 | } else { |
| 390 | stringBuilder.append(line + NEW_LINE); |
| 391 | } |
| 392 | line = bufferReader.readLine(); |
| 393 | } |
| 394 | FileWriter writer = new FileWriter(tempFile); |
| 395 | writer.write(stringBuilder.toString()); |
| 396 | writer.close(); |
| 397 | return tempFile; |
| 398 | } finally { |
| 399 | fileReader.close(); |
| 400 | bufferReader.close(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /*When delimiters are present in the given line.*/ |
| 405 | private static String whenDelimiterIsPersent(String line, int lineSize) { |
| 406 | StringBuilder stringBuilder = new StringBuilder(); |
| 407 | |
| 408 | if (line.length() > lineSize) { |
| 409 | String[] strArray = line.split(Pattern.quote(PERIOD)); |
| 410 | stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize); |
| 411 | } else { |
| 412 | stringBuilder.append(line + NEW_LINE); |
| 413 | } |
| 414 | String[] strArray = stringBuilder.toString().split(NEW_LINE); |
| 415 | StringBuilder tempBuilder = new StringBuilder(); |
| 416 | for (String str : strArray) { |
| 417 | if (str.length() > SUB_LINE_SIZE) { |
| 418 | if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) { |
| 419 | String[] strArr = str.split(Pattern.quote(PERIOD)); |
| 420 | tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE); |
| 421 | } else if (str.contains(SPACE)) { |
| 422 | tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE)); |
| 423 | } |
| 424 | } else { |
| 425 | tempBuilder.append(str + NEW_LINE); |
| 426 | } |
| 427 | } |
| 428 | return tempBuilder.toString(); |
| 429 | |
| 430 | } |
| 431 | |
| 432 | /*When spaces are present in the given line.*/ |
| 433 | private static String whenSpaceIsPresent(String line, int lineSize) { |
| 434 | StringBuilder stringBuilder = new StringBuilder(); |
| 435 | if (line.length() > lineSize) { |
| 436 | String[] strArray = line.split(SPACE); |
| 437 | stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize); |
| 438 | } else { |
| 439 | stringBuilder.append(line + NEW_LINE); |
| 440 | } |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 441 | |
| 442 | String[] strArray = stringBuilder.toString().split(NEW_LINE); |
| 443 | StringBuilder tempBuilder = new StringBuilder(); |
| 444 | for (String str : strArray) { |
| 445 | if (str.length() > SUB_LINE_SIZE) { |
| 446 | if (str.contains(SPACE)) { |
| 447 | String[] strArr = str.split(SPACE); |
| 448 | tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE); |
| 449 | } |
| 450 | } else { |
| 451 | tempBuilder.append(str + NEW_LINE); |
| 452 | } |
| 453 | } |
| 454 | return tempBuilder.toString(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /*Updates the given line with the given size conditions.*/ |
| 458 | private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string, |
| 459 | int lineSize) { |
| 460 | |
| 461 | StringBuilder tempBuilder = new StringBuilder(); |
| 462 | for (String str : strArray) { |
| 463 | tempBuilder.append(str + string); |
| 464 | if (tempBuilder.length() > lineSize) { |
| 465 | String tempString = stringBuilder.toString(); |
| 466 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 467 | tempString = trimAtLast(tempString, string); |
| 468 | stringBuilder.append(tempString); |
| 469 | if (string.equals(PERIOD)) { |
| 470 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string); |
| 471 | } else { |
| 472 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string); |
| 473 | } |
| 474 | tempBuilder.delete(ZERO, tempBuilder.length()); |
| 475 | tempBuilder.append(TWELVE_SPACE_INDENTATION); |
| 476 | } else { |
| 477 | stringBuilder.append(str + string); |
| 478 | } |
| 479 | } |
| 480 | String tempString = stringBuilder.toString(); |
| 481 | tempString = trimAtLast(tempString, string); |
| 482 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 483 | stringBuilder.append(tempString + NEW_LINE); |
| 484 | return stringBuilder; |
| 485 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 486 | } |