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