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 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 21 | import java.util.List; |
| 22 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.AttributeInfo; |
| 24 | import org.onosproject.yangutils.utils.UtilConstants; |
| 25 | import org.onosproject.yangutils.utils.io.impl.CopyrightHeader; |
| 26 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
| 27 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.utils.io.impl.TempDataStore; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_CLASS_MASK; |
| 34 | import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_INTERFACE_MASK; |
| 35 | import static org.onosproject.yangutils.translator.GeneratedFileType.IMPL_CLASS_MASK; |
| 36 | import static org.onosproject.yangutils.translator.GeneratedFileType.INTERFACE_MASK; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Generates java file. |
| 41 | */ |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 42 | public final class JavaFileGenerator { |
| 43 | |
| 44 | private static final Logger log = getLogger(JavaFileGenerator.class); |
| 45 | |
| 46 | /** |
| 47 | * Default constructor. |
| 48 | */ |
| 49 | private JavaFileGenerator() { |
| 50 | } |
| 51 | |
| 52 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 53 | * Returns a file object for generated file. |
| 54 | * |
| 55 | * @param fileName file name |
| 56 | * @param filePath file package path |
| 57 | * @param extension file extension |
| 58 | * @return file object |
| 59 | */ |
| 60 | public static File getFileObject(String filePath, String fileName, String extension) { |
| 61 | return new File(UtilConstants.YANG_GEN_DIR + filePath + File.separator + fileName + extension); |
| 62 | } |
| 63 | |
| 64 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 65 | * Returns generated interface file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 66 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 67 | * @param file file |
| 68 | * @param className class name |
| 69 | * @param imports imports for the file |
| 70 | * @param attrList attribute info |
| 71 | * @param pkg generated file package |
| 72 | * @return interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 73 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 74 | */ |
| 75 | public static File generateInterfaceFile(File file, String className, List<String> imports, |
| 76 | List<AttributeInfo> attrList, String pkg) throws IOException { |
| 77 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 78 | initiateFile(file, className, INTERFACE_MASK, imports, pkg); |
| 79 | |
| 80 | List<String> methods; |
| 81 | try { |
| 82 | methods = TempDataStore.getTempData(TempDataStoreType.GETTER_METHODS, className); |
| 83 | } catch (ClassNotFoundException | IOException e) { |
| 84 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 85 | throw new IOException("Fail to read data from temp file."); |
| 86 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Add getter methods to interface file. |
| 90 | */ |
| 91 | for (String method : methods) { |
| 92 | appendMethod(file, method); |
| 93 | } |
| 94 | return file; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Return generated builder interface file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 99 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 100 | * @param file file |
| 101 | * @param className class name |
| 102 | * @param pkg generated file package |
| 103 | * @param attrList attribute info |
| 104 | * @return builder interface file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 105 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 106 | */ |
| 107 | public static File generateBuilderInterfaceFile(File file, String className, String pkg, |
| 108 | List<AttributeInfo> attrList) throws IOException { |
| 109 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 110 | initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg); |
| 111 | List<String> methods; |
| 112 | try { |
| 113 | methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_INTERFACE_METHODS, |
| 114 | className + UtilConstants.BUILDER + UtilConstants.INTERFACE); |
| 115 | } catch (ClassNotFoundException | IOException e) { |
| 116 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 117 | throw new IOException("Fail to read data from temp file."); |
| 118 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Add build method to builder interface file. |
| 122 | */ |
| 123 | methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className)); |
| 124 | |
| 125 | /** |
| 126 | * Add getters and setters in builder interface. |
| 127 | */ |
| 128 | for (String method : methods) { |
| 129 | appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE); |
| 130 | } |
| 131 | |
| 132 | insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 133 | return file; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns generated builder class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 138 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 139 | * @param file file |
| 140 | * @param className class name |
| 141 | * @param imports imports for the file |
| 142 | * @param pkg generated file package |
| 143 | * @param attrList attribute info |
| 144 | * @return builder class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 145 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 146 | */ |
| 147 | public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg, |
| 148 | List<AttributeInfo> attrList) throws IOException { |
| 149 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 150 | initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg); |
| 151 | |
| 152 | /** |
| 153 | * Add attribute strings. |
| 154 | */ |
| 155 | List<String> attributes; |
| 156 | try { |
| 157 | attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className); |
| 158 | } catch (ClassNotFoundException | IOException e) { |
| 159 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 160 | throw new IOException("Fail to read data from temp file."); |
| 161 | } |
| 162 | /** |
| 163 | * Add attributes to the file. |
| 164 | */ |
| 165 | for (String attribute : attributes) { |
| 166 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 167 | } |
| 168 | insert(file, UtilConstants.NEW_LINE); |
| 169 | |
| 170 | List<String> methods; |
| 171 | try { |
| 172 | methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_METHODS, className + UtilConstants.BUILDER); |
| 173 | } catch (ClassNotFoundException | IOException e) { |
| 174 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 175 | throw new IOException("Fail to read data from temp file."); |
| 176 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Add default constructor and build method impl. |
| 180 | */ |
| 181 | methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 182 | + MethodsGenerator.getDefaultConstructorString(BUILDER_CLASS_MASK, className)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 183 | methods.add(MethodsGenerator.getBuildString(className)); |
| 184 | |
| 185 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 186 | * Add methods in builder class. |
| 187 | */ |
| 188 | for (String method : methods) { |
| 189 | appendMethod(file, method + UtilConstants.NEW_LINE); |
| 190 | } |
| 191 | return file; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns generated impl class file for current node. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 196 | * |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 197 | * @param file file |
| 198 | * @param className class name |
| 199 | * @param pkg generated file package |
| 200 | * @param attrList attribute's info |
| 201 | * @return impl class file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 202 | * @throws IOException when fails to write in file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 203 | */ |
| 204 | public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList) |
| 205 | throws IOException { |
| 206 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 207 | initiateFile(file, className, IMPL_CLASS_MASK, null, pkg); |
| 208 | |
| 209 | List<String> attributes; |
| 210 | try { |
| 211 | attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className); |
| 212 | } catch (ClassNotFoundException | IOException e) { |
| 213 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 214 | throw new IOException("Fail to read data from temp file."); |
| 215 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 216 | |
| 217 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 218 | * Add attributes to the file. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 219 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 220 | for (String attribute : attributes) { |
| 221 | insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute); |
| 222 | } |
| 223 | insert(file, UtilConstants.NEW_LINE); |
| 224 | |
| 225 | List<String> methods; |
| 226 | try { |
| 227 | methods = TempDataStore.getTempData(TempDataStoreType.IMPL_METHODS, className + UtilConstants.IMPL); |
| 228 | } catch (ClassNotFoundException | IOException e) { |
| 229 | log.info("There is no attribute info of " + className + " YANG file in the temporary files."); |
| 230 | throw new IOException("Fail to read data from temp file."); |
| 231 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * Add default constructor and constructor methods. |
| 235 | */ |
| 236 | methods.add(UtilConstants.JAVA_DOC_FIRST_LINE |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 237 | + MethodsGenerator.getDefaultConstructorString(IMPL_CLASS_MASK, className)); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 238 | methods.add(MethodsGenerator.getConstructorString(className)); |
| 239 | |
| 240 | /** |
| 241 | * Add methods in impl class. |
| 242 | */ |
| 243 | for (String method : methods) { |
| 244 | appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE); |
| 245 | } |
| 246 | insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE); |
| 247 | |
| 248 | return file; |
| 249 | } |
| 250 | |
| 251 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 252 | * Initiate generation of file based on generated file type. |
| 253 | * |
| 254 | * @param file generated file |
| 255 | * @param className generated file class name |
| 256 | * @param type generated file type |
| 257 | * @param imports imports for the file |
| 258 | * @param pkg generated file package |
| 259 | * @throws IOException when fails to generate a file |
| 260 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 261 | 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] | 262 | String pkg) throws IOException { |
| 263 | try { |
| 264 | file.createNewFile(); |
| 265 | appendContents(file, className, type, imports, pkg); |
| 266 | } catch (IOException e) { |
| 267 | throw new IOException("Failed to create " + file.getName() + " class file."); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Appends the temp files to main files. |
| 273 | * |
| 274 | * @param appendFile temp file |
| 275 | * @param srcFile main file |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 276 | * @throws IOException when fails to append contents |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 277 | */ |
| 278 | public static void appendFileContents(File appendFile, File srcFile) throws IOException { |
| 279 | try { |
| 280 | FileSystemUtil.appendFileContents(appendFile, srcFile); |
| 281 | } catch (IOException e) { |
| 282 | throw new IOException("Failed to append " + appendFile + " in " + srcFile); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Append methods to the generated files. |
| 288 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 289 | * @param file file in which method needs to be appended |
| 290 | * @param method method which needs to be appended |
| 291 | * @exception IOException file operation exceptions |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 292 | */ |
| 293 | private static void appendMethod(File file, String method) throws IOException { |
| 294 | insert(file, method); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Closes the current generated file. |
| 299 | * |
| 300 | * @param fileType generate file type |
| 301 | * @param yangName file name |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 302 | * @return end of class definition string |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 303 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 304 | public static String closeFile(int fileType, String yangName) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 305 | return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Parses attribute info and fetch specific data and creates serialized |
| 310 | * files of it. |
| 311 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 312 | * @param attr attribute info |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 313 | * @param genFileType generated file type |
| 314 | * @param className class name |
| 315 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 316 | public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 317 | |
| 318 | String attrString = ""; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 319 | String builderInterfaceMethodString = ""; |
| 320 | String builderClassMethodString = ""; |
| 321 | String implClassMethodString = ""; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 322 | String getterString = ""; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 323 | className = JavaIdentifierSyntax.getCaptialCase(className); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 324 | |
| 325 | try { |
| 326 | /* |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 327 | * Get the attribute definition and save attributes to temporary |
| 328 | * file. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 329 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 330 | attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), |
| 331 | attr.getImportInfo().getClassInfo(), |
| 332 | attr.getAttributeName()); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 333 | TempDataStore.setTempData(attrString, TempDataStore.TempDataStoreType.ATTRIBUTE, className); |
| 334 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 335 | if ((genFileType & INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 336 | getterString = MethodsGenerator.getGetterString(attr); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 337 | TempDataStore.setTempData(getterString, TempDataStore.TempDataStoreType.GETTER_METHODS, className); |
| 338 | } |
| 339 | |
| 340 | if ((genFileType & BUILDER_INTERFACE_MASK) != 0) { |
| 341 | builderInterfaceMethodString = MethodsGenerator.parseBuilderInterfaceMethodString(attr, className); |
| 342 | TempDataStore.setTempData(builderInterfaceMethodString, |
| 343 | TempDataStore.TempDataStoreType.BUILDER_INTERFACE_METHODS, |
| 344 | className + UtilConstants.BUILDER + UtilConstants.INTERFACE); |
| 345 | } |
| 346 | |
| 347 | if ((genFileType & BUILDER_CLASS_MASK) != 0) { |
| 348 | builderClassMethodString = MethodsGenerator.parseBuilderMethodString(attr, className); |
| 349 | TempDataStore.setTempData(builderClassMethodString, TempDataStore.TempDataStoreType.BUILDER_METHODS, |
| 350 | className + UtilConstants.BUILDER); |
| 351 | } |
| 352 | |
| 353 | if ((genFileType & IMPL_CLASS_MASK) != 0) { |
| 354 | implClassMethodString = MethodsGenerator.parseImplMethodString(attr); |
| 355 | TempDataStore.setTempData(implClassMethodString, TempDataStore.TempDataStoreType.IMPL_METHODS, |
| 356 | className + UtilConstants.IMPL); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 357 | } |
| 358 | } catch (IOException e) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 359 | 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] | 360 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 361 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Appends all the contents into a generated java file. |
| 366 | * |
| 367 | * @param file generated file |
| 368 | * @param fileName generated file name |
| 369 | * @param type generated file type |
| 370 | * @param pkg generated file package |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 371 | * @param importsList list of java imports |
| 372 | * @throws IOException when fails to append contents |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 373 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 374 | 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] | 375 | String pkg) throws IOException { |
| 376 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 377 | if ((type & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 378 | |
| 379 | write(file, fileName, type, JavaDocType.IMPL_CLASS); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 380 | } else if ((type & BUILDER_INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 381 | |
| 382 | write(file, fileName, type, JavaDocType.BUILDER_INTERFACE); |
| 383 | } else { |
| 384 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 385 | if ((type & INTERFACE_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 386 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 387 | insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE); |
| 388 | if (importsList != null) { |
| 389 | insert(file, UtilConstants.NEW_LINE); |
| 390 | for (String imports : importsList) { |
| 391 | insert(file, imports); |
| 392 | } |
| 393 | insert(file, UtilConstants.NEW_LINE); |
| 394 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 395 | write(file, fileName, type, JavaDocType.INTERFACE); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 396 | } else if ((type & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 397 | insert(file, CopyrightHeader.getCopyrightHeader()); |
| 398 | insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE); |
| 399 | if (importsList != null) { |
| 400 | insert(file, UtilConstants.NEW_LINE); |
| 401 | for (String imports : importsList) { |
| 402 | insert(file, imports); |
| 403 | } |
| 404 | insert(file, UtilConstants.NEW_LINE); |
| 405 | } |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 406 | write(file, fileName, type, JavaDocType.BUILDER_CLASS); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Write data to the specific generated file. |
| 413 | * |
| 414 | * @param file generated file |
| 415 | * @param fileName file name |
| 416 | * @param genType generated file type |
| 417 | * @param javaDocType java doc type |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 418 | * @throws IOException when fails to write into a file |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 419 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 420 | 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] | 421 | throws IOException { |
| 422 | |
| 423 | insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName)); |
| 424 | insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName)); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Insert in the generated file. |
| 429 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 430 | * @param file file in which need to be inserted |
| 431 | * @param data data which need to be inserted |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 432 | * @throws IOException when fails to insert into file |
| 433 | */ |
| 434 | public static void insert(File file, String data) throws IOException { |
| 435 | try { |
| 436 | FileSystemUtil.insertStringInFile(file, data); |
| 437 | } catch (IOException e) { |
| 438 | throw new IOException("Failed to insert in " + file + "file"); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Removes temp files. |
| 444 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 445 | * @param file file to be removed |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 446 | */ |
| 447 | public static void clean(File file) { |
| 448 | if (file.exists()) { |
| 449 | file.delete(); |
| 450 | } |
| 451 | } |
| 452 | } |