blob: efc106eee2c3f78165ff643c11ed5089ae5b749e [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;
Bharat saraswald72411a2016-04-19 01:00:16 +053023import java.util.Set;
24
Gaurav Agrawal338735b2016-04-18 18:53:11 +053025import org.onosproject.yangutils.datamodel.HasType;
Bharat saraswald72411a2016-04-19 01:00:16 +053026import org.onosproject.yangutils.datamodel.YangEnum;
27import org.onosproject.yangutils.datamodel.YangEnumeration;
Vinod Kumar S38046502016-03-23 15:30:27 +053028import org.onosproject.yangutils.datamodel.YangLeaf;
29import org.onosproject.yangutils.datamodel.YangLeafList;
30import org.onosproject.yangutils.datamodel.YangLeavesHolder;
31import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053032import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053033import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053034
Bharat saraswald72411a2016-04-19 01:00:16 +053035import static org.onosproject.yangutils.datamodel.YangNodeType.MODULE_NODE;
Vinod Kumar S38046502016-03-23 15:30:27 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswald72411a2016-04-19 01:00:16 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Vinod Kumar S38046502016-03-23 15:30:27 +053041import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
Vinod Kumar S38046502016-03-23 15:30:27 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
Bharat saraswald72411a2016-04-19 01:00:16 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
Vinod Kumar S38046502016-03-23 15:30:27 +053047import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
49import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
50import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053051import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
Vinod Kumar S38046502016-03-23 15:30:27 +053052import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
53import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
54import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053055import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
Bharat saraswald72411a2016-04-19 01:00:16 +053056import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfEnumAttribute;
Vinod Kumar S38046502016-03-23 15:30:27 +053057import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053058import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfType;
Vinod Kumar S38046502016-03-23 15:30:27 +053059import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053060import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getFromStringAttributeInfo;
Bharat saraswald72411a2016-04-19 01:00:16 +053061import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
Vinod Kumar S38046502016-03-23 15:30:27 +053062import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053063import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswale2d51d62016-03-23 19:40:35 +053064import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
65import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Bharat saraswald72411a2016-04-19 01:00:16 +053066import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
Bharat saraswale2d51d62016-03-23 19:40:35 +053067import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
68import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
69import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswale2d51d62016-03-23 19:40:35 +053071import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053072import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
73import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
74import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Vinod Kumar S38046502016-03-23 15:30:27 +053075import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053076import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
Vinod Kumar S38046502016-03-23 15:30:27 +053077import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
78import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
79import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
80import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053081import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar S38046502016-03-23 15:30:27 +053082import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
83import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
84import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Bharat saraswale2d51d62016-03-23 19:40:35 +053085import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053086import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
Vinod Kumar S38046502016-03-23 15:30:27 +053087import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
88import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
89import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
90import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053091import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Vinod Kumar S38046502016-03-23 15:30:27 +053092import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053093import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
94import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
95import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport;
96import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addImportsToStringAndHasCodeMethods;
97import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
98import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
99import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
100import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.prepareJavaFileGeneratorForExtendsList;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
102import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
103import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
104import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
105import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
106import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswald72411a2016-04-19 01:00:16 +0530107import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE_INFO_JAVADOC_OF_CHILD;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530108import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
109import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
110import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530111import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswald72411a2016-04-19 01:00:16 +0530112import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530113import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530114import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530115import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
116import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
117import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +0530118
119/**
Bharat saraswald9822e92016-04-05 15:13:44 +0530120 * Represents implementation of java code fragments temporary implementations.
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 */
122public class TempJavaCodeFragmentFiles {
123
124 /**
125 * The variable which guides the types of temporary files generated using
126 * the temporary generated file types mask.
127 */
128 private int generatedTempFiles;
129
130 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530131 * The variable which guides the types of files generated using
132 * the generated file types mask.
133 */
134 private int generatedJavaFiles;
135
136 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530137 * Absolute path where the target java file needs to be generated.
138 */
139 private String absoluteDirPath;
140
141 /**
142 * Name of java file that needs to be generated.
143 */
144 private String generatedJavaClassName;
145
146 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530147 * Contains all the class name which will be extended by generated files.
148 */
149 private List<String> extendsList = new ArrayList<>();
150
151 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530152 * File type extension for java classes.
153 */
154 private static final String JAVA_FILE_EXTENSION = ".java";
155
156 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530157 * File type extension for temporary classes.
158 */
159 private static final String TEMP_FILE_EXTENSION = ".tmp";
160
161 /**
162 * Folder suffix for temporary files folder.
163 */
164 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
165
166 /**
167 * File name for getter method.
168 */
169 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
170
171 /**
172 * File name for getter method implementation.
173 */
174 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
175
176 /**
177 * File name for setter method.
178 */
179 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
180
181 /**
182 * File name for setter method implementation.
183 */
184 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
185
186 /**
187 * File name for constructor.
188 */
189 private static final String CONSTRUCTOR_FILE_NAME = "Constructor";
190
191 /**
192 * File name for attributes.
193 */
194 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
195
196 /**
197 * File name for to string method.
198 */
199 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
200
201 /**
202 * File name for hash code method.
203 */
204 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
205
206 /**
207 * File name for equals method.
208 */
209 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
210
211 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530212 * File name for of string method.
213 */
214 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
215
216 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530217 * File name for temporary enum class.
218 */
219 private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
220
221 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530222 * File name for construction for special type like union, typedef.
223 */
224 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
225
226 /**
227 * File name for from string method.
228 */
229 private static final String UNION_FROM_STRING_METHOD_FILE_NAME = "UnionFromString";
230
231 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530232 * File name for interface java file name suffix.
233 */
234 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
235
236 /**
237 * File name for builder interface file name suffix.
238 */
239 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
240
241 /**
242 * File name for builder class file name suffix.
243 */
244 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
245
246 /**
247 * File name for impl class file name suffix.
248 */
249 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
250
251 /**
252 * File name for typedef class file name suffix.
253 */
254 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
255
256 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530257 * File name for enum class file name suffix.
258 */
259 private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
260
261 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530262 * File name for generated class file for special type like union, typedef suffix.
263 */
264 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
265
266 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530267 * Java file handle for interface file.
268 */
269 private File interfaceJavaFileHandle;
270
271 /**
272 * Java file handle for builder interface file.
273 */
274 private File builderInterfaceJavaFileHandle;
275
276 /**
277 * Java file handle for builder class file.
278 */
279 private File builderClassJavaFileHandle;
280
281 /**
282 * Java file handle for impl class file.
283 */
284 private File implClassJavaFileHandle;
285
286 /**
287 * Java file handle for typedef class file.
288 */
289 private File typedefClassJavaFileHandle;
290
291 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530292 * Java file handle for type class like union, typedef file.
293 */
294 private File typeClassJavaFileHandle;
295
296 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530297 * Temporary file handle for attribute.
298 */
299 private File attributesTempFileHandle;
300
301 /**
302 * Temporary file handle for getter of interface.
303 */
304 private File getterInterfaceTempFileHandle;
305
306 /**
307 * Temporary file handle for getter of class.
308 */
309 private File getterImplTempFileHandle;
310
311 /**
312 * Temporary file handle for setter of interface.
313 */
314 private File setterInterfaceTempFileHandle;
315
316 /**
317 * Temporary file handle for setter of class.
318 */
319 private File setterImplTempFileHandle;
320
321 /**
322 * Temporary file handle for constructor of class.
323 */
324 private File constructorImplTempFileHandle;
325
326 /**
327 * Temporary file handle for hash code method of class.
328 */
329 private File hashCodeImplTempFileHandle;
330
331 /**
332 * Temporary file handle for equals method of class.
333 */
334 private File equalsImplTempFileHandle;
335
336 /**
337 * Temporary file handle for to string method of class.
338 */
339 private File toStringImplTempFileHandle;
340
341 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530342 * Temporary file handle for enum class file.
343 */
344 private File enumClassTempFileHandle;
345
346 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530347 * Temporary file handle for of string method of class.
348 */
349 private File ofStringImplTempFileHandle;
350
351 /**
352 * Temporary file handle for constructor for type class.
353 */
354 private File constructorForTypeTempFileHandle;
355
356 /**
357 * Temporary file handle for union's from string method of class.
358 */
359 private File unionFromStringImplTempFileHandle;
360
361 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530362 * Java attribute info.
363 */
364 private JavaAttributeInfo newAttrInfo;
365
366 /**
367 * Current YANG node.
368 */
369 private YangNode curYangNode;
370
371 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530372 * Current enum's value.
373 */
374 private int enumValue;
375
376 /**
Bharat saraswal2f11f652016-03-25 18:19:46 +0530377 * Is attribute added.
378 */
379 private boolean isAttributePresent = false;
380
Bharat saraswald72411a2016-04-19 01:00:16 +0530381 /*
382 * Java file handle for enum class.
383 */
384 private File enumClassJavaFileHandle;
385
Bharat saraswal2f11f652016-03-25 18:19:46 +0530386 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530387 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +0530388 *
389 * @param genFileType file generation type
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530390 * @param genDir file generation directory
391 * @param className class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530392 * @throws IOException when fails to create new file handle
393 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530394 public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className)
395 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530396
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530397 setExtendsList(new ArrayList<>());
Vinod Kumar S38046502016-03-23 15:30:27 +0530398 generatedTempFiles = 0;
399 absoluteDirPath = genDir;
400 generatedJavaClassName = className;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530401 generatedJavaFiles = genFileType;
Vinod Kumar S38046502016-03-23 15:30:27 +0530402 /**
403 * Initialize getter when generation file type matches to interface
404 * mask.
405 */
406 if ((genFileType & INTERFACE_MASK) != 0) {
407 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
408 }
409
410 /**
411 * Initialize getter and setter when generation file type matches to
412 * builder interface mask.
413 */
414 if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
415 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
416 generatedTempFiles |= SETTER_FOR_INTERFACE_MASK;
417 }
418
419 /**
420 * Initialize getterImpl, setterImpl and attributes when generation file
421 * type matches to builder class mask.
422 */
423 if ((genFileType & BUILDER_CLASS_MASK) != 0) {
424 generatedTempFiles |= ATTRIBUTES_MASK;
425 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
426 generatedTempFiles |= SETTER_FOR_CLASS_MASK;
427 }
428
429 /**
430 * Initialize getterImpl, attributes, constructor, hash code, equals and
431 * to strings when generation file type matches to impl class mask.
432 */
433 if ((genFileType & IMPL_CLASS_MASK) != 0) {
434 generatedTempFiles |= ATTRIBUTES_MASK;
435 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
436 generatedTempFiles |= CONSTRUCTOR_IMPL_MASK;
437 generatedTempFiles |= HASH_CODE_IMPL_MASK;
438 generatedTempFiles |= EQUALS_IMPL_MASK;
439 generatedTempFiles |= TO_STRING_IMPL_MASK;
440 }
441
Bharat saraswale2d51d62016-03-23 19:40:35 +0530442 /**
443 * Initialize getterImpl, attributes, hash code, equals and
444 * to strings when generation file type matches to typeDef class mask.
445 */
446 if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
447 generatedTempFiles |= ATTRIBUTES_MASK;
448 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
449 generatedTempFiles |= HASH_CODE_IMPL_MASK;
450 generatedTempFiles |= EQUALS_IMPL_MASK;
451 generatedTempFiles |= TO_STRING_IMPL_MASK;
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530452 generatedTempFiles |= OF_STRING_IMPL_MASK;
453 generatedTempFiles |= CONSTRUCTOR_FOR_TYPE_MASK;
454 }
455
456 /**
457 * Initialize getterImpl, attributes, hash code, equals, of string,
458 * constructor, union's to string, union's from string when generation
459 * file type matches to union class mask.
460 */
461 if ((genFileType & GENERATE_UNION_CLASS) != 0) {
462 generatedTempFiles |= ATTRIBUTES_MASK;
463 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
464 generatedTempFiles |= HASH_CODE_IMPL_MASK;
465 generatedTempFiles |= EQUALS_IMPL_MASK;
466 generatedTempFiles |= OF_STRING_IMPL_MASK;
467 generatedTempFiles |= CONSTRUCTOR_FOR_TYPE_MASK;
468 generatedTempFiles |= TO_STRING_IMPL_MASK;
469 generatedTempFiles |= UNION_FROM_STRING_IMPL_MASK;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530470 }
Bharat saraswald72411a2016-04-19 01:00:16 +0530471 /**
472 * Initialize enum when generation file type matches to enum class mask.
473 */
474 if ((genFileType & GENERATE_ENUM_CLASS) != 0) {
475 generatedTempFiles |= ENUM_IMPL_MASK;
476 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530477
Bharat saraswald72411a2016-04-19 01:00:16 +0530478 /**
479 * Set temporary file handles.
480 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530481 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
482 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
483 }
484
485 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
486 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
487 }
488
489 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
490 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
491 }
492
493 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
494 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
495 }
496
497 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
498 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
499 }
500
501 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
502 setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME));
503 }
504
505 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
506 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
507 }
508
509 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
510 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
511 }
512 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
513 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
514 }
Bharat saraswald72411a2016-04-19 01:00:16 +0530515 if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
516 setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
517 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530518 if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
519 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
520 }
521
522 if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
523 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
524 }
525
526 if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
527 setUnionFromStringImplTempFileHandle(getTemporaryFileHandle(UNION_FROM_STRING_METHOD_FILE_NAME));
528 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530529 }
530
531 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530532 * Returns java file handle for interface file.
533 *
534 * @return java file handle for interface file
535 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530536 private File getInterfaceJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530537 return interfaceJavaFileHandle;
538 }
539
540 /**
541 * Sets the java file handle for interface file.
542 *
543 * @param interfaceJavaFileHandle java file handle
544 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530545 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530546 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
547 }
548
549 /**
550 * Returns java file handle for builder interface file.
551 *
552 * @return java file handle for builder interface file
553 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530554 private File getBuilderInterfaceJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530555 return builderInterfaceJavaFileHandle;
556 }
557
558 /**
559 * Sets the java file handle for builder interface file.
560 *
561 * @param builderInterfaceJavaFileHandle java file handle
562 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530563 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530564 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
565 }
566
567 /**
568 * Returns java file handle for builder class file.
569 *
570 * @return java file handle for builder class file
571 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530572 private File getBuilderClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530573 return builderClassJavaFileHandle;
574 }
575
576 /**
577 * Sets the java file handle for builder class file.
578 *
579 * @param builderClassJavaFileHandle java file handle
580 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530581 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530582 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
583 }
584
585 /**
586 * Returns java file handle for impl class file.
587 *
588 * @return java file handle for impl class file
589 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530590 private File getImplClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530591 return implClassJavaFileHandle;
592 }
593
594 /**
595 * Sets the java file handle for impl class file.
596 *
597 * @param implClassJavaFileHandle java file handle
598 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530599 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530600 this.implClassJavaFileHandle = implClassJavaFileHandle;
601 }
602
603 /**
604 * Returns java file handle for typedef class file.
605 *
606 * @return java file handle for typedef class file
607 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530608 private File getTypedefClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530609 return typedefClassJavaFileHandle;
610 }
611
612 /**
613 * Sets the java file handle for typedef class file.
614 *
615 * @param typedefClassJavaFileHandle java file handle
616 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530617 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530618 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
619 }
620
621 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530622 * Returns enum class java file handle.
623 *
624 * @return enum class java file handle
625 */
626 private File getEnumClassJavaFileHandle() {
627 return enumClassJavaFileHandle;
628 }
629
630 /**
631 * Sets enum class java file handle.
632 *
633 * @param enumClassJavaFileHandle enum class java file handle
634 */
635
636 private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) {
637 this.enumClassJavaFileHandle = enumClassJavaFileHandle;
638 }
639
640 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530641 * Returns java file handle for type class file.
642 *
643 * @return java file handle for type class file
644 */
645 private File getTypeClassJavaFileHandle() {
646 return typeClassJavaFileHandle;
647 }
648
649 /**
650 * Sets the java file handle for type class file.
651 *
652 * @param typeClassJavaFileHandle type file handle
653 */
654 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
655 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
656 }
657
658 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530659 * Returns attribute's temporary file handle.
660 *
661 * @return temporary file handle
662 */
663 public File getAttributesTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530664 return attributesTempFileHandle;
665 }
666
667 /**
668 * Sets attribute's temporary file handle.
669 *
670 * @param attributeForClass file handle for attribute
671 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530672 private void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530673 attributesTempFileHandle = attributeForClass;
674 }
675
676 /**
677 * Returns getter methods's temporary file handle.
678 *
679 * @return temporary file handle
680 */
681 public File getGetterInterfaceTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530682 return getterInterfaceTempFileHandle;
683 }
684
685 /**
686 * Sets to getter method's temporary file handle.
687 *
688 * @param getterForInterface file handle for to getter method
689 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530690 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530691 getterInterfaceTempFileHandle = getterForInterface;
692 }
693
694 /**
695 * Returns getter method's impl's temporary file handle.
696 *
697 * @return temporary file handle
698 */
699 public File getGetterImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530700 return getterImplTempFileHandle;
701 }
702
703 /**
704 * Sets to getter method's impl's temporary file handle.
705 *
706 * @param getterImpl file handle for to getter method's impl
707 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530708 private void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530709 getterImplTempFileHandle = getterImpl;
710 }
711
712 /**
713 * Returns setter method's temporary file handle.
714 *
715 * @return temporary file handle
716 */
717 public File getSetterInterfaceTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530718 return setterInterfaceTempFileHandle;
719 }
720
721 /**
722 * Sets to setter method's temporary file handle.
723 *
724 * @param setterForInterface file handle for to setter method
725 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530726 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530727 setterInterfaceTempFileHandle = setterForInterface;
728 }
729
730 /**
731 * Returns setter method's impl's temporary file handle.
732 *
733 * @return temporary file handle
734 */
735 public File getSetterImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530736 return setterImplTempFileHandle;
737 }
738
739 /**
740 * Sets to setter method's impl's temporary file handle.
741 *
742 * @param setterImpl file handle for to setter method's implementation class
743 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530744 private void setSetterImplTempFileHandle(File setterImpl) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530745 setterImplTempFileHandle = setterImpl;
746 }
747
748 /**
749 * Returns constructor's temporary file handle.
750 *
751 * @return temporary file handle
752 */
753 public File getConstructorImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530754 return constructorImplTempFileHandle;
755 }
756
757 /**
758 * Sets to constructor's temporary file handle.
759 *
760 * @param constructor file handle for to constructor
761 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530762 private void setConstructorImplTempFileHandle(File constructor) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530763 constructorImplTempFileHandle = constructor;
764 }
765
766 /**
767 * Returns hash code method's temporary file handle.
768 *
769 * @return temporary file handle
770 */
771 public File getHashCodeImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530772 return hashCodeImplTempFileHandle;
773 }
774
775 /**
776 * Sets hash code method's temporary file handle.
777 *
778 * @param hashCodeMethod file handle for hash code method
779 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530780 private void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530781 hashCodeImplTempFileHandle = hashCodeMethod;
782 }
783
784 /**
785 * Returns equals mehtod's temporary file handle.
786 *
787 * @return temporary file handle
788 */
789 public File getEqualsImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530790 return equalsImplTempFileHandle;
791 }
792
793 /**
794 * Sets equals method's temporary file handle.
795 *
796 * @param equalsMethod file handle for to equals method
797 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530798 private void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530799 equalsImplTempFileHandle = equalsMethod;
800 }
801
802 /**
803 * Returns to string method's temporary file handle.
804 *
805 * @return temporary file handle
806 */
807 public File getToStringImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530808 return toStringImplTempFileHandle;
809 }
810
811 /**
812 * Sets to string method's temporary file handle.
813 *
814 * @param toStringMethod file handle for to string method
815 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530816 private void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530817 toStringImplTempFileHandle = toStringMethod;
818 }
819
820 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530821 * Returns temporary file handle for enum class file.
822 *
823 * @return temporary file handle for enum class file
824 */
825 public File getEnumClassTempFileHandle() {
826 return enumClassTempFileHandle;
827 }
828
829 /**
830 * Sets temporary file handle for enum class file.
831 *
832 * @param enumClassTempFileHandle temporary file handle for enum class file
833 */
834
835 private void setEnumClassTempFileHandle(File enumClassTempFileHandle) {
836 this.enumClassTempFileHandle = enumClassTempFileHandle;
837 }
838
839 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530840 * Returns of string method's temporary file handle.
841 *
842 * @return of string method's temporary file handle
843 */
844 public File getOfStringImplTempFileHandle() {
845 return ofStringImplTempFileHandle;
846 }
847
848 /**
849 * Set of string method's temporary file handle.
850 *
851 * @param ofStringImplTempFileHandle of string method's temporary file handle
852 */
853 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
854 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
855 }
856
857 /**
858 * Returns type class constructor method's temporary file handle.
859 *
860 * @return type class constructor method's temporary file handle
861 */
862 public File getConstructorForTypeTempFileHandle() {
863 return constructorForTypeTempFileHandle;
864 }
865
866 /**
867 * Sets type class constructor method's temporary file handle.
868 *
869 * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
870 */
871 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
872 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
873 }
874
875 /**
876 * Returns union's from string method's temporary file handle.
877 *
878 * @return union's from string method's temporary file handle
879 */
880 public File getUnionFromStringImplTempFileHandle() {
881 return unionFromStringImplTempFileHandle;
882 }
883
884 /**
885 * Sets union's from string method's temporary file handle.
886 *
887 * @param unionFromStringImplTempFileHandle union's from string method's temporary file handle
888 */
889 private void setUnionFromStringImplTempFileHandle(File unionFromStringImplTempFileHandle) {
890 this.unionFromStringImplTempFileHandle = unionFromStringImplTempFileHandle;
891 }
892
893 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530894 * Returns java attribute info.
895 *
896 * @return java attribute info
897 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530898 private JavaAttributeInfo getNewAttrInfo() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530899 return newAttrInfo;
900 }
901
902 /**
903 * Sets java attribute info.
904 *
905 * @param newAttrInfo java attribute info
906 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530907 private void setNewAttrInfo(JavaAttributeInfo newAttrInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530908
Bharat saraswal2f11f652016-03-25 18:19:46 +0530909 if (newAttrInfo != null) {
910 isAttributePresent = true;
911 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530912 this.newAttrInfo = newAttrInfo;
913 }
914
915 /**
916 * Returns current YANG node.
917 *
918 * @return current YANG node
919 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530920 private YangNode getCurYangNode() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530921 return curYangNode;
922 }
923
924 /**
925 * Sets current YANG node.
926 *
927 * @param curYangNode YANG node
928 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530929 private void setCurYangNode(YangNode curYangNode) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530930 this.curYangNode = curYangNode;
931 }
932
933 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530934 * Returns enum's value.
935 *
936 * @return enum's value
937 */
938 public int getEnumValue() {
939 return enumValue;
940 }
941
942 /**
943 * Sets enum's value.
944 *
945 * @param enumValue enum's value
946 */
947 public void setEnumValue(int enumValue) {
948 this.enumValue = enumValue;
949 }
950
951 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530952 * Returns list of classes to be extended by generated files.
953 *
954 * @return list of classes to be extended by generated files
955 */
956 private List<String> getExtendsList() {
957 return extendsList;
958 }
959
960 /**
961 * Sets class to be extended by generated file.
962 *
963 * @param extendsList list of classes to be extended
964 */
965
966 private void setExtendsList(List<String> extendsList) {
967 this.extendsList = extendsList;
968 }
969
970 /**
971 * Adds class to the extends list.
972 *
973 * @param extend class to be extended
974 */
975 public void addToExtendsList(String extend) {
976 getExtendsList().add(extend);
977 }
978
979 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530980 * Adds of string for type.
981 *
982 * @param attr attribute info
983 * @throws IOException when fails to append to temporary file
984 */
985 private void addOfStringMethod(JavaAttributeInfo attr) throws IOException {
986 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr, generatedJavaClassName)
987 + NEW_LINE);
988 }
989
990 /**
991 * Adds type constructor.
992 *
993 * @param attr attribute info
994 * @throws IOException when fails to append to temporary file
995 */
996 private void addTypeConstructor(JavaAttributeInfo attr) throws IOException {
997 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
998 generatedJavaClassName) + NEW_LINE);
999 }
1000
1001 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301002 * Adds attribute for class.
1003 *
1004 * @param attr attribute info
1005 * @throws IOException when fails to append to temporary file
1006 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301007 private void addAttribute(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301008 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
Vinod Kumar S38046502016-03-23 15:30:27 +05301009 }
1010
1011 /**
1012 * Adds getter for interface.
1013 *
1014 * @param attr attribute info
1015 * @throws IOException when fails to append to temporary file
1016 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301017 private void addGetterForInterface(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301018 appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301019 }
1020
1021 /**
1022 * Adds getter method's impl for class.
1023 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301024 * @param attr attribute info
Bharat saraswale2d51d62016-03-23 19:40:35 +05301025 * @param genFiletype generated file type
Vinod Kumar S38046502016-03-23 15:30:27 +05301026 * @throws IOException when fails to append to temporary file
1027 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301028 private void addGetterImpl(JavaAttributeInfo attr, int genFiletype) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301029
Bharat saraswale2d51d62016-03-23 19:40:35 +05301030 if ((genFiletype & BUILDER_CLASS_MASK) != 0) {
1031 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr) + NEW_LINE);
1032 } else {
1033 appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
1034 + getGetterForClass(attr) + NEW_LINE);
1035 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301036 }
1037
1038 /**
1039 * Adds setter for interface.
1040 *
1041 * @param attr attribute info
1042 * @throws IOException when fails to append to temporary file
1043 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301044 private void addSetterForInterface(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301045 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +05301046 getSetterString(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301047 }
1048
1049 /**
1050 * Adds setter's implementation for class.
1051 *
1052 * @param attr attribute info
1053 * @throws IOException when fails to append to temporary file
1054 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301055 private void addSetterImpl(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301056 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +05301057 getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301058 }
1059
1060 /**
1061 * Adds build method for interface.
1062 *
1063 * @return build method for interface
1064 * @throws IOException when fails to append to temporary file
1065 */
1066 public String addBuildMethodForInterface() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301067 return parseBuilderInterfaceBuildMethodString(generatedJavaClassName);
1068 }
1069
1070 /**
1071 * Adds build method's implementation for class.
1072 *
1073 * @return build method implementation for class
1074 * @throws IOException when fails to append to temporary file
1075 */
1076 public String addBuildMethodImpl() throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301077 return getBuildString(generatedJavaClassName) + NEW_LINE;
Vinod Kumar S38046502016-03-23 15:30:27 +05301078 }
1079
1080 /**
1081 * Adds constructor for class.
1082 *
1083 * @param attr attribute info
1084 * @throws IOException when fails to append to temporary file
1085 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301086 private void addConstructor(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301087 appendToFile(getConstructorImplTempFileHandle(), getConstructor(generatedJavaClassName, attr));
1088 }
1089
1090 /**
1091 * Adds default constructor for class.
1092 *
Bharat saraswale2d51d62016-03-23 19:40:35 +05301093 * @param modifier modifier for constructor.
1094 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +05301095 * @return default constructor for class
1096 * @throws IOException when fails to append to file
1097 */
Bharat saraswale2d51d62016-03-23 19:40:35 +05301098 public String addDefaultConstructor(String modifier, String toAppend) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301099 return NEW_LINE + getDefaultConstructorString(generatedJavaClassName + toAppend, modifier);
1100 }
1101
1102 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301103 * Adds default constructor for class.
1104 *
1105 * @return default constructor for class
1106 * @throws IOException when fails to append to file
1107 */
1108 public String addOfMethod() throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301109 return getJavaDoc(OF_METHOD, generatedJavaClassName, false)
Bharat saraswale2d51d62016-03-23 19:40:35 +05301110 + getOfMethod(generatedJavaClassName, newAttrInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +05301111 }
1112
1113 /**
1114 * Adds hash code method for class.
1115 *
1116 * @param attr attribute info
1117 * @throws IOException when fails to append to temporary file
1118 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301119 private void addHashCodeMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301120 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301121 }
1122
1123 /**
1124 * Adds equals method for class.
1125 *
1126 * @param attr attribute info
1127 * @throws IOException when fails to append to temporary file
1128 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301129 private void addEqualsMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301130 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301131 }
1132
1133 /**
1134 * Adds ToString method for class.
1135 *
1136 * @param attr attribute info
1137 * @throws IOException when fails to append to temporary file
1138 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301139 private void addToStringMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301140 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +05301141 }
1142
1143 /**
Bharat saraswald72411a2016-04-19 01:00:16 +05301144 * Adds enum class attributes to temporary file.
1145 *
1146 * @param curEnum current YANG enum
1147 * @throws IOException when fails to do IO operations.
1148 */
1149 private void addAttributesForEnumClass(JavaAttributeInfo curEnumInfo) throws IOException {
1150 appendToFile(getEnumClassTempFileHandle(),
1151 generateEnumAttributeString(curEnumInfo.getAttributeName(), getEnumValue()));
1152 }
1153
1154 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301155 * Add from string method for union class.
1156 *
1157 * @param javaAttributeInfo type attribute info
1158 * @param fromStringAttributeInfo from string attribute info
1159 * @throws IOException when fails to append to temporary file
1160 */
1161 private void addUnionFromStringMethod(JavaAttributeInfo javaAttributeInfo,
1162 JavaAttributeInfo fromStringAttributeInfo) throws IOException {
1163 appendToFile(getUnionFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1164 fromStringAttributeInfo) + NEW_LINE);
1165 }
1166
1167 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301168 * Returns a temporary file handle for the specific file type.
1169 *
1170 * @param fileName file name
1171 * @return temporary file handle
1172 * @throws IOException when fails to create new file handle
1173 */
1174 private File getTemporaryFileHandle(String fileName) throws IOException {
1175
1176 String path = getTempDirPath();
1177 File dir = new File(path);
1178 if (!dir.exists()) {
1179 dir.mkdirs();
1180 }
1181
1182 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1183 if (!file.exists()) {
1184 file.createNewFile();
Vinod Kumar S38046502016-03-23 15:30:27 +05301185 }
1186 return file;
1187 }
1188
1189 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301190 * Returns a temporary file handle for the specific file type.
1191 *
1192 * @param fileName file name
1193 * @return temporary file handle
1194 * @throws IOException when fails to create new file handle
1195 */
1196 private File getJavaFileHandle(String fileName) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301197 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1198 }
1199
1200 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301201 * Returns data from the temporary files.
1202 *
1203 * @param file temporary file handle
1204 * @return stored data from temporary files
1205 * @throws IOException when failed to get data from the given file
1206 */
1207 public String getTemporaryDataFromFileHandle(File file) throws IOException {
1208
1209 String path = getTempDirPath();
1210 if (new File(path + file.getName()).exists()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301211 return readAppendFile(path + file.getName(), EMPTY_STRING);
Vinod Kumar S38046502016-03-23 15:30:27 +05301212 } else {
1213 throw new IOException("Unable to get data from the given "
Bharat saraswale2d51d62016-03-23 19:40:35 +05301214 + file.getName() + " file for " + generatedJavaClassName + PERIOD);
Vinod Kumar S38046502016-03-23 15:30:27 +05301215 }
1216 }
1217
1218 /**
1219 * Returns temporary directory path.
1220 *
1221 * @return directory path
1222 */
1223 private String getTempDirPath() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301224 return getPackageDirPathFromJavaJPackage(absoluteDirPath) + SLASH + generatedJavaClassName
1225 + TEMP_FOLDER_NAME_SUFIX + SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +05301226 }
1227
1228 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301229 * Parses attribute to get the attribute string.
Vinod Kumar S38046502016-03-23 15:30:27 +05301230 *
1231 * @param attr attribute info
1232 * @return attribute string
1233 */
1234 private String parseAttribute(JavaAttributeInfo attr) {
1235
1236 /*
1237 * TODO: check if this utility needs to be called or move to the caller
1238 */
janani bde4ffab2016-04-15 16:18:30 +05301239 String attributeName = getCamelCase(getSmallCase(attr.getAttributeName()), null);
Vinod Kumar S38046502016-03-23 15:30:27 +05301240 if (attr.isQualifiedName()) {
1241 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
1242 attributeName, attr.isListAttr());
1243 } else {
1244 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1245 attr.isListAttr());
1246 }
1247 }
1248
1249 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301250 * Appends content to temporary file.
Vinod Kumar S38046502016-03-23 15:30:27 +05301251 *
1252 * @param file temporary file
1253 * @param data data to be appended
1254 * @throws IOException when fails to append to file
1255 */
1256 private void appendToFile(File file, String data) throws IOException {
1257
1258 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301259 insertDataIntoJavaFile(file, data);
Vinod Kumar S38046502016-03-23 15:30:27 +05301260 } catch (IOException ex) {
1261 throw new IOException("failed to write in temp file.");
1262 }
1263 }
1264
1265 /**
1266 * Adds current node info as and attribute to the parent generated file.
1267 *
1268 * @param curNode current node which needs to be added as an attribute in
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301269 * the parent generated code
1270 * @param isList is list construct
Vinod Kumar S38046502016-03-23 15:30:27 +05301271 * @throws IOException IO operation exception
1272 */
1273 public void addCurNodeInfoInParentTempFile(YangNode curNode,
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301274 boolean isList) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301275
1276 YangNode parent = getParentNodeInGenCode(curNode);
1277 if (!(parent instanceof JavaCodeGenerator)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301278 throw new TranslatorException("missing parent node to contain current node info in generated file");
Vinod Kumar S38046502016-03-23 15:30:27 +05301279 }
1280 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1281 parent, isList);
1282
1283 if (!(parent instanceof HasTempJavaCodeFragmentFiles)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301284 throw new TranslatorException("missing parent temp file handle");
Vinod Kumar S38046502016-03-23 15:30:27 +05301285 }
1286 ((HasTempJavaCodeFragmentFiles) parent)
1287 .getTempJavaCodeFragmentFiles()
1288 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1289 }
1290
1291 /**
1292 * Adds leaf attributes in generated files.
1293 *
1294 * @param listOfLeaves list of YANG leaf
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301295 * @param curNode current data model node
Vinod Kumar S38046502016-03-23 15:30:27 +05301296 * @throws IOException IO operation fail
1297 */
1298 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301299 YangNode curNode) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301300
1301 if (listOfLeaves != null) {
1302 for (YangLeaf leaf : listOfLeaves) {
1303 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(curNode,
1304 leaf.getDataType(),
1305 leaf.getLeafName(), false);
1306 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1307 }
1308 }
1309 }
1310
1311 /**
1312 * Adds leaf list's attributes in generated files.
1313 *
1314 * @param listOfLeafList list of YANG leaves
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301315 * @param curNode cached file handle
Vinod Kumar S38046502016-03-23 15:30:27 +05301316 * @throws IOException IO operation fail
1317 */
1318 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList,
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301319 YangNode curNode) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +05301320
1321 if (listOfLeafList != null) {
1322
1323 /*
1324 * Check if the attribute is of type list, then the java.lang.list
1325 * needs to be imported.
1326 */
1327 if (listOfLeafList.size() != 0) {
1328 if (!(curNode instanceof HasJavaImportData)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301329 throw new TranslatorException("missing import info in current data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +05301330
1331 }
1332 ((HasJavaImportData) curNode).getJavaImportData()
1333 .setIfListImported(true);
1334
1335 }
1336
1337 for (YangLeafList leafList : listOfLeafList) {
1338 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(
1339 curNode, leafList.getDataType(), leafList.getLeafName(),
1340 true);
1341 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1342 }
1343 }
1344 }
1345
1346 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301347 * Adds all the leaves in the current data model node as part of the
Vinod Kumar S38046502016-03-23 15:30:27 +05301348 * generated temporary file.
1349 *
1350 * @param curNode java file info of the generated file
1351 * @throws IOException IO operation fail
1352 */
1353 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode) throws IOException {
1354
1355 if (curNode instanceof YangLeavesHolder) {
1356 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
1357 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), curNode);
1358 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), curNode);
1359 }
1360 }
1361
1362 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301363 * Add all the type in the current data model node as part of the
1364 * generated temporary file.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301365 *
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301366 * @param hasType YANG java data model node which has type info, eg union / typedef
Bharat saraswale2d51d62016-03-23 19:40:35 +05301367 * @throws IOException IO operation fail
1368 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301369 public void addTypeInfoToTempFiles(HasType hasType) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301370
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301371 List<YangType<?>> typeList = hasType.getTypeList();
1372 if (typeList != null) {
1373 for (YangType<?> yangType : typeList) {
1374 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfType((YangNode) hasType,
1375 yangType, getTypeName(yangType.getDataTypeName()), false);
1376 addJavaSnippetInfoToApplicableTempFiles((YangNode) hasType, javaAttributeInfo);
1377 }
1378 }
1379 }
1380
1381 private String getTypeName(String dataTypeName) {
1382 return dataTypeName;
1383 //TODO: implement the method.
1384 }
1385
1386 /**
Bharat saraswald72411a2016-04-19 01:00:16 +05301387 * Adds enum attributes to temporary files.
1388 *
1389 * @param curNode current YANG node
1390 * @throws IOException when fails to do IO operations
1391 */
1392 public void addEnumAttributeToTempFiles(YangNode curNode) throws IOException {
1393
1394 if (curNode instanceof YangEnumeration) {
1395 Set<YangEnum> enumSet = ((YangEnumeration) curNode).getEnumSet();
1396 for (YangEnum curEnum : enumSet) {
1397 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfEnumAttribute(curNode, curEnum.getNamedValue());
1398 setEnumValue(curEnum.getValue());
1399 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1400 }
1401 } else {
1402 throw new TranslatorException("curnode should be of type enum.");
1403 }
1404 }
1405
1406 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301407 * Adds the new attribute info to the target generated temporary files for union class.
1408 *
1409 * @param hasType the node for which the type is being added as an attribute
1410 * @param javaAttributeInfo the attribute info that needs to be added to temporary
1411 * files
1412 * @throws IOException IO operation fail
1413 */
1414 private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo)
1415 throws IOException {
1416
1417 JavaAttributeInfo fromStringAttributeInfo = getFromStringAttributeInfo(hasType, javaAttributeInfo);
1418
1419 if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
1420 addUnionFromStringMethod(javaAttributeInfo, fromStringAttributeInfo);
1421 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301422 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1423 }
1424
1425 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301426 * Adds the new attribute info to the target generated temporary files.
Vinod Kumar S38046502016-03-23 15:30:27 +05301427 *
1428 * @param newAttrInfo the attribute info that needs to be added to temporary
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301429 * files
Vinod Kumar S38046502016-03-23 15:30:27 +05301430 * @throws IOException IO operation fail
1431 */
1432 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1433 throws IOException {
1434
Bharat saraswale2d51d62016-03-23 19:40:35 +05301435 setNewAttrInfo(newAttrInfo);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301436 if (isAttributePresent) {
1437 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
1438 addAttribute(newAttrInfo);
1439 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301440
Bharat saraswal2f11f652016-03-25 18:19:46 +05301441 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
1442 addGetterForInterface(newAttrInfo);
1443 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301444
Bharat saraswal2f11f652016-03-25 18:19:46 +05301445 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
1446 addSetterForInterface(newAttrInfo);
1447 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301448
Bharat saraswal2f11f652016-03-25 18:19:46 +05301449 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
1450 addGetterImpl(newAttrInfo, generatedJavaFiles);
1451 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301452
Bharat saraswal2f11f652016-03-25 18:19:46 +05301453 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
1454 addSetterImpl(newAttrInfo);
1455 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301456
Bharat saraswal2f11f652016-03-25 18:19:46 +05301457 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
1458 addConstructor(newAttrInfo);
1459 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301460
Bharat saraswal2f11f652016-03-25 18:19:46 +05301461 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
1462 addHashCodeMethod(newAttrInfo);
1463 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301464
Bharat saraswal2f11f652016-03-25 18:19:46 +05301465 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
1466 addEqualsMethod(newAttrInfo);
1467 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301468
Bharat saraswal2f11f652016-03-25 18:19:46 +05301469 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
1470 addToStringMethod(newAttrInfo);
1471 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301472
Bharat saraswald72411a2016-04-19 01:00:16 +05301473 if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
1474 addAttributesForEnumClass(newAttrInfo);
1475 }
1476
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301477 if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
1478 addOfStringMethod(newAttrInfo);
1479 }
1480
1481 if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
1482 addTypeConstructor(newAttrInfo);
1483 }
1484
Vinod Kumar S38046502016-03-23 15:30:27 +05301485 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301486 }
1487
1488 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301489 * Returns java file info.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301490 *
1491 * @return java file info
1492 */
1493 private JavaFileInfo getJavaFileInfo() {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301494 return ((HasJavaFileInfo) getCurYangNode()).getJavaFileInfo();
1495 }
1496
1497 /**
1498 * Returns java class name.
1499 *
1500 * @param suffix for the class name based on the file type
1501 * @return java class name
1502 */
1503 private String getJavaClassName(String suffix) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301504 return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
Bharat saraswale2d51d62016-03-23 19:40:35 +05301505 }
1506
1507 /**
1508 * Returns the directory path.
1509 *
1510 * @return directory path
1511 */
1512 private String getDirPath() {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301513 return getJavaFileInfo().getPackageFilePath();
1514 }
1515
1516 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301517 * Constructs java code exit.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301518 *
1519 * @param fileType generated file type
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301520 * @param curNode current YANG node
Bharat saraswale2d51d62016-03-23 19:40:35 +05301521 * @throws IOException when fails to generate java files
1522 */
1523 public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
1524
1525 setCurYangNode(curNode);
1526 List<String> imports = new ArrayList<>();
Bharat saraswal2f11f652016-03-25 18:19:46 +05301527 if (curNode instanceof HasJavaImportData && isAttributePresent) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301528 imports = ((HasJavaImportData) curNode).getJavaImportData().getImports(getNewAttrInfo());
1529 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301530
Bharat saraswale2d51d62016-03-23 19:40:35 +05301531 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301532 * Prepares java file generator for extends list.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301533 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301534 prepareJavaFileGeneratorForExtendsList(getExtendsList());
Bharat saraswald72411a2016-04-19 01:00:16 +05301535 if (curNode.getNodeType().equals(MODULE_NODE)) {
1536 createPackage(absoluteDirPath, getJavaFileInfo().getJavaName());
1537 } else {
1538 createPackage(absoluteDirPath, ((HasJavaFileInfo) curNode.getParent()).getJavaFileInfo().getJavaName()
1539 + PACKAGE_INFO_JAVADOC_OF_CHILD);
1540 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301541 /**
1542 * Generate java code.
1543 */
1544 if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0) {
1545
1546 /**
1547 * Adds import for HasAugmentation class.
1548 */
1549 if (isHasAugmentationExtended(getExtendsList())) {
1550 addHasAugmentationImport(curNode, imports, true);
1551 }
1552
1553 if (isAugmentedInfoExtended(getExtendsList())) {
1554 addAugmentedInfoImport(curNode, imports, true);
1555 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301556
1557 /**
1558 * Create interface file.
1559 */
1560 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301561 setInterfaceJavaFileHandle(
1562 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301563 /**
1564 * Create builder interface file.
1565 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301566 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1567
1568 setBuilderInterfaceJavaFileHandle(
1569 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1570 setBuilderInterfaceJavaFileHandle(
1571 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, isAttributePresent));
1572 /**
1573 * Append builder interface file to interface file and close it.
1574 */
1575 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1576 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301577 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswale2d51d62016-03-23 19:40:35 +05301578
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301579 if (isHasAugmentationExtended(getExtendsList())) {
1580 addHasAugmentationImport(curNode, imports, false);
1581 }
1582 if (isAugmentedInfoExtended(getExtendsList())) {
1583 addAugmentedInfoImport(curNode, imports, false);
1584 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301585 }
1586
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301587 if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301588
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301589 if (isAttributePresent) {
1590 addImportsToStringAndHasCodeMethods(curNode, imports);
1591 }
1592 if (isHasAugmentationExtended(getExtendsList())) {
1593 addAugmentedInfoImport(curNode, imports, true);
1594 addArrayListImport(curNode, imports, true);
1595 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301596
1597 /**
1598 * Create builder class file.
1599 */
1600 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301601 setBuilderClassJavaFileHandle(
1602 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301603 /**
1604 * Create impl class file.
1605 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301606 if ((fileType & IMPL_CLASS_MASK) != 0) {
1607 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1608 setImplClassJavaFileHandle(
1609 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent));
1610 /**
1611 * Append impl class to builder class and close it.
1612 */
1613 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1614 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301615 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswale2d51d62016-03-23 19:40:35 +05301616 }
1617
1618 /**
1619 * Creates type def class file.
1620 */
1621 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301622 addImportsToStringAndHasCodeMethods(curNode, imports);
Bharat saraswale2d51d62016-03-23 19:40:35 +05301623 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
1624 setTypedefClassJavaFileHandle(generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports));
1625 }
1626
1627 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301628 * Creates type class file.
1629 */
1630 if ((fileType & GENERATE_UNION_CLASS) != 0) {
1631 addImportsToStringAndHasCodeMethods(curNode, imports);
1632 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
1633 setTypeClassJavaFileHandle(generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports));
1634 }
1635
1636 /**
Bharat saraswald72411a2016-04-19 01:00:16 +05301637 * Creates type enum class file.
1638 */
1639 if ((fileType & GENERATE_ENUM_CLASS) != 0) {
1640 setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
1641 setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
1642 }
1643
1644 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301645 * Close all the file handles.
1646 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301647 close(false);
Bharat saraswale2d51d62016-03-23 19:40:35 +05301648 }
1649
1650 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301651 * Removes all temporary file handles.
1652 *
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301653 * @param isErrorOccurred when translator fails to generate java files we need to close
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301654 * all open file handles include temporary files and java files.
Vinod Kumar S38046502016-03-23 15:30:27 +05301655 * @throws IOException when failed to delete the temporary files
1656 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301657 public void close(boolean isErrorOccurred) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301658
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301659 boolean isError = isErrorOccurred;
1660 /**
1661 * Close all java file handles and when error occurs delete the files.
1662 */
Bharat saraswal2f11f652016-03-25 18:19:46 +05301663 if ((generatedJavaFiles & INTERFACE_MASK) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301664 closeFile(getInterfaceJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301665 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301666 if ((generatedJavaFiles & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301667 closeFile(getBuilderClassJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301668 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301669 if ((generatedJavaFiles & BUILDER_INTERFACE_MASK) != 0) {
1670 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1671 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301672 if ((generatedJavaFiles & IMPL_CLASS_MASK) != 0) {
1673 closeFile(getImplClassJavaFileHandle(), true);
1674 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301675 if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301676 closeFile(getTypedefClassJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301677 }
Bharat saraswald72411a2016-04-19 01:00:16 +05301678 if ((generatedJavaFiles & GENERATE_ENUM_CLASS) != 0) {
1679 closeFile(getEnumClassJavaFileHandle(), isError);
1680 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301681 if ((generatedJavaFiles & GENERATE_UNION_CLASS) != 0) {
1682 closeFile(getTypeClassJavaFileHandle(), isError);
1683 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301684
Bharat saraswal2f11f652016-03-25 18:19:46 +05301685 /**
1686 * Close all temporary file handles and delete the files.
1687 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301688 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
1689 closeFile(getGetterInterfaceTempFileHandle(), true);
1690 }
1691 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
1692 closeFile(getGetterImplTempFileHandle(), true);
1693 }
1694 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
1695 closeFile(getSetterInterfaceTempFileHandle(), true);
1696 }
1697 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
1698 closeFile(getSetterImplTempFileHandle(), true);
1699 }
1700 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
1701 closeFile(getConstructorImplTempFileHandle(), true);
1702 }
1703 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
1704 closeFile(getAttributesTempFileHandle(), true);
1705 }
1706 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
1707 closeFile(getHashCodeImplTempFileHandle(), true);
1708 }
1709 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
1710 closeFile(getToStringImplTempFileHandle(), true);
1711 }
1712 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
1713 closeFile(getEqualsImplTempFileHandle(), true);
1714 }
Bharat saraswald72411a2016-04-19 01:00:16 +05301715 if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
1716 closeFile(getEnumClassTempFileHandle(), true);
1717 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301718 if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
1719 closeFile(getConstructorForTypeTempFileHandle(), true);
1720 }
1721 if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
1722 closeFile(getOfStringImplTempFileHandle(), true);
1723 }
1724 if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
1725 closeFile(getUnionFromStringImplTempFileHandle(), true);
1726 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301727 clean(getTempDirPath());
1728 generatedTempFiles = 0;
Vinod Kumar S38046502016-03-23 15:30:27 +05301729 }
1730
Vinod Kumar S38046502016-03-23 15:30:27 +05301731}