blob: b66e253e2892bbb7f699b4215fb62458a302b85c [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +05303 *
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;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053023import org.onosproject.yangutils.datamodel.HasType;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053028import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
32import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Vinod Kumar S38046502016-03-23 15:30:27 +053035import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
37import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
Vinod Kumar S38046502016-03-23 15:30:27 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
Vinod Kumar S38046502016-03-23 15:30:27 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
46import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
Vinod Kumar S38046502016-03-23 15:30:27 +053049import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053050import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfType;
Vinod Kumar S38046502016-03-23 15:30:27 +053051import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053052import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getFromStringAttributeInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053053import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053054import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
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;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053060import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswale2d51d62016-03-23 19:40:35 +053061import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053062import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
64import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Vinod Kumar S38046502016-03-23 15:30:27 +053065import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053066import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
Vinod Kumar S38046502016-03-23 15:30:27 +053067import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
68import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
69import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
70import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053071import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar S38046502016-03-23 15:30:27 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
73import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
74import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Bharat saraswale2d51d62016-03-23 19:40:35 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
Vinod Kumar S38046502016-03-23 15:30:27 +053077import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
78import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
79import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
80import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053081import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Vinod Kumar S38046502016-03-23 15:30:27 +053082import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053083import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
84import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
85import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport;
86import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addImportsToStringAndHasCodeMethods;
87import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
88import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
89import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
90import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.prepareJavaFileGeneratorForExtendsList;
Bharat saraswale2d51d62016-03-23 19:40:35 +053091import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
92import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
93import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
94import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
95import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
96import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
97import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
98import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
99import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530100import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530102import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530103import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530104import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
105import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
106import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +0530107
108/**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Represents implementation of java code fragments temporary implementations.
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 */
111public class TempJavaCodeFragmentFiles {
112
113 /**
114 * The variable which guides the types of temporary files generated using
115 * the temporary generated file types mask.
116 */
117 private int generatedTempFiles;
118
119 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120 * The variable which guides the types of files generated using
121 * the generated file types mask.
122 */
123 private int generatedJavaFiles;
124
125 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530126 * Absolute path where the target java file needs to be generated.
127 */
128 private String absoluteDirPath;
129
130 /**
131 * Name of java file that needs to be generated.
132 */
133 private String generatedJavaClassName;
134
135 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530136 * Contains all the class name which will be extended by generated files.
137 */
138 private List<String> extendsList = new ArrayList<>();
139
140 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530141 * File type extension for java classes.
142 */
143 private static final String JAVA_FILE_EXTENSION = ".java";
144
145 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530146 * File type extension for temporary classes.
147 */
148 private static final String TEMP_FILE_EXTENSION = ".tmp";
149
150 /**
151 * Folder suffix for temporary files folder.
152 */
153 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
154
155 /**
156 * File name for getter method.
157 */
158 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
159
160 /**
161 * File name for getter method implementation.
162 */
163 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
164
165 /**
166 * File name for setter method.
167 */
168 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
169
170 /**
171 * File name for setter method implementation.
172 */
173 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
174
175 /**
176 * File name for constructor.
177 */
178 private static final String CONSTRUCTOR_FILE_NAME = "Constructor";
179
180 /**
181 * File name for attributes.
182 */
183 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
184
185 /**
186 * File name for to string method.
187 */
188 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
189
190 /**
191 * File name for hash code method.
192 */
193 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
194
195 /**
196 * File name for equals method.
197 */
198 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
199
200 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530201 * File name for of string method.
202 */
203 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
204
205 /**
206 * File name for construction for special type like union, typedef.
207 */
208 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
209
210 /**
211 * File name for from string method.
212 */
213 private static final String UNION_FROM_STRING_METHOD_FILE_NAME = "UnionFromString";
214
215 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530216 * File name for interface java file name suffix.
217 */
218 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
219
220 /**
221 * File name for builder interface file name suffix.
222 */
223 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
224
225 /**
226 * File name for builder class file name suffix.
227 */
228 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
229
230 /**
231 * File name for impl class file name suffix.
232 */
233 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
234
235 /**
236 * File name for typedef class file name suffix.
237 */
238 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
239
240 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530241 * File name for generated class file for special type like union, typedef suffix.
242 */
243 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
244
245 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530246 * Java file handle for interface file.
247 */
248 private File interfaceJavaFileHandle;
249
250 /**
251 * Java file handle for builder interface file.
252 */
253 private File builderInterfaceJavaFileHandle;
254
255 /**
256 * Java file handle for builder class file.
257 */
258 private File builderClassJavaFileHandle;
259
260 /**
261 * Java file handle for impl class file.
262 */
263 private File implClassJavaFileHandle;
264
265 /**
266 * Java file handle for typedef class file.
267 */
268 private File typedefClassJavaFileHandle;
269
270 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530271 * Java file handle for type class like union, typedef file.
272 */
273 private File typeClassJavaFileHandle;
274
275 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530276 * Temporary file handle for attribute.
277 */
278 private File attributesTempFileHandle;
279
280 /**
281 * Temporary file handle for getter of interface.
282 */
283 private File getterInterfaceTempFileHandle;
284
285 /**
286 * Temporary file handle for getter of class.
287 */
288 private File getterImplTempFileHandle;
289
290 /**
291 * Temporary file handle for setter of interface.
292 */
293 private File setterInterfaceTempFileHandle;
294
295 /**
296 * Temporary file handle for setter of class.
297 */
298 private File setterImplTempFileHandle;
299
300 /**
301 * Temporary file handle for constructor of class.
302 */
303 private File constructorImplTempFileHandle;
304
305 /**
306 * Temporary file handle for hash code method of class.
307 */
308 private File hashCodeImplTempFileHandle;
309
310 /**
311 * Temporary file handle for equals method of class.
312 */
313 private File equalsImplTempFileHandle;
314
315 /**
316 * Temporary file handle for to string method of class.
317 */
318 private File toStringImplTempFileHandle;
319
320 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530321 * Temporary file handle for of string method of class.
322 */
323 private File ofStringImplTempFileHandle;
324
325 /**
326 * Temporary file handle for constructor for type class.
327 */
328 private File constructorForTypeTempFileHandle;
329
330 /**
331 * Temporary file handle for union's from string method of class.
332 */
333 private File unionFromStringImplTempFileHandle;
334
335 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530336 * Java attribute info.
337 */
338 private JavaAttributeInfo newAttrInfo;
339
340 /**
341 * Current YANG node.
342 */
343 private YangNode curYangNode;
344
345 /**
Bharat saraswal2f11f652016-03-25 18:19:46 +0530346 * Is attribute added.
347 */
348 private boolean isAttributePresent = false;
349
350 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530351 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +0530352 *
353 * @param genFileType file generation type
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530354 * @param genDir file generation directory
355 * @param className class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530356 * @throws IOException when fails to create new file handle
357 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530358 public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className)
359 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530360
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530361 setExtendsList(new ArrayList<>());
Vinod Kumar S38046502016-03-23 15:30:27 +0530362 generatedTempFiles = 0;
363 absoluteDirPath = genDir;
364 generatedJavaClassName = className;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530365 generatedJavaFiles = genFileType;
Vinod Kumar S38046502016-03-23 15:30:27 +0530366 /**
367 * Initialize getter when generation file type matches to interface
368 * mask.
369 */
370 if ((genFileType & INTERFACE_MASK) != 0) {
371 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
372 }
373
374 /**
375 * Initialize getter and setter when generation file type matches to
376 * builder interface mask.
377 */
378 if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
379 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
380 generatedTempFiles |= SETTER_FOR_INTERFACE_MASK;
381 }
382
383 /**
384 * Initialize getterImpl, setterImpl and attributes when generation file
385 * type matches to builder class mask.
386 */
387 if ((genFileType & BUILDER_CLASS_MASK) != 0) {
388 generatedTempFiles |= ATTRIBUTES_MASK;
389 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
390 generatedTempFiles |= SETTER_FOR_CLASS_MASK;
391 }
392
393 /**
394 * Initialize getterImpl, attributes, constructor, hash code, equals and
395 * to strings when generation file type matches to impl class mask.
396 */
397 if ((genFileType & IMPL_CLASS_MASK) != 0) {
398 generatedTempFiles |= ATTRIBUTES_MASK;
399 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
400 generatedTempFiles |= CONSTRUCTOR_IMPL_MASK;
401 generatedTempFiles |= HASH_CODE_IMPL_MASK;
402 generatedTempFiles |= EQUALS_IMPL_MASK;
403 generatedTempFiles |= TO_STRING_IMPL_MASK;
404 }
405
Bharat saraswale2d51d62016-03-23 19:40:35 +0530406 /**
407 * Initialize getterImpl, attributes, hash code, equals and
408 * to strings when generation file type matches to typeDef class mask.
409 */
410 if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
411 generatedTempFiles |= ATTRIBUTES_MASK;
412 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
413 generatedTempFiles |= HASH_CODE_IMPL_MASK;
414 generatedTempFiles |= EQUALS_IMPL_MASK;
415 generatedTempFiles |= TO_STRING_IMPL_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530416 generatedTempFiles |= OF_STRING_IMPL_MASK;
417 generatedTempFiles |= CONSTRUCTOR_FOR_TYPE_MASK;
418 }
419
420 /**
421 * Initialize getterImpl, attributes, hash code, equals, of string,
422 * constructor, union's to string, union's from string when generation
423 * file type matches to union class mask.
424 */
425 if ((genFileType & GENERATE_UNION_CLASS) != 0) {
426 generatedTempFiles |= ATTRIBUTES_MASK;
427 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
428 generatedTempFiles |= HASH_CODE_IMPL_MASK;
429 generatedTempFiles |= EQUALS_IMPL_MASK;
430 generatedTempFiles |= OF_STRING_IMPL_MASK;
431 generatedTempFiles |= CONSTRUCTOR_FOR_TYPE_MASK;
432 generatedTempFiles |= TO_STRING_IMPL_MASK;
433 generatedTempFiles |= UNION_FROM_STRING_IMPL_MASK;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530434 }
435
Vinod Kumar S38046502016-03-23 15:30:27 +0530436 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
437 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
438 }
439
440 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
441 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
442 }
443
444 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
445 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
446 }
447
448 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
449 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
450 }
451
452 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
453 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
454 }
455
456 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
457 setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME));
458 }
459
460 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
461 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
462 }
463
464 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
465 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
466 }
467 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
468 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
469 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530470
471 if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
472 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
473 }
474
475 if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
476 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
477 }
478
479 if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
480 setUnionFromStringImplTempFileHandle(getTemporaryFileHandle(UNION_FROM_STRING_METHOD_FILE_NAME));
481 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530482 }
483
484 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530485 * Returns java file handle for interface file.
486 *
487 * @return java file handle for interface file
488 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530489 private File getInterfaceJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530490 return interfaceJavaFileHandle;
491 }
492
493 /**
494 * Sets the java file handle for interface file.
495 *
496 * @param interfaceJavaFileHandle java file handle
497 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530498 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530499 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
500 }
501
502 /**
503 * Returns java file handle for builder interface file.
504 *
505 * @return java file handle for builder interface file
506 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530507 private File getBuilderInterfaceJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530508 return builderInterfaceJavaFileHandle;
509 }
510
511 /**
512 * Sets the java file handle for builder interface file.
513 *
514 * @param builderInterfaceJavaFileHandle java file handle
515 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530516 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530517 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
518 }
519
520 /**
521 * Returns java file handle for builder class file.
522 *
523 * @return java file handle for builder class file
524 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530525 private File getBuilderClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530526 return builderClassJavaFileHandle;
527 }
528
529 /**
530 * Sets the java file handle for builder class file.
531 *
532 * @param builderClassJavaFileHandle java file handle
533 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530534 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530535 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
536 }
537
538 /**
539 * Returns java file handle for impl class file.
540 *
541 * @return java file handle for impl class file
542 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530543 private File getImplClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530544 return implClassJavaFileHandle;
545 }
546
547 /**
548 * Sets the java file handle for impl class file.
549 *
550 * @param implClassJavaFileHandle java file handle
551 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530552 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530553 this.implClassJavaFileHandle = implClassJavaFileHandle;
554 }
555
556 /**
557 * Returns java file handle for typedef class file.
558 *
559 * @return java file handle for typedef class file
560 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530561 private File getTypedefClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530562 return typedefClassJavaFileHandle;
563 }
564
565 /**
566 * Sets the java file handle for typedef class file.
567 *
568 * @param typedefClassJavaFileHandle java file handle
569 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530570 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530571 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
572 }
573
574 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530575 * Returns java file handle for type class file.
576 *
577 * @return java file handle for type class file
578 */
579 private File getTypeClassJavaFileHandle() {
580 return typeClassJavaFileHandle;
581 }
582
583 /**
584 * Sets the java file handle for type class file.
585 *
586 * @param typeClassJavaFileHandle type file handle
587 */
588 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
589 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
590 }
591
592 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530593 * Returns attribute's temporary file handle.
594 *
595 * @return temporary file handle
596 */
597 public File getAttributesTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530598 return attributesTempFileHandle;
599 }
600
601 /**
602 * Sets attribute's temporary file handle.
603 *
604 * @param attributeForClass file handle for attribute
605 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530606 private void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530607 attributesTempFileHandle = attributeForClass;
608 }
609
610 /**
611 * Returns getter methods's temporary file handle.
612 *
613 * @return temporary file handle
614 */
615 public File getGetterInterfaceTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530616 return getterInterfaceTempFileHandle;
617 }
618
619 /**
620 * Sets to getter method's temporary file handle.
621 *
622 * @param getterForInterface file handle for to getter method
623 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530624 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530625 getterInterfaceTempFileHandle = getterForInterface;
626 }
627
628 /**
629 * Returns getter method's impl's temporary file handle.
630 *
631 * @return temporary file handle
632 */
633 public File getGetterImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530634 return getterImplTempFileHandle;
635 }
636
637 /**
638 * Sets to getter method's impl's temporary file handle.
639 *
640 * @param getterImpl file handle for to getter method's impl
641 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530642 private void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530643 getterImplTempFileHandle = getterImpl;
644 }
645
646 /**
647 * Returns setter method's temporary file handle.
648 *
649 * @return temporary file handle
650 */
651 public File getSetterInterfaceTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530652 return setterInterfaceTempFileHandle;
653 }
654
655 /**
656 * Sets to setter method's temporary file handle.
657 *
658 * @param setterForInterface file handle for to setter method
659 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530660 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530661 setterInterfaceTempFileHandle = setterForInterface;
662 }
663
664 /**
665 * Returns setter method's impl's temporary file handle.
666 *
667 * @return temporary file handle
668 */
669 public File getSetterImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530670 return setterImplTempFileHandle;
671 }
672
673 /**
674 * Sets to setter method's impl's temporary file handle.
675 *
676 * @param setterImpl file handle for to setter method's implementation class
677 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530678 private void setSetterImplTempFileHandle(File setterImpl) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530679 setterImplTempFileHandle = setterImpl;
680 }
681
682 /**
683 * Returns constructor's temporary file handle.
684 *
685 * @return temporary file handle
686 */
687 public File getConstructorImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530688 return constructorImplTempFileHandle;
689 }
690
691 /**
692 * Sets to constructor's temporary file handle.
693 *
694 * @param constructor file handle for to constructor
695 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530696 private void setConstructorImplTempFileHandle(File constructor) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530697 constructorImplTempFileHandle = constructor;
698 }
699
700 /**
701 * Returns hash code method's temporary file handle.
702 *
703 * @return temporary file handle
704 */
705 public File getHashCodeImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530706 return hashCodeImplTempFileHandle;
707 }
708
709 /**
710 * Sets hash code method's temporary file handle.
711 *
712 * @param hashCodeMethod file handle for hash code method
713 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530714 private void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530715 hashCodeImplTempFileHandle = hashCodeMethod;
716 }
717
718 /**
719 * Returns equals mehtod's temporary file handle.
720 *
721 * @return temporary file handle
722 */
723 public File getEqualsImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530724 return equalsImplTempFileHandle;
725 }
726
727 /**
728 * Sets equals method's temporary file handle.
729 *
730 * @param equalsMethod file handle for to equals method
731 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530732 private void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530733 equalsImplTempFileHandle = equalsMethod;
734 }
735
736 /**
737 * Returns to string method's temporary file handle.
738 *
739 * @return temporary file handle
740 */
741 public File getToStringImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530742 return toStringImplTempFileHandle;
743 }
744
745 /**
746 * Sets to string method's temporary file handle.
747 *
748 * @param toStringMethod file handle for to string method
749 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530750 private void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530751 toStringImplTempFileHandle = toStringMethod;
752 }
753
754 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530755 * Returns of string method's temporary file handle.
756 *
757 * @return of string method's temporary file handle
758 */
759 public File getOfStringImplTempFileHandle() {
760 return ofStringImplTempFileHandle;
761 }
762
763 /**
764 * Set of string method's temporary file handle.
765 *
766 * @param ofStringImplTempFileHandle of string method's temporary file handle
767 */
768 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
769 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
770 }
771
772 /**
773 * Returns type class constructor method's temporary file handle.
774 *
775 * @return type class constructor method's temporary file handle
776 */
777 public File getConstructorForTypeTempFileHandle() {
778 return constructorForTypeTempFileHandle;
779 }
780
781 /**
782 * Sets type class constructor method's temporary file handle.
783 *
784 * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
785 */
786 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
787 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
788 }
789
790 /**
791 * Returns union's from string method's temporary file handle.
792 *
793 * @return union's from string method's temporary file handle
794 */
795 public File getUnionFromStringImplTempFileHandle() {
796 return unionFromStringImplTempFileHandle;
797 }
798
799 /**
800 * Sets union's from string method's temporary file handle.
801 *
802 * @param unionFromStringImplTempFileHandle union's from string method's temporary file handle
803 */
804 private void setUnionFromStringImplTempFileHandle(File unionFromStringImplTempFileHandle) {
805 this.unionFromStringImplTempFileHandle = unionFromStringImplTempFileHandle;
806 }
807
808 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530809 * Returns java attribute info.
810 *
811 * @return java attribute info
812 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530813 private JavaAttributeInfo getNewAttrInfo() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530814 return newAttrInfo;
815 }
816
817 /**
818 * Sets java attribute info.
819 *
820 * @param newAttrInfo java attribute info
821 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530822 private void setNewAttrInfo(JavaAttributeInfo newAttrInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530823
Bharat saraswal2f11f652016-03-25 18:19:46 +0530824 if (newAttrInfo != null) {
825 isAttributePresent = true;
826 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530827 this.newAttrInfo = newAttrInfo;
828 }
829
830 /**
831 * Returns current YANG node.
832 *
833 * @return current YANG node
834 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530835 private YangNode getCurYangNode() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530836 return curYangNode;
837 }
838
839 /**
840 * Sets current YANG node.
841 *
842 * @param curYangNode YANG node
843 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530844 private void setCurYangNode(YangNode curYangNode) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530845 this.curYangNode = curYangNode;
846 }
847
848 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530849 * Returns list of classes to be extended by generated files.
850 *
851 * @return list of classes to be extended by generated files
852 */
853 private List<String> getExtendsList() {
854 return extendsList;
855 }
856
857 /**
858 * Sets class to be extended by generated file.
859 *
860 * @param extendsList list of classes to be extended
861 */
862
863 private void setExtendsList(List<String> extendsList) {
864 this.extendsList = extendsList;
865 }
866
867 /**
868 * Adds class to the extends list.
869 *
870 * @param extend class to be extended
871 */
872 public void addToExtendsList(String extend) {
873 getExtendsList().add(extend);
874 }
875
876 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530877 * Adds of string for type.
878 *
879 * @param attr attribute info
880 * @throws IOException when fails to append to temporary file
881 */
882 private void addOfStringMethod(JavaAttributeInfo attr) throws IOException {
883 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr, generatedJavaClassName)
884 + NEW_LINE);
885 }
886
887 /**
888 * Adds type constructor.
889 *
890 * @param attr attribute info
891 * @throws IOException when fails to append to temporary file
892 */
893 private void addTypeConstructor(JavaAttributeInfo attr) throws IOException {
894 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
895 generatedJavaClassName) + NEW_LINE);
896 }
897
898 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530899 * Adds attribute for class.
900 *
901 * @param attr attribute info
902 * @throws IOException when fails to append to temporary file
903 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530904 private void addAttribute(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530905 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
Vinod Kumar S38046502016-03-23 15:30:27 +0530906 }
907
908 /**
909 * Adds getter for interface.
910 *
911 * @param attr attribute info
912 * @throws IOException when fails to append to temporary file
913 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530914 private void addGetterForInterface(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530915 appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530916 }
917
918 /**
919 * Adds getter method's impl for class.
920 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530921 * @param attr attribute info
Bharat saraswale2d51d62016-03-23 19:40:35 +0530922 * @param genFiletype generated file type
Vinod Kumar S38046502016-03-23 15:30:27 +0530923 * @throws IOException when fails to append to temporary file
924 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530925 private void addGetterImpl(JavaAttributeInfo attr, int genFiletype) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530926
Bharat saraswale2d51d62016-03-23 19:40:35 +0530927 if ((genFiletype & BUILDER_CLASS_MASK) != 0) {
928 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr) + NEW_LINE);
929 } else {
930 appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
931 + getGetterForClass(attr) + NEW_LINE);
932 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530933 }
934
935 /**
936 * Adds setter for interface.
937 *
938 * @param attr attribute info
939 * @throws IOException when fails to append to temporary file
940 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530941 private void addSetterForInterface(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530942 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530943 getSetterString(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530944 }
945
946 /**
947 * Adds setter's implementation for class.
948 *
949 * @param attr attribute info
950 * @throws IOException when fails to append to temporary file
951 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530952 private void addSetterImpl(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530953 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530954 getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530955 }
956
957 /**
958 * Adds build method for interface.
959 *
960 * @return build method for interface
961 * @throws IOException when fails to append to temporary file
962 */
963 public String addBuildMethodForInterface() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530964 return parseBuilderInterfaceBuildMethodString(generatedJavaClassName);
965 }
966
967 /**
968 * Adds build method's implementation for class.
969 *
970 * @return build method implementation for class
971 * @throws IOException when fails to append to temporary file
972 */
973 public String addBuildMethodImpl() throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530974 return getBuildString(generatedJavaClassName) + NEW_LINE;
Vinod Kumar S38046502016-03-23 15:30:27 +0530975 }
976
977 /**
978 * Adds constructor for class.
979 *
980 * @param attr attribute info
981 * @throws IOException when fails to append to temporary file
982 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530983 private void addConstructor(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530984 appendToFile(getConstructorImplTempFileHandle(), getConstructor(generatedJavaClassName, attr));
985 }
986
987 /**
988 * Adds default constructor for class.
989 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530990 * @param modifier modifier for constructor.
991 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530992 * @return default constructor for class
993 * @throws IOException when fails to append to file
994 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530995 public String addDefaultConstructor(String modifier, String toAppend) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530996 return NEW_LINE + getDefaultConstructorString(generatedJavaClassName + toAppend, modifier);
997 }
998
999 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301000 * Adds default constructor for class.
1001 *
1002 * @return default constructor for class
1003 * @throws IOException when fails to append to file
1004 */
1005 public String addOfMethod() throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301006 return getJavaDoc(OF_METHOD, generatedJavaClassName, false)
Bharat saraswale2d51d62016-03-23 19:40:35 +05301007 + getOfMethod(generatedJavaClassName, newAttrInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +05301008 }
1009
1010 /**
1011 * Adds hash code method for class.
1012 *
1013 * @param attr attribute info
1014 * @throws IOException when fails to append to temporary file
1015 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301016 private void addHashCodeMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301017 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301018 }
1019
1020 /**
1021 * Adds equals method for class.
1022 *
1023 * @param attr attribute info
1024 * @throws IOException when fails to append to temporary file
1025 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301026 private void addEqualsMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301027 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301028 }
1029
1030 /**
1031 * Adds ToString method for class.
1032 *
1033 * @param attr attribute info
1034 * @throws IOException when fails to append to temporary file
1035 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301036 private void addToStringMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301037 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301038 }
1039
1040 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301041 * Add from string method for union class.
1042 *
1043 * @param javaAttributeInfo type attribute info
1044 * @param fromStringAttributeInfo from string attribute info
1045 * @throws IOException when fails to append to temporary file
1046 */
1047 private void addUnionFromStringMethod(JavaAttributeInfo javaAttributeInfo,
1048 JavaAttributeInfo fromStringAttributeInfo) throws IOException {
1049 appendToFile(getUnionFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1050 fromStringAttributeInfo) + NEW_LINE);
1051 }
1052
1053 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301054 * Returns a temporary file handle for the specific file type.
1055 *
1056 * @param fileName file name
1057 * @return temporary file handle
1058 * @throws IOException when fails to create new file handle
1059 */
1060 private File getTemporaryFileHandle(String fileName) throws IOException {
1061
1062 String path = getTempDirPath();
1063 File dir = new File(path);
1064 if (!dir.exists()) {
1065 dir.mkdirs();
1066 }
1067
1068 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1069 if (!file.exists()) {
1070 file.createNewFile();
Vinod Kumar S38046502016-03-23 15:30:27 +05301071 }
1072 return file;
1073 }
1074
1075 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301076 * Returns a temporary file handle for the specific file type.
1077 *
1078 * @param fileName file name
1079 * @return temporary file handle
1080 * @throws IOException when fails to create new file handle
1081 */
1082 private File getJavaFileHandle(String fileName) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301083 createPackage(absoluteDirPath, getJavaFileInfo().getJavaName());
Bharat saraswale2d51d62016-03-23 19:40:35 +05301084 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1085 }
1086
1087 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301088 * Returns data from the temporary files.
1089 *
1090 * @param file temporary file handle
1091 * @return stored data from temporary files
1092 * @throws IOException when failed to get data from the given file
1093 */
1094 public String getTemporaryDataFromFileHandle(File file) throws IOException {
1095
1096 String path = getTempDirPath();
1097 if (new File(path + file.getName()).exists()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301098 return readAppendFile(path + file.getName(), EMPTY_STRING);
Vinod Kumar S38046502016-03-23 15:30:27 +05301099 } else {
1100 throw new IOException("Unable to get data from the given "
Bharat saraswale2d51d62016-03-23 19:40:35 +05301101 + file.getName() + " file for " + generatedJavaClassName + PERIOD);
Vinod Kumar S38046502016-03-23 15:30:27 +05301102 }
1103 }
1104
1105 /**
1106 * Returns temporary directory path.
1107 *
1108 * @return directory path
1109 */
1110 private String getTempDirPath() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301111 return getPackageDirPathFromJavaJPackage(absoluteDirPath) + SLASH + generatedJavaClassName
1112 + TEMP_FOLDER_NAME_SUFIX + SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +05301113 }
1114
1115 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301116 * Parses attribute to get the attribute string.
Vinod Kumar S38046502016-03-23 15:30:27 +05301117 *
1118 * @param attr attribute info
1119 * @return attribute string
1120 */
1121 private String parseAttribute(JavaAttributeInfo attr) {
1122
1123 /*
1124 * TODO: check if this utility needs to be called or move to the caller
1125 */
janani bde4ffab2016-04-15 16:18:30 +05301126 String attributeName = getCamelCase(getSmallCase(attr.getAttributeName()), null);
Vinod Kumar S38046502016-03-23 15:30:27 +05301127 if (attr.isQualifiedName()) {
1128 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
1129 attributeName, attr.isListAttr());
1130 } else {
1131 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1132 attr.isListAttr());
1133 }
1134 }
1135
1136 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301137 * Appends content to temporary file.
Vinod Kumar S38046502016-03-23 15:30:27 +05301138 *
1139 * @param file temporary file
1140 * @param data data to be appended
1141 * @throws IOException when fails to append to file
1142 */
1143 private void appendToFile(File file, String data) throws IOException {
1144
1145 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301146 insertDataIntoJavaFile(file, data);
Vinod Kumar S38046502016-03-23 15:30:27 +05301147 } catch (IOException ex) {
1148 throw new IOException("failed to write in temp file.");
1149 }
1150 }
1151
1152 /**
1153 * Adds current node info as and attribute to the parent generated file.
1154 *
1155 * @param curNode current node which needs to be added as an attribute in
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301156 * the parent generated code
1157 * @param isList is list construct
Vinod Kumar S38046502016-03-23 15:30:27 +05301158 * @throws IOException IO operation exception
1159 */
1160 public void addCurNodeInfoInParentTempFile(YangNode curNode,
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301161 boolean isList) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301162
1163 YangNode parent = getParentNodeInGenCode(curNode);
1164 if (!(parent instanceof JavaCodeGenerator)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301165 throw new TranslatorException("missing parent node to contain current node info in generated file");
Vinod Kumar S38046502016-03-23 15:30:27 +05301166 }
1167 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1168 parent, isList);
1169
1170 if (!(parent instanceof HasTempJavaCodeFragmentFiles)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301171 throw new TranslatorException("missing parent temp file handle");
Vinod Kumar S38046502016-03-23 15:30:27 +05301172 }
1173 ((HasTempJavaCodeFragmentFiles) parent)
1174 .getTempJavaCodeFragmentFiles()
1175 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1176 }
1177
1178 /**
1179 * Adds leaf attributes in generated files.
1180 *
1181 * @param listOfLeaves list of YANG leaf
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301182 * @param curNode current data model node
Vinod Kumar S38046502016-03-23 15:30:27 +05301183 * @throws IOException IO operation fail
1184 */
1185 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301186 YangNode curNode) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301187
1188 if (listOfLeaves != null) {
1189 for (YangLeaf leaf : listOfLeaves) {
1190 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(curNode,
1191 leaf.getDataType(),
1192 leaf.getLeafName(), false);
1193 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1194 }
1195 }
1196 }
1197
1198 /**
1199 * Adds leaf list's attributes in generated files.
1200 *
1201 * @param listOfLeafList list of YANG leaves
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301202 * @param curNode cached file handle
Vinod Kumar S38046502016-03-23 15:30:27 +05301203 * @throws IOException IO operation fail
1204 */
1205 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList,
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301206 YangNode curNode) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301207
1208 if (listOfLeafList != null) {
1209
1210 /*
1211 * Check if the attribute is of type list, then the java.lang.list
1212 * needs to be imported.
1213 */
1214 if (listOfLeafList.size() != 0) {
1215 if (!(curNode instanceof HasJavaImportData)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301216 throw new TranslatorException("missing import info in current data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +05301217
1218 }
1219 ((HasJavaImportData) curNode).getJavaImportData()
1220 .setIfListImported(true);
1221
1222 }
1223
1224 for (YangLeafList leafList : listOfLeafList) {
1225 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(
1226 curNode, leafList.getDataType(), leafList.getLeafName(),
1227 true);
1228 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1229 }
1230 }
1231 }
1232
1233 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301234 * Adds all the leaves in the current data model node as part of the
Vinod Kumar S38046502016-03-23 15:30:27 +05301235 * generated temporary file.
1236 *
1237 * @param curNode java file info of the generated file
1238 * @throws IOException IO operation fail
1239 */
1240 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode) throws IOException {
1241
1242 if (curNode instanceof YangLeavesHolder) {
1243 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
1244 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), curNode);
1245 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), curNode);
1246 }
1247 }
1248
1249 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301250 * Add all the type in the current data model node as part of the
1251 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301252 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301253 * @param hasType YANG java data model node which has type info, eg union / typedef
Bharat saraswale2d51d62016-03-23 19:40:35 +05301254 * @throws IOException IO operation fail
1255 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301256 public void addTypeInfoToTempFiles(HasType hasType) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301257
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301258 List<YangType<?>> typeList = hasType.getTypeList();
1259 if (typeList != null) {
1260 for (YangType<?> yangType : typeList) {
1261 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfType((YangNode) hasType,
1262 yangType, getTypeName(yangType.getDataTypeName()), false);
1263 addJavaSnippetInfoToApplicableTempFiles((YangNode) hasType, javaAttributeInfo);
1264 }
1265 }
1266 }
1267
1268 private String getTypeName(String dataTypeName) {
1269 return dataTypeName;
1270 //TODO: implement the method.
1271 }
1272
1273 /**
1274 * Adds the new attribute info to the target generated temporary files for union class.
1275 *
1276 * @param hasType the node for which the type is being added as an attribute
1277 * @param javaAttributeInfo the attribute info that needs to be added to temporary
1278 * files
1279 * @throws IOException IO operation fail
1280 */
1281 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo)
1282 throws IOException {
1283
1284 JavaAttributeInfo fromStringAttributeInfo = getFromStringAttributeInfo(hasType, javaAttributeInfo);
1285
1286 if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
1287 addUnionFromStringMethod(javaAttributeInfo, fromStringAttributeInfo);
1288 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301289 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1290 }
1291
1292 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301293 * Adds the new attribute info to the target generated temporary files.
Vinod Kumar S38046502016-03-23 15:30:27 +05301294 *
1295 * @param newAttrInfo the attribute info that needs to be added to temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301296 * files
Vinod Kumar S38046502016-03-23 15:30:27 +05301297 * @throws IOException IO operation fail
1298 */
1299 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1300 throws IOException {
1301
Bharat saraswale2d51d62016-03-23 19:40:35 +05301302 setNewAttrInfo(newAttrInfo);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301303 if (isAttributePresent) {
1304 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
1305 addAttribute(newAttrInfo);
1306 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301307
Bharat saraswal2f11f652016-03-25 18:19:46 +05301308 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
1309 addGetterForInterface(newAttrInfo);
1310 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301311
Bharat saraswal2f11f652016-03-25 18:19:46 +05301312 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
1313 addSetterForInterface(newAttrInfo);
1314 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301315
Bharat saraswal2f11f652016-03-25 18:19:46 +05301316 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
1317 addGetterImpl(newAttrInfo, generatedJavaFiles);
1318 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301319
Bharat saraswal2f11f652016-03-25 18:19:46 +05301320 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
1321 addSetterImpl(newAttrInfo);
1322 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301323
Bharat saraswal2f11f652016-03-25 18:19:46 +05301324 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
1325 addConstructor(newAttrInfo);
1326 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301327
Bharat saraswal2f11f652016-03-25 18:19:46 +05301328 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
1329 addHashCodeMethod(newAttrInfo);
1330 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301331
Bharat saraswal2f11f652016-03-25 18:19:46 +05301332 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
1333 addEqualsMethod(newAttrInfo);
1334 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301335
Bharat saraswal2f11f652016-03-25 18:19:46 +05301336 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
1337 addToStringMethod(newAttrInfo);
1338 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301339
1340 if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
1341 addOfStringMethod(newAttrInfo);
1342 }
1343
1344 if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
1345 addTypeConstructor(newAttrInfo);
1346 }
1347
Vinod Kumar S38046502016-03-23 15:30:27 +05301348 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301349 }
1350
1351 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301352 * Returns java file info.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301353 *
1354 * @return java file info
1355 */
1356 private JavaFileInfo getJavaFileInfo() {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301357 return ((HasJavaFileInfo) getCurYangNode()).getJavaFileInfo();
1358 }
1359
1360 /**
1361 * Returns java class name.
1362 *
1363 * @param suffix for the class name based on the file type
1364 * @return java class name
1365 */
1366 private String getJavaClassName(String suffix) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301367 return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
Bharat saraswale2d51d62016-03-23 19:40:35 +05301368 }
1369
1370 /**
1371 * Returns the directory path.
1372 *
1373 * @return directory path
1374 */
1375 private String getDirPath() {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301376 return getJavaFileInfo().getPackageFilePath();
1377 }
1378
1379 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301380 * Constructs java code exit.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301381 *
1382 * @param fileType generated file type
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301383 * @param curNode current YANG node
Bharat saraswale2d51d62016-03-23 19:40:35 +05301384 * @throws IOException when fails to generate java files
1385 */
1386 public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
1387
1388 setCurYangNode(curNode);
1389 List<String> imports = new ArrayList<>();
Bharat saraswal2f11f652016-03-25 18:19:46 +05301390 if (curNode instanceof HasJavaImportData && isAttributePresent) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301391 imports = ((HasJavaImportData) curNode).getJavaImportData().getImports(getNewAttrInfo());
1392 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301393
Bharat saraswale2d51d62016-03-23 19:40:35 +05301394 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301395 * Prepares java file generator for extends list.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301396 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301397 prepareJavaFileGeneratorForExtendsList(getExtendsList());
1398
1399 /**
1400 * Generate java code.
1401 */
1402 if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0) {
1403
1404 /**
1405 * Adds import for HasAugmentation class.
1406 */
1407 if (isHasAugmentationExtended(getExtendsList())) {
1408 addHasAugmentationImport(curNode, imports, true);
1409 }
1410
1411 if (isAugmentedInfoExtended(getExtendsList())) {
1412 addAugmentedInfoImport(curNode, imports, true);
1413 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301414
1415 /**
1416 * Create interface file.
1417 */
1418 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301419 setInterfaceJavaFileHandle(
1420 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301421 /**
1422 * Create builder interface file.
1423 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301424 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1425
1426 setBuilderInterfaceJavaFileHandle(
1427 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1428 setBuilderInterfaceJavaFileHandle(
1429 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, isAttributePresent));
1430 /**
1431 * Append builder interface file to interface file and close it.
1432 */
1433 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1434 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301435 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswale2d51d62016-03-23 19:40:35 +05301436
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301437 if (isHasAugmentationExtended(getExtendsList())) {
1438 addHasAugmentationImport(curNode, imports, false);
1439 }
1440 if (isAugmentedInfoExtended(getExtendsList())) {
1441 addAugmentedInfoImport(curNode, imports, false);
1442 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301443 }
1444
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301445 if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301446
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301447 if (isAttributePresent) {
1448 addImportsToStringAndHasCodeMethods(curNode, imports);
1449 }
1450 if (isHasAugmentationExtended(getExtendsList())) {
1451 addAugmentedInfoImport(curNode, imports, true);
1452 addArrayListImport(curNode, imports, true);
1453 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301454
1455 /**
1456 * Create builder class file.
1457 */
1458 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301459 setBuilderClassJavaFileHandle(
1460 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301461 /**
1462 * Create impl class file.
1463 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301464 if ((fileType & IMPL_CLASS_MASK) != 0) {
1465 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1466 setImplClassJavaFileHandle(
1467 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent));
1468 /**
1469 * Append impl class to builder class and close it.
1470 */
1471 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1472 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301473 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswale2d51d62016-03-23 19:40:35 +05301474 }
1475
1476 /**
1477 * Creates type def class file.
1478 */
1479 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301480 addImportsToStringAndHasCodeMethods(curNode, imports);
Bharat saraswale2d51d62016-03-23 19:40:35 +05301481 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
1482 setTypedefClassJavaFileHandle(generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports));
1483 }
1484
1485 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301486 * Creates type class file.
1487 */
1488 if ((fileType & GENERATE_UNION_CLASS) != 0) {
1489 addImportsToStringAndHasCodeMethods(curNode, imports);
1490 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
1491 setTypeClassJavaFileHandle(generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports));
1492 }
1493
1494 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301495 * Close all the file handles.
1496 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301497 close(false);
Bharat saraswale2d51d62016-03-23 19:40:35 +05301498 }
1499
1500 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301501 * Removes all temporary file handles.
1502 *
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301503 * @param isErrorOccurred when translator fails to generate java files we need to close
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301504 * all open file handles include temporary files and java files.
Vinod Kumar S38046502016-03-23 15:30:27 +05301505 * @throws IOException when failed to delete the temporary files
1506 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301507 public void close(boolean isErrorOccurred) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301508
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301509 boolean isError = isErrorOccurred;
1510 /**
1511 * Close all java file handles and when error occurs delete the files.
1512 */
Bharat saraswal2f11f652016-03-25 18:19:46 +05301513 if ((generatedJavaFiles & INTERFACE_MASK) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301514 closeFile(getInterfaceJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301515 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301516 if ((generatedJavaFiles & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301517 closeFile(getBuilderClassJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301518 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301519 if ((generatedJavaFiles & BUILDER_INTERFACE_MASK) != 0) {
1520 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1521 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301522 if ((generatedJavaFiles & IMPL_CLASS_MASK) != 0) {
1523 closeFile(getImplClassJavaFileHandle(), true);
1524 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301525 if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301526 closeFile(getTypedefClassJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301527 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301528 if ((generatedJavaFiles & GENERATE_UNION_CLASS) != 0) {
1529 closeFile(getTypeClassJavaFileHandle(), isError);
1530 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301531
Bharat saraswal2f11f652016-03-25 18:19:46 +05301532 /**
1533 * Close all temporary file handles and delete the files.
1534 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301535 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
1536 closeFile(getGetterInterfaceTempFileHandle(), true);
1537 }
1538 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
1539 closeFile(getGetterImplTempFileHandle(), true);
1540 }
1541 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
1542 closeFile(getSetterInterfaceTempFileHandle(), true);
1543 }
1544 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
1545 closeFile(getSetterImplTempFileHandle(), true);
1546 }
1547 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
1548 closeFile(getConstructorImplTempFileHandle(), true);
1549 }
1550 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
1551 closeFile(getAttributesTempFileHandle(), true);
1552 }
1553 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
1554 closeFile(getHashCodeImplTempFileHandle(), true);
1555 }
1556 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
1557 closeFile(getToStringImplTempFileHandle(), true);
1558 }
1559 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
1560 closeFile(getEqualsImplTempFileHandle(), true);
1561 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301562 if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
1563 closeFile(getConstructorForTypeTempFileHandle(), true);
1564 }
1565 if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
1566 closeFile(getOfStringImplTempFileHandle(), true);
1567 }
1568 if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
1569 closeFile(getUnionFromStringImplTempFileHandle(), true);
1570 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301571 clean(getTempDirPath());
1572 generatedTempFiles = 0;
Vinod Kumar S38046502016-03-23 15:30:27 +05301573 }
1574
Vinod Kumar S38046502016-03-23 15:30:27 +05301575}