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 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 21 | import java.io.BufferedWriter; |
| 22 | import java.io.File; |
| 23 | import java.io.FileWriter; |
| 24 | import java.io.IOException; |
| 25 | import java.util.List; |
| 26 | |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 27 | import org.apache.commons.io.FileUtils; |
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; |
| 30 | import org.onosproject.yangutils.utils.UtilConstants; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.sonatype.plexus.build.incremental.BuildContext; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 33 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 34 | /** |
| 35 | * Provides common utility functionalities for code generation. |
| 36 | */ |
| 37 | public final class YangIoUtils { |
| 38 | |
| 39 | private static final Logger log = getLogger(YangIoUtils.class); |
| 40 | |
| 41 | /** |
| 42 | * Default constructor. |
| 43 | */ |
| 44 | private YangIoUtils() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates the directory structure. |
| 49 | * |
| 50 | * @param path directory path |
| 51 | * @return directory structure |
| 52 | */ |
| 53 | public static File createDirectories(String path) { |
| 54 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 55 | File generatedDir = new File(path); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 56 | generatedDir.mkdirs(); |
| 57 | return generatedDir; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Adds package info file for the created directory. |
| 62 | * |
| 63 | * @param path directory path |
| 64 | * @param classInfo class info for the package |
| 65 | * @param pack package of the directory |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 66 | * @throws IOException when fails to create package info file |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 67 | */ |
| 68 | public static void addPackageInfo(File path, String classInfo, String pack) throws IOException { |
| 69 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 70 | if (pack.contains(UtilConstants.YANG_GEN_DIR)) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 71 | String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR); |
| 72 | pack = strArray[1]; |
| 73 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 74 | try { |
| 75 | |
| 76 | File packageInfo = new File(path + File.separator + "package-info.java"); |
| 77 | packageInfo.createNewFile(); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 78 | FileWriter fileWriter = null; |
| 79 | BufferedWriter bufferedWriter = null; |
| 80 | fileWriter = new FileWriter(packageInfo); |
| 81 | bufferedWriter = new BufferedWriter(fileWriter); |
| 82 | bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 83 | bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo, false)); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 84 | bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN); |
| 85 | bufferedWriter.close(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 86 | } catch (IOException e) { |
| 87 | throw new IOException("Exception occured while creating package info file."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Cleans the generated directory if already exist in source folder. |
| 93 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 94 | * @param baseDir generated directory in previous build |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 95 | */ |
| 96 | public static void clean(String baseDir) { |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 97 | File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR |
| 98 | + UtilConstants.DEFAULT_BASE_PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH)); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 99 | if (generatedDirectory.exists()) { |
| 100 | List<String> javafiles; |
| 101 | try { |
| 102 | javafiles = YangFileScanner.getJavaFiles(generatedDirectory.toString()); |
| 103 | for (String file : javafiles) { |
| 104 | File currentFile = new File(file); |
| 105 | currentFile.delete(); |
| 106 | } |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 107 | FileUtils.deleteDirectory(generatedDirectory); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 108 | } catch (IOException e) { |
| 109 | log.info("Failed to delete the generated files in " + generatedDirectory + " directory"); |
| 110 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Adds generated source directory to the compilation root. |
| 116 | * |
| 117 | * @param source directory |
| 118 | * @param project current maven project |
| 119 | * @param context current build context |
| 120 | */ |
| 121 | public static void addToSource(String source, MavenProject project, BuildContext context) { |
| 122 | |
| 123 | project.addCompileSourceRoot(source); |
| 124 | Resource rsc = new Resource(); |
| 125 | rsc.setDirectory(source); |
| 126 | project.addResource(rsc); |
| 127 | context.refresh(project.getBasedir()); |
| 128 | log.info("Source directory added to compilation root: " + source); |
| 129 | } |
| 130 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 131 | /** |
| 132 | * Removes extra char from the string. |
| 133 | * |
| 134 | * @param valueString string to be trimmed |
| 135 | * @param removealStirng extra chars |
| 136 | * @return new string |
| 137 | */ |
| 138 | public static String trimAtLast(String valueString, String removealStirng) { |
| 139 | StringBuilder stringBuilder = new StringBuilder(valueString); |
| 140 | int index = valueString.lastIndexOf(removealStirng); |
| 141 | stringBuilder.deleteCharAt(index); |
| 142 | return stringBuilder.toString(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns new parted string. |
| 147 | * |
| 148 | * @param partString string to be parted |
| 149 | * @return parted string |
| 150 | */ |
| 151 | public static String partString(String partString) { |
| 152 | String[] strArray = partString.split(UtilConstants.COMMA); |
| 153 | String newString = ""; |
| 154 | for (int i = 0; i < strArray.length; i++) { |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 155 | if (i % 4 != 0 || i == 0) { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 156 | newString = newString + strArray[i] + UtilConstants.COMMA; |
| 157 | } else { |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame^] | 158 | newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION |
| 159 | + strArray[i] |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 160 | + UtilConstants.COMMA; |
| 161 | } |
| 162 | } |
| 163 | return trimAtLast(newString, UtilConstants.COMMA); |
| 164 | } |
| 165 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 166 | } |