blob: d5701ed5644888d1ef8005cfcdbb1d2195e6a496 [file] [log] [blame]
Vinod Kumar S79a374b2016-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;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053020import java.util.List;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053021
Bharat saraswalaf413b82016-07-14 15:18:20 +053022import org.onosproject.yangutils.datamodel.YangAugmentableNode;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053023import org.onosproject.yangutils.datamodel.YangCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswalaf413b82016-07-14 15:18:20 +053027import org.onosproject.yangutils.datamodel.YangModule;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053028import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalaf413b82016-07-14 15:18:20 +053029import org.onosproject.yangutils.datamodel.YangSubModule;
Shankara-Huaweib7564772016-08-02 18:13:13 +053030import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
31import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Shankara-Huaweib7564772016-08-02 18:13:13 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGroupingTranslator;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053035import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053036
Bharat saraswalc2d3be12016-06-16 00:29:12 +053037import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
39import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.DEFAULT_CLASS_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053041import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Bharat saraswalaf413b82016-07-14 15:18:20 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
44import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_IMPL_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ADD_TO_LIST_INTERFACE_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswal250a7472016-05-12 13:16:57 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053051import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
52import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
53import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Bharat saraswal8beac342016-08-04 02:00:03 +053054import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053055import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
56import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
57import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
58import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053059import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
60import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053061import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Bharat saraswal8beac342016-08-04 02:00:03 +053062import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefinition;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053064import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswalaf413b82016-07-14 15:18:20 +053065import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053066import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
67import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053068import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateDefaultClassFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053069import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswald14cbe82016-07-14 13:26:18 +053071import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodImpl;
73import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddToListMethodInterface;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053074import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
76import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswal250a7472016-05-12 13:16:57 +053077import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053078import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
79import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
80import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053081import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053082import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
83import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
84import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053085import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
Bharat saraswal8beac342016-08-04 02:00:03 +053086import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST_IMPORT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053087import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
Bharat saraswald14cbe82016-07-14 13:26:18 +053088import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053089import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
90import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053091import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
92import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
Bharat saraswal8beac342016-08-04 02:00:03 +053093import static org.onosproject.yangutils.utils.UtilConstants.INVOCATION_TARGET_EXCEPTION_IMPORT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053094import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal8beac342016-08-04 02:00:03 +053095import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053096import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswal039f59c2016-07-14 21:57:13 +053097import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
98import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053099import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530100import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530101import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530102import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530103import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswal8beac342016-08-04 02:00:03 +0530104import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ADD_TO_LIST;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530105import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530106import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530107import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswald14cbe82016-07-14 13:26:18 +0530108import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
109import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
110import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530111import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
112import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530113import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530114
115/**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530116 * Represents implementation of java code fragments temporary implementations. Manages the common temp file required for
117 * Java file(s) generated.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530118 */
119public class TempJavaFragmentFiles {
Bharat saraswal250a7472016-05-12 13:16:57 +0530120
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530121 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530122 * File type extension for java classes.
123 */
124 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswal250a7472016-05-12 13:16:57 +0530125
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530126 /**
127 * File type extension for temporary classes.
128 */
129 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswal250a7472016-05-12 13:16:57 +0530130
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530131 /**
132 * Folder suffix for temporary files folder.
133 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530134 private static final String TEMP_FOLDER_NAME_SUFFIX = "-Temp";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530135
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530136 /**
137 * File name for getter method.
138 */
139 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530140
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530141 /**
142 * File name for setter method.
143 */
144 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530145
146 /**
147 * File name for getter method implementation.
148 */
149 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
150
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530151 /**
152 * File name for setter method implementation.
153 */
154 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530155
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530156 /**
157 * File name for attributes.
158 */
159 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswal250a7472016-05-12 13:16:57 +0530160
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530161 /**
162 * File name for to string method.
163 */
164 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswal250a7472016-05-12 13:16:57 +0530165
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530166 /**
167 * File name for hash code method.
168 */
169 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswal250a7472016-05-12 13:16:57 +0530170
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530171 /**
172 * File name for equals method.
173 */
174 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530175
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530176 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530177 * File name for from string method.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530178 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530179 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530180
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530181 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530182 * File name for from add to list interface method.
183 */
184 private static final String ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME = "addToList";
185
186 /**
187 * File name for from add to list impl method.
188 */
189 private static final String ADD_TO_LIST_IMPL_METHOD_FILE_NAME = "addToListImpl";
190
191 /**
192 * File name for from leaf identifier attributes.
193 */
194 private static final String LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME = "leafIdentifierAtr";
195
196 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530197 * File name for interface java file name suffix.
198 */
199 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswal250a7472016-05-12 13:16:57 +0530200
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530201 /**
202 * File name for builder interface file name suffix.
203 */
204 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswal250a7472016-05-12 13:16:57 +0530205
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530206 /**
207 * File name for builder class file name suffix.
208 */
209 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswal250a7472016-05-12 13:16:57 +0530210
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530211 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530212 * Information about the java files being generated.
213 */
214 private JavaFileInfo javaFileInfo;
215
216 /**
217 * Imported class info.
218 */
219 private JavaImportData javaImportData;
220
221 /**
222 * The variable which guides the types of temporary files generated using the temporary generated file types mask.
223 */
224 private int generatedTempFiles;
225
226 /**
227 * Absolute path where the target java file needs to be generated.
228 */
229 private String absoluteDirPath;
230
231 /**
232 * Contains all the interface(s)/class name which will be extended by generated files.
233 */
234 private JavaExtendsListHolder javaExtendsListHolder;
235
Bharat saraswald14cbe82016-07-14 13:26:18 +0530236 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530237 * Java file handle for interface file.
238 */
239 private File interfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530240
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530241 /**
242 * Java file handle for builder interface file.
243 */
244 private File builderInterfaceJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530245
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530246 /**
247 * Java file handle for builder class file.
248 */
249 private File builderClassJavaFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530250
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530251 /**
252 * Java file handle for impl class file.
253 */
254 private File implClassJavaFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530255
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530256 /**
257 * Temporary file handle for attribute.
258 */
259 private File attributesTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530260
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530261 /**
262 * Temporary file handle for getter of interface.
263 */
264 private File getterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530265
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530266 /**
267 * Temporary file handle for setter of interface.
268 */
269 private File setterInterfaceTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530270
271 /**
272 * Temporary file handle for getter of class.
273 */
274 private File getterImplTempFileHandle;
275
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530276 /**
277 * Temporary file handle for setter of class.
278 */
279 private File setterImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530280
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530281 /**
282 * Temporary file handle for hash code method of class.
283 */
284 private File hashCodeImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530285
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530286 /**
287 * Temporary file handle for equals method of class.
288 */
289 private File equalsImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530290
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530291 /**
292 * Temporary file handle for to string method of class.
293 */
294 private File toStringImplTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +0530295
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530296 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530297 * Temporary file handle for from string method of class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530298 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530299 private File fromStringImplTempFileHandle;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530300
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530301 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530302 * Temporary file handle for add to list interface method of class.
303 */
304 private File addToListInterfaceTempFileHandle;
305
306 /**
307 * Temporary file handle for add to list impl method of class.
308 */
309 private File addToListImplTempFileHandle;
310
311 /**
312 * Temporary file handle for leaf id attributes of enum.
313 */
314 private File leafIdAttributeTempFileHandle;
315
316 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530317 * Import info for case.
318 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530319 private JavaQualifiedTypeInfoTranslator caseImportInfo;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530320
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530321 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530322 * Leaf count.
323 */
324 private int leafCount = 0;
325
326 /**
327 * If current node is root node.
328 */
329 private boolean isRooNode;
330
331 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530332 * Is attribute added.
333 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530334 private boolean isAttributePresent;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530335
Bharat saraswalaf413b82016-07-14 15:18:20 +0530336 TempJavaFragmentFiles() {
337 }
338
339 /**
340 * Creates an instance of temporary java code fragment.
341 *
342 * @param javaFileInfo generated java file information
343 * @throws IOException when fails to create new file handle
344 */
345 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
346 throws IOException {
347 setJavaExtendsListHolder(new JavaExtendsListHolder());
348 setJavaImportData(new JavaImportData());
349 setJavaFileInfo(javaFileInfo);
Bharat saraswal8beac342016-08-04 02:00:03 +0530350 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
351 getJavaFileInfo().getPackageFilePath()));
352
Bharat saraswalaf413b82016-07-14 15:18:20 +0530353 /*
354 * Initialize getter when generation file type matches to interface
355 * mask.
356 */
357 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
358 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
Bharat saraswal8beac342016-08-04 02:00:03 +0530359 addGeneratedTempFile(ADD_TO_LIST_INTERFACE_MASK);
360 addGeneratedTempFile(LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530361 }
362
363 /*
364 * Initialize getter and setter when generation file type matches to
365 * builder interface mask.
366 */
367 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
368 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
369 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
370 }
371
372 /*
373 * Initialize getterImpl, setterImpl and attributes when generation file
374 * type matches to builder class mask.
375 */
376 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
377 addGeneratedTempFile(ATTRIBUTES_MASK);
378 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
379 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530380 }
381
382 /*
383 * Initialize getterImpl, attributes, constructor, hash code, equals and
384 * to strings when generation file type matches to impl class mask.
385 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530386 if ((getGeneratedJavaFiles() & DEFAULT_CLASS_MASK) != 0) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530387 addGeneratedTempFile(ATTRIBUTES_MASK);
388 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
389 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
390 addGeneratedTempFile(EQUALS_IMPL_MASK);
391 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswal8beac342016-08-04 02:00:03 +0530392 addGeneratedTempFile(ADD_TO_LIST_IMPL_MASK);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530393 }
394
395 /*
396 * Initialize temp files to generate type class.
397 */
398 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
399 addGeneratedTempFile(ATTRIBUTES_MASK);
400 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
401 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
402 addGeneratedTempFile(EQUALS_IMPL_MASK);
403 addGeneratedTempFile(TO_STRING_IMPL_MASK);
404 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
405 }
406
407 /*
408 * Initialize temp files to generate enum class.
409 */
410 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
411 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
412 }
413
414 /*
415 * Set temporary file handles.
416 */
417 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
418 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
419 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530420 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
421 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
422 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530423 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
424 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
425 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530426 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
427 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
428 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530429 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
430 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
431 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530432 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
433 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
434 }
435 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
436 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
437 }
438 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
439 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
440 }
441 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
442 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
443 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530444 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0) {
445 setAddToListInterfaceTempFileHandle(getTemporaryFileHandle(ADD_TO_LIST_INTERFACE_METHOD_FILE_NAME));
446 }
447 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0) {
448 setAddToListImplTempFileHandle(getTemporaryFileHandle(ADD_TO_LIST_IMPL_METHOD_FILE_NAME));
449 }
450 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0) {
451 setLeafIdAttributeTempFileHandle(getTemporaryFileHandle(LEAF_IDENTIFIER_ATTRIBUTES_FILE_NAME));
452 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530453 }
454
455 /**
456 * Adds current node info as and attribute to the parent generated file.
457 *
458 * @param curNode current node which needs to be added as an attribute in the parent generated code
459 * @param isList is list construct
460 * @param pluginConfig plugin configurations
461 * @throws IOException IO operation exception
462 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530463 static void addCurNodeInfoInParentTempFile(YangNode curNode,
464 boolean isList, YangPluginConfig pluginConfig)
Bharat saraswalaf413b82016-07-14 15:18:20 +0530465 throws IOException {
466 YangNode parent = getParentNodeInGenCode(curNode);
467 if (!(parent instanceof JavaCodeGenerator)) {
468 throw new TranslatorException("missing parent node to contain current node info in generated file");
469 }
470
Shankara-Huaweib7564772016-08-02 18:13:13 +0530471 if (parent instanceof YangJavaGroupingTranslator) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530472 /*
473 * In case of grouping, there is no need to add the information, it
474 * will be taken care in uses
475 */
476 return;
477 }
478 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) parent)
479 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
480
481 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
482 parent, isList, tempJavaBeanFragmentFiles);
Bharat saraswal8beac342016-08-04 02:00:03 +0530483 tempJavaBeanFragmentFiles.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530484 }
485
486 /**
487 * Creates an attribute info object corresponding to a data model node and return it.
488 *
489 * @param curNode current data model node for which the java code generation is being handled
490 * @param targetNode target node in which the current node is an attribute
491 * @param isListNode is the current added attribute needs to be a list
492 * @param tempJavaFragmentFiles temp java fragment files
493 * @return AttributeInfo attribute details required to add in temporary files
494 */
495 public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode,
496 YangNode targetNode, boolean isListNode,
497 TempJavaFragmentFiles tempJavaFragmentFiles) {
498 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
499 if (curNodeName == null) {
500 updateJavaFileInfo(curNode, null);
501 curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
502 }
503
504 /*
505 * Get the import info corresponding to the attribute for import in
506 * generated java files or qualified access
507 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530508 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
Bharat saraswalaf413b82016-07-14 15:18:20 +0530509 getCapitalCase(curNodeName));
510 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
511 throw new TranslatorException("Parent node does not have file info");
512 }
513 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
514 JavaFileInfo fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
515
516 boolean isQualified;
Bharat saraswalaab24b92016-08-02 18:43:16 +0530517 if ((tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles)
Bharat saraswal8beac342016-08-04 02:00:03 +0530518 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE))
519 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530520
521 isQualified = true;
522 } else {
523 String className;
Bharat saraswalaab24b92016-08-02 18:43:16 +0530524 if (tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530525 className = getCapitalCase(fileInfo.getJavaName()) + SERVICE;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530526 } else {
527 className = getCapitalCase(fileInfo.getJavaName());
528 }
529
530 isQualified = parentImportData.addImportInfo(qualifiedTypeInfo,
531 className, fileInfo.getPackage());
532 }
533
534 if (isListNode) {
535 parentImportData.setIfListImported(true);
536 }
537
538 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
539 }
540
541 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530542 * Returns java attribute for leaf.
543 *
544 * @param tempJavaFragmentFiles temporary generated file
545 * @param leaf YANG leaf
546 * @param yangPluginConfig plugin configurations
547 * @return java attribute for leaf
548 */
549 public static JavaAttributeInfo getJavaAttributeOfLeaf(TempJavaFragmentFiles tempJavaFragmentFiles, YangLeaf leaf,
550 YangPluginConfig yangPluginConfig) {
551 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
552 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
553 javaLeaf.updateJavaQualifiedInfo();
554 return getAttributeInfoForTheData(
555 javaLeaf.getJavaQualifiedInfo(),
556 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
557 javaLeaf.getDataType(),
558 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
559 false);
560 }
561
562 /**
563 * Returns java attribute for leaf-list.
564 *
565 * @param tempJavaFragmentFiles temporary generated file
566 * @param leafList YANG leaf-list
567 * @param yangPluginConfig plugin configurations
568 * @return java attribute for leaf-list
569 */
570 public static JavaAttributeInfo getJavaAttributeOfLeafList(TempJavaFragmentFiles tempJavaFragmentFiles,
571 YangLeafList leafList,
572 YangPluginConfig yangPluginConfig) {
573 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
574 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
575 javaLeaf.updateJavaQualifiedInfo();
576 tempJavaFragmentFiles.getJavaImportData().setIfListImported(true);
577 return getAttributeInfoForTheData(
578 javaLeaf.getJavaQualifiedInfo(),
579 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
580 javaLeaf.getDataType(),
581 tempJavaFragmentFiles.getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
582 true);
583 }
584
Bharat saraswal039f59c2016-07-14 21:57:13 +0530585 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530586 * Retrieves the absolute path where the file needs to be generated.
587 *
588 * @return absolute path where the file needs to be generated
589 */
590 private String getAbsoluteDirPath() {
591 return absoluteDirPath;
592 }
593
594 /**
595 * Sets absolute path where the file needs to be generated.
596 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530597 * @param absoluteDirPath absolute path where the file needs to be generated.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530598 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530599 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530600 this.absoluteDirPath = absoluteDirPath;
601 }
602
603 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530604 * Retrieves the generated java file information.
605 *
606 * @return generated java file information
607 */
608 public JavaFileInfo getJavaFileInfo() {
609 return javaFileInfo;
610 }
611
612 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530613 * Sets the generated java file information.
614 *
615 * @param javaFileInfo generated java file information
616 */
617 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
618 this.javaFileInfo = javaFileInfo;
619 }
620
621 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530622 * Retrieves the generated temp files.
623 *
624 * @return generated temp files
625 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530626 int getGeneratedTempFiles() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530627 return generatedTempFiles;
628 }
629
630 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +0530631 * Sets generated file files.
632 *
633 * @param fileType generated file type
634 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530635 private void setGeneratedTempFiles(int fileType) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530636 generatedTempFiles = fileType;
637 }
638
639 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530640 * Adds to generated temporary files.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530641 *
642 * @param generatedTempFile generated file
643 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530644 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530645 generatedTempFiles |= generatedTempFile;
Bharat saraswal250a7472016-05-12 13:16:57 +0530646 setGeneratedTempFiles(generatedTempFiles);
647 }
648
649 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530650 * Retrieves the generated Java files.
651 *
652 * @return generated Java files
653 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530654 int getGeneratedJavaFiles() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530655 return getJavaFileInfo().getGeneratedFileTypes();
656 }
657
658 /**
659 * Retrieves the mapped Java class name.
660 *
661 * @return mapped Java class name
662 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530663 String getGeneratedJavaClassName() {
janani b703cfe42016-05-17 13:12:22 +0530664 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530665 }
666
667 /**
668 * Retrieves the import data for the generated Java file.
669 *
670 * @return import data for the generated Java file
671 */
672 public JavaImportData getJavaImportData() {
673 return javaImportData;
674 }
675
676 /**
677 * Sets import data for the generated Java file.
678 *
679 * @param javaImportData import data for the generated Java file
680 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530681 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530682 this.javaImportData = javaImportData;
683 }
684
685 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530686 * Retrieves the status of any attributes added.
687 *
688 * @return status of any attributes added
689 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530690 boolean isAttributePresent() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530691 return isAttributePresent;
692 }
693
694 /**
695 * Sets status of any attributes added.
696 *
697 * @param attributePresent status of any attributes added
698 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530699 private void setAttributePresent(boolean attributePresent) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530700 isAttributePresent = attributePresent;
701 }
702
703 /**
704 * Returns getter methods's temporary file handle.
705 *
706 * @return temporary file handle
707 */
708 public File getGetterInterfaceTempFileHandle() {
709 return getterInterfaceTempFileHandle;
710 }
711
712 /**
713 * Sets to getter method's temporary file handle.
714 *
715 * @param getterForInterface file handle for to getter method
716 */
717 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
718 getterInterfaceTempFileHandle = getterForInterface;
719 }
720
721 /**
722 * Returns setter method's temporary file handle.
723 *
724 * @return temporary file handle
725 */
726 public File getSetterInterfaceTempFileHandle() {
727 return setterInterfaceTempFileHandle;
728 }
729
730 /**
731 * Sets to setter method's temporary file handle.
732 *
733 * @param setterForInterface file handle for to setter method
734 */
735 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
736 setterInterfaceTempFileHandle = setterForInterface;
737 }
738
739 /**
740 * Returns setter method's impl's temporary file handle.
741 *
742 * @return temporary file handle
743 */
744 public File getSetterImplTempFileHandle() {
745 return setterImplTempFileHandle;
746 }
747
748 /**
749 * Sets to setter method's impl's temporary file handle.
750 *
751 * @param setterImpl file handle for to setter method's implementation class
752 */
753 private void setSetterImplTempFileHandle(File setterImpl) {
754 setterImplTempFileHandle = setterImpl;
755 }
756
757 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530758 * Returns from string method's temporary file handle.
759 *
760 * @return from string method's temporary file handle
761 */
762 public File getFromStringImplTempFileHandle() {
763 return fromStringImplTempFileHandle;
764 }
765
766 /**
767 * Sets from string method's temporary file handle.
768 *
Bharat saraswalaf413b82016-07-14 15:18:20 +0530769 * @param fromStringImplTempFileHandle from string method's temporary file handle
Bharat saraswal250a7472016-05-12 13:16:57 +0530770 */
771 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
772 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
773 }
774
775 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530776 * Returns java file handle for interface file.
777 *
778 * @return java file handle for interface file
779 */
780 private File getInterfaceJavaFileHandle() {
781 return interfaceJavaFileHandle;
782 }
783
784 /**
785 * Sets the java file handle for interface file.
786 *
787 * @param interfaceJavaFileHandle java file handle
788 */
789 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
790 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
791 }
792
793 /**
794 * Returns java file handle for builder interface file.
795 *
796 * @return java file handle for builder interface file
797 */
798 private File getBuilderInterfaceJavaFileHandle() {
799 return builderInterfaceJavaFileHandle;
800 }
801
802 /**
803 * Sets the java file handle for builder interface file.
804 *
805 * @param builderInterfaceJavaFileHandle java file handle
806 */
807 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
808 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
809 }
810
811 /**
812 * Returns java file handle for builder class file.
813 *
814 * @return java file handle for builder class file
815 */
816 private File getBuilderClassJavaFileHandle() {
817 return builderClassJavaFileHandle;
818 }
819
820 /**
821 * Sets the java file handle for builder class file.
822 *
823 * @param builderClassJavaFileHandle java file handle
824 */
825 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
826 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
827 }
828
829 /**
830 * Returns java file handle for impl class file.
831 *
832 * @return java file handle for impl class file
833 */
834 private File getImplClassJavaFileHandle() {
835 return implClassJavaFileHandle;
836 }
837
838 /**
839 * Sets the java file handle for impl class file.
840 *
841 * @param implClassJavaFileHandle java file handle
842 */
843 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
844 this.implClassJavaFileHandle = implClassJavaFileHandle;
845 }
846
847 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530848 * Returns attribute's temporary file handle.
849 *
850 * @return temporary file handle
851 */
852 public File getAttributesTempFileHandle() {
853 return attributesTempFileHandle;
854 }
855
856 /**
857 * Sets attribute's temporary file handle.
858 *
859 * @param attributeForClass file handle for attribute
860 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530861 private void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530862 attributesTempFileHandle = attributeForClass;
863 }
864
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530865 /**
866 * Returns getter method's impl's temporary file handle.
867 *
868 * @return temporary file handle
869 */
870 public File getGetterImplTempFileHandle() {
871 return getterImplTempFileHandle;
872 }
873
874 /**
875 * Sets to getter method's impl's temporary file handle.
876 *
877 * @param getterImpl file handle for to getter method's impl
878 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530879 private void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530880 getterImplTempFileHandle = getterImpl;
881 }
882
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530883 /**
884 * Returns hash code method's temporary file handle.
885 *
886 * @return temporary file handle
887 */
888 public File getHashCodeImplTempFileHandle() {
889 return hashCodeImplTempFileHandle;
890 }
891
892 /**
893 * Sets hash code method's temporary file handle.
894 *
895 * @param hashCodeMethod file handle for hash code method
896 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530897 private void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530898 hashCodeImplTempFileHandle = hashCodeMethod;
899 }
900
901 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530902 * Returns equals method's temporary file handle.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530903 *
904 * @return temporary file handle
905 */
906 public File getEqualsImplTempFileHandle() {
907 return equalsImplTempFileHandle;
908 }
909
910 /**
911 * Sets equals method's temporary file handle.
912 *
913 * @param equalsMethod file handle for to equals method
914 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530915 private void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530916 equalsImplTempFileHandle = equalsMethod;
917 }
918
919 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530920 * Returns to string method's temporary file handle.
921 *
922 * @return temporary file handle
923 */
924 public File getToStringImplTempFileHandle() {
925 return toStringImplTempFileHandle;
926 }
927
928 /**
929 * Sets to string method's temporary file handle.
930 *
931 * @param toStringMethod file handle for to string method
932 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530933 private void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530934 toStringImplTempFileHandle = toStringMethod;
935 }
936
937 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530938 * Returns java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530939 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530940 * @return java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530941 */
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530942 public JavaExtendsListHolder getJavaExtendsListHolder() {
943 return javaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530944 }
945
946 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530947 * Sets java extends list holder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530948 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530949 * @param javaExtendsListHolder java extends list holder
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530950 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530951 void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530952 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530953 }
954
955 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530956 * Adds attribute for class.
957 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530958 * @param attr attribute info
959 * @param yangPluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530960 * @throws IOException when fails to append to temporary file
961 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530962 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530963 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530964 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
965 + FOUR_SPACE_INDENTATION);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530966 }
967
968 /**
969 * Adds getter for interface.
970 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530971 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530972 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530973 * @throws IOException when fails to append to temporary file
974 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530975 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530976 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530977 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530978 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530979 }
980
981 /**
982 * Adds setter for interface.
983 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530984 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530985 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530986 * @throws IOException when fails to append to temporary file
987 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530988 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530989 throws IOException {
990 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530991 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
992 + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530993 }
994
995 /**
996 * Adds setter's implementation for class.
997 *
998 * @param attr attribute info
999 * @throws IOException when fails to append to temporary file
1000 */
1001 private void addSetterImpl(JavaAttributeInfo attr)
1002 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301003 if (isRooNode()) {
1004 appendToFile(getSetterImplTempFileHandle(),
1005 getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1006 + NEW_LINE);
1007 } else {
1008 appendToFile(getSetterImplTempFileHandle(), getOverRideString() +
1009 getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
1010 + NEW_LINE);
1011 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301012 }
1013
1014 /**
1015 * Adds getter method's impl for class.
1016 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301017 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301018 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301019 * @throws IOException when fails to append to temporary file
1020 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301021 void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301022 throws IOException {
1023 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
1024 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301025 if (!isRooNode()) {
1026 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
1027 getGeneratedJavaFiles()) + NEW_LINE);
1028 } else {
1029 appendToFile(getGetterImplTempFileHandle(), getGetterForClass(attr,
1030 getGeneratedJavaFiles()) + NEW_LINE);
1031 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301032 } else {
janani b703cfe42016-05-17 13:12:22 +05301033 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301034 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b703cfe42016-05-17 13:12:22 +05301035 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301036 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301037 }
1038
1039 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301040 * Adds add to list interface method.
1041 *
1042 * @param attr attribute
1043 * @param pluginConfig plugin configurations
1044 * @throws IOException when fails to do IO operations
1045 */
1046 private void addAddToListInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig) throws IOException {
1047 appendToFile(getAddToListInterfaceTempFileHandle(),
1048 getJavaDoc(ADD_TO_LIST, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
1049 + getAddToListMethodInterface(attr) + NEW_LINE);
1050 }
1051
1052 /**
1053 * Adds add to list interface method.
1054 *
1055 * @param attr attribute
1056 * @throws IOException when fails to do IO operations
1057 */
1058 private void addAddToListImpl(JavaAttributeInfo attr) throws IOException {
1059 appendToFile(getAddToListImplTempFileHandle(),
1060 getAddToListMethodImpl(attr) + NEW_LINE);
1061 }
1062
1063 /**
1064 * Adds leaf identifier enum attributes.
1065 *
1066 * @param attr attribute
1067 * @param value value
1068 * @param yangPluginConfig plugin config
1069 * @throws IOException when fails to do IO operations
1070 */
1071 private void addLeafIdAttributes(JavaAttributeInfo attr, int value, YangPluginConfig yangPluginConfig)
1072 throws IOException {
1073 appendToFile(getLeafIdAttributeTempFileHandle(),
1074 FOUR_SPACE_INDENTATION + generateEnumAttributeString(attr.getAttributeName(),
1075 value, yangPluginConfig));
1076 }
1077
1078 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301079 * Adds build method for interface.
1080 *
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301081 * @param pluginConfig plugin configurations
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301082 * @return build method for interface
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301083 * @throws IOException when fails to append to temporary file
1084 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301085 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301086 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301087 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301088 }
1089
1090 /**
1091 * Adds build method's implementation for class.
1092 *
1093 * @return build method implementation for class
1094 * @throws IOException when fails to append to temporary file
1095 */
1096 String addBuildMethodImpl()
1097 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301098 return getBuildString(getGeneratedJavaClassName(), isRooNode()) + NEW_LINE;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301099 }
1100
1101 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301102 * Adds default constructor for class.
1103 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301104 * @param modifier modifier for constructor.
1105 * @param toAppend string which need to be appended with the class name
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301106 * @param pluginConfig plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +05301107 * @param isSuffix is value need to be appended as suffix
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301108 * @return default constructor for class
1109 * @throws IOException when fails to append to file
1110 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301111 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig, boolean isSuffix)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301112 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301113 String name = getGeneratedJavaClassName();
1114 if (isRooNode() && !toAppend.equals(BUILDER)) {
1115 name = name + OP_PARAM;
1116 return NEW_LINE
1117 + getDefaultConstructorString(name, modifier,
1118 pluginConfig);
1119 }
1120 if (isSuffix) {
1121 return NEW_LINE +
1122 getDefaultConstructorString(name + toAppend, modifier, pluginConfig);
1123 }
1124 String appended;
1125 if (toAppend.equals(DEFAULT)) {
1126 appended = getCapitalCase(toAppend);
1127 } else {
1128 appended = toAppend;
1129 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301130 return NEW_LINE
Bharat saraswal8beac342016-08-04 02:00:03 +05301131 + getDefaultConstructorString(appended + name, modifier,
1132 pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301133 }
1134
1135 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301136 * Adds hash code method for class.
1137 *
1138 * @param attr attribute info
1139 * @throws IOException when fails to append to temporary file
1140 */
1141 private void addHashCodeMethod(JavaAttributeInfo attr)
1142 throws IOException {
1143 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
1144 }
1145
1146 /**
1147 * Adds equals method for class.
1148 *
1149 * @param attr attribute info
1150 * @throws IOException when fails to append to temporary file
1151 */
1152 private void addEqualsMethod(JavaAttributeInfo attr)
1153 throws IOException {
1154 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
1155 }
1156
1157 /**
1158 * Adds ToString method for class.
1159 *
1160 * @param attr attribute info
1161 * @throws IOException when fails to append to temporary file
1162 */
1163 private void addToStringMethod(JavaAttributeInfo attr)
1164 throws IOException {
1165 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
1166 }
1167
1168 /**
Bharat saraswal250a7472016-05-12 13:16:57 +05301169 * Adds from string method for union class.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301170 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301171 * @param javaAttributeInfo type attribute info
Bharat saraswal250a7472016-05-12 13:16:57 +05301172 * @param fromStringAttributeInfo from string attribute info
1173 * @throws IOException when fails to append to temporary file
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301174 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301175 void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
1176 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301177 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +05301178 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1179 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301180 }
1181
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301182 /**
1183 * Returns a temporary file handle for the specific file type.
1184 *
1185 * @param fileName file name
1186 * @return temporary file handle
1187 * @throws IOException when fails to create new file handle
1188 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301189 File getTemporaryFileHandle(String fileName)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301190 throws IOException {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301191 String path = getTempDirPath(getAbsoluteDirPath());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301192 File dir = new File(path);
Bharat saraswal8beac342016-08-04 02:00:03 +05301193 boolean isCreated;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301194 if (!dir.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301195 isCreated = dir.mkdirs();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301196 if (!isCreated) {
1197 throw new IOException("failed to create temporary directory for " + fileName);
1198 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301199 }
1200 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1201 if (!file.exists()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301202 isCreated = file.createNewFile();
Shankara-Huaweib7564772016-08-02 18:13:13 +05301203 if (!isCreated) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301204 throw new IOException("failed to create temporary file for " + fileName);
Shankara-Huaweib7564772016-08-02 18:13:13 +05301205 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301206 } else {
1207 throw new IOException(fileName + " is reused due to YANG naming");
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301208 }
1209 return file;
1210 }
1211
1212 /**
1213 * Returns a temporary file handle for the specific file type.
1214 *
1215 * @param fileName file name
1216 * @return temporary file handle
1217 * @throws IOException when fails to create new file handle
1218 */
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301219 File getJavaFileHandle(String fileName)
1220 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +05301221 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301222 }
1223
1224 /**
1225 * Returns data from the temporary files.
1226 *
Bharat saraswalaf413b82016-07-14 15:18:20 +05301227 * @param file temporary file handle
Bharat saraswald14cbe82016-07-14 13:26:18 +05301228 * @param absolutePath absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301229 * @return stored data from temporary files
1230 * @throws IOException when failed to get data from the given file
1231 */
Bharat saraswald14cbe82016-07-14 13:26:18 +05301232 public String getTemporaryDataFromFileHandle(File file, String absolutePath)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301233 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301234
Bharat saraswald14cbe82016-07-14 13:26:18 +05301235 String path = getTempDirPath(absolutePath);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301236 if (new File(path + file.getName()).exists()) {
1237 return readAppendFile(path + file.getName(), EMPTY_STRING);
1238 } else {
1239 throw new IOException("Unable to get data from the given "
1240 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1241 }
1242 }
1243
1244 /**
1245 * Returns temporary directory path.
1246 *
Bharat saraswald14cbe82016-07-14 13:26:18 +05301247 * @param absolutePath absolute path
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301248 * @return directory path
1249 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301250 private String getTempDirPath(String absolutePath) {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301251 return getPackageDirPathFromJavaJPackage(absolutePath) + SLASH + getGeneratedJavaClassName()
Bharat saraswal64e7e232016-07-14 23:33:55 +05301252 + TEMP_FOLDER_NAME_SUFFIX + SLASH;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301253 }
1254
1255 /**
1256 * Parses attribute to get the attribute string.
1257 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301258 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301259 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301260 * @return attribute string
1261 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301262 String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301263 /*
1264 * TODO: check if this utility needs to be called or move to the caller
1265 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301266 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301267 String attributeAccessType = PRIVATE;
1268 if ((javaFileInfo.getGeneratedFileTypes() & GENERATE_INTERFACE_WITH_BUILDER) != 0) {
1269 attributeAccessType = PROTECTED;
1270 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301271 if (attr.isQualifiedName()) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301272 return getJavaAttributeDefinition(attr.getImportInfo().getPkgInfo(),
Bharat saraswal250a7472016-05-12 13:16:57 +05301273 attr.getImportInfo().getClassInfo(),
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301274 attributeName, attr.isListAttr(), attributeAccessType);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301275 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +05301276 return getJavaAttributeDefinition(null, attr.getImportInfo().getClassInfo(), attributeName,
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301277 attr.isListAttr(), attributeAccessType);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301278 }
1279 }
1280
1281 /**
1282 * Appends content to temporary file.
1283 *
1284 * @param file temporary file
1285 * @param data data to be appended
1286 * @throws IOException when fails to append to file
1287 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301288 void appendToFile(File file, String data)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301289 throws IOException {
1290 try {
1291 insertDataIntoJavaFile(file, data);
1292 } catch (IOException ex) {
1293 throw new IOException("failed to write in temp file.");
1294 }
1295 }
1296
1297 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301298 * Adds parent's info to current node import list.
1299 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301300 * @param curNode current node for which import list needs to be updated
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301301 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301302 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301303 void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Shankara-Huaweib7564772016-08-02 18:13:13 +05301304 caseImportInfo = new JavaQualifiedTypeInfoTranslator();
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301305 YangNode parent = getParentNodeInGenCode(curNode);
1306 if (!(parent instanceof JavaCodeGenerator)) {
1307 throw new TranslatorException("missing parent node to contain current node info in generated file");
1308 }
1309 if (!(curNode instanceof JavaFileInfoContainer)) {
1310 throw new TranslatorException("missing java file information to get the package details "
1311 + "of attribute corresponding to child node");
1312 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301313 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1314 pluginConfig.getConflictResolver())));
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301315 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301316
1317 JavaFileInfo fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1318
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301319 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301320 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +05301321 getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301322 }
1323
1324 /**
1325 * Adds leaf attributes in generated files.
1326 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301327 * @param listOfLeaves list of YANG leaf
1328 * @param yangPluginConfig plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +05301329 * @param curNode current node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301330 * @throws IOException IO operation fail
1331 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301332 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1333 YangPluginConfig yangPluginConfig, YangNode curNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301334 throws IOException {
1335 if (listOfLeaves != null) {
1336 for (YangLeaf leaf : listOfLeaves) {
1337 if (!(leaf instanceof JavaLeafInfoContainer)) {
1338 throw new TranslatorException("Leaf does not have java information");
1339 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301340 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1341 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1342 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
1343 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(tempJavaBeanFragmentFiles, leaf,
1344 yangPluginConfig), yangPluginConfig);
1345 } else {
1346 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(this, leaf, yangPluginConfig),
1347 yangPluginConfig);
1348 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301349 }
1350 }
1351 }
1352
1353 /**
1354 * Adds leaf list's attributes in generated files.
1355 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301356 * @param listOfLeafList list of YANG leaves
1357 * @param yangPluginConfig plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +05301358 * @param curNode current node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301359 * @throws IOException IO operation fail
1360 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301361 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig,
1362 YangNode curNode) throws IOException {
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301363 if (listOfLeafList != null) {
1364 for (YangLeafList leafList : listOfLeafList) {
1365 if (!(leafList instanceof JavaLeafInfoContainer)) {
1366 throw new TranslatorException("Leaf-list does not have java information");
1367 }
Bharat saraswal64e7e232016-07-14 23:33:55 +05301368 if (curNode instanceof YangModule || curNode instanceof YangSubModule) {
1369 TempJavaBeanFragmentFiles tempJavaBeanFragmentFiles = ((JavaCodeGeneratorInfo) curNode)
1370 .getTempJavaCodeFragmentFiles().getBeanTempFiles();
1371 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(tempJavaBeanFragmentFiles,
1372 leafList, yangPluginConfig), yangPluginConfig);
1373 } else {
1374 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(this,
1375 leafList, yangPluginConfig), yangPluginConfig);
1376 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301377 }
1378 }
1379 }
1380
1381 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +05301382 * Adds all the leaves in the current data model node as part of the generated temporary file.
Bharat saraswald14cbe82016-07-14 13:26:18 +05301383 *
Bharat saraswal64e7e232016-07-14 23:33:55 +05301384 * @param curNode current node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301385 * @param yangPluginConfig plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301386 * @throws IOException IO operation fail
1387 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301388 void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
Shankara-Huaweib7564772016-08-02 18:13:13 +05301389 YangPluginConfig yangPluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301390 throws IOException {
1391 if (!(curNode instanceof YangLeavesHolder)) {
1392 throw new TranslatorException("Data model node does not have any leaves");
1393 }
1394 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
Bharat saraswal64e7e232016-07-14 23:33:55 +05301395 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig, curNode);
1396 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig, curNode);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301397 }
1398
1399 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301400 * Adds the new attribute info to the target generated temporary files.
1401 *
Bharat saraswalaf413b82016-07-14 15:18:20 +05301402 * @param newAttrInfo the attribute info that needs to be added to temporary files
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301403 * @param pluginConfig plugin configurations
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301404 * @throws IOException IO operation fail
1405 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301406 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301407 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301408 setAttributePresent(true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301409 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301410 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301411 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301412
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301413 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301414 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301415 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301416
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301417 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +05301418 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301419 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301420
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301421 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1422 addSetterImpl(newAttrInfo);
1423 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301424
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301425 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1426 addHashCodeMethod(newAttrInfo);
1427 }
1428 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1429 addEqualsMethod(newAttrInfo);
1430 }
1431 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1432 addToStringMethod(newAttrInfo);
1433 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301434 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0 && newAttrInfo.isListAttr()) {
1435 addAddToListImpl(newAttrInfo);
1436 }
1437 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0 && newAttrInfo.isListAttr()) {
1438 addAddToListInterface(newAttrInfo, pluginConfig);
1439 }
1440 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0 && !newAttrInfo.isListAttr()
1441 && newAttrInfo.getAttributeType() != null) {
1442 leafCount++;
1443 addLeafIdAttributes(newAttrInfo, leafCount, pluginConfig);
1444 }
Bharat saraswal250a7472016-05-12 13:16:57 +05301445
Bharat saraswal64e7e232016-07-14 23:33:55 +05301446 if (!newAttrInfo.isIntConflict() &&
1447 !newAttrInfo.isLongConflict()) {
1448 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1449 addGetterImpl(newAttrInfo, pluginConfig);
1450 }
1451
1452 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Shankara-Huaweib7564772016-08-02 18:13:13 +05301453 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +05301454 pluginConfig.getConflictResolver());
Bharat saraswal250a7472016-05-12 13:16:57 +05301455 /*
1456 * Create a new java attribute info with qualified information of
1457 * wrapper classes.
1458 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301459 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1460 newAttrInfo.getAttributeName(),
1461 newAttrInfo.getAttributeType(),
1462 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
Bharat saraswal250a7472016-05-12 13:16:57 +05301463
Bharat saraswal64e7e232016-07-14 23:33:55 +05301464 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
1465 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301466 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301467 }
1468
1469 /**
1470 * Returns java class name.
1471 *
1472 * @param suffix for the class name based on the file type
1473 * @return java class name
1474 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301475 String getJavaClassName(String suffix) {
janani b703cfe42016-05-17 13:12:22 +05301476 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301477 }
1478
1479 /**
Bharat saraswald14cbe82016-07-14 13:26:18 +05301480 * Returns java class name.
1481 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301482 * @param node YANG node
Bharat saraswald14cbe82016-07-14 13:26:18 +05301483 * @return java class name
1484 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301485 private String getImplClassName(YangNode node) {
1486 if (node instanceof YangModule || node instanceof YangSubModule) {
1487 return getCapitalCase(getJavaFileInfo().getJavaName()) + OP_PARAM;
1488 }
Bharat saraswald14cbe82016-07-14 13:26:18 +05301489 return getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo().getJavaName());
1490 }
1491
1492 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301493 * Returns the directory path.
1494 *
1495 * @return directory path
1496 */
1497 private String getDirPath() {
1498 return getJavaFileInfo().getPackageFilePath();
1499 }
1500
1501 /**
1502 * Constructs java code exit.
1503 *
1504 * @param fileType generated file type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301505 * @param curNode current YANG node
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301506 * @throws IOException when fails to generate java files
1507 */
1508 public void generateJavaFile(int fileType, YangNode curNode)
1509 throws IOException {
Bharat saraswalaf413b82016-07-14 15:18:20 +05301510 List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getBeanTempFiles()
1511 .getJavaImportData().getImports();
Bharat saraswal8beac342016-08-04 02:00:03 +05301512 if (curNode instanceof YangAugmentableNode) {
1513 addImportsForAugmentableClass(imports, true, true);
1514 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301515 createPackage(curNode);
1516
1517 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301518 * Generate java code.
1519 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301520 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1521 BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +05301522
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301523 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301524 * Create interface file.
1525 */
1526 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1527 setInterfaceJavaFileHandle(
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301528 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
Bharat saraswal8beac342016-08-04 02:00:03 +05301529 if (!(curNode instanceof YangModule) && !(curNode instanceof YangSubModule)) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301530 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301531 * Create builder interface file.
1532 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301533 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1534 setBuilderInterfaceJavaFileHandle(
1535 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1536 setBuilderInterfaceJavaFileHandle(
1537 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1538 isAttributePresent()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301539 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301540 * Append builder interface file to interface file and close it.
1541 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301542 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1543 validateLineLength(getInterfaceJavaFileHandle());
1544 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301545 }
1546 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301547
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301548 if (curNode instanceof YangCase) {
1549 removeCaseImport(imports);
1550 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301551 if (curNode instanceof YangAugmentableNode) {
1552 addImportsForAugmentableClass(imports, false, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301553 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301554 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301555 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & DEFAULT_CLASS_MASK) != 0) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301556 if (isAttributePresent()) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301557 addImportsToStringAndHasCodeMethods(imports, true);
Bharat saraswal8beac342016-08-04 02:00:03 +05301558 addArrayListImport(imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301559 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301560 addBitsetImport(imports);
Bharat saraswalaf413b82016-07-14 15:18:20 +05301561 if (curNode instanceof YangAugmentableNode) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301562 addImportsForAugmentableClass(imports, true, false);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301563 }
Bharat saraswal250a7472016-05-12 13:16:57 +05301564 sortImports(imports);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301565 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301566 * Create impl class file.
1567 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301568 setImplClassJavaFileHandle(getJavaFileHandle(getImplClassName(curNode)));
Bharat saraswald14cbe82016-07-14 13:26:18 +05301569 setImplClassJavaFileHandle(
Bharat saraswal8beac342016-08-04 02:00:03 +05301570 generateDefaultClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent(), imports));
Bharat saraswald14cbe82016-07-14 13:26:18 +05301571
1572 /*
1573 * Create builder class file.
1574 */
1575 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1576 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1577 setBuilderClassJavaFileHandle(
1578 generateBuilderClassFile(getBuilderClassJavaFileHandle(), curNode,
1579 isAttributePresent()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301580 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301581 * Append impl class to builder class and close it.
1582 */
Bharat saraswald14cbe82016-07-14 13:26:18 +05301583 mergeJavaFiles(getBuilderClassJavaFileHandle(), getImplClassJavaFileHandle());
1584 validateLineLength(getImplClassJavaFileHandle());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301585 }
Bharat saraswald14cbe82016-07-14 13:26:18 +05301586 insertDataIntoJavaFile(getImplClassJavaFileHandle(), getJavaClassDefClose());
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301587
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301588 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301589 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301590 * Close all the file handles.
1591 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301592 freeTemporaryResources(false);
1593 }
1594
Bharat saraswal8beac342016-08-04 02:00:03 +05301595 /*Adds import for array list.*/
1596 private void addArrayListImport(List<String> imports) {
1597 if (imports.contains(getJavaImportData().getImportForList())) {
1598 imports.add(ARRAY_LIST_IMPORT);
1599 }
1600 }
1601
1602 /*Adds import for bitset list.*/
1603 private void addBitsetImport(List<String> imports) {
1604 imports.add(getJavaImportData().getImportForToBitSet());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301605 }
1606
1607 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301608 * Adds imports for ToString and HashCodeMethod.
1609 *
Bharat saraswal039f59c2016-07-14 21:57:13 +05301610 * @param imports import list
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301611 * @param operation add or remove
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301612 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301613 void addImportsToStringAndHasCodeMethods(List<String> imports, boolean operation) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301614 if (operation) {
1615 imports.add(getJavaImportData().getImportForHashAndEquals());
1616 imports.add(getJavaImportData().getImportForToString());
1617 } else {
1618 imports.remove(getJavaImportData().getImportForHashAndEquals());
1619 imports.remove(getJavaImportData().getImportForToString());
1620 }
1621 }
1622
1623 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +05301624 * Adds import for map and hash map.
1625 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301626 * @param imports import list
1627 * @param operations true for adding and false for deletion
1628 * @param isInterfaceFile if need to add in interface file
Bharat saraswalaf413b82016-07-14 15:18:20 +05301629 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301630 private void addImportsForAugmentableClass(List<String> imports, boolean operations, boolean isInterfaceFile) {
Bharat saraswal039f59c2016-07-14 21:57:13 +05301631 if (operations) {
Bharat saraswal8beac342016-08-04 02:00:03 +05301632 if (!isInterfaceFile) {
1633 imports.add(getJavaImportData().getHashMapImport());
1634 }
Bharat saraswal039f59c2016-07-14 21:57:13 +05301635 imports.add(getJavaImportData().getMapImport());
Bharat saraswal8beac342016-08-04 02:00:03 +05301636 addInvocationExceptionImport(imports);
Bharat saraswal039f59c2016-07-14 21:57:13 +05301637 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +05301638 if (!isInterfaceFile) {
1639 imports.remove(getJavaImportData().getHashMapImport());
1640 }
Bharat saraswal039f59c2016-07-14 21:57:13 +05301641 imports.remove(getJavaImportData().getMapImport());
Bharat saraswald14cbe82016-07-14 13:26:18 +05301642 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301643 sortImports(imports);
Bharat saraswald14cbe82016-07-14 13:26:18 +05301644 }
1645
1646 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301647 * Removes case import info from import list.
1648 *
1649 * @param imports list of imports
1650 * @return import for class
1651 */
1652 private List<String> removeCaseImport(List<String> imports) {
1653 if (imports != null && caseImportInfo != null) {
1654 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1655 SEMI_COLAN + NEW_LINE;
1656 imports.remove(caseImport);
1657 }
1658 return imports;
1659 }
1660
1661 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301662 * Adds invocation exception import.
Bharat saraswal039f59c2016-07-14 21:57:13 +05301663 *
1664 * @param imports list of imports
Bharat saraswal039f59c2016-07-14 21:57:13 +05301665 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301666 private void addInvocationExceptionImport(List<String> imports) {
1667 imports.add(INVOCATION_TARGET_EXCEPTION_IMPORT);
Bharat saraswal039f59c2016-07-14 21:57:13 +05301668 }
1669
1670 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301671 * Removes all temporary file handles.
1672 *
Bharat saraswal64e7e232016-07-14 23:33:55 +05301673 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301674 * @throws IOException when failed to delete the temporary files
1675 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301676 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301677 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301678 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301679 * Close all java file handles and when error occurs delete the files.
1680 */
1681 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +05301682 closeFile(getInterfaceJavaFileHandle(), isErrorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301683 }
1684 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
Bharat saraswald14cbe82016-07-14 13:26:18 +05301685 closeFile(getBuilderClassJavaFileHandle(), true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301686 }
1687 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1688 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1689 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301690 if ((getGeneratedJavaFiles() & DEFAULT_CLASS_MASK) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +05301691 closeFile(getImplClassJavaFileHandle(), isErrorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301692 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301693
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301694 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301695 * Close all temporary file handles and delete the files.
1696 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301697 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1698 closeFile(getGetterImplTempFileHandle(), true);
1699 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301700 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1701 closeFile(getAttributesTempFileHandle(), true);
1702 }
1703 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1704 closeFile(getHashCodeImplTempFileHandle(), true);
1705 }
1706 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1707 closeFile(getToStringImplTempFileHandle(), true);
1708 }
1709 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1710 closeFile(getEqualsImplTempFileHandle(), true);
1711 }
Bharat saraswal250a7472016-05-12 13:16:57 +05301712 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1713 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301714 }
Bharat saraswal8beac342016-08-04 02:00:03 +05301715 if ((getGeneratedTempFiles() & ADD_TO_LIST_IMPL_MASK) != 0) {
1716 closeFile(getAddToListImplTempFileHandle(), true);
1717 }
1718 if ((getGeneratedTempFiles() & ADD_TO_LIST_INTERFACE_MASK) != 0) {
1719 closeFile(getAddToListInterfaceTempFileHandle(), true);
1720 }
1721 if ((getGeneratedTempFiles() & LEAF_IDENTIFIER_ENUM_ATTRIBUTES_MASK) != 0) {
1722 closeFile(getLeafIdAttributeTempFileHandle(), true);
1723 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301724 }
1725
1726 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +05301727 * Returns if the attribute needs to be accessed in a qualified manner or not, if it needs to be imported, then the
1728 * same needs to be done.
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301729 *
1730 * @param importInfo import info for the current attribute being added
1731 * @return status of the qualified access to the attribute
1732 */
Bharat saraswal64e7e232016-07-14 23:33:55 +05301733 boolean getIsQualifiedAccessOrAddToImportList(
Shankara-Huaweib7564772016-08-02 18:13:13 +05301734 JavaQualifiedTypeInfoTranslator importInfo) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +05301735
1736 return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(),
1737 getJavaFileInfo().getPackage());
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301738 }
1739
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301740 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301741 * Returns temp file handle for add to list interface.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301742 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301743 * @return temp file handle for add to list interface
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301744 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301745 public File getAddToListInterfaceTempFileHandle() {
1746 return addToListInterfaceTempFileHandle;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301747 }
1748
1749 /**
Bharat saraswal8beac342016-08-04 02:00:03 +05301750 * Sets temp file handle for add to list interface.
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301751 *
Bharat saraswal8beac342016-08-04 02:00:03 +05301752 * @param addToListInterfaceTempFileHandle temp file handle for add to list interface
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301753 */
Bharat saraswal8beac342016-08-04 02:00:03 +05301754 private void setAddToListInterfaceTempFileHandle(File addToListInterfaceTempFileHandle) {
1755 this.addToListInterfaceTempFileHandle = addToListInterfaceTempFileHandle;
1756 }
1757
1758 /**
1759 * Returns temp file handle for add to list impl.
1760 *
1761 * @return temp file handle for add to list impl
1762 */
1763 public File getAddToListImplTempFileHandle() {
1764 return addToListImplTempFileHandle;
1765 }
1766
1767 /**
1768 * Sets temp file handle for add to list impl.
1769 *
1770 * @param addToListImplTempFileHandle temp file handle for add to list impl
1771 */
1772 private void setAddToListImplTempFileHandle(File addToListImplTempFileHandle) {
1773 this.addToListImplTempFileHandle = addToListImplTempFileHandle;
1774 }
1775
1776 /**
1777 * Returns temp file handle for leaf identifier attributes.
1778 *
1779 * @return temp file handle for leaf identifier attributes
1780 */
1781 public File getLeafIdAttributeTempFileHandle() {
1782 return leafIdAttributeTempFileHandle;
1783 }
1784
1785 /**
1786 * Sets temp file handle for leaf identifier attributes.
1787 *
1788 * @param leafIdAttributeTempFileHandle temp file handle for leaf identifier attributes.
1789 */
1790 private void setLeafIdAttributeTempFileHandle(File leafIdAttributeTempFileHandle) {
1791 this.leafIdAttributeTempFileHandle = leafIdAttributeTempFileHandle;
1792 }
1793
1794 /**
1795 * Returns if root node is set.
1796 *
1797 * @return true if root node
1798 */
1799 private boolean isRooNode() {
1800 return isRooNode;
1801 }
1802
1803 /**
1804 * Sets true if root node.
1805 *
1806 * @param rooNode true if root node
1807 */
1808 public void setRooNode(boolean rooNode) {
1809 isRooNode = rooNode;
Shankara-Huaweia1039e52016-07-14 16:53:09 +05301810 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301811}