blob: 4ceb55aa52e0ea52e716b6ace6f6823c3dff0035 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
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
17package org.onosproject.yangutils.translator.tojava;
18
19import java.io.File;
20import java.io.IOException;
Bharat saraswale2d51d62016-03-23 19:40:35 +053021import java.util.ArrayList;
Vinod Kumar S38046502016-03-23 15:30:27 +053022import java.util.List;
23
24import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswale2d51d62016-03-23 19:40:35 +053028import org.onosproject.yangutils.datamodel.YangTypeDef;
29import org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen;
Vinod Kumar S38046502016-03-23 15:30:27 +053030import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
31import org.onosproject.yangutils.utils.UtilConstants;
32import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
34import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
Vinod Kumar S38046502016-03-23 15:30:27 +053035
36import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswale2d51d62016-03-23 19:40:35 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
39import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Vinod Kumar S38046502016-03-23 15:30:27 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
44import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
45import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
46import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
49import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
50import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
51import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf;
Bharat saraswale2d51d62016-03-23 19:40:35 +053052import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfTypeDef;
Vinod Kumar S38046502016-03-23 15:30:27 +053053import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
54import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
Bharat saraswale2d51d62016-03-23 19:40:35 +053055import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
56import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
57import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
58import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
59import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Vinod Kumar S38046502016-03-23 15:30:27 +053061import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
62import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
63import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
64import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
65import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
66import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
67import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
68import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Bharat saraswale2d51d62016-03-23 19:40:35 +053069import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar S38046502016-03-23 15:30:27 +053070import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
71import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
Bharat saraswale2d51d62016-03-23 19:40:35 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
Vinod Kumar S38046502016-03-23 15:30:27 +053073import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
74import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Bharat saraswale2d51d62016-03-23 19:40:35 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeDefConstructor;
Vinod Kumar S38046502016-03-23 15:30:27 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
Bharat saraswale2d51d62016-03-23 19:40:35 +053077import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
78import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
79import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
80import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
81import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
82import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
83import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
84import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
85import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
86import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
87import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
88import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_CONSTRUCTOR;
89import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
90import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
91import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
92import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053093
94/**
95 * Provides implementation of java code fragments temporary implementations.
96 */
97public 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 saraswale2d51d62016-03-23 19:40:35 +0530106 * 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 S38046502016-03-23 15:30:27 +0530112 * 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 saraswale2d51d62016-03-23 19:40:35 +0530122 * File type extension for java classes.
123 */
124 private static final String JAVA_FILE_EXTENSION = ".java";
125
126 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530127 * 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 saraswale2d51d62016-03-23 19:40:35 +0530182 * 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 S38046502016-03-23 15:30:27 +0530232 * 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 saraswale2d51d62016-03-23 19:40:35 +0530277 * Java attribute info.
278 */
279 private JavaAttributeInfo newAttrInfo;
280
281 /**
282 * Current YANG node.
283 */
284 private YangNode curYangNode;
285
286 /**
Bharat saraswal2f11f652016-03-25 18:19:46 +0530287 * Is attribute added.
288 */
289 private boolean isAttributePresent = false;
290
291 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530292 * Construct an object of temporary java code fragment.
293 *
294 * @param genFileType file generation type
295 * @param genDir file generation directory
296 * @param className class name
297 * @throws IOException when fails to create new file handle
298 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530299 public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className)
300 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530301
302 generatedTempFiles = 0;
303 absoluteDirPath = genDir;
304 generatedJavaClassName = className;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530305 generatedJavaFiles = genFileType;
Vinod Kumar S38046502016-03-23 15:30:27 +0530306 /**
307 * Initialize getter when generation file type matches to interface
308 * mask.
309 */
310 if ((genFileType & INTERFACE_MASK) != 0) {
311 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
312 }
313
314 /**
315 * Initialize getter and setter when generation file type matches to
316 * builder interface mask.
317 */
318 if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
319 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
320 generatedTempFiles |= SETTER_FOR_INTERFACE_MASK;
321 }
322
323 /**
324 * Initialize getterImpl, setterImpl and attributes when generation file
325 * type matches to builder class mask.
326 */
327 if ((genFileType & BUILDER_CLASS_MASK) != 0) {
328 generatedTempFiles |= ATTRIBUTES_MASK;
329 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
330 generatedTempFiles |= SETTER_FOR_CLASS_MASK;
331 }
332
333 /**
334 * Initialize getterImpl, attributes, constructor, hash code, equals and
335 * to strings when generation file type matches to impl class mask.
336 */
337 if ((genFileType & IMPL_CLASS_MASK) != 0) {
338 generatedTempFiles |= ATTRIBUTES_MASK;
339 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
340 generatedTempFiles |= CONSTRUCTOR_IMPL_MASK;
341 generatedTempFiles |= HASH_CODE_IMPL_MASK;
342 generatedTempFiles |= EQUALS_IMPL_MASK;
343 generatedTempFiles |= TO_STRING_IMPL_MASK;
344 }
345
Bharat saraswale2d51d62016-03-23 19:40:35 +0530346 /**
347 * Initialize getterImpl, attributes, hash code, equals and
348 * to strings when generation file type matches to typeDef class mask.
349 */
350 if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
351 generatedTempFiles |= ATTRIBUTES_MASK;
352 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
353 generatedTempFiles |= HASH_CODE_IMPL_MASK;
354 generatedTempFiles |= EQUALS_IMPL_MASK;
355 generatedTempFiles |= TO_STRING_IMPL_MASK;
356 }
357
Vinod Kumar S38046502016-03-23 15:30:27 +0530358 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
359 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
360 }
361
362 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
363 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
364 }
365
366 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
367 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
368 }
369
370 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
371 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
372 }
373
374 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
375 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
376 }
377
378 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
379 setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME));
380 }
381
382 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
383 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
384 }
385
386 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
387 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
388 }
389 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
390 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
391 }
392
393 }
394
395 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530396 * Returns java file handle for interface file.
397 *
398 * @return java file handle for interface file
399 */
400 public File getInterfaceJavaFileHandle() {
401
402 return interfaceJavaFileHandle;
403 }
404
405 /**
406 * Sets the java file handle for interface file.
407 *
408 * @param interfaceJavaFileHandle java file handle
409 */
410 public void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
411
412 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
413 }
414
415 /**
416 * Returns java file handle for builder interface file.
417 *
418 * @return java file handle for builder interface file
419 */
420 public File getBuilderInterfaceJavaFileHandle() {
421
422 return builderInterfaceJavaFileHandle;
423 }
424
425 /**
426 * Sets the java file handle for builder interface file.
427 *
428 * @param builderInterfaceJavaFileHandle java file handle
429 */
430 public void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
431
432 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
433 }
434
435 /**
436 * Returns java file handle for builder class file.
437 *
438 * @return java file handle for builder class file
439 */
440 public File getBuilderClassJavaFileHandle() {
441
442 return builderClassJavaFileHandle;
443 }
444
445 /**
446 * Sets the java file handle for builder class file.
447 *
448 * @param builderClassJavaFileHandle java file handle
449 */
450 public void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
451
452 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
453 }
454
455 /**
456 * Returns java file handle for impl class file.
457 *
458 * @return java file handle for impl class file
459 */
460 public File getImplClassJavaFileHandle() {
461
462 return implClassJavaFileHandle;
463 }
464
465 /**
466 * Sets the java file handle for impl class file.
467 *
468 * @param implClassJavaFileHandle java file handle
469 */
470 public void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
471
472 this.implClassJavaFileHandle = implClassJavaFileHandle;
473 }
474
475 /**
476 * Returns java file handle for typedef class file.
477 *
478 * @return java file handle for typedef class file
479 */
480 public File getTypedefClassJavaFileHandle() {
481
482 return typedefClassJavaFileHandle;
483 }
484
485 /**
486 * Sets the java file handle for typedef class file.
487 *
488 * @param typedefClassJavaFileHandle java file handle
489 */
490 public void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
491
492 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
493 }
494
495 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530496 * Returns attribute's temporary file handle.
497 *
498 * @return temporary file handle
499 */
500 public File getAttributesTempFileHandle() {
501
502 return attributesTempFileHandle;
503 }
504
505 /**
506 * Sets attribute's temporary file handle.
507 *
508 * @param attributeForClass file handle for attribute
509 */
510 public void setAttributesTempFileHandle(File attributeForClass) {
511
512 attributesTempFileHandle = attributeForClass;
513 }
514
515 /**
516 * Returns getter methods's temporary file handle.
517 *
518 * @return temporary file handle
519 */
520 public File getGetterInterfaceTempFileHandle() {
521
522 return getterInterfaceTempFileHandle;
523 }
524
525 /**
526 * Sets to getter method's temporary file handle.
527 *
528 * @param getterForInterface file handle for to getter method
529 */
530 public void setGetterInterfaceTempFileHandle(File getterForInterface) {
531
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() {
541
542 return getterImplTempFileHandle;
543 }
544
545 /**
546 * Sets to getter method's impl's temporary file handle.
547 *
548 * @param getterImpl file handle for to getter method's impl
549 */
550 public void setGetterImplTempFileHandle(File getterImpl) {
551
552 getterImplTempFileHandle = getterImpl;
553 }
554
555 /**
556 * Returns setter method's temporary file handle.
557 *
558 * @return temporary file handle
559 */
560 public File getSetterInterfaceTempFileHandle() {
561
562 return setterInterfaceTempFileHandle;
563 }
564
565 /**
566 * Sets to setter method's temporary file handle.
567 *
568 * @param setterForInterface file handle for to setter method
569 */
570 public void setSetterInterfaceTempFileHandle(File setterForInterface) {
571
572 setterInterfaceTempFileHandle = setterForInterface;
573 }
574
575 /**
576 * Returns setter method's impl's temporary file handle.
577 *
578 * @return temporary file handle
579 */
580 public File getSetterImplTempFileHandle() {
581
582 return setterImplTempFileHandle;
583 }
584
585 /**
586 * Sets to setter method's impl's temporary file handle.
587 *
588 * @param setterImpl file handle for to setter method's implementation class
589 */
590 public void setSetterImplTempFileHandle(File setterImpl) {
591
592 setterImplTempFileHandle = setterImpl;
593 }
594
595 /**
596 * Returns constructor's temporary file handle.
597 *
598 * @return temporary file handle
599 */
600 public File getConstructorImplTempFileHandle() {
601
602 return constructorImplTempFileHandle;
603 }
604
605 /**
606 * Sets to constructor's temporary file handle.
607 *
608 * @param constructor file handle for to constructor
609 */
610 public void setConstructorImplTempFileHandle(File constructor) {
611
612 constructorImplTempFileHandle = constructor;
613 }
614
615 /**
616 * Returns hash code method's temporary file handle.
617 *
618 * @return temporary file handle
619 */
620 public File getHashCodeImplTempFileHandle() {
621
622 return hashCodeImplTempFileHandle;
623 }
624
625 /**
626 * Sets hash code method's temporary file handle.
627 *
628 * @param hashCodeMethod file handle for hash code method
629 */
630 public void setHashCodeImplTempFileHandle(File hashCodeMethod) {
631
632 hashCodeImplTempFileHandle = hashCodeMethod;
633 }
634
635 /**
636 * Returns equals mehtod's temporary file handle.
637 *
638 * @return temporary file handle
639 */
640 public File getEqualsImplTempFileHandle() {
641
642 return equalsImplTempFileHandle;
643 }
644
645 /**
646 * Sets equals method's temporary file handle.
647 *
648 * @param equalsMethod file handle for to equals method
649 */
650 public void setEqualsImplTempFileHandle(File equalsMethod) {
651
652 equalsImplTempFileHandle = equalsMethod;
653 }
654
655 /**
656 * Returns to string method's temporary file handle.
657 *
658 * @return temporary file handle
659 */
660 public File getToStringImplTempFileHandle() {
661
662 return toStringImplTempFileHandle;
663 }
664
665 /**
666 * Sets to string method's temporary file handle.
667 *
668 * @param toStringMethod file handle for to string method
669 */
670 public void setToStringImplTempFileHandle(File toStringMethod) {
671
672 toStringImplTempFileHandle = toStringMethod;
673 }
674
675 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530676 * Returns java attribute info.
677 *
678 * @return java attribute info
679 */
680 public JavaAttributeInfo getNewAttrInfo() {
681
682 return newAttrInfo;
683 }
684
685 /**
686 * Sets java attribute info.
687 *
688 * @param newAttrInfo java attribute info
689 */
690 public void setNewAttrInfo(JavaAttributeInfo newAttrInfo) {
691
Bharat saraswal2f11f652016-03-25 18:19:46 +0530692 if (newAttrInfo != null) {
693 isAttributePresent = true;
694 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530695 this.newAttrInfo = newAttrInfo;
696 }
697
698 /**
699 * Returns current YANG node.
700 *
701 * @return current YANG node
702 */
703 public YangNode getCurYangNode() {
704
705 return curYangNode;
706 }
707
708 /**
709 * Sets current YANG node.
710 *
711 * @param curYangNode YANG node
712 */
713 public void setCurYangNode(YangNode curYangNode) {
714
715 this.curYangNode = curYangNode;
716 }
717
718 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530719 * Adds attribute for class.
720 *
721 * @param attr attribute info
722 * @throws IOException when fails to append to temporary file
723 */
724 public void addAttribute(JavaAttributeInfo attr) throws IOException {
725
Bharat saraswale2d51d62016-03-23 19:40:35 +0530726 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
Vinod Kumar S38046502016-03-23 15:30:27 +0530727 }
728
729 /**
730 * Adds getter for interface.
731 *
732 * @param attr attribute info
733 * @throws IOException when fails to append to temporary file
734 */
735 public void addGetterForInterface(JavaAttributeInfo attr) throws IOException {
736
Bharat saraswale2d51d62016-03-23 19:40:35 +0530737 appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530738 }
739
740 /**
741 * Adds getter method's impl for class.
742 *
743 * @param attr attribute info
Bharat saraswale2d51d62016-03-23 19:40:35 +0530744 * @param genFiletype generated file type
Vinod Kumar S38046502016-03-23 15:30:27 +0530745 * @throws IOException when fails to append to temporary file
746 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530747 public void addGetterImpl(JavaAttributeInfo attr, int genFiletype) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530748
Bharat saraswale2d51d62016-03-23 19:40:35 +0530749 if ((genFiletype & BUILDER_CLASS_MASK) != 0) {
750 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr) + NEW_LINE);
751 } else {
752 appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
753 + getGetterForClass(attr) + NEW_LINE);
754 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530755 }
756
757 /**
758 * Adds setter for interface.
759 *
760 * @param attr attribute info
761 * @throws IOException when fails to append to temporary file
762 */
763 public void addSetterForInterface(JavaAttributeInfo attr) throws IOException {
764
765 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530766 getSetterString(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530767 }
768
769 /**
770 * Adds setter's implementation for class.
771 *
772 * @param attr attribute info
773 * @throws IOException when fails to append to temporary file
774 */
775 public void addSetterImpl(JavaAttributeInfo attr) throws IOException {
776
777 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530778 getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530779 }
780
781 /**
782 * Adds build method for interface.
783 *
784 * @return build method for interface
785 * @throws IOException when fails to append to temporary file
786 */
787 public String addBuildMethodForInterface() throws IOException {
788
789 return parseBuilderInterfaceBuildMethodString(generatedJavaClassName);
790 }
791
792 /**
793 * Adds build method's implementation for class.
794 *
795 * @return build method implementation for class
796 * @throws IOException when fails to append to temporary file
797 */
798 public String addBuildMethodImpl() throws IOException {
799
Bharat saraswale2d51d62016-03-23 19:40:35 +0530800 return getBuildString(generatedJavaClassName) + NEW_LINE;
Vinod Kumar S38046502016-03-23 15:30:27 +0530801 }
802
803 /**
804 * Adds constructor for class.
805 *
806 * @param attr attribute info
807 * @throws IOException when fails to append to temporary file
808 */
809 public void addConstructor(JavaAttributeInfo attr) throws IOException {
810
811 appendToFile(getConstructorImplTempFileHandle(), getConstructor(generatedJavaClassName, attr));
812 }
813
814 /**
815 * Adds default constructor for class.
816 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530817 * @param modifier modifier for constructor.
818 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530819 * @return default constructor for class
820 * @throws IOException when fails to append to file
821 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530822 public String addDefaultConstructor(String modifier, String toAppend) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530823
Bharat saraswale2d51d62016-03-23 19:40:35 +0530824 return NEW_LINE + getDefaultConstructorString(generatedJavaClassName + toAppend, modifier);
825 }
826
827 /**
828 * Adds typedef constructor for class.
829 *
830 * @return typedef constructor for class
831 * @throws IOException when fails to append to file
832 */
833 public String addTypeDefConstructor() throws IOException {
834
835 return NEW_LINE + getJavaDoc(TYPE_DEF_CONSTRUCTOR, generatedJavaClassName, false)
836 + getTypeDefConstructor(newAttrInfo, generatedJavaClassName) + NEW_LINE;
837 }
838
839 /**
840 * Adds default constructor for class.
841 *
842 * @return default constructor for class
843 * @throws IOException when fails to append to file
844 */
845 public String addTypeDefsSetter() throws IOException {
846
847 return getJavaDoc(TYPE_DEF_SETTER_METHOD, generatedJavaClassName, false) + getSetterForTypeDefClass(newAttrInfo)
848 + NEW_LINE;
849 }
850
851 /**
852 * Adds default constructor for class.
853 *
854 * @return default constructor for class
855 * @throws IOException when fails to append to file
856 */
857 public String addOfMethod() throws IOException {
858
859 return JavaDocGen.getJavaDoc(JavaDocType.OF_METHOD, generatedJavaClassName, false)
860 + getOfMethod(generatedJavaClassName, newAttrInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530861 }
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 */
869 public void addHashCodeMethod(JavaAttributeInfo attr) throws IOException {
870
Bharat saraswale2d51d62016-03-23 19:40:35 +0530871 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530872 }
873
874 /**
875 * Adds equals method for class.
876 *
877 * @param attr attribute info
878 * @throws IOException when fails to append to temporary file
879 */
880 public void addEqualsMethod(JavaAttributeInfo attr) throws IOException {
881
Bharat saraswale2d51d62016-03-23 19:40:35 +0530882 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530883 }
884
885 /**
886 * Adds ToString method for class.
887 *
888 * @param attr attribute info
889 * @throws IOException when fails to append to temporary file
890 */
891 public void addToStringMethod(JavaAttributeInfo attr) throws IOException {
892
Bharat saraswale2d51d62016-03-23 19:40:35 +0530893 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530894 }
895
896 /**
897 * Returns a temporary file handle for the specific file type.
898 *
899 * @param fileName file name
900 * @return temporary file handle
901 * @throws IOException when fails to create new file handle
902 */
903 private File getTemporaryFileHandle(String fileName) throws IOException {
904
905 String path = getTempDirPath();
906 File dir = new File(path);
907 if (!dir.exists()) {
908 dir.mkdirs();
909 }
910
911 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
912 if (!file.exists()) {
913 file.createNewFile();
Vinod Kumar S38046502016-03-23 15:30:27 +0530914 }
915 return file;
916 }
917
918 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530919 * Returns a temporary file handle for the specific file type.
920 *
921 * @param fileName file name
922 * @return temporary file handle
923 * @throws IOException when fails to create new file handle
924 */
925 private File getJavaFileHandle(String fileName) throws IOException {
926
927 createPackage(absoluteDirPath, getJavaFileInfo().getJavaName());
928
929 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
930 }
931
932 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530933 * Returns data from the temporary files.
934 *
935 * @param file temporary file handle
936 * @return stored data from temporary files
937 * @throws IOException when failed to get data from the given file
938 */
939 public String getTemporaryDataFromFileHandle(File file) throws IOException {
940
941 String path = getTempDirPath();
942 if (new File(path + file.getName()).exists()) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530943 return FileSystemUtil.readAppendFile(path + file.getName(), EMPTY_STRING);
Vinod Kumar S38046502016-03-23 15:30:27 +0530944 } else {
945 throw new IOException("Unable to get data from the given "
Bharat saraswale2d51d62016-03-23 19:40:35 +0530946 + file.getName() + " file for " + generatedJavaClassName + PERIOD);
Vinod Kumar S38046502016-03-23 15:30:27 +0530947 }
948 }
949
950 /**
951 * Returns temporary directory path.
952 *
953 * @return directory path
954 */
955 private String getTempDirPath() {
956
Bharat saraswale2d51d62016-03-23 19:40:35 +0530957 return absoluteDirPath.replace(PERIOD, UtilConstants.SLASH)
958 + SLASH + generatedJavaClassName + TEMP_FOLDER_NAME_SUFIX + SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +0530959 }
960
961 /**
962 * Parse attribute to get the attribute string.
963 *
964 * @param attr attribute info
965 * @return attribute string
966 */
967 private String parseAttribute(JavaAttributeInfo attr) {
968
969 /*
970 * TODO: check if this utility needs to be called or move to the caller
971 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530972 String attributeName = JavaIdentifierSyntax
Bharat saraswal2f11f652016-03-25 18:19:46 +0530973 .getCamelCase(JavaIdentifierSyntax.getSmallCase(attr.getAttributeName()));
Vinod Kumar S38046502016-03-23 15:30:27 +0530974 if (attr.isQualifiedName()) {
975 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
976 attributeName, attr.isListAttr());
977 } else {
978 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
979 attr.isListAttr());
980 }
981 }
982
983 /**
984 * Append content to temporary file.
985 *
986 * @param file temporary file
987 * @param data data to be appended
988 * @throws IOException when fails to append to file
989 */
990 private void appendToFile(File file, String data) throws IOException {
991
992 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530993 insertDataIntoJavaFile(file, data);
Vinod Kumar S38046502016-03-23 15:30:27 +0530994 } catch (IOException ex) {
995 throw new IOException("failed to write in temp file.");
996 }
997 }
998
999 /**
1000 * Adds current node info as and attribute to the parent generated file.
1001 *
1002 * @param curNode current node which needs to be added as an attribute in
1003 * the parent generated code
1004 * @param isList is list construct
1005 * @throws IOException IO operation exception
1006 */
1007 public void addCurNodeInfoInParentTempFile(YangNode curNode,
1008 boolean isList) throws IOException {
1009
1010 YangNode parent = getParentNodeInGenCode(curNode);
1011 if (!(parent instanceof JavaCodeGenerator)) {
1012 throw new RuntimeException("missing parent node to contain current node info in generated file");
1013 }
1014 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1015 parent, isList);
1016
1017 if (!(parent instanceof HasTempJavaCodeFragmentFiles)) {
1018 throw new RuntimeException("missing parent temp file handle");
1019 }
1020 ((HasTempJavaCodeFragmentFiles) parent)
1021 .getTempJavaCodeFragmentFiles()
1022 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1023 }
1024
1025 /**
1026 * Adds leaf attributes in generated files.
1027 *
1028 * @param listOfLeaves list of YANG leaf
1029 * @param curNode current data model node
1030 * @throws IOException IO operation fail
1031 */
1032 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1033 YangNode curNode) throws IOException {
1034
1035 if (listOfLeaves != null) {
1036 for (YangLeaf leaf : listOfLeaves) {
1037 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(curNode,
1038 leaf.getDataType(),
1039 leaf.getLeafName(), false);
1040 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1041 }
1042 }
1043 }
1044
1045 /**
1046 * Adds leaf list's attributes in generated files.
1047 *
1048 * @param listOfLeafList list of YANG leaves
1049 * @param curNode cached file handle
1050 * @throws IOException IO operation fail
1051 */
1052 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList,
1053 YangNode curNode) throws IOException {
1054
1055 if (listOfLeafList != null) {
1056
1057 /*
1058 * Check if the attribute is of type list, then the java.lang.list
1059 * needs to be imported.
1060 */
1061 if (listOfLeafList.size() != 0) {
1062 if (!(curNode instanceof HasJavaImportData)) {
1063 throw new RuntimeException("missing import info in current data model node");
1064
1065 }
1066 ((HasJavaImportData) curNode).getJavaImportData()
1067 .setIfListImported(true);
1068
1069 }
1070
1071 for (YangLeafList leafList : listOfLeafList) {
1072 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(
1073 curNode, leafList.getDataType(), leafList.getLeafName(),
1074 true);
1075 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1076 }
1077 }
1078 }
1079
1080 /**
1081 * Add all the leaves in the current data model node as part of the
1082 * generated temporary file.
1083 *
1084 * @param curNode java file info of the generated file
1085 * @throws IOException IO operation fail
1086 */
1087 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode) throws IOException {
1088
1089 if (curNode instanceof YangLeavesHolder) {
1090 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
1091 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), curNode);
1092 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), curNode);
1093 }
1094 }
1095
1096 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301097 * Adds leaf attributes in generated files.
1098 *
1099 * @param curNode current data model node
1100 * @throws IOException IO operation fail
1101 */
1102 public void addTypeDefAttributeToTempFiles(YangNode curNode) throws IOException {
1103
1104 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfTypeDef(curNode,
1105 ((YangTypeDef) curNode).getDerivedType().getDataTypeExtendedInfo().getBaseType(),
1106 ((YangTypeDef) curNode).getName(), false);
1107 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1108 }
1109
1110 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301111 * Add the new attribute info to the target generated temporary files.
1112 *
1113 * @param newAttrInfo the attribute info that needs to be added to temporary
1114 * files
1115 * @throws IOException IO operation fail
1116 */
1117 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1118 throws IOException {
1119
Bharat saraswale2d51d62016-03-23 19:40:35 +05301120 setNewAttrInfo(newAttrInfo);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301121 if (isAttributePresent) {
1122 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
1123 addAttribute(newAttrInfo);
1124 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301125
Bharat saraswal2f11f652016-03-25 18:19:46 +05301126 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
1127 addGetterForInterface(newAttrInfo);
1128 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301129
Bharat saraswal2f11f652016-03-25 18:19:46 +05301130 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
1131 addSetterForInterface(newAttrInfo);
1132 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301133
Bharat saraswal2f11f652016-03-25 18:19:46 +05301134 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
1135 addGetterImpl(newAttrInfo, generatedJavaFiles);
1136 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301137
Bharat saraswal2f11f652016-03-25 18:19:46 +05301138 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
1139 addSetterImpl(newAttrInfo);
1140 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301141
Bharat saraswal2f11f652016-03-25 18:19:46 +05301142 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
1143 addConstructor(newAttrInfo);
1144 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301145
Bharat saraswal2f11f652016-03-25 18:19:46 +05301146 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
1147 addHashCodeMethod(newAttrInfo);
1148 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301149
Bharat saraswal2f11f652016-03-25 18:19:46 +05301150 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
1151 addEqualsMethod(newAttrInfo);
1152 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301153
Bharat saraswal2f11f652016-03-25 18:19:46 +05301154 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
1155 addToStringMethod(newAttrInfo);
1156 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301157 }
1158 return;
1159 }
1160
1161 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301162 * Return java file info.
1163 *
1164 * @return java file info
1165 */
1166 private JavaFileInfo getJavaFileInfo() {
1167
1168 return ((HasJavaFileInfo) getCurYangNode()).getJavaFileInfo();
1169 }
1170
1171 /**
1172 * Returns java class name.
1173 *
1174 * @param suffix for the class name based on the file type
1175 * @return java class name
1176 */
1177 private String getJavaClassName(String suffix) {
1178
1179 return JavaIdentifierSyntax.getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
1180 }
1181
1182 /**
1183 * Returns the directory path.
1184 *
1185 * @return directory path
1186 */
1187 private String getDirPath() {
1188
1189 return getJavaFileInfo().getPackageFilePath();
1190 }
1191
1192 /**
1193 * Construct java code exit.
1194 *
1195 * @param fileType generated file type
1196 * @param curNode current YANG node
1197 * @throws IOException when fails to generate java files
1198 */
1199 public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
1200
1201 setCurYangNode(curNode);
1202 List<String> imports = new ArrayList<>();
Bharat saraswal2f11f652016-03-25 18:19:46 +05301203 if (curNode instanceof HasJavaImportData && isAttributePresent) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301204 imports = ((HasJavaImportData) curNode).getJavaImportData().getImports(getNewAttrInfo());
1205 }
1206 /**
1207 * Start generation of files.
1208 */
1209 if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0
1210 | fileType == GENERATE_INTERFACE_WITH_BUILDER) {
1211
1212 /**
1213 * Create interface file.
1214 */
1215 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301216 setInterfaceJavaFileHandle(
1217 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301218 /**
1219 * Create builder interface file.
1220 */
1221 setBuilderInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1222 setBuilderInterfaceJavaFileHandle(
Bharat saraswal2f11f652016-03-25 18:19:46 +05301223 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301224 /**
1225 * Append builder interface file to interface file and close it.
1226 */
1227 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1228 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), JavaCodeSnippetGen.getJavaClassDefClose());
1229
1230 }
1231
Bharat saraswal2f11f652016-03-25 18:19:46 +05301232 if (curNode instanceof HasJavaImportData && isAttributePresent) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301233 imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForHashAndEquals());
1234 imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForToString());
1235 java.util.Collections.sort(imports);
1236 }
1237
1238 if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0
1239 | fileType == GENERATE_INTERFACE_WITH_BUILDER) {
1240
1241 /**
1242 * Create builder class file.
1243 */
1244 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301245 setBuilderClassJavaFileHandle(
1246 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301247 /**
1248 * Create impl class file.
1249 */
1250 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301251 setImplClassJavaFileHandle(
1252 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301253 /**
1254 * Append impl class to builder class and close it.
1255 */
1256 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1257 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), JavaCodeSnippetGen.getJavaClassDefClose());
1258
1259 }
1260
1261 /**
1262 * Creates type def class file.
1263 */
1264 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
1265 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
1266 setTypedefClassJavaFileHandle(generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports));
1267 }
1268
1269 /**
1270 * Close all the file handles.
1271 */
1272 close();
1273 }
1274
1275 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301276 * Removes all temporary file handles.
1277 *
1278 * @throws IOException when failed to delete the temporary files
1279 */
Bharat saraswale2d51d62016-03-23 19:40:35 +05301280 private void close() throws IOException {
1281
Bharat saraswal2f11f652016-03-25 18:19:46 +05301282 if ((generatedJavaFiles & INTERFACE_MASK) != 0) {
1283 closeFile(getInterfaceJavaFileHandle(), false);
1284 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301285
Bharat saraswal2f11f652016-03-25 18:19:46 +05301286 if ((generatedJavaFiles & BUILDER_CLASS_MASK) != 0) {
1287 closeFile(getBuilderClassJavaFileHandle(), false);
1288 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301289
Bharat saraswal2f11f652016-03-25 18:19:46 +05301290 if ((generatedJavaFiles & BUILDER_INTERFACE_MASK) != 0) {
1291 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1292 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301293
Bharat saraswal2f11f652016-03-25 18:19:46 +05301294 if ((generatedJavaFiles & IMPL_CLASS_MASK) != 0) {
1295 closeFile(getImplClassJavaFileHandle(), true);
1296 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301297
Bharat saraswal2f11f652016-03-25 18:19:46 +05301298 if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) {
1299 closeFile(getTypedefClassJavaFileHandle(), false);
1300 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301301
Bharat saraswal2f11f652016-03-25 18:19:46 +05301302 /**
1303 * Close all temporary file handles and delete the files.
1304 */
1305 closeFile(getGetterInterfaceTempFileHandle(), true);
1306 closeFile(getGetterImplTempFileHandle(), true);
1307 closeFile(getSetterInterfaceTempFileHandle(), true);
1308 closeFile(getSetterImplTempFileHandle(), true);
1309 closeFile(getConstructorImplTempFileHandle(), true);
1310 closeFile(getAttributesTempFileHandle(), true);
1311 closeFile(getHashCodeImplTempFileHandle(), true);
1312 closeFile(getToStringImplTempFileHandle(), true);
1313 closeFile(getEqualsImplTempFileHandle(), true);
Vinod Kumar S38046502016-03-23 15:30:27 +05301314 }
1315
1316 /**
1317 * Closes the file handle for temporary file.
1318 *
1319 * @param fileName temporary file's name
1320 * @throws IOException when failed to close the file handle
1321 */
Bharat saraswal2f11f652016-03-25 18:19:46 +05301322 private void closeFile(File file, boolean toBeDeleted) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301323
Bharat saraswal2f11f652016-03-25 18:19:46 +05301324 if (file.exists()) {
1325 FileSystemUtil.updateFileHandle(file, null, true);
1326 if (toBeDeleted) {
1327 file.delete();
1328 }
1329 clean(getTempDirPath());
1330 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301331 }
1332}