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 | |
| 19 | import java.io.BufferedWriter; |
| 20 | import java.io.File; |
| 21 | import java.io.FileWriter; |
| 22 | import java.io.IOException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | import java.nio.file.Files; |
| 24 | import java.nio.file.StandardCopyOption; |
| 25 | import java.util.ArrayList; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 28 | import org.apache.maven.model.Resource; |
| 29 | import org.apache.maven.project.MavenProject; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.sonatype.plexus.build.incremental.BuildContext; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 32 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 33 | import static org.apache.commons.io.FileUtils.deleteDirectory; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.ORG; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 40 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES; |
| 45 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents; |
| 46 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 48 | 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] | 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 51 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 52 | * Represents common utility functionalities for code generation. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 53 | */ |
| 54 | public final class YangIoUtils { |
| 55 | |
| 56 | private static final Logger log = getLogger(YangIoUtils.class); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 57 | private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 58 | |
| 59 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 60 | * Creates an instance of YANG io utils. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 61 | */ |
| 62 | private YangIoUtils() { |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates the directory structure. |
| 67 | * |
| 68 | * @param path directory path |
| 69 | * @return directory structure |
| 70 | */ |
| 71 | public static File createDirectories(String path) { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 72 | File generatedDir = new File(path); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | generatedDir.mkdirs(); |
| 74 | return generatedDir; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Adds package info file for the created directory. |
| 79 | * |
| 80 | * @param path directory path |
| 81 | * @param classInfo class info for the package |
| 82 | * @param pack package of the directory |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 83 | * @throws IOException when fails to create package info file |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 84 | */ |
| 85 | public static void addPackageInfo(File path, String classInfo, String pack) throws IOException { |
| 86 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 87 | if (pack.contains(ORG)) { |
| 88 | String[] strArray = pack.split(ORG); |
| 89 | pack = ORG + strArray[1]; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 90 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 91 | try { |
| 92 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 93 | File packageInfo = new File(path + SLASH + "package-info.java"); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 94 | packageInfo.createNewFile(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 95 | |
| 96 | FileWriter fileWriter = new FileWriter(packageInfo); |
| 97 | BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); |
| 98 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 99 | bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 100 | bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, false)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 101 | bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN); |
| 102 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 103 | bufferedWriter.close(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 104 | fileWriter.close(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 105 | } catch (IOException e) { |
| 106 | throw new IOException("Exception occured while creating package info file."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Cleans the generated directory if already exist in source folder. |
| 112 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 113 | * @param dir generated directory in previous build |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 114 | * @throws IOException when failed to delete directory |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 115 | */ |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 116 | public static void clean(String dir) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 117 | File generatedDirectory = new File(dir); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 118 | if (generatedDirectory.exists()) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 119 | try { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 120 | deleteDirectory(generatedDirectory); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 121 | } catch (IOException e) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 122 | throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory"); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 123 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Adds generated source directory to the compilation root. |
| 129 | * |
| 130 | * @param source directory |
| 131 | * @param project current maven project |
| 132 | * @param context current build context |
| 133 | */ |
| 134 | public static void addToSource(String source, MavenProject project, BuildContext context) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 135 | project.addCompileSourceRoot(source); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 136 | context.refresh(project.getBasedir()); |
| 137 | log.info("Source directory added to compilation root: " + source); |
| 138 | } |
| 139 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 140 | /** |
| 141 | * Removes extra char from the string. |
| 142 | * |
| 143 | * @param valueString string to be trimmed |
| 144 | * @param removealStirng extra chars |
| 145 | * @return new string |
| 146 | */ |
| 147 | public static String trimAtLast(String valueString, String removealStirng) { |
| 148 | StringBuilder stringBuilder = new StringBuilder(valueString); |
| 149 | int index = valueString.lastIndexOf(removealStirng); |
| 150 | stringBuilder.deleteCharAt(index); |
| 151 | return stringBuilder.toString(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns new parted string. |
| 156 | * |
| 157 | * @param partString string to be parted |
| 158 | * @return parted string |
| 159 | */ |
| 160 | public static String partString(String partString) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 161 | String[] strArray = partString.split(COMMA); |
| 162 | String newString = EMPTY_STRING; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 163 | for (int i = 0; i < strArray.length; i++) { |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 164 | if (i % 4 != 0 || i == 0) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 165 | newString = newString + strArray[i] + COMMA; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 166 | } else { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 167 | newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION |
| 168 | + strArray[i] + COMMA; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 169 | } |
| 170 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 171 | return trimAtLast(newString, COMMA); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 172 | } |
| 173 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 174 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 175 | * Returns the directory path of the package in canonical form. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 176 | * |
| 177 | * @param baseCodeGenPath base path where the generated files needs to be |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 178 | * put |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 179 | * @param pathOfJavaPkg java package of the file being generated |
| 180 | * @return absolute path of the package in canonical form |
| 181 | */ |
| 182 | public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) { |
| 183 | |
| 184 | if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 185 | pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 186 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 187 | String[] strArray = pathOfJavaPkg.split(SLASH); |
| 188 | if (strArray[0].equals(EMPTY_STRING)) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 189 | return pathOfJavaPkg; |
| 190 | } else { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 191 | return baseCodeGenPath + SLASH + pathOfJavaPkg; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 196 | * Returns the absolute path of the package in canonical form. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 197 | * |
| 198 | * @param baseCodeGenPath base path where the generated files needs to be |
Bharat saraswal | c34b944 | 2016-03-28 15:50:13 +0530 | [diff] [blame] | 199 | * put |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 200 | * @param pathOfJavaPkg java package of the file being generated |
| 201 | * @return absolute path of the package in canonical form |
| 202 | */ |
| 203 | public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 204 | return baseCodeGenPath + pathOfJavaPkg; |
| 205 | } |
| 206 | |
| 207 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 208 | * Copies YANG files to the current project's output directory. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 209 | * |
| 210 | * @param yangFiles list of YANG files |
| 211 | * @param outputDir project's output directory |
| 212 | * @param project maven project |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 213 | * @throws IOException when fails to copy files to destination resource directory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 214 | */ |
| 215 | public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project) |
| 216 | throws IOException { |
| 217 | |
| 218 | List<File> files = getListOfFile(yangFiles); |
| 219 | |
| 220 | String path = outputDir + TARGET_RESOURCE_PATH; |
| 221 | File targetDir = new File(path); |
| 222 | targetDir.mkdirs(); |
| 223 | |
| 224 | for (File file : files) { |
| 225 | Files.copy(file.toPath(), |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame^] | 226 | new File(path + file.getName()).toPath(), |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 227 | StandardCopyOption.REPLACE_EXISTING); |
| 228 | } |
| 229 | Resource rsc = new Resource(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 230 | rsc.setDirectory(outputDir + SLASH + TEMP + SLASH); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 231 | project.addResource(rsc); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Provides a list of files from list of strings. |
| 236 | * |
| 237 | * @param strings list of strings |
| 238 | * @return list of files |
| 239 | */ |
| 240 | private static List<File> getListOfFile(List<String> strings) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 241 | List<File> files = new ArrayList<>(); |
| 242 | for (String file : strings) { |
| 243 | files.add(new File(file)); |
| 244 | } |
| 245 | return files; |
| 246 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 247 | |
| 248 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 249 | * Merges the temp java files to main java files. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 250 | * |
| 251 | * @param appendFile temp file |
| 252 | * @param srcFile main file |
| 253 | * @throws IOException when fails to append contents |
| 254 | */ |
| 255 | public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 256 | try { |
| 257 | appendFileContents(appendFile, srcFile); |
| 258 | } catch (IOException e) { |
| 259 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 264 | * Inserts data in the generated file. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 265 | * |
| 266 | * @param file file in which need to be inserted |
| 267 | * @param data data which need to be inserted |
| 268 | * @throws IOException when fails to insert into file |
| 269 | */ |
| 270 | public static void insertDataIntoJavaFile(File file, String data) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 271 | try { |
| 272 | updateFileHandle(file, data, false); |
| 273 | } catch (IOException e) { |
| 274 | throw new IOException("Failed to insert in " + file + "file"); |
| 275 | } |
| 276 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 277 | } |