blob: 06c08d7cfdbedfd96e8967cb7a64ef0b8b2043fc [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;
23
24import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswale2d51d62016-03-23 19:40:35 +053028import org.onosproject.yangutils.datamodel.YangTypeDef;
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;
Vinod Kumar S38046502016-03-23 15:30:27 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
37import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
38import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
39import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
44import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
45import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf;
Bharat saraswale2d51d62016-03-23 19:40:35 +053046import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfTypeDef;
Vinod Kumar S38046502016-03-23 15:30:27 +053047import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
48import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053049import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswale2d51d62016-03-23 19:40:35 +053050import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
51import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
52import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
53import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
54import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
55import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053056import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
57import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
58import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Vinod Kumar S38046502016-03-23 15:30:27 +053059import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053060import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
Vinod Kumar S38046502016-03-23 15:30:27 +053061import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
62import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
63import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
64import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
65import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
66import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
67import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Bharat saraswale2d51d62016-03-23 19:40:35 +053068import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar S38046502016-03-23 15:30:27 +053069import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
70import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
Bharat saraswale2d51d62016-03-23 19:40:35 +053071import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
Vinod Kumar S38046502016-03-23 15:30:27 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
73import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Bharat saraswale2d51d62016-03-23 19:40:35 +053074import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeDefConstructor;
Vinod Kumar S38046502016-03-23 15:30:27 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053076import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
77import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
78import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport;
79import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addImportsToStringAndHasCodeMethods;
80import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
81import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
82import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
83import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.prepareJavaFileGeneratorForExtendsList;
Bharat saraswale2d51d62016-03-23 19:40:35 +053084import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
85import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
86import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
87import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
88import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
89import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
90import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
91import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
92import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053093import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswale2d51d62016-03-23 19:40:35 +053094import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
95import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053096import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Bharat saraswale2d51d62016-03-23 19:40:35 +053097import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_CONSTRUCTOR;
98import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
99import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
100import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
101import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +0530102
103/**
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * Represents implementation of java code fragments temporary implementations.
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 */
106public class TempJavaCodeFragmentFiles {
107
108 /**
109 * The variable which guides the types of temporary files generated using
110 * the temporary generated file types mask.
111 */
112 private int generatedTempFiles;
113
114 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530115 * The variable which guides the types of files generated using
116 * the generated file types mask.
117 */
118 private int generatedJavaFiles;
119
120 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 * Absolute path where the target java file needs to be generated.
122 */
123 private String absoluteDirPath;
124
125 /**
126 * Name of java file that needs to be generated.
127 */
128 private String generatedJavaClassName;
129
130 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530131 * Contains all the class name which will be extended by generated files.
132 */
133 private List<String> extendsList = new ArrayList<>();
134
135 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530136 * File type extension for java classes.
137 */
138 private static final String JAVA_FILE_EXTENSION = ".java";
139
140 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530141 * File type extension for temporary classes.
142 */
143 private static final String TEMP_FILE_EXTENSION = ".tmp";
144
145 /**
146 * Folder suffix for temporary files folder.
147 */
148 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
149
150 /**
151 * File name for getter method.
152 */
153 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
154
155 /**
156 * File name for getter method implementation.
157 */
158 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
159
160 /**
161 * File name for setter method.
162 */
163 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
164
165 /**
166 * File name for setter method implementation.
167 */
168 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
169
170 /**
171 * File name for constructor.
172 */
173 private static final String CONSTRUCTOR_FILE_NAME = "Constructor";
174
175 /**
176 * File name for attributes.
177 */
178 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
179
180 /**
181 * File name for to string method.
182 */
183 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
184
185 /**
186 * File name for hash code method.
187 */
188 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
189
190 /**
191 * File name for equals method.
192 */
193 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
194
195 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530196 * File name for interface java file name suffix.
197 */
198 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
199
200 /**
201 * File name for builder interface file name suffix.
202 */
203 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
204
205 /**
206 * File name for builder class file name suffix.
207 */
208 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
209
210 /**
211 * File name for impl class file name suffix.
212 */
213 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
214
215 /**
216 * File name for typedef class file name suffix.
217 */
218 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
219
220 /**
221 * Java file handle for interface file.
222 */
223 private File interfaceJavaFileHandle;
224
225 /**
226 * Java file handle for builder interface file.
227 */
228 private File builderInterfaceJavaFileHandle;
229
230 /**
231 * Java file handle for builder class file.
232 */
233 private File builderClassJavaFileHandle;
234
235 /**
236 * Java file handle for impl class file.
237 */
238 private File implClassJavaFileHandle;
239
240 /**
241 * Java file handle for typedef class file.
242 */
243 private File typedefClassJavaFileHandle;
244
245 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530246 * Temporary file handle for attribute.
247 */
248 private File attributesTempFileHandle;
249
250 /**
251 * Temporary file handle for getter of interface.
252 */
253 private File getterInterfaceTempFileHandle;
254
255 /**
256 * Temporary file handle for getter of class.
257 */
258 private File getterImplTempFileHandle;
259
260 /**
261 * Temporary file handle for setter of interface.
262 */
263 private File setterInterfaceTempFileHandle;
264
265 /**
266 * Temporary file handle for setter of class.
267 */
268 private File setterImplTempFileHandle;
269
270 /**
271 * Temporary file handle for constructor of class.
272 */
273 private File constructorImplTempFileHandle;
274
275 /**
276 * Temporary file handle for hash code method of class.
277 */
278 private File hashCodeImplTempFileHandle;
279
280 /**
281 * Temporary file handle for equals method of class.
282 */
283 private File equalsImplTempFileHandle;
284
285 /**
286 * Temporary file handle for to string method of class.
287 */
288 private File toStringImplTempFileHandle;
289
290 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530291 * Java attribute info.
292 */
293 private JavaAttributeInfo newAttrInfo;
294
295 /**
296 * Current YANG node.
297 */
298 private YangNode curYangNode;
299
300 /**
Bharat saraswal2f11f652016-03-25 18:19:46 +0530301 * Is attribute added.
302 */
303 private boolean isAttributePresent = false;
304
305 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530306 * Creates an instance of temporary java code fragment.
Vinod Kumar S38046502016-03-23 15:30:27 +0530307 *
308 * @param genFileType file generation type
309 * @param genDir file generation directory
310 * @param className class name
311 * @throws IOException when fails to create new file handle
312 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530313 public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className)
314 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530315
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530316 setExtendsList(new ArrayList<>());
Vinod Kumar S38046502016-03-23 15:30:27 +0530317 generatedTempFiles = 0;
318 absoluteDirPath = genDir;
319 generatedJavaClassName = className;
Bharat saraswale2d51d62016-03-23 19:40:35 +0530320 generatedJavaFiles = genFileType;
Vinod Kumar S38046502016-03-23 15:30:27 +0530321 /**
322 * Initialize getter when generation file type matches to interface
323 * mask.
324 */
325 if ((genFileType & INTERFACE_MASK) != 0) {
326 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
327 }
328
329 /**
330 * Initialize getter and setter when generation file type matches to
331 * builder interface mask.
332 */
333 if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
334 generatedTempFiles |= GETTER_FOR_INTERFACE_MASK;
335 generatedTempFiles |= SETTER_FOR_INTERFACE_MASK;
336 }
337
338 /**
339 * Initialize getterImpl, setterImpl and attributes when generation file
340 * type matches to builder class mask.
341 */
342 if ((genFileType & BUILDER_CLASS_MASK) != 0) {
343 generatedTempFiles |= ATTRIBUTES_MASK;
344 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
345 generatedTempFiles |= SETTER_FOR_CLASS_MASK;
346 }
347
348 /**
349 * Initialize getterImpl, attributes, constructor, hash code, equals and
350 * to strings when generation file type matches to impl class mask.
351 */
352 if ((genFileType & IMPL_CLASS_MASK) != 0) {
353 generatedTempFiles |= ATTRIBUTES_MASK;
354 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
355 generatedTempFiles |= CONSTRUCTOR_IMPL_MASK;
356 generatedTempFiles |= HASH_CODE_IMPL_MASK;
357 generatedTempFiles |= EQUALS_IMPL_MASK;
358 generatedTempFiles |= TO_STRING_IMPL_MASK;
359 }
360
Bharat saraswale2d51d62016-03-23 19:40:35 +0530361 /**
362 * Initialize getterImpl, attributes, hash code, equals and
363 * to strings when generation file type matches to typeDef class mask.
364 */
365 if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
366 generatedTempFiles |= ATTRIBUTES_MASK;
367 generatedTempFiles |= GETTER_FOR_CLASS_MASK;
368 generatedTempFiles |= HASH_CODE_IMPL_MASK;
369 generatedTempFiles |= EQUALS_IMPL_MASK;
370 generatedTempFiles |= TO_STRING_IMPL_MASK;
371 }
372
Vinod Kumar S38046502016-03-23 15:30:27 +0530373 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
374 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
375 }
376
377 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
378 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
379 }
380
381 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
382 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
383 }
384
385 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
386 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
387 }
388
389 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
390 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
391 }
392
393 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
394 setConstructorImplTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FILE_NAME));
395 }
396
397 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
398 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
399 }
400
401 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
402 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
403 }
404 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
405 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
406 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530407 }
408
409 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530410 * Returns java file handle for interface file.
411 *
412 * @return java file handle for interface file
413 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530414 private File getInterfaceJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530415 return interfaceJavaFileHandle;
416 }
417
418 /**
419 * Sets the java file handle for interface file.
420 *
421 * @param interfaceJavaFileHandle java file handle
422 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530423 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530424 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
425 }
426
427 /**
428 * Returns java file handle for builder interface file.
429 *
430 * @return java file handle for builder interface file
431 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530432 private File getBuilderInterfaceJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530433 return builderInterfaceJavaFileHandle;
434 }
435
436 /**
437 * Sets the java file handle for builder interface file.
438 *
439 * @param builderInterfaceJavaFileHandle java file handle
440 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530441 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530442 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
443 }
444
445 /**
446 * Returns java file handle for builder class file.
447 *
448 * @return java file handle for builder class file
449 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530450 private File getBuilderClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530451 return builderClassJavaFileHandle;
452 }
453
454 /**
455 * Sets the java file handle for builder class file.
456 *
457 * @param builderClassJavaFileHandle java file handle
458 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530459 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530460 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
461 }
462
463 /**
464 * Returns java file handle for impl class file.
465 *
466 * @return java file handle for impl class file
467 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530468 private File getImplClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530469 return implClassJavaFileHandle;
470 }
471
472 /**
473 * Sets the java file handle for impl class file.
474 *
475 * @param implClassJavaFileHandle java file handle
476 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530477 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530478 this.implClassJavaFileHandle = implClassJavaFileHandle;
479 }
480
481 /**
482 * Returns java file handle for typedef class file.
483 *
484 * @return java file handle for typedef class file
485 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530486 private File getTypedefClassJavaFileHandle() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530487 return typedefClassJavaFileHandle;
488 }
489
490 /**
491 * Sets the java file handle for typedef class file.
492 *
493 * @param typedefClassJavaFileHandle java file handle
494 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530495 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530496 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
497 }
498
499 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530500 * Returns attribute's temporary file handle.
501 *
502 * @return temporary file handle
503 */
504 public File getAttributesTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530505 return attributesTempFileHandle;
506 }
507
508 /**
509 * Sets attribute's temporary file handle.
510 *
511 * @param attributeForClass file handle for attribute
512 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530513 private void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530514 attributesTempFileHandle = attributeForClass;
515 }
516
517 /**
518 * Returns getter methods's temporary file handle.
519 *
520 * @return temporary file handle
521 */
522 public File getGetterInterfaceTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530523 return getterInterfaceTempFileHandle;
524 }
525
526 /**
527 * Sets to getter method's temporary file handle.
528 *
529 * @param getterForInterface file handle for to getter method
530 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530531 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530532 getterInterfaceTempFileHandle = getterForInterface;
533 }
534
535 /**
536 * Returns getter method's impl's temporary file handle.
537 *
538 * @return temporary file handle
539 */
540 public File getGetterImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530541 return getterImplTempFileHandle;
542 }
543
544 /**
545 * Sets to getter method's impl's temporary file handle.
546 *
547 * @param getterImpl file handle for to getter method's impl
548 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530549 private void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530550 getterImplTempFileHandle = getterImpl;
551 }
552
553 /**
554 * Returns setter method's temporary file handle.
555 *
556 * @return temporary file handle
557 */
558 public File getSetterInterfaceTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530559 return setterInterfaceTempFileHandle;
560 }
561
562 /**
563 * Sets to setter method's temporary file handle.
564 *
565 * @param setterForInterface file handle for to setter method
566 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530567 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530568 setterInterfaceTempFileHandle = setterForInterface;
569 }
570
571 /**
572 * Returns setter method's impl's temporary file handle.
573 *
574 * @return temporary file handle
575 */
576 public File getSetterImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530577 return setterImplTempFileHandle;
578 }
579
580 /**
581 * Sets to setter method's impl's temporary file handle.
582 *
583 * @param setterImpl file handle for to setter method's implementation class
584 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530585 private void setSetterImplTempFileHandle(File setterImpl) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530586 setterImplTempFileHandle = setterImpl;
587 }
588
589 /**
590 * Returns constructor's temporary file handle.
591 *
592 * @return temporary file handle
593 */
594 public File getConstructorImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530595 return constructorImplTempFileHandle;
596 }
597
598 /**
599 * Sets to constructor's temporary file handle.
600 *
601 * @param constructor file handle for to constructor
602 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530603 private void setConstructorImplTempFileHandle(File constructor) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530604 constructorImplTempFileHandle = constructor;
605 }
606
607 /**
608 * Returns hash code method's temporary file handle.
609 *
610 * @return temporary file handle
611 */
612 public File getHashCodeImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530613 return hashCodeImplTempFileHandle;
614 }
615
616 /**
617 * Sets hash code method's temporary file handle.
618 *
619 * @param hashCodeMethod file handle for hash code method
620 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530621 private void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530622 hashCodeImplTempFileHandle = hashCodeMethod;
623 }
624
625 /**
626 * Returns equals mehtod's temporary file handle.
627 *
628 * @return temporary file handle
629 */
630 public File getEqualsImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530631 return equalsImplTempFileHandle;
632 }
633
634 /**
635 * Sets equals method's temporary file handle.
636 *
637 * @param equalsMethod file handle for to equals method
638 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530639 private void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530640 equalsImplTempFileHandle = equalsMethod;
641 }
642
643 /**
644 * Returns to string method's temporary file handle.
645 *
646 * @return temporary file handle
647 */
648 public File getToStringImplTempFileHandle() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530649 return toStringImplTempFileHandle;
650 }
651
652 /**
653 * Sets to string method's temporary file handle.
654 *
655 * @param toStringMethod file handle for to string method
656 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530657 private void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530658 toStringImplTempFileHandle = toStringMethod;
659 }
660
661 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530662 * Returns java attribute info.
663 *
664 * @return java attribute info
665 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530666 private JavaAttributeInfo getNewAttrInfo() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530667 return newAttrInfo;
668 }
669
670 /**
671 * Sets java attribute info.
672 *
673 * @param newAttrInfo java attribute info
674 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530675 private void setNewAttrInfo(JavaAttributeInfo newAttrInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530676
Bharat saraswal2f11f652016-03-25 18:19:46 +0530677 if (newAttrInfo != null) {
678 isAttributePresent = true;
679 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530680 this.newAttrInfo = newAttrInfo;
681 }
682
683 /**
684 * Returns current YANG node.
685 *
686 * @return current YANG node
687 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530688 private YangNode getCurYangNode() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530689 return curYangNode;
690 }
691
692 /**
693 * Sets current YANG node.
694 *
695 * @param curYangNode YANG node
696 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530697 private void setCurYangNode(YangNode curYangNode) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530698 this.curYangNode = curYangNode;
699 }
700
701 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530702 * Returns list of classes to be extended by generated files.
703 *
704 * @return list of classes to be extended by generated files
705 */
706 private List<String> getExtendsList() {
707 return extendsList;
708 }
709
710 /**
711 * Sets class to be extended by generated file.
712 *
713 * @param extendsList list of classes to be extended
714 */
715
716 private void setExtendsList(List<String> extendsList) {
717 this.extendsList = extendsList;
718 }
719
720 /**
721 * Adds class to the extends list.
722 *
723 * @param extend class to be extended
724 */
725 public void addToExtendsList(String extend) {
726 getExtendsList().add(extend);
727 }
728
729 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530730 * Adds attribute for class.
731 *
732 * @param attr attribute info
733 * @throws IOException when fails to append to temporary file
734 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530735 private void addAttribute(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530736 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
Vinod Kumar S38046502016-03-23 15:30:27 +0530737 }
738
739 /**
740 * Adds getter for interface.
741 *
742 * @param attr attribute info
743 * @throws IOException when fails to append to temporary file
744 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530745 private void addGetterForInterface(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530746 appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530747 }
748
749 /**
750 * Adds getter method's impl for class.
751 *
752 * @param attr attribute info
Bharat saraswale2d51d62016-03-23 19:40:35 +0530753 * @param genFiletype generated file type
Vinod Kumar S38046502016-03-23 15:30:27 +0530754 * @throws IOException when fails to append to temporary file
755 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530756 private void addGetterImpl(JavaAttributeInfo attr, int genFiletype) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530757
Bharat saraswale2d51d62016-03-23 19:40:35 +0530758 if ((genFiletype & BUILDER_CLASS_MASK) != 0) {
759 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr) + NEW_LINE);
760 } else {
761 appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
762 + getGetterForClass(attr) + NEW_LINE);
763 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530764 }
765
766 /**
767 * Adds setter for interface.
768 *
769 * @param attr attribute info
770 * @throws IOException when fails to append to temporary file
771 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530772 private void addSetterForInterface(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530773 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530774 getSetterString(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530775 }
776
777 /**
778 * Adds setter's implementation for class.
779 *
780 * @param attr attribute info
781 * @throws IOException when fails to append to temporary file
782 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530783 private void addSetterImpl(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530784 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530785 getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530786 }
787
788 /**
789 * Adds build method for interface.
790 *
791 * @return build method for interface
792 * @throws IOException when fails to append to temporary file
793 */
794 public String addBuildMethodForInterface() throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530795 return parseBuilderInterfaceBuildMethodString(generatedJavaClassName);
796 }
797
798 /**
799 * Adds build method's implementation for class.
800 *
801 * @return build method implementation for class
802 * @throws IOException when fails to append to temporary file
803 */
804 public String addBuildMethodImpl() throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530805 return getBuildString(generatedJavaClassName) + NEW_LINE;
Vinod Kumar S38046502016-03-23 15:30:27 +0530806 }
807
808 /**
809 * Adds constructor for class.
810 *
811 * @param attr attribute info
812 * @throws IOException when fails to append to temporary file
813 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530814 private void addConstructor(JavaAttributeInfo attr) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530815 appendToFile(getConstructorImplTempFileHandle(), getConstructor(generatedJavaClassName, attr));
816 }
817
818 /**
819 * Adds default constructor for class.
820 *
Bharat saraswale2d51d62016-03-23 19:40:35 +0530821 * @param modifier modifier for constructor.
822 * @param toAppend string which need to be appended with the class name
Vinod Kumar S38046502016-03-23 15:30:27 +0530823 * @return default constructor for class
824 * @throws IOException when fails to append to file
825 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530826 public String addDefaultConstructor(String modifier, String toAppend) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530827 return NEW_LINE + getDefaultConstructorString(generatedJavaClassName + toAppend, modifier);
828 }
829
830 /**
831 * Adds typedef constructor for class.
832 *
833 * @return typedef constructor for class
834 * @throws IOException when fails to append to file
835 */
836 public String addTypeDefConstructor() throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530837 return NEW_LINE + getJavaDoc(TYPE_DEF_CONSTRUCTOR, generatedJavaClassName, false)
838 + getTypeDefConstructor(newAttrInfo, generatedJavaClassName) + NEW_LINE;
839 }
840
841 /**
842 * Adds default constructor for class.
843 *
844 * @return default constructor for class
845 * @throws IOException when fails to append to file
846 */
847 public String addTypeDefsSetter() throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530848 return getJavaDoc(TYPE_DEF_SETTER_METHOD, generatedJavaClassName, false) + getSetterForTypeDefClass(newAttrInfo)
849 + NEW_LINE;
850 }
851
852 /**
853 * Adds default constructor for class.
854 *
855 * @return default constructor for class
856 * @throws IOException when fails to append to file
857 */
858 public String addOfMethod() throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530859 return getJavaDoc(OF_METHOD, generatedJavaClassName, false)
Bharat saraswale2d51d62016-03-23 19:40:35 +0530860 + getOfMethod(generatedJavaClassName, newAttrInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530861 }
862
863 /**
864 * Adds hash code method for class.
865 *
866 * @param attr attribute info
867 * @throws IOException when fails to append to temporary file
868 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530869 private void addHashCodeMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530870 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530871 }
872
873 /**
874 * Adds equals method for class.
875 *
876 * @param attr attribute info
877 * @throws IOException when fails to append to temporary file
878 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530879 private void addEqualsMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530880 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530881 }
882
883 /**
884 * Adds ToString method for class.
885 *
886 * @param attr attribute info
887 * @throws IOException when fails to append to temporary file
888 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530889 private void addToStringMethod(JavaAttributeInfo attr) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530890 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
Vinod Kumar S38046502016-03-23 15:30:27 +0530891 }
892
893 /**
894 * Returns a temporary file handle for the specific file type.
895 *
896 * @param fileName file name
897 * @return temporary file handle
898 * @throws IOException when fails to create new file handle
899 */
900 private File getTemporaryFileHandle(String fileName) throws IOException {
901
902 String path = getTempDirPath();
903 File dir = new File(path);
904 if (!dir.exists()) {
905 dir.mkdirs();
906 }
907
908 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
909 if (!file.exists()) {
910 file.createNewFile();
Vinod Kumar S38046502016-03-23 15:30:27 +0530911 }
912 return file;
913 }
914
915 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +0530916 * Returns a temporary file handle for the specific file type.
917 *
918 * @param fileName file name
919 * @return temporary file handle
920 * @throws IOException when fails to create new file handle
921 */
922 private File getJavaFileHandle(String fileName) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530923 createPackage(absoluteDirPath, getJavaFileInfo().getJavaName());
Bharat saraswale2d51d62016-03-23 19:40:35 +0530924 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
925 }
926
927 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530928 * Returns data from the temporary files.
929 *
930 * @param file temporary file handle
931 * @return stored data from temporary files
932 * @throws IOException when failed to get data from the given file
933 */
934 public String getTemporaryDataFromFileHandle(File file) throws IOException {
935
936 String path = getTempDirPath();
937 if (new File(path + file.getName()).exists()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530938 return readAppendFile(path + file.getName(), EMPTY_STRING);
Vinod Kumar S38046502016-03-23 15:30:27 +0530939 } else {
940 throw new IOException("Unable to get data from the given "
Bharat saraswale2d51d62016-03-23 19:40:35 +0530941 + file.getName() + " file for " + generatedJavaClassName + PERIOD);
Vinod Kumar S38046502016-03-23 15:30:27 +0530942 }
943 }
944
945 /**
946 * Returns temporary directory path.
947 *
948 * @return directory path
949 */
950 private String getTempDirPath() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530951 return getPackageDirPathFromJavaJPackage(absoluteDirPath) + SLASH + generatedJavaClassName
952 + TEMP_FOLDER_NAME_SUFIX + SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +0530953 }
954
955 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530956 * Parses attribute to get the attribute string.
Vinod Kumar S38046502016-03-23 15:30:27 +0530957 *
958 * @param attr attribute info
959 * @return attribute string
960 */
961 private String parseAttribute(JavaAttributeInfo attr) {
962
963 /*
964 * TODO: check if this utility needs to be called or move to the caller
965 */
janani bde4ffab2016-04-15 16:18:30 +0530966 String attributeName = getCamelCase(getSmallCase(attr.getAttributeName()), null);
Vinod Kumar S38046502016-03-23 15:30:27 +0530967 if (attr.isQualifiedName()) {
968 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
969 attributeName, attr.isListAttr());
970 } else {
971 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
972 attr.isListAttr());
973 }
974 }
975
976 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530977 * Appends content to temporary file.
Vinod Kumar S38046502016-03-23 15:30:27 +0530978 *
979 * @param file temporary file
980 * @param data data to be appended
981 * @throws IOException when fails to append to file
982 */
983 private void appendToFile(File file, String data) throws IOException {
984
985 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530986 insertDataIntoJavaFile(file, data);
Vinod Kumar S38046502016-03-23 15:30:27 +0530987 } catch (IOException ex) {
988 throw new IOException("failed to write in temp file.");
989 }
990 }
991
992 /**
993 * Adds current node info as and attribute to the parent generated file.
994 *
995 * @param curNode current node which needs to be added as an attribute in
996 * the parent generated code
997 * @param isList is list construct
998 * @throws IOException IO operation exception
999 */
1000 public void addCurNodeInfoInParentTempFile(YangNode curNode,
1001 boolean isList) throws IOException {
1002
1003 YangNode parent = getParentNodeInGenCode(curNode);
1004 if (!(parent instanceof JavaCodeGenerator)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301005 throw new TranslatorException("missing parent node to contain current node info in generated file");
Vinod Kumar S38046502016-03-23 15:30:27 +05301006 }
1007 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1008 parent, isList);
1009
1010 if (!(parent instanceof HasTempJavaCodeFragmentFiles)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301011 throw new TranslatorException("missing parent temp file handle");
Vinod Kumar S38046502016-03-23 15:30:27 +05301012 }
1013 ((HasTempJavaCodeFragmentFiles) parent)
1014 .getTempJavaCodeFragmentFiles()
1015 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1016 }
1017
1018 /**
1019 * Adds leaf attributes in generated files.
1020 *
1021 * @param listOfLeaves list of YANG leaf
1022 * @param curNode current data model node
1023 * @throws IOException IO operation fail
1024 */
1025 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1026 YangNode curNode) throws IOException {
1027
1028 if (listOfLeaves != null) {
1029 for (YangLeaf leaf : listOfLeaves) {
1030 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(curNode,
1031 leaf.getDataType(),
1032 leaf.getLeafName(), false);
1033 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1034 }
1035 }
1036 }
1037
1038 /**
1039 * Adds leaf list's attributes in generated files.
1040 *
1041 * @param listOfLeafList list of YANG leaves
1042 * @param curNode cached file handle
1043 * @throws IOException IO operation fail
1044 */
1045 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList,
1046 YangNode curNode) throws IOException {
1047
1048 if (listOfLeafList != null) {
1049
1050 /*
1051 * Check if the attribute is of type list, then the java.lang.list
1052 * needs to be imported.
1053 */
1054 if (listOfLeafList.size() != 0) {
1055 if (!(curNode instanceof HasJavaImportData)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301056 throw new TranslatorException("missing import info in current data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +05301057
1058 }
1059 ((HasJavaImportData) curNode).getJavaImportData()
1060 .setIfListImported(true);
1061
1062 }
1063
1064 for (YangLeafList leafList : listOfLeafList) {
1065 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfLeaf(
1066 curNode, leafList.getDataType(), leafList.getLeafName(),
1067 true);
1068 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1069 }
1070 }
1071 }
1072
1073 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301074 * Adds all the leaves in the current data model node as part of the
Vinod Kumar S38046502016-03-23 15:30:27 +05301075 * generated temporary file.
1076 *
1077 * @param curNode java file info of the generated file
1078 * @throws IOException IO operation fail
1079 */
1080 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode) throws IOException {
1081
1082 if (curNode instanceof YangLeavesHolder) {
1083 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
1084 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), curNode);
1085 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), curNode);
1086 }
1087 }
1088
1089 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +05301090 * Adds leaf attributes in generated files.
1091 *
1092 * @param curNode current data model node
1093 * @throws IOException IO operation fail
1094 */
1095 public void addTypeDefAttributeToTempFiles(YangNode curNode) throws IOException {
1096
1097 JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfTypeDef(curNode,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +05301098 ((YangTypeDef) curNode).getTypeDefBaseType(),
Bharat saraswale2d51d62016-03-23 19:40:35 +05301099 ((YangTypeDef) curNode).getName(), false);
1100 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1101 }
1102
1103 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301104 * Adds the new attribute info to the target generated temporary files.
Vinod Kumar S38046502016-03-23 15:30:27 +05301105 *
1106 * @param newAttrInfo the attribute info that needs to be added to temporary
1107 * files
1108 * @throws IOException IO operation fail
1109 */
1110 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1111 throws IOException {
1112
Bharat saraswale2d51d62016-03-23 19:40:35 +05301113 setNewAttrInfo(newAttrInfo);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301114 if (isAttributePresent) {
1115 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
1116 addAttribute(newAttrInfo);
1117 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301118
Bharat saraswal2f11f652016-03-25 18:19:46 +05301119 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
1120 addGetterForInterface(newAttrInfo);
1121 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301122
Bharat saraswal2f11f652016-03-25 18:19:46 +05301123 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
1124 addSetterForInterface(newAttrInfo);
1125 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301126
Bharat saraswal2f11f652016-03-25 18:19:46 +05301127 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
1128 addGetterImpl(newAttrInfo, generatedJavaFiles);
1129 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301130
Bharat saraswal2f11f652016-03-25 18:19:46 +05301131 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
1132 addSetterImpl(newAttrInfo);
1133 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301134
Bharat saraswal2f11f652016-03-25 18:19:46 +05301135 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
1136 addConstructor(newAttrInfo);
1137 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301138
Bharat saraswal2f11f652016-03-25 18:19:46 +05301139 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
1140 addHashCodeMethod(newAttrInfo);
1141 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301142
Bharat saraswal2f11f652016-03-25 18:19:46 +05301143 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
1144 addEqualsMethod(newAttrInfo);
1145 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301146
Bharat saraswal2f11f652016-03-25 18:19:46 +05301147 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
1148 addToStringMethod(newAttrInfo);
1149 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301150 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301151 }
1152
1153 /**
Bharat saraswald9822e92016-04-05 15:13:44 +05301154 * Returns java file info.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301155 *
1156 * @return java file info
1157 */
1158 private JavaFileInfo getJavaFileInfo() {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301159 return ((HasJavaFileInfo) getCurYangNode()).getJavaFileInfo();
1160 }
1161
1162 /**
1163 * Returns java class name.
1164 *
1165 * @param suffix for the class name based on the file type
1166 * @return java class name
1167 */
1168 private String getJavaClassName(String suffix) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301169 return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
Bharat saraswale2d51d62016-03-23 19:40:35 +05301170 }
1171
1172 /**
1173 * Returns the directory path.
1174 *
1175 * @return directory path
1176 */
1177 private String getDirPath() {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301178 return getJavaFileInfo().getPackageFilePath();
1179 }
1180
1181 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301182 * Constructs java code exit.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301183 *
1184 * @param fileType generated file type
1185 * @param curNode current YANG node
1186 * @throws IOException when fails to generate java files
1187 */
1188 public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
1189
1190 setCurYangNode(curNode);
1191 List<String> imports = new ArrayList<>();
Bharat saraswal2f11f652016-03-25 18:19:46 +05301192 if (curNode instanceof HasJavaImportData && isAttributePresent) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301193 imports = ((HasJavaImportData) curNode).getJavaImportData().getImports(getNewAttrInfo());
1194 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301195
Bharat saraswale2d51d62016-03-23 19:40:35 +05301196 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301197 * Prepares java file generator for extends list.
Bharat saraswale2d51d62016-03-23 19:40:35 +05301198 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301199 prepareJavaFileGeneratorForExtendsList(getExtendsList());
1200
1201 /**
1202 * Generate java code.
1203 */
1204 if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0) {
1205
1206 /**
1207 * Adds import for HasAugmentation class.
1208 */
1209 if (isHasAugmentationExtended(getExtendsList())) {
1210 addHasAugmentationImport(curNode, imports, true);
1211 }
1212
1213 if (isAugmentedInfoExtended(getExtendsList())) {
1214 addAugmentedInfoImport(curNode, imports, true);
1215 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301216
1217 /**
1218 * Create interface file.
1219 */
1220 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301221 setInterfaceJavaFileHandle(
1222 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301223 /**
1224 * Create builder interface file.
1225 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301226 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1227
1228 setBuilderInterfaceJavaFileHandle(
1229 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1230 setBuilderInterfaceJavaFileHandle(
1231 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode, isAttributePresent));
1232 /**
1233 * Append builder interface file to interface file and close it.
1234 */
1235 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1236 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301237 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswale2d51d62016-03-23 19:40:35 +05301238
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301239 if (isHasAugmentationExtended(getExtendsList())) {
1240 addHasAugmentationImport(curNode, imports, false);
1241 }
1242 if (isAugmentedInfoExtended(getExtendsList())) {
1243 addAugmentedInfoImport(curNode, imports, false);
1244 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301245 }
1246
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301247 if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301248
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301249 if (isAttributePresent) {
1250 addImportsToStringAndHasCodeMethods(curNode, imports);
1251 }
1252 if (isHasAugmentationExtended(getExtendsList())) {
1253 addAugmentedInfoImport(curNode, imports, true);
1254 addArrayListImport(curNode, imports, true);
1255 }
Bharat saraswale2d51d62016-03-23 19:40:35 +05301256
1257 /**
1258 * Create builder class file.
1259 */
1260 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
Bharat saraswal2f11f652016-03-25 18:19:46 +05301261 setBuilderClassJavaFileHandle(
1262 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent));
Bharat saraswale2d51d62016-03-23 19:40:35 +05301263 /**
1264 * Create impl class file.
1265 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301266 if ((fileType & IMPL_CLASS_MASK) != 0) {
1267 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1268 setImplClassJavaFileHandle(
1269 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent));
1270 /**
1271 * Append impl class to builder class and close it.
1272 */
1273 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1274 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301275 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswale2d51d62016-03-23 19:40:35 +05301276 }
1277
1278 /**
1279 * Creates type def class file.
1280 */
1281 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +05301282 addImportsToStringAndHasCodeMethods(curNode, imports);
Bharat saraswale2d51d62016-03-23 19:40:35 +05301283 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
1284 setTypedefClassJavaFileHandle(generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports));
1285 }
1286
1287 /**
1288 * Close all the file handles.
1289 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301290 close(false);
Bharat saraswale2d51d62016-03-23 19:40:35 +05301291 }
1292
1293 /**
Vinod Kumar S38046502016-03-23 15:30:27 +05301294 * Removes all temporary file handles.
1295 *
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301296 * @param isErrorOccurred when translator fails to generate java files we need to close
1297 * all open file handles include temporary files and java files.
1298 *
Vinod Kumar S38046502016-03-23 15:30:27 +05301299 * @throws IOException when failed to delete the temporary files
1300 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301301 public void close(boolean isErrorOccurred) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +05301302
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301303 boolean isError = isErrorOccurred;
1304 /**
1305 * Close all java file handles and when error occurs delete the files.
1306 */
Bharat saraswal2f11f652016-03-25 18:19:46 +05301307 if ((generatedJavaFiles & INTERFACE_MASK) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301308 closeFile(getInterfaceJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301309 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301310 if ((generatedJavaFiles & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301311 closeFile(getBuilderClassJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301312 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301313 if ((generatedJavaFiles & BUILDER_INTERFACE_MASK) != 0) {
1314 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1315 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301316 if ((generatedJavaFiles & IMPL_CLASS_MASK) != 0) {
1317 closeFile(getImplClassJavaFileHandle(), true);
1318 }
Bharat saraswal2f11f652016-03-25 18:19:46 +05301319 if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301320 closeFile(getTypedefClassJavaFileHandle(), isError);
Bharat saraswal2f11f652016-03-25 18:19:46 +05301321 }
Vinod Kumar S38046502016-03-23 15:30:27 +05301322
Bharat saraswal2f11f652016-03-25 18:19:46 +05301323 /**
1324 * Close all temporary file handles and delete the files.
1325 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +05301326 if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
1327 closeFile(getGetterInterfaceTempFileHandle(), true);
1328 }
1329 if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
1330 closeFile(getGetterImplTempFileHandle(), true);
1331 }
1332 if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
1333 closeFile(getSetterInterfaceTempFileHandle(), true);
1334 }
1335 if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
1336 closeFile(getSetterImplTempFileHandle(), true);
1337 }
1338 if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
1339 closeFile(getConstructorImplTempFileHandle(), true);
1340 }
1341 if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
1342 closeFile(getAttributesTempFileHandle(), true);
1343 }
1344 if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
1345 closeFile(getHashCodeImplTempFileHandle(), true);
1346 }
1347 if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
1348 closeFile(getToStringImplTempFileHandle(), true);
1349 }
1350 if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
1351 closeFile(getEqualsImplTempFileHandle(), true);
1352 }
1353
1354 clean(getTempDirPath());
1355 generatedTempFiles = 0;
Vinod Kumar S38046502016-03-23 15:30:27 +05301356 }
1357
Vinod Kumar S38046502016-03-23 15:30:27 +05301358}