blob: 03ace2a967536f17f208acc671d0f47c124e3397 [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;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053028import org.onosproject.yangutils.datamodel.YangList;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import org.onosproject.yangutils.datamodel.YangNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053030import org.onosproject.yangutils.translator.exception.TranslatorException;
31import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswalcad0e652016-05-26 23:48:38 +053032import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
Bharat saraswalcad0e652016-05-26 23:48:38 +053033import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
Bharat saraswalcad0e652016-05-26 23:48:38 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053035import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053036import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053037
Bharat saraswal96dfef02016-06-16 00:29:12 +053038import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
43import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
45import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
46import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
50import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
51import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
53import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
54import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
55import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053056import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053057import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053058import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
61import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
62import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053063import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
64import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053066import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
67import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
68import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswalcad0e652016-05-26 23:48:38 +053069import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
72import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswalc0e04842016-05-12 13:16:57 +053073import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053074import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
75import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
76import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
77import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053078import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
80import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
81import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053082import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
83import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
Bharat saraswal96dfef02016-06-16 00:29:12 +053084import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentationHoldersImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswal96dfef02016-06-16 00:29:12 +053087import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentationHolderExtended;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053088import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
Bharat saraswalc0e04842016-05-12 13:16:57 +053089import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.sortImports;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053090import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053091import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053092import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT;
93import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053094import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
95import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
96import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
97import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
98import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053099import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530100import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530102import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE;
103import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE_CARDINALITY;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530104import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530105import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530106import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530107import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswal96dfef02016-06-16 00:29:12 +0530109import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530110import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
111import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530112import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
113import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
114import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Bharat saraswal96dfef02016-06-16 00:29:12 +0530115import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530116
117/**
118 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530119 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530120 */
121public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530122
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530123 /**
124 * Information about the java files being generated.
125 */
126 private JavaFileInfo javaFileInfo;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530127
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530128 /**
129 * Imported class info.
130 */
131 private JavaImportData javaImportData;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530132
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 /**
134 * The variable which guides the types of temporary files generated using
135 * the temporary generated file types mask.
136 */
137 private int generatedTempFiles;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530138
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530139 /**
140 * Absolute path where the target java file needs to be generated.
141 */
142 private String absoluteDirPath;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530143
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530144 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530145 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530146 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530147 private JavaExtendsListHolder javaExtendsListHolder;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530148
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530149 /**
150 * File type extension for java classes.
151 */
152 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530153
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530154 /**
155 * File type extension for temporary classes.
156 */
157 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530158
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159 /**
160 * Folder suffix for temporary files folder.
161 */
162 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530163
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164 /**
165 * File name for getter method.
166 */
167 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530168
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 /**
170 * File name for setter method.
171 */
172 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530173
174 /**
175 * File name for getter method implementation.
176 */
177 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
178
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530179 /**
180 * File name for setter method implementation.
181 */
182 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530184 /**
185 * File name for attributes.
186 */
187 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530188
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 /**
190 * File name for to string method.
191 */
192 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530193
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530194 /**
195 * File name for hash code method.
196 */
197 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530198
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530199 /**
200 * File name for equals method.
201 */
202 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530203
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530204 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530205 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530207 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530208
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530209 /**
210 * File name for interface java file name suffix.
211 */
212 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530213
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530214 /**
215 * File name for builder interface file name suffix.
216 */
217 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530218
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530219 /**
220 * File name for builder class file name suffix.
221 */
222 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530223
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530224 /**
225 * File name for impl class file name suffix.
226 */
227 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530228
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530229 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530230 * Java file handle for interface file.
231 */
232 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530233
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530234 /**
235 * Java file handle for builder interface file.
236 */
237 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530238
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530239 /**
240 * Java file handle for builder class file.
241 */
242 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530243
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 /**
245 * Java file handle for impl class file.
246 */
247 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530248
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530249 /**
250 * Temporary file handle for attribute.
251 */
252 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530253
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530254 /**
255 * Temporary file handle for getter of interface.
256 */
257 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530258
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530259 /**
260 * Temporary file handle for setter of interface.
261 */
262 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530263
264 /**
265 * Temporary file handle for getter of class.
266 */
267 private File getterImplTempFileHandle;
268
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530269 /**
270 * Temporary file handle for setter of class.
271 */
272 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530273
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530274 /**
275 * Temporary file handle for hash code method of class.
276 */
277 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530278
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530279 /**
280 * Temporary file handle for equals method of class.
281 */
282 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530283
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530284 /**
285 * Temporary file handle for to string method of class.
286 */
287 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530288
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530289 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530290 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530291 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530292 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530293
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530294 /**
295 * Import info for case.
296 */
297 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530298
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530299 /**
300 * Is attribute added.
301 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530302 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530303
Bharat saraswalc0e04842016-05-12 13:16:57 +0530304 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530305 * Retrieves the absolute path where the file needs to be generated.
306 *
307 * @return absolute path where the file needs to be generated
308 */
309 private String getAbsoluteDirPath() {
310 return absoluteDirPath;
311 }
312
313 /**
314 * Sets absolute path where the file needs to be generated.
315 *
316 * @param absoluteDirPath absolute path where the file needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530317 * generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530318 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530319 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530320 this.absoluteDirPath = absoluteDirPath;
321 }
322
323 /**
324 * Sets the generated java file information.
325 *
326 * @param javaFileInfo generated java file information
327 */
328 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
329 this.javaFileInfo = javaFileInfo;
330 }
331
332 /**
333 * Retrieves the generated java file information.
334 *
335 * @return generated java file information
336 */
337 public JavaFileInfo getJavaFileInfo() {
338 return javaFileInfo;
339 }
340
341 /**
342 * Retrieves the generated temp files.
343 *
344 * @return generated temp files
345 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530346 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530347 return generatedTempFiles;
348 }
349
350 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530351 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530352 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530353 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530354 generatedTempFiles = 0;
355 }
356
357 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530358 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530359 *
360 * @param generatedTempFile generated file
361 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530362 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530363 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530364 setGeneratedTempFiles(generatedTempFiles);
365 }
366
367 /**
368 * Sets generated file files.
369 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530370 * @param fileType generated file type
Bharat saraswalc0e04842016-05-12 13:16:57 +0530371 */
372 void setGeneratedTempFiles(int fileType) {
373 generatedTempFiles = fileType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530374 }
375
376 /**
377 * Retrieves the generated Java files.
378 *
379 * @return generated Java files
380 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530381 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530382 return getJavaFileInfo().getGeneratedFileTypes();
383 }
384
385 /**
386 * Retrieves the mapped Java class name.
387 *
388 * @return mapped Java class name
389 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530390 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530391 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530392 }
393
394 /**
395 * Retrieves the import data for the generated Java file.
396 *
397 * @return import data for the generated Java file
398 */
399 public JavaImportData getJavaImportData() {
400 return javaImportData;
401 }
402
403 /**
404 * Sets import data for the generated Java file.
405 *
406 * @param javaImportData import data for the generated Java file
407 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530408 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530409 this.javaImportData = javaImportData;
410 }
411
412 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530413 * Retrieves the status of any attributes added.
414 *
415 * @return status of any attributes added
416 */
417 public boolean isAttributePresent() {
418 return isAttributePresent;
419 }
420
421 /**
422 * Sets status of any attributes added.
423 *
424 * @param attributePresent status of any attributes added
425 */
426 public void setAttributePresent(boolean attributePresent) {
427 isAttributePresent = attributePresent;
428 }
429
430 /**
431 * Returns getter methods's temporary file handle.
432 *
433 * @return temporary file handle
434 */
435 public File getGetterInterfaceTempFileHandle() {
436 return getterInterfaceTempFileHandle;
437 }
438
439 /**
440 * Sets to getter method's temporary file handle.
441 *
442 * @param getterForInterface file handle for to getter method
443 */
444 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
445 getterInterfaceTempFileHandle = getterForInterface;
446 }
447
448 /**
449 * Returns setter method's temporary file handle.
450 *
451 * @return temporary file handle
452 */
453 public File getSetterInterfaceTempFileHandle() {
454 return setterInterfaceTempFileHandle;
455 }
456
457 /**
458 * Sets to setter method's temporary file handle.
459 *
460 * @param setterForInterface file handle for to setter method
461 */
462 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
463 setterInterfaceTempFileHandle = setterForInterface;
464 }
465
466 /**
467 * Returns setter method's impl's temporary file handle.
468 *
469 * @return temporary file handle
470 */
471 public File getSetterImplTempFileHandle() {
472 return setterImplTempFileHandle;
473 }
474
475 /**
476 * Sets to setter method's impl's temporary file handle.
477 *
478 * @param setterImpl file handle for to setter method's implementation class
479 */
480 private void setSetterImplTempFileHandle(File setterImpl) {
481 setterImplTempFileHandle = setterImpl;
482 }
483
484 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530485 * Returns from string method's temporary file handle.
486 *
487 * @return from string method's temporary file handle
488 */
489 public File getFromStringImplTempFileHandle() {
490 return fromStringImplTempFileHandle;
491 }
492
493 /**
494 * Sets from string method's temporary file handle.
495 *
496 * @param fromStringImplTempFileHandle from string method's temporary file
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530497 * handle
Bharat saraswalc0e04842016-05-12 13:16:57 +0530498 */
499 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
500 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
501 }
502
503 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530504 * Creates an instance of temporary java code fragment.
505 *
506 * @param javaFileInfo generated java file information
507 * @throws IOException when fails to create new file handle
508 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530509 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
510 throws IOException {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530511 setJavaExtendsListHolder(new JavaExtendsListHolder());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530512 setJavaImportData(new JavaImportData());
513 setJavaFileInfo(javaFileInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530514 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
515 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530516
517 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530518 * Initialize getter when generation file type matches to interface
519 * mask.
520 */
521 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
522 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
523 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530524
525 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530526 * Initialize getter and setter when generation file type matches to
527 * builder interface mask.
528 */
529 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
530 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
531 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
532 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530533
534 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530535 * Initialize getterImpl, setterImpl and attributes when generation file
536 * type matches to builder class mask.
537 */
538 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
539 addGeneratedTempFile(ATTRIBUTES_MASK);
540 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
541 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
542 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530543
544 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530545 * Initialize getterImpl, attributes, constructor, hash code, equals and
546 * to strings when generation file type matches to impl class mask.
547 */
548 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
549 addGeneratedTempFile(ATTRIBUTES_MASK);
550 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530551 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
552 addGeneratedTempFile(EQUALS_IMPL_MASK);
553 addGeneratedTempFile(TO_STRING_IMPL_MASK);
554 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530555
556 /*
557 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530558 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530559 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530560 addGeneratedTempFile(ATTRIBUTES_MASK);
561 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
562 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
563 addGeneratedTempFile(EQUALS_IMPL_MASK);
564 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530565 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530566 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530567
568 /*
Bharat saraswalc0e04842016-05-12 13:16:57 +0530569 * Initialize temp files to generate enum class.
570 */
571 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
572 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
573 }
574 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530575 * Initialize getter and setter when generation file type matches to
576 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530577 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530578 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
579 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
580 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530581 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530582 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530583 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530584
585 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530586 * Set temporary file handles.
587 */
588 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
589 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
590 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530591
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530592 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
593 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
594 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530595
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530596 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
597 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
598 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530599
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530600 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
601 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
602 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530603
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530604 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
605 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
606 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530607
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530608 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
609 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
610 }
611 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
612 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
613 }
614 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
615 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
616 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530617 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
618 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530619 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530620
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530621 }
622
623 /**
624 * Returns java file handle for interface file.
625 *
626 * @return java file handle for interface file
627 */
628 private File getInterfaceJavaFileHandle() {
629 return interfaceJavaFileHandle;
630 }
631
632 /**
633 * Sets the java file handle for interface file.
634 *
635 * @param interfaceJavaFileHandle java file handle
636 */
637 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
638 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
639 }
640
641 /**
642 * Returns java file handle for builder interface file.
643 *
644 * @return java file handle for builder interface file
645 */
646 private File getBuilderInterfaceJavaFileHandle() {
647 return builderInterfaceJavaFileHandle;
648 }
649
650 /**
651 * Sets the java file handle for builder interface file.
652 *
653 * @param builderInterfaceJavaFileHandle java file handle
654 */
655 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
656 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
657 }
658
659 /**
660 * Returns java file handle for builder class file.
661 *
662 * @return java file handle for builder class file
663 */
664 private File getBuilderClassJavaFileHandle() {
665 return builderClassJavaFileHandle;
666 }
667
668 /**
669 * Sets the java file handle for builder class file.
670 *
671 * @param builderClassJavaFileHandle java file handle
672 */
673 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
674 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
675 }
676
677 /**
678 * Returns java file handle for impl class file.
679 *
680 * @return java file handle for impl class file
681 */
682 private File getImplClassJavaFileHandle() {
683 return implClassJavaFileHandle;
684 }
685
686 /**
687 * Sets the java file handle for impl class file.
688 *
689 * @param implClassJavaFileHandle java file handle
690 */
691 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
692 this.implClassJavaFileHandle = implClassJavaFileHandle;
693 }
694
695 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530696 * Returns attribute's temporary file handle.
697 *
698 * @return temporary file handle
699 */
700 public File getAttributesTempFileHandle() {
701 return attributesTempFileHandle;
702 }
703
704 /**
705 * Sets attribute's temporary file handle.
706 *
707 * @param attributeForClass file handle for attribute
708 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530709 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530710 attributesTempFileHandle = attributeForClass;
711 }
712
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530713 /**
714 * Returns getter method's impl's temporary file handle.
715 *
716 * @return temporary file handle
717 */
718 public File getGetterImplTempFileHandle() {
719 return getterImplTempFileHandle;
720 }
721
722 /**
723 * Sets to getter method's impl's temporary file handle.
724 *
725 * @param getterImpl file handle for to getter method's impl
726 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530727 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530728 getterImplTempFileHandle = getterImpl;
729 }
730
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530731 /**
732 * Returns hash code method's temporary file handle.
733 *
734 * @return temporary file handle
735 */
736 public File getHashCodeImplTempFileHandle() {
737 return hashCodeImplTempFileHandle;
738 }
739
740 /**
741 * Sets hash code method's temporary file handle.
742 *
743 * @param hashCodeMethod file handle for hash code method
744 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530745 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530746 hashCodeImplTempFileHandle = hashCodeMethod;
747 }
748
749 /**
750 * Returns equals mehtod's temporary file handle.
751 *
752 * @return temporary file handle
753 */
754 public File getEqualsImplTempFileHandle() {
755 return equalsImplTempFileHandle;
756 }
757
758 /**
759 * Sets equals method's temporary file handle.
760 *
761 * @param equalsMethod file handle for to equals method
762 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530763 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530764 equalsImplTempFileHandle = equalsMethod;
765 }
766
767 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530768 * Returns to string method's temporary file handle.
769 *
770 * @return temporary file handle
771 */
772 public File getToStringImplTempFileHandle() {
773 return toStringImplTempFileHandle;
774 }
775
776 /**
777 * Sets to string method's temporary file handle.
778 *
779 * @param toStringMethod file handle for to string method
780 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530781 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530782 toStringImplTempFileHandle = toStringMethod;
783 }
784
785 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530786 * Returns java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530787 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530788 * @return java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530789 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530790 public JavaExtendsListHolder getJavaExtendsListHolder() {
791 return javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530792 }
793
794 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530795 * Sets java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530796 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530797 * @param javaExtendsListHolder java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530798 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530799 public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
800 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530801 }
802
803 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530804 * Adds attribute for class.
805 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530806 * @param attr attribute info
807 * @param yangPluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530808 * @throws IOException when fails to append to temporary file
809 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530810 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530811 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530812 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
813 + FOUR_SPACE_INDENTATION);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530814 }
815
816 /**
817 * Adds getter for interface.
818 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530819 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530820 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530821 * @throws IOException when fails to append to temporary file
822 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530823 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530824 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530825 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530826 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530827 }
828
829 /**
830 * Adds setter for interface.
831 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530832 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530833 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530834 * @throws IOException when fails to append to temporary file
835 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530836 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530837 throws IOException {
838 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530839 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
840 + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530841 }
842
843 /**
844 * Adds setter's implementation for class.
845 *
846 * @param attr attribute info
847 * @throws IOException when fails to append to temporary file
848 */
849 private void addSetterImpl(JavaAttributeInfo attr)
850 throws IOException {
851 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530852 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
853 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530854 NEW_LINE);
855 }
856
857 /**
858 * Adds getter method's impl for class.
859 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530860 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530861 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530862 * @throws IOException when fails to append to temporary file
863 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530864 private void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530865 throws IOException {
866 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
867 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
868 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
869 getGeneratedJavaFiles()) + NEW_LINE);
870 } else {
janani b4a6711a2016-05-17 13:12:22 +0530871 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530872 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b4a6711a2016-05-17 13:12:22 +0530873 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530874 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530875 }
876
877 /**
878 * Adds build method for interface.
879 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530880 * @param pluginConfig plugin configurations
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530881 * @return build method for interface
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530882 * @throws IOException when fails to append to temporary file
883 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530884 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530885 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530886 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530887 }
888
889 /**
890 * Adds build method's implementation for class.
891 *
892 * @return build method implementation for class
893 * @throws IOException when fails to append to temporary file
894 */
895 String addBuildMethodImpl()
896 throws IOException {
897 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
898 }
899
900 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530901 * Adds default constructor for class.
902 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530903 * @param modifier modifier for constructor.
904 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530905 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530906 * @return default constructor for class
907 * @throws IOException when fails to append to file
908 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530909 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530910 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530911 return NEW_LINE
912 + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530913 }
914
915 /**
916 * Adds default constructor for class.
917 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530918 * @param pluginCnfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530919 * @return default constructor for class
920 * @throws IOException when fails to append to file
921 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530922 public String addOfMethod(YangPluginConfig pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530923 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530924 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false, pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530925 + getOfMethod(getGeneratedJavaClassName(), null);
926 }
927
928 /**
929 * Adds hash code method for class.
930 *
931 * @param attr attribute info
932 * @throws IOException when fails to append to temporary file
933 */
934 private void addHashCodeMethod(JavaAttributeInfo attr)
935 throws IOException {
936 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
937 }
938
939 /**
940 * Adds equals method for class.
941 *
942 * @param attr attribute info
943 * @throws IOException when fails to append to temporary file
944 */
945 private void addEqualsMethod(JavaAttributeInfo attr)
946 throws IOException {
947 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
948 }
949
950 /**
951 * Adds ToString method for class.
952 *
953 * @param attr attribute info
954 * @throws IOException when fails to append to temporary file
955 */
956 private void addToStringMethod(JavaAttributeInfo attr)
957 throws IOException {
958 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
959 }
960
961 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530962 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530963 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530964 * @param javaAttributeInfo type attribute info
Bharat saraswalc0e04842016-05-12 13:16:57 +0530965 * @param fromStringAttributeInfo from string attribute info
966 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530967 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530968 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
969 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530970 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530971 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
972 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530973 }
974
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530975 /**
976 * Returns a temporary file handle for the specific file type.
977 *
978 * @param fileName file name
979 * @return temporary file handle
980 * @throws IOException when fails to create new file handle
981 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530982 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530983 throws IOException {
984 String path = getTempDirPath();
985 File dir = new File(path);
986 if (!dir.exists()) {
987 dir.mkdirs();
988 }
989 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
990 if (!file.exists()) {
991 file.createNewFile();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530992 } else {
993 throw new IOException(fileName + " is reused due to YANG naming");
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530994 }
995 return file;
996 }
997
998 /**
999 * Returns a temporary file handle for the specific file type.
1000 *
1001 * @param fileName file name
1002 * @return temporary file handle
1003 * @throws IOException when fails to create new file handle
1004 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301005 File getJavaFileHandle(String fileName)
1006 throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301007 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1008 }
1009
1010 /**
1011 * Returns data from the temporary files.
1012 *
1013 * @param file temporary file handle
1014 * @return stored data from temporary files
1015 * @throws IOException when failed to get data from the given file
1016 */
1017 public String getTemporaryDataFromFileHandle(File file)
1018 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301019
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301020 String path = getTempDirPath();
1021 if (new File(path + file.getName()).exists()) {
1022 return readAppendFile(path + file.getName(), EMPTY_STRING);
1023 } else {
1024 throw new IOException("Unable to get data from the given "
1025 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1026 }
1027 }
1028
1029 /**
1030 * Returns temporary directory path.
1031 *
1032 * @return directory path
1033 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301034 String getTempDirPath() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301035 return getPackageDirPathFromJavaJPackage(getAbsoluteDirPath()) + SLASH + getGeneratedJavaClassName()
1036 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1037 }
1038
1039 /**
1040 * Parses attribute to get the attribute string.
1041 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301042 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301043 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301044 * @return attribute string
1045 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301046 public String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301047 /*
1048 * TODO: check if this utility needs to be called or move to the caller
1049 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301050 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301051 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301052 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1053 attr.getImportInfo().getClassInfo(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301054 attributeName, attr.isListAttr());
1055 } else {
1056 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1057 attr.isListAttr());
1058 }
1059 }
1060
1061 /**
1062 * Appends content to temporary file.
1063 *
1064 * @param file temporary file
1065 * @param data data to be appended
1066 * @throws IOException when fails to append to file
1067 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301068 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301069 throws IOException {
1070 try {
1071 insertDataIntoJavaFile(file, data);
1072 } catch (IOException ex) {
1073 throw new IOException("failed to write in temp file.");
1074 }
1075 }
1076
1077 /**
1078 * Adds current node info as and attribute to the parent generated file.
1079 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301080 * @param curNode current node which needs to be added as an attribute in
1081 * the parent generated code
1082 * @param isList is list construct
Bharat saraswal33dfa012016-05-17 19:59:16 +05301083 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301084 * @throws IOException IO operation exception
1085 */
1086 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
Bharat saraswal33dfa012016-05-17 19:59:16 +05301087 boolean isList, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301088 throws IOException {
1089 YangNode parent = getParentNodeInGenCode(curNode);
1090 if (!(parent instanceof JavaCodeGenerator)) {
1091 throw new TranslatorException("missing parent node to contain current node info in generated file");
1092 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301093
1094 if (parent instanceof YangJavaGrouping) {
1095 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +05301096 * In case of grouping, there is no need to add the information, it
1097 * will be taken care in uses
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301098 */
1099 return;
Bharat saraswalcad0e652016-05-26 23:48:38 +05301100 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301101
1102 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301103 parent, isList);
1104 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1105 throw new TranslatorException("missing parent temp file handle");
1106 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301107 getNodesInterfaceFragmentFiles(parent)
Bharat saraswal33dfa012016-05-17 19:59:16 +05301108 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301109 }
1110
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301111 /**
1112 * Adds current node info as and attribute to the parent generated file.
1113 *
1114 * @param curNode current node which needs to be added as an attribute in
1115 * the parent generated code
1116 * @param pluginConfig plugin configurations
1117 * @param targetNode target node to add the attribute
1118 * @throws IOException IO operation exception
1119 */
1120 public static void addCurNodeAsAttributeInTargetTempFile(YangNode curNode,
1121 YangPluginConfig pluginConfig, YangNode targetNode)
1122 throws IOException {
1123
1124 if (!(targetNode instanceof JavaCodeGenerator)) {
1125 throw new TranslatorException("invalid target node to generated file");
1126 }
1127
1128 if (targetNode instanceof YangJavaGrouping) {
1129 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +05301130 * In case of grouping, there is no need to add the information, it
1131 * will be taken care in uses
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301132 */
1133 return;
1134 }
1135
1136 boolean isList = curNode instanceof YangList;
1137
1138 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
1139 targetNode, isList);
1140 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
1141 throw new TranslatorException("missing target node's temp file handle");
1142 }
1143 getNodesInterfaceFragmentFiles(targetNode)
1144 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
1145 }
1146
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301147 /**
1148 * Creates an attribute info object corresponding to a data model node and
1149 * return it.
1150 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301151 * @param curNode current data model node for which the java code generation
1152 * is being handled
1153 * @param targetNode target node in which the current node is an attribute
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301154 * @param isListNode is the current added attribute needs to be a list
1155 * @return AttributeInfo attribute details required to add in temporary
1156 * files
1157 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301158 public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode,
1159 YangNode targetNode, boolean isListNode) {
1160 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
1161 if (curNodeName == null) {
1162 updateJavaFileInfo(curNode, null);
1163 curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301164 }
Bharat saraswalcad0e652016-05-26 23:48:38 +05301165
1166 /*
1167 * Get the import info corresponding to the attribute for import in
1168 * generated java files or qualified access
1169 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301170 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
1171 getCapitalCase(curNodeName));
1172 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
1173 throw new TranslatorException("Parent node does not have file info");
1174 }
1175 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(targetNode);
1176 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
1177 JavaFileInfo fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
Bharat saraswalcad0e652016-05-26 23:48:38 +05301178
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301179 boolean isQualified;
1180 if ((targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule)
1181 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE)
Bharat saraswal96dfef02016-06-16 00:29:12 +05301182 || qualifiedTypeInfo.getClassInfo().contentEquals(COMPONENT)
1183 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE))
1184 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(DEACTIVATE))
1185 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE_CARDINALITY)
1186 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE))
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301187 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))
1188 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + MANAGER))) {
1189
1190 isQualified = true;
Bharat saraswalcad0e652016-05-26 23:48:38 +05301191 } else {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301192 String className;
1193 if (targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule) {
1194 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
1195 } else {
1196 className = getCapitalCase(fileInfo.getJavaName());
1197 }
1198
1199 isQualified = parentImportData.addImportInfo(qualifiedTypeInfo,
1200 className, fileInfo.getPackage());
Bharat saraswalcad0e652016-05-26 23:48:38 +05301201 }
1202
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301203 if (isListNode) {
1204 parentImportData.setIfListImported(true);
Bharat saraswalc0e04842016-05-12 13:16:57 +05301205 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301206
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301207 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1208 }
1209
Bharat saraswalc0e04842016-05-12 13:16:57 +05301210 /**
Bharat saraswalcad0e652016-05-26 23:48:38 +05301211 * Resolves groupings java qualified info.
1212 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301213 * @param curNode grouping node
Bharat saraswalcad0e652016-05-26 23:48:38 +05301214 * @param pluginConfig plugin configurations
1215 * @return groupings java qualified info
1216 */
1217 public static JavaQualifiedTypeInfo resolveGroupingsQuailifiedInfo(YangNode curNode,
1218 YangPluginConfig pluginConfig) {
1219
1220 JavaFileInfo groupingFileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1221 JavaQualifiedTypeInfo qualifiedTypeInfo = new JavaQualifiedTypeInfo();
1222 if (groupingFileInfo.getPackage() == null) {
1223 List<String> parentNames = new ArrayList<>();
1224
1225 YangNode tempNode = curNode.getParent();
1226 YangNode groupingSuperParent = null;
1227 while (tempNode != null) {
1228 parentNames.add(tempNode.getName());
1229 groupingSuperParent = tempNode;
1230 tempNode = tempNode.getParent();
1231 }
1232
1233 String pkg = null;
1234 JavaFileInfo parentInfo = ((JavaFileInfoContainer) groupingSuperParent).getJavaFileInfo();
1235 if (parentInfo.getPackage() == null) {
1236 if (groupingSuperParent instanceof YangJavaModule) {
1237 YangJavaModule module = (YangJavaModule) groupingSuperParent;
1238 String modulePkg = getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
1239 .getRevision().getRevDate(), pluginConfig.getConflictResolver());
1240 pkg = modulePkg;
1241 } else if (groupingSuperParent instanceof YangJavaSubModule) {
1242 YangJavaSubModule submodule = (YangJavaSubModule) groupingSuperParent;
1243 String subModulePkg = getRootPackage(submodule.getVersion(),
1244 submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
1245 submodule.getRevision().getRevDate(), pluginConfig.getConflictResolver());
1246 pkg = subModulePkg;
1247 }
1248 } else {
1249 pkg = parentInfo.getPackage();
1250 }
1251 for (String name : parentNames) {
1252 pkg = pkg + PERIOD + getCamelCase(name, pluginConfig.getConflictResolver());
1253 }
1254
1255 qualifiedTypeInfo.setPkgInfo(pkg.toLowerCase());
1256 qualifiedTypeInfo.setClassInfo(
1257 getCapitalCase(getCamelCase(curNode.getName(), pluginConfig.getConflictResolver())));
1258 return qualifiedTypeInfo;
1259
1260 } else {
1261 qualifiedTypeInfo.setPkgInfo(groupingFileInfo.getPackage().toLowerCase());
1262 qualifiedTypeInfo.setClassInfo(getCapitalCase(groupingFileInfo.getJavaName()));
1263 return qualifiedTypeInfo;
1264 }
1265 }
1266
1267 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +05301268 * Returns interface fragment files for node.
1269 *
1270 * @param node YANG node
1271 * @return interface fragment files for node
1272 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301273 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1274 TempJavaFragmentFiles tempJavaFragmentFiles;
1275 if (node instanceof RpcNotificationContainer) {
1276 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1277 .getTempJavaCodeFragmentFiles()
1278 .getServiceTempFiles();
1279 } else {
1280 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1281 .getTempJavaCodeFragmentFiles()
1282 .getBeanTempFiles();
1283 }
1284 return tempJavaFragmentFiles;
1285 }
1286
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301287 /**
1288 * Adds parent's info to current node import list.
1289 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301290 * @param curNode current node for which import list needs to be updated
Bharat saraswal33dfa012016-05-17 19:59:16 +05301291 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301292 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301293 public void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301294 caseImportInfo = new JavaQualifiedTypeInfo();
1295 YangNode parent = getParentNodeInGenCode(curNode);
1296 if (!(parent instanceof JavaCodeGenerator)) {
1297 throw new TranslatorException("missing parent node to contain current node info in generated file");
1298 }
1299 if (!(curNode instanceof JavaFileInfoContainer)) {
1300 throw new TranslatorException("missing java file information to get the package details "
1301 + "of attribute corresponding to child node");
1302 }
Bharat saraswal33dfa012016-05-17 19:59:16 +05301303 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1304 pluginConfig.getConflictResolver())));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301305 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301306
1307 JavaFileInfo fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1308
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301309 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301310 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo,
Bharat saraswal96dfef02016-06-16 00:29:12 +05301311 getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301312 }
1313
1314 /**
1315 * Adds leaf attributes in generated files.
1316 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301317 * @param listOfLeaves list of YANG leaf
1318 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301319 * @throws IOException IO operation fail
1320 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301321 public void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301322 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301323 throws IOException {
1324 if (listOfLeaves != null) {
1325 for (YangLeaf leaf : listOfLeaves) {
1326 if (!(leaf instanceof JavaLeafInfoContainer)) {
1327 throw new TranslatorException("Leaf does not have java information");
1328 }
1329 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
Bharat saraswal33dfa012016-05-17 19:59:16 +05301330 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301331 javaLeaf.updateJavaQualifiedInfo();
1332 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1333 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301334 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1335 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301336 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1337 false);
Bharat saraswal33dfa012016-05-17 19:59:16 +05301338 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301339 }
1340 }
1341 }
1342
1343 /**
1344 * Adds leaf list's attributes in generated files.
1345 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301346 * @param listOfLeafList list of YANG leaves
1347 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301348 * @throws IOException IO operation fail
1349 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301350 public void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301351 throws IOException {
1352 if (listOfLeafList != null) {
1353 for (YangLeafList leafList : listOfLeafList) {
1354 if (!(leafList instanceof JavaLeafInfoContainer)) {
1355 throw new TranslatorException("Leaf-list does not have java information");
1356 }
1357 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
Bharat saraswal33dfa012016-05-17 19:59:16 +05301358 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301359 javaLeaf.updateJavaQualifiedInfo();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301360 getJavaImportData().setIfListImported(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301361 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1362 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301363 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1364 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301365 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1366 true);
Bharat saraswal33dfa012016-05-17 19:59:16 +05301367 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301368 }
1369 }
1370 }
1371
1372 /**
1373 * Adds all the leaves in the current data model node as part of the
1374 * generated temporary file.
1375 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301376 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301377 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301378 * @throws IOException IO operation fail
1379 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301380 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1381 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301382 throws IOException {
1383 if (!(curNode instanceof YangLeavesHolder)) {
1384 throw new TranslatorException("Data model node does not have any leaves");
1385 }
1386 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301387 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1388 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301389 }
1390
1391 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301392 * Adds the new attribute info to the target generated temporary files.
1393 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301394 * @param newAttrInfo the attribute info that needs to be added to temporary
1395 * files
Bharat saraswal33dfa012016-05-17 19:59:16 +05301396 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301397 * @throws IOException IO operation fail
1398 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301399 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301400 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301401 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301402 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301403 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301404 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301405
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301406 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301407 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301408 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301409
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301410 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301411 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301412 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301413
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301414 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1415 addSetterImpl(newAttrInfo);
1416 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301417
1418 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301419 addGetterImpl(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301420 }
1421 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1422 addHashCodeMethod(newAttrInfo);
1423 }
1424 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1425 addEqualsMethod(newAttrInfo);
1426 }
1427 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1428 addToStringMethod(newAttrInfo);
1429 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301430
1431 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Bharat saraswal96dfef02016-06-16 00:29:12 +05301432 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
1433 pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +05301434 /*
1435 * Create a new java attribute info with qualified information of
1436 * wrapper classes.
1437 */
1438 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1439 newAttrInfo.getAttributeName(),
1440 newAttrInfo.getAttributeType(),
1441 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1442
1443 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301444 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301445 }
1446
1447 /**
1448 * Returns java class name.
1449 *
1450 * @param suffix for the class name based on the file type
1451 * @return java class name
1452 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301453 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301454 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301455 }
1456
1457 /**
1458 * Returns the directory path.
1459 *
1460 * @return directory path
1461 */
1462 private String getDirPath() {
1463 return getJavaFileInfo().getPackageFilePath();
1464 }
1465
1466 /**
1467 * Constructs java code exit.
1468 *
1469 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301470 * @param curNode current YANG node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301471 * @throws IOException when fails to generate java files
1472 */
1473 public void generateJavaFile(int fileType, YangNode curNode)
1474 throws IOException {
1475 List<String> imports = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +05301476 imports = getJavaImportData().getImports();
1477
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301478 createPackage(curNode);
1479
1480 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301481 * Generate java code.
1482 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301483 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1484 BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301485
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301486 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301487 * Create interface file.
1488 */
1489 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1490 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301491 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1492 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301493 * Create builder interface file.
1494 */
1495 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1496 setBuilderInterfaceJavaFileHandle(
1497 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1498 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301499 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1500 isAttributePresent()));
1501 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301502 * Append builder interface file to interface file and close it.
1503 */
1504 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
Bharat saraswal96dfef02016-06-16 00:29:12 +05301505 validateLineLength(getInterfaceJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301506 }
1507 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301508 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1509 addAugmentationHoldersImport(curNode, imports, false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301510 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301511 if (isAugmentedInfoExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301512 addAugmentedInfoImport(curNode, imports, false);
1513 }
1514 if (curNode instanceof YangCase) {
1515 removeCaseImport(imports);
1516 }
1517 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301518 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301519 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301520 addImportsToStringAndHasCodeMethods(curNode, imports);
1521 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301522 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301523 addAugmentedInfoImport(curNode, imports, true);
1524 addArrayListImport(curNode, imports, true);
1525 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301526 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301527 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301528 * Create builder class file.
1529 */
1530 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1531 setBuilderClassJavaFileHandle(
Bharat saraswalc0e04842016-05-12 13:16:57 +05301532 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode,
1533 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301534 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301535 * Create impl class file.
1536 */
1537 if ((fileType & IMPL_CLASS_MASK) != 0) {
1538 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1539 setImplClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301540 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent()));
1541 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301542 * Append impl class to builder class and close it.
1543 */
1544 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
Bharat saraswal96dfef02016-06-16 00:29:12 +05301545 validateLineLength(getBuilderClassJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301546 }
1547 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301548 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1549 addAugmentedInfoImport(curNode, imports, false);
1550 addArrayListImport(curNode, imports, false);
1551 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301552 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301553 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301554 * Close all the file handles.
1555 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301556 freeTemporaryResources(false);
1557 }
1558
1559 /**
1560 * Adds imports for ToString and HashCodeMethod.
1561 *
1562 * @param curNode current YANG node
1563 * @param imports import list
1564 * @return import list
1565 */
1566 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1567 imports.add(getJavaImportData().getImportForHashAndEquals());
1568 imports.add(getJavaImportData().getImportForToString());
1569 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301570 }
1571
1572 /**
1573 * Removes case import info from import list.
1574 *
1575 * @param imports list of imports
1576 * @return import for class
1577 */
1578 private List<String> removeCaseImport(List<String> imports) {
1579 if (imports != null && caseImportInfo != null) {
1580 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1581 SEMI_COLAN + NEW_LINE;
1582 imports.remove(caseImport);
1583 }
1584 return imports;
1585 }
1586
1587 /**
1588 * Removes all temporary file handles.
1589 *
1590 * @param isErrorOccurred when translator fails to generate java files we
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301591 * need to close all open file handles include temporary files
1592 * and java files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301593 * @throws IOException when failed to delete the temporary files
1594 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301595 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301596 throws IOException {
1597 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301598 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301599 * Close all java file handles and when error occurs delete the files.
1600 */
1601 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1602 closeFile(getInterfaceJavaFileHandle(), isError);
1603 }
1604 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
1605 closeFile(getBuilderClassJavaFileHandle(), isError);
1606 }
1607 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1608 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1609 }
1610 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
1611 closeFile(getImplClassJavaFileHandle(), true);
1612 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301613
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301614 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301615 * Close all temporary file handles and delete the files.
1616 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301617 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1618 closeFile(getGetterImplTempFileHandle(), true);
1619 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301620 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1621 closeFile(getAttributesTempFileHandle(), true);
1622 }
1623 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1624 closeFile(getHashCodeImplTempFileHandle(), true);
1625 }
1626 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1627 closeFile(getToStringImplTempFileHandle(), true);
1628 }
1629 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1630 closeFile(getEqualsImplTempFileHandle(), true);
1631 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301632 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1633 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301634 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301635 }
1636
1637 /**
1638 * Returns if the attribute needs to be accessed in a qualified manner or
1639 * not, if it needs to be imported, then the same needs to be done.
1640 *
1641 * @param importInfo import info for the current attribute being added
1642 * @return status of the qualified access to the attribute
1643 */
1644 public boolean getIsQualifiedAccessOrAddToImportList(
1645 JavaQualifiedTypeInfo importInfo) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301646
1647 return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(),
1648 getJavaFileInfo().getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301649 }
1650
1651 /**
1652 * Checks if the import info is same as the package of the current generated
1653 * java file.
1654 *
1655 * @param importInfo import info for an attribute
1656 * @return true if the import info is same as the current nodes package
1657 * false otherwise
1658 */
1659 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1660 return getJavaFileInfo().getPackage()
1661 .contentEquals(importInfo.getPkgInfo());
1662 }
1663}