blob: 7ae7b289cae3a51b4bb87253da5c779c9dbabd3f [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;
22import java.util.Set;
23
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053024import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053025import org.onosproject.yangutils.datamodel.YangCase;
26import org.onosproject.yangutils.datamodel.YangEnum;
27import org.onosproject.yangutils.datamodel.YangEnumeration;
28import org.onosproject.yangutils.datamodel.YangLeaf;
29import org.onosproject.yangutils.datamodel.YangLeafList;
30import org.onosproject.yangutils.datamodel.YangLeavesHolder;
31import org.onosproject.yangutils.datamodel.YangNode;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053035
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
38import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPE_CLASS;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
45import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
47import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
48import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
50import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
51import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
52import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053053import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
54import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
55import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
56import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
57import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
58import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
59import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
60import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
61import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053062import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
63import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
64import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
65import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
66import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
67import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
68import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
70import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
72import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
73import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
74import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
77import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
78import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
80import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
81import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
82import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addHasAugmentationImport;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
84import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isAugmentedInfoExtended;
85import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils
86 .isHasAugmentationExtended;
87import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils
88 .prepareJavaFileGeneratorForExtendsList;
89import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
90import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
91import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
92import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
93import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
94import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
95import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053096import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
97import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
98import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
99import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
100import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
101import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
102import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530104import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
105import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
106import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
107
108/**
109 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530110 * Manages the common temp file required for Java file(s) generated.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111 */
112public class TempJavaFragmentFiles {
113 /**
114 * Information about the java files being generated.
115 */
116 private JavaFileInfo javaFileInfo;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530117
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 /**
119 * Imported class info.
120 */
121 private JavaImportData javaImportData;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530122
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530123 /**
124 * The variable which guides the types of temporary files generated using
125 * the temporary generated file types mask.
126 */
127 private int generatedTempFiles;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530128
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530129 /**
130 * Absolute path where the target java file needs to be generated.
131 */
132 private String absoluteDirPath;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530133
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530134 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530135 * Contains all the interface(s)/class name which will be extended by generated files.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 */
137 private List<String> extendsList = new ArrayList<>();
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530138
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530139 /**
140 * File type extension for java classes.
141 */
142 private static final String JAVA_FILE_EXTENSION = ".java";
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530143
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530144 /**
145 * File type extension for temporary classes.
146 */
147 private static final String TEMP_FILE_EXTENSION = ".tmp";
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530148
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530149 /**
150 * Folder suffix for temporary files folder.
151 */
152 private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530153
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530154 /**
155 * File name for getter method.
156 */
157 private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530158
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159 /**
160 * File name for setter method.
161 */
162 private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530163
164 /**
165 * File name for getter method implementation.
166 */
167 private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
168
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 /**
170 * File name for setter method implementation.
171 */
172 private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530173
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530174 /**
175 * File name for attributes.
176 */
177 private static final String ATTRIBUTE_FILE_NAME = "Attributes";
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530178
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530179 /**
180 * File name for to string method.
181 */
182 private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530183
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530184 /**
185 * File name for hash code method.
186 */
187 private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530188
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 /**
190 * File name for equals method.
191 */
192 private static final String EQUALS_METHOD_FILE_NAME = "Equals";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530193
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530194 /**
195 * File name for temporary enum class.
196 */
197 private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530198
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530199 /**
200 * File name for interface java file name suffix.
201 */
202 private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530203
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530204 /**
205 * File name for builder interface file name suffix.
206 */
207 private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530208
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530209 /**
210 * File name for builder class file name suffix.
211 */
212 private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530213
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530214 /**
215 * File name for impl class file name suffix.
216 */
217 private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530218
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530219 /**
220 * File name for enum class file name suffix.
221 */
222 private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530223
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530224 /**
225 * Java file handle for interface file.
226 */
227 private File interfaceJavaFileHandle;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530228
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530229 /**
230 * Java file handle for builder interface file.
231 */
232 private File builderInterfaceJavaFileHandle;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530233
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530234 /**
235 * Java file handle for builder class file.
236 */
237 private File builderClassJavaFileHandle;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530238
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530239 /**
240 * Java file handle for impl class file.
241 */
242 private File implClassJavaFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530243
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 /**
245 * Temporary file handle for attribute.
246 */
247 private File attributesTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530248
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530249 /**
250 * Temporary file handle for getter of interface.
251 */
252 private File getterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530253
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530254 /**
255 * Temporary file handle for setter of interface.
256 */
257 private File setterInterfaceTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530258
259 /**
260 * Temporary file handle for getter of class.
261 */
262 private File getterImplTempFileHandle;
263
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530264 /**
265 * Temporary file handle for setter of class.
266 */
267 private File setterImplTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530268
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530269 /**
270 * Temporary file handle for hash code method of class.
271 */
272 private File hashCodeImplTempFileHandle;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530273
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530274 /**
275 * Temporary file handle for equals method of class.
276 */
277 private File equalsImplTempFileHandle;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530278
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530279 /**
280 * Temporary file handle for to string method of class.
281 */
282 private File toStringImplTempFileHandle;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530283
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530284 /**
285 * Temporary file handle for enum class file.
286 */
287 private File enumClassTempFileHandle;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530288
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530289 /**
290 * Import info for case.
291 */
292 private JavaQualifiedTypeInfo caseImportInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530293
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530294 /**
295 * Is attribute added.
296 */
297 private boolean isAttributePresent = false;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530298
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530299 /**
300 * Current enum's value.
301 */
302 private int enumValue;
VinodKumarS-Huaweicbdf0f22016-05-11 09:34:21 +0530303
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530304 /*
305 * Java file handle for enum class.
306 */
307 private File enumClassJavaFileHandle;
308
309 /**
310 * Returns enum class java file handle.
311 *
312 * @return enum class java file handle
313 */
314 private File getEnumClassJavaFileHandle() {
315 return enumClassJavaFileHandle;
316 }
317
318 /**
319 * Sets enum class java file handle.
320 *
321 * @param enumClassJavaFileHandle enum class java file handle
322 */
323 private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) {
324 this.enumClassJavaFileHandle = enumClassJavaFileHandle;
325 }
326
327 /**
328 * Returns enum's value.
329 *
330 * @return enum's value
331 */
332 private int getEnumValue() {
333 return enumValue;
334 }
335
336 /**
337 * Sets enum's value.
338 *
339 * @param enumValue enum's value
340 */
341 private void setEnumValue(int enumValue) {
342 this.enumValue = enumValue;
343 }
344
345 /**
346 * Retrieves the absolute path where the file needs to be generated.
347 *
348 * @return absolute path where the file needs to be generated
349 */
350 private String getAbsoluteDirPath() {
351 return absoluteDirPath;
352 }
353
354 /**
355 * Sets absolute path where the file needs to be generated.
356 *
357 * @param absoluteDirPath absolute path where the file needs to be
358 * generated.
359 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530360 void setAbsoluteDirPath(String absoluteDirPath) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530361 this.absoluteDirPath = absoluteDirPath;
362 }
363
364 /**
365 * Sets the generated java file information.
366 *
367 * @param javaFileInfo generated java file information
368 */
369 public void setJavaFileInfo(JavaFileInfo javaFileInfo) {
370 this.javaFileInfo = javaFileInfo;
371 }
372
373 /**
374 * Retrieves the generated java file information.
375 *
376 * @return generated java file information
377 */
378 public JavaFileInfo getJavaFileInfo() {
379 return javaFileInfo;
380 }
381
382 /**
383 * Retrieves the generated temp files.
384 *
385 * @return generated temp files
386 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530387 int getGeneratedTempFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530388 return generatedTempFiles;
389 }
390
391 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530392 * Clears the generated file mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530393 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530394 void clearGeneratedTempFileMask() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530395 generatedTempFiles = 0;
396 }
397
398 /**
399 * Sets generated file files.
400 *
401 * @param generatedTempFile generated file
402 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530403 void addGeneratedTempFile(int generatedTempFile) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530404 generatedTempFiles |= generatedTempFile;
405 }
406
407 /**
408 * Retrieves the generated Java files.
409 *
410 * @return generated Java files
411 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530412 int getGeneratedJavaFiles() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530413 return getJavaFileInfo().getGeneratedFileTypes();
414 }
415
416 /**
417 * Retrieves the mapped Java class name.
418 *
419 * @return mapped Java class name
420 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530421 String getGeneratedJavaClassName() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530422 return getJavaFileInfo().getJavaName();
423 }
424
425 /**
426 * Retrieves the import data for the generated Java file.
427 *
428 * @return import data for the generated Java file
429 */
430 public JavaImportData getJavaImportData() {
431 return javaImportData;
432 }
433
434 /**
435 * Sets import data for the generated Java file.
436 *
437 * @param javaImportData import data for the generated Java file
438 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530439 void setJavaImportData(JavaImportData javaImportData) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530440 this.javaImportData = javaImportData;
441 }
442
443 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530444 * Retrieves the status of any attributes added.
445 *
446 * @return status of any attributes added
447 */
448 public boolean isAttributePresent() {
449 return isAttributePresent;
450 }
451
452 /**
453 * Sets status of any attributes added.
454 *
455 * @param attributePresent status of any attributes added
456 */
457 public void setAttributePresent(boolean attributePresent) {
458 isAttributePresent = attributePresent;
459 }
460
461 /**
462 * Returns getter methods's temporary file handle.
463 *
464 * @return temporary file handle
465 */
466 public File getGetterInterfaceTempFileHandle() {
467 return getterInterfaceTempFileHandle;
468 }
469
470 /**
471 * Sets to getter method's temporary file handle.
472 *
473 * @param getterForInterface file handle for to getter method
474 */
475 private void setGetterInterfaceTempFileHandle(File getterForInterface) {
476 getterInterfaceTempFileHandle = getterForInterface;
477 }
478
479 /**
480 * Returns setter method's temporary file handle.
481 *
482 * @return temporary file handle
483 */
484 public File getSetterInterfaceTempFileHandle() {
485 return setterInterfaceTempFileHandle;
486 }
487
488 /**
489 * Sets to setter method's temporary file handle.
490 *
491 * @param setterForInterface file handle for to setter method
492 */
493 private void setSetterInterfaceTempFileHandle(File setterForInterface) {
494 setterInterfaceTempFileHandle = setterForInterface;
495 }
496
497 /**
498 * Returns setter method's impl's temporary file handle.
499 *
500 * @return temporary file handle
501 */
502 public File getSetterImplTempFileHandle() {
503 return setterImplTempFileHandle;
504 }
505
506 /**
507 * Sets to setter method's impl's temporary file handle.
508 *
509 * @param setterImpl file handle for to setter method's implementation class
510 */
511 private void setSetterImplTempFileHandle(File setterImpl) {
512 setterImplTempFileHandle = setterImpl;
513 }
514
515 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530516 * Creates an instance of temporary java code fragment.
517 *
518 * @param javaFileInfo generated java file information
519 * @throws IOException when fails to create new file handle
520 */
521 TempJavaFragmentFiles(JavaFileInfo javaFileInfo)
522 throws IOException {
523 setExtendsList(new ArrayList<>());
524 setJavaImportData(new JavaImportData());
525 setJavaFileInfo(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530526 clearGeneratedTempFileMask();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530527 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
528 getJavaFileInfo().getPackageFilePath()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530529
530 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530531 * Initialize getter when generation file type matches to interface
532 * mask.
533 */
534 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
535 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
536 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530537
538 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530539 * Initialize getter and setter when generation file type matches to
540 * builder interface mask.
541 */
542 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
543 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
544 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
545 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530546
547 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530548 * Initialize getterImpl, setterImpl and attributes when generation file
549 * type matches to builder class mask.
550 */
551 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
552 addGeneratedTempFile(ATTRIBUTES_MASK);
553 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
554 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
555 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530556
557 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530558 * Initialize getterImpl, attributes, constructor, hash code, equals and
559 * to strings when generation file type matches to impl class mask.
560 */
561 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
562 addGeneratedTempFile(ATTRIBUTES_MASK);
563 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530564 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
565 addGeneratedTempFile(EQUALS_IMPL_MASK);
566 addGeneratedTempFile(TO_STRING_IMPL_MASK);
567 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530568
569 /*
570 * Initialize temp files to generate type class.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530571 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530572 if ((getGeneratedJavaFiles() & GENERATE_TYPE_CLASS) != 0) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530573 addGeneratedTempFile(ATTRIBUTES_MASK);
574 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
575 addGeneratedTempFile(HASH_CODE_IMPL_MASK);
576 addGeneratedTempFile(EQUALS_IMPL_MASK);
577 addGeneratedTempFile(TO_STRING_IMPL_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530578 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530579
580 /*
581 * Initialize getter and setter when generation file type matches to
582 * builder interface mask.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530583 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530584 if ((getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
585 addGeneratedTempFile(GETTER_FOR_INTERFACE_MASK);
586 addGeneratedTempFile(SETTER_FOR_INTERFACE_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530587 addGeneratedTempFile(GETTER_FOR_CLASS_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530588 addGeneratedTempFile(SETTER_FOR_CLASS_MASK);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530589 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530590
591 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530592 * Initialize enum when generation file type matches to enum class mask.
593 */
594 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
595 addGeneratedTempFile(ENUM_IMPL_MASK);
596 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530597
598 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530599 * Set temporary file handles.
600 */
601 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
602 setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
603 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530604
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530605 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
606 setGetterInterfaceTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_FILE_NAME));
607 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530608
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530609 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
610 setSetterInterfaceTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_FILE_NAME));
611 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530612
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530613 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
614 setGetterImplTempFileHandle(getTemporaryFileHandle(GETTER_METHOD_IMPL_FILE_NAME));
615 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530616
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530617 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
618 setSetterImplTempFileHandle(getTemporaryFileHandle(SETTER_METHOD_IMPL_FILE_NAME));
619 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530620
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530621 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
622 setHashCodeImplTempFileHandle(getTemporaryFileHandle(HASH_CODE_METHOD_FILE_NAME));
623 }
624 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
625 setEqualsImplTempFileHandle(getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME));
626 }
627 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
628 setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
629 }
630 if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) {
631 setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
632 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530633 }
634
635 /**
636 * Returns java file handle for interface file.
637 *
638 * @return java file handle for interface file
639 */
640 private File getInterfaceJavaFileHandle() {
641 return interfaceJavaFileHandle;
642 }
643
644 /**
645 * Sets the java file handle for interface file.
646 *
647 * @param interfaceJavaFileHandle java file handle
648 */
649 private void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
650 this.interfaceJavaFileHandle = interfaceJavaFileHandle;
651 }
652
653 /**
654 * Returns java file handle for builder interface file.
655 *
656 * @return java file handle for builder interface file
657 */
658 private File getBuilderInterfaceJavaFileHandle() {
659 return builderInterfaceJavaFileHandle;
660 }
661
662 /**
663 * Sets the java file handle for builder interface file.
664 *
665 * @param builderInterfaceJavaFileHandle java file handle
666 */
667 private void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
668 this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
669 }
670
671 /**
672 * Returns java file handle for builder class file.
673 *
674 * @return java file handle for builder class file
675 */
676 private File getBuilderClassJavaFileHandle() {
677 return builderClassJavaFileHandle;
678 }
679
680 /**
681 * Sets the java file handle for builder class file.
682 *
683 * @param builderClassJavaFileHandle java file handle
684 */
685 private void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
686 this.builderClassJavaFileHandle = builderClassJavaFileHandle;
687 }
688
689 /**
690 * Returns java file handle for impl class file.
691 *
692 * @return java file handle for impl class file
693 */
694 private File getImplClassJavaFileHandle() {
695 return implClassJavaFileHandle;
696 }
697
698 /**
699 * Sets the java file handle for impl class file.
700 *
701 * @param implClassJavaFileHandle java file handle
702 */
703 private void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
704 this.implClassJavaFileHandle = implClassJavaFileHandle;
705 }
706
707 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530708 * Returns attribute's temporary file handle.
709 *
710 * @return temporary file handle
711 */
712 public File getAttributesTempFileHandle() {
713 return attributesTempFileHandle;
714 }
715
716 /**
717 * Sets attribute's temporary file handle.
718 *
719 * @param attributeForClass file handle for attribute
720 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530721 void setAttributesTempFileHandle(File attributeForClass) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530722 attributesTempFileHandle = attributeForClass;
723 }
724
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530725
726 /**
727 * Returns getter method's impl's temporary file handle.
728 *
729 * @return temporary file handle
730 */
731 public File getGetterImplTempFileHandle() {
732 return getterImplTempFileHandle;
733 }
734
735 /**
736 * Sets to getter method's impl's temporary file handle.
737 *
738 * @param getterImpl file handle for to getter method's impl
739 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530740 void setGetterImplTempFileHandle(File getterImpl) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530741 getterImplTempFileHandle = getterImpl;
742 }
743
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530744
745 /**
746 * Returns hash code method's temporary file handle.
747 *
748 * @return temporary file handle
749 */
750 public File getHashCodeImplTempFileHandle() {
751 return hashCodeImplTempFileHandle;
752 }
753
754 /**
755 * Sets hash code method's temporary file handle.
756 *
757 * @param hashCodeMethod file handle for hash code method
758 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530759 void setHashCodeImplTempFileHandle(File hashCodeMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530760 hashCodeImplTempFileHandle = hashCodeMethod;
761 }
762
763 /**
764 * Returns equals mehtod's temporary file handle.
765 *
766 * @return temporary file handle
767 */
768 public File getEqualsImplTempFileHandle() {
769 return equalsImplTempFileHandle;
770 }
771
772 /**
773 * Sets equals method's temporary file handle.
774 *
775 * @param equalsMethod file handle for to equals method
776 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530777 void setEqualsImplTempFileHandle(File equalsMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530778 equalsImplTempFileHandle = equalsMethod;
779 }
780
781 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530782 * Returns to string method's temporary file handle.
783 *
784 * @return temporary file handle
785 */
786 public File getToStringImplTempFileHandle() {
787 return toStringImplTempFileHandle;
788 }
789
790 /**
791 * Sets to string method's temporary file handle.
792 *
793 * @param toStringMethod file handle for to string method
794 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530795 void setToStringImplTempFileHandle(File toStringMethod) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530796 toStringImplTempFileHandle = toStringMethod;
797 }
798
799 /**
800 * Returns temporary file handle for enum class file.
801 *
802 * @return temporary file handle for enum class file
803 */
804 public File getEnumClassTempFileHandle() {
805 return enumClassTempFileHandle;
806 }
807
808 /**
809 * Sets temporary file handle for enum class file.
810 *
811 * @param enumClassTempFileHandle temporary file handle for enum class file
812 */
813 private void setEnumClassTempFileHandle(File enumClassTempFileHandle) {
814 this.enumClassTempFileHandle = enumClassTempFileHandle;
815 }
816
817 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530818 * Returns list of classes to be extended by generated files.
819 *
820 * @return list of classes to be extended by generated files
821 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530822 List<String> getExtendsList() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530823 return extendsList;
824 }
825
826 /**
827 * Sets class to be extended by generated file.
828 *
829 * @param extendsList list of classes to be extended
830 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530831 void setExtendsList(List<String> extendsList) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530832 this.extendsList = extendsList;
833 }
834
835 /**
836 * Adds class to the extends list.
837 *
838 * @param extend class to be extended
839 */
840 public void addToExtendsList(String extend) {
841 getExtendsList().add(extend);
842 }
843
844 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530845 * Adds attribute for class.
846 *
847 * @param attr attribute info
848 * @throws IOException when fails to append to temporary file
849 */
850 private void addAttribute(JavaAttributeInfo attr)
851 throws IOException {
852 appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
853 }
854
855 /**
856 * Adds getter for interface.
857 *
858 * @param attr attribute info
859 * @throws IOException when fails to append to temporary file
860 */
861 private void addGetterForInterface(JavaAttributeInfo attr)
862 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530863 appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr, getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530864 }
865
866 /**
867 * Adds setter for interface.
868 *
869 * @param attr attribute info
870 * @throws IOException when fails to append to temporary file
871 */
872 private void addSetterForInterface(JavaAttributeInfo attr)
873 throws IOException {
874 appendToFile(getSetterInterfaceTempFileHandle(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530875 getSetterString(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) + NEW_LINE);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530876 }
877
878 /**
879 * Adds setter's implementation for class.
880 *
881 * @param attr attribute info
882 * @throws IOException when fails to append to temporary file
883 */
884 private void addSetterImpl(JavaAttributeInfo attr)
885 throws IOException {
886 appendToFile(getSetterImplTempFileHandle(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530887 getOverRideString() + getSetterForClass(attr, getGeneratedJavaClassName(), getGeneratedJavaFiles()) +
888 NEW_LINE);
889 }
890
891 /**
892 * Adds getter method's impl for class.
893 *
894 * @param attr attribute info
895 * @throws IOException when fails to append to temporary file
896 */
897 private void addGetterImpl(JavaAttributeInfo attr)
898 throws IOException {
899 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0
900 || (getGeneratedJavaFiles() & GENERATE_SERVICE_AND_MANAGER) != 0) {
901 appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr,
902 getGeneratedJavaFiles()) + NEW_LINE);
903 } else {
904 appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
905 + getGetterForClass(attr, getGeneratedJavaFiles()) + NEW_LINE);
906 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530907 }
908
909 /**
910 * Adds build method for interface.
911 *
912 * @return build method for interface
913 * @throws IOException when fails to append to temporary file
914 */
915 String addBuildMethodForInterface()
916 throws IOException {
917 return parseBuilderInterfaceBuildMethodString(getGeneratedJavaClassName());
918 }
919
920 /**
921 * Adds build method's implementation for class.
922 *
923 * @return build method implementation for class
924 * @throws IOException when fails to append to temporary file
925 */
926 String addBuildMethodImpl()
927 throws IOException {
928 return getBuildString(getGeneratedJavaClassName()) + NEW_LINE;
929 }
930
931 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530932 * Adds default constructor for class.
933 *
934 * @param modifier modifier for constructor.
935 * @param toAppend string which need to be appended with the class name
936 * @return default constructor for class
937 * @throws IOException when fails to append to file
938 */
939 String addDefaultConstructor(String modifier, String toAppend)
940 throws IOException {
941 return NEW_LINE + getDefaultConstructorString(getGeneratedJavaClassName() + toAppend, modifier);
942 }
943
944 /**
945 * Adds default constructor for class.
946 *
947 * @return default constructor for class
948 * @throws IOException when fails to append to file
949 */
950 public String addOfMethod()
951 throws IOException {
952 return getJavaDoc(OF_METHOD, getGeneratedJavaClassName(), false)
953 + getOfMethod(getGeneratedJavaClassName(), null);
954 }
955
956 /**
957 * Adds hash code method for class.
958 *
959 * @param attr attribute info
960 * @throws IOException when fails to append to temporary file
961 */
962 private void addHashCodeMethod(JavaAttributeInfo attr)
963 throws IOException {
964 appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
965 }
966
967 /**
968 * Adds equals method for class.
969 *
970 * @param attr attribute info
971 * @throws IOException when fails to append to temporary file
972 */
973 private void addEqualsMethod(JavaAttributeInfo attr)
974 throws IOException {
975 appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
976 }
977
978 /**
979 * Adds ToString method for class.
980 *
981 * @param attr attribute info
982 * @throws IOException when fails to append to temporary file
983 */
984 private void addToStringMethod(JavaAttributeInfo attr)
985 throws IOException {
986 appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
987 }
988
989 /**
990 * Adds enum class attributes to temporary file.
991 *
992 * @param curEnumInfo current YANG enum
993 * @throws IOException when fails to do IO operations.
994 */
995 private void addAttributesForEnumClass(JavaAttributeInfo curEnumInfo)
996 throws IOException {
997 appendToFile(getEnumClassTempFileHandle(),
998 generateEnumAttributeString(curEnumInfo.getAttributeName(), getEnumValue()));
999 }
1000
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301001
1002 /**
1003 * Returns a temporary file handle for the specific file type.
1004 *
1005 * @param fileName file name
1006 * @return temporary file handle
1007 * @throws IOException when fails to create new file handle
1008 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301009 File getTemporaryFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301010 throws IOException {
1011 String path = getTempDirPath();
1012 File dir = new File(path);
1013 if (!dir.exists()) {
1014 dir.mkdirs();
1015 }
1016 File file = new File(path + fileName + TEMP_FILE_EXTENSION);
1017 if (!file.exists()) {
1018 file.createNewFile();
1019 }
1020 return file;
1021 }
1022
1023 /**
1024 * Returns a temporary file handle for the specific file type.
1025 *
1026 * @param fileName file name
1027 * @return temporary file handle
1028 * @throws IOException when fails to create new file handle
1029 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301030 File getJavaFileHandle(String fileName)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301031 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301032// createPackage(getAbsoluteDirPath(), getJavaFileInfo().getJavaName());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301033 return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
1034 }
1035
1036 /**
1037 * Returns data from the temporary files.
1038 *
1039 * @param file temporary file handle
1040 * @return stored data from temporary files
1041 * @throws IOException when failed to get data from the given file
1042 */
1043 public String getTemporaryDataFromFileHandle(File file)
1044 throws IOException {
1045 String path = getTempDirPath();
1046 if (new File(path + file.getName()).exists()) {
1047 return readAppendFile(path + file.getName(), EMPTY_STRING);
1048 } else {
1049 throw new IOException("Unable to get data from the given "
1050 + file.getName() + " file for " + getGeneratedJavaClassName() + PERIOD);
1051 }
1052 }
1053
1054 /**
1055 * Returns temporary directory path.
1056 *
1057 * @return directory path
1058 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301059 String getTempDirPath() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301060 return getPackageDirPathFromJavaJPackage(getAbsoluteDirPath()) + SLASH + getGeneratedJavaClassName()
1061 + TEMP_FOLDER_NAME_SUFIX + SLASH;
1062 }
1063
1064 /**
1065 * Parses attribute to get the attribute string.
1066 *
1067 * @param attr attribute info
1068 * @return attribute string
1069 */
1070 private String parseAttribute(JavaAttributeInfo attr) {
1071 /*
1072 * TODO: check if this utility needs to be called or move to the caller
1073 */
1074 String attributeName = getCamelCase(getSmallCase(attr.getAttributeName()), null);
1075 if (attr.isQualifiedName()) {
1076 return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
1077 attributeName, attr.isListAttr());
1078 } else {
1079 return getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(), attributeName,
1080 attr.isListAttr());
1081 }
1082 }
1083
1084 /**
1085 * Appends content to temporary file.
1086 *
1087 * @param file temporary file
1088 * @param data data to be appended
1089 * @throws IOException when fails to append to file
1090 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301091 void appendToFile(File file, String data)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301092 throws IOException {
1093 try {
1094 insertDataIntoJavaFile(file, data);
1095 } catch (IOException ex) {
1096 throw new IOException("failed to write in temp file.");
1097 }
1098 }
1099
1100 /**
1101 * Adds current node info as and attribute to the parent generated file.
1102 *
1103 * @param curNode current node which needs to be added as an attribute in
1104 * the parent generated code
1105 * @param isList is list construct
1106 * @throws IOException IO operation exception
1107 */
1108 public static void addCurNodeInfoInParentTempFile(YangNode curNode,
1109 boolean isList)
1110 throws IOException {
1111 YangNode parent = getParentNodeInGenCode(curNode);
1112 if (!(parent instanceof JavaCodeGenerator)) {
1113 throw new TranslatorException("missing parent node to contain current node info in generated file");
1114 }
1115 JavaAttributeInfo javaAttributeInfo = getCurNodeAsAttributeInParent(curNode,
1116 parent, isList);
1117 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
1118 throw new TranslatorException("missing parent temp file handle");
1119 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301120 getNodesInterfaceFragmentFiles(parent)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301121 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1122 }
1123
1124 /**
1125 * Creates an attribute info object corresponding to a data model node and
1126 * return it.
1127 *
1128 * @param curNode current data model node for which the java code generation
1129 * is being handled
1130 * @param parentNode parent node in which the current node is an attribute
1131 * @param isListNode is the current added attribute needs to be a list
1132 * @return AttributeInfo attribute details required to add in temporary
1133 * files
1134 */
1135 public static JavaAttributeInfo getCurNodeAsAttributeInParent(
1136 YangNode curNode, YangNode parentNode, boolean isListNode) {
1137 String curNodeName = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName();
1138 /*
1139 * Get the import info corresponding to the attribute for import in
1140 * generated java files or qualified access
1141 */
1142 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
1143 curNodeName);
1144 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
1145 throw new TranslatorException("Parent node does not have file info");
1146 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301147 TempJavaFragmentFiles tempJavaFragmentFiles = getNodesInterfaceFragmentFiles(parentNode);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301148 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
1149 boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo);
1150 return getAttributeInfoForTheData(qualifiedTypeInfo, curNodeName, null, isQualified, isListNode);
1151 }
1152
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301153 public static TempJavaFragmentFiles getNodesInterfaceFragmentFiles(YangNode node) {
1154 TempJavaFragmentFiles tempJavaFragmentFiles;
1155 if (node instanceof RpcNotificationContainer) {
1156 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1157 .getTempJavaCodeFragmentFiles()
1158 .getServiceTempFiles();
1159 } else {
1160 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) node)
1161 .getTempJavaCodeFragmentFiles()
1162 .getBeanTempFiles();
1163 }
1164 return tempJavaFragmentFiles;
1165 }
1166
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301167 /**
1168 * Adds parent's info to current node import list.
1169 *
1170 * @param curNode current node for which import list needs to be updated
1171 */
1172 public void addParentInfoInCurNodeTempFile(YangNode curNode) {
1173 caseImportInfo = new JavaQualifiedTypeInfo();
1174 YangNode parent = getParentNodeInGenCode(curNode);
1175 if (!(parent instanceof JavaCodeGenerator)) {
1176 throw new TranslatorException("missing parent node to contain current node info in generated file");
1177 }
1178 if (!(curNode instanceof JavaFileInfoContainer)) {
1179 throw new TranslatorException("missing java file information to get the package details "
1180 + "of attribute corresponding to child node");
1181 }
1182 caseImportInfo.setClassInfo(getCaptialCase(getCamelCase(parent.getName(), null)));
1183 caseImportInfo.setPkgInfo(((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage());
1184 ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1185 .getBeanTempFiles().getJavaImportData().addImportInfo(caseImportInfo);
1186 }
1187
1188 /**
1189 * Adds leaf attributes in generated files.
1190 *
1191 * @param listOfLeaves list of YANG leaf
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301192 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301193 * @throws IOException IO operation fail
1194 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301195 private void addLeavesInfoToTempFiles(List<YangLeaf> listOfLeaves,
1196 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301197 throws IOException {
1198 if (listOfLeaves != null) {
1199 for (YangLeaf leaf : listOfLeaves) {
1200 if (!(leaf instanceof JavaLeafInfoContainer)) {
1201 throw new TranslatorException("Leaf does not have java information");
1202 }
1203 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
1204 javaLeaf.updateJavaQualifiedInfo();
1205 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1206 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301207 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1208 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301209 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1210 false);
1211 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1212 }
1213 }
1214 }
1215
1216 /**
1217 * Adds leaf list's attributes in generated files.
1218 *
1219 * @param listOfLeafList list of YANG leaves
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301220 * @param yangPluginConfig
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301221 * @throws IOException IO operation fail
1222 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301223 private void addLeafListInfoToTempFiles(List<YangLeafList> listOfLeafList, YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301224 throws IOException {
1225 if (listOfLeafList != null) {
1226 for (YangLeafList leafList : listOfLeafList) {
1227 if (!(leafList instanceof JavaLeafInfoContainer)) {
1228 throw new TranslatorException("Leaf-list does not have java information");
1229 }
1230 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
1231 javaLeaf.updateJavaQualifiedInfo();
1232 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
1233 javaLeaf.getJavaQualifiedInfo(),
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301234 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
1235 javaLeaf.getDataType(),
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301236 getIsQualifiedAccessOrAddToImportList(javaLeaf.getJavaQualifiedInfo()),
1237 true);
1238 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1239 }
1240 }
1241 }
1242
1243 /**
1244 * Adds all the leaves in the current data model node as part of the
1245 * generated temporary file.
1246 *
1247 * @param curNode java file info of the generated file
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301248 * @param yangPluginConfig plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301249 * @throws IOException IO operation fail
1250 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301251 public void addCurNodeLeavesInfoToTempFiles(YangNode curNode,
1252 YangPluginConfig yangPluginConfig)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301253 throws IOException {
1254 if (!(curNode instanceof YangLeavesHolder)) {
1255 throw new TranslatorException("Data model node does not have any leaves");
1256 }
1257 YangLeavesHolder leavesHolder = (YangLeavesHolder) curNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301258 addLeavesInfoToTempFiles(leavesHolder.getListOfLeaf(), yangPluginConfig);
1259 addLeafListInfoToTempFiles(leavesHolder.getListOfLeafList(), yangPluginConfig);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301260 }
1261
1262 /**
1263 * Adds enum attributes to temporary files.
1264 *
1265 * @param curNode current YANG node
1266 * @throws IOException when fails to do IO operations
1267 */
1268 public void addEnumAttributeToTempFiles(YangNode curNode)
1269 throws IOException {
1270 if (curNode instanceof YangEnumeration) {
1271 Set<YangEnum> enumSet = ((YangEnumeration) curNode).getEnumSet();
1272 /*
1273 * Get the import info corresponding to the attribute for import in
1274 * generated java files or qualified access
1275 */
1276 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
1277 getJavaFileInfo().getJavaName());
1278 for (YangEnum curEnum : enumSet) {
1279 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(qualifiedTypeInfo,
1280 curEnum.getNamedValue(), null, false, false);
1281 setEnumValue(curEnum.getValue());
1282 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
1283 }
1284 } else {
1285 throw new TranslatorException("current node should be of type enum.");
1286 }
1287 }
1288
1289 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301290 * Adds the new attribute info to the target generated temporary files.
1291 *
1292 * @param newAttrInfo the attribute info that needs to be added to temporary
1293 * files
1294 * @throws IOException IO operation fail
1295 */
1296 void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
1297 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301298 setAttributePresent(true);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301299 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1300 addAttribute(newAttrInfo);
1301 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301302
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301303 if ((getGeneratedTempFiles() & GETTER_FOR_INTERFACE_MASK) != 0) {
1304 addGetterForInterface(newAttrInfo);
1305 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301306
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301307 if ((getGeneratedTempFiles() & SETTER_FOR_INTERFACE_MASK) != 0) {
1308 addSetterForInterface(newAttrInfo);
1309 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301310
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301311 if ((getGeneratedTempFiles() & SETTER_FOR_CLASS_MASK) != 0) {
1312 addSetterImpl(newAttrInfo);
1313 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301314
1315 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1316 addGetterImpl(newAttrInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301317 }
1318 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1319 addHashCodeMethod(newAttrInfo);
1320 }
1321 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1322 addEqualsMethod(newAttrInfo);
1323 }
1324 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1325 addToStringMethod(newAttrInfo);
1326 }
1327 if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) {
1328 addAttributesForEnumClass(newAttrInfo);
1329 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301330 }
1331
1332 /**
1333 * Returns java class name.
1334 *
1335 * @param suffix for the class name based on the file type
1336 * @return java class name
1337 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301338 String getJavaClassName(String suffix) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301339 return getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
1340 }
1341
1342 /**
1343 * Returns the directory path.
1344 *
1345 * @return directory path
1346 */
1347 private String getDirPath() {
1348 return getJavaFileInfo().getPackageFilePath();
1349 }
1350
1351 /**
1352 * Constructs java code exit.
1353 *
1354 * @param fileType generated file type
1355 * @param curNode current YANG node
1356 * @throws IOException when fails to generate java files
1357 */
1358 public void generateJavaFile(int fileType, YangNode curNode)
1359 throws IOException {
1360 List<String> imports = new ArrayList<>();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301361 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301362 imports = getJavaImportData().getImports();
1363 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301364 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301365 * Prepares java file generator for extends list.
1366 */
1367 prepareJavaFileGeneratorForExtendsList(getExtendsList());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301368 createPackage(curNode);
1369
1370 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301371 * Generate java code.
1372 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301373 if ((fileType & INTERFACE_MASK) != 0 || (fileType &
1374 BUILDER_INTERFACE_MASK) != 0) {
1375 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301376 * Adds import for case.
1377 */
1378 if (curNode instanceof YangCase) {
1379 List<String> importData = ((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
1380 .getBeanTempFiles().getJavaImportData().getImports();
1381 for (String importInfo : importData) {
1382 if (!imports.contains(importInfo)) {
1383 imports.add(importInfo);
1384 }
1385 }
1386 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301387 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301388 * Adds import for HasAugmentation class.
1389 */
1390 if (isHasAugmentationExtended(getExtendsList())) {
1391 addHasAugmentationImport(curNode, imports, true);
1392 }
1393 if (isAugmentedInfoExtended(getExtendsList())) {
1394 addAugmentedInfoImport(curNode, imports, true);
1395 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301396 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301397 * Create interface file.
1398 */
1399 setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
1400 setInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301401 generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode, isAttributePresent()));
1402 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301403 * Create builder interface file.
1404 */
1405 if ((fileType & BUILDER_INTERFACE_MASK) != 0) {
1406 setBuilderInterfaceJavaFileHandle(
1407 getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
1408 setBuilderInterfaceJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301409 generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode,
1410 isAttributePresent()));
1411 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301412 * Append builder interface file to interface file and close it.
1413 */
1414 mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
1415 }
1416 insertDataIntoJavaFile(getInterfaceJavaFileHandle(), getJavaClassDefClose());
1417 if (isHasAugmentationExtended(getExtendsList())) {
1418 addHasAugmentationImport(curNode, imports, false);
1419 }
1420 if (isAugmentedInfoExtended(getExtendsList())) {
1421 addAugmentedInfoImport(curNode, imports, false);
1422 }
1423 if (curNode instanceof YangCase) {
1424 removeCaseImport(imports);
1425 }
1426 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301427 if (((fileType & GENERATE_SERVICE_AND_MANAGER) != 0)
1428 && ((fileType & BUILDER_CLASS_MASK) != 0 || (fileType & IMPL_CLASS_MASK) != 0)) {
1429 if (isAttributePresent()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301430 addImportsToStringAndHasCodeMethods(curNode, imports);
1431 }
1432 if (isHasAugmentationExtended(getExtendsList())) {
1433 addAugmentedInfoImport(curNode, imports, true);
1434 addArrayListImport(curNode, imports, true);
1435 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301436 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301437 * Create builder class file.
1438 */
1439 setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
1440 setBuilderClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301441 generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode, isAttributePresent()));
1442 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301443 * Create impl class file.
1444 */
1445 if ((fileType & IMPL_CLASS_MASK) != 0) {
1446 setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
1447 setImplClassJavaFileHandle(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301448 generateImplClassFile(getImplClassJavaFileHandle(), curNode, isAttributePresent()));
1449 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301450 * Append impl class to builder class and close it.
1451 */
1452 mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
1453 }
1454 insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), getJavaClassDefClose());
1455 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301456
1457 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301458 * Creates type enum class file.
1459 */
1460 if ((fileType & GENERATE_ENUM_CLASS) != 0) {
1461 setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
1462 setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
1463 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301464
1465 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301466 * Close all the file handles.
1467 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301468 freeTemporaryResources(false);
1469 }
1470
1471 /**
1472 * Adds imports for ToString and HashCodeMethod.
1473 *
1474 * @param curNode current YANG node
1475 * @param imports import list
1476 * @return import list
1477 */
1478 public List<String> addImportsToStringAndHasCodeMethods(YangNode curNode, List<String> imports) {
1479 imports.add(getJavaImportData().getImportForHashAndEquals());
1480 imports.add(getJavaImportData().getImportForToString());
1481 return imports;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301482 }
1483
1484 /**
1485 * Removes case import info from import list.
1486 *
1487 * @param imports list of imports
1488 * @return import for class
1489 */
1490 private List<String> removeCaseImport(List<String> imports) {
1491 if (imports != null && caseImportInfo != null) {
1492 String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
1493 SEMI_COLAN + NEW_LINE;
1494 imports.remove(caseImport);
1495 }
1496 return imports;
1497 }
1498
1499 /**
1500 * Removes all temporary file handles.
1501 *
1502 * @param isErrorOccurred when translator fails to generate java files we
1503 * need to close all open file handles include temporary files
1504 * and java files.
1505 * @throws IOException when failed to delete the temporary files
1506 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301507 public void freeTemporaryResources(boolean isErrorOccurred)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301508 throws IOException {
1509 boolean isError = isErrorOccurred;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301510 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301511 * Close all java file handles and when error occurs delete the files.
1512 */
1513 if ((getGeneratedJavaFiles() & INTERFACE_MASK) != 0) {
1514 closeFile(getInterfaceJavaFileHandle(), isError);
1515 }
1516 if ((getGeneratedJavaFiles() & BUILDER_CLASS_MASK) != 0) {
1517 closeFile(getBuilderClassJavaFileHandle(), isError);
1518 }
1519 if ((getGeneratedJavaFiles() & BUILDER_INTERFACE_MASK) != 0) {
1520 closeFile(getBuilderInterfaceJavaFileHandle(), true);
1521 }
1522 if ((getGeneratedJavaFiles() & IMPL_CLASS_MASK) != 0) {
1523 closeFile(getImplClassJavaFileHandle(), true);
1524 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301525
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301526 if ((getGeneratedJavaFiles() & GENERATE_ENUM_CLASS) != 0) {
1527 closeFile(getEnumClassJavaFileHandle(), isError);
1528 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +05301529
1530 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301531 * Close all temporary file handles and delete the files.
1532 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301533 if ((getGeneratedTempFiles() & GETTER_FOR_CLASS_MASK) != 0) {
1534 closeFile(getGetterImplTempFileHandle(), true);
1535 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301536 if ((getGeneratedTempFiles() & ATTRIBUTES_MASK) != 0) {
1537 closeFile(getAttributesTempFileHandle(), true);
1538 }
1539 if ((getGeneratedTempFiles() & HASH_CODE_IMPL_MASK) != 0) {
1540 closeFile(getHashCodeImplTempFileHandle(), true);
1541 }
1542 if ((getGeneratedTempFiles() & TO_STRING_IMPL_MASK) != 0) {
1543 closeFile(getToStringImplTempFileHandle(), true);
1544 }
1545 if ((getGeneratedTempFiles() & EQUALS_IMPL_MASK) != 0) {
1546 closeFile(getEqualsImplTempFileHandle(), true);
1547 }
1548 if ((getGeneratedTempFiles() & ENUM_IMPL_MASK) != 0) {
1549 closeFile(getEnumClassTempFileHandle(), true);
1550 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301551 }
1552
1553 /**
1554 * Returns if the attribute needs to be accessed in a qualified manner or
1555 * not, if it needs to be imported, then the same needs to be done.
1556 *
1557 * @param importInfo import info for the current attribute being added
1558 * @return status of the qualified access to the attribute
1559 */
1560 public boolean getIsQualifiedAccessOrAddToImportList(
1561 JavaQualifiedTypeInfo importInfo) {
1562 boolean isImportPkgEqualCurNodePkg;
1563 if (importInfo.getClassInfo().contentEquals(
1564 getGeneratedJavaClassName())) {
1565 /*
1566 * if the current class name is same as the attribute class name,
1567 * then the attribute must be accessed in a qualified manner.
1568 */
1569 return true;
1570 } else if (importInfo.getPkgInfo() != null) {
1571 /*
1572 * If the attribute type is having the package info, it is contender
1573 * for import list and also need to check if it needs to be a
1574 * qualified access.
1575 */
1576 isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(importInfo);
1577 if (!isImportPkgEqualCurNodePkg) {
1578 /*
1579 * If the package of the attribute added is not same as the
1580 * current class package, then it must either be imported for
1581 * access or it must be a qualified access.
1582 */
1583 boolean isImportAdded = getJavaImportData().addImportInfo(importInfo);
1584 if (!isImportAdded) {
1585 /*
1586 * If the attribute type info is not imported, then it must
1587 * be a qualified access.
1588 */
1589 return true;
1590 }
1591 }
1592 }
1593 return false;
1594 }
1595
1596 /**
1597 * Checks if the import info is same as the package of the current generated
1598 * java file.
1599 *
1600 * @param importInfo import info for an attribute
1601 * @return true if the import info is same as the current nodes package
1602 * false otherwise
1603 */
1604 public boolean isImportPkgEqualCurNodePkg(JavaQualifiedTypeInfo importInfo) {
1605 return getJavaFileInfo().getPackage()
1606 .contentEquals(importInfo.getPkgInfo());
1607 }
1608}