blob: 0820c0724166e69925534b8bcbf779acf9b4553e [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.yangutils.translator.tojava;
17
18import java.io.File;
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.List;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053022
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053023import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053024import org.onosproject.yangutils.datamodel.YangCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangLeafList;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
28import org.onosproject.yangutils.datamodel.YangNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
30import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswalcad0e652016-05-26 23:48:38 +053031import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
32import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaInput;
33import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
34import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaOutput;
35import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
36import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053037import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053038import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039
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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053048import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053050import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
51import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
52import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
54import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
55import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
56import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053057import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053058import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
61import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
62import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053063import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
64import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
66import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053067import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
69import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Bharat saraswalcad0e652016-05-26 23:48:38 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
73import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswalc0e04842016-05-12 13:16:57 +053074import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
76import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
77import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
78import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
81import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
82import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
84import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053085import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentationHoldersImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053088import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.detectCollisionBwParentAndChildForImport;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053089import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentationHolderExtended;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053090import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
Bharat saraswalc0e04842016-05-12 13:16:57 +053091import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.sortImports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
93import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
94import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
95import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
96import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
97import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
98import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053099import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
100import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
101import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
102import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
103import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530104import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530105import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
106import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
108import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
109import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
110
111/**
112 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530113 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530114 */
115public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530116
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530117 /**
118 * Information about the java files being generated.
119 */
120 private JavaFileInfo javaFileInfo;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530121
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530122 /**
123 * Imported class info.
124 */
125 private JavaImportData javaImportData;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530126
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 /**
128 * The variable which guides the types of temporary files generated using
129 * the temporary generated file types mask.
130 */
131 private int generatedTempFiles;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530132
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 /**
134 * Absolute path where the target java file needs to be generated.
135 */
136 private String absoluteDirPath;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530137
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530138 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530139 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530140 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530141 private JavaExtendsListHolder javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 /**
143 * File type extension for java classes.
144 */
145 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530146
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530147 /**
148 * File type extension for temporary classes.
149 */
150 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530151
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 /**
153 * Folder suffix for temporary files folder.
154 */
155 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530156
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 /**
158 * File name for getter method.
159 */
160 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530161
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530162 /**
163 * File name for setter method.
164 */
165 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166
167 /**
168 * File name for getter method implementation.
169 */
170 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
171
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530172 /**
173 * File name for setter method implementation.
174 */
175 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530176
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530177 /**
178 * File name for attributes.
179 */
180 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530181
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530182 /**
183 * File name for to string method.
184 */
185 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530187 /**
188 * File name for hash code method.
189 */
190 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530191
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 /**
193 * File name for equals method.
194 */
195 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530196
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530197 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530198 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530199 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530200 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530201
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530202 /**
203 * File name for interface java file name suffix.
204 */
205 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530206
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530207 /**
208 * File name for builder interface file name suffix.
209 */
210 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530211
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530212 /**
213 * File name for builder class file name suffix.
214 */
215 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530216
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530217 /**
218 * File name for impl class file name suffix.
219 */
220 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530221
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530222 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530223 * Java file handle for interface file.
224 */
225 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530226
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530227 /**
228 * Java file handle for builder interface file.
229 */
230 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530231
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530232 /**
233 * Java file handle for builder class file.
234 */
235 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530236
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530237 /**
238 * Java file handle for impl class file.
239 */
240 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530242 /**
243 * Temporary file handle for attribute.
244 */
245 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530246
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530247 /**
248 * Temporary file handle for getter of interface.
249 */
250 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530251
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530252 /**
253 * Temporary file handle for setter of interface.
254 */
255 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530256
257 /**
258 * Temporary file handle for getter of class.
259 */
260 private File getterImplTempFileHandle;
261
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530262 /**
263 * Temporary file handle for setter of class.
264 */
265 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530266
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530267 /**
268 * Temporary file handle for hash code method of class.
269 */
270 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530271
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530272 /**
273 * Temporary file handle for equals method of class.
274 */
275 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530276
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530277 /**
278 * Temporary file handle for to string method of class.
279 */
280 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530281
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530282 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530283 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530284 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530285 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530286
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530287 /**
288 * Import info for case.
289 */
290 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530291
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530292 /**
293 * Is attribute added.
294 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530295 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530296
Bharat saraswalc0e04842016-05-12 13:16:57 +0530297 /**
298 * Creates an instance of temp java fragment files.
299 */
Ray Milkey994c4982016-05-11 18:39:39 +0000300 public TempJavaFragmentFiles() {
301 }
302
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530303 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530304 * Retrieves the absolute path where the file needs to be generated.
305 *
306 * @return absolute path where the file needs to be generated
307 */
308 private String getAbsoluteDirPath() {
309 return absoluteDirPath;
310 }
311
312 /**
313 * Sets absolute path where the file needs to be generated.
314 *
315 * @param absoluteDirPath absolute path where the file needs to be
316 * generated.
317 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530318 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530319 this.absoluteDirPath = absoluteDirPath;
320 }
321
322 /**
323 * Sets the generated java file information.
324 *
325 * @param javaFileInfo generated java file information
326 */
327 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
328 this.javaFileInfo = javaFileInfo;
329 }
330
331 /**
332 * Retrieves the generated java file information.
333 *
334 * @return generated java file information
335 */
336 public JavaFileInfo getJavaFileInfo() {
337 return javaFileInfo;
338 }
339
340 /**
341 * Retrieves the generated temp files.
342 *
343 * @return generated temp files
344 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530345 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530346 return generatedTempFiles;
347 }
348
349 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530350 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530351 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530352 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530353 generatedTempFiles = 0;
354 }
355
356 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530357 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530358 *
359 * @param generatedTempFile generated file
360 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530361 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530362 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530363 setGeneratedTempFiles(generatedTempFiles);
364 }
365
366 /**
367 * Sets generated file files.
368 *
369 * @param generatedTempFile generated file
370 */
371 void setGeneratedTempFiles(int fileType) {
372 generatedTempFiles = fileType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530373 }
374
375 /**
376 * Retrieves the generated Java files.
377 *
378 * @return generated Java files
379 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530380 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530381 return getJavaFileInfo().getGeneratedFileTypes();
382 }
383
384 /**
385 * Retrieves the mapped Java class name.
386 *
387 * @return mapped Java class name
388 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530389 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530390 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530391 }
392
393 /**
394 * Retrieves the import data for the generated Java file.
395 *
396 * @return import data for the generated Java file
397 */
398 public JavaImportData getJavaImportData() {
399 return javaImportData;
400 }
401
402 /**
403 * Sets import data for the generated Java file.
404 *
405 * @param javaImportData import data for the generated Java file
406 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530407 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530408 this.javaImportData = javaImportData;
409 }
410
411 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530412 * Retrieves the status of any attributes added.
413 *
414 * @return status of any attributes added
415 */
416 public boolean isAttributePresent() {
417 return isAttributePresent;
418 }
419
420 /**
421 * Sets status of any attributes added.
422 *
423 * @param attributePresent status of any attributes added
424 */
425 public void setAttributePresent(boolean attributePresent) {
426 isAttributePresent = attributePresent;
427 }
428
429 /**
430 * Returns getter methods's temporary file handle.
431 *
432 * @return temporary file handle
433 */
434 public File getGetterInterfaceTempFileHandle() {
435 return getterInterfaceTempFileHandle;
436 }
437
438 /**
439 * Sets to getter method's temporary file handle.
440 *
441 * @param getterForInterface file handle for to getter method
442 */
443 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
444 getterInterfaceTempFileHandle = getterForInterface;
445 }
446
447 /**
448 * Returns setter method's temporary file handle.
449 *
450 * @return temporary file handle
451 */
452 public File getSetterInterfaceTempFileHandle() {
453 return setterInterfaceTempFileHandle;
454 }
455
456 /**
457 * Sets to setter method's temporary file handle.
458 *
459 * @param setterForInterface file handle for to setter method
460 */
461 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
462 setterInterfaceTempFileHandle = setterForInterface;
463 }
464
465 /**
466 * Returns setter method's impl's temporary file handle.
467 *
468 * @return temporary file handle
469 */
470 public File getSetterImplTempFileHandle() {
471 return setterImplTempFileHandle;
472 }
473
474 /**
475 * Sets to setter method's impl's temporary file handle.
476 *
477 * @param setterImpl file handle for to setter method's implementation class
478 */
479 private void setSetterImplTempFileHandle(File setterImpl) {
480 setterImplTempFileHandle = setterImpl;
481 }
482
483 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530484 * Returns from string method's temporary file handle.
485 *
486 * @return from string method's temporary file handle
487 */
488 public File getFromStringImplTempFileHandle() {
489 return fromStringImplTempFileHandle;
490 }
491
492 /**
493 * Sets from string method's temporary file handle.
494 *
495 * @param fromStringImplTempFileHandle from string method's temporary file
496 * handle
497 */
498 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
499 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
500 }
501
502 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530503 * Creates an instance of temporary java code fragment.
504 *
505 * @param javaFileInfo generated java file information
506 * @throws IOException when fails to create new file handle
507 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530508 TempJavaFragmentFiles(JavaFileInfo javaFileInfo) throws IOException {
509 setJavaExtendsListHolder(new JavaExtendsListHolder());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530510 setJavaImportData(new JavaImportData());
511 setJavaFileInfo(javaFileInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530512 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
513 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530514
515 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530516 * Initialize getter when generation file type matches to interface
517 * mask.
518 */
519 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
520 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
521 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530522
523 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530524 * Initialize getter and setter when generation file type matches to
525 * builder interface mask.
526 */
527 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
528 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
529 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
530 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530531
532 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530533 * Initialize getterImpl, setterImpl and attributes when generation file
534 * type matches to builder class mask.
535 */
536 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
537 addGeneratedTempFile(ATTRIBUTES_MASK);
538 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
539 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
540 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530541
542 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530543 * Initialize getterImpl, attributes, constructor, hash code, equals and
544 * to strings when generation file type matches to impl class mask.
545 */
546 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
547 addGeneratedTempFile(ATTRIBUTES_MASK);
548 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530549 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
550 addGeneratedTempFile(EQUALS_IMPL_MASK);
551 addGeneratedTempFile(TO_STRING_IMPL_MASK);
552 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530553
554 /*
555 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530556 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530557 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530558 addGeneratedTempFile(ATTRIBUTES_MASK);
559 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
560 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
561 addGeneratedTempFile(EQUALS_IMPL_MASK);
562 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530563 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530564 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530565
566 /*
Bharat saraswalc0e04842016-05-12 13:16:57 +0530567 * Initialize temp files to generate enum class.
568 */
569 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
570 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
571 }
572 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530573 * Initialize getter and setter when generation file type matches to
574 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530575 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530576 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
577 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
578 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530579 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530580 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530581 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530582
583 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530584 * Set temporary file handles.
585 */
586 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
587 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
588 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530589
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530590 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
591 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
592 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530593
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530594 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
595 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
596 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530597
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530598 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
599 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
600 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530601
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530602 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
603 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
604 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530605
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530606 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
607 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
608 }
609 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
610 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
611 }
612 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
613 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
614 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530615 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
616 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530617 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530618
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530619 }
620
621 /**
622 * Returns java file handle for interface file.
623 *
624 * @return java file handle for interface file
625 */
626 private File getInterfaceJavaFileHandle() {
627 return interfaceJavaFileHandle;
628 }
629
630 /**
631 * Sets the java file handle for interface file.
632 *
633 * @param interfaceJavaFileHandle java file handle
634 */
635 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
636 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
637 }
638
639 /**
640 * Returns java file handle for builder interface file.
641 *
642 * @return java file handle for builder interface file
643 */
644 private File getBuilderInterfaceJavaFileHandle() {
645 return builderInterfaceJavaFileHandle;
646 }
647
648 /**
649 * Sets the java file handle for builder interface file.
650 *
651 * @param builderInterfaceJavaFileHandle java file handle
652 */
653 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
654 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
655 }
656
657 /**
658 * Returns java file handle for builder class file.
659 *
660 * @return java file handle for builder class file
661 */
662 private File getBuilderClassJavaFileHandle() {
663 return builderClassJavaFileHandle;
664 }
665
666 /**
667 * Sets the java file handle for builder class file.
668 *
669 * @param builderClassJavaFileHandle java file handle
670 */
671 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
672 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
673 }
674
675 /**
676 * Returns java file handle for impl class file.
677 *
678 * @return java file handle for impl class file
679 */
680 private File getImplClassJavaFileHandle() {
681 return implClassJavaFileHandle;
682 }
683
684 /**
685 * Sets the java file handle for impl class file.
686 *
687 * @param implClassJavaFileHandle java file handle
688 */
689 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
690 this.implClassJavaFileHandle = implClassJavaFileHandle;
691 }
692
693 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530694 * Returns attribute's temporary file handle.
695 *
696 * @return temporary file handle
697 */
698 public File getAttributesTempFileHandle() {
699 return attributesTempFileHandle;
700 }
701
702 /**
703 * Sets attribute's temporary file handle.
704 *
705 * @param attributeForClass file handle for attribute
706 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530707 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530708 attributesTempFileHandle = attributeForClass;
709 }
710
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530711 /**
712 * Returns getter method's impl's temporary file handle.
713 *
714 * @return temporary file handle
715 */
716 public File getGetterImplTempFileHandle() {
717 return getterImplTempFileHandle;
718 }
719
720 /**
721 * Sets to getter method's impl's temporary file handle.
722 *
723 * @param getterImpl file handle for to getter method's impl
724 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530725 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530726 getterImplTempFileHandle = getterImpl;
727 }
728
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530729 /**
730 * Returns hash code method's temporary file handle.
731 *
732 * @return temporary file handle
733 */
734 public File getHashCodeImplTempFileHandle() {
735 return hashCodeImplTempFileHandle;
736 }
737
738 /**
739 * Sets hash code method's temporary file handle.
740 *
741 * @param hashCodeMethod file handle for hash code method
742 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530743 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530744 hashCodeImplTempFileHandle = hashCodeMethod;
745 }
746
747 /**
748 * Returns equals mehtod's temporary file handle.
749 *
750 * @return temporary file handle
751 */
752 public File getEqualsImplTempFileHandle() {
753 return equalsImplTempFileHandle;
754 }
755
756 /**
757 * Sets equals method's temporary file handle.
758 *
759 * @param equalsMethod file handle for to equals method
760 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530761 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530762 equalsImplTempFileHandle = equalsMethod;
763 }
764
765 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530766 * Returns to string method's temporary file handle.
767 *
768 * @return temporary file handle
769 */
770 public File getToStringImplTempFileHandle() {
771 return toStringImplTempFileHandle;
772 }
773
774 /**
775 * Sets to string method's temporary file handle.
776 *
777 * @param toStringMethod file handle for to string method
778 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530779 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530780 toStringImplTempFileHandle = toStringMethod;
781 }
782
783 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530784 * Returns java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530785 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530786 * @return java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530787 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530788 public JavaExtendsListHolder getJavaExtendsListHolder() {
789 return javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530790 }
791
792 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530793 * Sets java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530794 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530795 * @param javaExtendsListHolder java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530796 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530797 public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
798 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530799 }
800
801 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530802 * Adds attribute for class.
803 *
804 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530805 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530806 * @throws IOException when fails to append to temporary file
807 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530808 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530809 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530810 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
811 + FOUR_SPACE_INDENTATION);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530812 }
813
814 /**
815 * Adds getter for interface.
816 *
817 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530818 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530819 * @throws IOException when fails to append to temporary file
820 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530821 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530822 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530823 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530824 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530825 }
826
827 /**
828 * Adds setter for interface.
829 *
830 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530831 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530832 * @throws IOException when fails to append to temporary file
833 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530834 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530835 throws IOException {
836 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530837 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
838 + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530839 }
840
841 /**
842 * Adds setter's implementation for class.
843 *
844 * @param attr attribute info
845 * @throws IOException when fails to append to temporary file
846 */
847 private void addSetterImpl(JavaAttributeInfo attr)
848 throws IOException {
849 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530850 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
851 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530852 NEW_LINE);
853 }
854
855 /**
856 * Adds getter method's impl for class.
857 *
858 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530859 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530860 * @throws IOException when fails to append to temporary file
861 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530862 private void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530863 throws IOException {
864 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
865 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
866 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
867 getGeneratedJavaFiles()) + NEW_LINE);
868 } else {
janani b4a6711a2016-05-17 13:12:22 +0530869 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530870 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b4a6711a2016-05-17 13:12:22 +0530871 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530872 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530873 }
874
875 /**
876 * Adds build method for interface.
877 *
878 * @return build method for interface
Bharat saraswal33dfa012016-05-17 19:59:16 +0530879 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530880 * @throws IOException when fails to append to temporary file
881 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530882 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530883 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530884 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530885 }
886
887 /**
888 * Adds build method's implementation for class.
889 *
890 * @return build method implementation for class
891 * @throws IOException when fails to append to temporary file
892 */
893 String addBuildMethodImpl()
894 throws IOException {
895 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
896 }
897
898 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530899 * Adds default constructor for class.
900 *
901 * @param modifier modifier for constructor.
902 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530903 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530904 * @return default constructor for class
905 * @throws IOException when fails to append to file
906 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530907 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530908 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530909 return NEW_LINE
910 + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530911 }
912
913 /**
914 * Adds default constructor for class.
915 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530916 * @param pluginCnfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530917 * @return default constructor for class
918 * @throws IOException when fails to append to file
919 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530920 public String addOfMethod(YangPluginConfig pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530921 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530922 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false, pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530923 + getOfMethod(getGeneratedJavaClassName(), null);
924 }
925
926 /**
927 * Adds hash code method for class.
928 *
929 * @param attr attribute info
930 * @throws IOException when fails to append to temporary file
931 */
932 private void addHashCodeMethod(JavaAttributeInfo attr)
933 throws IOException {
934 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
935 }
936
937 /**
938 * Adds equals method for class.
939 *
940 * @param attr attribute info
941 * @throws IOException when fails to append to temporary file
942 */
943 private void addEqualsMethod(JavaAttributeInfo attr)
944 throws IOException {
945 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
946 }
947
948 /**
949 * Adds ToString method for class.
950 *
951 * @param attr attribute info
952 * @throws IOException when fails to append to temporary file
953 */
954 private void addToStringMethod(JavaAttributeInfo attr)
955 throws IOException {
956 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
957 }
958
959 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530960 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530961 *
Bharat saraswalc0e04842016-05-12 13:16:57 +0530962 * @param javaAttributeInfo type attribute info
963 * @param fromStringAttributeInfo from string attribute info
964 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530965 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530966 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
967 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530968 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530969 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
970 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530971 }
972
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530973 /**
974 * Returns a temporary file handle for the specific file type.
975 *
976 * @param fileName file name
977 * @return temporary file handle
978 * @throws IOException when fails to create new file handle
979 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530980 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530981 throws IOException {
982 String path = getTempDirPath();
983 File dir = new File(path);
984 if (!dir.exists()) {
985 dir.mkdirs();
986 }
987 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
988 if (!file.exists()) {
989 file.createNewFile();
990 }
991 return file;
992 }
993
994 /**
995 * Returns a temporary file handle for the specific file type.
996 *
997 * @param fileName file name
998 * @return temporary file handle
999 * @throws IOException when fails to create new file handle
1000 */
Bharat saraswalc0e04842016-05-12 13:16:57 +05301001 File getJavaFileHandle(String fileName) throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301002 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1003 }
1004
1005 /**
1006 * Returns data from the temporary files.
1007 *
1008 * @param file temporary file handle
1009 * @return stored data from temporary files
1010 * @throws IOException when failed to get data from the given file
1011 */
1012 public String getTemporaryDataFromFileHandle(File file)
1013 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301014
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301015 String path = getTempDirPath();
1016 if (new File(path + file.getName()).exists()) {
1017 return readAppendFile(path + file.getName(), EMPTY_STRING);
1018 } else {
1019 throw new IOException("Unable to get data from the given "
1020 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1021 }
1022 }
1023
1024 /**
1025 * Returns temporary directory path.
1026 *
1027 * @return directory path
1028 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301029 String getTempDirPath() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301030 return getPackageDirPathFromJavaJPackage(getAbsoluteDirPath()) + SLASH + getGeneratedJavaClassName()
1031 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1032 }
1033
1034 /**
1035 * Parses attribute to get the attribute string.
1036 *
1037 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301038 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301039 * @return attribute string
1040 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301041 public String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301042 /*
1043 * TODO: check if this utility needs to be called or move to the caller
1044 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301045 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301046 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301047 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1048 attr.getImportInfo().getClassInfo(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301049 attributeName, attr.isListAttr());
1050 } else {
1051 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1052 attr.isListAttr());
1053 }
1054 }
1055
1056 /**
1057 * Appends content to temporary file.
1058 *
1059 * @param file temporary file
1060 * @param data data to be appended
1061 * @throws IOException when fails to append to file
1062 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301063 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301064 throws IOException {
1065 try {
1066 insertDataIntoJavaFile(file, data);
1067 } catch (IOException ex) {
1068 throw new IOException("failed to write in temp file.");
1069 }
1070 }
1071
1072 /**
1073 * Adds current node info as and attribute to the parent generated file.
1074 *
1075 * @param curNode current node which needs to be added as an attribute in
1076 * the parent generated code
1077 * @param isList is list construct
Bharat saraswal33dfa012016-05-17 19:59:16 +05301078 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301079 * @throws IOException IO operation exception
1080 */
1081 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
Bharat saraswal33dfa012016-05-17 19:59:16 +05301082 boolean isList, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301083 throws IOException {
1084 YangNode parent = getParentNodeInGenCode(curNode);
1085 if (!(parent instanceof JavaCodeGenerator)) {
1086 throw new TranslatorException("missing parent node to contain current node info in generated file");
1087 }
Bharat saraswalcad0e652016-05-26 23:48:38 +05301088 if (curNode instanceof YangJavaUses) {
1089 curNode = ((YangJavaUses) curNode).getRefGroup();
1090 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301091 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1092 parent, isList);
1093 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1094 throw new TranslatorException("missing parent temp file handle");
1095 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301096 getNodesInterfaceFragmentFiles(parent)
Bharat saraswal33dfa012016-05-17 19:59:16 +05301097 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301098 }
1099
1100 /**
1101 * Creates an attribute info object corresponding to a data model node and
1102 * return it.
1103 *
1104 * @param curNode current data model node for which the java code generation
1105 * is being handled
1106 * @param parentNode parent node in which the current node is an attribute
1107 * @param isListNode is the current added attribute needs to be a list
1108 * @return AttributeInfo attribute details required to add in temporary
1109 * files
1110 */
1111 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
1112 YangNode curNode, YangNode parentNode, boolean isListNode) {
Bharat saraswalcad0e652016-05-26 23:48:38 +05301113
1114 YangPluginConfig pluginConfig = ((JavaFileInfoContainer) parentNode).getJavaFileInfo().getPluginConfig();
1115 JavaFileInfo curNodeJavaInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1116 String curNodeName = null;
1117
1118 if (curNodeJavaInfo.getJavaName() != null) {
1119 curNodeName = curNodeJavaInfo.getJavaName();
1120 } else {
1121 curNodeName = getCamelCase(curNode.getName(), pluginConfig.getConflictResolver());
1122 }
1123
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301124 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
1125 throw new TranslatorException("Parent node does not have file info");
1126 }
Bharat saraswalcad0e652016-05-26 23:48:38 +05301127
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301128 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(parentNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +05301129 boolean isQualified = true;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301130 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301131 if (isListNode) {
1132 parentImportData.setIfListImported(true);
1133 }
Bharat saraswalcad0e652016-05-26 23:48:38 +05301134
1135 /*
1136 * Get the import info corresponding to the attribute for import in
1137 * generated java files or qualified access
1138 */
1139
1140 JavaQualifiedTypeInfo qualifiedTypeInfo = new JavaQualifiedTypeInfo();
1141 if (curNode instanceof YangJavaGrouping) {
1142 qualifiedTypeInfo = resolveGroupingsQuailifiedInfo(curNode, pluginConfig);
1143 } else {
1144 qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
1145 getCapitalCase(curNodeName));
1146 }
1147
1148 if (parentNode instanceof YangJavaModule
1149 || parentNode instanceof YangJavaSubModule
1150 || parentNode instanceof YangJavaInput
1151 || parentNode instanceof YangJavaOutput) {
1152 parentImportData.addImportInfo(qualifiedTypeInfo);
1153 isQualified = false;
1154 } else if (curNode instanceof YangJavaGrouping) {
1155 JavaFileInfo parentsClassInfo = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
1156 if (qualifiedTypeInfo.getClassInfo().equals(parentsClassInfo.getJavaName())) {
1157 isQualified = true;
1158 }
1159 if (!qualifiedTypeInfo.getPkgInfo().equals(parentsClassInfo.getPackage())) {
1160 parentImportData.addImportInfo(qualifiedTypeInfo);
1161 isQualified = false;
1162 }
1163 } else if (!detectCollisionBwParentAndChildForImport(curNode, qualifiedTypeInfo)) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301164 parentImportData.addImportInfo(qualifiedTypeInfo);
1165 isQualified = false;
1166 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301167 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1168 }
1169
Bharat saraswalc0e04842016-05-12 13:16:57 +05301170 /**
Bharat saraswalcad0e652016-05-26 23:48:38 +05301171 * Resolves groupings java qualified info.
1172 *
1173 * @param curNode grouping node
1174 * @param pluginConfig plugin configurations
1175 * @return groupings java qualified info
1176 */
1177 public static JavaQualifiedTypeInfo resolveGroupingsQuailifiedInfo(YangNode curNode,
1178 YangPluginConfig pluginConfig) {
1179
1180 JavaFileInfo groupingFileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
1181 JavaQualifiedTypeInfo qualifiedTypeInfo = new JavaQualifiedTypeInfo();
1182 if (groupingFileInfo.getPackage() == null) {
1183 List<String> parentNames = new ArrayList<>();
1184
1185 YangNode tempNode = curNode.getParent();
1186 YangNode groupingSuperParent = null;
1187 while (tempNode != null) {
1188 parentNames.add(tempNode.getName());
1189 groupingSuperParent = tempNode;
1190 tempNode = tempNode.getParent();
1191 }
1192
1193 String pkg = null;
1194 JavaFileInfo parentInfo = ((JavaFileInfoContainer) groupingSuperParent).getJavaFileInfo();
1195 if (parentInfo.getPackage() == null) {
1196 if (groupingSuperParent instanceof YangJavaModule) {
1197 YangJavaModule module = (YangJavaModule) groupingSuperParent;
1198 String modulePkg = getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
1199 .getRevision().getRevDate(), pluginConfig.getConflictResolver());
1200 pkg = modulePkg;
1201 } else if (groupingSuperParent instanceof YangJavaSubModule) {
1202 YangJavaSubModule submodule = (YangJavaSubModule) groupingSuperParent;
1203 String subModulePkg = getRootPackage(submodule.getVersion(),
1204 submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
1205 submodule.getRevision().getRevDate(), pluginConfig.getConflictResolver());
1206 pkg = subModulePkg;
1207 }
1208 } else {
1209 pkg = parentInfo.getPackage();
1210 }
1211 for (String name : parentNames) {
1212 pkg = pkg + PERIOD + getCamelCase(name, pluginConfig.getConflictResolver());
1213 }
1214
1215 qualifiedTypeInfo.setPkgInfo(pkg.toLowerCase());
1216 qualifiedTypeInfo.setClassInfo(
1217 getCapitalCase(getCamelCase(curNode.getName(), pluginConfig.getConflictResolver())));
1218 return qualifiedTypeInfo;
1219
1220 } else {
1221 qualifiedTypeInfo.setPkgInfo(groupingFileInfo.getPackage().toLowerCase());
1222 qualifiedTypeInfo.setClassInfo(getCapitalCase(groupingFileInfo.getJavaName()));
1223 return qualifiedTypeInfo;
1224 }
1225 }
1226
1227 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +05301228 * Returns interface fragment files for node.
1229 *
1230 * @param node YANG node
1231 * @return interface fragment files for node
1232 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301233 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1234 TempJavaFragmentFiles tempJavaFragmentFiles;
1235 if (node instanceof RpcNotificationContainer) {
1236 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1237 .getTempJavaCodeFragmentFiles()
1238 .getServiceTempFiles();
1239 } else {
1240 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1241 .getTempJavaCodeFragmentFiles()
1242 .getBeanTempFiles();
1243 }
1244 return tempJavaFragmentFiles;
1245 }
1246
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301247 /**
1248 * Adds parent's info to current node import list.
1249 *
1250 * @param curNode current node for which import list needs to be updated
Bharat saraswal33dfa012016-05-17 19:59:16 +05301251 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301252 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301253 public void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301254 caseImportInfo = new JavaQualifiedTypeInfo();
1255 YangNode parent = getParentNodeInGenCode(curNode);
1256 if (!(parent instanceof JavaCodeGenerator)) {
1257 throw new TranslatorException("missing parent node to contain current node info in generated file");
1258 }
1259 if (!(curNode instanceof JavaFileInfoContainer)) {
1260 throw new TranslatorException("missing java file information to get the package details "
1261 + "of attribute corresponding to child node");
1262 }
Bharat saraswal33dfa012016-05-17 19:59:16 +05301263 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1264 pluginConfig.getConflictResolver())));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301265 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
1266 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1267 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo);
1268 }
1269
1270 /**
1271 * Adds leaf attributes in generated files.
1272 *
1273 * @param listOfLeaves list of YANG leaf
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301274 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301275 * @throws IOException IO operation fail
1276 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301277 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1278 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301279 throws IOException {
1280 if (listOfLeaves != null) {
1281 for (YangLeaf leaf : listOfLeaves) {
1282 if (!(leaf instanceof JavaLeafInfoContainer)) {
1283 throw new TranslatorException("Leaf does not have java information");
1284 }
1285 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
Bharat saraswal33dfa012016-05-17 19:59:16 +05301286 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301287 javaLeaf.updateJavaQualifiedInfo();
1288 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1289 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301290 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1291 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301292 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1293 false);
Bharat saraswal33dfa012016-05-17 19:59:16 +05301294 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301295 }
1296 }
1297 }
1298
1299 /**
1300 * Adds leaf list's attributes in generated files.
1301 *
1302 * @param listOfLeafList list of YANG leaves
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301303 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301304 * @throws IOException IO operation fail
1305 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301306 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301307 throws IOException {
1308 if (listOfLeafList != null) {
1309 for (YangLeafList leafList : listOfLeafList) {
1310 if (!(leafList instanceof JavaLeafInfoContainer)) {
1311 throw new TranslatorException("Leaf-list does not have java information");
1312 }
1313 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
Bharat saraswal33dfa012016-05-17 19:59:16 +05301314 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301315 javaLeaf.updateJavaQualifiedInfo();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301316 getJavaImportData().setIfListImported(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301317 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1318 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301319 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1320 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301321 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1322 true);
Bharat saraswal33dfa012016-05-17 19:59:16 +05301323 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301324 }
1325 }
1326 }
1327
1328 /**
1329 * Adds all the leaves in the current data model node as part of the
1330 * generated temporary file.
1331 *
1332 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301333 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301334 * @throws IOException IO operation fail
1335 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301336 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1337 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301338 throws IOException {
1339 if (!(curNode instanceof YangLeavesHolder)) {
1340 throw new TranslatorException("Data model node does not have any leaves");
1341 }
1342 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301343 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1344 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301345 }
1346
1347 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301348 * Adds the new attribute info to the target generated temporary files.
1349 *
1350 * @param newAttrInfo the attribute info that needs to be added to temporary
1351 * files
Bharat saraswal33dfa012016-05-17 19:59:16 +05301352 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301353 * @throws IOException IO operation fail
1354 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301355 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301356 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301357 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301358 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301359 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301360 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301361
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301362 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301363 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301364 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301365
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301366 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301367 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301368 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301369
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301370 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1371 addSetterImpl(newAttrInfo);
1372 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301373
1374 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301375 addGetterImpl(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301376 }
1377 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1378 addHashCodeMethod(newAttrInfo);
1379 }
1380 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1381 addEqualsMethod(newAttrInfo);
1382 }
1383 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1384 addToStringMethod(newAttrInfo);
1385 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301386
1387 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301388 JavaQualifiedTypeInfo qualifiedInfoOfFromString =
1389 getQualifiedInfoOfFromString(newAttrInfo, pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +05301390 /*
1391 * Create a new java attribute info with qualified information of
1392 * wrapper classes.
1393 */
1394 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1395 newAttrInfo.getAttributeName(),
1396 newAttrInfo.getAttributeType(),
1397 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1398
1399 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301400 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301401 }
1402
1403 /**
1404 * Returns java class name.
1405 *
1406 * @param suffix for the class name based on the file type
1407 * @return java class name
1408 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301409 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301410 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301411 }
1412
1413 /**
1414 * Returns the directory path.
1415 *
1416 * @return directory path
1417 */
1418 private String getDirPath() {
1419 return getJavaFileInfo().getPackageFilePath();
1420 }
1421
1422 /**
1423 * Constructs java code exit.
1424 *
1425 * @param fileType generated file type
1426 * @param curNode current YANG node
1427 * @throws IOException when fails to generate java files
1428 */
1429 public void generateJavaFile(int fileType, YangNode curNode)
1430 throws IOException {
1431 List<String> imports = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +05301432 imports = getJavaImportData().getImports();
1433
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301434 createPackage(curNode);
1435
1436 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301437 * Generate java code.
1438 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301439 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1440 BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301441
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301442 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301443 * Create interface file.
1444 */
1445 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1446 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301447 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1448 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301449 * Create builder interface file.
1450 */
1451 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1452 setBuilderInterfaceJavaFileHandle(
1453 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1454 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301455 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1456 isAttributePresent()));
1457 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301458 * Append builder interface file to interface file and close it.
1459 */
1460 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1461 }
1462 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301463 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1464 addAugmentationHoldersImport(curNode, imports, false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301465 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301466 if (isAugmentedInfoExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301467 addAugmentedInfoImport(curNode, imports, false);
1468 }
1469 if (curNode instanceof YangCase) {
1470 removeCaseImport(imports);
1471 }
1472 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301473 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301474 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301475 addImportsToStringAndHasCodeMethods(curNode, imports);
1476 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301477 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301478 addAugmentedInfoImport(curNode, imports, true);
1479 addArrayListImport(curNode, imports, true);
1480 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301481 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301482 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301483 * Create builder class file.
1484 */
1485 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1486 setBuilderClassJavaFileHandle(
Bharat saraswalc0e04842016-05-12 13:16:57 +05301487 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode,
1488 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301489 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301490 * Create impl class file.
1491 */
1492 if ((fileType & IMPL_CLASS_MASK) != 0) {
1493 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1494 setImplClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301495 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent()));
1496 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301497 * Append impl class to builder class and close it.
1498 */
1499 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1500 }
1501 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301502 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1503 addAugmentedInfoImport(curNode, imports, false);
1504 addArrayListImport(curNode, imports, false);
1505 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301506 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301507 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301508 * Close all the file handles.
1509 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301510 freeTemporaryResources(false);
1511 }
1512
1513 /**
1514 * Adds imports for ToString and HashCodeMethod.
1515 *
1516 * @param curNode current YANG node
1517 * @param imports import list
1518 * @return import list
1519 */
1520 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1521 imports.add(getJavaImportData().getImportForHashAndEquals());
1522 imports.add(getJavaImportData().getImportForToString());
1523 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301524 }
1525
1526 /**
1527 * Removes case import info from import list.
1528 *
1529 * @param imports list of imports
1530 * @return import for class
1531 */
1532 private List<String> removeCaseImport(List<String> imports) {
1533 if (imports != null && caseImportInfo != null) {
1534 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1535 SEMI_COLAN + NEW_LINE;
1536 imports.remove(caseImport);
1537 }
1538 return imports;
1539 }
1540
1541 /**
1542 * Removes all temporary file handles.
1543 *
1544 * @param isErrorOccurred when translator fails to generate java files we
1545 * need to close all open file handles include temporary files
1546 * and java files.
1547 * @throws IOException when failed to delete the temporary files
1548 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301549 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301550 throws IOException {
1551 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301552 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301553 * Close all java file handles and when error occurs delete the files.
1554 */
1555 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1556 closeFile(getInterfaceJavaFileHandle(), isError);
1557 }
1558 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
1559 closeFile(getBuilderClassJavaFileHandle(), isError);
1560 }
1561 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1562 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1563 }
1564 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
1565 closeFile(getImplClassJavaFileHandle(), true);
1566 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301567
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301568 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301569 * Close all temporary file handles and delete the files.
1570 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301571 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1572 closeFile(getGetterImplTempFileHandle(), true);
1573 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301574 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1575 closeFile(getAttributesTempFileHandle(), true);
1576 }
1577 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1578 closeFile(getHashCodeImplTempFileHandle(), true);
1579 }
1580 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1581 closeFile(getToStringImplTempFileHandle(), true);
1582 }
1583 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1584 closeFile(getEqualsImplTempFileHandle(), true);
1585 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301586 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1587 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301588 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301589 }
1590
1591 /**
1592 * Returns if the attribute needs to be accessed in a qualified manner or
1593 * not, if it needs to be imported, then the same needs to be done.
1594 *
1595 * @param importInfo import info for the current attribute being added
1596 * @return status of the qualified access to the attribute
1597 */
1598 public boolean getIsQualifiedAccessOrAddToImportList(
1599 JavaQualifiedTypeInfo importInfo) {
1600 boolean isImportPkgEqualCurNodePkg;
1601 if (importInfo.getClassInfo().contentEquals(
1602 getGeneratedJavaClassName())) {
1603 /*
1604 * if the current class name is same as the attribute class name,
1605 * then the attribute must be accessed in a qualified manner.
1606 */
1607 return true;
1608 } else if (importInfo.getPkgInfo() != null) {
1609 /*
1610 * If the attribute type is having the package info, it is contender
1611 * for import list and also need to check if it needs to be a
1612 * qualified access.
1613 */
1614 isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(importInfo);
1615 if (!isImportPkgEqualCurNodePkg) {
1616 /*
1617 * If the package of the attribute added is not same as the
1618 * current class package, then it must either be imported for
1619 * access or it must be a qualified access.
1620 */
1621 boolean isImportAdded = getJavaImportData().addImportInfo(importInfo);
1622 if (!isImportAdded) {
1623 /*
1624 * If the attribute type info is not imported, then it must
1625 * be a qualified access.
1626 */
1627 return true;
1628 }
1629 }
1630 }
1631 return false;
1632 }
1633
1634 /**
1635 * Checks if the import info is same as the package of the current generated
1636 * java file.
1637 *
1638 * @param importInfo import info for an attribute
1639 * @return true if the import info is same as the current nodes package
1640 * false otherwise
1641 */
1642 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1643 return getJavaFileInfo().getPackage()
1644 .contentEquals(importInfo.getPkgInfo());
1645 }
1646}