Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 21 | import java.util.ArrayList; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | import java.util.List; |
| 23 | |
| 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 27 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 30 | |
| 31 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 32 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
| 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
| 37 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK; |
| 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK; |
| 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK; |
| 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK; |
| 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK; |
| 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK; |
| 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK; |
| 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK; |
| 45 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK; |
| 46 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfTypeDef; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent; |
| 49 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile; |
| 52 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile; |
| 53 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile; |
| 54 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile; |
| 55 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile; |
| 56 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 58 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 59 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString; |
| 63 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor; |
| 64 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString; |
| 65 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod; |
| 66 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass; |
| 67 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString; |
| 68 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString; |
| 71 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 73 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString; |
| 74 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeDefConstructor; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 77 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 78 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 79 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
| 80 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL; |
| 81 | import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE; |
| 82 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 83 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 84 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 85 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 86 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile; |
| 87 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 88 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
| 89 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 90 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 91 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_CONSTRUCTOR; |
| 92 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD; |
| 93 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean; |
| 94 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
| 95 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 96 | |
| 97 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 98 | * Represents implementation of java code fragments temporary implementations. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 99 | */ |
| 100 | public class TempJavaCodeFragmentFiles { |
| 101 | |
| 102 | /** |
| 103 | * The variable which guides the types of temporary files generated using |
| 104 | * the temporary generated file types mask. |
| 105 | */ |
| 106 | private int generatedTempFiles; |
| 107 | |
| 108 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 109 | * The variable which guides the types of files generated using |
| 110 | * the generated file types mask. |
| 111 | */ |
| 112 | private int generatedJavaFiles; |
| 113 | |
| 114 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 115 | * Absolute path where the target java file needs to be generated. |
| 116 | */ |
| 117 | private String absoluteDirPath; |
| 118 | |
| 119 | /** |
| 120 | * Name of java file that needs to be generated. |
| 121 | */ |
| 122 | private String generatedJavaClassName; |
| 123 | |
| 124 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 125 | * File type extension for java classes. |
| 126 | */ |
| 127 | private static final String JAVA_FILE_EXTENSION = ".java"; |
| 128 | |
| 129 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 130 | * File type extension for temporary classes. |
| 131 | */ |
| 132 | private static final String TEMP_FILE_EXTENSION = ".tmp"; |
| 133 | |
| 134 | /** |
| 135 | * Folder suffix for temporary files folder. |
| 136 | */ |
| 137 | private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp"; |
| 138 | |
| 139 | /** |
| 140 | * File name for getter method. |
| 141 | */ |
| 142 | private static final String GETTER_METHOD_FILE_NAME = "GetterMethod"; |
| 143 | |
| 144 | /** |
| 145 | * File name for getter method implementation. |
| 146 | */ |
| 147 | private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl"; |
| 148 | |
| 149 | /** |
| 150 | * File name for setter method. |
| 151 | */ |
| 152 | private static final String SETTER_METHOD_FILE_NAME = "SetterMethod"; |
| 153 | |
| 154 | /** |
| 155 | * File name for setter method implementation. |
| 156 | */ |
| 157 | private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl"; |
| 158 | |
| 159 | /** |
| 160 | * File name for constructor. |
| 161 | */ |
| 162 | private static final String CONSTRUCTOR_FILE_NAME = "Constructor"; |
| 163 | |
| 164 | /** |
| 165 | * File name for attributes. |
| 166 | */ |
| 167 | private static final String ATTRIBUTE_FILE_NAME = "Attributes"; |
| 168 | |
| 169 | /** |
| 170 | * File name for to string method. |
| 171 | */ |
| 172 | private static final String TO_STRING_METHOD_FILE_NAME = "ToString"; |
| 173 | |
| 174 | /** |
| 175 | * File name for hash code method. |
| 176 | */ |
| 177 | private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode"; |
| 178 | |
| 179 | /** |
| 180 | * File name for equals method. |
| 181 | */ |
| 182 | private static final String EQUALS_METHOD_FILE_NAME = "Equals"; |
| 183 | |
| 184 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 185 | * File name for interface java file name suffix. |
| 186 | */ |
| 187 | private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING; |
| 188 | |
| 189 | /** |
| 190 | * File name for builder interface file name suffix. |
| 191 | */ |
| 192 | private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE; |
| 193 | |
| 194 | /** |
| 195 | * File name for builder class file name suffix. |
| 196 | */ |
| 197 | private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER; |
| 198 | |
| 199 | /** |
| 200 | * File name for impl class file name suffix. |
| 201 | */ |
| 202 | private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL; |
| 203 | |
| 204 | /** |
| 205 | * File name for typedef class file name suffix. |
| 206 | */ |
| 207 | private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING; |
| 208 | |
| 209 | /** |
| 210 | * Java file handle for interface file. |
| 211 | */ |
| 212 | private File interfaceJavaFileHandle; |
| 213 | |
| 214 | /** |
| 215 | * Java file handle for builder interface file. |
| 216 | */ |
| 217 | private File builderInterfaceJavaFileHandle; |
| 218 | |
| 219 | /** |
| 220 | * Java file handle for builder class file. |
| 221 | */ |
| 222 | private File builderClassJavaFileHandle; |
| 223 | |
| 224 | /** |
| 225 | * Java file handle for impl class file. |
| 226 | */ |
| 227 | private File implClassJavaFileHandle; |
| 228 | |
| 229 | /** |
| 230 | * Java file handle for typedef class file. |
| 231 | */ |
| 232 | private File typedefClassJavaFileHandle; |
| 233 | |
| 234 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 235 | * Temporary file handle for attribute. |
| 236 | */ |
| 237 | private File attributesTempFileHandle; |
| 238 | |
| 239 | /** |
| 240 | * Temporary file handle for getter of interface. |
| 241 | */ |
| 242 | private File getterInterfaceTempFileHandle; |
| 243 | |
| 244 | /** |
| 245 | * Temporary file handle for getter of class. |
| 246 | */ |
| 247 | private File getterImplTempFileHandle; |
| 248 | |
| 249 | /** |
| 250 | * Temporary file handle for setter of interface. |
| 251 | */ |
| 252 | private File setterInterfaceTempFileHandle; |
| 253 | |
| 254 | /** |
| 255 | * Temporary file handle for setter of class. |
| 256 | */ |
| 257 | private File setterImplTempFileHandle; |
| 258 | |
| 259 | /** |
| 260 | * Temporary file handle for constructor of class. |
| 261 | */ |
| 262 | private File constructorImplTempFileHandle; |
| 263 | |
| 264 | /** |
| 265 | * Temporary file handle for hash code method of class. |
| 266 | */ |
| 267 | private File hashCodeImplTempFileHandle; |
| 268 | |
| 269 | /** |
| 270 | * Temporary file handle for equals method of class. |
| 271 | */ |
| 272 | private File equalsImplTempFileHandle; |
| 273 | |
| 274 | /** |
| 275 | * Temporary file handle for to string method of class. |
| 276 | */ |
| 277 | private File toStringImplTempFileHandle; |
| 278 | |
| 279 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 280 | * Java attribute info. |
| 281 | */ |
| 282 | private JavaAttributeInfo newAttrInfo; |
| 283 | |
| 284 | /** |
| 285 | * Current YANG node. |
| 286 | */ |
| 287 | private YangNode curYangNode; |
| 288 | |
| 289 | /** |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 290 | * Is attribute added. |
| 291 | */ |
| 292 | private boolean isAttributePresent = false; |
| 293 | |
| 294 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 295 | * Creates an instance of temporary java code fragment. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 296 | * |
| 297 | * @param genFileType file generation type |
| 298 | * @param genDir file generation directory |
| 299 | * @param className class name |
| 300 | * @throws IOException when fails to create new file handle |
| 301 | */ |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 302 | public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className) |
| 303 | throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 304 | |
| 305 | generatedTempFiles = 0; |
| 306 | absoluteDirPath = genDir; |
| 307 | generatedJavaClassName = className; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 308 | generatedJavaFiles = genFileType; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 309 | /** |
| 310 | * Initialize getter when generation file type matches to interface |
| 311 | * mask. |
| 312 | */ |
| 313 | if ((genFileType & INTERFACE_MASK) != 0) { |
| 314 | generatedTempFiles |= GETTER_FOR_INTERFACE_MASK; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Initialize getter and setter when generation file type matches to |
| 319 | * builder interface mask. |
| 320 | */ |
| 321 | if ((genFileType & BUILDER_INTERFACE_MASK) != 0) { |
| 322 | generatedTempFiles |= GETTER_FOR_INTERFACE_MASK; |
| 323 | generatedTempFiles |= SETTER_FOR_INTERFACE_MASK; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Initialize getterImpl, setterImpl and attributes when generation file |
| 328 | * type matches to builder class mask. |
| 329 | */ |
| 330 | if ((genFileType & BUILDER_CLASS_MASK) != 0) { |
| 331 | generatedTempFiles |= ATTRIBUTES_MASK; |
| 332 | generatedTempFiles |= GETTER_FOR_CLASS_MASK; |
| 333 | generatedTempFiles |= SETTER_FOR_CLASS_MASK; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Initialize getterImpl, attributes, constructor, hash code, equals and |
| 338 | * to strings when generation file type matches to impl class mask. |
| 339 | */ |
| 340 | if ((genFileType & IMPL_CLASS_MASK) != 0) { |
| 341 | generatedTempFiles |= ATTRIBUTES_MASK; |
| 342 | generatedTempFiles |= GETTER_FOR_CLASS_MASK; |
| 343 | generatedTempFiles |= CONSTRUCTOR_IMPL_MASK; |
| 344 | generatedTempFiles |= HASH_CODE_IMPL_MASK; |
| 345 | generatedTempFiles |= EQUALS_IMPL_MASK; |
| 346 | generatedTempFiles |= TO_STRING_IMPL_MASK; |
| 347 | } |
| 348 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 349 | /** |
| 350 | * Initialize getterImpl, attributes, hash code, equals and |
| 351 | * to strings when generation file type matches to typeDef class mask. |
| 352 | */ |
| 353 | if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) { |
| 354 | generatedTempFiles |= ATTRIBUTES_MASK; |
| 355 | generatedTempFiles |= GETTER_FOR_CLASS_MASK; |
| 356 | generatedTempFiles |= HASH_CODE_IMPL_MASK; |
| 357 | generatedTempFiles |= EQUALS_IMPL_MASK; |
| 358 | generatedTempFiles |= TO_STRING_IMPL_MASK; |
| 359 | } |
| 360 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 361 | if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) { |
| 362 | setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME)); |
| 363 | } |
| 364 | |
| 365 | if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 366 | setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME)); |
| 367 | } |
| 368 | |
| 369 | if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 370 | setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME)); |
| 371 | } |
| 372 | |
| 373 | if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) { |
| 374 | setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME)); |
| 375 | } |
| 376 | |
| 377 | if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) { |
| 378 | setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME)); |
| 379 | } |
| 380 | |
| 381 | if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) { |
| 382 | setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME)); |
| 383 | } |
| 384 | |
| 385 | if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) { |
| 386 | setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME)); |
| 387 | } |
| 388 | |
| 389 | if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) { |
| 390 | setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME)); |
| 391 | } |
| 392 | if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) { |
| 393 | setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME)); |
| 394 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 398 | * Returns java file handle for interface file. |
| 399 | * |
| 400 | * @return java file handle for interface file |
| 401 | */ |
| 402 | public File getInterfaceJavaFileHandle() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 403 | return interfaceJavaFileHandle; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Sets the java file handle for interface file. |
| 408 | * |
| 409 | * @param interfaceJavaFileHandle java file handle |
| 410 | */ |
| 411 | public void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 412 | this.interfaceJavaFileHandle = interfaceJavaFileHandle; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Returns java file handle for builder interface file. |
| 417 | * |
| 418 | * @return java file handle for builder interface file |
| 419 | */ |
| 420 | public File getBuilderInterfaceJavaFileHandle() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 421 | return builderInterfaceJavaFileHandle; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Sets the java file handle for builder interface file. |
| 426 | * |
| 427 | * @param builderInterfaceJavaFileHandle java file handle |
| 428 | */ |
| 429 | public void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 430 | this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Returns java file handle for builder class file. |
| 435 | * |
| 436 | * @return java file handle for builder class file |
| 437 | */ |
| 438 | public File getBuilderClassJavaFileHandle() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 439 | return builderClassJavaFileHandle; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Sets the java file handle for builder class file. |
| 444 | * |
| 445 | * @param builderClassJavaFileHandle java file handle |
| 446 | */ |
| 447 | public void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 448 | this.builderClassJavaFileHandle = builderClassJavaFileHandle; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Returns java file handle for impl class file. |
| 453 | * |
| 454 | * @return java file handle for impl class file |
| 455 | */ |
| 456 | public File getImplClassJavaFileHandle() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 457 | return implClassJavaFileHandle; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Sets the java file handle for impl class file. |
| 462 | * |
| 463 | * @param implClassJavaFileHandle java file handle |
| 464 | */ |
| 465 | public void setImplClassJavaFileHandle(File implClassJavaFileHandle) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 466 | this.implClassJavaFileHandle = implClassJavaFileHandle; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Returns java file handle for typedef class file. |
| 471 | * |
| 472 | * @return java file handle for typedef class file |
| 473 | */ |
| 474 | public File getTypedefClassJavaFileHandle() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 475 | return typedefClassJavaFileHandle; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Sets the java file handle for typedef class file. |
| 480 | * |
| 481 | * @param typedefClassJavaFileHandle java file handle |
| 482 | */ |
| 483 | public void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 484 | this.typedefClassJavaFileHandle = typedefClassJavaFileHandle; |
| 485 | } |
| 486 | |
| 487 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 488 | * Returns attribute's temporary file handle. |
| 489 | * |
| 490 | * @return temporary file handle |
| 491 | */ |
| 492 | public File getAttributesTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 493 | return attributesTempFileHandle; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Sets attribute's temporary file handle. |
| 498 | * |
| 499 | * @param attributeForClass file handle for attribute |
| 500 | */ |
| 501 | public void setAttributesTempFileHandle(File attributeForClass) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 502 | attributesTempFileHandle = attributeForClass; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Returns getter methods's temporary file handle. |
| 507 | * |
| 508 | * @return temporary file handle |
| 509 | */ |
| 510 | public File getGetterInterfaceTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 511 | return getterInterfaceTempFileHandle; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Sets to getter method's temporary file handle. |
| 516 | * |
| 517 | * @param getterForInterface file handle for to getter method |
| 518 | */ |
| 519 | public void setGetterInterfaceTempFileHandle(File getterForInterface) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 520 | getterInterfaceTempFileHandle = getterForInterface; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Returns getter method's impl's temporary file handle. |
| 525 | * |
| 526 | * @return temporary file handle |
| 527 | */ |
| 528 | public File getGetterImplTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 529 | return getterImplTempFileHandle; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Sets to getter method's impl's temporary file handle. |
| 534 | * |
| 535 | * @param getterImpl file handle for to getter method's impl |
| 536 | */ |
| 537 | public void setGetterImplTempFileHandle(File getterImpl) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 538 | getterImplTempFileHandle = getterImpl; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Returns setter method's temporary file handle. |
| 543 | * |
| 544 | * @return temporary file handle |
| 545 | */ |
| 546 | public File getSetterInterfaceTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 547 | return setterInterfaceTempFileHandle; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Sets to setter method's temporary file handle. |
| 552 | * |
| 553 | * @param setterForInterface file handle for to setter method |
| 554 | */ |
| 555 | public void setSetterInterfaceTempFileHandle(File setterForInterface) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 556 | setterInterfaceTempFileHandle = setterForInterface; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Returns setter method's impl's temporary file handle. |
| 561 | * |
| 562 | * @return temporary file handle |
| 563 | */ |
| 564 | public File getSetterImplTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 565 | return setterImplTempFileHandle; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Sets to setter method's impl's temporary file handle. |
| 570 | * |
| 571 | * @param setterImpl file handle for to setter method's implementation class |
| 572 | */ |
| 573 | public void setSetterImplTempFileHandle(File setterImpl) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 574 | setterImplTempFileHandle = setterImpl; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Returns constructor's temporary file handle. |
| 579 | * |
| 580 | * @return temporary file handle |
| 581 | */ |
| 582 | public File getConstructorImplTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 583 | return constructorImplTempFileHandle; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Sets to constructor's temporary file handle. |
| 588 | * |
| 589 | * @param constructor file handle for to constructor |
| 590 | */ |
| 591 | public void setConstructorImplTempFileHandle(File constructor) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 592 | constructorImplTempFileHandle = constructor; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Returns hash code method's temporary file handle. |
| 597 | * |
| 598 | * @return temporary file handle |
| 599 | */ |
| 600 | public File getHashCodeImplTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 601 | return hashCodeImplTempFileHandle; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Sets hash code method's temporary file handle. |
| 606 | * |
| 607 | * @param hashCodeMethod file handle for hash code method |
| 608 | */ |
| 609 | public void setHashCodeImplTempFileHandle(File hashCodeMethod) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 610 | hashCodeImplTempFileHandle = hashCodeMethod; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Returns equals mehtod's temporary file handle. |
| 615 | * |
| 616 | * @return temporary file handle |
| 617 | */ |
| 618 | public File getEqualsImplTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 619 | return equalsImplTempFileHandle; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Sets equals method's temporary file handle. |
| 624 | * |
| 625 | * @param equalsMethod file handle for to equals method |
| 626 | */ |
| 627 | public void setEqualsImplTempFileHandle(File equalsMethod) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 628 | equalsImplTempFileHandle = equalsMethod; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Returns to string method's temporary file handle. |
| 633 | * |
| 634 | * @return temporary file handle |
| 635 | */ |
| 636 | public File getToStringImplTempFileHandle() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 637 | return toStringImplTempFileHandle; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Sets to string method's temporary file handle. |
| 642 | * |
| 643 | * @param toStringMethod file handle for to string method |
| 644 | */ |
| 645 | public void setToStringImplTempFileHandle(File toStringMethod) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 646 | toStringImplTempFileHandle = toStringMethod; |
| 647 | } |
| 648 | |
| 649 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 650 | * Returns java attribute info. |
| 651 | * |
| 652 | * @return java attribute info |
| 653 | */ |
| 654 | public JavaAttributeInfo getNewAttrInfo() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 655 | return newAttrInfo; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Sets java attribute info. |
| 660 | * |
| 661 | * @param newAttrInfo java attribute info |
| 662 | */ |
| 663 | public void setNewAttrInfo(JavaAttributeInfo newAttrInfo) { |
| 664 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 665 | if (newAttrInfo != null) { |
| 666 | isAttributePresent = true; |
| 667 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 668 | this.newAttrInfo = newAttrInfo; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Returns current YANG node. |
| 673 | * |
| 674 | * @return current YANG node |
| 675 | */ |
| 676 | public YangNode getCurYangNode() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 677 | return curYangNode; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Sets current YANG node. |
| 682 | * |
| 683 | * @param curYangNode YANG node |
| 684 | */ |
| 685 | public void setCurYangNode(YangNode curYangNode) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 686 | this.curYangNode = curYangNode; |
| 687 | } |
| 688 | |
| 689 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 690 | * Adds attribute for class. |
| 691 | * |
| 692 | * @param attr attribute info |
| 693 | * @throws IOException when fails to append to temporary file |
| 694 | */ |
| 695 | public void addAttribute(JavaAttributeInfo attr) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 696 | appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Adds getter for interface. |
| 701 | * |
| 702 | * @param attr attribute info |
| 703 | * @throws IOException when fails to append to temporary file |
| 704 | */ |
| 705 | public void addGetterForInterface(JavaAttributeInfo attr) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 706 | appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr) + NEW_LINE); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Adds getter method's impl for class. |
| 711 | * |
| 712 | * @param attr attribute info |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 713 | * @param genFiletype generated file type |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 714 | * @throws IOException when fails to append to temporary file |
| 715 | */ |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 716 | public void addGetterImpl(JavaAttributeInfo attr, int genFiletype) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 717 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 718 | if ((genFiletype & BUILDER_CLASS_MASK) != 0) { |
| 719 | appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr) + NEW_LINE); |
| 720 | } else { |
| 721 | appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false) |
| 722 | + getGetterForClass(attr) + NEW_LINE); |
| 723 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Adds setter for interface. |
| 728 | * |
| 729 | * @param attr attribute info |
| 730 | * @throws IOException when fails to append to temporary file |
| 731 | */ |
| 732 | public void addSetterForInterface(JavaAttributeInfo attr) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 733 | appendToFile(getSetterInterfaceTempFileHandle(), |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 734 | getSetterString(attr, generatedJavaClassName) + NEW_LINE); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Adds setter's implementation for class. |
| 739 | * |
| 740 | * @param attr attribute info |
| 741 | * @throws IOException when fails to append to temporary file |
| 742 | */ |
| 743 | public void addSetterImpl(JavaAttributeInfo attr) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 744 | appendToFile(getSetterImplTempFileHandle(), |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 745 | getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + NEW_LINE); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /** |
| 749 | * Adds build method for interface. |
| 750 | * |
| 751 | * @return build method for interface |
| 752 | * @throws IOException when fails to append to temporary file |
| 753 | */ |
| 754 | public String addBuildMethodForInterface() throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 755 | return parseBuilderInterfaceBuildMethodString(generatedJavaClassName); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Adds build method's implementation for class. |
| 760 | * |
| 761 | * @return build method implementation for class |
| 762 | * @throws IOException when fails to append to temporary file |
| 763 | */ |
| 764 | public String addBuildMethodImpl() throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 765 | return getBuildString(generatedJavaClassName) + NEW_LINE; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /** |
| 769 | * Adds constructor for class. |
| 770 | * |
| 771 | * @param attr attribute info |
| 772 | * @throws IOException when fails to append to temporary file |
| 773 | */ |
| 774 | public void addConstructor(JavaAttributeInfo attr) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 775 | appendToFile(getConstructorImplTempFileHandle(), getConstructor(generatedJavaClassName, attr)); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Adds default constructor for class. |
| 780 | * |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 781 | * @param modifier modifier for constructor. |
| 782 | * @param toAppend string which need to be appended with the class name |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 783 | * @return default constructor for class |
| 784 | * @throws IOException when fails to append to file |
| 785 | */ |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 786 | public String addDefaultConstructor(String modifier, String toAppend) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 787 | return NEW_LINE + getDefaultConstructorString(generatedJavaClassName + toAppend, modifier); |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Adds typedef constructor for class. |
| 792 | * |
| 793 | * @return typedef constructor for class |
| 794 | * @throws IOException when fails to append to file |
| 795 | */ |
| 796 | public String addTypeDefConstructor() throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 797 | return NEW_LINE + getJavaDoc(TYPE_DEF_CONSTRUCTOR, generatedJavaClassName, false) |
| 798 | + getTypeDefConstructor(newAttrInfo, generatedJavaClassName) + NEW_LINE; |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Adds default constructor for class. |
| 803 | * |
| 804 | * @return default constructor for class |
| 805 | * @throws IOException when fails to append to file |
| 806 | */ |
| 807 | public String addTypeDefsSetter() throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 808 | return getJavaDoc(TYPE_DEF_SETTER_METHOD, generatedJavaClassName, false) + getSetterForTypeDefClass(newAttrInfo) |
| 809 | + NEW_LINE; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Adds default constructor for class. |
| 814 | * |
| 815 | * @return default constructor for class |
| 816 | * @throws IOException when fails to append to file |
| 817 | */ |
| 818 | public String addOfMethod() throws IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 819 | return getJavaDoc(OF_METHOD, generatedJavaClassName, false) |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 820 | + getOfMethod(generatedJavaClassName, newAttrInfo); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Adds hash code method for class. |
| 825 | * |
| 826 | * @param attr attribute info |
| 827 | * @throws IOException when fails to append to temporary file |
| 828 | */ |
| 829 | public void addHashCodeMethod(JavaAttributeInfo attr) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 830 | appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Adds equals method for class. |
| 835 | * |
| 836 | * @param attr attribute info |
| 837 | * @throws IOException when fails to append to temporary file |
| 838 | */ |
| 839 | public void addEqualsMethod(JavaAttributeInfo attr) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 840 | appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Adds ToString method for class. |
| 845 | * |
| 846 | * @param attr attribute info |
| 847 | * @throws IOException when fails to append to temporary file |
| 848 | */ |
| 849 | public void addToStringMethod(JavaAttributeInfo attr) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 850 | appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Returns a temporary file handle for the specific file type. |
| 855 | * |
| 856 | * @param fileName file name |
| 857 | * @return temporary file handle |
| 858 | * @throws IOException when fails to create new file handle |
| 859 | */ |
| 860 | private File getTemporaryFileHandle(String fileName) throws IOException { |
| 861 | |
| 862 | String path = getTempDirPath(); |
| 863 | File dir = new File(path); |
| 864 | if (!dir.exists()) { |
| 865 | dir.mkdirs(); |
| 866 | } |
| 867 | |
| 868 | File file = new File(path + fileName + TEMP_FILE_EXTENSION); |
| 869 | if (!file.exists()) { |
| 870 | file.createNewFile(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 871 | } |
| 872 | return file; |
| 873 | } |
| 874 | |
| 875 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 876 | * Returns a temporary file handle for the specific file type. |
| 877 | * |
| 878 | * @param fileName file name |
| 879 | * @return temporary file handle |
| 880 | * @throws IOException when fails to create new file handle |
| 881 | */ |
| 882 | private File getJavaFileHandle(String fileName) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 883 | createPackage(absoluteDirPath, getJavaFileInfo().getJavaName()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 884 | return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo()); |
| 885 | } |
| 886 | |
| 887 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 888 | * Returns data from the temporary files. |
| 889 | * |
| 890 | * @param file temporary file handle |
| 891 | * @return stored data from temporary files |
| 892 | * @throws IOException when failed to get data from the given file |
| 893 | */ |
| 894 | public String getTemporaryDataFromFileHandle(File file) throws IOException { |
| 895 | |
| 896 | String path = getTempDirPath(); |
| 897 | if (new File(path + file.getName()).exists()) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 898 | return readAppendFile(path + file.getName(), EMPTY_STRING); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 899 | } else { |
| 900 | throw new IOException("Unable to get data from the given " |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 901 | + file.getName() + " file for " + generatedJavaClassName + PERIOD); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
| 905 | /** |
| 906 | * Returns temporary directory path. |
| 907 | * |
| 908 | * @return directory path |
| 909 | */ |
| 910 | private String getTempDirPath() { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 911 | return getPackageDirPathFromJavaJPackage(absoluteDirPath) + SLASH + generatedJavaClassName |
| 912 | + TEMP_FOLDER_NAME_SUFIX + SLASH; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 916 | * Parses attribute to get the attribute string. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 917 | * |
| 918 | * @param attr attribute info |
| 919 | * @return attribute string |
| 920 | */ |
| 921 | private String parseAttribute(JavaAttributeInfo attr) { |
| 922 | |
| 923 | /* |
| 924 | * TODO: check if this utility needs to be called or move to the caller |
| 925 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 926 | String attributeName = getCamelCase(getSmallCase(attr.getAttributeName())); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 927 | if (attr.isQualifiedName()) { |
| 928 | return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(), |
| 929 | attributeName, attr.isListAttr()); |
| 930 | } else { |
| 931 | return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName, |
| 932 | attr.isListAttr()); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 937 | * Appends content to temporary file. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 938 | * |
| 939 | * @param file temporary file |
| 940 | * @param data data to be appended |
| 941 | * @throws IOException when fails to append to file |
| 942 | */ |
| 943 | private void appendToFile(File file, String data) throws IOException { |
| 944 | |
| 945 | try { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 946 | insertDataIntoJavaFile(file, data); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 947 | } catch (IOException ex) { |
| 948 | throw new IOException("failed to write in temp file."); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Adds current node info as and attribute to the parent generated file. |
| 954 | * |
| 955 | * @param curNode current node which needs to be added as an attribute in |
| 956 | * the parent generated code |
| 957 | * @param isList is list construct |
| 958 | * @throws IOException IO operation exception |
| 959 | */ |
| 960 | public void addCurNodeInfoInParentTempFile(YangNode curNode, |
| 961 | boolean isList) throws IOException { |
| 962 | |
| 963 | YangNode parent = getParentNodeInGenCode(curNode); |
| 964 | if (!(parent instanceof JavaCodeGenerator)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 965 | throw new TranslatorException("missing parent node to contain current node info in generated file"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 966 | } |
| 967 | JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode, |
| 968 | parent, isList); |
| 969 | |
| 970 | if (!(parent instanceof HasTempJavaCodeFragmentFiles)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 971 | throw new TranslatorException("missing parent temp file handle"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 972 | } |
| 973 | ((HasTempJavaCodeFragmentFiles) parent) |
| 974 | .getTempJavaCodeFragmentFiles() |
| 975 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Adds leaf attributes in generated files. |
| 980 | * |
| 981 | * @param listOfLeaves list of YANG leaf |
| 982 | * @param curNode current data model node |
| 983 | * @throws IOException IO operation fail |
| 984 | */ |
| 985 | private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves, |
| 986 | YangNode curNode) throws IOException { |
| 987 | |
| 988 | if (listOfLeaves != null) { |
| 989 | for (YangLeaf leaf : listOfLeaves) { |
| 990 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(curNode, |
| 991 | leaf.getDataType(), |
| 992 | leaf.getLeafName(), false); |
| 993 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * Adds leaf list's attributes in generated files. |
| 1000 | * |
| 1001 | * @param listOfLeafList list of YANG leaves |
| 1002 | * @param curNode cached file handle |
| 1003 | * @throws IOException IO operation fail |
| 1004 | */ |
| 1005 | private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, |
| 1006 | YangNode curNode) throws IOException { |
| 1007 | |
| 1008 | if (listOfLeafList != null) { |
| 1009 | |
| 1010 | /* |
| 1011 | * Check if the attribute is of type list, then the java.lang.list |
| 1012 | * needs to be imported. |
| 1013 | */ |
| 1014 | if (listOfLeafList.size() != 0) { |
| 1015 | if (!(curNode instanceof HasJavaImportData)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1016 | throw new TranslatorException("missing import info in current data model node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1017 | |
| 1018 | } |
| 1019 | ((HasJavaImportData) curNode).getJavaImportData() |
| 1020 | .setIfListImported(true); |
| 1021 | |
| 1022 | } |
| 1023 | |
| 1024 | for (YangLeafList leafList : listOfLeafList) { |
| 1025 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf( |
| 1026 | curNode, leafList.getDataType(), leafList.getLeafName(), |
| 1027 | true); |
| 1028 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * Add all the leaves in the current data model node as part of the |
| 1035 | * generated temporary file. |
| 1036 | * |
| 1037 | * @param curNode java file info of the generated file |
| 1038 | * @throws IOException IO operation fail |
| 1039 | */ |
| 1040 | public void addCurNodeLeavesInfoToTempFiles(YangNode curNode) throws IOException { |
| 1041 | |
| 1042 | if (curNode instanceof YangLeavesHolder) { |
| 1043 | YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode; |
| 1044 | addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), curNode); |
| 1045 | addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), curNode); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1050 | * Adds leaf attributes in generated files. |
| 1051 | * |
| 1052 | * @param curNode current data model node |
| 1053 | * @throws IOException IO operation fail |
| 1054 | */ |
| 1055 | public void addTypeDefAttributeToTempFiles(YangNode curNode) throws IOException { |
| 1056 | |
| 1057 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfTypeDef(curNode, |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 1058 | ((YangTypeDef) curNode).getDataType(), |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1059 | ((YangTypeDef) curNode).getName(), false); |
| 1060 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 1064 | * Adds the new attribute info to the target generated temporary files. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1065 | * |
| 1066 | * @param newAttrInfo the attribute info that needs to be added to temporary |
| 1067 | * files |
| 1068 | * @throws IOException IO operation fail |
| 1069 | */ |
| 1070 | void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo) |
| 1071 | throws IOException { |
| 1072 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1073 | setNewAttrInfo(newAttrInfo); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1074 | if (isAttributePresent) { |
| 1075 | if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) { |
| 1076 | addAttribute(newAttrInfo); |
| 1077 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1078 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1079 | if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 1080 | addGetterForInterface(newAttrInfo); |
| 1081 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1082 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1083 | if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 1084 | addSetterForInterface(newAttrInfo); |
| 1085 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1086 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1087 | if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) { |
| 1088 | addGetterImpl(newAttrInfo, generatedJavaFiles); |
| 1089 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1090 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1091 | if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) { |
| 1092 | addSetterImpl(newAttrInfo); |
| 1093 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1094 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1095 | if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) { |
| 1096 | addConstructor(newAttrInfo); |
| 1097 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1098 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1099 | if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) { |
| 1100 | addHashCodeMethod(newAttrInfo); |
| 1101 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1102 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1103 | if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) { |
| 1104 | addEqualsMethod(newAttrInfo); |
| 1105 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1106 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1107 | if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) { |
| 1108 | addToStringMethod(newAttrInfo); |
| 1109 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1110 | } |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 1115 | * Returns java file info. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1116 | * |
| 1117 | * @return java file info |
| 1118 | */ |
| 1119 | private JavaFileInfo getJavaFileInfo() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1120 | return ((HasJavaFileInfo) getCurYangNode()).getJavaFileInfo(); |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * Returns java class name. |
| 1125 | * |
| 1126 | * @param suffix for the class name based on the file type |
| 1127 | * @return java class name |
| 1128 | */ |
| 1129 | private String getJavaClassName(String suffix) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1130 | return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * Returns the directory path. |
| 1135 | * |
| 1136 | * @return directory path |
| 1137 | */ |
| 1138 | private String getDirPath() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1139 | return getJavaFileInfo().getPackageFilePath(); |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * Construct java code exit. |
| 1144 | * |
| 1145 | * @param fileType generated file type |
| 1146 | * @param curNode current YANG node |
| 1147 | * @throws IOException when fails to generate java files |
| 1148 | */ |
| 1149 | public void generateJavaFile(int fileType, YangNode curNode) throws IOException { |
| 1150 | |
| 1151 | setCurYangNode(curNode); |
| 1152 | List<String> imports = new ArrayList<>(); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1153 | if (curNode instanceof HasJavaImportData && isAttributePresent) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1154 | imports = ((HasJavaImportData) curNode).getJavaImportData().getImports(getNewAttrInfo()); |
| 1155 | } |
| 1156 | /** |
| 1157 | * Start generation of files. |
| 1158 | */ |
| 1159 | if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0 |
| 1160 | | fileType == GENERATE_INTERFACE_WITH_BUILDER) { |
| 1161 | |
| 1162 | /** |
| 1163 | * Create interface file. |
| 1164 | */ |
| 1165 | setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX))); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1166 | setInterfaceJavaFileHandle( |
| 1167 | generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1168 | /** |
| 1169 | * Create builder interface file. |
| 1170 | */ |
| 1171 | setBuilderInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX))); |
| 1172 | setBuilderInterfaceJavaFileHandle( |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1173 | generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, isAttributePresent)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1174 | /** |
| 1175 | * Append builder interface file to interface file and close it. |
| 1176 | */ |
| 1177 | mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle()); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1178 | insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1179 | |
| 1180 | } |
| 1181 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1182 | if (curNode instanceof HasJavaImportData && isAttributePresent) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1183 | imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForHashAndEquals()); |
| 1184 | imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForToString()); |
| 1185 | java.util.Collections.sort(imports); |
| 1186 | } |
| 1187 | |
| 1188 | if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0 |
| 1189 | | fileType == GENERATE_INTERFACE_WITH_BUILDER) { |
| 1190 | |
| 1191 | /** |
| 1192 | * Create builder class file. |
| 1193 | */ |
| 1194 | setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX))); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1195 | setBuilderClassJavaFileHandle( |
| 1196 | generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1197 | /** |
| 1198 | * Create impl class file. |
| 1199 | */ |
| 1200 | setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX))); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1201 | setImplClassJavaFileHandle( |
| 1202 | generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent)); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1203 | /** |
| 1204 | * Append impl class to builder class and close it. |
| 1205 | */ |
| 1206 | mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle()); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1207 | insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | /** |
| 1211 | * Creates type def class file. |
| 1212 | */ |
| 1213 | if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) { |
| 1214 | setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX))); |
| 1215 | setTypedefClassJavaFileHandle(generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports)); |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * Close all the file handles. |
| 1220 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1221 | close(false); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1225 | * Removes all temporary file handles. |
| 1226 | * |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1227 | * @param isErrorOccurred when translator fails to generate java files we need to close |
| 1228 | * all open file handles include temporary files and java files. |
| 1229 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1230 | * @throws IOException when failed to delete the temporary files |
| 1231 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1232 | public void close(boolean isErrorOccurred) throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 1233 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1234 | boolean isError = isErrorOccurred; |
| 1235 | /** |
| 1236 | * Close all java file handles and when error occurs delete the files. |
| 1237 | */ |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1238 | if ((generatedJavaFiles & INTERFACE_MASK) != 0) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1239 | closeFile(getInterfaceJavaFileHandle(), isError); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1240 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1241 | if ((generatedJavaFiles & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1242 | closeFile(getBuilderClassJavaFileHandle(), isError); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1243 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1244 | if ((generatedJavaFiles & BUILDER_INTERFACE_MASK) != 0) { |
| 1245 | closeFile(getBuilderInterfaceJavaFileHandle(), true); |
| 1246 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1247 | if ((generatedJavaFiles & IMPL_CLASS_MASK) != 0) { |
| 1248 | closeFile(getImplClassJavaFileHandle(), true); |
| 1249 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1250 | if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1251 | closeFile(getTypedefClassJavaFileHandle(), isError); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1252 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1253 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1254 | /** |
| 1255 | * Close all temporary file handles and delete the files. |
| 1256 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1257 | if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 1258 | closeFile(getGetterInterfaceTempFileHandle(), true); |
| 1259 | } |
| 1260 | if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) { |
| 1261 | closeFile(getGetterImplTempFileHandle(), true); |
| 1262 | } |
| 1263 | if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 1264 | closeFile(getSetterInterfaceTempFileHandle(), true); |
| 1265 | } |
| 1266 | if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) { |
| 1267 | closeFile(getSetterImplTempFileHandle(), true); |
| 1268 | } |
| 1269 | if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) { |
| 1270 | closeFile(getConstructorImplTempFileHandle(), true); |
| 1271 | } |
| 1272 | if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) { |
| 1273 | closeFile(getAttributesTempFileHandle(), true); |
| 1274 | } |
| 1275 | if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) { |
| 1276 | closeFile(getHashCodeImplTempFileHandle(), true); |
| 1277 | } |
| 1278 | if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) { |
| 1279 | closeFile(getToStringImplTempFileHandle(), true); |
| 1280 | } |
| 1281 | if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) { |
| 1282 | closeFile(getEqualsImplTempFileHandle(), true); |
| 1283 | } |
| 1284 | |
| 1285 | clean(getTempDirPath()); |
| 1286 | generatedTempFiles = 0; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * Closes the file handle for temporary file. |
| 1291 | * |
| 1292 | * @param fileName temporary file's name |
| 1293 | * @throws IOException when failed to close the file handle |
| 1294 | */ |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1295 | private void closeFile(File file, boolean toBeDeleted) throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1296 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 1297 | if (file != null) { |
| 1298 | updateFileHandle(file, null, true); |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1299 | if (toBeDeleted) { |
| 1300 | file.delete(); |
| 1301 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 1302 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1303 | } |
| 1304 | } |