blob: 0ae81f75ef1186005aa692d8bea8e19a0a16088b [file] [log] [blame]
Bharat saraswal4bf8b152016-02-25 02:26:43 +05301/*
2 * Copyright 2016 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 */
16
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053019import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_CLASS_MASK;
20import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_INTERFACE_MASK;
21import static org.onosproject.yangutils.translator.GeneratedFileType.GENERATE_TYPEDEF_CLASS;
22import static org.onosproject.yangutils.translator.GeneratedFileType.IMPL_CLASS_MASK;
23import static org.onosproject.yangutils.translator.GeneratedFileType.INTERFACE_MASK;
24import static org.slf4j.LoggerFactory.getLogger;
25
Bharat saraswal4bf8b152016-02-25 02:26:43 +053026import java.io.File;
27import java.io.IOException;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053028import java.util.ArrayList;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053029import java.util.List;
30
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053031import org.apache.commons.io.FileUtils;
32import org.onosproject.yangutils.translator.CachedFileHandle;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053033import org.onosproject.yangutils.translator.tojava.AttributeInfo;
34import org.onosproject.yangutils.utils.UtilConstants;
35import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
36import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
37import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053038import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053039import org.slf4j.Logger;
40
Vinod Kumar Sc4216002016-03-03 19:55:30 +053041/**
42 * Generates java file.
43 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +053044public final class JavaFileGenerator {
45
46 private static final Logger log = getLogger(JavaFileGenerator.class);
47
48 /**
49 * Default constructor.
50 */
51 private JavaFileGenerator() {
52 }
53
54 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053055 * Returns a file object for generated file.
56 *
57 * @param fileName file name
58 * @param filePath file package path
59 * @param extension file extension
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053060 * @param handle cached file handle
Vinod Kumar Sc4216002016-03-03 19:55:30 +053061 * @return file object
62 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053063 public static File getFileObject(String filePath, String fileName, String extension, CachedFileHandle handle) {
64 return new File(handle.getCodeGenFilePath() + filePath + File.separator + fileName + extension);
Vinod Kumar Sc4216002016-03-03 19:55:30 +053065 }
66
67 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +053068 * Returns generated interface file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053069 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +053070 * @param file file
71 * @param className class name
72 * @param imports imports for the file
73 * @param attrList attribute info
74 * @param pkg generated file package
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053075 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +053076 * @return interface file
Vinod Kumar Sc4216002016-03-03 19:55:30 +053077 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +053078 */
79 public static File generateInterfaceFile(File file, String className, List<String> imports,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053080 List<AttributeInfo> attrList, String pkg, CachedFileHandle handle) throws IOException {
81 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
Vinod Kumar Sc4216002016-03-03 19:55:30 +053082 initiateFile(file, className, INTERFACE_MASK, imports, pkg);
83
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053084 List<String> methods = new ArrayList<>();
Vinod Kumar Sc4216002016-03-03 19:55:30 +053085 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053086 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
Vinod Kumar Sc4216002016-03-03 19:55:30 +053087 } catch (ClassNotFoundException | IOException e) {
88 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
89 throw new IOException("Fail to read data from temp file.");
90 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +053091
92 /**
93 * Add getter methods to interface file.
94 */
95 for (String method : methods) {
96 appendMethod(file, method);
97 }
98 return file;
99 }
100
101 /**
102 * Return generated builder interface file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530103 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530104 * @param file file
105 * @param className class name
106 * @param pkg generated file package
107 * @param attrList attribute info
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530108 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530109 * @return builder interface file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530110 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530111 */
112 public static File generateBuilderInterfaceFile(File file, String className, String pkg,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530113 List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
114 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530115 initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530116 List<String> methods = new ArrayList<>();
117
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530118 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530119 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
120 methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530121 } catch (ClassNotFoundException | IOException e) {
122 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
123 throw new IOException("Fail to read data from temp file.");
124 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530125
126 /**
127 * Add build method to builder interface file.
128 */
129 methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className));
130
131 /**
132 * Add getters and setters in builder interface.
133 */
134 for (String method : methods) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530135 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530136 }
137
138 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
139 return file;
140 }
141
142 /**
143 * Returns generated builder class file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530144 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530145 * @param file file
146 * @param className class name
147 * @param imports imports for the file
148 * @param pkg generated file package
149 * @param attrList attribute info
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530150 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530151 * @return builder class file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530152 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530153 */
154 public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530155 List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
156 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530157 initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg);
158
159 /**
160 * Add attribute strings.
161 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530162 List<String> attributes = new ArrayList<>();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530163 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530164 attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530165 } catch (ClassNotFoundException | IOException e) {
166 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
167 throw new IOException("Fail to read data from temp file.");
168 }
169 /**
170 * Add attributes to the file.
171 */
172 for (String attribute : attributes) {
173 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
174 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530175
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530176 List<String> methods = new ArrayList<>();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530177 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530178 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
179 methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530180 } catch (ClassNotFoundException | IOException e) {
181 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
182 throw new IOException("Fail to read data from temp file.");
183 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530184
185 /**
186 * Add default constructor and build method impl.
187 */
188 methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530189 + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER,
190 UtilConstants.PUBLIC));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530191 methods.add(MethodsGenerator.getBuildString(className));
192
193 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530194 * Add methods in builder class.
195 */
196 for (String method : methods) {
197 appendMethod(file, method + UtilConstants.NEW_LINE);
198 }
199 return file;
200 }
201
202 /**
203 * Returns generated impl class file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530204 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530205 * @param file file
206 * @param className class name
207 * @param pkg generated file package
208 * @param attrList attribute's info
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530209 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530210 * @return impl class file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530211 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530212 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530213 public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList,
214 CachedFileHandle handle)
215 throws IOException {
216 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
217 initiateFile(file, className, IMPL_CLASS_MASK, null, path);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530218
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530219 List<String> attributes = new ArrayList<>();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530220 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530221 attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530222 } catch (ClassNotFoundException | IOException e) {
223 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
224 throw new IOException("Fail to read data from temp file.");
225 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530226
227 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530228 * Add attributes to the file.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530229 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530230 for (String attribute : attributes) {
231 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
232 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530233
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530234 List<String> methods = new ArrayList<>();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530235 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530236
237 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
238
239 methods.add(getConstructorString(className)
240 + handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path)
241 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET);
242
243 methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen()
244 + handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(UtilConstants.NEW_LINE,
245 "")));
246
247 methods.add(MethodsGenerator
248 .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL)
249 + handle.getTempData(TempDataStoreTypes.EQUALS, className, path)));
250
251 methods.add(MethodsGenerator.getToStringMethodOpen()
252 + handle.getTempData(TempDataStoreTypes.TO_STRING, className, path)
253 + MethodsGenerator.getToStringMethodClose());
254
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530255 } catch (ClassNotFoundException | IOException e) {
256 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
257 throw new IOException("Fail to read data from temp file.");
258 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530259
260 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530261 * Add methods in impl class.
262 */
263 for (String method : methods) {
264 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
265 }
266 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
267
268 return file;
269 }
270
271 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530272 * Generate class file for type def.
273 *
274 * @param file generated file
275 * @param className file name
276 * @param imports imports for file
277 * @param pkg package path
278 * @param cachedAttributeList attribute list
279 * @param handle cached file handle
280 * @return type def class file
281 * @throws IOException when fails to generate class file
282 */
283 public static File generateTypeDefClassFile(File file, String className, List<String> imports,
284 String pkg, List<AttributeInfo> cachedAttributeList, CachedFileHandle handle) throws IOException {
285 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
286 initiateFile(file, className, GENERATE_TYPEDEF_CLASS, imports, pkg);
287
288 List<String> typeDef = new ArrayList<>();
289 try {
290 typeDef.add(handle.getTempData(TempDataStoreTypes.TYPE_DEF, className, path));
291 } catch (ClassNotFoundException | IOException e) {
292 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
293 throw new IOException("Fail to read data from temp file.");
294 }
295
296 /**
297 * Add attributes to the file.
298 */
299 for (String attribute : typeDef) {
300 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
301 }
302
303 return file;
304 }
305
306 /**
307 * Returns constructor string for impl class.
308 *
309 * @param yangName class name
310 * @return constructor string
311 */
312 private static String getConstructorString(String yangName) {
313
314 String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1);
315 String javadoc = MethodsGenerator.getConstructorString(yangName);
316 String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
317 + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
318 + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
319 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
320 return javadoc + constructor;
321 }
322
323 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530324 * Initiate generation of file based on generated file type.
325 *
326 * @param file generated file
327 * @param className generated file class name
328 * @param type generated file type
329 * @param imports imports for the file
330 * @param pkg generated file package
331 * @throws IOException when fails to generate a file
332 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530333 private static void initiateFile(File file, String className, int type, List<String> imports,
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530334 String pkg) throws IOException {
335 try {
336 file.createNewFile();
337 appendContents(file, className, type, imports, pkg);
338 } catch (IOException e) {
339 throw new IOException("Failed to create " + file.getName() + " class file.");
340 }
341 }
342
343 /**
344 * Appends the temp files to main files.
345 *
346 * @param appendFile temp file
347 * @param srcFile main file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530348 * @throws IOException when fails to append contents
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530349 */
350 public static void appendFileContents(File appendFile, File srcFile) throws IOException {
351 try {
352 FileSystemUtil.appendFileContents(appendFile, srcFile);
353 } catch (IOException e) {
354 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
355 }
356 }
357
358 /**
359 * Append methods to the generated files.
360 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530361 * @param file file in which method needs to be appended
362 * @param method method which needs to be appended
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530363 */
364 private static void appendMethod(File file, String method) throws IOException {
365 insert(file, method);
366 }
367
368 /**
369 * Closes the current generated file.
370 *
371 * @param fileType generate file type
372 * @param yangName file name
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530373 * @return end of class definition string
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530374 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530375 public static String closeFile(int fileType, String yangName) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530376 return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
377 }
378
379 /**
380 * Parses attribute info and fetch specific data and creates serialized
381 * files of it.
382 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530383 * @param attr attribute info.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530384 * @param genFileType generated file type
385 * @param className class name
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530386 * @param path file path
387 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530388 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530389 public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className, String path,
390 CachedFileHandle handle) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530391
392 String attrString = "";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530393
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530394 String getterString = "";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530395 String getterImplString = "";
396
397 String setterString = "";
398 String setterImplString = "";
399
400 String constructorString = "";
401 String typeDefString = "";
402
403 String toString = "";
404 String hashCodeString = "";
405 String equalsString = "";
406
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530407 className = JavaIdentifierSyntax.getCaptialCase(className);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530408
409 try {
410 /*
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530411 * Get the attribute definition and save attributes to temporary
412 * file.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530413 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530414
415 boolean isList = attr.isListAttr();
416 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
417 if (attr.isQualifiedName()) {
418 attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
419 attr.getImportInfo().getClassInfo(),
420 attributeName, attr.isListAttr());
421 } else {
422 attrString = JavaCodeSnippetGen.getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(),
423 attributeName, attr.isListAttr());
424 }
425 handle.setTempData(attrString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION,
426 TempDataStoreTypes.ATTRIBUTE, className,
427 path);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530428
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530429 if ((genFileType & INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530430 getterString = MethodsGenerator.getGetterString(attr);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530431 handle.setTempData(getterString + UtilConstants.NEW_LINE,
432 TempDataStoreTypes.GETTER_METHODS,
433 className,
434 path);
435
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530436 }
437
438 if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530439 setterString = MethodsGenerator.getSetterString(attr, className);
440 handle.setTempData(setterString + UtilConstants.NEW_LINE,
441 TempDataStoreTypes.SETTER_METHODS,
442 className,
443 path);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530444 }
445
446 if ((genFileType & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530447 getterImplString = MethodsGenerator.getGetterForClass(attr);
448 handle.setTempData(
449 MethodsGenerator.getOverRideString() + getterImplString + UtilConstants.NEW_LINE,
450 TempDataStoreTypes.GETTER_METHODS_IMPL, className,
451 path);
452 setterImplString = MethodsGenerator.getSetterForClass(attr, className);
453 handle.setTempData(
454 MethodsGenerator.getOverRideString() + setterImplString + UtilConstants.NEW_LINE,
455 TempDataStoreTypes.SETTER_METHODS_IMPL, className,
456 path);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530457 }
458
459 if ((genFileType & IMPL_CLASS_MASK) != 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530460 constructorString = MethodsGenerator.getConstructor(className, attr);
461 handle.setTempData(constructorString, TempDataStoreTypes.CONSTRUCTOR, className,
462 path);
463
464 hashCodeString = MethodsGenerator.getHashCodeMethod(attr);
465 handle.setTempData(hashCodeString + UtilConstants.NEW_LINE,
466 TempDataStoreTypes.HASH_CODE,
467 className,
468 path);
469 equalsString = MethodsGenerator.getEqualsMethod(attr);
470 handle.setTempData(equalsString + UtilConstants.NEW_LINE,
471 TempDataStoreTypes.EQUALS,
472 className,
473 path);
474
475 toString = MethodsGenerator.getToStringMethod(attr);
476 handle.setTempData(toString + UtilConstants.NEW_LINE,
477 TempDataStoreTypes.TO_STRING,
478 className,
479 path);
480
481 }
482
483 if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
484
485 if (attr.isQualifiedName()) {
486 typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
487 attr.getImportInfo().getClassInfo(),
488 attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
489 } else {
490 typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(null,
491 attr.getImportInfo().getClassInfo(),
492 attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
493 }
494
495 typeDefString = typeDefString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
496 + UtilConstants.JAVA_DOC_FIRST_LINE;
497
498 typeDefString = typeDefString
499 + MethodsGenerator.getDefaultConstructorString(className, UtilConstants.PRIVATE)
500 + UtilConstants.NEW_LINE;
501
502 typeDefString = typeDefString
503 + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_CONSTRUCTOR, className, isList)
504 + MethodsGenerator.getTypeDefConstructor(attr, className)
505 + UtilConstants.NEW_LINE;
506
507 typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.OF, className, isList)
508 + MethodsGenerator.getOfMethod(className, attr) + UtilConstants.NEW_LINE;
509
510 typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.GETTER, className, isList)
511 + MethodsGenerator.getGetterForClass(attr) + UtilConstants.NEW_LINE;
512
513 typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, className, isList)
514 + MethodsGenerator.getSetterForTypeDefClass(attr)
515 + UtilConstants.NEW_LINE;
516
517 hashCodeString = hashCodeString + MethodsGenerator.getHashCodeMethodOpen()
518 + MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, "");
519 hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE;
520
521 equalsString = equalsString + MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE
522 + MethodsGenerator.getEqualsMethod(attr);
523 equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE;
524
525 toString = toString + MethodsGenerator.getToStringMethodOpen()
526 + MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE
527 + MethodsGenerator.getToStringMethodClose()
528 + UtilConstants.NEW_LINE;
529 typeDefString = typeDefString + hashCodeString + equalsString + toString;
530 handle.setTempData(typeDefString, TempDataStoreTypes.TYPE_DEF, className,
531 path);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530532 }
533 } catch (IOException e) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530534 log.info("Failed to set data for " + attr.getAttributeName() + " in temp data files.");
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530535 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530536
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530537 }
538
539 /**
540 * Appends all the contents into a generated java file.
541 *
542 * @param file generated file
543 * @param fileName generated file name
544 * @param type generated file type
545 * @param pkg generated file package
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530546 * @param importsList list of java imports.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530547 * @throws IOException when fails to append contents
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530548 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530549 private static void appendContents(File file, String fileName, int type, List<String> importsList,
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530550 String pkg) throws IOException {
551
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530552 if (pkg.contains(UtilConstants.YANG_GEN_DIR)) {
553 String[] strArray = pkg.split(UtilConstants.YANG_GEN_DIR);
554 pkg = strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD);
555 }
556
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530557 if ((type & IMPL_CLASS_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530558
559 write(file, fileName, type, JavaDocType.IMPL_CLASS);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530560 } else if ((type & BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530561
562 write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530563 } else if ((type & GENERATE_TYPEDEF_CLASS) != 0) {
564 insert(file, CopyrightHeader.getCopyrightHeader());
565 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
566 if (importsList != null) {
567 insert(file, UtilConstants.NEW_LINE);
568 for (String imports : importsList) {
569 insert(file, imports);
570 }
571 insert(file, UtilConstants.NEW_LINE);
572 }
573 write(file, fileName, type, JavaDocType.IMPL_CLASS);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530574 } else {
575
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530576 if ((type & INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530577 insert(file, CopyrightHeader.getCopyrightHeader());
578 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
579 if (importsList != null) {
580 insert(file, UtilConstants.NEW_LINE);
581 for (String imports : importsList) {
582 insert(file, imports);
583 }
584 insert(file, UtilConstants.NEW_LINE);
585 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530586 write(file, fileName, type, JavaDocType.INTERFACE);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530587 } else if ((type & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530588 insert(file, CopyrightHeader.getCopyrightHeader());
589 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
590 if (importsList != null) {
591 insert(file, UtilConstants.NEW_LINE);
592 for (String imports : importsList) {
593 insert(file, imports);
594 }
595 insert(file, UtilConstants.NEW_LINE);
596 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530597 write(file, fileName, type, JavaDocType.BUILDER_CLASS);
598 }
599 }
600 }
601
602 /**
603 * Write data to the specific generated file.
604 *
605 * @param file generated file
606 * @param fileName file name
607 * @param genType generated file type
608 * @param javaDocType java doc type
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530609 * @throws IOException when fails to write into a file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530610 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530611 private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType)
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530612 throws IOException {
613
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530614 insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName, false));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530615 insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
616 }
617
618 /**
619 * Insert in the generated file.
620 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530621 * @param file file in which need to be inserted
622 * @param data data which need to be inserted
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530623 * @throws IOException when fails to insert into file
624 */
625 public static void insert(File file, String data) throws IOException {
626 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530627 FileSystemUtil.updateFileHandle(file, data, false);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530628 } catch (IOException e) {
629 throw new IOException("Failed to insert in " + file + "file");
630 }
631 }
632
633 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530634 * Closes the files handle for generate files.
635 *
636 * @param file generate files
637 * @throws IOException when failed to close the file handle
638 */
639 public static void closeFileHandles(File file) throws IOException {
640 try {
641 FileSystemUtil.updateFileHandle(file, null, true);
642 } catch (IOException e) {
643 throw new IOException("Failed to close file handle for " + file + "file");
644 }
645 }
646
647 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530648 * Removes temp files.
649 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530650 * @param file file to be removed
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530651 */
652 public static void clean(File file) {
653 if (file.exists()) {
654 file.delete();
655 }
656 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530657
658 /**
659 * Removes temp files.
660 *
661 * @param tempDir temp directory
662 * @throws IOException when fails to delete the directory
663 */
664 public static void cleanTempFiles(File tempDir) throws IOException {
665 FileUtils.deleteDirectory(tempDir);
666 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530667}