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