blob: b0a65a44d72433ad25f972c087c3967badf163e0 [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;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053032
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
34import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
39import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Bharat saraswalc0e04842016-05-12 13:16:57 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
44import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
45import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
49import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswalc0e04842016-05-12 13:16:57 +053050import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053051import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
53import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
54import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
55import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053056import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
57import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053058import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
59import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053060import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
62import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053063import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
65import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Bharat saraswalc0e04842016-05-12 13:16:57 +053066import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
68import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
69import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
70import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
73import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
74import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
76import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
77import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
78import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053080import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.detectCollisionBwParentAndChildForImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053081import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
Bharat saraswalc0e04842016-05-12 13:16:57 +053082import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
83import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.prepareJavaFileGeneratorForExtendsList;
84import 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 */
134 private List<String> extendsList = new ArrayList<>();
Bharat saraswalc0e04842016-05-12 13:16:57 +0530135
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 /**
137 * File type extension for java classes.
138 */
139 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530140
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530141 /**
142 * File type extension for temporary classes.
143 */
144 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530145
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530146 /**
147 * Folder suffix for temporary files folder.
148 */
149 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530150
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530151 /**
152 * File name for getter method.
153 */
154 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530155
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530156 /**
157 * File name for setter method.
158 */
159 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530160
161 /**
162 * File name for getter method implementation.
163 */
164 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
165
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530166 /**
167 * File name for setter method implementation.
168 */
169 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530170
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530171 /**
172 * File name for attributes.
173 */
174 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530175
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530176 /**
177 * File name for to string method.
178 */
179 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530180
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530181 /**
182 * File name for hash code method.
183 */
184 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530185
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530186 /**
187 * File name for equals method.
188 */
189 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530190
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530191 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530192 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530193 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530194 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530195
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530196 /**
197 * File name for interface java file name suffix.
198 */
199 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530200
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530201 /**
202 * File name for builder interface file name suffix.
203 */
204 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530205
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 /**
207 * File name for builder class file name suffix.
208 */
209 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530210
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530211 /**
212 * File name for impl class file name suffix.
213 */
214 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530215
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530216 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530217 * Java file handle for interface file.
218 */
219 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530220
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530221 /**
222 * Java file handle for builder interface file.
223 */
224 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530225
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530226 /**
227 * Java file handle for builder class file.
228 */
229 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530230
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530231 /**
232 * Java file handle for impl class file.
233 */
234 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530235
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530236 /**
237 * Temporary file handle for attribute.
238 */
239 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530240
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530241 /**
242 * Temporary file handle for getter of interface.
243 */
244 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530245
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530246 /**
247 * Temporary file handle for setter of interface.
248 */
249 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530250
251 /**
252 * Temporary file handle for getter of class.
253 */
254 private File getterImplTempFileHandle;
255
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530256 /**
257 * Temporary file handle for setter of class.
258 */
259 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530260
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530261 /**
262 * Temporary file handle for hash code method of class.
263 */
264 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530265
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530266 /**
267 * Temporary file handle for equals method of class.
268 */
269 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530270
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530271 /**
272 * Temporary file handle for to string method of class.
273 */
274 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530275
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530276 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530277 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530278 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530279 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530280
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530281 /**
282 * Import info for case.
283 */
284 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530285
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530286 /**
287 * Is attribute added.
288 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530289 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530290
Bharat saraswalc0e04842016-05-12 13:16:57 +0530291 /**
292 * Creates an instance of temp java fragment files.
293 */
Ray Milkey994c4982016-05-11 18:39:39 +0000294 public TempJavaFragmentFiles() {
295 }
296
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530297 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530298 * Retrieves the absolute path where the file needs to be generated.
299 *
300 * @return absolute path where the file needs to be generated
301 */
302 private String getAbsoluteDirPath() {
303 return absoluteDirPath;
304 }
305
306 /**
307 * Sets absolute path where the file needs to be generated.
308 *
309 * @param absoluteDirPath absolute path where the file needs to be
310 * generated.
311 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530312 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530313 this.absoluteDirPath = absoluteDirPath;
314 }
315
316 /**
317 * Sets the generated java file information.
318 *
319 * @param javaFileInfo generated java file information
320 */
321 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
322 this.javaFileInfo = javaFileInfo;
323 }
324
325 /**
326 * Retrieves the generated java file information.
327 *
328 * @return generated java file information
329 */
330 public JavaFileInfo getJavaFileInfo() {
331 return javaFileInfo;
332 }
333
334 /**
335 * Retrieves the generated temp files.
336 *
337 * @return generated temp files
338 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530339 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530340 return generatedTempFiles;
341 }
342
343 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530344 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530345 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530346 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530347 generatedTempFiles = 0;
348 }
349
350 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530351 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530352 *
353 * @param generatedTempFile generated file
354 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530355 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530356 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530357 setGeneratedTempFiles(generatedTempFiles);
358 }
359
360 /**
361 * Sets generated file files.
362 *
363 * @param generatedTempFile generated file
364 */
365 void setGeneratedTempFiles(int fileType) {
366 generatedTempFiles = fileType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530367 }
368
369 /**
370 * Retrieves the generated Java files.
371 *
372 * @return generated Java files
373 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530374 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530375 return getJavaFileInfo().getGeneratedFileTypes();
376 }
377
378 /**
379 * Retrieves the mapped Java class name.
380 *
381 * @return mapped Java class name
382 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530383 String getGeneratedJavaClassName() {
janani b4a6711a2016-05-17 13:12:22 +0530384 return getCapitalCase(getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530385 }
386
387 /**
388 * Retrieves the import data for the generated Java file.
389 *
390 * @return import data for the generated Java file
391 */
392 public JavaImportData getJavaImportData() {
393 return javaImportData;
394 }
395
396 /**
397 * Sets import data for the generated Java file.
398 *
399 * @param javaImportData import data for the generated Java file
400 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530401 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530402 this.javaImportData = javaImportData;
403 }
404
405 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530406 * Retrieves the status of any attributes added.
407 *
408 * @return status of any attributes added
409 */
410 public boolean isAttributePresent() {
411 return isAttributePresent;
412 }
413
414 /**
415 * Sets status of any attributes added.
416 *
417 * @param attributePresent status of any attributes added
418 */
419 public void setAttributePresent(boolean attributePresent) {
420 isAttributePresent = attributePresent;
421 }
422
423 /**
424 * Returns getter methods's temporary file handle.
425 *
426 * @return temporary file handle
427 */
428 public File getGetterInterfaceTempFileHandle() {
429 return getterInterfaceTempFileHandle;
430 }
431
432 /**
433 * Sets to getter method's temporary file handle.
434 *
435 * @param getterForInterface file handle for to getter method
436 */
437 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
438 getterInterfaceTempFileHandle = getterForInterface;
439 }
440
441 /**
442 * Returns setter method's temporary file handle.
443 *
444 * @return temporary file handle
445 */
446 public File getSetterInterfaceTempFileHandle() {
447 return setterInterfaceTempFileHandle;
448 }
449
450 /**
451 * Sets to setter method's temporary file handle.
452 *
453 * @param setterForInterface file handle for to setter method
454 */
455 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
456 setterInterfaceTempFileHandle = setterForInterface;
457 }
458
459 /**
460 * Returns setter method's impl's temporary file handle.
461 *
462 * @return temporary file handle
463 */
464 public File getSetterImplTempFileHandle() {
465 return setterImplTempFileHandle;
466 }
467
468 /**
469 * Sets to setter method's impl's temporary file handle.
470 *
471 * @param setterImpl file handle for to setter method's implementation class
472 */
473 private void setSetterImplTempFileHandle(File setterImpl) {
474 setterImplTempFileHandle = setterImpl;
475 }
476
477 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530478 * Returns from string method's temporary file handle.
479 *
480 * @return from string method's temporary file handle
481 */
482 public File getFromStringImplTempFileHandle() {
483 return fromStringImplTempFileHandle;
484 }
485
486 /**
487 * Sets from string method's temporary file handle.
488 *
489 * @param fromStringImplTempFileHandle from string method's temporary file
490 * handle
491 */
492 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
493 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
494 }
495
496 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530497 * Creates an instance of temporary java code fragment.
498 *
499 * @param javaFileInfo generated java file information
500 * @throws IOException when fails to create new file handle
501 */
502 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
503 throws IOException {
504 setExtendsList(new ArrayList<>());
505 setJavaImportData(new JavaImportData());
506 setJavaFileInfo(javaFileInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530507 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
508 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530509
510 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530511 * Initialize getter when generation file type matches to interface
512 * mask.
513 */
514 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
515 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
516 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530517
518 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530519 * Initialize getter and setter when generation file type matches to
520 * builder interface mask.
521 */
522 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
523 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
524 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
525 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530526
527 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530528 * Initialize getterImpl, setterImpl and attributes when generation file
529 * type matches to builder class mask.
530 */
531 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
532 addGeneratedTempFile(ATTRIBUTES_MASK);
533 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
534 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
535 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530536
537 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530538 * Initialize getterImpl, attributes, constructor, hash code, equals and
539 * to strings when generation file type matches to impl class mask.
540 */
541 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
542 addGeneratedTempFile(ATTRIBUTES_MASK);
543 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530544 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
545 addGeneratedTempFile(EQUALS_IMPL_MASK);
546 addGeneratedTempFile(TO_STRING_IMPL_MASK);
547 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530548
549 /*
550 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530551 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530552 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530553 addGeneratedTempFile(ATTRIBUTES_MASK);
554 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
555 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
556 addGeneratedTempFile(EQUALS_IMPL_MASK);
557 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530558 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530559 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530560
561 /*
Bharat saraswalc0e04842016-05-12 13:16:57 +0530562 * Initialize temp files to generate enum class.
563 */
564 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
565 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
566 }
567 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530568 * Initialize getter and setter when generation file type matches to
569 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530570 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530571 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
572 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
573 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530574 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530575 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530576 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530577
578 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530579 * Set temporary file handles.
580 */
581 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
582 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
583 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530584
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530585 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
586 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
587 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530588
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530589 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
590 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
591 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530592
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530593 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
594 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
595 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530596
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530597 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
598 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
599 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530600
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530601 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
602 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
603 }
604 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
605 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
606 }
607 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
608 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
609 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530610 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
611 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530612 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530613
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530614 }
615
616 /**
617 * Returns java file handle for interface file.
618 *
619 * @return java file handle for interface file
620 */
621 private File getInterfaceJavaFileHandle() {
622 return interfaceJavaFileHandle;
623 }
624
625 /**
626 * Sets the java file handle for interface file.
627 *
628 * @param interfaceJavaFileHandle java file handle
629 */
630 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
631 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
632 }
633
634 /**
635 * Returns java file handle for builder interface file.
636 *
637 * @return java file handle for builder interface file
638 */
639 private File getBuilderInterfaceJavaFileHandle() {
640 return builderInterfaceJavaFileHandle;
641 }
642
643 /**
644 * Sets the java file handle for builder interface file.
645 *
646 * @param builderInterfaceJavaFileHandle java file handle
647 */
648 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
649 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
650 }
651
652 /**
653 * Returns java file handle for builder class file.
654 *
655 * @return java file handle for builder class file
656 */
657 private File getBuilderClassJavaFileHandle() {
658 return builderClassJavaFileHandle;
659 }
660
661 /**
662 * Sets the java file handle for builder class file.
663 *
664 * @param builderClassJavaFileHandle java file handle
665 */
666 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
667 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
668 }
669
670 /**
671 * Returns java file handle for impl class file.
672 *
673 * @return java file handle for impl class file
674 */
675 private File getImplClassJavaFileHandle() {
676 return implClassJavaFileHandle;
677 }
678
679 /**
680 * Sets the java file handle for impl class file.
681 *
682 * @param implClassJavaFileHandle java file handle
683 */
684 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
685 this.implClassJavaFileHandle = implClassJavaFileHandle;
686 }
687
688 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530689 * Returns attribute's temporary file handle.
690 *
691 * @return temporary file handle
692 */
693 public File getAttributesTempFileHandle() {
694 return attributesTempFileHandle;
695 }
696
697 /**
698 * Sets attribute's temporary file handle.
699 *
700 * @param attributeForClass file handle for attribute
701 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530702 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530703 attributesTempFileHandle = attributeForClass;
704 }
705
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530706 /**
707 * Returns getter method's impl's temporary file handle.
708 *
709 * @return temporary file handle
710 */
711 public File getGetterImplTempFileHandle() {
712 return getterImplTempFileHandle;
713 }
714
715 /**
716 * Sets to getter method's impl's temporary file handle.
717 *
718 * @param getterImpl file handle for to getter method's impl
719 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530720 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530721 getterImplTempFileHandle = getterImpl;
722 }
723
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530724 /**
725 * Returns hash code method's temporary file handle.
726 *
727 * @return temporary file handle
728 */
729 public File getHashCodeImplTempFileHandle() {
730 return hashCodeImplTempFileHandle;
731 }
732
733 /**
734 * Sets hash code method's temporary file handle.
735 *
736 * @param hashCodeMethod file handle for hash code method
737 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530738 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530739 hashCodeImplTempFileHandle = hashCodeMethod;
740 }
741
742 /**
743 * Returns equals mehtod's temporary file handle.
744 *
745 * @return temporary file handle
746 */
747 public File getEqualsImplTempFileHandle() {
748 return equalsImplTempFileHandle;
749 }
750
751 /**
752 * Sets equals method's temporary file handle.
753 *
754 * @param equalsMethod file handle for to equals method
755 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530756 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530757 equalsImplTempFileHandle = equalsMethod;
758 }
759
760 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530761 * Returns to string method's temporary file handle.
762 *
763 * @return temporary file handle
764 */
765 public File getToStringImplTempFileHandle() {
766 return toStringImplTempFileHandle;
767 }
768
769 /**
770 * Sets to string method's temporary file handle.
771 *
772 * @param toStringMethod file handle for to string method
773 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530774 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530775 toStringImplTempFileHandle = toStringMethod;
776 }
777
778 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530779 * Returns list of classes to be extended by generated files.
780 *
781 * @return list of classes to be extended by generated files
782 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530783 List<String> getExtendsList() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530784 return extendsList;
785 }
786
787 /**
788 * Sets class to be extended by generated file.
789 *
790 * @param extendsList list of classes to be extended
791 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530792 void setExtendsList(List<String> extendsList) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530793 this.extendsList = extendsList;
794 }
795
796 /**
797 * Adds class to the extends list.
798 *
799 * @param extend class to be extended
800 */
801 public void addToExtendsList(String extend) {
802 getExtendsList().add(extend);
803 }
804
805 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530806 * Adds attribute for class.
807 *
808 * @param attr attribute info
809 * @throws IOException when fails to append to temporary file
810 */
811 private void addAttribute(JavaAttributeInfo attr)
812 throws IOException {
813 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
814 }
815
816 /**
817 * Adds getter for interface.
818 *
819 * @param attr attribute info
820 * @throws IOException when fails to append to temporary file
821 */
822 private void addGetterForInterface(JavaAttributeInfo attr)
823 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530824 appendToFile(getGetterInterfaceTempFileHandle(),
825 getGetterString(attr, getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530826 }
827
828 /**
829 * Adds setter for interface.
830 *
831 * @param attr attribute info
832 * @throws IOException when fails to append to temporary file
833 */
834 private void addSetterForInterface(JavaAttributeInfo attr)
835 throws IOException {
836 appendToFile(getSetterInterfaceTempFileHandle(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530837 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530838 }
839
840 /**
841 * Adds setter's implementation for class.
842 *
843 * @param attr attribute info
844 * @throws IOException when fails to append to temporary file
845 */
846 private void addSetterImpl(JavaAttributeInfo attr)
847 throws IOException {
848 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530849 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
850 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530851 NEW_LINE);
852 }
853
854 /**
855 * Adds getter method's impl for class.
856 *
857 * @param attr attribute info
858 * @throws IOException when fails to append to temporary file
859 */
860 private void addGetterImpl(JavaAttributeInfo attr)
861 throws IOException {
862 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
863 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
864 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
865 getGeneratedJavaFiles()) + NEW_LINE);
866 } else {
janani b4a6711a2016-05-17 13:12:22 +0530867 appendToFile(getGetterImplTempFileHandle(),
868 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false)
869 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530870 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530871 }
872
873 /**
874 * Adds build method for interface.
875 *
876 * @return build method for interface
877 * @throws IOException when fails to append to temporary file
878 */
879 String addBuildMethodForInterface()
880 throws IOException {
881 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName());
882 }
883
884 /**
885 * Adds build method's implementation for class.
886 *
887 * @return build method implementation for class
888 * @throws IOException when fails to append to temporary file
889 */
890 String addBuildMethodImpl()
891 throws IOException {
892 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
893 }
894
895 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530896 * Adds default constructor for class.
897 *
898 * @param modifier modifier for constructor.
899 * @param toAppend string which need to be appended with the class name
900 * @return default constructor for class
901 * @throws IOException when fails to append to file
902 */
903 String addDefaultConstructor(String modifier, String toAppend)
904 throws IOException {
905 return NEW_LINE + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier);
906 }
907
908 /**
909 * Adds default constructor for class.
910 *
911 * @return default constructor for class
912 * @throws IOException when fails to append to file
913 */
914 public String addOfMethod()
915 throws IOException {
916 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false)
917 + getOfMethod(getGeneratedJavaClassName(), null);
918 }
919
920 /**
921 * Adds hash code method for class.
922 *
923 * @param attr attribute info
924 * @throws IOException when fails to append to temporary file
925 */
926 private void addHashCodeMethod(JavaAttributeInfo attr)
927 throws IOException {
928 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
929 }
930
931 /**
932 * Adds equals method for class.
933 *
934 * @param attr attribute info
935 * @throws IOException when fails to append to temporary file
936 */
937 private void addEqualsMethod(JavaAttributeInfo attr)
938 throws IOException {
939 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
940 }
941
942 /**
943 * Adds ToString method for class.
944 *
945 * @param attr attribute info
946 * @throws IOException when fails to append to temporary file
947 */
948 private void addToStringMethod(JavaAttributeInfo attr)
949 throws IOException {
950 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
951 }
952
953 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530954 * Adds from string method for union class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530955 *
Bharat saraswalc0e04842016-05-12 13:16:57 +0530956 * @param javaAttributeInfo type attribute info
957 * @param fromStringAttributeInfo from string attribute info
958 * @throws IOException when fails to append to temporary file
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530959 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530960 private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
961 JavaAttributeInfo fromStringAttributeInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530962 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530963 appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
964 fromStringAttributeInfo) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530965 }
966
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530967 /**
968 * Returns a temporary file handle for the specific file type.
969 *
970 * @param fileName file name
971 * @return temporary file handle
972 * @throws IOException when fails to create new file handle
973 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530974 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530975 throws IOException {
976 String path = getTempDirPath();
977 File dir = new File(path);
978 if (!dir.exists()) {
979 dir.mkdirs();
980 }
981 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
982 if (!file.exists()) {
983 file.createNewFile();
984 }
985 return file;
986 }
987
988 /**
989 * Returns a temporary file handle for the specific file type.
990 *
991 * @param fileName file name
992 * @return temporary file handle
993 * @throws IOException when fails to create new file handle
994 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530995 File getJavaFileHandle(String fileName) throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530996 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
997 }
998
999 /**
1000 * Returns data from the temporary files.
1001 *
1002 * @param file temporary file handle
1003 * @return stored data from temporary files
1004 * @throws IOException when failed to get data from the given file
1005 */
1006 public String getTemporaryDataFromFileHandle(File file)
1007 throws IOException {
1008 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
1031 * @return attribute string
1032 */
1033 private String parseAttribute(JavaAttributeInfo attr) {
1034 /*
1035 * TODO: check if this utility needs to be called or move to the caller
1036 */
janani b4a6711a2016-05-17 13:12:22 +05301037 String attributeName = getCamelCase(attr.getAttributeName(), null);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301038 if (attr.isQualifiedName()) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301039 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
1040 attr.getImportInfo().getClassInfo(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301041 attributeName, attr.isListAttr());
1042 } else {
1043 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1044 attr.isListAttr());
1045 }
1046 }
1047
1048 /**
1049 * Appends content to temporary file.
1050 *
1051 * @param file temporary file
1052 * @param data data to be appended
1053 * @throws IOException when fails to append to file
1054 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301055 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301056 throws IOException {
1057 try {
1058 insertDataIntoJavaFile(file, data);
1059 } catch (IOException ex) {
1060 throw new IOException("failed to write in temp file.");
1061 }
1062 }
1063
1064 /**
1065 * Adds current node info as and attribute to the parent generated file.
1066 *
1067 * @param curNode current node which needs to be added as an attribute in
1068 * the parent generated code
1069 * @param isList is list construct
1070 * @throws IOException IO operation exception
1071 */
1072 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
1073 boolean isList)
1074 throws IOException {
1075 YangNode parent = getParentNodeInGenCode(curNode);
1076 if (!(parent instanceof JavaCodeGenerator)) {
1077 throw new TranslatorException("missing parent node to contain current node info in generated file");
1078 }
1079 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1080 parent, isList);
1081 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1082 throw new TranslatorException("missing parent temp file handle");
1083 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301084 getNodesInterfaceFragmentFiles(parent)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301085 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1086 }
1087
1088 /**
1089 * Creates an attribute info object corresponding to a data model node and
1090 * return it.
1091 *
1092 * @param curNode current data model node for which the java code generation
1093 * is being handled
1094 * @param parentNode parent node in which the current node is an attribute
1095 * @param isListNode is the current added attribute needs to be a list
1096 * @return AttributeInfo attribute details required to add in temporary
1097 * files
1098 */
1099 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
1100 YangNode curNode, YangNode parentNode, boolean isListNode) {
1101 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
1102 /*
1103 * Get the import info corresponding to the attribute for import in
1104 * generated java files or qualified access
1105 */
1106 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
janani b4a6711a2016-05-17 13:12:22 +05301107 getCapitalCase(curNodeName));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301108 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
1109 throw new TranslatorException("Parent node does not have file info");
1110 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301111 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(parentNode);
Bharat saraswalc0e04842016-05-12 13:16:57 +05301112 boolean isQualified = true;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301113 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301114 if (isListNode) {
1115 parentImportData.setIfListImported(true);
1116 }
1117 if (!detectCollisionBwParentAndChildForImport(curNode, qualifiedTypeInfo)) {
1118 parentImportData.addImportInfo(qualifiedTypeInfo);
1119 isQualified = false;
1120 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301121 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1122 }
1123
Bharat saraswalc0e04842016-05-12 13:16:57 +05301124 /**
1125 * Returns interface fragment files for node.
1126 *
1127 * @param node YANG node
1128 * @return interface fragment files for node
1129 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301130 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1131 TempJavaFragmentFiles tempJavaFragmentFiles;
1132 if (node instanceof RpcNotificationContainer) {
1133 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1134 .getTempJavaCodeFragmentFiles()
1135 .getServiceTempFiles();
1136 } else {
1137 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1138 .getTempJavaCodeFragmentFiles()
1139 .getBeanTempFiles();
1140 }
1141 return tempJavaFragmentFiles;
1142 }
1143
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301144 /**
1145 * Adds parent's info to current node import list.
1146 *
1147 * @param curNode current node for which import list needs to be updated
1148 */
1149 public void addParentInfoInCurNodeTempFile(YangNode curNode) {
1150 caseImportInfo = new JavaQualifiedTypeInfo();
1151 YangNode parent = getParentNodeInGenCode(curNode);
1152 if (!(parent instanceof JavaCodeGenerator)) {
1153 throw new TranslatorException("missing parent node to contain current node info in generated file");
1154 }
1155 if (!(curNode instanceof JavaFileInfoContainer)) {
1156 throw new TranslatorException("missing java file information to get the package details "
1157 + "of attribute corresponding to child node");
1158 }
janani b4a6711a2016-05-17 13:12:22 +05301159 caseImportInfo.setClassInfo(getCapitalCase(getCamelCase(parent.getName(), null)));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301160 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
1161 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1162 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo);
1163 }
1164
1165 /**
1166 * Adds leaf attributes in generated files.
1167 *
1168 * @param listOfLeaves list of YANG leaf
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301169 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301170 * @throws IOException IO operation fail
1171 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301172 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1173 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301174 throws IOException {
1175 if (listOfLeaves != null) {
1176 for (YangLeaf leaf : listOfLeaves) {
1177 if (!(leaf instanceof JavaLeafInfoContainer)) {
1178 throw new TranslatorException("Leaf does not have java information");
1179 }
1180 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
1181 javaLeaf.updateJavaQualifiedInfo();
1182 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1183 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301184 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1185 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301186 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1187 false);
1188 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1189 }
1190 }
1191 }
1192
1193 /**
1194 * Adds leaf list's attributes in generated files.
1195 *
1196 * @param listOfLeafList list of YANG leaves
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301197 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301198 * @throws IOException IO operation fail
1199 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301200 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301201 throws IOException {
1202 if (listOfLeafList != null) {
1203 for (YangLeafList leafList : listOfLeafList) {
1204 if (!(leafList instanceof JavaLeafInfoContainer)) {
1205 throw new TranslatorException("Leaf-list does not have java information");
1206 }
1207 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
1208 javaLeaf.updateJavaQualifiedInfo();
Bharat saraswalc0e04842016-05-12 13:16:57 +05301209 getJavaImportData().setIfListImported(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301210 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1211 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301212 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1213 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301214 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1215 true);
1216 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1217 }
1218 }
1219 }
1220
1221 /**
1222 * Adds all the leaves in the current data model node as part of the
1223 * generated temporary file.
1224 *
1225 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301226 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301227 * @throws IOException IO operation fail
1228 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301229 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1230 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301231 throws IOException {
1232 if (!(curNode instanceof YangLeavesHolder)) {
1233 throw new TranslatorException("Data model node does not have any leaves");
1234 }
1235 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301236 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1237 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301238 }
1239
1240 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301241 * Adds the new attribute info to the target generated temporary files.
1242 *
1243 * @param newAttrInfo the attribute info that needs to be added to temporary
1244 * files
1245 * @throws IOException IO operation fail
1246 */
1247 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1248 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301249 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301250 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1251 addAttribute(newAttrInfo);
1252 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301253
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301254 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
1255 addGetterForInterface(newAttrInfo);
1256 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301257
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301258 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
1259 addSetterForInterface(newAttrInfo);
1260 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301261
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301262 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1263 addSetterImpl(newAttrInfo);
1264 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301265
1266 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1267 addGetterImpl(newAttrInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301268 }
1269 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1270 addHashCodeMethod(newAttrInfo);
1271 }
1272 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1273 addEqualsMethod(newAttrInfo);
1274 }
1275 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1276 addToStringMethod(newAttrInfo);
1277 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301278
1279 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1280 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo);
1281 /*
1282 * Create a new java attribute info with qualified information of
1283 * wrapper classes.
1284 */
1285 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
1286 newAttrInfo.getAttributeName(),
1287 newAttrInfo.getAttributeType(),
1288 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
1289
1290 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301291 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301292 }
1293
1294 /**
1295 * Returns java class name.
1296 *
1297 * @param suffix for the class name based on the file type
1298 * @return java class name
1299 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301300 String getJavaClassName(String suffix) {
janani b4a6711a2016-05-17 13:12:22 +05301301 return getCapitalCase(getJavaFileInfo().getJavaName()) + suffix;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301302 }
1303
1304 /**
1305 * Returns the directory path.
1306 *
1307 * @return directory path
1308 */
1309 private String getDirPath() {
1310 return getJavaFileInfo().getPackageFilePath();
1311 }
1312
1313 /**
1314 * Constructs java code exit.
1315 *
1316 * @param fileType generated file type
1317 * @param curNode current YANG node
1318 * @throws IOException when fails to generate java files
1319 */
1320 public void generateJavaFile(int fileType, YangNode curNode)
1321 throws IOException {
1322 List<String> imports = new ArrayList<>();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301323 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301324 imports = getJavaImportData().getImports();
1325 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301326 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301327 * Prepares java file generator for extends list.
1328 */
1329 prepareJavaFileGeneratorForExtendsList(getExtendsList());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301330 createPackage(curNode);
1331
1332 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301333 * Generate java code.
1334 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301335 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1336 BUILDER_INTERFACE_MASK) != 0) {
1337 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301338 * Adds import for case.
1339 */
1340 if (curNode instanceof YangCase) {
Bharat saraswalc0e04842016-05-12 13:16:57 +05301341 List<String> importData =
1342 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1343 .getBeanTempFiles().getJavaImportData().getImports();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301344 for (String importInfo : importData) {
1345 if (!imports.contains(importInfo)) {
1346 imports.add(importInfo);
1347 }
1348 }
1349 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301350 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301351 * Adds import for HasAugmentation class.
1352 */
1353 if (isHasAugmentationExtended(getExtendsList())) {
1354 addHasAugmentationImport(curNode, imports, true);
1355 }
1356 if (isAugmentedInfoExtended(getExtendsList())) {
1357 addAugmentedInfoImport(curNode, imports, true);
1358 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301359 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301360 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301361 * Create interface file.
1362 */
1363 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1364 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301365 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1366 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301367 * Create builder interface file.
1368 */
1369 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1370 setBuilderInterfaceJavaFileHandle(
1371 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1372 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301373 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1374 isAttributePresent()));
1375 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301376 * Append builder interface file to interface file and close it.
1377 */
1378 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1379 }
1380 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
1381 if (isHasAugmentationExtended(getExtendsList())) {
1382 addHasAugmentationImport(curNode, imports, false);
1383 }
1384 if (isAugmentedInfoExtended(getExtendsList())) {
1385 addAugmentedInfoImport(curNode, imports, false);
1386 }
1387 if (curNode instanceof YangCase) {
1388 removeCaseImport(imports);
1389 }
1390 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301391 if ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301392 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301393 addImportsToStringAndHasCodeMethods(curNode, imports);
1394 }
1395 if (isHasAugmentationExtended(getExtendsList())) {
1396 addAugmentedInfoImport(curNode, imports, true);
1397 addArrayListImport(curNode, imports, true);
1398 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301399 sortImports(imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301400 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301401 * Create builder class file.
1402 */
1403 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1404 setBuilderClassJavaFileHandle(
Bharat saraswalc0e04842016-05-12 13:16:57 +05301405 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode,
1406 isAttributePresent()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301407 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301408 * Create impl class file.
1409 */
1410 if ((fileType & IMPL_CLASS_MASK) != 0) {
1411 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1412 setImplClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301413 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent()));
1414 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301415 * Append impl class to builder class and close it.
1416 */
1417 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1418 }
1419 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
1420 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301421
1422 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301423 * Close all the file handles.
1424 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301425 freeTemporaryResources(false);
1426 }
1427
1428 /**
1429 * Adds imports for ToString and HashCodeMethod.
1430 *
1431 * @param curNode current YANG node
1432 * @param imports import list
1433 * @return import list
1434 */
1435 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1436 imports.add(getJavaImportData().getImportForHashAndEquals());
1437 imports.add(getJavaImportData().getImportForToString());
1438 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301439 }
1440
1441 /**
1442 * Removes case import info from import list.
1443 *
1444 * @param imports list of imports
1445 * @return import for class
1446 */
1447 private List<String> removeCaseImport(List<String> imports) {
1448 if (imports != null && caseImportInfo != null) {
1449 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1450 SEMI_COLAN + NEW_LINE;
1451 imports.remove(caseImport);
1452 }
1453 return imports;
1454 }
1455
1456 /**
1457 * Removes all temporary file handles.
1458 *
1459 * @param isErrorOccurred when translator fails to generate java files we
1460 * need to close all open file handles include temporary files
1461 * and java files.
1462 * @throws IOException when failed to delete the temporary files
1463 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301464 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301465 throws IOException {
1466 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301467 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301468 * Close all java file handles and when error occurs delete the files.
1469 */
1470 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1471 closeFile(getInterfaceJavaFileHandle(), isError);
1472 }
1473 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
1474 closeFile(getBuilderClassJavaFileHandle(), isError);
1475 }
1476 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1477 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1478 }
1479 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
1480 closeFile(getImplClassJavaFileHandle(), true);
1481 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301482
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301483 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301484 * Close all temporary file handles and delete the files.
1485 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301486 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1487 closeFile(getGetterImplTempFileHandle(), true);
1488 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301489 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1490 closeFile(getAttributesTempFileHandle(), true);
1491 }
1492 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1493 closeFile(getHashCodeImplTempFileHandle(), true);
1494 }
1495 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1496 closeFile(getToStringImplTempFileHandle(), true);
1497 }
1498 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1499 closeFile(getEqualsImplTempFileHandle(), true);
1500 }
Bharat saraswalc0e04842016-05-12 13:16:57 +05301501 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
1502 closeFile(getFromStringImplTempFileHandle(), true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301503 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301504 }
1505
1506 /**
1507 * Returns if the attribute needs to be accessed in a qualified manner or
1508 * not, if it needs to be imported, then the same needs to be done.
1509 *
1510 * @param importInfo import info for the current attribute being added
1511 * @return status of the qualified access to the attribute
1512 */
1513 public boolean getIsQualifiedAccessOrAddToImportList(
1514 JavaQualifiedTypeInfo importInfo) {
1515 boolean isImportPkgEqualCurNodePkg;
1516 if (importInfo.getClassInfo().contentEquals(
1517 getGeneratedJavaClassName())) {
1518 /*
1519 * if the current class name is same as the attribute class name,
1520 * then the attribute must be accessed in a qualified manner.
1521 */
1522 return true;
1523 } else if (importInfo.getPkgInfo() != null) {
1524 /*
1525 * If the attribute type is having the package info, it is contender
1526 * for import list and also need to check if it needs to be a
1527 * qualified access.
1528 */
1529 isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(importInfo);
1530 if (!isImportPkgEqualCurNodePkg) {
1531 /*
1532 * If the package of the attribute added is not same as the
1533 * current class package, then it must either be imported for
1534 * access or it must be a qualified access.
1535 */
1536 boolean isImportAdded = getJavaImportData().addImportInfo(importInfo);
1537 if (!isImportAdded) {
1538 /*
1539 * If the attribute type info is not imported, then it must
1540 * be a qualified access.
1541 */
1542 return true;
1543 }
1544 }
1545 }
1546 return false;
1547 }
1548
1549 /**
1550 * Checks if the import info is same as the package of the current generated
1551 * java file.
1552 *
1553 * @param importInfo import info for an attribute
1554 * @return true if the import info is same as the current nodes package
1555 * false otherwise
1556 */
1557 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1558 return getJavaFileInfo().getPackage()
1559 .contentEquals(importInfo.getPkgInfo());
1560 }
1561}