blob: 467ff8a94faaefd10c92b4db2c42551c841b0247 [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.yangutils.translator.tojava;
17
18import java.io.File;
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.List;
22import java.util.Set;
23
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053024import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053025import org.onosproject.yangutils.datamodel.YangCase;
26import org.onosproject.yangutils.datamodel.YangEnum;
27import org.onosproject.yangutils.datamodel.YangEnumeration;
28import org.onosproject.yangutils.datamodel.YangLeaf;
29import org.onosproject.yangutils.datamodel.YangLeafList;
30import org.onosproject.yangutils.datamodel.YangLeavesHolder;
31import org.onosproject.yangutils.datamodel.YangNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053035
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
38import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
45import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
50import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
51import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
52import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
54import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
55import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
56import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
57import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
58import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
59import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
61import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053062import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
64import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
65import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
66import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
67import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
68import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
70import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
72import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
73import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
74import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
77import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
78import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
80import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
81import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
82import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
84import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
85import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils
86 .isHasAugmentationExtended;
87import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils
88 .prepareJavaFileGeneratorForExtendsList;
89import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
90import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
91import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
92import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
93import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
94import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
95import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053096import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
97import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
98import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
99import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
100import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
101import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
102import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530104import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
105import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
106import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
107
108/**
109 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530110 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111 */
112public class TempJavaFragmentFiles {
113 /**
114 * Information about the java files being generated.
115 */
116 private JavaFileInfo javaFileInfo;
117 /**
118 * Imported class info.
119 */
120 private JavaImportData javaImportData;
121 /**
122 * The variable which guides the types of temporary files generated using
123 * the temporary generated file types mask.
124 */
125 private int generatedTempFiles;
126 /**
127 * Absolute path where the target java file needs to be generated.
128 */
129 private String absoluteDirPath;
130 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530131 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530132 */
133 private List<String> extendsList = new ArrayList<>();
134 /**
135 * File type extension for java classes.
136 */
137 private static final String JAVA_FILE_EXTENSION = ".java";
138 /**
139 * File type extension for temporary classes.
140 */
141 private static final String TEMP_FILE_EXTENSION = ".tmp";
142 /**
143 * Folder suffix for temporary files folder.
144 */
145 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530146
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530147 /**
148 * File name for getter method.
149 */
150 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530151
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 /**
153 * File name for setter method.
154 */
155 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530156
157 /**
158 * File name for getter method implementation.
159 */
160 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
161
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530162 /**
163 * File name for setter method implementation.
164 */
165 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530167 /**
168 * File name for attributes.
169 */
170 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
171 /**
172 * File name for to string method.
173 */
174 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
175 /**
176 * File name for hash code method.
177 */
178 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
179 /**
180 * File name for equals method.
181 */
182 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530184 /**
185 * File name for temporary enum class.
186 */
187 private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530188
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 /**
190 * File name for interface java file name suffix.
191 */
192 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
193 /**
194 * File name for builder interface file name suffix.
195 */
196 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
197 /**
198 * File name for builder class file name suffix.
199 */
200 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
201 /**
202 * File name for impl class file name suffix.
203 */
204 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530205
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 /**
207 * File name for enum class file name suffix.
208 */
209 private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530210
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530211 /**
212 * Java file handle for interface file.
213 */
214 private File interfaceJavaFileHandle;
215 /**
216 * Java file handle for builder interface file.
217 */
218 private File builderInterfaceJavaFileHandle;
219 /**
220 * Java file handle for builder class file.
221 */
222 private File builderClassJavaFileHandle;
223 /**
224 * Java file handle for impl class file.
225 */
226 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530227
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530228 /**
229 * Temporary file handle for attribute.
230 */
231 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530232
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530233 /**
234 * Temporary file handle for getter of interface.
235 */
236 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530237
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530238 /**
239 * Temporary file handle for setter of interface.
240 */
241 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530242
243 /**
244 * Temporary file handle for getter of class.
245 */
246 private File getterImplTempFileHandle;
247
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530248 /**
249 * Temporary file handle for setter of class.
250 */
251 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530252
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530253 /**
254 * Temporary file handle for hash code method of class.
255 */
256 private File hashCodeImplTempFileHandle;
257 /**
258 * Temporary file handle for equals method of class.
259 */
260 private File equalsImplTempFileHandle;
261 /**
262 * Temporary file handle for to string method of class.
263 */
264 private File toStringImplTempFileHandle;
265 /**
266 * Temporary file handle for enum class file.
267 */
268 private File enumClassTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530269
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530270 /**
271 * Import info for case.
272 */
273 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530274
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530275 /**
276 * Is attribute added.
277 */
278 private boolean isAttributePresent = false;
279 /**
280 * Current enum's value.
281 */
282 private int enumValue;
283 /*
284 * Java file handle for enum class.
285 */
286 private File enumClassJavaFileHandle;
287
Ray Milkey994c4982016-05-11 18:39:39 +0000288 public TempJavaFragmentFiles() {
289 }
290
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530291 /**
292 * Returns enum class java file handle.
293 *
294 * @return enum class java file handle
295 */
296 private File getEnumClassJavaFileHandle() {
297 return enumClassJavaFileHandle;
298 }
299
300 /**
301 * Sets enum class java file handle.
302 *
303 * @param enumClassJavaFileHandle enum class java file handle
304 */
305 private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) {
306 this.enumClassJavaFileHandle = enumClassJavaFileHandle;
307 }
308
309 /**
310 * Returns enum's value.
311 *
312 * @return enum's value
313 */
314 private int getEnumValue() {
315 return enumValue;
316 }
317
318 /**
319 * Sets enum's value.
320 *
321 * @param enumValue enum's value
322 */
323 private void setEnumValue(int enumValue) {
324 this.enumValue = enumValue;
325 }
326
327 /**
328 * Retrieves the absolute path where the file needs to be generated.
329 *
330 * @return absolute path where the file needs to be generated
331 */
332 private String getAbsoluteDirPath() {
333 return absoluteDirPath;
334 }
335
336 /**
337 * Sets absolute path where the file needs to be generated.
338 *
339 * @param absoluteDirPath absolute path where the file needs to be
340 * generated.
341 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530342 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530343 this.absoluteDirPath = absoluteDirPath;
344 }
345
346 /**
347 * Sets the generated java file information.
348 *
349 * @param javaFileInfo generated java file information
350 */
351 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
352 this.javaFileInfo = javaFileInfo;
353 }
354
355 /**
356 * Retrieves the generated java file information.
357 *
358 * @return generated java file information
359 */
360 public JavaFileInfo getJavaFileInfo() {
361 return javaFileInfo;
362 }
363
364 /**
365 * Retrieves the generated temp files.
366 *
367 * @return generated temp files
368 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530369 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530370 return generatedTempFiles;
371 }
372
373 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530374 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530375 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530376 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530377 generatedTempFiles = 0;
378 }
379
380 /**
381 * Sets generated file files.
382 *
383 * @param generatedTempFile generated file
384 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530385 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530386 generatedTempFiles |= generatedTempFile;
387 }
388
389 /**
390 * Retrieves the generated Java files.
391 *
392 * @return generated Java files
393 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530394 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530395 return getJavaFileInfo().getGeneratedFileTypes();
396 }
397
398 /**
399 * Retrieves the mapped Java class name.
400 *
401 * @return mapped Java class name
402 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530403 String getGeneratedJavaClassName() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530404 return getJavaFileInfo().getJavaName();
405 }
406
407 /**
408 * Retrieves the import data for the generated Java file.
409 *
410 * @return import data for the generated Java file
411 */
412 public JavaImportData getJavaImportData() {
413 return javaImportData;
414 }
415
416 /**
417 * Sets import data for the generated Java file.
418 *
419 * @param javaImportData import data for the generated Java file
420 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530421 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530422 this.javaImportData = javaImportData;
423 }
424
425 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530426 * Retrieves the status of any attributes added.
427 *
428 * @return status of any attributes added
429 */
430 public boolean isAttributePresent() {
431 return isAttributePresent;
432 }
433
434 /**
435 * Sets status of any attributes added.
436 *
437 * @param attributePresent status of any attributes added
438 */
439 public void setAttributePresent(boolean attributePresent) {
440 isAttributePresent = attributePresent;
441 }
442
443 /**
444 * Returns getter methods's temporary file handle.
445 *
446 * @return temporary file handle
447 */
448 public File getGetterInterfaceTempFileHandle() {
449 return getterInterfaceTempFileHandle;
450 }
451
452 /**
453 * Sets to getter method's temporary file handle.
454 *
455 * @param getterForInterface file handle for to getter method
456 */
457 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
458 getterInterfaceTempFileHandle = getterForInterface;
459 }
460
461 /**
462 * Returns setter method's temporary file handle.
463 *
464 * @return temporary file handle
465 */
466 public File getSetterInterfaceTempFileHandle() {
467 return setterInterfaceTempFileHandle;
468 }
469
470 /**
471 * Sets to setter method's temporary file handle.
472 *
473 * @param setterForInterface file handle for to setter method
474 */
475 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
476 setterInterfaceTempFileHandle = setterForInterface;
477 }
478
479 /**
480 * Returns setter method's impl's temporary file handle.
481 *
482 * @return temporary file handle
483 */
484 public File getSetterImplTempFileHandle() {
485 return setterImplTempFileHandle;
486 }
487
488 /**
489 * Sets to setter method's impl's temporary file handle.
490 *
491 * @param setterImpl file handle for to setter method's implementation class
492 */
493 private void setSetterImplTempFileHandle(File setterImpl) {
494 setterImplTempFileHandle = setterImpl;
495 }
496
497 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530498 * Creates an instance of temporary java code fragment.
499 *
500 * @param javaFileInfo generated java file information
501 * @throws IOException when fails to create new file handle
502 */
503 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
504 throws IOException {
505 setExtendsList(new ArrayList<>());
506 setJavaImportData(new JavaImportData());
507 setJavaFileInfo(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530508 clearGeneratedTempFileMask();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530509 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
510 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530511
512 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530513 * Initialize getter when generation file type matches to interface
514 * mask.
515 */
516 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
517 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
518 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530519
520 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530521 * Initialize getter and setter when generation file type matches to
522 * builder interface mask.
523 */
524 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
525 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
526 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
527 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530528
529 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530530 * Initialize getterImpl, setterImpl and attributes when generation file
531 * type matches to builder class mask.
532 */
533 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
534 addGeneratedTempFile(ATTRIBUTES_MASK);
535 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
536 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
537 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530538
539 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530540 * Initialize getterImpl, attributes, constructor, hash code, equals and
541 * to strings when generation file type matches to impl class mask.
542 */
543 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
544 addGeneratedTempFile(ATTRIBUTES_MASK);
545 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530546 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
547 addGeneratedTempFile(EQUALS_IMPL_MASK);
548 addGeneratedTempFile(TO_STRING_IMPL_MASK);
549 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530550
551 /*
552 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530553 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530554 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530555 addGeneratedTempFile(ATTRIBUTES_MASK);
556 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
557 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
558 addGeneratedTempFile(EQUALS_IMPL_MASK);
559 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530560 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530561
562 /*
563 * Initialize getter and setter when generation file type matches to
564 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530565 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530566 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
567 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
568 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530569 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530570 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530571 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530572
573 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530574 * Initialize enum when generation file type matches to enum class mask.
575 */
576 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
577 addGeneratedTempFile(ENUM_IMPL_MASK);
578 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530579
580 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530581 * Set temporary file handles.
582 */
583 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
584 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
585 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530586
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530587 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
588 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
589 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530590
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530591 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
592 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
593 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530594
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530595 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
596 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
597 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530598
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530599 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
600 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
601 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530602
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530603 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
604 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
605 }
606 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
607 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
608 }
609 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
610 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
611 }
612 if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) {
613 setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
614 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530615 }
616
617 /**
618 * Returns java file handle for interface file.
619 *
620 * @return java file handle for interface file
621 */
622 private File getInterfaceJavaFileHandle() {
623 return interfaceJavaFileHandle;
624 }
625
626 /**
627 * Sets the java file handle for interface file.
628 *
629 * @param interfaceJavaFileHandle java file handle
630 */
631 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
632 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
633 }
634
635 /**
636 * Returns java file handle for builder interface file.
637 *
638 * @return java file handle for builder interface file
639 */
640 private File getBuilderInterfaceJavaFileHandle() {
641 return builderInterfaceJavaFileHandle;
642 }
643
644 /**
645 * Sets the java file handle for builder interface file.
646 *
647 * @param builderInterfaceJavaFileHandle java file handle
648 */
649 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
650 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
651 }
652
653 /**
654 * Returns java file handle for builder class file.
655 *
656 * @return java file handle for builder class file
657 */
658 private File getBuilderClassJavaFileHandle() {
659 return builderClassJavaFileHandle;
660 }
661
662 /**
663 * Sets the java file handle for builder class file.
664 *
665 * @param builderClassJavaFileHandle java file handle
666 */
667 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
668 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
669 }
670
671 /**
672 * Returns java file handle for impl class file.
673 *
674 * @return java file handle for impl class file
675 */
676 private File getImplClassJavaFileHandle() {
677 return implClassJavaFileHandle;
678 }
679
680 /**
681 * Sets the java file handle for impl class file.
682 *
683 * @param implClassJavaFileHandle java file handle
684 */
685 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
686 this.implClassJavaFileHandle = implClassJavaFileHandle;
687 }
688
689 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530690 * Returns attribute's temporary file handle.
691 *
692 * @return temporary file handle
693 */
694 public File getAttributesTempFileHandle() {
695 return attributesTempFileHandle;
696 }
697
698 /**
699 * Sets attribute's temporary file handle.
700 *
701 * @param attributeForClass file handle for attribute
702 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530703 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530704 attributesTempFileHandle = attributeForClass;
705 }
706
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530707
708 /**
709 * Returns getter method's impl's temporary file handle.
710 *
711 * @return temporary file handle
712 */
713 public File getGetterImplTempFileHandle() {
714 return getterImplTempFileHandle;
715 }
716
717 /**
718 * Sets to getter method's impl's temporary file handle.
719 *
720 * @param getterImpl file handle for to getter method's impl
721 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530722 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530723 getterImplTempFileHandle = getterImpl;
724 }
725
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530726
727 /**
728 * Returns hash code method's temporary file handle.
729 *
730 * @return temporary file handle
731 */
732 public File getHashCodeImplTempFileHandle() {
733 return hashCodeImplTempFileHandle;
734 }
735
736 /**
737 * Sets hash code method's temporary file handle.
738 *
739 * @param hashCodeMethod file handle for hash code method
740 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530741 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530742 hashCodeImplTempFileHandle = hashCodeMethod;
743 }
744
745 /**
746 * Returns equals mehtod's temporary file handle.
747 *
748 * @return temporary file handle
749 */
750 public File getEqualsImplTempFileHandle() {
751 return equalsImplTempFileHandle;
752 }
753
754 /**
755 * Sets equals method's temporary file handle.
756 *
757 * @param equalsMethod file handle for to equals method
758 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530759 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530760 equalsImplTempFileHandle = equalsMethod;
761 }
762
763 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530764 * Returns to string method's temporary file handle.
765 *
766 * @return temporary file handle
767 */
768 public File getToStringImplTempFileHandle() {
769 return toStringImplTempFileHandle;
770 }
771
772 /**
773 * Sets to string method's temporary file handle.
774 *
775 * @param toStringMethod file handle for to string method
776 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530777 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530778 toStringImplTempFileHandle = toStringMethod;
779 }
780
781 /**
782 * Returns temporary file handle for enum class file.
783 *
784 * @return temporary file handle for enum class file
785 */
786 public File getEnumClassTempFileHandle() {
787 return enumClassTempFileHandle;
788 }
789
790 /**
791 * Sets temporary file handle for enum class file.
792 *
793 * @param enumClassTempFileHandle temporary file handle for enum class file
794 */
795 private void setEnumClassTempFileHandle(File enumClassTempFileHandle) {
796 this.enumClassTempFileHandle = enumClassTempFileHandle;
797 }
798
799 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530800 * Returns list of classes to be extended by generated files.
801 *
802 * @return list of classes to be extended by generated files
803 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530804 List<String> getExtendsList() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530805 return extendsList;
806 }
807
808 /**
809 * Sets class to be extended by generated file.
810 *
811 * @param extendsList list of classes to be extended
812 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530813 void setExtendsList(List<String> extendsList) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530814 this.extendsList = extendsList;
815 }
816
817 /**
818 * Adds class to the extends list.
819 *
820 * @param extend class to be extended
821 */
822 public void addToExtendsList(String extend) {
823 getExtendsList().add(extend);
824 }
825
826 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530827 * Adds attribute for class.
828 *
829 * @param attr attribute info
830 * @throws IOException when fails to append to temporary file
831 */
832 private void addAttribute(JavaAttributeInfo attr)
833 throws IOException {
834 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
835 }
836
837 /**
838 * Adds getter for interface.
839 *
840 * @param attr attribute info
841 * @throws IOException when fails to append to temporary file
842 */
843 private void addGetterForInterface(JavaAttributeInfo attr)
844 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530845 appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr, getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530846 }
847
848 /**
849 * Adds setter for interface.
850 *
851 * @param attr attribute info
852 * @throws IOException when fails to append to temporary file
853 */
854 private void addSetterForInterface(JavaAttributeInfo attr)
855 throws IOException {
856 appendToFile(getSetterInterfaceTempFileHandle(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530857 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530858 }
859
860 /**
861 * Adds setter's implementation for class.
862 *
863 * @param attr attribute info
864 * @throws IOException when fails to append to temporary file
865 */
866 private void addSetterImpl(JavaAttributeInfo attr)
867 throws IOException {
868 appendToFile(getSetterImplTempFileHandle(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530869 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) +
870 NEW_LINE);
871 }
872
873 /**
874 * Adds getter method's impl for class.
875 *
876 * @param attr attribute info
877 * @throws IOException when fails to append to temporary file
878 */
879 private void addGetterImpl(JavaAttributeInfo attr)
880 throws IOException {
881 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
882 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
883 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
884 getGeneratedJavaFiles()) + NEW_LINE);
885 } else {
886 appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
887 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
888 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530889 }
890
891 /**
892 * Adds build method for interface.
893 *
894 * @return build method for interface
895 * @throws IOException when fails to append to temporary file
896 */
897 String addBuildMethodForInterface()
898 throws IOException {
899 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName());
900 }
901
902 /**
903 * Adds build method's implementation for class.
904 *
905 * @return build method implementation for class
906 * @throws IOException when fails to append to temporary file
907 */
908 String addBuildMethodImpl()
909 throws IOException {
910 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
911 }
912
913 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530914 * Adds default constructor for class.
915 *
916 * @param modifier modifier for constructor.
917 * @param toAppend string which need to be appended with the class name
918 * @return default constructor for class
919 * @throws IOException when fails to append to file
920 */
921 String addDefaultConstructor(String modifier, String toAppend)
922 throws IOException {
923 return NEW_LINE + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier);
924 }
925
926 /**
927 * Adds default constructor for class.
928 *
929 * @return default constructor for class
930 * @throws IOException when fails to append to file
931 */
932 public String addOfMethod()
933 throws IOException {
934 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false)
935 + getOfMethod(getGeneratedJavaClassName(), null);
936 }
937
938 /**
939 * Adds hash code method for class.
940 *
941 * @param attr attribute info
942 * @throws IOException when fails to append to temporary file
943 */
944 private void addHashCodeMethod(JavaAttributeInfo attr)
945 throws IOException {
946 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
947 }
948
949 /**
950 * Adds equals method for class.
951 *
952 * @param attr attribute info
953 * @throws IOException when fails to append to temporary file
954 */
955 private void addEqualsMethod(JavaAttributeInfo attr)
956 throws IOException {
957 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
958 }
959
960 /**
961 * Adds ToString method for class.
962 *
963 * @param attr attribute info
964 * @throws IOException when fails to append to temporary file
965 */
966 private void addToStringMethod(JavaAttributeInfo attr)
967 throws IOException {
968 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
969 }
970
971 /**
972 * Adds enum class attributes to temporary file.
973 *
974 * @param curEnumInfo current YANG enum
975 * @throws IOException when fails to do IO operations.
976 */
977 private void addAttributesForEnumClass(JavaAttributeInfo curEnumInfo)
978 throws IOException {
979 appendToFile(getEnumClassTempFileHandle(),
980 generateEnumAttributeString(curEnumInfo.getAttributeName(), getEnumValue()));
981 }
982
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530983
984 /**
985 * Returns a temporary file handle for the specific file type.
986 *
987 * @param fileName file name
988 * @return temporary file handle
989 * @throws IOException when fails to create new file handle
990 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530991 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530992 throws IOException {
993 String path = getTempDirPath();
994 File dir = new File(path);
995 if (!dir.exists()) {
996 dir.mkdirs();
997 }
998 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
999 if (!file.exists()) {
1000 file.createNewFile();
1001 }
1002 return file;
1003 }
1004
1005 /**
1006 * Returns a temporary file handle for the specific file type.
1007 *
1008 * @param fileName file name
1009 * @return temporary file handle
1010 * @throws IOException when fails to create new file handle
1011 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301012 File getJavaFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301013 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301014// createPackage(getAbsoluteDirPath(), getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301015 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1016 }
1017
1018 /**
1019 * Returns data from the temporary files.
1020 *
1021 * @param file temporary file handle
1022 * @return stored data from temporary files
1023 * @throws IOException when failed to get data from the given file
1024 */
1025 public String getTemporaryDataFromFileHandle(File file)
1026 throws IOException {
1027 String path = getTempDirPath();
1028 if (new File(path + file.getName()).exists()) {
1029 return readAppendFile(path + file.getName(), EMPTY_STRING);
1030 } else {
1031 throw new IOException("Unable to get data from the given "
1032 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1033 }
1034 }
1035
1036 /**
1037 * Returns temporary directory path.
1038 *
1039 * @return directory path
1040 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301041 String getTempDirPath() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301042 return getPackageDirPathFromJavaJPackage(getAbsoluteDirPath()) + SLASH + getGeneratedJavaClassName()
1043 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1044 }
1045
1046 /**
1047 * Parses attribute to get the attribute string.
1048 *
1049 * @param attr attribute info
1050 * @return attribute string
1051 */
1052 private String parseAttribute(JavaAttributeInfo attr) {
1053 /*
1054 * TODO: check if this utility needs to be called or move to the caller
1055 */
1056 String attributeName = getCamelCase(getSmallCase(attr.getAttributeName()), null);
1057 if (attr.isQualifiedName()) {
1058 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
1059 attributeName, attr.isListAttr());
1060 } else {
1061 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1062 attr.isListAttr());
1063 }
1064 }
1065
1066 /**
1067 * Appends content to temporary file.
1068 *
1069 * @param file temporary file
1070 * @param data data to be appended
1071 * @throws IOException when fails to append to file
1072 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301073 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301074 throws IOException {
1075 try {
1076 insertDataIntoJavaFile(file, data);
1077 } catch (IOException ex) {
1078 throw new IOException("failed to write in temp file.");
1079 }
1080 }
1081
1082 /**
1083 * Adds current node info as and attribute to the parent generated file.
1084 *
1085 * @param curNode current node which needs to be added as an attribute in
1086 * the parent generated code
1087 * @param isList is list construct
1088 * @throws IOException IO operation exception
1089 */
1090 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
1091 boolean isList)
1092 throws IOException {
1093 YangNode parent = getParentNodeInGenCode(curNode);
1094 if (!(parent instanceof JavaCodeGenerator)) {
1095 throw new TranslatorException("missing parent node to contain current node info in generated file");
1096 }
1097 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1098 parent, isList);
1099 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1100 throw new TranslatorException("missing parent temp file handle");
1101 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301102 getNodesInterfaceFragmentFiles(parent)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301103 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1104 }
1105
1106 /**
1107 * Creates an attribute info object corresponding to a data model node and
1108 * return it.
1109 *
1110 * @param curNode current data model node for which the java code generation
1111 * is being handled
1112 * @param parentNode parent node in which the current node is an attribute
1113 * @param isListNode is the current added attribute needs to be a list
1114 * @return AttributeInfo attribute details required to add in temporary
1115 * files
1116 */
1117 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
1118 YangNode curNode, YangNode parentNode, boolean isListNode) {
1119 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
1120 /*
1121 * Get the import info corresponding to the attribute for import in
1122 * generated java files or qualified access
1123 */
1124 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
1125 curNodeName);
1126 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
1127 throw new TranslatorException("Parent node does not have file info");
1128 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301129 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(parentNode);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301130 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
1131 boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo);
1132 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1133 }
1134
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301135 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1136 TempJavaFragmentFiles tempJavaFragmentFiles;
1137 if (node instanceof RpcNotificationContainer) {
1138 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1139 .getTempJavaCodeFragmentFiles()
1140 .getServiceTempFiles();
1141 } else {
1142 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1143 .getTempJavaCodeFragmentFiles()
1144 .getBeanTempFiles();
1145 }
1146 return tempJavaFragmentFiles;
1147 }
1148
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301149 /**
1150 * Adds parent's info to current node import list.
1151 *
1152 * @param curNode current node for which import list needs to be updated
1153 */
1154 public void addParentInfoInCurNodeTempFile(YangNode curNode) {
1155 caseImportInfo = new JavaQualifiedTypeInfo();
1156 YangNode parent = getParentNodeInGenCode(curNode);
1157 if (!(parent instanceof JavaCodeGenerator)) {
1158 throw new TranslatorException("missing parent node to contain current node info in generated file");
1159 }
1160 if (!(curNode instanceof JavaFileInfoContainer)) {
1161 throw new TranslatorException("missing java file information to get the package details "
1162 + "of attribute corresponding to child node");
1163 }
1164 caseImportInfo.setClassInfo(getCaptialCase(getCamelCase(parent.getName(), null)));
1165 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
1166 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1167 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo);
1168 }
1169
1170 /**
1171 * Adds leaf attributes in generated files.
1172 *
1173 * @param listOfLeaves list of YANG leaf
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301174 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301175 * @throws IOException IO operation fail
1176 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301177 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1178 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301179 throws IOException {
1180 if (listOfLeaves != null) {
1181 for (YangLeaf leaf : listOfLeaves) {
1182 if (!(leaf instanceof JavaLeafInfoContainer)) {
1183 throw new TranslatorException("Leaf does not have java information");
1184 }
1185 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
1186 javaLeaf.updateJavaQualifiedInfo();
1187 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1188 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301189 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1190 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301191 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1192 false);
1193 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1194 }
1195 }
1196 }
1197
1198 /**
1199 * Adds leaf list's attributes in generated files.
1200 *
1201 * @param listOfLeafList list of YANG leaves
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301202 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301203 * @throws IOException IO operation fail
1204 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301205 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301206 throws IOException {
1207 if (listOfLeafList != null) {
1208 for (YangLeafList leafList : listOfLeafList) {
1209 if (!(leafList instanceof JavaLeafInfoContainer)) {
1210 throw new TranslatorException("Leaf-list does not have java information");
1211 }
1212 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
1213 javaLeaf.updateJavaQualifiedInfo();
1214 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1215 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301216 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1217 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301218 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1219 true);
1220 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1221 }
1222 }
1223 }
1224
1225 /**
1226 * Adds all the leaves in the current data model node as part of the
1227 * generated temporary file.
1228 *
1229 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301230 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301231 * @throws IOException IO operation fail
1232 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301233 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1234 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301235 throws IOException {
1236 if (!(curNode instanceof YangLeavesHolder)) {
1237 throw new TranslatorException("Data model node does not have any leaves");
1238 }
1239 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301240 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1241 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301242 }
1243
1244 /**
1245 * Adds enum attributes to temporary files.
1246 *
1247 * @param curNode current YANG node
1248 * @throws IOException when fails to do IO operations
1249 */
1250 public void addEnumAttributeToTempFiles(YangNode curNode)
1251 throws IOException {
1252 if (curNode instanceof YangEnumeration) {
1253 Set<YangEnum> enumSet = ((YangEnumeration) curNode).getEnumSet();
1254 /*
1255 * Get the import info corresponding to the attribute for import in
1256 * generated java files or qualified access
1257 */
1258 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
1259 getJavaFileInfo().getJavaName());
1260 for (YangEnum curEnum : enumSet) {
1261 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(qualifiedTypeInfo,
1262 curEnum.getNamedValue(), null, false, false);
1263 setEnumValue(curEnum.getValue());
1264 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1265 }
1266 } else {
1267 throw new TranslatorException("current node should be of type enum.");
1268 }
1269 }
1270
1271 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301272 * Adds the new attribute info to the target generated temporary files.
1273 *
1274 * @param newAttrInfo the attribute info that needs to be added to temporary
1275 * files
1276 * @throws IOException IO operation fail
1277 */
1278 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1279 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301280 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301281 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1282 addAttribute(newAttrInfo);
1283 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301284
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301285 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
1286 addGetterForInterface(newAttrInfo);
1287 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301288
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301289 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
1290 addSetterForInterface(newAttrInfo);
1291 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301292
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301293 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1294 addSetterImpl(newAttrInfo);
1295 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301296
1297 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1298 addGetterImpl(newAttrInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301299 }
1300 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1301 addHashCodeMethod(newAttrInfo);
1302 }
1303 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1304 addEqualsMethod(newAttrInfo);
1305 }
1306 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1307 addToStringMethod(newAttrInfo);
1308 }
1309 if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) {
1310 addAttributesForEnumClass(newAttrInfo);
1311 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301312 }
1313
1314 /**
1315 * Returns java class name.
1316 *
1317 * @param suffix for the class name based on the file type
1318 * @return java class name
1319 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301320 String getJavaClassName(String suffix) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301321 return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
1322 }
1323
1324 /**
1325 * Returns the directory path.
1326 *
1327 * @return directory path
1328 */
1329 private String getDirPath() {
1330 return getJavaFileInfo().getPackageFilePath();
1331 }
1332
1333 /**
1334 * Constructs java code exit.
1335 *
1336 * @param fileType generated file type
1337 * @param curNode current YANG node
1338 * @throws IOException when fails to generate java files
1339 */
1340 public void generateJavaFile(int fileType, YangNode curNode)
1341 throws IOException {
1342 List<String> imports = new ArrayList<>();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301343 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301344 imports = getJavaImportData().getImports();
1345 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301346 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301347 * Prepares java file generator for extends list.
1348 */
1349 prepareJavaFileGeneratorForExtendsList(getExtendsList());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301350 createPackage(curNode);
1351
1352 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301353 * Generate java code.
1354 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301355 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1356 BUILDER_INTERFACE_MASK) != 0) {
1357 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301358 * Adds import for case.
1359 */
1360 if (curNode instanceof YangCase) {
1361 List<String> importData = ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1362 .getBeanTempFiles().getJavaImportData().getImports();
1363 for (String importInfo : importData) {
1364 if (!imports.contains(importInfo)) {
1365 imports.add(importInfo);
1366 }
1367 }
1368 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301369 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301370 * Adds import for HasAugmentation class.
1371 */
1372 if (isHasAugmentationExtended(getExtendsList())) {
1373 addHasAugmentationImport(curNode, imports, true);
1374 }
1375 if (isAugmentedInfoExtended(getExtendsList())) {
1376 addAugmentedInfoImport(curNode, imports, true);
1377 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301378 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301379 * Create interface file.
1380 */
1381 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1382 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301383 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1384 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301385 * Create builder interface file.
1386 */
1387 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1388 setBuilderInterfaceJavaFileHandle(
1389 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1390 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301391 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1392 isAttributePresent()));
1393 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301394 * Append builder interface file to interface file and close it.
1395 */
1396 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1397 }
1398 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
1399 if (isHasAugmentationExtended(getExtendsList())) {
1400 addHasAugmentationImport(curNode, imports, false);
1401 }
1402 if (isAugmentedInfoExtended(getExtendsList())) {
1403 addAugmentedInfoImport(curNode, imports, false);
1404 }
1405 if (curNode instanceof YangCase) {
1406 removeCaseImport(imports);
1407 }
1408 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301409 if (((fileType & GENERATE_SERVICE_AND_MANAGER) != 0)
1410 && ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0)) {
1411 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301412 addImportsToStringAndHasCodeMethods(curNode, imports);
1413 }
1414 if (isHasAugmentationExtended(getExtendsList())) {
1415 addAugmentedInfoImport(curNode, imports, true);
1416 addArrayListImport(curNode, imports, true);
1417 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301418 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301419 * Create builder class file.
1420 */
1421 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1422 setBuilderClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301423 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent()));
1424 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301425 * Create impl class file.
1426 */
1427 if ((fileType & IMPL_CLASS_MASK) != 0) {
1428 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1429 setImplClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301430 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent()));
1431 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301432 * Append impl class to builder class and close it.
1433 */
1434 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1435 }
1436 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
1437 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301438
1439 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301440 * Creates type enum class file.
1441 */
1442 if ((fileType & GENERATE_ENUM_CLASS) != 0) {
1443 setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
1444 setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
1445 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301446
1447 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301448 * Close all the file handles.
1449 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301450 freeTemporaryResources(false);
1451 }
1452
1453 /**
1454 * Adds imports for ToString and HashCodeMethod.
1455 *
1456 * @param curNode current YANG node
1457 * @param imports import list
1458 * @return import list
1459 */
1460 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1461 imports.add(getJavaImportData().getImportForHashAndEquals());
1462 imports.add(getJavaImportData().getImportForToString());
1463 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301464 }
1465
1466 /**
1467 * Removes case import info from import list.
1468 *
1469 * @param imports list of imports
1470 * @return import for class
1471 */
1472 private List<String> removeCaseImport(List<String> imports) {
1473 if (imports != null && caseImportInfo != null) {
1474 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1475 SEMI_COLAN + NEW_LINE;
1476 imports.remove(caseImport);
1477 }
1478 return imports;
1479 }
1480
1481 /**
1482 * Removes all temporary file handles.
1483 *
1484 * @param isErrorOccurred when translator fails to generate java files we
1485 * need to close all open file handles include temporary files
1486 * and java files.
1487 * @throws IOException when failed to delete the temporary files
1488 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301489 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301490 throws IOException {
1491 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301492 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301493 * Close all java file handles and when error occurs delete the files.
1494 */
1495 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1496 closeFile(getInterfaceJavaFileHandle(), isError);
1497 }
1498 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
1499 closeFile(getBuilderClassJavaFileHandle(), isError);
1500 }
1501 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1502 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1503 }
1504 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
1505 closeFile(getImplClassJavaFileHandle(), true);
1506 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301507
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301508 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
1509 closeFile(getEnumClassJavaFileHandle(), isError);
1510 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301511
1512 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301513 * Close all temporary file handles and delete the files.
1514 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301515 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1516 closeFile(getGetterImplTempFileHandle(), true);
1517 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301518 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1519 closeFile(getAttributesTempFileHandle(), true);
1520 }
1521 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1522 closeFile(getHashCodeImplTempFileHandle(), true);
1523 }
1524 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1525 closeFile(getToStringImplTempFileHandle(), true);
1526 }
1527 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1528 closeFile(getEqualsImplTempFileHandle(), true);
1529 }
1530 if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) {
1531 closeFile(getEnumClassTempFileHandle(), true);
1532 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301533 }
1534
1535 /**
1536 * Returns if the attribute needs to be accessed in a qualified manner or
1537 * not, if it needs to be imported, then the same needs to be done.
1538 *
1539 * @param importInfo import info for the current attribute being added
1540 * @return status of the qualified access to the attribute
1541 */
1542 public boolean getIsQualifiedAccessOrAddToImportList(
1543 JavaQualifiedTypeInfo importInfo) {
1544 boolean isImportPkgEqualCurNodePkg;
1545 if (importInfo.getClassInfo().contentEquals(
1546 getGeneratedJavaClassName())) {
1547 /*
1548 * if the current class name is same as the attribute class name,
1549 * then the attribute must be accessed in a qualified manner.
1550 */
1551 return true;
1552 } else if (importInfo.getPkgInfo() != null) {
1553 /*
1554 * If the attribute type is having the package info, it is contender
1555 * for import list and also need to check if it needs to be a
1556 * qualified access.
1557 */
1558 isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(importInfo);
1559 if (!isImportPkgEqualCurNodePkg) {
1560 /*
1561 * If the package of the attribute added is not same as the
1562 * current class package, then it must either be imported for
1563 * access or it must be a qualified access.
1564 */
1565 boolean isImportAdded = getJavaImportData().addImportInfo(importInfo);
1566 if (!isImportAdded) {
1567 /*
1568 * If the attribute type info is not imported, then it must
1569 * be a qualified access.
1570 */
1571 return true;
1572 }
1573 }
1574 }
1575 return false;
1576 }
1577
1578 /**
1579 * Checks if the import info is same as the package of the current generated
1580 * java file.
1581 *
1582 * @param importInfo import info for an attribute
1583 * @return true if the import info is same as the current nodes package
1584 * false otherwise
1585 */
1586 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1587 return getJavaFileInfo().getPackage()
1588 .contentEquals(importInfo.getPkgInfo());
1589 }
1590}