blob: 59cd01be5f7eb539e6cfec4185bc3665f6713c4d [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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053022
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053023import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053024import org.onosproject.yangutils.datamodel.YangCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangLeafList;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
28import org.onosproject.yangutils.datamodel.YangNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
30import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053031import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
38import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
45import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
46import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
49import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
50import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053051import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
54import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
55import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
56import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053057import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
58import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053061import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053062import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
66import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswalc0e04842016-05-12 13:16:57 +053067import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
69import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
70import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
71import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053073import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
74import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
75import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
77import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053078import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentationHoldersImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053081import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.detectCollisionBwParentAndChildForImport;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053082import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentationHolderExtended;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
Bharat saraswalc0e04842016-05-12 13:16:57 +053084import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.sortImports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
86import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
87import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
88import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
89import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
90import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
91import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
93import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
94import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
95import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
96import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053097import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053098import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
99import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530100import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
101import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
102import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
103
104/**
105 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530106 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107 */
108public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530109
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530110 /**
111 * Information about the java files being generated.
112 */
113 private JavaFileInfo javaFileInfo;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530114
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 /**
116 * Imported class info.
117 */
118 private JavaImportData javaImportData;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530119
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530120 /**
121 * The variable which guides the types of temporary files generated using
122 * the temporary generated file types mask.
123 */
124 private int generatedTempFiles;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530125
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530126 /**
127 * Absolute path where the target java file needs to be generated.
128 */
129 private String absoluteDirPath;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530130
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530131 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530132 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 private JavaExtendsListHolder javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530135 /**
136 * File type extension for java classes.
137 */
138 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530139
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530140 /**
141 * File type extension for temporary classes.
142 */
143 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530144
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530145 /**
146 * Folder suffix for temporary files folder.
147 */
148 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530149
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530150 /**
151 * File name for getter method.
152 */
153 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530154
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 /**
156 * File name for setter method.
157 */
158 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530159
160 /**
161 * File name for getter method implementation.
162 */
163 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
164
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530165 /**
166 * File name for setter method implementation.
167 */
168 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530169
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530170 /**
171 * File name for attributes.
172 */
173 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530174
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530175 /**
176 * File name for to string method.
177 */
178 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530179
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 /**
181 * File name for hash code method.
182 */
183 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530184
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530185 /**
186 * File name for equals method.
187 */
188 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530189
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530190 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530191 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530193 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530194
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530195 /**
196 * File name for interface java file name suffix.
197 */
198 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530199
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530200 /**
201 * File name for builder interface file name suffix.
202 */
203 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530204
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530205 /**
206 * File name for builder class file name suffix.
207 */
208 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530209
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530210 /**
211 * File name for impl class file name suffix.
212 */
213 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530214
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530215 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530216 * Java file handle for interface file.
217 */
218 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530219
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530220 /**
221 * Java file handle for builder interface file.
222 */
223 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530224
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530225 /**
226 * Java file handle for builder class file.
227 */
228 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530229
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530230 /**
231 * Java file handle for impl class file.
232 */
233 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530234
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530235 /**
236 * Temporary file handle for attribute.
237 */
238 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530239
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 /**
241 * Temporary file handle for getter of interface.
242 */
243 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530244
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530245 /**
246 * Temporary file handle for setter of interface.
247 */
248 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530249
250 /**
251 * Temporary file handle for getter of class.
252 */
253 private File getterImplTempFileHandle;
254
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530255 /**
256 * Temporary file handle for setter of class.
257 */
258 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530259
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530260 /**
261 * Temporary file handle for hash code method of class.
262 */
263 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530264
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530265 /**
266 * Temporary file handle for equals method of class.
267 */
268 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530269
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530270 /**
271 * Temporary file handle for to string method of class.
272 */
273 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530274
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530275 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530276 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530277 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530278 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530279
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530280 /**
281 * Import info for case.
282 */
283 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530284
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530285 /**
286 * Is attribute added.
287 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530288 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530289
Bharat saraswalc0e04842016-05-12 13:16:57 +0530290 /**
291 * Creates an instance of temp java fragment files.
292 */
Ray Milkey994c4982016-05-11 18:39:39 +0000293 public TempJavaFragmentFiles() {
294 }
295
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530296 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530297 * Retrieves the absolute path where the file needs to be generated.
298 *
299 * @return absolute path where the file needs to be generated
300 */
301 private String getAbsoluteDirPath() {
302 return absoluteDirPath;
303 }
304
305 /**
306 * Sets absolute path where the file needs to be generated.
307 *
308 * @param absoluteDirPath absolute path where the file needs to be
309 * generated.
310 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530311 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530312 this.absoluteDirPath = absoluteDirPath;
313 }
314
315 /**
316 * Sets the generated java file information.
317 *
318 * @param javaFileInfo generated java file information
319 */
320 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
321 this.javaFileInfo = javaFileInfo;
322 }
323
324 /**
325 * Retrieves the generated java file information.
326 *
327 * @return generated java file information
328 */
329 public JavaFileInfo getJavaFileInfo() {
330 return javaFileInfo;
331 }
332
333 /**
334 * Retrieves the generated temp files.
335 *
336 * @return generated temp files
337 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530338 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530339 return generatedTempFiles;
340 }
341
342 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530343 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530344 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530345 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530346 generatedTempFiles = 0;
347 }
348
349 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530350 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530351 *
352 * @param generatedTempFile generated file
353 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530354 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530355 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530356 setGeneratedTempFiles(generatedTempFiles);
357 }
358
359 /**
360 * Sets generated file files.
361 *
362 * @param generatedTempFile generated file
363 */
364 void setGeneratedTempFiles(int fileType) {
365 generatedTempFiles = fileType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530366 }
367
368 /**
369 * Retrieves the generated Java files.
370 *
371 * @return generated Java files
372 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530373 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530374 return getJavaFileInfo().getGeneratedFileTypes();
375 }
376
377 /**
378 * Retrieves the mapped Java class name.
379 *
380 * @return mapped Java class name
381 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530382 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530383 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530384 }
385
386 /**
387 * Retrieves the import data for the generated Java file.
388 *
389 * @return import data for the generated Java file
390 */
391 public JavaImportData getJavaImportData() {
392 return javaImportData;
393 }
394
395 /**
396 * Sets import data for the generated Java file.
397 *
398 * @param javaImportData import data for the generated Java file
399 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530400 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530401 this.javaImportData = javaImportData;
402 }
403
404 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530405 * Retrieves the status of any attributes added.
406 *
407 * @return status of any attributes added
408 */
409 public boolean isAttributePresent() {
410 return isAttributePresent;
411 }
412
413 /**
414 * Sets status of any attributes added.
415 *
416 * @param attributePresent status of any attributes added
417 */
418 public void setAttributePresent(boolean attributePresent) {
419 isAttributePresent = attributePresent;
420 }
421
422 /**
423 * Returns getter methods's temporary file handle.
424 *
425 * @return temporary file handle
426 */
427 public File getGetterInterfaceTempFileHandle() {
428 return getterInterfaceTempFileHandle;
429 }
430
431 /**
432 * Sets to getter method's temporary file handle.
433 *
434 * @param getterForInterface file handle for to getter method
435 */
436 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
437 getterInterfaceTempFileHandle = getterForInterface;
438 }
439
440 /**
441 * Returns setter method's temporary file handle.
442 *
443 * @return temporary file handle
444 */
445 public File getSetterInterfaceTempFileHandle() {
446 return setterInterfaceTempFileHandle;
447 }
448
449 /**
450 * Sets to setter method's temporary file handle.
451 *
452 * @param setterForInterface file handle for to setter method
453 */
454 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
455 setterInterfaceTempFileHandle = setterForInterface;
456 }
457
458 /**
459 * Returns setter method's impl's temporary file handle.
460 *
461 * @return temporary file handle
462 */
463 public File getSetterImplTempFileHandle() {
464 return setterImplTempFileHandle;
465 }
466
467 /**
468 * Sets to setter method's impl's temporary file handle.
469 *
470 * @param setterImpl file handle for to setter method's implementation class
471 */
472 private void setSetterImplTempFileHandle(File setterImpl) {
473 setterImplTempFileHandle = setterImpl;
474 }
475
476 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530477 * Returns from string method's temporary file handle.
478 *
479 * @return from string method's temporary file handle
480 */
481 public File getFromStringImplTempFileHandle() {
482 return fromStringImplTempFileHandle;
483 }
484
485 /**
486 * Sets from string method's temporary file handle.
487 *
488 * @param fromStringImplTempFileHandle from string method's temporary file
489 * handle
490 */
491 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
492 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
493 }
494
495 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530496 * Creates an instance of temporary java code fragment.
497 *
498 * @param javaFileInfo generated java file information
499 * @throws IOException when fails to create new file handle
500 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530501 TempJavaFragmentFiles(JavaFileInfo javaFileInfo) throws IOException {
502 setJavaExtendsListHolder(new JavaExtendsListHolder());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530503 setJavaImportData(new JavaImportData());
504 setJavaFileInfo(javaFileInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530505 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
506 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530507
508 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530509 * Initialize getter when generation file type matches to interface
510 * mask.
511 */
512 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
513 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
514 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530515
516 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530517 * Initialize getter and setter when generation file type matches to
518 * builder interface mask.
519 */
520 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
521 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
522 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
523 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530524
525 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530526 * Initialize getterImpl, setterImpl and attributes when generation file
527 * type matches to builder class mask.
528 */
529 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
530 addGeneratedTempFile(ATTRIBUTES_MASK);
531 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
532 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
533 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530534
535 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530536 * Initialize getterImpl, attributes, constructor, hash code, equals and
537 * to strings when generation file type matches to impl class mask.
538 */
539 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
540 addGeneratedTempFile(ATTRIBUTES_MASK);
541 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530542 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
543 addGeneratedTempFile(EQUALS_IMPL_MASK);
544 addGeneratedTempFile(TO_STRING_IMPL_MASK);
545 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530546
547 /*
548 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530549 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530550 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530551 addGeneratedTempFile(ATTRIBUTES_MASK);
552 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
553 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
554 addGeneratedTempFile(EQUALS_IMPL_MASK);
555 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530556 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530557 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530558
559 /*
Bharat saraswalc0e04842016-05-12 13:16:57 +0530560 * Initialize temp files to generate enum class.
561 */
562 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
563 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
564 }
565 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530566 * Initialize getter and setter when generation file type matches to
567 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530568 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530569 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
570 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
571 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530572 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530573 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530574 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530575
576 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530577 * Set temporary file handles.
578 */
579 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
580 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
581 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530582
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530583 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
584 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
585 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530586
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530587 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
588 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
589 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530590
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530591 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
592 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
593 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530594
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530595 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
596 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
597 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530598
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530599 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
600 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
601 }
602 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
603 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
604 }
605 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
606 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
607 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530608 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
609 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530610 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530611
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530612 }
613
614 /**
615 * Returns java file handle for interface file.
616 *
617 * @return java file handle for interface file
618 */
619 private File getInterfaceJavaFileHandle() {
620 return interfaceJavaFileHandle;
621 }
622
623 /**
624 * Sets the java file handle for interface file.
625 *
626 * @param interfaceJavaFileHandle java file handle
627 */
628 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
629 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
630 }
631
632 /**
633 * Returns java file handle for builder interface file.
634 *
635 * @return java file handle for builder interface file
636 */
637 private File getBuilderInterfaceJavaFileHandle() {
638 return builderInterfaceJavaFileHandle;
639 }
640
641 /**
642 * Sets the java file handle for builder interface file.
643 *
644 * @param builderInterfaceJavaFileHandle java file handle
645 */
646 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
647 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
648 }
649
650 /**
651 * Returns java file handle for builder class file.
652 *
653 * @return java file handle for builder class file
654 */
655 private File getBuilderClassJavaFileHandle() {
656 return builderClassJavaFileHandle;
657 }
658
659 /**
660 * Sets the java file handle for builder class file.
661 *
662 * @param builderClassJavaFileHandle java file handle
663 */
664 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
665 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
666 }
667
668 /**
669 * Returns java file handle for impl class file.
670 *
671 * @return java file handle for impl class file
672 */
673 private File getImplClassJavaFileHandle() {
674 return implClassJavaFileHandle;
675 }
676
677 /**
678 * Sets the java file handle for impl class file.
679 *
680 * @param implClassJavaFileHandle java file handle
681 */
682 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
683 this.implClassJavaFileHandle = implClassJavaFileHandle;
684 }
685
686 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530687 * Returns attribute's temporary file handle.
688 *
689 * @return temporary file handle
690 */
691 public File getAttributesTempFileHandle() {
692 return attributesTempFileHandle;
693 }
694
695 /**
696 * Sets attribute's temporary file handle.
697 *
698 * @param attributeForClass file handle for attribute
699 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530700 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530701 attributesTempFileHandle = attributeForClass;
702 }
703
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530704 /**
705 * Returns getter method's impl's temporary file handle.
706 *
707 * @return temporary file handle
708 */
709 public File getGetterImplTempFileHandle() {
710 return getterImplTempFileHandle;
711 }
712
713 /**
714 * Sets to getter method's impl's temporary file handle.
715 *
716 * @param getterImpl file handle for to getter method's impl
717 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530718 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530719 getterImplTempFileHandle = getterImpl;
720 }
721
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530722 /**
723 * Returns hash code method's temporary file handle.
724 *
725 * @return temporary file handle
726 */
727 public File getHashCodeImplTempFileHandle() {
728 return hashCodeImplTempFileHandle;
729 }
730
731 /**
732 * Sets hash code method's temporary file handle.
733 *
734 * @param hashCodeMethod file handle for hash code method
735 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530736 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530737 hashCodeImplTempFileHandle = hashCodeMethod;
738 }
739
740 /**
741 * Returns equals mehtod's temporary file handle.
742 *
743 * @return temporary file handle
744 */
745 public File getEqualsImplTempFileHandle() {
746 return equalsImplTempFileHandle;
747 }
748
749 /**
750 * Sets equals method's temporary file handle.
751 *
752 * @param equalsMethod file handle for to equals method
753 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530754 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530755 equalsImplTempFileHandle = equalsMethod;
756 }
757
758 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530759 * Returns to string method's temporary file handle.
760 *
761 * @return temporary file handle
762 */
763 public File getToStringImplTempFileHandle() {
764 return toStringImplTempFileHandle;
765 }
766
767 /**
768 * Sets to string method's temporary file handle.
769 *
770 * @param toStringMethod file handle for to string method
771 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530772 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530773 toStringImplTempFileHandle = toStringMethod;
774 }
775
776 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530777 * Returns java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530778 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530779 * @return java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530780 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530781 public JavaExtendsListHolder getJavaExtendsListHolder() {
782 return javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530783 }
784
785 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530786 * Sets java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530787 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530788 * @param javaExtendsListHolder java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530789 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530790 public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
791 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530792 }
793
794 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530795 * Adds attribute for class.
796 *
797 * @param attr attribute info
798 * @throws IOException when fails to append to temporary file
799 */
800 private void addAttribute(JavaAttributeInfo attr)
801 throws IOException {
802 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
803 }
804
805 /**
806 * Adds getter for interface.
807 *
808 * @param attr attribute info
809 * @throws IOException when fails to append to temporary file
810 */
811 private void addGetterForInterface(JavaAttributeInfo attr)
812 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530813 appendToFile(getGetterInterfaceTempFileHandle(),
814 getGetterString(attr, getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530815 }
816
817 /**
818 * Adds setter for interface.
819 *
820 * @param attr attribute info
821 * @throws IOException when fails to append to temporary file
822 */
823 private void addSetterForInterface(JavaAttributeInfo attr)
824 throws IOException {
825 appendToFile(getSetterInterfaceTempFileHandle(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530826 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530827 }
828
829 /**
830 * Adds setter's implementation for class.
831 *
832 * @param attr attribute info
833 * @throws IOException when fails to append to temporary file
834 */
835 private void addSetterImpl(JavaAttributeInfo attr)
836 throws IOException {
837 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530838 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
839 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530840 NEW_LINE);
841 }
842
843 /**
844 * Adds getter method's impl for class.
845 *
846 * @param attr attribute info
847 * @throws IOException when fails to append to temporary file
848 */
849 private void addGetterImpl(JavaAttributeInfo attr)
850 throws IOException {
851 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
852 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
853 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
854 getGeneratedJavaFiles()) + NEW_LINE);
855 } else {
janani b4a6711a2016-05-17 13:12:22 +0530856 appendToFile(getGetterImplTempFileHandle(),
857 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false)
858 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530859 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530860 }
861
862 /**
863 * Adds build method for interface.
864 *
865 * @return build method for interface
866 * @throws IOException when fails to append to temporary file
867 */
868 String addBuildMethodForInterface()
869 throws IOException {
870 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName());
871 }
872
873 /**
874 * Adds build method's implementation for class.
875 *
876 * @return build method implementation for class
877 * @throws IOException when fails to append to temporary file
878 */
879 String addBuildMethodImpl()
880 throws IOException {
881 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
882 }
883
884 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530885 * Adds default constructor for class.
886 *
887 * @param modifier modifier for constructor.
888 * @param toAppend string which need to be appended with the class name
889 * @return default constructor for class
890 * @throws IOException when fails to append to file
891 */
892 String addDefaultConstructor(String modifier, String toAppend)
893 throws IOException {
894 return NEW_LINE + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier);
895 }
896
897 /**
898 * Adds default constructor for class.
899 *
900 * @return default constructor for class
901 * @throws IOException when fails to append to file
902 */
903 public String addOfMethod()
904 throws IOException {
905 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false)
906 + getOfMethod(getGeneratedJavaClassName(), null);
907 }
908
909 /**
910 * Adds hash code method for class.
911 *
912 * @param attr attribute info
913 * @throws IOException when fails to append to temporary file
914 */
915 private void addHashCodeMethod(JavaAttributeInfo attr)
916 throws IOException {
917 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
918 }
919
920 /**
921 * Adds equals method for class.
922 *
923 * @param attr attribute info
924 * @throws IOException when fails to append to temporary file
925 */
926 private void addEqualsMethod(JavaAttributeInfo attr)
927 throws IOException {
928 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
929 }
930
931 /**
932 * Adds ToString method for class.
933 *
934 * @param attr attribute info
935 * @throws IOException when fails to append to temporary file
936 */
937 private void addToStringMethod(JavaAttributeInfo attr)
938 throws IOException {
939 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
940 }
941
942 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530943 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530944 *
Bharat saraswalc0e04842016-05-12 13:16:57 +0530945 * @param javaAttributeInfo type attribute info
946 * @param fromStringAttributeInfo from string attribute info
947 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530948 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530949 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
950 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530951 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530952 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
953 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530954 }
955
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530956 /**
957 * Returns a temporary file handle for the specific file type.
958 *
959 * @param fileName file name
960 * @return temporary file handle
961 * @throws IOException when fails to create new file handle
962 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530963 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530964 throws IOException {
965 String path = getTempDirPath();
966 File dir = new File(path);
967 if (!dir.exists()) {
968 dir.mkdirs();
969 }
970 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
971 if (!file.exists()) {
972 file.createNewFile();
973 }
974 return file;
975 }
976
977 /**
978 * Returns a temporary file handle for the specific file type.
979 *
980 * @param fileName file name
981 * @return temporary file handle
982 * @throws IOException when fails to create new file handle
983 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530984 File getJavaFileHandle(String fileName) throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530985 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
986 }
987
988 /**
989 * Returns data from the temporary files.
990 *
991 * @param file temporary file handle
992 * @return stored data from temporary files
993 * @throws IOException when failed to get data from the given file
994 */
995 public String getTemporaryDataFromFileHandle(File file)
996 throws IOException {
997 String path = getTempDirPath();
998 if (new File(path + file.getName()).exists()) {
999 return readAppendFile(path + file.getName(), EMPTY_STRING);
1000 } else {
1001 throw new IOException("Unable to get data from the given "
1002 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1003 }
1004 }
1005
1006 /**
1007 * Returns temporary directory path.
1008 *
1009 * @return directory path
1010 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301011 String getTempDirPath() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301012 return getPackageDirPathFromJavaJPackage(getAbsoluteDirPath()) + SLASH + getGeneratedJavaClassName()
1013 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1014 }
1015
1016 /**
1017 * Parses attribute to get the attribute string.
1018 *
1019 * @param attr attribute info
1020 * @return attribute string
1021 */
1022 private String parseAttribute(JavaAttributeInfo attr) {
1023 /*
1024 * TODO: check if this utility needs to be called or move to the caller
1025 */
janani b4a6711a2016-05-17 13:12:22 +05301026 String attributeName = getCamelCase(attr.getAttributeName(), null);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301027 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301028 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1029 attr.getImportInfo().getClassInfo(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301030 attributeName, attr.isListAttr());
1031 } else {
1032 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1033 attr.isListAttr());
1034 }
1035 }
1036
1037 /**
1038 * Appends content to temporary file.
1039 *
1040 * @param file temporary file
1041 * @param data data to be appended
1042 * @throws IOException when fails to append to file
1043 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301044 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301045 throws IOException {
1046 try {
1047 insertDataIntoJavaFile(file, data);
1048 } catch (IOException ex) {
1049 throw new IOException("failed to write in temp file.");
1050 }
1051 }
1052
1053 /**
1054 * Adds current node info as and attribute to the parent generated file.
1055 *
1056 * @param curNode current node which needs to be added as an attribute in
1057 * the parent generated code
1058 * @param isList is list construct
1059 * @throws IOException IO operation exception
1060 */
1061 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
1062 boolean isList)
1063 throws IOException {
1064 YangNode parent = getParentNodeInGenCode(curNode);
1065 if (!(parent instanceof JavaCodeGenerator)) {
1066 throw new TranslatorException("missing parent node to contain current node info in generated file");
1067 }
1068 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1069 parent, isList);
1070 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1071 throw new TranslatorException("missing parent temp file handle");
1072 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301073 getNodesInterfaceFragmentFiles(parent)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301074 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1075 }
1076
1077 /**
1078 * Creates an attribute info object corresponding to a data model node and
1079 * return it.
1080 *
1081 * @param curNode current data model node for which the java code generation
1082 * is being handled
1083 * @param parentNode parent node in which the current node is an attribute
1084 * @param isListNode is the current added attribute needs to be a list
1085 * @return AttributeInfo attribute details required to add in temporary
1086 * files
1087 */
1088 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
1089 YangNode curNode, YangNode parentNode, boolean isListNode) {
1090 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
1091 /*
1092 * Get the import info corresponding to the attribute for import in
1093 * generated java files or qualified access
1094 */
1095 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
janani b4a6711a2016-05-17 13:12:22 +05301096 getCapitalCase(curNodeName));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301097 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
1098 throw new TranslatorException("Parent node does not have file info");
1099 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301100 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(parentNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +05301101 boolean isQualified = true;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301102 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301103 if (isListNode) {
1104 parentImportData.setIfListImported(true);
1105 }
1106 if (!detectCollisionBwParentAndChildForImport(curNode, qualifiedTypeInfo)) {
1107 parentImportData.addImportInfo(qualifiedTypeInfo);
1108 isQualified = false;
1109 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301110 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1111 }
1112
Bharat saraswalc0e04842016-05-12 13:16:57 +05301113 /**
1114 * Returns interface fragment files for node.
1115 *
1116 * @param node YANG node
1117 * @return interface fragment files for node
1118 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301119 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1120 TempJavaFragmentFiles tempJavaFragmentFiles;
1121 if (node instanceof RpcNotificationContainer) {
1122 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1123 .getTempJavaCodeFragmentFiles()
1124 .getServiceTempFiles();
1125 } else {
1126 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1127 .getTempJavaCodeFragmentFiles()
1128 .getBeanTempFiles();
1129 }
1130 return tempJavaFragmentFiles;
1131 }
1132
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301133 /**
1134 * Adds parent's info to current node import list.
1135 *
1136 * @param curNode current node for which import list needs to be updated
1137 */
1138 public void addParentInfoInCurNodeTempFile(YangNode curNode) {
1139 caseImportInfo = new JavaQualifiedTypeInfo();
1140 YangNode parent = getParentNodeInGenCode(curNode);
1141 if (!(parent instanceof JavaCodeGenerator)) {
1142 throw new TranslatorException("missing parent node to contain current node info in generated file");
1143 }
1144 if (!(curNode instanceof JavaFileInfoContainer)) {
1145 throw new TranslatorException("missing java file information to get the package details "
1146 + "of attribute corresponding to child node");
1147 }
janani b4a6711a2016-05-17 13:12:22 +05301148 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(), null)));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301149 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
1150 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1151 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo);
1152 }
1153
1154 /**
1155 * Adds leaf attributes in generated files.
1156 *
1157 * @param listOfLeaves list of YANG leaf
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301158 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301159 * @throws IOException IO operation fail
1160 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301161 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1162 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301163 throws IOException {
1164 if (listOfLeaves != null) {
1165 for (YangLeaf leaf : listOfLeaves) {
1166 if (!(leaf instanceof JavaLeafInfoContainer)) {
1167 throw new TranslatorException("Leaf does not have java information");
1168 }
1169 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
1170 javaLeaf.updateJavaQualifiedInfo();
1171 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1172 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301173 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1174 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301175 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1176 false);
1177 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1178 }
1179 }
1180 }
1181
1182 /**
1183 * Adds leaf list's attributes in generated files.
1184 *
1185 * @param listOfLeafList list of YANG leaves
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301186 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301187 * @throws IOException IO operation fail
1188 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301189 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301190 throws IOException {
1191 if (listOfLeafList != null) {
1192 for (YangLeafList leafList : listOfLeafList) {
1193 if (!(leafList instanceof JavaLeafInfoContainer)) {
1194 throw new TranslatorException("Leaf-list does not have java information");
1195 }
1196 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
1197 javaLeaf.updateJavaQualifiedInfo();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301198 getJavaImportData().setIfListImported(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301199 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1200 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301201 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1202 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301203 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1204 true);
1205 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1206 }
1207 }
1208 }
1209
1210 /**
1211 * Adds all the leaves in the current data model node as part of the
1212 * generated temporary file.
1213 *
1214 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301215 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301216 * @throws IOException IO operation fail
1217 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301218 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1219 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301220 throws IOException {
1221 if (!(curNode instanceof YangLeavesHolder)) {
1222 throw new TranslatorException("Data model node does not have any leaves");
1223 }
1224 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301225 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1226 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301227 }
1228
1229 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301230 * Adds the new attribute info to the target generated temporary files.
1231 *
1232 * @param newAttrInfo the attribute info that needs to be added to temporary
1233 * files
1234 * @throws IOException IO operation fail
1235 */
1236 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1237 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301238 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301239 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1240 addAttribute(newAttrInfo);
1241 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301242
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301243 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
1244 addGetterForInterface(newAttrInfo);
1245 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301246
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301247 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
1248 addSetterForInterface(newAttrInfo);
1249 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301250
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301251 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1252 addSetterImpl(newAttrInfo);
1253 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301254
1255 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1256 addGetterImpl(newAttrInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301257 }
1258 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1259 addHashCodeMethod(newAttrInfo);
1260 }
1261 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1262 addEqualsMethod(newAttrInfo);
1263 }
1264 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1265 addToStringMethod(newAttrInfo);
1266 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301267
1268 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1269 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo);
1270 /*
1271 * Create a new java attribute info with qualified information of
1272 * wrapper classes.
1273 */
1274 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1275 newAttrInfo.getAttributeName(),
1276 newAttrInfo.getAttributeType(),
1277 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1278
1279 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301280 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301281 }
1282
1283 /**
1284 * Returns java class name.
1285 *
1286 * @param suffix for the class name based on the file type
1287 * @return java class name
1288 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301289 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301290 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301291 }
1292
1293 /**
1294 * Returns the directory path.
1295 *
1296 * @return directory path
1297 */
1298 private String getDirPath() {
1299 return getJavaFileInfo().getPackageFilePath();
1300 }
1301
1302 /**
1303 * Constructs java code exit.
1304 *
1305 * @param fileType generated file type
1306 * @param curNode current YANG node
1307 * @throws IOException when fails to generate java files
1308 */
1309 public void generateJavaFile(int fileType, YangNode curNode)
1310 throws IOException {
1311 List<String> imports = new ArrayList<>();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301312 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301313 imports = getJavaImportData().getImports();
1314 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301315 createPackage(curNode);
1316
1317 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301318 * Generate java code.
1319 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301320 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1321 BUILDER_INTERFACE_MASK) != 0) {
1322 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301323 * Adds import for case.
1324 */
1325 if (curNode instanceof YangCase) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301326 List<String> importData =
1327 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1328 .getBeanTempFiles().getJavaImportData().getImports();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301329 for (String importInfo : importData) {
1330 if (!imports.contains(importInfo)) {
1331 imports.add(importInfo);
1332 }
1333 }
1334 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301335
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301336 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301337 * Create interface file.
1338 */
1339 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1340 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301341 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1342 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301343 * Create builder interface file.
1344 */
1345 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1346 setBuilderInterfaceJavaFileHandle(
1347 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1348 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301349 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1350 isAttributePresent()));
1351 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301352 * Append builder interface file to interface file and close it.
1353 */
1354 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1355 }
1356 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301357 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1358 addAugmentationHoldersImport(curNode, imports, false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301359 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301360 if (isAugmentedInfoExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301361 addAugmentedInfoImport(curNode, imports, false);
1362 }
1363 if (curNode instanceof YangCase) {
1364 removeCaseImport(imports);
1365 }
1366 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301367 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301368 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301369 addImportsToStringAndHasCodeMethods(curNode, imports);
1370 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301371 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301372 addAugmentedInfoImport(curNode, imports, true);
1373 addArrayListImport(curNode, imports, true);
1374 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301375 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301376 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301377 * Create builder class file.
1378 */
1379 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1380 setBuilderClassJavaFileHandle(
Bharat saraswalc0e04842016-05-12 13:16:57 +05301381 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode,
1382 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301383 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301384 * Create impl class file.
1385 */
1386 if ((fileType & IMPL_CLASS_MASK) != 0) {
1387 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1388 setImplClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301389 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent()));
1390 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301391 * Append impl class to builder class and close it.
1392 */
1393 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1394 }
1395 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301396 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1397 addAugmentedInfoImport(curNode, imports, false);
1398 addArrayListImport(curNode, imports, false);
1399 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301400 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301401 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301402 * Close all the file handles.
1403 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301404 freeTemporaryResources(false);
1405 }
1406
1407 /**
1408 * Adds imports for ToString and HashCodeMethod.
1409 *
1410 * @param curNode current YANG node
1411 * @param imports import list
1412 * @return import list
1413 */
1414 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1415 imports.add(getJavaImportData().getImportForHashAndEquals());
1416 imports.add(getJavaImportData().getImportForToString());
1417 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301418 }
1419
1420 /**
1421 * Removes case import info from import list.
1422 *
1423 * @param imports list of imports
1424 * @return import for class
1425 */
1426 private List<String> removeCaseImport(List<String> imports) {
1427 if (imports != null && caseImportInfo != null) {
1428 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1429 SEMI_COLAN + NEW_LINE;
1430 imports.remove(caseImport);
1431 }
1432 return imports;
1433 }
1434
1435 /**
1436 * Removes all temporary file handles.
1437 *
1438 * @param isErrorOccurred when translator fails to generate java files we
1439 * need to close all open file handles include temporary files
1440 * and java files.
1441 * @throws IOException when failed to delete the temporary files
1442 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301443 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301444 throws IOException {
1445 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301446 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301447 * Close all java file handles and when error occurs delete the files.
1448 */
1449 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1450 closeFile(getInterfaceJavaFileHandle(), isError);
1451 }
1452 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
1453 closeFile(getBuilderClassJavaFileHandle(), isError);
1454 }
1455 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1456 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1457 }
1458 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
1459 closeFile(getImplClassJavaFileHandle(), true);
1460 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301461
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301462 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301463 * Close all temporary file handles and delete the files.
1464 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301465 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1466 closeFile(getGetterImplTempFileHandle(), true);
1467 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301468 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1469 closeFile(getAttributesTempFileHandle(), true);
1470 }
1471 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1472 closeFile(getHashCodeImplTempFileHandle(), true);
1473 }
1474 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1475 closeFile(getToStringImplTempFileHandle(), true);
1476 }
1477 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1478 closeFile(getEqualsImplTempFileHandle(), true);
1479 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301480 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1481 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301482 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301483 }
1484
1485 /**
1486 * Returns if the attribute needs to be accessed in a qualified manner or
1487 * not, if it needs to be imported, then the same needs to be done.
1488 *
1489 * @param importInfo import info for the current attribute being added
1490 * @return status of the qualified access to the attribute
1491 */
1492 public boolean getIsQualifiedAccessOrAddToImportList(
1493 JavaQualifiedTypeInfo importInfo) {
1494 boolean isImportPkgEqualCurNodePkg;
1495 if (importInfo.getClassInfo().contentEquals(
1496 getGeneratedJavaClassName())) {
1497 /*
1498 * if the current class name is same as the attribute class name,
1499 * then the attribute must be accessed in a qualified manner.
1500 */
1501 return true;
1502 } else if (importInfo.getPkgInfo() != null) {
1503 /*
1504 * If the attribute type is having the package info, it is contender
1505 * for import list and also need to check if it needs to be a
1506 * qualified access.
1507 */
1508 isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(importInfo);
1509 if (!isImportPkgEqualCurNodePkg) {
1510 /*
1511 * If the package of the attribute added is not same as the
1512 * current class package, then it must either be imported for
1513 * access or it must be a qualified access.
1514 */
1515 boolean isImportAdded = getJavaImportData().addImportInfo(importInfo);
1516 if (!isImportAdded) {
1517 /*
1518 * If the attribute type info is not imported, then it must
1519 * be a qualified access.
1520 */
1521 return true;
1522 }
1523 }
1524 }
1525 return false;
1526 }
1527
1528 /**
1529 * Checks if the import info is same as the package of the current generated
1530 * java file.
1531 *
1532 * @param importInfo import info for an attribute
1533 * @return true if the import info is same as the current nodes package
1534 * false otherwise
1535 */
1536 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1537 return getJavaFileInfo().getPackage()
1538 .contentEquals(importInfo.getPkgInfo());
1539 }
1540}