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