blob: 60d8c2a4d4d737f78da1007c7e6581d316f6b802 [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
19import java.io.File;
20import java.io.IOException;
21import java.util.LinkedList;
22import java.util.List;
23
24import org.onosproject.yangutils.translator.GeneratedFileType;
25import org.onosproject.yangutils.translator.tojava.AttributeInfo;
26import org.onosproject.yangutils.utils.UtilConstants;
27import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
28import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
29import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
30import org.onosproject.yangutils.utils.io.impl.TempDataStore;
31import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
32import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType;
33
34import static org.slf4j.LoggerFactory.getLogger;
35import org.slf4j.Logger;
36
37public final class JavaFileGenerator {
38
39 private static final Logger log = getLogger(JavaFileGenerator.class);
40
41 /**
42 * Default constructor.
43 */
44 private JavaFileGenerator() {
45 }
46
47 /**
48 * Returns generated interface file for current node.
49 * @param file file
50 * @param className class name
51 * @param imports imports for the file
52 * @param attrList attribute info
53 * @param pkg generated file package
54 * @return interface file
55 * @throws IOException when fails to write in file.
56 */
57 public static File generateInterfaceFile(File file, String className, List<String> imports,
58 List<AttributeInfo> attrList, String pkg) throws IOException {
59
60 initiateFile(file, className, GeneratedFileType.INTERFACE, imports, pkg);
61 List<String> methods = getMethodStrings(TempDataStoreType.GETTER_METHODS, GeneratedFileType.INTERFACE,
62 className, file, attrList);
63
64 /**
65 * Add getter methods to interface file.
66 */
67 for (String method : methods) {
68 appendMethod(file, method);
69 }
70 return file;
71 }
72
73 /**
74 * Return generated builder interface file for current node.
75 * @param file file
76 * @param className class name
77 * @param pkg generated file package
78 * @param attrList attribute info
79 * @return builder interface file
80 * @throws IOException when fails to write in file.
81 */
82 public static File generateBuilderInterfaceFile(File file, String className, String pkg,
83 List<AttributeInfo> attrList) throws IOException {
84
85 initiateFile(file, className, GeneratedFileType.BUILDER_INTERFACE, null, pkg);
86 List<String> methods = getMethodStrings(TempDataStoreType.BUILDER_INTERFACE_METHODS,
87 GeneratedFileType.BUILDER_INTERFACE, className, file, attrList);
88
89 /**
90 * Add build method to builder interface file.
91 */
92 methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className));
93
94 /**
95 * Add getters and setters in builder interface.
96 */
97 for (String method : methods) {
98 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
99 }
100
101 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
102 return file;
103 }
104
105 /**
106 * Returns generated builder class file for current node.
107 * @param file file
108 * @param className class name
109 * @param imports imports for the file
110 * @param pkg generated file package
111 * @param attrList attribute info
112 * @return builder class file
113 * @throws IOException when fails to write in file.
114 */
115 public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg,
116 List<AttributeInfo> attrList) throws IOException {
117
118 initiateFile(file, className, GeneratedFileType.BUILDER_CLASS, imports, pkg);
119 List<String> methods = getMethodStrings(TempDataStoreType.BUILDER_METHODS, GeneratedFileType.BUILDER_CLASS,
120 className, file, attrList);
121
122 /**
123 * Add default constructor and build method impl.
124 */
125 methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
126 + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.BUILDER_CLASS, className));
127 methods.add(MethodsGenerator.getBuildString(className));
128
129 /**
130 * Add attribute strings.
131 */
132 addAttributeSring(file, className, attrList, GeneratedFileType.BUILDER_CLASS);
133
134 /**
135 * Add methods in builder class.
136 */
137 for (String method : methods) {
138 appendMethod(file, method + UtilConstants.NEW_LINE);
139 }
140 return file;
141 }
142
143 /**
144 * Returns generated impl class file for current node.
145 * @param file file
146 * @param className class name
147 * @param pkg generated file package
148 * @param attrList attribute's info
149 * @return impl class file
150 * @throws IOException when fails to write in file.
151 */
152 public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList)
153 throws IOException {
154
155 initiateFile(file, className, GeneratedFileType.IMPL, null, pkg);
156 List<String> methods = getMethodStrings(TempDataStoreType.IMPL_METHODS, GeneratedFileType.IMPL, className, file,
157 attrList);
158
159 /**
160 * Add attributes.
161 */
162 addAttributeSring(file, className, attrList, GeneratedFileType.IMPL);
163
164 /**
165 * Add default constructor and constructor methods.
166 */
167 methods.add(UtilConstants.JAVA_DOC_FIRST_LINE
168 + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.IMPL, className));
169 methods.add(MethodsGenerator.getConstructorString(className));
170
171 /**
172 * Add methods in impl class.
173 */
174 for (String method : methods) {
175 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
176 }
177 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
178
179 return file;
180 }
181
182 /**
183 * Adds attribute string for generated files.
184 *
185 * @param className class name
186 * @param file generated file
187 * @param attrList attribute info
188 * @param genFileType generated file type
189 * @param IOException when fails to add attributes in files.
190 */
191 private static void addAttributeSring(File file, String className, List<AttributeInfo> attrList,
192 GeneratedFileType genFileType) throws IOException {
193 List<String> attributes = new LinkedList<>();
194 try {
195 attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
196 } catch (ClassNotFoundException | IOException e) {
197 log.info("There is no attribute info of " + className + " YANG file in the serialized files.");
198 }
199
200 if (attrList != null) {
201 MethodsGenerator.setAttrInfo(attrList);
202 for (AttributeInfo attr : attrList) {
203 if (attr.isListAttr()) {
204 attr.setAttributeType(AttributesJavaDataType.getListString(attr));
205 }
206 attributes.add(getAttributeString(attr, genFileType));
207 }
208 }
209
210 /**
211 * Add attributes to the file.
212 */
213 for (String attribute : attributes) {
214 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
215 }
216 insert(file, UtilConstants.NEW_LINE);
217 }
218
219 /**
220 * Returns method strings for generated files.
221 *
222 * @param dataStoreType temp data store file type.
223 * @param genFileType generated file type
224 * @param className generated file name
225 * @param attrList attribute info
226 * @return method strings
227 */
228 private static List<String> getMethodStrings(TempDataStoreType dataStoreType, GeneratedFileType genFileType,
229 String className, File file, List<AttributeInfo> attrList) {
230
231 List<String> methods = new LinkedList<>();
232 try {
233 methods = TempDataStore.getTempData(dataStoreType, className);
234 } catch (ClassNotFoundException | IOException e) {
235 log.info("There is no attribute info of " + className + " YANG file in the serialized files.");
236 }
237
238 if (attrList != null) {
239 MethodsGenerator.setAttrInfo(attrList);
240 for (AttributeInfo attr : attrList) {
241 if (attr.isListAttr()) {
242 attr.setAttributeType(AttributesJavaDataType.getListString(attr));
243 }
244 methods.add(MethodsGenerator.getMethodString(attr, genFileType));
245 }
246 }
247 return methods;
248 }
249
250 /**
251 * Initiate generation of file based on generated file type.
252 *
253 * @param file generated file
254 * @param className generated file class name
255 * @param type generated file type
256 * @param imports imports for the file
257 * @param pkg generated file package
258 * @throws IOException when fails to generate a file
259 */
260 private static void initiateFile(File file, String className, GeneratedFileType type, List<String> imports,
261 String pkg) throws IOException {
262 try {
263 file.createNewFile();
264 appendContents(file, className, type, imports, pkg);
265 } catch (IOException e) {
266 throw new IOException("Failed to create " + file.getName() + " class file.");
267 }
268 }
269
270 /**
271 * Appends the temp files to main files.
272 *
273 * @param appendFile temp file
274 * @param srcFile main file
275 * @throws IOException when fails to append contents.
276 */
277 public static void appendFileContents(File appendFile, File srcFile) throws IOException {
278 try {
279 FileSystemUtil.appendFileContents(appendFile, srcFile);
280 } catch (IOException e) {
281 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
282 }
283 }
284
285 /**
286 * Append methods to the generated files.
287 *
288 * @param file file in which method needs to be appended.
289 * @param method method which needs to be appended.
290 */
291 private static void appendMethod(File file, String method) throws IOException {
292 insert(file, method);
293 }
294
295 /**
296 * Closes the current generated file.
297 *
298 * @param fileType generate file type
299 * @param yangName file name
300 * @return end of class definition string.
301 */
302 public static String closeFile(GeneratedFileType fileType, String yangName) {
303 return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
304 }
305
306 /**
307 * Parses attribute info and fetch specific data and creates serialized
308 * files of it.
309 *
310 * @param attr attribute info.
311 * @param genFileType generated file type
312 * @param className class name
313 */
314 public static void parseAttributeInfo(AttributeInfo attr, GeneratedFileType genFileType, String className) {
315
316 String attrString = "";
317 String methodString = "";
318 String getterString = "";
319
320 try {
321 /*
322 * Serialize attributes.
323 */
324 attrString = getAttributeString(attr, genFileType);
325 attrString = attrString.replace("\"", "");
326 TempDataStore.setTempData(attrString, TempDataStore.TempDataStoreType.ATTRIBUTE, className);
327
328 if (genFileType.equals(GeneratedFileType.ALL)) {
329
330 methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.INTERFACE);
331 TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
332
333 methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_CLASS);
334 TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.BUILDER_METHODS, className);
335
336 methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_INTERFACE);
337 TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.BUILDER_INTERFACE_METHODS,
338 className);
339
340 methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.IMPL);
341 TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.IMPL_METHODS, className);
342
343 } else if (genFileType.equals(GeneratedFileType.INTERFACE)) {
344
345 getterString = MethodsGenerator.getGetterString(attr);
346 TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
347 }
348 } catch (IOException e) {
349 log.info("Failed to get data for " + attr.getAttributeName() + " from serialized files.");
350 }
351 }
352
353 /**
354 * Returns attribute string.
355 *
356 * @param attr attribute info
357 * @param genFileType generated file type
358 * @return attribute string
359 */
360 private static String getAttributeString(AttributeInfo attr, GeneratedFileType genFileType) {
361 return JavaCodeSnippetGen.getJavaAttributeInfo(genFileType, attr.getAttributeName(), attr.getAttributeType());
362 }
363
364 /**
365 * Appends all the contents into a generated java file.
366 *
367 * @param file generated file
368 * @param fileName generated file name
369 * @param type generated file type
370 * @param pkg generated file package
371 * @throws IOException when fails to append contents.
372 */
373 private static void appendContents(File file, String fileName, GeneratedFileType type, List<String> importsList,
374 String pkg) throws IOException {
375
376 if (type.equals(GeneratedFileType.IMPL)) {
377
378 write(file, fileName, type, JavaDocType.IMPL_CLASS);
379 } else if (type.equals(GeneratedFileType.BUILDER_INTERFACE)) {
380
381 write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
382 } else {
383
384 if (type.equals(GeneratedFileType.INTERFACE)) {
385 insert(file, CopyrightHeader.getCopyrightHeader());
386 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
387 if (importsList != null) {
388 insert(file, UtilConstants.NEW_LINE);
389 for (String imports : importsList) {
390 insert(file, imports);
391 }
392 insert(file, UtilConstants.NEW_LINE);
393 }
394 insert(file, UtilConstants.NEW_LINE);
395 write(file, fileName, type, JavaDocType.INTERFACE);
396 } else if (type.equals(GeneratedFileType.BUILDER_CLASS)) {
397 insert(file, CopyrightHeader.getCopyrightHeader());
398 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
399 if (importsList != null) {
400 insert(file, UtilConstants.NEW_LINE);
401 for (String imports : importsList) {
402 insert(file, imports);
403 }
404 insert(file, UtilConstants.NEW_LINE);
405 }
406 insert(file, UtilConstants.NEW_LINE);
407 write(file, fileName, type, JavaDocType.BUILDER_CLASS);
408 }
409 }
410 }
411
412 /**
413 * Write data to the specific generated file.
414 *
415 * @param file generated file
416 * @param fileName file name
417 * @param genType generated file type
418 * @param javaDocType java doc type
419 * @throws IOException when fails to write into a file.
420 */
421 private static void write(File file, String fileName, GeneratedFileType genType, JavaDocGen.JavaDocType javaDocType)
422 throws IOException {
423
424 insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName));
425 insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
426 }
427
428 /**
429 * Insert in the generated file.
430 *
431 * @param file file in which need to be inserted.
432 * @param data data which need to be inserted.
433 * @throws IOException when fails to insert into file
434 */
435 public static void insert(File file, String data) throws IOException {
436 try {
437 FileSystemUtil.insertStringInFile(file, data);
438 } catch (IOException e) {
439 throw new IOException("Failed to insert in " + file + "file");
440 }
441 }
442
443 /**
444 * Removes temp files.
445 *
446 * @param file file to be removed.
447 */
448 public static void clean(File file) {
449 if (file.exists()) {
450 file.delete();
451 }
452 }
453}