blob: 221f7576c1779ea4b8f82b195726fcb203caed70 [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 saraswal8f2a6c52016-03-09 18:34:56 +053039import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053040import org.slf4j.Logger;
41
Vinod Kumar Sc4216002016-03-03 19:55:30 +053042/**
43 * Generates java file.
44 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +053045public final class JavaFileGenerator {
46
47 private static final Logger log = getLogger(JavaFileGenerator.class);
48
49 /**
50 * Default constructor.
51 */
52 private JavaFileGenerator() {
53 }
54
55 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053056 * Returns a file object for generated file.
57 *
58 * @param fileName file name
59 * @param filePath file package path
60 * @param extension file extension
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053061 * @param handle cached file handle
Vinod Kumar Sc4216002016-03-03 19:55:30 +053062 * @return file object
63 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053064 public static File getFileObject(String filePath, String fileName, String extension, CachedFileHandle handle) {
65 return new File(handle.getCodeGenFilePath() + filePath + File.separator + fileName + extension);
Vinod Kumar Sc4216002016-03-03 19:55:30 +053066 }
67
68 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +053069 * Returns generated interface file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053070 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +053071 * @param file file
72 * @param className class name
73 * @param imports imports for the file
74 * @param attrList attribute info
75 * @param pkg generated file package
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053076 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +053077 * @return interface file
Vinod Kumar Sc4216002016-03-03 19:55:30 +053078 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +053079 */
80 public static File generateInterfaceFile(File file, String className, List<String> imports,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053081 List<AttributeInfo> attrList, String pkg, CachedFileHandle handle) throws IOException {
82 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
Vinod Kumar Sc4216002016-03-03 19:55:30 +053083 initiateFile(file, className, INTERFACE_MASK, imports, pkg);
84
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053085 if (!attrList.isEmpty()) {
86 List<String> methods = new ArrayList<>();
87 try {
88 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
89 } catch (ClassNotFoundException | IOException e) {
90 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
91 throw new IOException("Fail to read data from temp file.");
92 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +053093
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053094 /**
95 * Add getter methods to interface file.
96 */
97 for (String method : methods) {
98 appendMethod(file, method);
99 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530100 }
101 return file;
102 }
103
104 /**
105 * Return generated builder interface file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530106 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530107 * @param file file
108 * @param className class name
109 * @param pkg generated file package
110 * @param attrList attribute info
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530111 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530112 * @return builder interface file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530113 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530114 */
115 public static File generateBuilderInterfaceFile(File file, String className, String pkg,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530116 List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
117 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530118 initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530119 List<String> methods = new ArrayList<>();
120
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530121 if (!attrList.isEmpty()) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530122
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530123 try {
124 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
125 methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path));
126 } catch (ClassNotFoundException | IOException e) {
127 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
128 throw new IOException("Fail to read data from temp file.");
129 }
130 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530131 /**
132 * Add build method to builder interface file.
133 */
134 methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className));
135
136 /**
137 * Add getters and setters in builder interface.
138 */
139 for (String method : methods) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530140 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530141 }
142
143 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
144 return file;
145 }
146
147 /**
148 * Returns generated builder class file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530149 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530150 * @param file file
151 * @param className class name
152 * @param imports imports for the file
153 * @param pkg generated file package
154 * @param attrList attribute info
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530155 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530156 * @return builder class file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530157 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530158 */
159 public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530160 List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
161 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530162 initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg);
163
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530164 List<String> methods = new ArrayList<>();
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530165 if (!attrList.isEmpty()) {
166 /**
167 * Add attribute strings.
168 */
169 List<String> attributes = new ArrayList<>();
170 try {
171 attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
172 } catch (ClassNotFoundException | IOException e) {
173 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
174 throw new IOException("Fail to read data from temp file.");
175 }
176 /**
177 * Add attributes to the file.
178 */
179 for (String attribute : attributes) {
180 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
181 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530182
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530183 try {
184 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
185 methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path));
186 } catch (ClassNotFoundException | IOException e) {
187 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
188 throw new IOException("Fail to read data from temp file.");
189 }
190
191 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530192 /**
193 * Add default constructor and build method impl.
194 */
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530195 methods.add(
196 UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
197 + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER,
198 UtilConstants.PUBLIC));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530199 methods.add(MethodsGenerator.getBuildString(className));
200
201 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530202 * Add methods in builder class.
203 */
204 for (String method : methods) {
205 appendMethod(file, method + UtilConstants.NEW_LINE);
206 }
207 return file;
208 }
209
210 /**
211 * Returns generated impl class file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530212 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530213 * @param file file
214 * @param className class name
215 * @param pkg generated file package
216 * @param attrList attribute's info
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530217 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530218 * @return impl class file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530219 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530220 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530221 public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList,
222 CachedFileHandle handle)
223 throws IOException {
224 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
225 initiateFile(file, className, IMPL_CLASS_MASK, null, path);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530226
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530227 List<String> methods = new ArrayList<>();
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530228 if (!attrList.isEmpty()) {
229 List<String> attributes = new ArrayList<>();
230 try {
231 attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
232 } catch (ClassNotFoundException | IOException e) {
233 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
234 throw new IOException("Fail to read data from temp file.");
235 }
236
237 /**
238 * Add attributes to the file.
239 */
240 for (String attribute : attributes) {
241 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
242 }
243
244 try {
245
246 methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
247
248 methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen()
249 + YangIoUtils
250 .partString(handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(
251 UtilConstants.NEW_LINE, ""))));
252
253 methods.add(MethodsGenerator
254 .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL)
255 + handle.getTempData(TempDataStoreTypes.EQUALS, className, path)));
256
257 methods.add(MethodsGenerator.getToStringMethodOpen()
258 + handle.getTempData(TempDataStoreTypes.TO_STRING, className, path)
259 + MethodsGenerator.getToStringMethodClose());
260
261 } catch (ClassNotFoundException | IOException e) {
262 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
263 throw new IOException("Fail to read data from temp file.");
264 }
265
266 }
267
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530268 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530269 methods.add(getConstructorString(className)
270 + handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path)
271 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530272 } catch (ClassNotFoundException | IOException e) {
273 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
274 throw new IOException("Fail to read data from temp file.");
275 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530276 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530277 * Add methods in impl class.
278 */
279 for (String method : methods) {
280 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
281 }
282 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
283
284 return file;
285 }
286
287 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530288 * Generate class file for type def.
289 *
290 * @param file generated file
291 * @param className file name
292 * @param imports imports for file
293 * @param pkg package path
294 * @param cachedAttributeList attribute list
295 * @param handle cached file handle
296 * @return type def class file
297 * @throws IOException when fails to generate class file
298 */
299 public static File generateTypeDefClassFile(File file, String className, List<String> imports,
300 String pkg, List<AttributeInfo> cachedAttributeList, CachedFileHandle handle) throws IOException {
301 String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
302 initiateFile(file, className, GENERATE_TYPEDEF_CLASS, imports, pkg);
303
304 List<String> typeDef = new ArrayList<>();
305 try {
306 typeDef.add(handle.getTempData(TempDataStoreTypes.TYPE_DEF, className, path));
307 } catch (ClassNotFoundException | IOException e) {
308 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
309 throw new IOException("Fail to read data from temp file.");
310 }
311
312 /**
313 * Add attributes to the file.
314 */
315 for (String attribute : typeDef) {
316 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
317 }
318
319 return file;
320 }
321
322 /**
323 * Returns constructor string for impl class.
324 *
325 * @param yangName class name
326 * @return constructor string
327 */
328 private static String getConstructorString(String yangName) {
329
330 String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1);
331 String javadoc = MethodsGenerator.getConstructorString(yangName);
332 String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
333 + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530334 + UtilConstants.SPACE + builderAttribute + UtilConstants.BUILDER + UtilConstants.OBJECT
335 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
336 + UtilConstants.NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530337 return javadoc + constructor;
338 }
339
340 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530341 * Initiate generation of file based on generated file type.
342 *
343 * @param file generated file
344 * @param className generated file class name
345 * @param type generated file type
346 * @param imports imports for the file
347 * @param pkg generated file package
348 * @throws IOException when fails to generate a file
349 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530350 private static void initiateFile(File file, String className, int type, List<String> imports,
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530351 String pkg) throws IOException {
352 try {
353 file.createNewFile();
354 appendContents(file, className, type, imports, pkg);
355 } catch (IOException e) {
356 throw new IOException("Failed to create " + file.getName() + " class file.");
357 }
358 }
359
360 /**
361 * Appends the temp files to main files.
362 *
363 * @param appendFile temp file
364 * @param srcFile main file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530365 * @throws IOException when fails to append contents
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530366 */
367 public static void appendFileContents(File appendFile, File srcFile) throws IOException {
368 try {
369 FileSystemUtil.appendFileContents(appendFile, srcFile);
370 } catch (IOException e) {
371 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
372 }
373 }
374
375 /**
376 * Append methods to the generated files.
377 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530378 * @param file file in which method needs to be appended
379 * @param method method which needs to be appended
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530380 */
381 private static void appendMethod(File file, String method) throws IOException {
382 insert(file, method);
383 }
384
385 /**
386 * Closes the current generated file.
387 *
388 * @param fileType generate file type
389 * @param yangName file name
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530390 * @return end of class definition string
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530391 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530392 public static String closeFile(int fileType, String yangName) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530393 return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
394 }
395
396 /**
397 * Parses attribute info and fetch specific data and creates serialized
398 * files of it.
399 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530400 * @param attr attribute info.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530401 * @param genFileType generated file type
402 * @param className class name
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530403 * @param path file path
404 * @param handle cached file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530405 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530406 public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className, String path,
407 CachedFileHandle handle) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530408
409 String attrString = "";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530410
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530411 String getterString = "";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530412 String getterImplString = "";
413
414 String setterString = "";
415 String setterImplString = "";
416
417 String constructorString = "";
418 String typeDefString = "";
419
420 String toString = "";
421 String hashCodeString = "";
422 String equalsString = "";
423
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530424 className = JavaIdentifierSyntax.getCaptialCase(className);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530425
426 try {
427 /*
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530428 * Get the attribute definition and save attributes to temporary
429 * file.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530430 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530431
432 boolean isList = attr.isListAttr();
433 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
434 if (attr.isQualifiedName()) {
435 attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
436 attr.getImportInfo().getClassInfo(),
437 attributeName, attr.isListAttr());
438 } else {
439 attrString = JavaCodeSnippetGen.getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(),
440 attributeName, attr.isListAttr());
441 }
442 handle.setTempData(attrString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION,
443 TempDataStoreTypes.ATTRIBUTE, className,
444 path);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530445
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530446 if ((genFileType & INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530447 getterString = MethodsGenerator.getGetterString(attr);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530448 handle.setTempData(getterString + UtilConstants.NEW_LINE,
449 TempDataStoreTypes.GETTER_METHODS,
450 className,
451 path);
452
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530453 }
454
455 if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530456 setterString = MethodsGenerator.getSetterString(attr, className);
457 handle.setTempData(setterString + UtilConstants.NEW_LINE,
458 TempDataStoreTypes.SETTER_METHODS,
459 className,
460 path);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530461 }
462
463 if ((genFileType & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530464 getterImplString = MethodsGenerator.getGetterForClass(attr);
465 handle.setTempData(
466 MethodsGenerator.getOverRideString() + getterImplString + UtilConstants.NEW_LINE,
467 TempDataStoreTypes.GETTER_METHODS_IMPL, className,
468 path);
469 setterImplString = MethodsGenerator.getSetterForClass(attr, className);
470 handle.setTempData(
471 MethodsGenerator.getOverRideString() + setterImplString + UtilConstants.NEW_LINE,
472 TempDataStoreTypes.SETTER_METHODS_IMPL, className,
473 path);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530474 }
475
476 if ((genFileType & IMPL_CLASS_MASK) != 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530477 constructorString = MethodsGenerator.getConstructor(className, attr);
478 handle.setTempData(constructorString, TempDataStoreTypes.CONSTRUCTOR, className,
479 path);
480
481 hashCodeString = MethodsGenerator.getHashCodeMethod(attr);
482 handle.setTempData(hashCodeString + UtilConstants.NEW_LINE,
483 TempDataStoreTypes.HASH_CODE,
484 className,
485 path);
486 equalsString = MethodsGenerator.getEqualsMethod(attr);
487 handle.setTempData(equalsString + UtilConstants.NEW_LINE,
488 TempDataStoreTypes.EQUALS,
489 className,
490 path);
491
492 toString = MethodsGenerator.getToStringMethod(attr);
493 handle.setTempData(toString + UtilConstants.NEW_LINE,
494 TempDataStoreTypes.TO_STRING,
495 className,
496 path);
497
498 }
499
500 if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
501
502 if (attr.isQualifiedName()) {
503 typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
504 attr.getImportInfo().getClassInfo(),
505 attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
506 } else {
507 typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(null,
508 attr.getImportInfo().getClassInfo(),
509 attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
510 }
511
512 typeDefString = typeDefString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
513 + UtilConstants.JAVA_DOC_FIRST_LINE;
514
515 typeDefString = typeDefString
516 + MethodsGenerator.getDefaultConstructorString(className, UtilConstants.PRIVATE)
517 + UtilConstants.NEW_LINE;
518
519 typeDefString = typeDefString
520 + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_CONSTRUCTOR, className, isList)
521 + MethodsGenerator.getTypeDefConstructor(attr, className)
522 + UtilConstants.NEW_LINE;
523
524 typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.OF, className, isList)
525 + MethodsGenerator.getOfMethod(className, attr) + UtilConstants.NEW_LINE;
526
527 typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.GETTER, className, isList)
528 + MethodsGenerator.getGetterForClass(attr) + UtilConstants.NEW_LINE;
529
530 typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, className, isList)
531 + MethodsGenerator.getSetterForTypeDefClass(attr)
532 + UtilConstants.NEW_LINE;
533
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530534 hashCodeString = MethodsGenerator.getHashCodeMethodOpen()
535 + YangIoUtils.partString(
536 MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, ""));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530537 hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE;
538
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530539 equalsString = MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530540 + MethodsGenerator.getEqualsMethod(attr);
541 equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE;
542
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530543 toString = MethodsGenerator.getToStringMethodOpen()
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530544 + MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE
545 + MethodsGenerator.getToStringMethodClose()
546 + UtilConstants.NEW_LINE;
547 typeDefString = typeDefString + hashCodeString + equalsString + toString;
548 handle.setTempData(typeDefString, TempDataStoreTypes.TYPE_DEF, className,
549 path);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530550 }
551 } catch (IOException e) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530552 log.info("Failed to set data for " + attr.getAttributeName() + " in temp data files.");
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530553 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530554
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530555 }
556
557 /**
558 * Appends all the contents into a generated java file.
559 *
560 * @param file generated file
561 * @param fileName generated file name
562 * @param type generated file type
563 * @param pkg generated file package
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530564 * @param importsList list of java imports.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530565 * @throws IOException when fails to append contents
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530566 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530567 private static void appendContents(File file, String fileName, int type, List<String> importsList,
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530568 String pkg) throws IOException {
569
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530570 if (pkg.contains(UtilConstants.YANG_GEN_DIR)) {
571 String[] strArray = pkg.split(UtilConstants.YANG_GEN_DIR);
572 pkg = strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD);
573 }
574
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530575 if ((type & IMPL_CLASS_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530576
577 write(file, fileName, type, JavaDocType.IMPL_CLASS);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530578 } else if ((type & BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530579
580 write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530581 } else if ((type & GENERATE_TYPEDEF_CLASS) != 0) {
582 insert(file, CopyrightHeader.getCopyrightHeader());
583 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
584 if (importsList != null) {
585 insert(file, UtilConstants.NEW_LINE);
586 for (String imports : importsList) {
587 insert(file, imports);
588 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530589 }
590 write(file, fileName, type, JavaDocType.IMPL_CLASS);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530591 } else {
592
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530593 if ((type & INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530594 insert(file, CopyrightHeader.getCopyrightHeader());
595 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
596 if (importsList != null) {
597 insert(file, UtilConstants.NEW_LINE);
598 for (String imports : importsList) {
599 insert(file, imports);
600 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530601 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530602 write(file, fileName, type, JavaDocType.INTERFACE);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530603 } else if ((type & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530604 insert(file, CopyrightHeader.getCopyrightHeader());
605 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
606 if (importsList != null) {
607 insert(file, UtilConstants.NEW_LINE);
608 for (String imports : importsList) {
609 insert(file, imports);
610 }
611 insert(file, UtilConstants.NEW_LINE);
612 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530613 write(file, fileName, type, JavaDocType.BUILDER_CLASS);
614 }
615 }
616 }
617
618 /**
619 * Write data to the specific generated file.
620 *
621 * @param file generated file
622 * @param fileName file name
623 * @param genType generated file type
624 * @param javaDocType java doc type
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530625 * @throws IOException when fails to write into a file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530626 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530627 private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType)
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530628 throws IOException {
629
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530630 insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName, false));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530631 insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
632 }
633
634 /**
635 * Insert in the generated file.
636 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530637 * @param file file in which need to be inserted
638 * @param data data which need to be inserted
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530639 * @throws IOException when fails to insert into file
640 */
641 public static void insert(File file, String data) throws IOException {
642 try {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530643 FileSystemUtil.updateFileHandle(file, data, false);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530644 } catch (IOException e) {
645 throw new IOException("Failed to insert in " + file + "file");
646 }
647 }
648
649 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530650 * Closes the files handle for generate files.
651 *
652 * @param file generate files
653 * @throws IOException when failed to close the file handle
654 */
655 public static void closeFileHandles(File file) throws IOException {
656 try {
657 FileSystemUtil.updateFileHandle(file, null, true);
658 } catch (IOException e) {
659 throw new IOException("Failed to close file handle for " + file + "file");
660 }
661 }
662
663 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530664 * Removes temp files.
665 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530666 * @param file file to be removed
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530667 */
668 public static void clean(File file) {
669 if (file.exists()) {
670 file.delete();
671 }
672 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530673
674 /**
675 * Removes temp files.
676 *
677 * @param tempDir temp directory
678 * @throws IOException when fails to delete the directory
679 */
680 public static void cleanTempFiles(File tempDir) throws IOException {
681 FileUtils.deleteDirectory(tempDir);
682 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530683}