Vinod Kumar S | 3804650 | 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; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 24 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 26 | import org.onosproject.yangutils.datamodel.YangNode; |
| 27 | import org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator; |
| 28 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
| 29 | import org.onosproject.yangutils.utils.UtilConstants; |
| 30 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
| 31 | |
| 32 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
| 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; |
| 46 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent; |
| 47 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination; |
| 48 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
| 49 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString; |
| 50 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor; |
| 51 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString; |
| 52 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod; |
| 53 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass; |
| 54 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString; |
| 55 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod; |
| 56 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString; |
| 57 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass; |
| 58 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString; |
| 59 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod; |
| 60 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString; |
| 61 | |
| 62 | /** |
| 63 | * Provides implementation of java code fragments temporary implementations. |
| 64 | */ |
| 65 | public class TempJavaCodeFragmentFiles { |
| 66 | |
| 67 | /** |
| 68 | * The variable which guides the types of temporary files generated using |
| 69 | * the temporary generated file types mask. |
| 70 | */ |
| 71 | private int generatedTempFiles; |
| 72 | |
| 73 | /** |
| 74 | * Absolute path where the target java file needs to be generated. |
| 75 | */ |
| 76 | private String absoluteDirPath; |
| 77 | |
| 78 | /** |
| 79 | * Name of java file that needs to be generated. |
| 80 | */ |
| 81 | private String generatedJavaClassName; |
| 82 | |
| 83 | /** |
| 84 | * File type extension for temporary classes. |
| 85 | */ |
| 86 | private static final String TEMP_FILE_EXTENSION = ".tmp"; |
| 87 | |
| 88 | /** |
| 89 | * Folder suffix for temporary files folder. |
| 90 | */ |
| 91 | private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp"; |
| 92 | |
| 93 | /** |
| 94 | * File name for getter method. |
| 95 | */ |
| 96 | private static final String GETTER_METHOD_FILE_NAME = "GetterMethod"; |
| 97 | |
| 98 | /** |
| 99 | * File name for getter method implementation. |
| 100 | */ |
| 101 | private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl"; |
| 102 | |
| 103 | /** |
| 104 | * File name for setter method. |
| 105 | */ |
| 106 | private static final String SETTER_METHOD_FILE_NAME = "SetterMethod"; |
| 107 | |
| 108 | /** |
| 109 | * File name for setter method implementation. |
| 110 | */ |
| 111 | private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl"; |
| 112 | |
| 113 | /** |
| 114 | * File name for constructor. |
| 115 | */ |
| 116 | private static final String CONSTRUCTOR_FILE_NAME = "Constructor"; |
| 117 | |
| 118 | /** |
| 119 | * File name for attributes. |
| 120 | */ |
| 121 | private static final String ATTRIBUTE_FILE_NAME = "Attributes"; |
| 122 | |
| 123 | /** |
| 124 | * File name for to string method. |
| 125 | */ |
| 126 | private static final String TO_STRING_METHOD_FILE_NAME = "ToString"; |
| 127 | |
| 128 | /** |
| 129 | * File name for hash code method. |
| 130 | */ |
| 131 | private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode"; |
| 132 | |
| 133 | /** |
| 134 | * File name for equals method. |
| 135 | */ |
| 136 | private static final String EQUALS_METHOD_FILE_NAME = "Equals"; |
| 137 | |
| 138 | /** |
| 139 | * Temporary file handle for attribute. |
| 140 | */ |
| 141 | private File attributesTempFileHandle; |
| 142 | |
| 143 | /** |
| 144 | * Temporary file handle for getter of interface. |
| 145 | */ |
| 146 | private File getterInterfaceTempFileHandle; |
| 147 | |
| 148 | /** |
| 149 | * Temporary file handle for getter of class. |
| 150 | */ |
| 151 | private File getterImplTempFileHandle; |
| 152 | |
| 153 | /** |
| 154 | * Temporary file handle for setter of interface. |
| 155 | */ |
| 156 | private File setterInterfaceTempFileHandle; |
| 157 | |
| 158 | /** |
| 159 | * Temporary file handle for setter of class. |
| 160 | */ |
| 161 | private File setterImplTempFileHandle; |
| 162 | |
| 163 | /** |
| 164 | * Temporary file handle for constructor of class. |
| 165 | */ |
| 166 | private File constructorImplTempFileHandle; |
| 167 | |
| 168 | /** |
| 169 | * Temporary file handle for hash code method of class. |
| 170 | */ |
| 171 | private File hashCodeImplTempFileHandle; |
| 172 | |
| 173 | /** |
| 174 | * Temporary file handle for equals method of class. |
| 175 | */ |
| 176 | private File equalsImplTempFileHandle; |
| 177 | |
| 178 | /** |
| 179 | * Temporary file handle for to string method of class. |
| 180 | */ |
| 181 | private File toStringImplTempFileHandle; |
| 182 | |
| 183 | /** |
| 184 | * Construct an object of temporary java code fragment. |
| 185 | * |
| 186 | * @param genFileType file generation type |
| 187 | * @param genDir file generation directory |
| 188 | * @param className class name |
| 189 | * @throws IOException when fails to create new file handle |
| 190 | */ |
| 191 | public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className) throws IOException { |
| 192 | |
| 193 | generatedTempFiles = 0; |
| 194 | absoluteDirPath = genDir; |
| 195 | generatedJavaClassName = className; |
| 196 | |
| 197 | /** |
| 198 | * Initialize getter when generation file type matches to interface |
| 199 | * mask. |
| 200 | */ |
| 201 | if ((genFileType & INTERFACE_MASK) != 0) { |
| 202 | generatedTempFiles |= GETTER_FOR_INTERFACE_MASK; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Initialize getter and setter when generation file type matches to |
| 207 | * builder interface mask. |
| 208 | */ |
| 209 | if ((genFileType & BUILDER_INTERFACE_MASK) != 0) { |
| 210 | generatedTempFiles |= GETTER_FOR_INTERFACE_MASK; |
| 211 | generatedTempFiles |= SETTER_FOR_INTERFACE_MASK; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Initialize getterImpl, setterImpl and attributes when generation file |
| 216 | * type matches to builder class mask. |
| 217 | */ |
| 218 | if ((genFileType & BUILDER_CLASS_MASK) != 0) { |
| 219 | generatedTempFiles |= ATTRIBUTES_MASK; |
| 220 | generatedTempFiles |= GETTER_FOR_CLASS_MASK; |
| 221 | generatedTempFiles |= SETTER_FOR_CLASS_MASK; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Initialize getterImpl, attributes, constructor, hash code, equals and |
| 226 | * to strings when generation file type matches to impl class mask. |
| 227 | */ |
| 228 | if ((genFileType & IMPL_CLASS_MASK) != 0) { |
| 229 | generatedTempFiles |= ATTRIBUTES_MASK; |
| 230 | generatedTempFiles |= GETTER_FOR_CLASS_MASK; |
| 231 | generatedTempFiles |= CONSTRUCTOR_IMPL_MASK; |
| 232 | generatedTempFiles |= HASH_CODE_IMPL_MASK; |
| 233 | generatedTempFiles |= EQUALS_IMPL_MASK; |
| 234 | generatedTempFiles |= TO_STRING_IMPL_MASK; |
| 235 | } |
| 236 | |
| 237 | if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) { |
| 238 | setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME)); |
| 239 | } |
| 240 | |
| 241 | if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 242 | setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME)); |
| 243 | } |
| 244 | |
| 245 | if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 246 | setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME)); |
| 247 | } |
| 248 | |
| 249 | if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) { |
| 250 | setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME)); |
| 251 | } |
| 252 | |
| 253 | if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) { |
| 254 | setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME)); |
| 255 | } |
| 256 | |
| 257 | if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) { |
| 258 | setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME)); |
| 259 | } |
| 260 | |
| 261 | if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) { |
| 262 | setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME)); |
| 263 | } |
| 264 | |
| 265 | if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) { |
| 266 | setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME)); |
| 267 | } |
| 268 | if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) { |
| 269 | setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME)); |
| 270 | } |
| 271 | |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Returns attribute's temporary file handle. |
| 276 | * |
| 277 | * @return temporary file handle |
| 278 | */ |
| 279 | public File getAttributesTempFileHandle() { |
| 280 | |
| 281 | return attributesTempFileHandle; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Sets attribute's temporary file handle. |
| 286 | * |
| 287 | * @param attributeForClass file handle for attribute |
| 288 | */ |
| 289 | public void setAttributesTempFileHandle(File attributeForClass) { |
| 290 | |
| 291 | attributesTempFileHandle = attributeForClass; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Returns getter methods's temporary file handle. |
| 296 | * |
| 297 | * @return temporary file handle |
| 298 | */ |
| 299 | public File getGetterInterfaceTempFileHandle() { |
| 300 | |
| 301 | return getterInterfaceTempFileHandle; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Sets to getter method's temporary file handle. |
| 306 | * |
| 307 | * @param getterForInterface file handle for to getter method |
| 308 | */ |
| 309 | public void setGetterInterfaceTempFileHandle(File getterForInterface) { |
| 310 | |
| 311 | getterInterfaceTempFileHandle = getterForInterface; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Returns getter method's impl's temporary file handle. |
| 316 | * |
| 317 | * @return temporary file handle |
| 318 | */ |
| 319 | public File getGetterImplTempFileHandle() { |
| 320 | |
| 321 | return getterImplTempFileHandle; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Sets to getter method's impl's temporary file handle. |
| 326 | * |
| 327 | * @param getterImpl file handle for to getter method's impl |
| 328 | */ |
| 329 | public void setGetterImplTempFileHandle(File getterImpl) { |
| 330 | |
| 331 | getterImplTempFileHandle = getterImpl; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Returns setter method's temporary file handle. |
| 336 | * |
| 337 | * @return temporary file handle |
| 338 | */ |
| 339 | public File getSetterInterfaceTempFileHandle() { |
| 340 | |
| 341 | return setterInterfaceTempFileHandle; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Sets to setter method's temporary file handle. |
| 346 | * |
| 347 | * @param setterForInterface file handle for to setter method |
| 348 | */ |
| 349 | public void setSetterInterfaceTempFileHandle(File setterForInterface) { |
| 350 | |
| 351 | setterInterfaceTempFileHandle = setterForInterface; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Returns setter method's impl's temporary file handle. |
| 356 | * |
| 357 | * @return temporary file handle |
| 358 | */ |
| 359 | public File getSetterImplTempFileHandle() { |
| 360 | |
| 361 | return setterImplTempFileHandle; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Sets to setter method's impl's temporary file handle. |
| 366 | * |
| 367 | * @param setterImpl file handle for to setter method's implementation class |
| 368 | */ |
| 369 | public void setSetterImplTempFileHandle(File setterImpl) { |
| 370 | |
| 371 | setterImplTempFileHandle = setterImpl; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Returns constructor's temporary file handle. |
| 376 | * |
| 377 | * @return temporary file handle |
| 378 | */ |
| 379 | public File getConstructorImplTempFileHandle() { |
| 380 | |
| 381 | return constructorImplTempFileHandle; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Sets to constructor's temporary file handle. |
| 386 | * |
| 387 | * @param constructor file handle for to constructor |
| 388 | */ |
| 389 | public void setConstructorImplTempFileHandle(File constructor) { |
| 390 | |
| 391 | constructorImplTempFileHandle = constructor; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Returns hash code method's temporary file handle. |
| 396 | * |
| 397 | * @return temporary file handle |
| 398 | */ |
| 399 | public File getHashCodeImplTempFileHandle() { |
| 400 | |
| 401 | return hashCodeImplTempFileHandle; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Sets hash code method's temporary file handle. |
| 406 | * |
| 407 | * @param hashCodeMethod file handle for hash code method |
| 408 | */ |
| 409 | public void setHashCodeImplTempFileHandle(File hashCodeMethod) { |
| 410 | |
| 411 | hashCodeImplTempFileHandle = hashCodeMethod; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Returns equals mehtod's temporary file handle. |
| 416 | * |
| 417 | * @return temporary file handle |
| 418 | */ |
| 419 | public File getEqualsImplTempFileHandle() { |
| 420 | |
| 421 | return equalsImplTempFileHandle; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Sets equals method's temporary file handle. |
| 426 | * |
| 427 | * @param equalsMethod file handle for to equals method |
| 428 | */ |
| 429 | public void setEqualsImplTempFileHandle(File equalsMethod) { |
| 430 | |
| 431 | equalsImplTempFileHandle = equalsMethod; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Returns to string method's temporary file handle. |
| 436 | * |
| 437 | * @return temporary file handle |
| 438 | */ |
| 439 | public File getToStringImplTempFileHandle() { |
| 440 | |
| 441 | return toStringImplTempFileHandle; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Sets to string method's temporary file handle. |
| 446 | * |
| 447 | * @param toStringMethod file handle for to string method |
| 448 | */ |
| 449 | public void setToStringImplTempFileHandle(File toStringMethod) { |
| 450 | |
| 451 | toStringImplTempFileHandle = toStringMethod; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Adds attribute for class. |
| 456 | * |
| 457 | * @param attr attribute info |
| 458 | * @throws IOException when fails to append to temporary file |
| 459 | */ |
| 460 | public void addAttribute(JavaAttributeInfo attr) throws IOException { |
| 461 | |
| 462 | appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + UtilConstants.FOUR_SPACE_INDENTATION); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Adds getter for interface. |
| 467 | * |
| 468 | * @param attr attribute info |
| 469 | * @throws IOException when fails to append to temporary file |
| 470 | */ |
| 471 | public void addGetterForInterface(JavaAttributeInfo attr) throws IOException { |
| 472 | |
| 473 | appendToFile(getGetterInterfaceTempFileHandle(), |
| 474 | getGetterString(attr) + UtilConstants.NEW_LINE); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Adds getter method's impl for class. |
| 479 | * |
| 480 | * @param attr attribute info |
| 481 | * @throws IOException when fails to append to temporary file |
| 482 | */ |
| 483 | public void addGetterImpl(JavaAttributeInfo attr) throws IOException { |
| 484 | |
| 485 | appendToFile(getGetterImplTempFileHandle(), |
| 486 | getOverRideString() + getGetterForClass(attr) + UtilConstants.NEW_LINE); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Adds setter for interface. |
| 491 | * |
| 492 | * @param attr attribute info |
| 493 | * @throws IOException when fails to append to temporary file |
| 494 | */ |
| 495 | public void addSetterForInterface(JavaAttributeInfo attr) throws IOException { |
| 496 | |
| 497 | appendToFile(getSetterInterfaceTempFileHandle(), |
| 498 | getSetterString(attr, generatedJavaClassName) + UtilConstants.NEW_LINE); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Adds setter's implementation for class. |
| 503 | * |
| 504 | * @param attr attribute info |
| 505 | * @throws IOException when fails to append to temporary file |
| 506 | */ |
| 507 | public void addSetterImpl(JavaAttributeInfo attr) throws IOException { |
| 508 | |
| 509 | appendToFile(getSetterImplTempFileHandle(), |
| 510 | getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + UtilConstants.NEW_LINE); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Adds build method for interface. |
| 515 | * |
| 516 | * @return build method for interface |
| 517 | * @throws IOException when fails to append to temporary file |
| 518 | */ |
| 519 | public String addBuildMethodForInterface() throws IOException { |
| 520 | |
| 521 | return parseBuilderInterfaceBuildMethodString(generatedJavaClassName); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Adds build method's implementation for class. |
| 526 | * |
| 527 | * @return build method implementation for class |
| 528 | * @throws IOException when fails to append to temporary file |
| 529 | */ |
| 530 | public String addBuildMethodImpl() throws IOException { |
| 531 | |
| 532 | return getBuildString(generatedJavaClassName) + UtilConstants.NEW_LINE; |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Adds constructor for class. |
| 537 | * |
| 538 | * @param attr attribute info |
| 539 | * @throws IOException when fails to append to temporary file |
| 540 | */ |
| 541 | public void addConstructor(JavaAttributeInfo attr) throws IOException { |
| 542 | |
| 543 | appendToFile(getConstructorImplTempFileHandle(), getConstructor(generatedJavaClassName, attr)); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Adds default constructor for class. |
| 548 | * |
| 549 | * @return default constructor for class |
| 550 | * @throws IOException when fails to append to file |
| 551 | */ |
| 552 | public String addDefaultConstructor() throws IOException { |
| 553 | |
| 554 | return UtilConstants.NEW_LINE + getDefaultConstructorString(generatedJavaClassName + UtilConstants.BUILDER, |
| 555 | UtilConstants.PUBLIC); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Adds hash code method for class. |
| 560 | * |
| 561 | * @param attr attribute info |
| 562 | * @throws IOException when fails to append to temporary file |
| 563 | */ |
| 564 | public void addHashCodeMethod(JavaAttributeInfo attr) throws IOException { |
| 565 | |
| 566 | appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + UtilConstants.NEW_LINE); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Adds equals method for class. |
| 571 | * |
| 572 | * @param attr attribute info |
| 573 | * @throws IOException when fails to append to temporary file |
| 574 | */ |
| 575 | public void addEqualsMethod(JavaAttributeInfo attr) throws IOException { |
| 576 | |
| 577 | appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + UtilConstants.NEW_LINE); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Adds ToString method for class. |
| 582 | * |
| 583 | * @param attr attribute info |
| 584 | * @throws IOException when fails to append to temporary file |
| 585 | */ |
| 586 | public void addToStringMethod(JavaAttributeInfo attr) throws IOException { |
| 587 | |
| 588 | appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + UtilConstants.NEW_LINE); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Returns a temporary file handle for the specific file type. |
| 593 | * |
| 594 | * @param fileName file name |
| 595 | * @return temporary file handle |
| 596 | * @throws IOException when fails to create new file handle |
| 597 | */ |
| 598 | private File getTemporaryFileHandle(String fileName) throws IOException { |
| 599 | |
| 600 | String path = getTempDirPath(); |
| 601 | File dir = new File(path); |
| 602 | if (!dir.exists()) { |
| 603 | dir.mkdirs(); |
| 604 | } |
| 605 | |
| 606 | File file = new File(path + fileName + TEMP_FILE_EXTENSION); |
| 607 | if (!file.exists()) { |
| 608 | file.createNewFile(); |
| 609 | } else { |
| 610 | file.delete(); |
| 611 | file.createNewFile(); |
| 612 | } |
| 613 | return file; |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Returns data from the temporary files. |
| 618 | * |
| 619 | * @param file temporary file handle |
| 620 | * @return stored data from temporary files |
| 621 | * @throws IOException when failed to get data from the given file |
| 622 | */ |
| 623 | public String getTemporaryDataFromFileHandle(File file) throws IOException { |
| 624 | |
| 625 | String path = getTempDirPath(); |
| 626 | if (new File(path + file.getName()).exists()) { |
| 627 | return FileSystemUtil.readAppendFile(path + file.getName(), UtilConstants.EMPTY_STRING); |
| 628 | } else { |
| 629 | throw new IOException("Unable to get data from the given " |
| 630 | + file.getName() + " file for " |
| 631 | + generatedJavaClassName + UtilConstants.PERIOD); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Returns temporary directory path. |
| 637 | * |
| 638 | * @return directory path |
| 639 | */ |
| 640 | private String getTempDirPath() { |
| 641 | |
| 642 | return absoluteDirPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH) |
| 643 | + File.separator + generatedJavaClassName + TEMP_FOLDER_NAME_SUFIX + File.separator; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Parse attribute to get the attribute string. |
| 648 | * |
| 649 | * @param attr attribute info |
| 650 | * @return attribute string |
| 651 | */ |
| 652 | private String parseAttribute(JavaAttributeInfo attr) { |
| 653 | |
| 654 | /* |
| 655 | * TODO: check if this utility needs to be called or move to the caller |
| 656 | */ |
| 657 | String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName()); |
| 658 | if (attr.isQualifiedName()) { |
| 659 | return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(), |
| 660 | attributeName, attr.isListAttr()); |
| 661 | } else { |
| 662 | return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName, |
| 663 | attr.isListAttr()); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Append content to temporary file. |
| 669 | * |
| 670 | * @param file temporary file |
| 671 | * @param data data to be appended |
| 672 | * @throws IOException when fails to append to file |
| 673 | */ |
| 674 | private void appendToFile(File file, String data) throws IOException { |
| 675 | |
| 676 | try { |
| 677 | JavaFileGenerator.insert(file, data); |
| 678 | } catch (IOException ex) { |
| 679 | throw new IOException("failed to write in temp file."); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Adds current node info as and attribute to the parent generated file. |
| 685 | * |
| 686 | * @param curNode current node which needs to be added as an attribute in |
| 687 | * the parent generated code |
| 688 | * @param isList is list construct |
| 689 | * @throws IOException IO operation exception |
| 690 | */ |
| 691 | public void addCurNodeInfoInParentTempFile(YangNode curNode, |
| 692 | boolean isList) throws IOException { |
| 693 | |
| 694 | YangNode parent = getParentNodeInGenCode(curNode); |
| 695 | if (!(parent instanceof JavaCodeGenerator)) { |
| 696 | throw new RuntimeException("missing parent node to contain current node info in generated file"); |
| 697 | } |
| 698 | JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode, |
| 699 | parent, isList); |
| 700 | |
| 701 | if (!(parent instanceof HasTempJavaCodeFragmentFiles)) { |
| 702 | throw new RuntimeException("missing parent temp file handle"); |
| 703 | } |
| 704 | ((HasTempJavaCodeFragmentFiles) parent) |
| 705 | .getTempJavaCodeFragmentFiles() |
| 706 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Adds leaf attributes in generated files. |
| 711 | * |
| 712 | * @param listOfLeaves list of YANG leaf |
| 713 | * @param curNode current data model node |
| 714 | * @throws IOException IO operation fail |
| 715 | */ |
| 716 | private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves, |
| 717 | YangNode curNode) throws IOException { |
| 718 | |
| 719 | if (listOfLeaves != null) { |
| 720 | for (YangLeaf leaf : listOfLeaves) { |
| 721 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(curNode, |
| 722 | leaf.getDataType(), |
| 723 | leaf.getLeafName(), false); |
| 724 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Adds leaf list's attributes in generated files. |
| 731 | * |
| 732 | * @param listOfLeafList list of YANG leaves |
| 733 | * @param curNode cached file handle |
| 734 | * @throws IOException IO operation fail |
| 735 | */ |
| 736 | private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, |
| 737 | YangNode curNode) throws IOException { |
| 738 | |
| 739 | if (listOfLeafList != null) { |
| 740 | |
| 741 | /* |
| 742 | * Check if the attribute is of type list, then the java.lang.list |
| 743 | * needs to be imported. |
| 744 | */ |
| 745 | if (listOfLeafList.size() != 0) { |
| 746 | if (!(curNode instanceof HasJavaImportData)) { |
| 747 | throw new RuntimeException("missing import info in current data model node"); |
| 748 | |
| 749 | } |
| 750 | ((HasJavaImportData) curNode).getJavaImportData() |
| 751 | .setIfListImported(true); |
| 752 | |
| 753 | } |
| 754 | |
| 755 | for (YangLeafList leafList : listOfLeafList) { |
| 756 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf( |
| 757 | curNode, leafList.getDataType(), leafList.getLeafName(), |
| 758 | true); |
| 759 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Add all the leaves in the current data model node as part of the |
| 766 | * generated temporary file. |
| 767 | * |
| 768 | * @param curNode java file info of the generated file |
| 769 | * @throws IOException IO operation fail |
| 770 | */ |
| 771 | public void addCurNodeLeavesInfoToTempFiles(YangNode curNode) throws IOException { |
| 772 | |
| 773 | if (curNode instanceof YangLeavesHolder) { |
| 774 | YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode; |
| 775 | addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), curNode); |
| 776 | addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), curNode); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Add the new attribute info to the target generated temporary files. |
| 782 | * |
| 783 | * @param newAttrInfo the attribute info that needs to be added to temporary |
| 784 | * files |
| 785 | * @throws IOException IO operation fail |
| 786 | */ |
| 787 | void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo) |
| 788 | throws IOException { |
| 789 | |
| 790 | if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) { |
| 791 | addAttribute(newAttrInfo); |
| 792 | } |
| 793 | |
| 794 | if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) { |
| 795 | addGetterForInterface(newAttrInfo); |
| 796 | } |
| 797 | |
| 798 | if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) { |
| 799 | addSetterForInterface(newAttrInfo); |
| 800 | } |
| 801 | |
| 802 | if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) { |
| 803 | addGetterImpl(newAttrInfo); |
| 804 | } |
| 805 | |
| 806 | if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) { |
| 807 | addSetterImpl(newAttrInfo); |
| 808 | } |
| 809 | |
| 810 | if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) { |
| 811 | addConstructor(newAttrInfo); |
| 812 | } |
| 813 | |
| 814 | if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) { |
| 815 | addHashCodeMethod(newAttrInfo); |
| 816 | } |
| 817 | |
| 818 | if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) { |
| 819 | addEqualsMethod(newAttrInfo); |
| 820 | } |
| 821 | |
| 822 | if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) { |
| 823 | addToStringMethod(newAttrInfo); |
| 824 | } |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Removes all temporary file handles. |
| 830 | * |
| 831 | * @throws IOException when failed to delete the temporary files |
| 832 | */ |
| 833 | public void close() throws IOException { |
| 834 | |
| 835 | closeFile(GETTER_METHOD_FILE_NAME); |
| 836 | getTemporaryFileHandle(GETTER_METHOD_FILE_NAME).delete(); |
| 837 | |
| 838 | closeFile(GETTER_METHOD_IMPL_FILE_NAME); |
| 839 | getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME).delete(); |
| 840 | |
| 841 | closeFile(SETTER_METHOD_FILE_NAME); |
| 842 | getTemporaryFileHandle(SETTER_METHOD_FILE_NAME).delete(); |
| 843 | |
| 844 | closeFile(SETTER_METHOD_IMPL_FILE_NAME); |
| 845 | getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME).delete(); |
| 846 | |
| 847 | closeFile(CONSTRUCTOR_FILE_NAME); |
| 848 | getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME).delete(); |
| 849 | |
| 850 | closeFile(ATTRIBUTE_FILE_NAME); |
| 851 | getTemporaryFileHandle(ATTRIBUTE_FILE_NAME).delete(); |
| 852 | |
| 853 | closeFile(HASH_CODE_METHOD_FILE_NAME); |
| 854 | getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME).delete(); |
| 855 | |
| 856 | closeFile(TO_STRING_METHOD_FILE_NAME); |
| 857 | getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME).delete(); |
| 858 | |
| 859 | closeFile(EQUALS_METHOD_FILE_NAME); |
| 860 | getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME).delete(); |
| 861 | |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Closes the file handle for temporary file. |
| 866 | * |
| 867 | * @param fileName temporary file's name |
| 868 | * @throws IOException when failed to close the file handle |
| 869 | */ |
| 870 | private void closeFile(String fileName) throws IOException { |
| 871 | |
| 872 | FileSystemUtil.updateFileHandle(new File(fileName), null, true); |
| 873 | } |
| 874 | } |