blob: bbc346dbd9adfd851df3c1c2b9fc0144edd48c0a [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 saraswalb1170bd2016-07-14 13:26:18 +053032import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment;
Bharat saraswalcad0e652016-05-26 23:48:38 +053033import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
Bharat saraswalcad0e652016-05-26 23:48:38 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
Bharat saraswalcad0e652016-05-26 23:48:38 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053036import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053037import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053038
Bharat saraswal96dfef02016-06-16 00:29:12 +053039import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-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 Se4b9b0c2016-04-30 21:09:15 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
46import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.AUGMENTE_CLASS_CONSTRUCTOR_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053054import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
55import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
56import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
57import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053058import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053060import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.updateJavaFileInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
62import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
64import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
66import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053068import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
69import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentedConstructor;
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;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053083import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
84import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.sortImports;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053085import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053087import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT;
88import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053089import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053090import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
91import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
92import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
93import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
94import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053095import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053096import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053097import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053098import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE;
99import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE_CARDINALITY;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530100import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530101import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530102import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
104import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
105import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530106import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswalb1170bd2016-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 Se4b9b0c2016-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 saraswal96dfef02016-06-16 00:29:12 +0530113import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530114
115/**
116 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530117 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 */
119public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530120
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 /**
122 * Information about the java files being generated.
123 */
124 private JavaFileInfo javaFileInfo;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530125
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530126 /**
127 * Imported class info.
128 */
129 private JavaImportData javaImportData;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530130
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530131 /**
132 * The variable which guides the types of temporary files generated using
133 * the temporary generated file types mask.
134 */
135 private int generatedTempFiles;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530136
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530137 /**
138 * Absolute path where the target java file needs to be generated.
139 */
140 private String absoluteDirPath;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530141
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530143 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530144 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530145 private JavaExtendsListHolder javaExtendsListHolder;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530146
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530147 /**
148 * File type extension for java classes.
149 */
150 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530151
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 /**
153 * File type extension for temporary classes.
154 */
155 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530156
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 /**
158 * Folder suffix for temporary files folder.
159 */
160 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530161
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530162 /**
163 * File name for getter method.
164 */
165 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530167 /**
168 * File name for setter method.
169 */
170 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530171
172 /**
173 * File name for getter method implementation.
174 */
175 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
176
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530177 /**
178 * File name for setter method implementation.
179 */
180 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530181
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530182 /**
183 * File name for attributes.
184 */
185 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530187 /**
188 * File name for to string method.
189 */
190 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530191
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 /**
193 * File name for hash code method.
194 */
195 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530196
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530197 /**
198 * File name for equals method.
199 */
200 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530201
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530202 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530203 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530204 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530205 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530206
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530207 /**
208 * File name for interface java file name suffix.
209 */
210 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530211
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530212 /**
213 * File name for builder interface file name suffix.
214 */
215 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530216
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530217 /**
218 * File name for builder class file name suffix.
219 */
220 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530221
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530222 /**
223 * File name for impl class file name suffix.
224 */
225 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530226
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530227 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530228 * File name for augment copy constructor method.
229 */
230 private static final String AUGMENT_CONSTRUCTOR_FILE_NAME = "AugmentConstructor";
231 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530232 * Java file handle for interface file.
233 */
234 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530235
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530236 /**
237 * Java file handle for builder interface file.
238 */
239 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530240
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530241 /**
242 * Java file handle for builder class file.
243 */
244 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530245
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530246 /**
247 * Java file handle for impl class file.
248 */
249 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530250
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530251 /**
252 * Temporary file handle for attribute.
253 */
254 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530255
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530256 /**
257 * Temporary file handle for getter of interface.
258 */
259 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530260
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530261 /**
262 * Temporary file handle for setter of interface.
263 */
264 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530265
266 /**
267 * Temporary file handle for getter of class.
268 */
269 private File getterImplTempFileHandle;
270
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530271 /**
272 * Temporary file handle for setter of class.
273 */
274 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530275
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530276 /**
277 * Temporary file handle for hash code method of class.
278 */
279 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530280
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530281 /**
282 * Temporary file handle for equals method of class.
283 */
284 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530285
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530286 /**
287 * Temporary file handle for to string method of class.
288 */
289 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530290
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530291 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530292 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530293 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530294 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530295
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530296 /**
297 * Import info for case.
298 */
299 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530300
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530301 /**
302 * Is attribute added.
303 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530304 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530305
Bharat saraswalc0e04842016-05-12 13:16:57 +0530306 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530307 * Temporary file handle for augments copy constructor method of class.
308 */
309 private File augmentConstructorImplTempFileHandle;
310
311 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530312 * Retrieves the absolute path where the file needs to be generated.
313 *
314 * @return absolute path where the file needs to be generated
315 */
316 private String getAbsoluteDirPath() {
317 return absoluteDirPath;
318 }
319
320 /**
321 * Sets absolute path where the file needs to be generated.
322 *
323 * @param absoluteDirPath absolute path where the file needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530324 * generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530325 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530326 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530327 this.absoluteDirPath = absoluteDirPath;
328 }
329
330 /**
331 * Sets the generated java file information.
332 *
333 * @param javaFileInfo generated java file information
334 */
335 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
336 this.javaFileInfo = javaFileInfo;
337 }
338
339 /**
340 * Retrieves the generated java file information.
341 *
342 * @return generated java file information
343 */
344 public JavaFileInfo getJavaFileInfo() {
345 return javaFileInfo;
346 }
347
348 /**
349 * Retrieves the generated temp files.
350 *
351 * @return generated temp files
352 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530353 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530354 return generatedTempFiles;
355 }
356
357 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530358 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530359 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530360 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530361 generatedTempFiles = 0;
362 }
363
364 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530365 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530366 *
367 * @param generatedTempFile generated file
368 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530369 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530370 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530371 setGeneratedTempFiles(generatedTempFiles);
372 }
373
374 /**
375 * Sets generated file files.
376 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530377 * @param fileType generated file type
Bharat saraswalc0e04842016-05-12 13:16:57 +0530378 */
379 void setGeneratedTempFiles(int fileType) {
380 generatedTempFiles = fileType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530381 }
382
383 /**
384 * Retrieves the generated Java files.
385 *
386 * @return generated Java files
387 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530388 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530389 return getJavaFileInfo().getGeneratedFileTypes();
390 }
391
392 /**
393 * Retrieves the mapped Java class name.
394 *
395 * @return mapped Java class name
396 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530397 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530398 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530399 }
400
401 /**
402 * Retrieves the import data for the generated Java file.
403 *
404 * @return import data for the generated Java file
405 */
406 public JavaImportData getJavaImportData() {
407 return javaImportData;
408 }
409
410 /**
411 * Sets import data for the generated Java file.
412 *
413 * @param javaImportData import data for the generated Java file
414 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530415 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530416 this.javaImportData = javaImportData;
417 }
418
419 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530420 * Retrieves the status of any attributes added.
421 *
422 * @return status of any attributes added
423 */
424 public boolean isAttributePresent() {
425 return isAttributePresent;
426 }
427
428 /**
429 * Sets status of any attributes added.
430 *
431 * @param attributePresent status of any attributes added
432 */
433 public void setAttributePresent(boolean attributePresent) {
434 isAttributePresent = attributePresent;
435 }
436
437 /**
438 * Returns getter methods's temporary file handle.
439 *
440 * @return temporary file handle
441 */
442 public File getGetterInterfaceTempFileHandle() {
443 return getterInterfaceTempFileHandle;
444 }
445
446 /**
447 * Sets to getter method's temporary file handle.
448 *
449 * @param getterForInterface file handle for to getter method
450 */
451 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
452 getterInterfaceTempFileHandle = getterForInterface;
453 }
454
455 /**
456 * Returns setter method's temporary file handle.
457 *
458 * @return temporary file handle
459 */
460 public File getSetterInterfaceTempFileHandle() {
461 return setterInterfaceTempFileHandle;
462 }
463
464 /**
465 * Sets to setter method's temporary file handle.
466 *
467 * @param setterForInterface file handle for to setter method
468 */
469 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
470 setterInterfaceTempFileHandle = setterForInterface;
471 }
472
473 /**
474 * Returns setter method's impl's temporary file handle.
475 *
476 * @return temporary file handle
477 */
478 public File getSetterImplTempFileHandle() {
479 return setterImplTempFileHandle;
480 }
481
482 /**
483 * Sets to setter method's impl's temporary file handle.
484 *
485 * @param setterImpl file handle for to setter method's implementation class
486 */
487 private void setSetterImplTempFileHandle(File setterImpl) {
488 setterImplTempFileHandle = setterImpl;
489 }
490
491 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530492 * Returns from string method's temporary file handle.
493 *
494 * @return from string method's temporary file handle
495 */
496 public File getFromStringImplTempFileHandle() {
497 return fromStringImplTempFileHandle;
498 }
499
500 /**
501 * Sets from string method's temporary file handle.
502 *
503 * @param fromStringImplTempFileHandle from string method's temporary file
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530504 * handle
Bharat saraswalc0e04842016-05-12 13:16:57 +0530505 */
506 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
507 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
508 }
509
510 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530511 * Creates an instance of temporary java code fragment.
512 *
513 * @param javaFileInfo generated java file information
514 * @throws IOException when fails to create new file handle
515 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530516 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
517 throws IOException {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530518 setJavaExtendsListHolder(new JavaExtendsListHolder());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530519 setJavaImportData(new JavaImportData());
520 setJavaFileInfo(javaFileInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530521 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
522 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530523
524 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530525 * Initialize getter when generation file type matches to interface
526 * mask.
527 */
528 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
529 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
530 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530531
532 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530533 * Initialize getter and setter when generation file type matches to
534 * builder interface mask.
535 */
536 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
537 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
538 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
539 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530540
541 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530542 * Initialize getterImpl, setterImpl and attributes when generation file
543 * type matches to builder class mask.
544 */
545 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
546 addGeneratedTempFile(ATTRIBUTES_MASK);
547 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
548 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530549 addGeneratedTempFile(AUGMENTE_CLASS_CONSTRUCTOR_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530550 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530551
552 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530553 * Initialize getterImpl, attributes, constructor, hash code, equals and
554 * to strings when generation file type matches to impl class mask.
555 */
556 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
557 addGeneratedTempFile(ATTRIBUTES_MASK);
558 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530559 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
560 addGeneratedTempFile(EQUALS_IMPL_MASK);
561 addGeneratedTempFile(TO_STRING_IMPL_MASK);
562 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530563
564 /*
565 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530566 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530567 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530568 addGeneratedTempFile(ATTRIBUTES_MASK);
569 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
570 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
571 addGeneratedTempFile(EQUALS_IMPL_MASK);
572 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530573 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530574 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530575
576 /*
Bharat saraswalc0e04842016-05-12 13:16:57 +0530577 * Initialize temp files to generate enum class.
578 */
579 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
580 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
581 }
582 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530583 * Initialize getter and setter when generation file type matches to
584 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530585 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530586 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
587 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
588 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530589 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530590 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530591 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530592
593 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530594 * Set temporary file handles.
595 */
596 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
597 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
598 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530599
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530600 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
601 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
602 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530603
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530604 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
605 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
606 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530607
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530608 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
609 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
610 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530611
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530612 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
613 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
614 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530615
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530616 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
617 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
618 }
619 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
620 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
621 }
622 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
623 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
624 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530625 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
626 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530627 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530628 if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) {
629 setAugmentConstructorImplTempFileHandle(getTemporaryFileHandle(AUGMENT_CONSTRUCTOR_FILE_NAME));
630 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530631
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530632 }
633
634 /**
635 * Returns java file handle for interface file.
636 *
637 * @return java file handle for interface file
638 */
639 private File getInterfaceJavaFileHandle() {
640 return interfaceJavaFileHandle;
641 }
642
643 /**
644 * Sets the java file handle for interface file.
645 *
646 * @param interfaceJavaFileHandle java file handle
647 */
648 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
649 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
650 }
651
652 /**
653 * Returns java file handle for builder interface file.
654 *
655 * @return java file handle for builder interface file
656 */
657 private File getBuilderInterfaceJavaFileHandle() {
658 return builderInterfaceJavaFileHandle;
659 }
660
661 /**
662 * Sets the java file handle for builder interface file.
663 *
664 * @param builderInterfaceJavaFileHandle java file handle
665 */
666 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
667 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
668 }
669
670 /**
671 * Returns java file handle for builder class file.
672 *
673 * @return java file handle for builder class file
674 */
675 private File getBuilderClassJavaFileHandle() {
676 return builderClassJavaFileHandle;
677 }
678
679 /**
680 * Sets the java file handle for builder class file.
681 *
682 * @param builderClassJavaFileHandle java file handle
683 */
684 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
685 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
686 }
687
688 /**
689 * Returns java file handle for impl class file.
690 *
691 * @return java file handle for impl class file
692 */
693 private File getImplClassJavaFileHandle() {
694 return implClassJavaFileHandle;
695 }
696
697 /**
698 * Sets the java file handle for impl class file.
699 *
700 * @param implClassJavaFileHandle java file handle
701 */
702 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
703 this.implClassJavaFileHandle = implClassJavaFileHandle;
704 }
705
706 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530707 * Returns attribute's temporary file handle.
708 *
709 * @return temporary file handle
710 */
711 public File getAttributesTempFileHandle() {
712 return attributesTempFileHandle;
713 }
714
715 /**
716 * Sets attribute's temporary file handle.
717 *
718 * @param attributeForClass file handle for attribute
719 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530720 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530721 attributesTempFileHandle = attributeForClass;
722 }
723
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530724 /**
725 * Returns getter method's impl's temporary file handle.
726 *
727 * @return temporary file handle
728 */
729 public File getGetterImplTempFileHandle() {
730 return getterImplTempFileHandle;
731 }
732
733 /**
734 * Sets to getter method's impl's temporary file handle.
735 *
736 * @param getterImpl file handle for to getter method's impl
737 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530738 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530739 getterImplTempFileHandle = getterImpl;
740 }
741
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530742 /**
743 * Returns hash code method's temporary file handle.
744 *
745 * @return temporary file handle
746 */
747 public File getHashCodeImplTempFileHandle() {
748 return hashCodeImplTempFileHandle;
749 }
750
751 /**
752 * Sets hash code method's temporary file handle.
753 *
754 * @param hashCodeMethod file handle for hash code method
755 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530756 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530757 hashCodeImplTempFileHandle = hashCodeMethod;
758 }
759
760 /**
761 * Returns equals mehtod's temporary file handle.
762 *
763 * @return temporary file handle
764 */
765 public File getEqualsImplTempFileHandle() {
766 return equalsImplTempFileHandle;
767 }
768
769 /**
770 * Sets equals method's temporary file handle.
771 *
772 * @param equalsMethod file handle for to equals method
773 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530774 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530775 equalsImplTempFileHandle = equalsMethod;
776 }
777
778 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530779 * Returns to string method's temporary file handle.
780 *
781 * @return temporary file handle
782 */
783 public File getToStringImplTempFileHandle() {
784 return toStringImplTempFileHandle;
785 }
786
787 /**
788 * Sets to string method's temporary file handle.
789 *
790 * @param toStringMethod file handle for to string method
791 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530792 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530793 toStringImplTempFileHandle = toStringMethod;
794 }
795
796 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530797 * Returns java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530798 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530799 * @return java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530800 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530801 public JavaExtendsListHolder getJavaExtendsListHolder() {
802 return javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530803 }
804
805 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530806 * Sets java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530807 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530808 * @param javaExtendsListHolder java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530809 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530810 public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
811 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530812 }
813
814 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530815 * Returns augments copy constructor method impl class file.
816 *
817 * @return augments copy constructor method impl class file
818 */
819 public File getAugmentConstructorImplTempFileHandle() {
820 return augmentConstructorImplTempFileHandle;
821 }
822
823 /**
824 * Sets augments copy constructor method impl class.
825 *
826 * @param augmentConstructorImplTempFileHandle augments copy constructor method impl class file
827 */
828 public void setAugmentConstructorImplTempFileHandle(File augmentConstructorImplTempFileHandle) {
829 this.augmentConstructorImplTempFileHandle = augmentConstructorImplTempFileHandle;
830 }
831
832 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530833 * Adds attribute for class.
834 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530835 * @param attr attribute info
836 * @param yangPluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530837 * @throws IOException when fails to append to temporary file
838 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530839 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530840 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530841 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
842 + FOUR_SPACE_INDENTATION);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530843 }
844
845 /**
846 * Adds getter for interface.
847 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530848 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530849 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530850 * @throws IOException when fails to append to temporary file
851 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530852 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530853 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530854 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530855 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530856 }
857
858 /**
859 * Adds setter for interface.
860 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530861 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530862 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530863 * @throws IOException when fails to append to temporary file
864 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530865 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530866 throws IOException {
867 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530868 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
869 + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530870 }
871
872 /**
873 * Adds setter's implementation for class.
874 *
875 * @param attr attribute info
876 * @throws IOException when fails to append to temporary file
877 */
878 private void addSetterImpl(JavaAttributeInfo attr)
879 throws IOException {
880 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530881 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
882 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530883 NEW_LINE);
884 }
885
886 /**
887 * Adds getter method's impl for class.
888 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530889 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530890 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530891 * @throws IOException when fails to append to temporary file
892 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530893 private void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530894 throws IOException {
895 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
896 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
897 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
898 getGeneratedJavaFiles()) + NEW_LINE);
899 } else {
janani b4a6711a2016-05-17 13:12:22 +0530900 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530901 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b4a6711a2016-05-17 13:12:22 +0530902 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530903 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530904 }
905
906 /**
907 * Adds build method for interface.
908 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530909 * @param pluginConfig plugin configurations
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530910 * @return build method for interface
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530911 * @throws IOException when fails to append to temporary file
912 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530913 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530914 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530915 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530916 }
917
918 /**
919 * Adds build method's implementation for class.
920 *
921 * @return build method implementation for class
922 * @throws IOException when fails to append to temporary file
923 */
924 String addBuildMethodImpl()
925 throws IOException {
926 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
927 }
928
929 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530930 * Adds default constructor for class.
931 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530932 * @param modifier modifier for constructor.
933 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530934 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530935 * @return default constructor for class
936 * @throws IOException when fails to append to file
937 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530938 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530939 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530940 return NEW_LINE
941 + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530942 }
943
944 /**
945 * Adds default constructor for class.
946 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530947 * @param pluginCnfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530948 * @return default constructor for class
949 * @throws IOException when fails to append to file
950 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530951 public String addOfMethod(YangPluginConfig pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530952 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530953 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false, pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530954 + getOfMethod(getGeneratedJavaClassName(), null);
955 }
956
957 /**
958 * Adds hash code method for class.
959 *
960 * @param attr attribute info
961 * @throws IOException when fails to append to temporary file
962 */
963 private void addHashCodeMethod(JavaAttributeInfo attr)
964 throws IOException {
965 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
966 }
967
968 /**
969 * Adds equals method for class.
970 *
971 * @param attr attribute info
972 * @throws IOException when fails to append to temporary file
973 */
974 private void addEqualsMethod(JavaAttributeInfo attr)
975 throws IOException {
976 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
977 }
978
979 /**
980 * Adds ToString method for class.
981 *
982 * @param attr attribute info
983 * @throws IOException when fails to append to temporary file
984 */
985 private void addToStringMethod(JavaAttributeInfo attr)
986 throws IOException {
987 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
988 }
989
990 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530991 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530992 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530993 * @param javaAttributeInfo type attribute info
Bharat saraswalc0e04842016-05-12 13:16:57 +0530994 * @param fromStringAttributeInfo from string attribute info
995 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530996 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530997 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530998 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530999 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301000 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
1001 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301002 }
1003
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301004 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301005 * Adds constructor for augment class.
1006 *
1007 * @param attr attribute info
1008 * @throws IOException when fails to append to temporary file
1009 */
1010 private void addAugmentConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
1011 throws IOException {
1012 appendToFile(getAugmentConstructorImplTempFileHandle(), getAugmentedConstructor(attr, pluginConfig));
1013 }
1014
1015 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301016 * Returns a temporary file handle for the specific file type.
1017 *
1018 * @param fileName file name
1019 * @return temporary file handle
1020 * @throws IOException when fails to create new file handle
1021 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301022 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301023 throws IOException {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301024 String path = getTempDirPath(getAbsoluteDirPath());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301025 File dir = new File(path);
1026 if (!dir.exists()) {
1027 dir.mkdirs();
1028 }
1029 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1030 if (!file.exists()) {
1031 file.createNewFile();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301032 } else {
1033 throw new IOException(fileName + " is reused due to YANG naming");
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301034 }
1035 return file;
1036 }
1037
1038 /**
1039 * Returns a temporary file handle for the specific file type.
1040 *
1041 * @param fileName file name
1042 * @return temporary file handle
1043 * @throws IOException when fails to create new file handle
1044 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301045 File getJavaFileHandle(String fileName)
1046 throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301047 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1048 }
1049
1050 /**
1051 * Returns data from the temporary files.
1052 *
1053 * @param file temporary file handle
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301054 * @param absolutePath absolute path
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301055 * @return stored data from temporary files
1056 * @throws IOException when failed to get data from the given file
1057 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301058 public String getTemporaryDataFromFileHandle(File file, String absolutePath)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301059 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301060
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301061 String path = getTempDirPath(absolutePath);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301062 if (new File(path + file.getName()).exists()) {
1063 return readAppendFile(path + file.getName(), EMPTY_STRING);
1064 } else {
1065 throw new IOException("Unable to get data from the given "
1066 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1067 }
1068 }
1069
1070 /**
1071 * Returns temporary directory path.
1072 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301073 * @param absolutePath absolute path
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301074 * @return directory path
1075 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301076 String getTempDirPath(String absolutePath) {
1077 return getPackageDirPathFromJavaJPackage(absolutePath) + SLASH + getGeneratedJavaClassName()
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301078 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1079 }
1080
1081 /**
1082 * Parses attribute to get the attribute string.
1083 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301084 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301085 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301086 * @return attribute string
1087 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301088 public String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301089 /*
1090 * TODO: check if this utility needs to be called or move to the caller
1091 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301092 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301093 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301094 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1095 attr.getImportInfo().getClassInfo(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301096 attributeName, attr.isListAttr());
1097 } else {
1098 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1099 attr.isListAttr());
1100 }
1101 }
1102
1103 /**
1104 * Appends content to temporary file.
1105 *
1106 * @param file temporary file
1107 * @param data data to be appended
1108 * @throws IOException when fails to append to file
1109 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301110 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301111 throws IOException {
1112 try {
1113 insertDataIntoJavaFile(file, data);
1114 } catch (IOException ex) {
1115 throw new IOException("failed to write in temp file.");
1116 }
1117 }
1118
1119 /**
1120 * Adds current node info as and attribute to the parent generated file.
1121 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301122 * @param curNode current node which needs to be added as an attribute in
1123 * the parent generated code
1124 * @param isList is list construct
Bharat saraswal33dfa012016-05-17 19:59:16 +05301125 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301126 * @throws IOException IO operation exception
1127 */
1128 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301129 boolean isList, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301130 throws IOException {
1131 YangNode parent = getParentNodeInGenCode(curNode);
1132 if (!(parent instanceof JavaCodeGenerator)) {
1133 throw new TranslatorException("missing parent node to contain current node info in generated file");
1134 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301135
1136 if (parent instanceof YangJavaGrouping) {
1137 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +05301138 * In case of grouping, there is no need to add the information, it
1139 * will be taken care in uses
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301140 */
1141 return;
Bharat saraswalcad0e652016-05-26 23:48:38 +05301142 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301143
1144 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301145 parent, isList);
1146 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1147 throw new TranslatorException("missing parent temp file handle");
1148 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301149 getNodesInterfaceFragmentFiles(parent)
Bharat saraswal33dfa012016-05-17 19:59:16 +05301150 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301151 }
1152
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301153 /**
1154 * Adds current node info as and attribute to the parent generated file.
1155 *
1156 * @param curNode current node which needs to be added as an attribute in
1157 * the parent generated code
1158 * @param pluginConfig plugin configurations
1159 * @param targetNode target node to add the attribute
1160 * @throws IOException IO operation exception
1161 */
1162 public static void addCurNodeAsAttributeInTargetTempFile(YangNode curNode,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301163 YangPluginConfig pluginConfig, YangNode targetNode)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301164 throws IOException {
1165
1166 if (!(targetNode instanceof JavaCodeGenerator)) {
1167 throw new TranslatorException("invalid target node to generated file");
1168 }
1169
1170 if (targetNode instanceof YangJavaGrouping) {
1171 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +05301172 * In case of grouping, there is no need to add the information, it
1173 * will be taken care in uses
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301174 */
1175 return;
1176 }
1177
1178 boolean isList = curNode instanceof YangList;
1179
1180 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInTarget(curNode,
1181 targetNode, isList);
1182 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
1183 throw new TranslatorException("missing target node's temp file handle");
1184 }
1185 getNodesInterfaceFragmentFiles(targetNode)
1186 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
1187 }
1188
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301189 /**
1190 * Creates an attribute info object corresponding to a data model node and
1191 * return it.
1192 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301193 * @param curNode current data model node for which the java code generation
1194 * is being handled
1195 * @param targetNode target node in which the current node is an attribute
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301196 * @param isListNode is the current added attribute needs to be a list
1197 * @return AttributeInfo attribute details required to add in temporary
1198 * files
1199 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301200 public static JavaAttributeInfo getCurNodeAsAttributeInTarget(YangNode curNode,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301201 YangNode targetNode, boolean isListNode) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301202 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
1203 if (curNodeName == null) {
1204 updateJavaFileInfo(curNode, null);
1205 curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301206 }
Bharat saraswalcad0e652016-05-26 23:48:38 +05301207
1208 /*
1209 * Get the import info corresponding to the attribute for import in
1210 * generated java files or qualified access
1211 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301212 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
1213 getCapitalCase(curNodeName));
1214 if (!(targetNode instanceof TempJavaCodeFragmentFilesContainer)) {
1215 throw new TranslatorException("Parent node does not have file info");
1216 }
1217 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(targetNode);
1218 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
1219 JavaFileInfo fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
Bharat saraswalcad0e652016-05-26 23:48:38 +05301220
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301221 boolean isQualified;
1222 if ((targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule)
1223 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE)
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301224 || qualifiedTypeInfo.getClassInfo().contentEquals(COMPONENT)
1225 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE))
1226 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(DEACTIVATE))
1227 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE_CARDINALITY)
1228 || qualifiedTypeInfo.getClassInfo().contentEquals(REFERENCE))
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301229 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))
1230 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + MANAGER))) {
1231
1232 isQualified = true;
Bharat saraswalcad0e652016-05-26 23:48:38 +05301233 } else {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301234 String className;
1235 if (targetNode instanceof YangJavaModule || targetNode instanceof YangJavaSubModule) {
1236 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
1237 } else {
1238 className = getCapitalCase(fileInfo.getJavaName());
1239 }
1240
1241 isQualified = parentImportData.addImportInfo(qualifiedTypeInfo,
1242 className, fileInfo.getPackage());
Bharat saraswalcad0e652016-05-26 23:48:38 +05301243 }
1244
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301245 if (isListNode) {
1246 parentImportData.setIfListImported(true);
Bharat saraswalc0e04842016-05-12 13:16:57 +05301247 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301248
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301249 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1250 }
1251
Bharat saraswalc0e04842016-05-12 13:16:57 +05301252 /**
1253 * Returns interface fragment files for node.
1254 *
1255 * @param node YANG node
1256 * @return interface fragment files for node
1257 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301258 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1259 TempJavaFragmentFiles tempJavaFragmentFiles;
1260 if (node instanceof RpcNotificationContainer) {
1261 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1262 .getTempJavaCodeFragmentFiles()
1263 .getServiceTempFiles();
1264 } else {
1265 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1266 .getTempJavaCodeFragmentFiles()
1267 .getBeanTempFiles();
1268 }
1269 return tempJavaFragmentFiles;
1270 }
1271
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301272 /**
1273 * Adds parent's info to current node import list.
1274 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301275 * @param curNode current node for which import list needs to be updated
Bharat saraswal33dfa012016-05-17 19:59:16 +05301276 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301277 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301278 public void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301279 caseImportInfo = new JavaQualifiedTypeInfo();
1280 YangNode parent = getParentNodeInGenCode(curNode);
1281 if (!(parent instanceof JavaCodeGenerator)) {
1282 throw new TranslatorException("missing parent node to contain current node info in generated file");
1283 }
1284 if (!(curNode instanceof JavaFileInfoContainer)) {
1285 throw new TranslatorException("missing java file information to get the package details "
1286 + "of attribute corresponding to child node");
1287 }
Bharat saraswal33dfa012016-05-17 19:59:16 +05301288 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1289 pluginConfig.getConflictResolver())));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301290 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301291
1292 JavaFileInfo fileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1293
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301294 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301295 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301296 getCapitalCase(fileInfo.getJavaName()), fileInfo.getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301297 }
1298
1299 /**
1300 * Adds leaf attributes in generated files.
1301 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301302 * @param listOfLeaves list of YANG leaf
1303 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301304 * @throws IOException IO operation fail
1305 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301306 public void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301307 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301308 throws IOException {
1309 if (listOfLeaves != null) {
1310 for (YangLeaf leaf : listOfLeaves) {
1311 if (!(leaf instanceof JavaLeafInfoContainer)) {
1312 throw new TranslatorException("Leaf does not have java information");
1313 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301314 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeaf(leaf, yangPluginConfig),
1315 yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301316 }
1317 }
1318 }
1319
1320 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301321 * Returns java attribute for leaf.
1322 *
1323 * @param leaf YANG leaf
1324 * @param yangPluginConfig plugin configurations
1325 * @return java attribute for leaf
1326 */
1327 private JavaAttributeInfo getJavaAttributeOfLeaf(YangLeaf leaf,
1328 YangPluginConfig yangPluginConfig) {
1329 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
1330 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
1331 javaLeaf.updateJavaQualifiedInfo();
1332 return getAttributeInfoForTheData(
1333 javaLeaf.getJavaQualifiedInfo(),
1334 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1335 javaLeaf.getDataType(),
1336 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1337 false);
1338 }
1339
1340 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301341 * Adds leaf list's attributes in generated files.
1342 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301343 * @param listOfLeafList list of YANG leaves
1344 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301345 * @throws IOException IO operation fail
1346 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301347 public void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301348 throws IOException {
1349 if (listOfLeafList != null) {
1350 for (YangLeafList leafList : listOfLeafList) {
1351 if (!(leafList instanceof JavaLeafInfoContainer)) {
1352 throw new TranslatorException("Leaf-list does not have java information");
1353 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301354 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeOfLeafList(leafList, yangPluginConfig),
1355 yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301356 }
1357 }
1358 }
1359
1360 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301361 * Returns java attribute for leaf-list.
1362 *
1363 * @param leafList YANG leaf-list
1364 * @param yangPluginConfig plugin configurations
1365 * @return java attribute for leaf-list
1366 */
1367 private JavaAttributeInfo getJavaAttributeOfLeafList(YangLeafList leafList, YangPluginConfig yangPluginConfig) {
1368 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
1369 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
1370 javaLeaf.updateJavaQualifiedInfo();
1371 getJavaImportData().setIfListImported(true);
1372 return getAttributeInfoForTheData(
1373 javaLeaf.getJavaQualifiedInfo(),
1374 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1375 javaLeaf.getDataType(),
1376 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1377 true);
1378 }
1379
1380 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301381 * Adds all the leaves in the current data model node as part of the
1382 * generated temporary file.
1383 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301384 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301385 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301386 * @throws IOException IO operation fail
1387 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301388 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301389 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-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;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301395 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1396 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301397 }
1398
1399 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301400 * Adds the new attribute info to the target generated temporary files.
1401 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301402 * @param newAttrInfo the attribute info that needs to be added to temporary
1403 * files
Bharat saraswal33dfa012016-05-17 19:59:16 +05301404 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301405 * @throws IOException IO operation fail
1406 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301407 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301408 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301409 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301410 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301411 addAttribute(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() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301415 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301416 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301417
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301418 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301419 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301420 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301421
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301422 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1423 addSetterImpl(newAttrInfo);
1424 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301425
1426 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301427 addGetterImpl(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301428 }
1429 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1430 addHashCodeMethod(newAttrInfo);
1431 }
1432 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1433 addEqualsMethod(newAttrInfo);
1434 }
1435 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1436 addToStringMethod(newAttrInfo);
1437 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301438
1439 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Bharat saraswal96dfef02016-06-16 00:29:12 +05301440 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
1441 pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +05301442 /*
1443 * Create a new java attribute info with qualified information of
1444 * wrapper classes.
1445 */
1446 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1447 newAttrInfo.getAttributeName(),
1448 newAttrInfo.getAttributeType(),
1449 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1450
1451 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301452 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301453 }
1454
1455 /**
1456 * Returns java class name.
1457 *
1458 * @param suffix for the class name based on the file type
1459 * @return java class name
1460 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301461 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301462 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301463 }
1464
1465 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301466 * Returns java class name.
1467 *
1468 * @return java class name
1469 */
1470 String getImplClassName() {
1471 return getCapitalCase(DEFAULT) + getCapitalCase(getJavaFileInfo().getJavaName());
1472 }
1473
1474 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301475 * Returns the directory path.
1476 *
1477 * @return directory path
1478 */
1479 private String getDirPath() {
1480 return getJavaFileInfo().getPackageFilePath();
1481 }
1482
1483 /**
1484 * Constructs java code exit.
1485 *
1486 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301487 * @param curNode current YANG node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301488 * @throws IOException when fails to generate java files
1489 */
1490 public void generateJavaFile(int fileType, YangNode curNode)
1491 throws IOException {
1492 List<String> imports = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +05301493 imports = getJavaImportData().getImports();
1494
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301495 JavaFileInfo curInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1496
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301497 createPackage(curNode);
1498
1499 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301500 * Generate java code.
1501 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301502 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1503 BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301504
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301505 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301506 * Create interface file.
1507 */
1508 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1509 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301510 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1511 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301512 * Create builder interface file.
1513 */
1514 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1515 setBuilderInterfaceJavaFileHandle(
1516 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1517 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301518 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1519 isAttributePresent()));
1520 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301521 * Append builder interface file to interface file and close it.
1522 */
1523 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
Bharat saraswal96dfef02016-06-16 00:29:12 +05301524 validateLineLength(getInterfaceJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301525 }
1526 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301527
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301528 if (curNode instanceof YangCase) {
1529 removeCaseImport(imports);
1530 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301531 if (curNode instanceof YangJavaAugment) {
1532 updateAugmentConstructorTempFile(curNode, curInfo.getPluginConfig());
1533 YangJavaAugment augment = (YangJavaAugment) curNode;
1534 List<JavaQualifiedTypeInfo> infoList = augment.getExtendedClassInfo();
1535 for (JavaQualifiedTypeInfo info : infoList) {
1536 if (info.getClassInfo()
1537 .equals(getCapitalCase(getCamelCase(augment.getAugmentedNode().getName(), null)))) {
1538 removeAugmentedImport(imports, info);
1539 }
1540
1541 }
1542 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301543 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301544 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301545 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301546 addImportsToStringAndHasCodeMethods(curNode, imports);
1547 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301548 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301549 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301550 * Create impl class file.
1551 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301552 setImplClassJavaFileHandle(getJavaFileHandle(getImplClassName()));
1553 setImplClassJavaFileHandle(
1554 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent(), imports));
1555
1556 /*
1557 * Create builder class file.
1558 */
1559 if ((fileType & BUILDER_CLASS_MASK) != 0) {
1560 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1561 setBuilderClassJavaFileHandle(
1562 generateBuilderClassFile(getBuilderClassJavaFileHandle(), curNode,
1563 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301564 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301565 * Append impl class to builder class and close it.
1566 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301567 mergeJavaFiles(getBuilderClassJavaFileHandle(), getImplClassJavaFileHandle());
1568 validateLineLength(getImplClassJavaFileHandle());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301569 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301570 insertDataIntoJavaFile(getImplClassJavaFileHandle(), getJavaClassDefClose());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301571 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301572 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301573 * Close all the file handles.
1574 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301575 freeTemporaryResources(false);
1576 }
1577
1578 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301579 * Updates augment constructor temp file.
1580 *
1581 * @param curNode current augment node
1582 * @param pluginConfig plugin configurations
1583 * @throws IOException when fails to do IO operations
1584 */
1585 private void updateAugmentConstructorTempFile(YangNode curNode, YangPluginConfig pluginConfig)
1586 throws IOException {
1587 YangJavaAugment augment = (YangJavaAugment) curNode;
1588
1589 YangNode augmentedNode = augment.getAugmentedNode();
1590 if (augmentedNode instanceof YangLeavesHolder) {
1591 YangLeavesHolder holder = (YangLeavesHolder) augmentedNode;
1592 if (holder.getListOfLeaf() != null) {
1593 for (YangLeaf leaf : holder.getListOfLeaf()) {
1594 addAugmentConstructor(getJavaAttributeOfLeaf(leaf,
1595 pluginConfig), pluginConfig);
1596 }
1597
1598 }
1599 if (holder.getListOfLeafList() != null) {
1600 for (YangLeafList leafList : holder.getListOfLeafList()) {
1601 addAugmentConstructor(getJavaAttributeOfLeafList(leafList,
1602 pluginConfig), pluginConfig);
1603 }
1604
1605 }
1606 }
1607 augmentedNode = augmentedNode.getChild();
1608 boolean isList = false;
1609 while (augmentedNode != null) {
1610 if (augmentedNode instanceof YangList) {
1611 isList = true;
1612 }
1613 addAugmentConstructor(getCurNodeAsAttributeInTarget(augmentedNode, augment, isList), pluginConfig);
1614 augmentedNode = augmentedNode.getNextSibling();
1615 }
1616
1617 }
1618
1619 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301620 * Adds imports for ToString and HashCodeMethod.
1621 *
1622 * @param curNode current YANG node
1623 * @param imports import list
1624 * @return import list
1625 */
1626 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1627 imports.add(getJavaImportData().getImportForHashAndEquals());
1628 imports.add(getJavaImportData().getImportForToString());
1629 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301630 }
1631
1632 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301633 * Removes augmented node import info from import list.
1634 *
1635 * @param imports list of imports
1636 * @return import for class
1637 */
1638 private List<String> removeAugmentedImport(List<String> imports, JavaQualifiedTypeInfo augmentedInfo) {
1639 String augmentedNodeImport = null;
1640 if (imports != null && augmentedInfo != null) {
1641 augmentedNodeImport = IMPORT + augmentedInfo.getPkgInfo() + PERIOD + getCapitalCase(DEFAULT)
1642 + augmentedInfo.getClassInfo() + PERIOD + augmentedInfo.getClassInfo()
1643 + BUILDER + SEMI_COLAN + NEW_LINE;
1644 imports.remove(augmentedNodeImport);
1645 augmentedNodeImport = IMPORT + augmentedInfo.getPkgInfo() + PERIOD
1646 + augmentedInfo.getClassInfo() + PERIOD + augmentedInfo.getClassInfo()
1647 + BUILDER + SEMI_COLAN + NEW_LINE;
1648 imports.remove(augmentedNodeImport);
1649 }
1650 return imports;
1651 }
1652
1653 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301654 * Removes case import info from import list.
1655 *
1656 * @param imports list of imports
1657 * @return import for class
1658 */
1659 private List<String> removeCaseImport(List<String> imports) {
1660 if (imports != null && caseImportInfo != null) {
1661 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1662 SEMI_COLAN + NEW_LINE;
1663 imports.remove(caseImport);
1664 }
1665 return imports;
1666 }
1667
1668 /**
1669 * Removes all temporary file handles.
1670 *
1671 * @param isErrorOccurred when translator fails to generate java files we
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301672 * need to close all open file handles include temporary files
1673 * and java files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301674 * @throws IOException when failed to delete the temporary files
1675 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301676 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301677 throws IOException {
1678 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301679 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301680 * Close all java file handles and when error occurs delete the files.
1681 */
1682 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1683 closeFile(getInterfaceJavaFileHandle(), isError);
1684 }
1685 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301686 closeFile(getBuilderClassJavaFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301687 }
1688 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1689 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1690 }
1691 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301692 closeFile(getImplClassJavaFileHandle(), isError);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301693 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301694
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301695 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301696 * Close all temporary file handles and delete the files.
1697 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301698 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1699 closeFile(getGetterImplTempFileHandle(), true);
1700 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301701 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1702 closeFile(getAttributesTempFileHandle(), true);
1703 }
1704 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1705 closeFile(getHashCodeImplTempFileHandle(), true);
1706 }
1707 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1708 closeFile(getToStringImplTempFileHandle(), true);
1709 }
1710 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1711 closeFile(getEqualsImplTempFileHandle(), true);
1712 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301713 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1714 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301715 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301716 if ((getGeneratedTempFiles() & AUGMENTE_CLASS_CONSTRUCTOR_MASK) != 0) {
1717 closeFile(getAugmentConstructorImplTempFileHandle(), true);
1718 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301719 }
1720
1721 /**
1722 * Returns if the attribute needs to be accessed in a qualified manner or
1723 * not, if it needs to be imported, then the same needs to be done.
1724 *
1725 * @param importInfo import info for the current attribute being added
1726 * @return status of the qualified access to the attribute
1727 */
1728 public boolean getIsQualifiedAccessOrAddToImportList(
1729 JavaQualifiedTypeInfo importInfo) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +05301730
1731 return getJavaImportData().addImportInfo(importInfo, getGeneratedJavaClassName(),
1732 getJavaFileInfo().getPackage());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301733 }
1734
1735 /**
1736 * Checks if the import info is same as the package of the current generated
1737 * java file.
1738 *
1739 * @param importInfo import info for an attribute
1740 * @return true if the import info is same as the current nodes package
1741 * false otherwise
1742 */
1743 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1744 return getJavaFileInfo().getPackage()
1745 .contentEquals(importInfo.getPkgInfo());
1746 }
1747}