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 | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 22 | import java.io.FileInputStream; |
| 23 | import java.io.FileOutputStream; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 24 | import java.io.FileReader; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 25 | import java.io.FileWriter; |
| 26 | import java.io.IOException; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 27 | import java.io.InputStream; |
| 28 | import java.io.ObjectInputStream; |
| 29 | import java.io.ObjectOutputStream; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 30 | import java.nio.file.Files; |
| 31 | import java.nio.file.StandardCopyOption; |
| 32 | import java.util.ArrayList; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 33 | import java.util.Enumeration; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 34 | import java.util.Iterator; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 35 | import java.util.LinkedList; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 36 | import java.util.List; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 37 | import java.util.Set; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 38 | import java.util.Stack; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 39 | import java.util.jar.JarEntry; |
| 40 | import java.util.jar.JarFile; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 41 | import java.util.regex.Pattern; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 42 | |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 43 | import org.apache.commons.io.FileUtils; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 44 | import org.apache.maven.artifact.repository.ArtifactRepository; |
| 45 | import org.apache.maven.model.Dependency; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 46 | import org.apache.maven.model.Resource; |
| 47 | import org.apache.maven.project.MavenProject; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 48 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 49 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 50 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | import org.sonatype.plexus.build.incremental.BuildContext; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 53 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 54 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 55 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
| 57 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 58 | import static org.onosproject.yangutils.utils.UtilConstants.HASH; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 59 | import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN; |
| 60 | import static org.onosproject.yangutils.utils.UtilConstants.JAR; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.utils.UtilConstants.ORG; |
| 64 | import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 65 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 66 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 67 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 68 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
| 69 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
| 70 | import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION; |
| 71 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES; |
| 72 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents; |
| 73 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 75 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | import static org.slf4j.LoggerFactory.getLogger; |
| 77 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 78 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 79 | * Represents common utility functionalities for code generation. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 80 | */ |
| 81 | public final class YangIoUtils { |
| 82 | |
| 83 | private static final Logger log = getLogger(YangIoUtils.class); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 84 | 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] | 85 | private static final int LINE_SIZE = 118; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 86 | private static final int SUB_LINE_SIZE = 112; |
| 87 | private static final int ZERO = 0; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 88 | private static final String SERIALIZED_FILE_EXTENSION = ".ser"; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 89 | |
| 90 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 91 | * Creates an instance of YANG io utils. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 92 | */ |
| 93 | private YangIoUtils() { |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Creates the directory structure. |
| 98 | * |
| 99 | * @param path directory path |
| 100 | * @return directory structure |
| 101 | */ |
| 102 | public static File createDirectories(String path) { |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 103 | File generatedDir = new File(path); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 104 | generatedDir.mkdirs(); |
| 105 | return generatedDir; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Adds package info file for the created directory. |
| 110 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 111 | * @param path directory path |
| 112 | * @param classInfo class info for the package |
| 113 | * @param pack package of the directory |
| 114 | * @param isChildNode is it a child node |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 115 | * @param pluginConfig plugin configurations |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 116 | * @throws IOException when fails to create package info file |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 117 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 118 | public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode, |
| 119 | YangPluginConfig pluginConfig) |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 120 | throws IOException { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 121 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 122 | pack = parsePkg(pack); |
| 123 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 124 | try { |
| 125 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 126 | File packageInfo = new File(path + SLASH + "package-info.java"); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 127 | packageInfo.createNewFile(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 128 | |
| 129 | FileWriter fileWriter = new FileWriter(packageInfo); |
| 130 | BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); |
| 131 | |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 132 | bufferedWriter.write(CopyrightHeader.getCopyrightHeader()); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 133 | bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode, pluginConfig)); |
| 134 | String pkg = PACKAGE + SPACE + pack + SEMI_COLAN; |
| 135 | if (pkg.length() > LINE_SIZE) { |
| 136 | pkg = whenDelimiterIsPersent(pkg, LINE_SIZE); |
| 137 | } |
| 138 | bufferedWriter.write(pkg); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 139 | bufferedWriter.close(); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 140 | fileWriter.close(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 141 | } catch (IOException e) { |
| 142 | throw new IOException("Exception occured while creating package info file."); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 147 | * Parses package and returns updated package. |
| 148 | * |
| 149 | * @param pack package needs to be updated |
| 150 | * @return updated package |
| 151 | */ |
| 152 | public static String parsePkg(String pack) { |
| 153 | |
| 154 | if (pack.contains(ORG)) { |
| 155 | String[] strArray = pack.split(ORG); |
| 156 | if (strArray.length >= 3) { |
| 157 | for (int i = 1; i < strArray.length; i++) { |
| 158 | if (i == 1) { |
| 159 | pack = ORG + strArray[1]; |
| 160 | } else { |
| 161 | pack = pack + ORG + strArray[i]; |
| 162 | } |
| 163 | } |
| 164 | } else { |
| 165 | pack = ORG + strArray[1]; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return pack; |
| 170 | } |
| 171 | |
| 172 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 173 | * Cleans the generated directory if already exist in source folder. |
| 174 | * |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 175 | * @param dir generated directory in previous build |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 176 | * @throws IOException when failed to delete directory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 177 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 178 | public static void deleteDirectory(String dir) |
| 179 | throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 180 | File generatedDirectory = new File(dir); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 181 | if (generatedDirectory.exists()) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 182 | try { |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 183 | FileUtils.deleteDirectory(generatedDirectory); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 184 | } catch (IOException e) { |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 185 | throw new IOException( |
| 186 | "Failed to delete the generated files in " + generatedDirectory + " directory"); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 187 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 192 | * Searches and deletes generated temporary directories. |
| 193 | * |
| 194 | * @param root root directory |
| 195 | * @throws IOException when fails to do IO operations. |
| 196 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 197 | public static void searchAndDeleteTempDir(String root) |
| 198 | throws IOException { |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 199 | List<File> store = new LinkedList<>(); |
| 200 | Stack<String> stack = new Stack<>(); |
| 201 | stack.push(root); |
| 202 | |
| 203 | while (!stack.empty()) { |
| 204 | root = stack.pop(); |
| 205 | File file = new File(root); |
| 206 | File[] filelist = file.listFiles(); |
| 207 | if (filelist == null || filelist.length == 0) { |
| 208 | continue; |
| 209 | } |
| 210 | for (File current : filelist) { |
| 211 | if (current.isDirectory()) { |
| 212 | stack.push(current.toString()); |
| 213 | if (current.getName().endsWith("-Temp")) { |
| 214 | store.add(current); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | for (File dir : store) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 221 | FileUtils.deleteDirectory(dir); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 226 | * Adds generated source directory to the compilation root. |
| 227 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 228 | * @param source directory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 229 | * @param project current maven project |
| 230 | * @param context current build context |
| 231 | */ |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 232 | public static void addToCompilationRoot(String source, MavenProject project, BuildContext context) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 233 | project.addCompileSourceRoot(source); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 234 | context.refresh(project.getBasedir()); |
| 235 | log.info("Source directory added to compilation root: " + source); |
| 236 | } |
| 237 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 238 | /** |
| 239 | * Removes extra char from the string. |
| 240 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 241 | * @param valueString string to be trimmed |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 242 | * @param removealStirng extra chars |
| 243 | * @return new string |
| 244 | */ |
| 245 | public static String trimAtLast(String valueString, String removealStirng) { |
| 246 | StringBuilder stringBuilder = new StringBuilder(valueString); |
| 247 | int index = valueString.lastIndexOf(removealStirng); |
| 248 | stringBuilder.deleteCharAt(index); |
| 249 | return stringBuilder.toString(); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Returns new parted string. |
| 254 | * |
| 255 | * @param partString string to be parted |
| 256 | * @return parted string |
| 257 | */ |
| 258 | public static String partString(String partString) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 259 | String[] strArray = partString.split(COMMA); |
| 260 | String newString = EMPTY_STRING; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 261 | for (int i = 0; i < strArray.length; i++) { |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 262 | if (i % 4 != 0 || i == 0) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 263 | newString = newString + strArray[i] + COMMA; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 264 | } else { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 265 | newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION |
| 266 | + strArray[i] + COMMA; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 267 | } |
| 268 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 269 | return trimAtLast(newString, COMMA); |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 270 | } |
| 271 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 272 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 273 | * Returns the directory path of the package in canonical form. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 274 | * |
| 275 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 276 | * put |
| 277 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 278 | * @return absolute path of the package in canonical form |
| 279 | */ |
| 280 | public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) { |
| 281 | |
| 282 | if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 283 | pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 284 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 285 | String[] strArray = pathOfJavaPkg.split(SLASH); |
| 286 | if (strArray[0].equals(EMPTY_STRING)) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 287 | return pathOfJavaPkg; |
| 288 | } else { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 289 | return baseCodeGenPath + SLASH + pathOfJavaPkg; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 294 | * Returns the absolute path of the package in canonical form. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 295 | * |
| 296 | * @param baseCodeGenPath base path where the generated files needs to be |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 297 | * put |
| 298 | * @param pathOfJavaPkg java package of the file being generated |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 299 | * @return absolute path of the package in canonical form |
| 300 | */ |
| 301 | public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 302 | return baseCodeGenPath + pathOfJavaPkg; |
| 303 | } |
| 304 | |
| 305 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 306 | * Copies YANG files to the current project's output directory. |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 307 | * |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 308 | * @param yangFileInfo list of YANG files |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 309 | * @param outputDir project's output directory |
| 310 | * @param project maven project |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 311 | * @throws IOException when fails to copy files to destination resource directory |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 312 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 313 | public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project) |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 314 | throws IOException { |
| 315 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 316 | List<File> files = getListOfFile(yangFileInfo); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 317 | |
| 318 | String path = outputDir + TARGET_RESOURCE_PATH; |
| 319 | File targetDir = new File(path); |
| 320 | targetDir.mkdirs(); |
| 321 | |
| 322 | for (File file : files) { |
| 323 | Files.copy(file.toPath(), |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 324 | new File(path + file.getName()).toPath(), |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 325 | StandardCopyOption.REPLACE_EXISTING); |
| 326 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 327 | addToProjectResource(outputDir + SLASH + TEMP + SLASH, project); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Provides a list of files from list of strings. |
| 332 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 333 | * @param yangFileInfo set of yang file information |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 334 | * @return list of files |
| 335 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 336 | private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 337 | List<File> files = new ArrayList<>(); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 338 | Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator(); |
| 339 | while (yangFileIterator.hasNext()) { |
| 340 | YangFileInfo yangFile = yangFileIterator.next(); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 341 | if (yangFile.isForTranslator()) { |
| 342 | files.add(new File(yangFile.getYangFileName())); |
| 343 | } |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 344 | } |
| 345 | return files; |
| 346 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 347 | |
| 348 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 349 | * Merges the temp java files to main java files. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 350 | * |
| 351 | * @param appendFile temp file |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 352 | * @param srcFile main file |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 353 | * @throws IOException when fails to append contents |
| 354 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 355 | public static void mergeJavaFiles(File appendFile, File srcFile) |
| 356 | throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 357 | try { |
| 358 | appendFileContents(appendFile, srcFile); |
| 359 | } catch (IOException e) { |
| 360 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 365 | * Inserts data in the generated file. |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 366 | * |
| 367 | * @param file file in which need to be inserted |
| 368 | * @param data data which need to be inserted |
| 369 | * @throws IOException when fails to insert into file |
| 370 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 371 | public static void insertDataIntoJavaFile(File file, String data) |
| 372 | throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 373 | try { |
| 374 | updateFileHandle(file, data, false); |
| 375 | } catch (IOException e) { |
| 376 | throw new IOException("Failed to insert in " + file + "file"); |
| 377 | } |
| 378 | } |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 379 | |
| 380 | /** |
| 381 | * Validates a line size in given file whether it is having more then 120 characters. |
| 382 | * If yes it will update and give a new file. |
| 383 | * |
| 384 | * @param dataFile file in which need to verify all lines. |
| 385 | * @return updated file |
| 386 | * @throws IOException when fails to do IO operations. |
| 387 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 388 | public static File validateLineLength(File dataFile) |
| 389 | throws IOException { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 390 | File tempFile = dataFile; |
| 391 | FileReader fileReader = new FileReader(dataFile); |
| 392 | BufferedReader bufferReader = new BufferedReader(fileReader); |
| 393 | try { |
| 394 | StringBuilder stringBuilder = new StringBuilder(); |
| 395 | String line = bufferReader.readLine(); |
| 396 | |
| 397 | while (line != null) { |
| 398 | if (line.length() > LINE_SIZE) { |
| 399 | if (line.contains(PERIOD)) { |
| 400 | line = whenDelimiterIsPersent(line, LINE_SIZE); |
| 401 | } else if (line.contains(SPACE)) { |
| 402 | line = whenSpaceIsPresent(line, LINE_SIZE); |
| 403 | } |
| 404 | stringBuilder.append(line); |
| 405 | } else { |
| 406 | stringBuilder.append(line + NEW_LINE); |
| 407 | } |
| 408 | line = bufferReader.readLine(); |
| 409 | } |
| 410 | FileWriter writer = new FileWriter(tempFile); |
| 411 | writer.write(stringBuilder.toString()); |
| 412 | writer.close(); |
| 413 | return tempFile; |
| 414 | } finally { |
| 415 | fileReader.close(); |
| 416 | bufferReader.close(); |
| 417 | } |
| 418 | } |
| 419 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 420 | /* When delimiters are present in the given line. */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 421 | private static String whenDelimiterIsPersent(String line, int lineSize) { |
| 422 | StringBuilder stringBuilder = new StringBuilder(); |
| 423 | |
| 424 | if (line.length() > lineSize) { |
| 425 | String[] strArray = line.split(Pattern.quote(PERIOD)); |
| 426 | stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize); |
| 427 | } else { |
| 428 | stringBuilder.append(line + NEW_LINE); |
| 429 | } |
| 430 | String[] strArray = stringBuilder.toString().split(NEW_LINE); |
| 431 | StringBuilder tempBuilder = new StringBuilder(); |
| 432 | for (String str : strArray) { |
| 433 | if (str.length() > SUB_LINE_SIZE) { |
| 434 | if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) { |
| 435 | String[] strArr = str.split(Pattern.quote(PERIOD)); |
| 436 | tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE); |
| 437 | } else if (str.contains(SPACE)) { |
| 438 | tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE)); |
| 439 | } |
| 440 | } else { |
| 441 | tempBuilder.append(str + NEW_LINE); |
| 442 | } |
| 443 | } |
| 444 | return tempBuilder.toString(); |
| 445 | |
| 446 | } |
| 447 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 448 | /* When spaces are present in the given line. */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 449 | private static String whenSpaceIsPresent(String line, int lineSize) { |
| 450 | StringBuilder stringBuilder = new StringBuilder(); |
| 451 | if (line.length() > lineSize) { |
| 452 | String[] strArray = line.split(SPACE); |
| 453 | stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize); |
| 454 | } else { |
| 455 | stringBuilder.append(line + NEW_LINE); |
| 456 | } |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 457 | |
| 458 | String[] strArray = stringBuilder.toString().split(NEW_LINE); |
| 459 | StringBuilder tempBuilder = new StringBuilder(); |
| 460 | for (String str : strArray) { |
| 461 | if (str.length() > SUB_LINE_SIZE) { |
| 462 | if (str.contains(SPACE)) { |
| 463 | String[] strArr = str.split(SPACE); |
| 464 | tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE); |
| 465 | } |
| 466 | } else { |
| 467 | tempBuilder.append(str + NEW_LINE); |
| 468 | } |
| 469 | } |
| 470 | return tempBuilder.toString(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 471 | } |
| 472 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 473 | /* Updates the given line with the given size conditions. */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 474 | private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string, |
| 475 | int lineSize) { |
| 476 | |
| 477 | StringBuilder tempBuilder = new StringBuilder(); |
| 478 | for (String str : strArray) { |
| 479 | tempBuilder.append(str + string); |
| 480 | if (tempBuilder.length() > lineSize) { |
| 481 | String tempString = stringBuilder.toString(); |
| 482 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 483 | tempString = trimAtLast(tempString, string); |
| 484 | stringBuilder.append(tempString); |
| 485 | if (string.equals(PERIOD)) { |
| 486 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string); |
| 487 | } else { |
| 488 | stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string); |
| 489 | } |
| 490 | tempBuilder.delete(ZERO, tempBuilder.length()); |
| 491 | tempBuilder.append(TWELVE_SPACE_INDENTATION); |
| 492 | } else { |
| 493 | stringBuilder.append(str + string); |
| 494 | } |
| 495 | } |
| 496 | String tempString = stringBuilder.toString(); |
| 497 | tempString = trimAtLast(tempString, string); |
| 498 | stringBuilder.delete(ZERO, stringBuilder.length()); |
| 499 | stringBuilder.append(tempString + NEW_LINE); |
| 500 | return stringBuilder; |
| 501 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 502 | |
| 503 | /** |
| 504 | * Serializes data-model. |
| 505 | * |
| 506 | * @param directory base directory for serialized files |
| 507 | * @param fileInfoSet YANG file info set |
| 508 | * @param project maven project |
| 509 | * @param operation true if need to add to resource |
| 510 | * @throws IOException when fails to do IO operations |
| 511 | */ |
| 512 | public static void serializeDataModel(String directory, Set<YangFileInfo> fileInfoSet, |
| 513 | MavenProject project, boolean operation) throws IOException { |
| 514 | |
| 515 | String serFileDirPath = directory + TARGET_RESOURCE_PATH; |
| 516 | File dir = new File(serFileDirPath); |
| 517 | dir.mkdirs(); |
| 518 | |
| 519 | if (operation) { |
| 520 | addToProjectResource(directory + SLASH + TEMP + SLASH, project); |
| 521 | } |
| 522 | |
| 523 | for (YangFileInfo fileInfo : fileInfoSet) { |
| 524 | |
| 525 | String serFileName = serFileDirPath + getCamelCase(fileInfo.getRootNode().getName(), null) |
| 526 | + SERIALIZED_FILE_EXTENSION; |
| 527 | fileInfo.setSerializedFile(serFileName); |
| 528 | FileOutputStream fileOutputStream = new FileOutputStream(serFileName); |
| 529 | ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); |
| 530 | objectOutputStream.writeObject(fileInfo.getRootNode()); |
| 531 | objectOutputStream.close(); |
| 532 | fileOutputStream.close(); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /* Adds directory to resources of project */ |
| 537 | private static void addToProjectResource(String dir, MavenProject project) { |
| 538 | Resource rsc = new Resource(); |
| 539 | rsc.setDirectory(dir); |
| 540 | project.addResource(rsc); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Returns de-serializes YANG data-model nodes. |
| 545 | * |
| 546 | * @param serailizedfileInfoSet YANG file info set |
| 547 | * @return de-serializes YANG data-model nodes |
| 548 | * @throws IOException when fails do IO operations |
| 549 | */ |
| 550 | public static List<YangNode> deSerializeDataModel(List<String> serailizedfileInfoSet) throws IOException { |
| 551 | |
| 552 | List<YangNode> nodes = new ArrayList<>(); |
| 553 | for (String fileInfo : serailizedfileInfoSet) { |
| 554 | YangNode node = null; |
| 555 | try { |
| 556 | FileInputStream fileInputStream = new FileInputStream(fileInfo); |
| 557 | ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); |
| 558 | node = (YangNode) objectInputStream.readObject(); |
| 559 | nodes.add(node); |
| 560 | objectInputStream.close(); |
| 561 | fileInputStream.close(); |
| 562 | } catch (IOException | ClassNotFoundException e) { |
| 563 | throw new IOException(fileInfo + " not found."); |
| 564 | } |
| 565 | } |
| 566 | return nodes; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Resolves inter jar dependencies. |
| 571 | * |
| 572 | * @param project current maven project |
| 573 | * @param localRepository local maven repository |
| 574 | * @param remoteRepos list of remote repository |
| 575 | * @param directory directory for serialized files |
| 576 | * @return list of resolved datamodel nodes |
| 577 | * @throws IOException when fails to do IO operations |
| 578 | */ |
| 579 | public static List<YangNode> resolveInterJarDependencies(MavenProject project, ArtifactRepository localRepository, |
| 580 | List<ArtifactRepository> remoteRepos, String directory) throws IOException { |
| 581 | |
| 582 | List<String> dependeciesJarPaths = resolveDependecyJarPath(project, localRepository, remoteRepos); |
| 583 | List<YangNode> resolvedDataModelNodes = new ArrayList<>(); |
| 584 | for (String dependecy : dependeciesJarPaths) { |
| 585 | resolvedDataModelNodes.addAll(deSerializeDataModel(parseJarFile(dependecy, directory))); |
| 586 | } |
| 587 | return resolvedDataModelNodes; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Returns list of jar path. |
| 592 | * |
| 593 | * @return list of jar paths |
| 594 | */ |
| 595 | private static List<String> resolveDependecyJarPath(MavenProject project, ArtifactRepository localRepository, |
| 596 | List<ArtifactRepository> remoteRepos) { |
| 597 | |
| 598 | StringBuilder path = new StringBuilder(); |
| 599 | List<String> jarPaths = new ArrayList<>(); |
| 600 | for (Dependency dependency : project.getDependencies()) { |
| 601 | |
| 602 | path.append(localRepository.getBasedir()); |
| 603 | path.append(SLASH); |
| 604 | path.append(getPackageDirPathFromJavaJPackage(dependency.getGroupId())); |
| 605 | path.append(SLASH); |
| 606 | path.append(dependency.getArtifactId()); |
| 607 | path.append(SLASH); |
| 608 | path.append(dependency.getVersion()); |
| 609 | path.append(SLASH); |
| 610 | path.append(dependency.getArtifactId() + HYPHEN + dependency.getVersion() + PERIOD + JAR); |
| 611 | File jarFile = new File(path.toString()); |
| 612 | if (jarFile.exists()) { |
| 613 | jarPaths.add(path.toString()); |
| 614 | } |
| 615 | path.delete(0, path.length()); |
| 616 | } |
| 617 | |
| 618 | for (ArtifactRepository repo : remoteRepos) { |
| 619 | // TODO: add resolver for remote repo. |
| 620 | } |
| 621 | return jarPaths; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Parses jar file and returns list of serialized file names. |
| 626 | * |
| 627 | * @param jarFile jar file to be parsed |
| 628 | * @param directory directory for keeping the searized files |
| 629 | * @return list of serialized files |
| 630 | * @throws IOException when fails to do IO operations |
| 631 | */ |
| 632 | public static List<String> parseJarFile(String jarFile, String directory) |
| 633 | throws IOException { |
| 634 | |
| 635 | List<String> serailizedFiles = new ArrayList<>(); |
| 636 | JarFile jar = new JarFile(jarFile); |
| 637 | Enumeration<?> enumEntries = jar.entries(); |
| 638 | |
| 639 | File serializedFileDir = new File(directory); |
| 640 | serializedFileDir.mkdirs(); |
| 641 | while (enumEntries.hasMoreElements()) { |
| 642 | JarEntry file = (JarEntry) enumEntries.nextElement(); |
| 643 | if (file.getName().endsWith(SERIALIZED_FILE_EXTENSION)) { |
| 644 | if (file.getName().contains(SLASH)) { |
| 645 | String[] strArray = file.getName().split(SLASH); |
| 646 | String tempPath = ""; |
| 647 | for (int i = 0; i < strArray.length - 1; i++) { |
| 648 | tempPath = SLASH + tempPath + SLASH + strArray[i]; |
| 649 | } |
| 650 | File dir = new File(directory + tempPath); |
| 651 | dir.mkdirs(); |
| 652 | } |
| 653 | File serailizedFile = new File(directory + SLASH + file.getName()); |
| 654 | if (file.isDirectory()) { |
| 655 | serailizedFile.mkdirs(); |
| 656 | continue; |
| 657 | } |
| 658 | InputStream inputStream = jar.getInputStream(file); |
| 659 | |
| 660 | FileOutputStream fileOutputStream = new FileOutputStream(serailizedFile); |
| 661 | while (inputStream.available() > 0) { |
| 662 | fileOutputStream.write(inputStream.read()); |
| 663 | } |
| 664 | fileOutputStream.close(); |
| 665 | inputStream.close(); |
| 666 | serailizedFiles.add(serailizedFile.toString()); |
| 667 | } |
| 668 | } |
| 669 | jar.close(); |
| 670 | return serailizedFiles; |
| 671 | } |
| 672 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 673 | } |