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