blob: c35b744f971f820d883607195d8ab57195fbc7d0 [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.yangutils.translator.tojava;
17
18import java.io.File;
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.List;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053022
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053023import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053024import org.onosproject.yangutils.datamodel.YangCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangLeafList;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
28import org.onosproject.yangutils.datamodel.YangNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
30import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053031import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
38import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
45import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
46import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
49import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
50import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053051import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
54import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
55import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
56import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053057import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
58import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053061import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053062import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
66import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswalc0e04842016-05-12 13:16:57 +053067import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
69import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
70import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
71import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053073import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
74import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
75import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
77import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053078import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentationHoldersImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053081import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.detectCollisionBwParentAndChildForImport;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053082import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentationHolderExtended;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
Bharat saraswalc0e04842016-05-12 13:16:57 +053084import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.sortImports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
86import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
87import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
88import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
89import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
90import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
91import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
93import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
94import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
95import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
96import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053097import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053098import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
99import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530100import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
101import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
102import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
103
104/**
105 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530106 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107 */
108public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530109
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530110 /**
111 * Information about the java files being generated.
112 */
113 private JavaFileInfo javaFileInfo;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530114
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 /**
116 * Imported class info.
117 */
118 private JavaImportData javaImportData;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530119
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530120 /**
121 * The variable which guides the types of temporary files generated using
122 * the temporary generated file types mask.
123 */
124 private int generatedTempFiles;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530125
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530126 /**
127 * Absolute path where the target java file needs to be generated.
128 */
129 private String absoluteDirPath;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530130
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530131 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530132 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 private JavaExtendsListHolder javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530135 /**
136 * File type extension for java classes.
137 */
138 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530139
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530140 /**
141 * File type extension for temporary classes.
142 */
143 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530144
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530145 /**
146 * Folder suffix for temporary files folder.
147 */
148 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530149
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530150 /**
151 * File name for getter method.
152 */
153 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530154
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 /**
156 * File name for setter method.
157 */
158 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530159
160 /**
161 * File name for getter method implementation.
162 */
163 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
164
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530165 /**
166 * File name for setter method implementation.
167 */
168 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530169
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530170 /**
171 * File name for attributes.
172 */
173 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530174
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530175 /**
176 * File name for to string method.
177 */
178 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530179
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 /**
181 * File name for hash code method.
182 */
183 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530184
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530185 /**
186 * File name for equals method.
187 */
188 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530189
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530190 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530191 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530193 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530194
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530195 /**
196 * File name for interface java file name suffix.
197 */
198 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530199
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530200 /**
201 * File name for builder interface file name suffix.
202 */
203 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530204
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530205 /**
206 * File name for builder class file name suffix.
207 */
208 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530209
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530210 /**
211 * File name for impl class file name suffix.
212 */
213 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530214
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530215 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530216 * Java file handle for interface file.
217 */
218 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530219
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530220 /**
221 * Java file handle for builder interface file.
222 */
223 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530224
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530225 /**
226 * Java file handle for builder class file.
227 */
228 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530229
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530230 /**
231 * Java file handle for impl class file.
232 */
233 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530234
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530235 /**
236 * Temporary file handle for attribute.
237 */
238 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530239
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 /**
241 * Temporary file handle for getter of interface.
242 */
243 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530244
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530245 /**
246 * Temporary file handle for setter of interface.
247 */
248 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530249
250 /**
251 * Temporary file handle for getter of class.
252 */
253 private File getterImplTempFileHandle;
254
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530255 /**
256 * Temporary file handle for setter of class.
257 */
258 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530259
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530260 /**
261 * Temporary file handle for hash code method of class.
262 */
263 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530264
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530265 /**
266 * Temporary file handle for equals method of class.
267 */
268 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530269
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530270 /**
271 * Temporary file handle for to string method of class.
272 */
273 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530274
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530275 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530276 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530277 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530278 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530279
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530280 /**
281 * Import info for case.
282 */
283 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530284
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530285 /**
286 * Is attribute added.
287 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530288 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530289
Bharat saraswalc0e04842016-05-12 13:16:57 +0530290 /**
291 * Creates an instance of temp java fragment files.
292 */
Ray Milkey994c4982016-05-11 18:39:39 +0000293 public TempJavaFragmentFiles() {
294 }
295
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530296 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530297 * Retrieves the absolute path where the file needs to be generated.
298 *
299 * @return absolute path where the file needs to be generated
300 */
301 private String getAbsoluteDirPath() {
302 return absoluteDirPath;
303 }
304
305 /**
306 * Sets absolute path where the file needs to be generated.
307 *
308 * @param absoluteDirPath absolute path where the file needs to be
309 * generated.
310 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530311 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530312 this.absoluteDirPath = absoluteDirPath;
313 }
314
315 /**
316 * Sets the generated java file information.
317 *
318 * @param javaFileInfo generated java file information
319 */
320 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
321 this.javaFileInfo = javaFileInfo;
322 }
323
324 /**
325 * Retrieves the generated java file information.
326 *
327 * @return generated java file information
328 */
329 public JavaFileInfo getJavaFileInfo() {
330 return javaFileInfo;
331 }
332
333 /**
334 * Retrieves the generated temp files.
335 *
336 * @return generated temp files
337 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530338 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530339 return generatedTempFiles;
340 }
341
342 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530343 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530344 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530345 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530346 generatedTempFiles = 0;
347 }
348
349 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530350 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530351 *
352 * @param generatedTempFile generated file
353 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530354 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530355 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530356 setGeneratedTempFiles(generatedTempFiles);
357 }
358
359 /**
360 * Sets generated file files.
361 *
362 * @param generatedTempFile generated file
363 */
364 void setGeneratedTempFiles(int fileType) {
365 generatedTempFiles = fileType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530366 }
367
368 /**
369 * Retrieves the generated Java files.
370 *
371 * @return generated Java files
372 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530373 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530374 return getJavaFileInfo().getGeneratedFileTypes();
375 }
376
377 /**
378 * Retrieves the mapped Java class name.
379 *
380 * @return mapped Java class name
381 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530382 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530383 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530384 }
385
386 /**
387 * Retrieves the import data for the generated Java file.
388 *
389 * @return import data for the generated Java file
390 */
391 public JavaImportData getJavaImportData() {
392 return javaImportData;
393 }
394
395 /**
396 * Sets import data for the generated Java file.
397 *
398 * @param javaImportData import data for the generated Java file
399 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530400 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530401 this.javaImportData = javaImportData;
402 }
403
404 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530405 * Retrieves the status of any attributes added.
406 *
407 * @return status of any attributes added
408 */
409 public boolean isAttributePresent() {
410 return isAttributePresent;
411 }
412
413 /**
414 * Sets status of any attributes added.
415 *
416 * @param attributePresent status of any attributes added
417 */
418 public void setAttributePresent(boolean attributePresent) {
419 isAttributePresent = attributePresent;
420 }
421
422 /**
423 * Returns getter methods's temporary file handle.
424 *
425 * @return temporary file handle
426 */
427 public File getGetterInterfaceTempFileHandle() {
428 return getterInterfaceTempFileHandle;
429 }
430
431 /**
432 * Sets to getter method's temporary file handle.
433 *
434 * @param getterForInterface file handle for to getter method
435 */
436 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
437 getterInterfaceTempFileHandle = getterForInterface;
438 }
439
440 /**
441 * Returns setter method's temporary file handle.
442 *
443 * @return temporary file handle
444 */
445 public File getSetterInterfaceTempFileHandle() {
446 return setterInterfaceTempFileHandle;
447 }
448
449 /**
450 * Sets to setter method's temporary file handle.
451 *
452 * @param setterForInterface file handle for to setter method
453 */
454 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
455 setterInterfaceTempFileHandle = setterForInterface;
456 }
457
458 /**
459 * Returns setter method's impl's temporary file handle.
460 *
461 * @return temporary file handle
462 */
463 public File getSetterImplTempFileHandle() {
464 return setterImplTempFileHandle;
465 }
466
467 /**
468 * Sets to setter method's impl's temporary file handle.
469 *
470 * @param setterImpl file handle for to setter method's implementation class
471 */
472 private void setSetterImplTempFileHandle(File setterImpl) {
473 setterImplTempFileHandle = setterImpl;
474 }
475
476 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530477 * Returns from string method's temporary file handle.
478 *
479 * @return from string method's temporary file handle
480 */
481 public File getFromStringImplTempFileHandle() {
482 return fromStringImplTempFileHandle;
483 }
484
485 /**
486 * Sets from string method's temporary file handle.
487 *
488 * @param fromStringImplTempFileHandle from string method's temporary file
489 * handle
490 */
491 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
492 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
493 }
494
495 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530496 * Creates an instance of temporary java code fragment.
497 *
498 * @param javaFileInfo generated java file information
499 * @throws IOException when fails to create new file handle
500 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530501 TempJavaFragmentFiles(JavaFileInfo javaFileInfo) throws IOException {
502 setJavaExtendsListHolder(new JavaExtendsListHolder());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530503 setJavaImportData(new JavaImportData());
504 setJavaFileInfo(javaFileInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530505 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
506 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530507
508 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530509 * Initialize getter when generation file type matches to interface
510 * mask.
511 */
512 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
513 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
514 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530515
516 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530517 * Initialize getter and setter when generation file type matches to
518 * builder interface mask.
519 */
520 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
521 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
522 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
523 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530524
525 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530526 * Initialize getterImpl, setterImpl and attributes when generation file
527 * type matches to builder class mask.
528 */
529 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
530 addGeneratedTempFile(ATTRIBUTES_MASK);
531 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
532 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
533 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530534
535 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530536 * Initialize getterImpl, attributes, constructor, hash code, equals and
537 * to strings when generation file type matches to impl class mask.
538 */
539 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
540 addGeneratedTempFile(ATTRIBUTES_MASK);
541 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530542 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
543 addGeneratedTempFile(EQUALS_IMPL_MASK);
544 addGeneratedTempFile(TO_STRING_IMPL_MASK);
545 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530546
547 /*
548 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530549 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530550 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530551 addGeneratedTempFile(ATTRIBUTES_MASK);
552 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
553 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
554 addGeneratedTempFile(EQUALS_IMPL_MASK);
555 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530556 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530557 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530558
559 /*
Bharat saraswalc0e04842016-05-12 13:16:57 +0530560 * Initialize temp files to generate enum class.
561 */
562 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
563 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
564 }
565 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530566 * Initialize getter and setter when generation file type matches to
567 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530568 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530569 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
570 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
571 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530572 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530573 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530574 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530575
576 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530577 * Set temporary file handles.
578 */
579 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
580 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
581 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530582
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530583 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
584 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
585 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530586
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530587 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
588 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
589 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530590
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530591 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
592 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
593 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530594
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530595 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
596 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
597 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530598
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530599 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
600 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
601 }
602 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
603 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
604 }
605 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
606 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
607 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530608 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
609 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530610 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530611
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530612 }
613
614 /**
615 * Returns java file handle for interface file.
616 *
617 * @return java file handle for interface file
618 */
619 private File getInterfaceJavaFileHandle() {
620 return interfaceJavaFileHandle;
621 }
622
623 /**
624 * Sets the java file handle for interface file.
625 *
626 * @param interfaceJavaFileHandle java file handle
627 */
628 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
629 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
630 }
631
632 /**
633 * Returns java file handle for builder interface file.
634 *
635 * @return java file handle for builder interface file
636 */
637 private File getBuilderInterfaceJavaFileHandle() {
638 return builderInterfaceJavaFileHandle;
639 }
640
641 /**
642 * Sets the java file handle for builder interface file.
643 *
644 * @param builderInterfaceJavaFileHandle java file handle
645 */
646 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
647 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
648 }
649
650 /**
651 * Returns java file handle for builder class file.
652 *
653 * @return java file handle for builder class file
654 */
655 private File getBuilderClassJavaFileHandle() {
656 return builderClassJavaFileHandle;
657 }
658
659 /**
660 * Sets the java file handle for builder class file.
661 *
662 * @param builderClassJavaFileHandle java file handle
663 */
664 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
665 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
666 }
667
668 /**
669 * Returns java file handle for impl class file.
670 *
671 * @return java file handle for impl class file
672 */
673 private File getImplClassJavaFileHandle() {
674 return implClassJavaFileHandle;
675 }
676
677 /**
678 * Sets the java file handle for impl class file.
679 *
680 * @param implClassJavaFileHandle java file handle
681 */
682 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
683 this.implClassJavaFileHandle = implClassJavaFileHandle;
684 }
685
686 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530687 * Returns attribute's temporary file handle.
688 *
689 * @return temporary file handle
690 */
691 public File getAttributesTempFileHandle() {
692 return attributesTempFileHandle;
693 }
694
695 /**
696 * Sets attribute's temporary file handle.
697 *
698 * @param attributeForClass file handle for attribute
699 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530700 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530701 attributesTempFileHandle = attributeForClass;
702 }
703
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530704 /**
705 * Returns getter method's impl's temporary file handle.
706 *
707 * @return temporary file handle
708 */
709 public File getGetterImplTempFileHandle() {
710 return getterImplTempFileHandle;
711 }
712
713 /**
714 * Sets to getter method's impl's temporary file handle.
715 *
716 * @param getterImpl file handle for to getter method's impl
717 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530718 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530719 getterImplTempFileHandle = getterImpl;
720 }
721
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530722 /**
723 * Returns hash code method's temporary file handle.
724 *
725 * @return temporary file handle
726 */
727 public File getHashCodeImplTempFileHandle() {
728 return hashCodeImplTempFileHandle;
729 }
730
731 /**
732 * Sets hash code method's temporary file handle.
733 *
734 * @param hashCodeMethod file handle for hash code method
735 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530736 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530737 hashCodeImplTempFileHandle = hashCodeMethod;
738 }
739
740 /**
741 * Returns equals mehtod's temporary file handle.
742 *
743 * @return temporary file handle
744 */
745 public File getEqualsImplTempFileHandle() {
746 return equalsImplTempFileHandle;
747 }
748
749 /**
750 * Sets equals method's temporary file handle.
751 *
752 * @param equalsMethod file handle for to equals method
753 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530754 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530755 equalsImplTempFileHandle = equalsMethod;
756 }
757
758 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530759 * Returns to string method's temporary file handle.
760 *
761 * @return temporary file handle
762 */
763 public File getToStringImplTempFileHandle() {
764 return toStringImplTempFileHandle;
765 }
766
767 /**
768 * Sets to string method's temporary file handle.
769 *
770 * @param toStringMethod file handle for to string method
771 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530772 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530773 toStringImplTempFileHandle = toStringMethod;
774 }
775
776 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530777 * Returns java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530778 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530779 * @return java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530780 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530781 public JavaExtendsListHolder getJavaExtendsListHolder() {
782 return javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530783 }
784
785 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530786 * Sets java extends list holder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530787 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530788 * @param javaExtendsListHolder java extends list holder
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530789 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530790 public void setJavaExtendsListHolder(JavaExtendsListHolder javaExtendsListHolder) {
791 this.javaExtendsListHolder = javaExtendsListHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530792 }
793
794 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530795 * Adds attribute for class.
796 *
797 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530798 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530799 * @throws IOException when fails to append to temporary file
800 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530801 private void addAttribute(JavaAttributeInfo attr, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530802 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530803 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr, yangPluginConfig)
804 + FOUR_SPACE_INDENTATION);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530805 }
806
807 /**
808 * Adds getter for interface.
809 *
810 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530811 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530812 * @throws IOException when fails to append to temporary file
813 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530814 private void addGetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530815 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530816 appendToFile(getGetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530817 getGetterString(attr, getGeneratedJavaFiles(), pluginConfig) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530818 }
819
820 /**
821 * Adds setter for interface.
822 *
823 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530824 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530825 * @throws IOException when fails to append to temporary file
826 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530827 private void addSetterForInterface(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530828 throws IOException {
829 appendToFile(getSetterInterfaceTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530830 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), pluginConfig)
831 + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530832 }
833
834 /**
835 * Adds setter's implementation for class.
836 *
837 * @param attr attribute info
838 * @throws IOException when fails to append to temporary file
839 */
840 private void addSetterImpl(JavaAttributeInfo attr)
841 throws IOException {
842 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530843 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
844 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530845 NEW_LINE);
846 }
847
848 /**
849 * Adds getter method's impl for class.
850 *
851 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530852 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530853 * @throws IOException when fails to append to temporary file
854 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530855 private void addGetterImpl(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530856 throws IOException {
857 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
858 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
859 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
860 getGeneratedJavaFiles()) + NEW_LINE);
861 } else {
janani b4a6711a2016-05-17 13:12:22 +0530862 appendToFile(getGetterImplTempFileHandle(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530863 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
janani b4a6711a2016-05-17 13:12:22 +0530864 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530865 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530866 }
867
868 /**
869 * Adds build method for interface.
870 *
871 * @return build method for interface
Bharat saraswal33dfa012016-05-17 19:59:16 +0530872 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530873 * @throws IOException when fails to append to temporary file
874 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530875 String addBuildMethodForInterface(YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530876 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530877 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName(), pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530878 }
879
880 /**
881 * Adds build method's implementation for class.
882 *
883 * @return build method implementation for class
884 * @throws IOException when fails to append to temporary file
885 */
886 String addBuildMethodImpl()
887 throws IOException {
888 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
889 }
890
891 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530892 * Adds default constructor for class.
893 *
894 * @param modifier modifier for constructor.
895 * @param toAppend string which need to be appended with the class name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530896 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530897 * @return default constructor for class
898 * @throws IOException when fails to append to file
899 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530900 String addDefaultConstructor(String modifier, String toAppend, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530901 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530902 return NEW_LINE
903 + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530904 }
905
906 /**
907 * Adds default constructor for class.
908 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530909 * @param pluginCnfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530910 * @return default constructor for class
911 * @throws IOException when fails to append to file
912 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530913 public String addOfMethod(YangPluginConfig pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530914 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530915 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false, pluginCnfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530916 + getOfMethod(getGeneratedJavaClassName(), null);
917 }
918
919 /**
920 * Adds hash code method for class.
921 *
922 * @param attr attribute info
923 * @throws IOException when fails to append to temporary file
924 */
925 private void addHashCodeMethod(JavaAttributeInfo attr)
926 throws IOException {
927 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
928 }
929
930 /**
931 * Adds equals method for class.
932 *
933 * @param attr attribute info
934 * @throws IOException when fails to append to temporary file
935 */
936 private void addEqualsMethod(JavaAttributeInfo attr)
937 throws IOException {
938 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
939 }
940
941 /**
942 * Adds ToString method for class.
943 *
944 * @param attr attribute info
945 * @throws IOException when fails to append to temporary file
946 */
947 private void addToStringMethod(JavaAttributeInfo attr)
948 throws IOException {
949 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
950 }
951
952 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530953 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530954 *
Bharat saraswalc0e04842016-05-12 13:16:57 +0530955 * @param javaAttributeInfo type attribute info
956 * @param fromStringAttributeInfo from string attribute info
957 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530958 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530959 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
960 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530961 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530962 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
963 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530964 }
965
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530966 /**
967 * Returns a temporary file handle for the specific file type.
968 *
969 * @param fileName file name
970 * @return temporary file handle
971 * @throws IOException when fails to create new file handle
972 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530973 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530974 throws IOException {
975 String path = getTempDirPath();
976 File dir = new File(path);
977 if (!dir.exists()) {
978 dir.mkdirs();
979 }
980 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
981 if (!file.exists()) {
982 file.createNewFile();
983 }
984 return file;
985 }
986
987 /**
988 * Returns a temporary file handle for the specific file type.
989 *
990 * @param fileName file name
991 * @return temporary file handle
992 * @throws IOException when fails to create new file handle
993 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530994 File getJavaFileHandle(String fileName) throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530995 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
996 }
997
998 /**
999 * Returns data from the temporary files.
1000 *
1001 * @param file temporary file handle
1002 * @return stored data from temporary files
1003 * @throws IOException when failed to get data from the given file
1004 */
1005 public String getTemporaryDataFromFileHandle(File file)
1006 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301007
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301008 String path = getTempDirPath();
1009 if (new File(path + file.getName()).exists()) {
1010 return readAppendFile(path + file.getName(), EMPTY_STRING);
1011 } else {
1012 throw new IOException("Unable to get data from the given "
1013 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1014 }
1015 }
1016
1017 /**
1018 * Returns temporary directory path.
1019 *
1020 * @return directory path
1021 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301022 String getTempDirPath() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301023 return getPackageDirPathFromJavaJPackage(getAbsoluteDirPath()) + SLASH + getGeneratedJavaClassName()
1024 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1025 }
1026
1027 /**
1028 * Parses attribute to get the attribute string.
1029 *
1030 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +05301031 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301032 * @return attribute string
1033 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301034 public String parseAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301035 /*
1036 * TODO: check if this utility needs to be called or move to the caller
1037 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301038 String attributeName = getCamelCase(attr.getAttributeName(), pluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301039 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301040 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1041 attr.getImportInfo().getClassInfo(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301042 attributeName, attr.isListAttr());
1043 } else {
1044 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1045 attr.isListAttr());
1046 }
1047 }
1048
1049 /**
1050 * Appends content to temporary file.
1051 *
1052 * @param file temporary file
1053 * @param data data to be appended
1054 * @throws IOException when fails to append to file
1055 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301056 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301057 throws IOException {
1058 try {
1059 insertDataIntoJavaFile(file, data);
1060 } catch (IOException ex) {
1061 throw new IOException("failed to write in temp file.");
1062 }
1063 }
1064
1065 /**
1066 * Adds current node info as and attribute to the parent generated file.
1067 *
1068 * @param curNode current node which needs to be added as an attribute in
1069 * the parent generated code
1070 * @param isList is list construct
Bharat saraswal33dfa012016-05-17 19:59:16 +05301071 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301072 * @throws IOException IO operation exception
1073 */
1074 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
Bharat saraswal33dfa012016-05-17 19:59:16 +05301075 boolean isList, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301076 throws IOException {
1077 YangNode parent = getParentNodeInGenCode(curNode);
1078 if (!(parent instanceof JavaCodeGenerator)) {
1079 throw new TranslatorException("missing parent node to contain current node info in generated file");
1080 }
1081 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1082 parent, isList);
1083 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1084 throw new TranslatorException("missing parent temp file handle");
1085 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301086 getNodesInterfaceFragmentFiles(parent)
Bharat saraswal33dfa012016-05-17 19:59:16 +05301087 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301088 }
1089
1090 /**
1091 * Creates an attribute info object corresponding to a data model node and
1092 * return it.
1093 *
1094 * @param curNode current data model node for which the java code generation
1095 * is being handled
1096 * @param parentNode parent node in which the current node is an attribute
1097 * @param isListNode is the current added attribute needs to be a list
1098 * @return AttributeInfo attribute details required to add in temporary
1099 * files
1100 */
1101 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
1102 YangNode curNode, YangNode parentNode, boolean isListNode) {
1103 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
1104 /*
1105 * Get the import info corresponding to the attribute for import in
1106 * generated java files or qualified access
1107 */
1108 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
janani b4a6711a2016-05-17 13:12:22 +05301109 getCapitalCase(curNodeName));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301110 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
1111 throw new TranslatorException("Parent node does not have file info");
1112 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301113 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(parentNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +05301114 boolean isQualified = true;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301115 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301116 if (isListNode) {
1117 parentImportData.setIfListImported(true);
1118 }
1119 if (!detectCollisionBwParentAndChildForImport(curNode, qualifiedTypeInfo)) {
1120 parentImportData.addImportInfo(qualifiedTypeInfo);
1121 isQualified = false;
1122 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301123 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1124 }
1125
Bharat saraswalc0e04842016-05-12 13:16:57 +05301126 /**
1127 * Returns interface fragment files for node.
1128 *
1129 * @param node YANG node
1130 * @return interface fragment files for node
1131 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301132 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1133 TempJavaFragmentFiles tempJavaFragmentFiles;
1134 if (node instanceof RpcNotificationContainer) {
1135 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1136 .getTempJavaCodeFragmentFiles()
1137 .getServiceTempFiles();
1138 } else {
1139 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1140 .getTempJavaCodeFragmentFiles()
1141 .getBeanTempFiles();
1142 }
1143 return tempJavaFragmentFiles;
1144 }
1145
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301146 /**
1147 * Adds parent's info to current node import list.
1148 *
1149 * @param curNode current node for which import list needs to be updated
Bharat saraswal33dfa012016-05-17 19:59:16 +05301150 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301151 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301152 public void addParentInfoInCurNodeTempFile(YangNode curNode, YangPluginConfig pluginConfig) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301153 caseImportInfo = new JavaQualifiedTypeInfo();
1154 YangNode parent = getParentNodeInGenCode(curNode);
1155 if (!(parent instanceof JavaCodeGenerator)) {
1156 throw new TranslatorException("missing parent node to contain current node info in generated file");
1157 }
1158 if (!(curNode instanceof JavaFileInfoContainer)) {
1159 throw new TranslatorException("missing java file information to get the package details "
1160 + "of attribute corresponding to child node");
1161 }
Bharat saraswal33dfa012016-05-17 19:59:16 +05301162 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(),
1163 pluginConfig.getConflictResolver())));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301164 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
1165 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1166 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo);
1167 }
1168
1169 /**
1170 * Adds leaf attributes in generated files.
1171 *
1172 * @param listOfLeaves list of YANG leaf
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301173 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301174 * @throws IOException IO operation fail
1175 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301176 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1177 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301178 throws IOException {
1179 if (listOfLeaves != null) {
1180 for (YangLeaf leaf : listOfLeaves) {
1181 if (!(leaf instanceof JavaLeafInfoContainer)) {
1182 throw new TranslatorException("Leaf does not have java information");
1183 }
1184 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
Bharat saraswal33dfa012016-05-17 19:59:16 +05301185 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301186 javaLeaf.updateJavaQualifiedInfo();
1187 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1188 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301189 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1190 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301191 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1192 false);
Bharat saraswal33dfa012016-05-17 19:59:16 +05301193 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301194 }
1195 }
1196 }
1197
1198 /**
1199 * Adds leaf list's attributes in generated files.
1200 *
1201 * @param listOfLeafList list of YANG leaves
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301202 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301203 * @throws IOException IO operation fail
1204 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301205 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301206 throws IOException {
1207 if (listOfLeafList != null) {
1208 for (YangLeafList leafList : listOfLeafList) {
1209 if (!(leafList instanceof JavaLeafInfoContainer)) {
1210 throw new TranslatorException("Leaf-list does not have java information");
1211 }
1212 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
Bharat saraswal33dfa012016-05-17 19:59:16 +05301213 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301214 javaLeaf.updateJavaQualifiedInfo();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301215 getJavaImportData().setIfListImported(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301216 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1217 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301218 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1219 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301220 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1221 true);
Bharat saraswal33dfa012016-05-17 19:59:16 +05301222 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301223 }
1224 }
1225 }
1226
1227 /**
1228 * Adds all the leaves in the current data model node as part of the
1229 * generated temporary file.
1230 *
1231 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301232 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301233 * @throws IOException IO operation fail
1234 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301235 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1236 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301237 throws IOException {
1238 if (!(curNode instanceof YangLeavesHolder)) {
1239 throw new TranslatorException("Data model node does not have any leaves");
1240 }
1241 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301242 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1243 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301244 }
1245
1246 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301247 * Adds the new attribute info to the target generated temporary files.
1248 *
1249 * @param newAttrInfo the attribute info that needs to be added to temporary
1250 * files
Bharat saraswal33dfa012016-05-17 19:59:16 +05301251 * @param pluginConfig plugin configurations
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301252 * @throws IOException IO operation fail
1253 */
Bharat saraswal33dfa012016-05-17 19:59:16 +05301254 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301255 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301256 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301257 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301258 addAttribute(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301259 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301260
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301261 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301262 addGetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301263 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301264
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301265 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301266 addSetterForInterface(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301267 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301268
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301269 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1270 addSetterImpl(newAttrInfo);
1271 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301272
1273 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301274 addGetterImpl(newAttrInfo, pluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301275 }
1276 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1277 addHashCodeMethod(newAttrInfo);
1278 }
1279 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1280 addEqualsMethod(newAttrInfo);
1281 }
1282 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1283 addToStringMethod(newAttrInfo);
1284 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301285
1286 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +05301287 JavaQualifiedTypeInfo qualifiedInfoOfFromString =
1288 getQualifiedInfoOfFromString(newAttrInfo, pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +05301289 /*
1290 * Create a new java attribute info with qualified information of
1291 * wrapper classes.
1292 */
1293 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1294 newAttrInfo.getAttributeName(),
1295 newAttrInfo.getAttributeType(),
1296 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1297
1298 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301299 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301300 }
1301
1302 /**
1303 * Returns java class name.
1304 *
1305 * @param suffix for the class name based on the file type
1306 * @return java class name
1307 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301308 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301309 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301310 }
1311
1312 /**
1313 * Returns the directory path.
1314 *
1315 * @return directory path
1316 */
1317 private String getDirPath() {
1318 return getJavaFileInfo().getPackageFilePath();
1319 }
1320
1321 /**
1322 * Constructs java code exit.
1323 *
1324 * @param fileType generated file type
1325 * @param curNode current YANG node
1326 * @throws IOException when fails to generate java files
1327 */
1328 public void generateJavaFile(int fileType, YangNode curNode)
1329 throws IOException {
1330 List<String> imports = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +05301331 imports = getJavaImportData().getImports();
1332
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301333 createPackage(curNode);
1334
1335 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301336 * Generate java code.
1337 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301338 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1339 BUILDER_INTERFACE_MASK) != 0) {
1340 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301341 * Adds import for case.
1342 */
1343 if (curNode instanceof YangCase) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301344 List<String> importData =
1345 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1346 .getBeanTempFiles().getJavaImportData().getImports();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301347 for (String importInfo : importData) {
1348 if (!imports.contains(importInfo)) {
1349 imports.add(importInfo);
1350 }
1351 }
1352 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301353
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301354 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301355 * Create interface file.
1356 */
1357 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1358 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301359 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1360 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301361 * Create builder interface file.
1362 */
1363 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1364 setBuilderInterfaceJavaFileHandle(
1365 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1366 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301367 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1368 isAttributePresent()));
1369 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301370 * Append builder interface file to interface file and close it.
1371 */
1372 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1373 }
1374 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301375 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1376 addAugmentationHoldersImport(curNode, imports, false);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301377 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301378 if (isAugmentedInfoExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301379 addAugmentedInfoImport(curNode, imports, false);
1380 }
1381 if (curNode instanceof YangCase) {
1382 removeCaseImport(imports);
1383 }
1384 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301385 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301386 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301387 addImportsToStringAndHasCodeMethods(curNode, imports);
1388 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301389 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301390 addAugmentedInfoImport(curNode, imports, true);
1391 addArrayListImport(curNode, imports, true);
1392 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301393 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301394 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301395 * Create builder class file.
1396 */
1397 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1398 setBuilderClassJavaFileHandle(
Bharat saraswalc0e04842016-05-12 13:16:57 +05301399 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode,
1400 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301401 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301402 * Create impl class file.
1403 */
1404 if ((fileType & IMPL_CLASS_MASK) != 0) {
1405 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1406 setImplClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301407 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent()));
1408 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301409 * Append impl class to builder class and close it.
1410 */
1411 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1412 }
1413 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +05301414 if (isAugmentationHolderExtended(getJavaExtendsListHolder().getExtendsList())) {
1415 addAugmentedInfoImport(curNode, imports, false);
1416 addArrayListImport(curNode, imports, false);
1417 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301418 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301419 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301420 * Close all the file handles.
1421 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301422 freeTemporaryResources(false);
1423 }
1424
1425 /**
1426 * Adds imports for ToString and HashCodeMethod.
1427 *
1428 * @param curNode current YANG node
1429 * @param imports import list
1430 * @return import list
1431 */
1432 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1433 imports.add(getJavaImportData().getImportForHashAndEquals());
1434 imports.add(getJavaImportData().getImportForToString());
1435 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301436 }
1437
1438 /**
1439 * Removes case import info from import list.
1440 *
1441 * @param imports list of imports
1442 * @return import for class
1443 */
1444 private List<String> removeCaseImport(List<String> imports) {
1445 if (imports != null && caseImportInfo != null) {
1446 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1447 SEMI_COLAN + NEW_LINE;
1448 imports.remove(caseImport);
1449 }
1450 return imports;
1451 }
1452
1453 /**
1454 * Removes all temporary file handles.
1455 *
1456 * @param isErrorOccurred when translator fails to generate java files we
1457 * need to close all open file handles include temporary files
1458 * and java files.
1459 * @throws IOException when failed to delete the temporary files
1460 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301461 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301462 throws IOException {
1463 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301464 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301465 * Close all java file handles and when error occurs delete the files.
1466 */
1467 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1468 closeFile(getInterfaceJavaFileHandle(), isError);
1469 }
1470 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
1471 closeFile(getBuilderClassJavaFileHandle(), isError);
1472 }
1473 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1474 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1475 }
1476 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
1477 closeFile(getImplClassJavaFileHandle(), true);
1478 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301479
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301480 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301481 * Close all temporary file handles and delete the files.
1482 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301483 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1484 closeFile(getGetterImplTempFileHandle(), true);
1485 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301486 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1487 closeFile(getAttributesTempFileHandle(), true);
1488 }
1489 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1490 closeFile(getHashCodeImplTempFileHandle(), true);
1491 }
1492 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1493 closeFile(getToStringImplTempFileHandle(), true);
1494 }
1495 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1496 closeFile(getEqualsImplTempFileHandle(), true);
1497 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301498 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1499 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301500 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301501 }
1502
1503 /**
1504 * Returns if the attribute needs to be accessed in a qualified manner or
1505 * not, if it needs to be imported, then the same needs to be done.
1506 *
1507 * @param importInfo import info for the current attribute being added
1508 * @return status of the qualified access to the attribute
1509 */
1510 public boolean getIsQualifiedAccessOrAddToImportList(
1511 JavaQualifiedTypeInfo importInfo) {
1512 boolean isImportPkgEqualCurNodePkg;
1513 if (importInfo.getClassInfo().contentEquals(
1514 getGeneratedJavaClassName())) {
1515 /*
1516 * if the current class name is same as the attribute class name,
1517 * then the attribute must be accessed in a qualified manner.
1518 */
1519 return true;
1520 } else if (importInfo.getPkgInfo() != null) {
1521 /*
1522 * If the attribute type is having the package info, it is contender
1523 * for import list and also need to check if it needs to be a
1524 * qualified access.
1525 */
1526 isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(importInfo);
1527 if (!isImportPkgEqualCurNodePkg) {
1528 /*
1529 * If the package of the attribute added is not same as the
1530 * current class package, then it must either be imported for
1531 * access or it must be a qualified access.
1532 */
1533 boolean isImportAdded = getJavaImportData().addImportInfo(importInfo);
1534 if (!isImportAdded) {
1535 /*
1536 * If the attribute type info is not imported, then it must
1537 * be a qualified access.
1538 */
1539 return true;
1540 }
1541 }
1542 }
1543 return false;
1544 }
1545
1546 /**
1547 * Checks if the import info is same as the package of the current generated
1548 * java file.
1549 *
1550 * @param importInfo import info for an attribute
1551 * @return true if the import info is same as the current nodes package
1552 * false otherwise
1553 */
1554 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1555 return getJavaFileInfo().getPackage()
1556 .contentEquals(importInfo.getPkgInfo());
1557 }
1558}