blob: bcfdf809564f37a61d871baa2e3f982dd49e8d82 [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;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
61import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
62import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
64import 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;
78import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
79import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport;
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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053082import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
Bharat saraswalc0e04842016-05-12 13:16:57 +053083import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
84import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.prepareJavaFileGeneratorForExtendsList;
85import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.sortImports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
87import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
88import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
89import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
90import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
91import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
92import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
94import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
95import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
96import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
97import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053098import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053099import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
100import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
102import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
103import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
104
105/**
106 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530107 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108 */
109public class TempJavaFragmentFiles {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530110
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111 /**
112 * Information about the java files being generated.
113 */
114 private JavaFileInfo javaFileInfo;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530115
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530116 /**
117 * Imported class info.
118 */
119 private JavaImportData javaImportData;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530120
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 /**
122 * The variable which guides the types of temporary files generated using
123 * the temporary generated file types mask.
124 */
125 private int generatedTempFiles;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530126
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 /**
128 * Absolute path where the target java file needs to be generated.
129 */
130 private String absoluteDirPath;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530131
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530132 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530133 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530134 */
135 private List<String> extendsList = new ArrayList<>();
Bharat saraswalc0e04842016-05-12 13:16:57 +0530136
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530137 /**
138 * File type extension for java classes.
139 */
140 private static final String JAVA_FILE_EXTENSION = ".java";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530141
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 /**
143 * File type extension for temporary classes.
144 */
145 private static final String TEMP_FILE_EXTENSION = ".tmp";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530146
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530147 /**
148 * Folder suffix for temporary files folder.
149 */
150 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530151
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 /**
153 * File name for getter method.
154 */
155 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530156
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 /**
158 * File name for setter method.
159 */
160 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530161
162 /**
163 * File name for getter method implementation.
164 */
165 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
166
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530167 /**
168 * File name for setter method implementation.
169 */
170 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530171
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530172 /**
173 * File name for attributes.
174 */
175 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530176
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530177 /**
178 * File name for to string method.
179 */
180 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530181
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530182 /**
183 * File name for hash code method.
184 */
185 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530187 /**
188 * File name for equals method.
189 */
190 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530191
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530193 * File name for from string method.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530194 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530195 private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530196
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530197 /**
198 * File name for interface java file name suffix.
199 */
200 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530201
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530202 /**
203 * File name for builder interface file name suffix.
204 */
205 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530206
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530207 /**
208 * File name for builder class file name suffix.
209 */
210 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530211
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530212 /**
213 * File name for impl class file name suffix.
214 */
215 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530216
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530217 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530218 * Java file handle for interface file.
219 */
220 private File interfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530221
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530222 /**
223 * Java file handle for builder interface file.
224 */
225 private File builderInterfaceJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530226
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530227 /**
228 * Java file handle for builder class file.
229 */
230 private File builderClassJavaFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530231
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530232 /**
233 * Java file handle for impl class file.
234 */
235 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530236
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530237 /**
238 * Temporary file handle for attribute.
239 */
240 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530242 /**
243 * Temporary file handle for getter of interface.
244 */
245 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530246
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530247 /**
248 * Temporary file handle for setter of interface.
249 */
250 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530251
252 /**
253 * Temporary file handle for getter of class.
254 */
255 private File getterImplTempFileHandle;
256
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530257 /**
258 * Temporary file handle for setter of class.
259 */
260 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530261
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530262 /**
263 * Temporary file handle for hash code method of class.
264 */
265 private File hashCodeImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530266
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530267 /**
268 * Temporary file handle for equals method of class.
269 */
270 private File equalsImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530271
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530272 /**
273 * Temporary file handle for to string method of class.
274 */
275 private File toStringImplTempFileHandle;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530276
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530277 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530278 * Temporary file handle for from string method of class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530279 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530280 private File fromStringImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530281
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530282 /**
283 * Import info for case.
284 */
285 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530286
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530287 /**
288 * Is attribute added.
289 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530290 private boolean isAttributePresent;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530291
Bharat saraswalc0e04842016-05-12 13:16:57 +0530292 /**
293 * Creates an instance of temp java fragment files.
294 */
Ray Milkey994c4982016-05-11 18:39:39 +0000295 public TempJavaFragmentFiles() {
296 }
297
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530298 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530299 * Retrieves the absolute path where the file needs to be generated.
300 *
301 * @return absolute path where the file needs to be generated
302 */
303 private String getAbsoluteDirPath() {
304 return absoluteDirPath;
305 }
306
307 /**
308 * Sets absolute path where the file needs to be generated.
309 *
310 * @param absoluteDirPath absolute path where the file needs to be
311 * generated.
312 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530313 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530314 this.absoluteDirPath = absoluteDirPath;
315 }
316
317 /**
318 * Sets the generated java file information.
319 *
320 * @param javaFileInfo generated java file information
321 */
322 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
323 this.javaFileInfo = javaFileInfo;
324 }
325
326 /**
327 * Retrieves the generated java file information.
328 *
329 * @return generated java file information
330 */
331 public JavaFileInfo getJavaFileInfo() {
332 return javaFileInfo;
333 }
334
335 /**
336 * Retrieves the generated temp files.
337 *
338 * @return generated temp files
339 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530340 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530341 return generatedTempFiles;
342 }
343
344 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530345 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530346 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530347 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530348 generatedTempFiles = 0;
349 }
350
351 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530352 * Adds to generated temporary files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530353 *
354 * @param generatedTempFile generated file
355 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530356 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530357 generatedTempFiles |= generatedTempFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +0530358 setGeneratedTempFiles(generatedTempFiles);
359 }
360
361 /**
362 * Sets generated file files.
363 *
364 * @param generatedTempFile generated file
365 */
366 void setGeneratedTempFiles(int fileType) {
367 generatedTempFiles = fileType;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530368 }
369
370 /**
371 * Retrieves the generated Java files.
372 *
373 * @return generated Java files
374 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530375 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530376 return getJavaFileInfo().getGeneratedFileTypes();
377 }
378
379 /**
380 * Retrieves the mapped Java class name.
381 *
382 * @return mapped Java class name
383 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530384 String getGeneratedJavaClassName() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530385 return getJavaFileInfo().getJavaName();
386 }
387
388 /**
389 * Retrieves the import data for the generated Java file.
390 *
391 * @return import data for the generated Java file
392 */
393 public JavaImportData getJavaImportData() {
394 return javaImportData;
395 }
396
397 /**
398 * Sets import data for the generated Java file.
399 *
400 * @param javaImportData import data for the generated Java file
401 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530402 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530403 this.javaImportData = javaImportData;
404 }
405
406 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530407 * Retrieves the status of any attributes added.
408 *
409 * @return status of any attributes added
410 */
411 public boolean isAttributePresent() {
412 return isAttributePresent;
413 }
414
415 /**
416 * Sets status of any attributes added.
417 *
418 * @param attributePresent status of any attributes added
419 */
420 public void setAttributePresent(boolean attributePresent) {
421 isAttributePresent = attributePresent;
422 }
423
424 /**
425 * Returns getter methods's temporary file handle.
426 *
427 * @return temporary file handle
428 */
429 public File getGetterInterfaceTempFileHandle() {
430 return getterInterfaceTempFileHandle;
431 }
432
433 /**
434 * Sets to getter method's temporary file handle.
435 *
436 * @param getterForInterface file handle for to getter method
437 */
438 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
439 getterInterfaceTempFileHandle = getterForInterface;
440 }
441
442 /**
443 * Returns setter method's temporary file handle.
444 *
445 * @return temporary file handle
446 */
447 public File getSetterInterfaceTempFileHandle() {
448 return setterInterfaceTempFileHandle;
449 }
450
451 /**
452 * Sets to setter method's temporary file handle.
453 *
454 * @param setterForInterface file handle for to setter method
455 */
456 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
457 setterInterfaceTempFileHandle = setterForInterface;
458 }
459
460 /**
461 * Returns setter method's impl's temporary file handle.
462 *
463 * @return temporary file handle
464 */
465 public File getSetterImplTempFileHandle() {
466 return setterImplTempFileHandle;
467 }
468
469 /**
470 * Sets to setter method's impl's temporary file handle.
471 *
472 * @param setterImpl file handle for to setter method's implementation class
473 */
474 private void setSetterImplTempFileHandle(File setterImpl) {
475 setterImplTempFileHandle = setterImpl;
476 }
477
478 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530479 * Returns from string method's temporary file handle.
480 *
481 * @return from string method's temporary file handle
482 */
483 public File getFromStringImplTempFileHandle() {
484 return fromStringImplTempFileHandle;
485 }
486
487 /**
488 * Sets from string method's temporary file handle.
489 *
490 * @param fromStringImplTempFileHandle from string method's temporary file
491 * handle
492 */
493 private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
494 this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
495 }
496
497 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530498 * Creates an instance of temporary java code fragment.
499 *
500 * @param javaFileInfo generated java file information
501 * @throws IOException when fails to create new file handle
502 */
503 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
504 throws IOException {
505 setExtendsList(new ArrayList<>());
506 setJavaImportData(new JavaImportData());
507 setJavaFileInfo(javaFileInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530508 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
509 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530510
511 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530512 * Initialize getter when generation file type matches to interface
513 * mask.
514 */
515 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
516 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
517 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530518
519 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530520 * Initialize getter and setter when generation file type matches to
521 * builder interface mask.
522 */
523 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
524 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
525 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
526 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530527
528 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530529 * Initialize getterImpl, setterImpl and attributes when generation file
530 * type matches to builder class mask.
531 */
532 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
533 addGeneratedTempFile(ATTRIBUTES_MASK);
534 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
535 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
536 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530537
538 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530539 * Initialize getterImpl, attributes, constructor, hash code, equals and
540 * to strings when generation file type matches to impl class mask.
541 */
542 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
543 addGeneratedTempFile(ATTRIBUTES_MASK);
544 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530545 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
546 addGeneratedTempFile(EQUALS_IMPL_MASK);
547 addGeneratedTempFile(TO_STRING_IMPL_MASK);
548 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530549
550 /*
551 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530552 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530553 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530554 addGeneratedTempFile(ATTRIBUTES_MASK);
555 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
556 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
557 addGeneratedTempFile(EQUALS_IMPL_MASK);
558 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530559 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530560 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530561
562 /*
Bharat saraswalc0e04842016-05-12 13:16:57 +0530563 * Initialize temp files to generate enum class.
564 */
565 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
566 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
567 }
568 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530569 * Initialize getter and setter when generation file type matches to
570 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530571 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530572 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
573 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
574 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530575 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530576 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530577 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530578
579 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530580 * Set temporary file handles.
581 */
582 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
583 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
584 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530585
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530586 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
587 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
588 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530589
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530590 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
591 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
592 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530593
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530594 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
595 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
596 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530597
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530598 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
599 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
600 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530601
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530602 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
603 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
604 }
605 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
606 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
607 }
608 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
609 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
610 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530611 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
612 setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530613 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530614
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530615 }
616
617 /**
618 * Returns java file handle for interface file.
619 *
620 * @return java file handle for interface file
621 */
622 private File getInterfaceJavaFileHandle() {
623 return interfaceJavaFileHandle;
624 }
625
626 /**
627 * Sets the java file handle for interface file.
628 *
629 * @param interfaceJavaFileHandle java file handle
630 */
631 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
632 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
633 }
634
635 /**
636 * Returns java file handle for builder interface file.
637 *
638 * @return java file handle for builder interface file
639 */
640 private File getBuilderInterfaceJavaFileHandle() {
641 return builderInterfaceJavaFileHandle;
642 }
643
644 /**
645 * Sets the java file handle for builder interface file.
646 *
647 * @param builderInterfaceJavaFileHandle java file handle
648 */
649 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
650 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
651 }
652
653 /**
654 * Returns java file handle for builder class file.
655 *
656 * @return java file handle for builder class file
657 */
658 private File getBuilderClassJavaFileHandle() {
659 return builderClassJavaFileHandle;
660 }
661
662 /**
663 * Sets the java file handle for builder class file.
664 *
665 * @param builderClassJavaFileHandle java file handle
666 */
667 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
668 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
669 }
670
671 /**
672 * Returns java file handle for impl class file.
673 *
674 * @return java file handle for impl class file
675 */
676 private File getImplClassJavaFileHandle() {
677 return implClassJavaFileHandle;
678 }
679
680 /**
681 * Sets the java file handle for impl class file.
682 *
683 * @param implClassJavaFileHandle java file handle
684 */
685 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
686 this.implClassJavaFileHandle = implClassJavaFileHandle;
687 }
688
689 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530690 * Returns attribute's temporary file handle.
691 *
692 * @return temporary file handle
693 */
694 public File getAttributesTempFileHandle() {
695 return attributesTempFileHandle;
696 }
697
698 /**
699 * Sets attribute's temporary file handle.
700 *
701 * @param attributeForClass file handle for attribute
702 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530703 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530704 attributesTempFileHandle = attributeForClass;
705 }
706
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530707 /**
708 * Returns getter method's impl's temporary file handle.
709 *
710 * @return temporary file handle
711 */
712 public File getGetterImplTempFileHandle() {
713 return getterImplTempFileHandle;
714 }
715
716 /**
717 * Sets to getter method's impl's temporary file handle.
718 *
719 * @param getterImpl file handle for to getter method's impl
720 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530721 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530722 getterImplTempFileHandle = getterImpl;
723 }
724
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530725 /**
726 * Returns hash code method's temporary file handle.
727 *
728 * @return temporary file handle
729 */
730 public File getHashCodeImplTempFileHandle() {
731 return hashCodeImplTempFileHandle;
732 }
733
734 /**
735 * Sets hash code method's temporary file handle.
736 *
737 * @param hashCodeMethod file handle for hash code method
738 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530739 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530740 hashCodeImplTempFileHandle = hashCodeMethod;
741 }
742
743 /**
744 * Returns equals mehtod's temporary file handle.
745 *
746 * @return temporary file handle
747 */
748 public File getEqualsImplTempFileHandle() {
749 return equalsImplTempFileHandle;
750 }
751
752 /**
753 * Sets equals method's temporary file handle.
754 *
755 * @param equalsMethod file handle for to equals method
756 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530757 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530758 equalsImplTempFileHandle = equalsMethod;
759 }
760
761 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530762 * Returns to string method's temporary file handle.
763 *
764 * @return temporary file handle
765 */
766 public File getToStringImplTempFileHandle() {
767 return toStringImplTempFileHandle;
768 }
769
770 /**
771 * Sets to string method's temporary file handle.
772 *
773 * @param toStringMethod file handle for to string method
774 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530775 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530776 toStringImplTempFileHandle = toStringMethod;
777 }
778
779 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530780 * Returns list of classes to be extended by generated files.
781 *
782 * @return list of classes to be extended by generated files
783 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530784 List<String> getExtendsList() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530785 return extendsList;
786 }
787
788 /**
789 * Sets class to be extended by generated file.
790 *
791 * @param extendsList list of classes to be extended
792 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530793 void setExtendsList(List<String> extendsList) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530794 this.extendsList = extendsList;
795 }
796
797 /**
798 * Adds class to the extends list.
799 *
800 * @param extend class to be extended
801 */
802 public void addToExtendsList(String extend) {
803 getExtendsList().add(extend);
804 }
805
806 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530807 * Adds attribute for class.
808 *
809 * @param attr attribute info
810 * @throws IOException when fails to append to temporary file
811 */
812 private void addAttribute(JavaAttributeInfo attr)
813 throws IOException {
814 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
815 }
816
817 /**
818 * Adds getter for interface.
819 *
820 * @param attr attribute info
821 * @throws IOException when fails to append to temporary file
822 */
823 private void addGetterForInterface(JavaAttributeInfo attr)
824 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530825 appendToFile(getGetterInterfaceTempFileHandle(),
826 getGetterString(attr, getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530827 }
828
829 /**
830 * Adds setter for interface.
831 *
832 * @param attr attribute info
833 * @throws IOException when fails to append to temporary file
834 */
835 private void addSetterForInterface(JavaAttributeInfo attr)
836 throws IOException {
837 appendToFile(getSetterInterfaceTempFileHandle(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530838 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530839 }
840
841 /**
842 * Adds setter's implementation for class.
843 *
844 * @param attr attribute info
845 * @throws IOException when fails to append to temporary file
846 */
847 private void addSetterImpl(JavaAttributeInfo attr)
848 throws IOException {
849 appendToFile(getSetterImplTempFileHandle(),
Bharat saraswalc0e04842016-05-12 13:16:57 +0530850 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles())
851 +
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530852 NEW_LINE);
853 }
854
855 /**
856 * Adds getter method's impl for class.
857 *
858 * @param attr attribute info
859 * @throws IOException when fails to append to temporary file
860 */
861 private void addGetterImpl(JavaAttributeInfo attr)
862 throws IOException {
863 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
864 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
865 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
866 getGeneratedJavaFiles()) + NEW_LINE);
867 } else {
868 appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
869 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
870 }
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 */
1037 String attributeName = getCamelCase(getSmallCase(attr.getAttributeName()), null);
1038 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,
1107 curNodeName);
1108 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 }
1159 caseImportInfo.setClassInfo(getCaptialCase(getCamelCase(parent.getName(), null)));
1160 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) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301301 return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
1302 }
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}