Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +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.translator.tojava.utils; |
| 18 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 19 | import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_CLASS_MASK; |
| 20 | import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_INTERFACE_MASK; |
| 21 | import static org.onosproject.yangutils.translator.GeneratedFileType.GENERATE_TYPEDEF_CLASS; |
| 22 | import static org.onosproject.yangutils.translator.GeneratedFileType.IMPL_CLASS_MASK; |
| 23 | import static org.onosproject.yangutils.translator.GeneratedFileType.INTERFACE_MASK; |
| 24 | import static org.slf4j.LoggerFactory.getLogger; |
| 25 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 26 | import java.io.File; |
| 27 | import java.io.IOException; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 28 | import java.util.ArrayList; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 31 | import org.apache.commons.io.FileUtils; |
| 32 | import org.onosproject.yangutils.translator.CachedFileHandle; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.AttributeInfo; |
| 34 | import org.onosproject.yangutils.utils.UtilConstants; |
| 35 | import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; |
| 36 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
| 37 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 41 | /** |
| 42 | * Generates java file. |
| 43 | */ |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 44 | public final class JavaFileGenerator { |
| 45 | |
| 46 | private static final Logger log = getLogger(JavaFileGenerator.class); |
| 47 | |
| 48 | /** |
| 49 | * Default constructor. |
| 50 | */ |
| 51 | private JavaFileGenerator() { |
| 52 | } |
| 53 | |
| 54 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 55 | * Returns a file object for generated file. |
| 56 | * |
| 57 | * @param fileName file name |
| 58 | * @param filePath file package path |
| 59 | * @param extension file extension |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 60 | * @param handle cached file handle |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 61 | * @return file object |
| 62 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 63 | public static File getFileObject(String filePath, String fileName, String extension, CachedFileHandle handle) { |
| 64 | return new File(handle.getCodeGenFilePath() + filePath + File.separator + fileName + extension); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 68 | * Returns generated interface file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 69 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 70 | * @param file file |
| 71 | * @param className class name |
| 72 | * @param imports imports for the file |
| 73 | * @param attrList attribute info |
| 74 | * @param pkg generated file package |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 75 | * @param handle cached file handle |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 76 | * @return interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 77 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 78 | */ |
| 79 | public static File generateInterfaceFile(File file, String className, List<String> imports, |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 80 | List<AttributeInfo> attrList, String pkg, CachedFileHandle handle) throws IOException { |
| 81 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 82 | initiateFile(file, className, INTERFACE_MASK, imports, pkg); |
| 83 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 84 | List<String> methods = new ArrayList<>(); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 85 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 86 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 87 | } catch (ClassNotFoundException | IOException e) { |
| 88 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 89 | throw new IOException("Fail to read data from temp file."); |
| 90 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Add getter methods to interface file. |
| 94 | */ |
| 95 | for (String method : methods) { |
| 96 | appendMethod(file, method); |
| 97 | } |
| 98 | return file; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return generated builder interface file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 103 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 104 | * @param file file |
| 105 | * @param className class name |
| 106 | * @param pkg generated file package |
| 107 | * @param attrList attribute info |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 108 | * @param handle cached file handle |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 109 | * @return builder interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 110 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 111 | */ |
| 112 | public static File generateBuilderInterfaceFile(File file, String className, String pkg, |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 113 | List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException { |
| 114 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 115 | initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 116 | List<String> methods = new ArrayList<>(); |
| 117 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 118 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 119 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path)); |
| 120 | methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 121 | } catch (ClassNotFoundException | IOException e) { |
| 122 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 123 | throw new IOException("Fail to read data from temp file."); |
| 124 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Add build method to builder interface file. |
| 128 | */ |
| 129 | methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className)); |
| 130 | |
| 131 | /** |
| 132 | * Add getters and setters in builder interface. |
| 133 | */ |
| 134 | for (String method : methods) { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 135 | appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 139 | return file; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns generated builder class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 144 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 145 | * @param file file |
| 146 | * @param className class name |
| 147 | * @param imports imports for the file |
| 148 | * @param pkg generated file package |
| 149 | * @param attrList attribute info |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 150 | * @param handle cached file handle |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 151 | * @return builder class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 152 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 153 | */ |
| 154 | public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg, |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 155 | List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException { |
| 156 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 157 | initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg); |
| 158 | |
| 159 | /** |
| 160 | * Add attribute strings. |
| 161 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 162 | List<String> attributes = new ArrayList<>(); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 163 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 164 | attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 165 | } catch (ClassNotFoundException | IOException e) { |
| 166 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 167 | throw new IOException("Fail to read data from temp file."); |
| 168 | } |
| 169 | /** |
| 170 | * Add attributes to the file. |
| 171 | */ |
| 172 | for (String attribute : attributes) { |
| 173 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 174 | } |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 175 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 176 | List<String> methods = new ArrayList<>(); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 177 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 178 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path)); |
| 179 | methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 180 | } catch (ClassNotFoundException | IOException e) { |
| 181 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 182 | throw new IOException("Fail to read data from temp file."); |
| 183 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Add default constructor and build method impl. |
| 187 | */ |
| 188 | methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 189 | + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER, |
| 190 | UtilConstants.PUBLIC)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 191 | methods.add(MethodsGenerator.getBuildString(className)); |
| 192 | |
| 193 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 194 | * Add methods in builder class. |
| 195 | */ |
| 196 | for (String method : methods) { |
| 197 | appendMethod(file, method + UtilConstants.NEW_LINE); |
| 198 | } |
| 199 | return file; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns generated impl class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 204 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 205 | * @param file file |
| 206 | * @param className class name |
| 207 | * @param pkg generated file package |
| 208 | * @param attrList attribute's info |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 209 | * @param handle cached file handle |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 210 | * @return impl class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 211 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 212 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 213 | public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList, |
| 214 | CachedFileHandle handle) |
| 215 | throws IOException { |
| 216 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
| 217 | initiateFile(file, className, IMPL_CLASS_MASK, null, path); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 218 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 219 | List<String> attributes = new ArrayList<>(); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 220 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 221 | attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path)); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 222 | } catch (ClassNotFoundException | IOException e) { |
| 223 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 224 | throw new IOException("Fail to read data from temp file."); |
| 225 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 226 | |
| 227 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 228 | * Add attributes to the file. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 229 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 230 | for (String attribute : attributes) { |
| 231 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 232 | } |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 233 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 234 | List<String> methods = new ArrayList<>(); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 235 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 236 | |
| 237 | methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path)); |
| 238 | |
| 239 | methods.add(getConstructorString(className) |
| 240 | + handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path) |
| 241 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET); |
| 242 | |
| 243 | methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen() |
| 244 | + handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(UtilConstants.NEW_LINE, |
| 245 | ""))); |
| 246 | |
| 247 | methods.add(MethodsGenerator |
| 248 | .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL) |
| 249 | + handle.getTempData(TempDataStoreTypes.EQUALS, className, path))); |
| 250 | |
| 251 | methods.add(MethodsGenerator.getToStringMethodOpen() |
| 252 | + handle.getTempData(TempDataStoreTypes.TO_STRING, className, path) |
| 253 | + MethodsGenerator.getToStringMethodClose()); |
| 254 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 255 | } catch (ClassNotFoundException | IOException e) { |
| 256 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 257 | throw new IOException("Fail to read data from temp file."); |
| 258 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 259 | |
| 260 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 261 | * Add methods in impl class. |
| 262 | */ |
| 263 | for (String method : methods) { |
| 264 | appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE); |
| 265 | } |
| 266 | insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 267 | |
| 268 | return file; |
| 269 | } |
| 270 | |
| 271 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 272 | * Generate class file for type def. |
| 273 | * |
| 274 | * @param file generated file |
| 275 | * @param className file name |
| 276 | * @param imports imports for file |
| 277 | * @param pkg package path |
| 278 | * @param cachedAttributeList attribute list |
| 279 | * @param handle cached file handle |
| 280 | * @return type def class file |
| 281 | * @throws IOException when fails to generate class file |
| 282 | */ |
| 283 | public static File generateTypeDefClassFile(File file, String className, List<String> imports, |
| 284 | String pkg, List<AttributeInfo> cachedAttributeList, CachedFileHandle handle) throws IOException { |
| 285 | String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
| 286 | initiateFile(file, className, GENERATE_TYPEDEF_CLASS, imports, pkg); |
| 287 | |
| 288 | List<String> typeDef = new ArrayList<>(); |
| 289 | try { |
| 290 | typeDef.add(handle.getTempData(TempDataStoreTypes.TYPE_DEF, className, path)); |
| 291 | } catch (ClassNotFoundException | IOException e) { |
| 292 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 293 | throw new IOException("Fail to read data from temp file."); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Add attributes to the file. |
| 298 | */ |
| 299 | for (String attribute : typeDef) { |
| 300 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 301 | } |
| 302 | |
| 303 | return file; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Returns constructor string for impl class. |
| 308 | * |
| 309 | * @param yangName class name |
| 310 | * @return constructor string |
| 311 | */ |
| 312 | private static String getConstructorString(String yangName) { |
| 313 | |
| 314 | String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1); |
| 315 | String javadoc = MethodsGenerator.getConstructorString(yangName); |
| 316 | String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE |
| 317 | + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER |
| 318 | + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS |
| 319 | + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE; |
| 320 | return javadoc + constructor; |
| 321 | } |
| 322 | |
| 323 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 324 | * Initiate generation of file based on generated file type. |
| 325 | * |
| 326 | * @param file generated file |
| 327 | * @param className generated file class name |
| 328 | * @param type generated file type |
| 329 | * @param imports imports for the file |
| 330 | * @param pkg generated file package |
| 331 | * @throws IOException when fails to generate a file |
| 332 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 333 | private static void initiateFile(File file, String className, int type, List<String> imports, |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 334 | String pkg) throws IOException { |
| 335 | try { |
| 336 | file.createNewFile(); |
| 337 | appendContents(file, className, type, imports, pkg); |
| 338 | } catch (IOException e) { |
| 339 | throw new IOException("Failed to create " + file.getName() + " class file."); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Appends the temp files to main files. |
| 345 | * |
| 346 | * @param appendFile temp file |
| 347 | * @param srcFile main file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 348 | * @throws IOException when fails to append contents |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 349 | */ |
| 350 | public static void appendFileContents(File appendFile, File srcFile) throws IOException { |
| 351 | try { |
| 352 | FileSystemUtil.appendFileContents(appendFile, srcFile); |
| 353 | } catch (IOException e) { |
| 354 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Append methods to the generated files. |
| 360 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 361 | * @param file file in which method needs to be appended |
| 362 | * @param method method which needs to be appended |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 363 | */ |
| 364 | private static void appendMethod(File file, String method) throws IOException { |
| 365 | insert(file, method); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Closes the current generated file. |
| 370 | * |
| 371 | * @param fileType generate file type |
| 372 | * @param yangName file name |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 373 | * @return end of class definition string |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 374 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 375 | public static String closeFile(int fileType, String yangName) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 376 | return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Parses attribute info and fetch specific data and creates serialized |
| 381 | * files of it. |
| 382 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 383 | * @param attr attribute info. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 384 | * @param genFileType generated file type |
| 385 | * @param className class name |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 386 | * @param path file path |
| 387 | * @param handle cached file handle |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 388 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 389 | public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className, String path, |
| 390 | CachedFileHandle handle) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 391 | |
| 392 | String attrString = ""; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 393 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 394 | String getterString = ""; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 395 | String getterImplString = ""; |
| 396 | |
| 397 | String setterString = ""; |
| 398 | String setterImplString = ""; |
| 399 | |
| 400 | String constructorString = ""; |
| 401 | String typeDefString = ""; |
| 402 | |
| 403 | String toString = ""; |
| 404 | String hashCodeString = ""; |
| 405 | String equalsString = ""; |
| 406 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 407 | className = JavaIdentifierSyntax.getCaptialCase(className); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 408 | |
| 409 | try { |
| 410 | /* |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 411 | * Get the attribute definition and save attributes to temporary |
| 412 | * file. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 413 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 414 | |
| 415 | boolean isList = attr.isListAttr(); |
| 416 | String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName()); |
| 417 | if (attr.isQualifiedName()) { |
| 418 | attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), |
| 419 | attr.getImportInfo().getClassInfo(), |
| 420 | attributeName, attr.isListAttr()); |
| 421 | } else { |
| 422 | attrString = JavaCodeSnippetGen.getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), |
| 423 | attributeName, attr.isListAttr()); |
| 424 | } |
| 425 | handle.setTempData(attrString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION, |
| 426 | TempDataStoreTypes.ATTRIBUTE, className, |
| 427 | path); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 428 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 429 | if ((genFileType & INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 430 | getterString = MethodsGenerator.getGetterString(attr); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 431 | handle.setTempData(getterString + UtilConstants.NEW_LINE, |
| 432 | TempDataStoreTypes.GETTER_METHODS, |
| 433 | className, |
| 434 | path); |
| 435 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if ((genFileType & BUILDER_INTERFACE_MASK) != 0) { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 439 | setterString = MethodsGenerator.getSetterString(attr, className); |
| 440 | handle.setTempData(setterString + UtilConstants.NEW_LINE, |
| 441 | TempDataStoreTypes.SETTER_METHODS, |
| 442 | className, |
| 443 | path); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if ((genFileType & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 447 | getterImplString = MethodsGenerator.getGetterForClass(attr); |
| 448 | handle.setTempData( |
| 449 | MethodsGenerator.getOverRideString() + getterImplString + UtilConstants.NEW_LINE, |
| 450 | TempDataStoreTypes.GETTER_METHODS_IMPL, className, |
| 451 | path); |
| 452 | setterImplString = MethodsGenerator.getSetterForClass(attr, className); |
| 453 | handle.setTempData( |
| 454 | MethodsGenerator.getOverRideString() + setterImplString + UtilConstants.NEW_LINE, |
| 455 | TempDataStoreTypes.SETTER_METHODS_IMPL, className, |
| 456 | path); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | if ((genFileType & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 460 | constructorString = MethodsGenerator.getConstructor(className, attr); |
| 461 | handle.setTempData(constructorString, TempDataStoreTypes.CONSTRUCTOR, className, |
| 462 | path); |
| 463 | |
| 464 | hashCodeString = MethodsGenerator.getHashCodeMethod(attr); |
| 465 | handle.setTempData(hashCodeString + UtilConstants.NEW_LINE, |
| 466 | TempDataStoreTypes.HASH_CODE, |
| 467 | className, |
| 468 | path); |
| 469 | equalsString = MethodsGenerator.getEqualsMethod(attr); |
| 470 | handle.setTempData(equalsString + UtilConstants.NEW_LINE, |
| 471 | TempDataStoreTypes.EQUALS, |
| 472 | className, |
| 473 | path); |
| 474 | |
| 475 | toString = MethodsGenerator.getToStringMethod(attr); |
| 476 | handle.setTempData(toString + UtilConstants.NEW_LINE, |
| 477 | TempDataStoreTypes.TO_STRING, |
| 478 | className, |
| 479 | path); |
| 480 | |
| 481 | } |
| 482 | |
| 483 | if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) { |
| 484 | |
| 485 | if (attr.isQualifiedName()) { |
| 486 | typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), |
| 487 | attr.getImportInfo().getClassInfo(), |
| 488 | attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE; |
| 489 | } else { |
| 490 | typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(null, |
| 491 | attr.getImportInfo().getClassInfo(), |
| 492 | attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE; |
| 493 | } |
| 494 | |
| 495 | typeDefString = typeDefString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 496 | + UtilConstants.JAVA_DOC_FIRST_LINE; |
| 497 | |
| 498 | typeDefString = typeDefString |
| 499 | + MethodsGenerator.getDefaultConstructorString(className, UtilConstants.PRIVATE) |
| 500 | + UtilConstants.NEW_LINE; |
| 501 | |
| 502 | typeDefString = typeDefString |
| 503 | + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_CONSTRUCTOR, className, isList) |
| 504 | + MethodsGenerator.getTypeDefConstructor(attr, className) |
| 505 | + UtilConstants.NEW_LINE; |
| 506 | |
| 507 | typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.OF, className, isList) |
| 508 | + MethodsGenerator.getOfMethod(className, attr) + UtilConstants.NEW_LINE; |
| 509 | |
| 510 | typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.GETTER, className, isList) |
| 511 | + MethodsGenerator.getGetterForClass(attr) + UtilConstants.NEW_LINE; |
| 512 | |
| 513 | typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, className, isList) |
| 514 | + MethodsGenerator.getSetterForTypeDefClass(attr) |
| 515 | + UtilConstants.NEW_LINE; |
| 516 | |
| 517 | hashCodeString = hashCodeString + MethodsGenerator.getHashCodeMethodOpen() |
| 518 | + MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, ""); |
| 519 | hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE; |
| 520 | |
| 521 | equalsString = equalsString + MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE |
| 522 | + MethodsGenerator.getEqualsMethod(attr); |
| 523 | equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE; |
| 524 | |
| 525 | toString = toString + MethodsGenerator.getToStringMethodOpen() |
| 526 | + MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE |
| 527 | + MethodsGenerator.getToStringMethodClose() |
| 528 | + UtilConstants.NEW_LINE; |
| 529 | typeDefString = typeDefString + hashCodeString + equalsString + toString; |
| 530 | handle.setTempData(typeDefString, TempDataStoreTypes.TYPE_DEF, className, |
| 531 | path); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 532 | } |
| 533 | } catch (IOException e) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 534 | log.info("Failed to set data for " + attr.getAttributeName() + " in temp data files."); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 535 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 536 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Appends all the contents into a generated java file. |
| 541 | * |
| 542 | * @param file generated file |
| 543 | * @param fileName generated file name |
| 544 | * @param type generated file type |
| 545 | * @param pkg generated file package |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 546 | * @param importsList list of java imports. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 547 | * @throws IOException when fails to append contents |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 548 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 549 | private static void appendContents(File file, String fileName, int type, List<String> importsList, |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 550 | String pkg) throws IOException { |
| 551 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 552 | if (pkg.contains(UtilConstants.YANG_GEN_DIR)) { |
| 553 | String[] strArray = pkg.split(UtilConstants.YANG_GEN_DIR); |
| 554 | pkg = strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD); |
| 555 | } |
| 556 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 557 | if ((type & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 558 | |
| 559 | write(file, fileName, type, JavaDocType.IMPL_CLASS); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 560 | } else if ((type & BUILDER_INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 561 | |
| 562 | write(file, fileName, type, JavaDocType.BUILDER_INTERFACE); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 563 | } else if ((type & GENERATE_TYPEDEF_CLASS) != 0) { |
| 564 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 565 | insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE); |
| 566 | if (importsList != null) { |
| 567 | insert(file, UtilConstants.NEW_LINE); |
| 568 | for (String imports : importsList) { |
| 569 | insert(file, imports); |
| 570 | } |
| 571 | insert(file, UtilConstants.NEW_LINE); |
| 572 | } |
| 573 | write(file, fileName, type, JavaDocType.IMPL_CLASS); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 574 | } else { |
| 575 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 576 | if ((type & INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 577 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 578 | insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE); |
| 579 | if (importsList != null) { |
| 580 | insert(file, UtilConstants.NEW_LINE); |
| 581 | for (String imports : importsList) { |
| 582 | insert(file, imports); |
| 583 | } |
| 584 | insert(file, UtilConstants.NEW_LINE); |
| 585 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 586 | write(file, fileName, type, JavaDocType.INTERFACE); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 587 | } else if ((type & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 588 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 589 | insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE); |
| 590 | if (importsList != null) { |
| 591 | insert(file, UtilConstants.NEW_LINE); |
| 592 | for (String imports : importsList) { |
| 593 | insert(file, imports); |
| 594 | } |
| 595 | insert(file, UtilConstants.NEW_LINE); |
| 596 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 597 | write(file, fileName, type, JavaDocType.BUILDER_CLASS); |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Write data to the specific generated file. |
| 604 | * |
| 605 | * @param file generated file |
| 606 | * @param fileName file name |
| 607 | * @param genType generated file type |
| 608 | * @param javaDocType java doc type |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 609 | * @throws IOException when fails to write into a file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 610 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 611 | private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType) |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 612 | throws IOException { |
| 613 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 614 | insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName, false)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 615 | insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName)); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Insert in the generated file. |
| 620 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 621 | * @param file file in which need to be inserted |
| 622 | * @param data data which need to be inserted |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 623 | * @throws IOException when fails to insert into file |
| 624 | */ |
| 625 | public static void insert(File file, String data) throws IOException { |
| 626 | try { |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 627 | FileSystemUtil.updateFileHandle(file, data, false); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 628 | } catch (IOException e) { |
| 629 | throw new IOException("Failed to insert in " + file + "file"); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 634 | * Closes the files handle for generate files. |
| 635 | * |
| 636 | * @param file generate files |
| 637 | * @throws IOException when failed to close the file handle |
| 638 | */ |
| 639 | public static void closeFileHandles(File file) throws IOException { |
| 640 | try { |
| 641 | FileSystemUtil.updateFileHandle(file, null, true); |
| 642 | } catch (IOException e) { |
| 643 | throw new IOException("Failed to close file handle for " + file + "file"); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 648 | * Removes temp files. |
| 649 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 650 | * @param file file to be removed |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 651 | */ |
| 652 | public static void clean(File file) { |
| 653 | if (file.exists()) { |
| 654 | file.delete(); |
| 655 | } |
| 656 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame^] | 657 | |
| 658 | /** |
| 659 | * Removes temp files. |
| 660 | * |
| 661 | * @param tempDir temp directory |
| 662 | * @throws IOException when fails to delete the directory |
| 663 | */ |
| 664 | public static void cleanTempFiles(File tempDir) throws IOException { |
| 665 | FileUtils.deleteDirectory(tempDir); |
| 666 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 667 | } |