blob: ff275aea5d5378ebc6a6f8e459ef4f5a61c10401 [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;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053021import java.util.List;
22
Bharat saraswal4bf8b152016-02-25 02:26:43 +053023import org.onosproject.yangutils.translator.tojava.AttributeInfo;
24import org.onosproject.yangutils.utils.UtilConstants;
25import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
26import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
27import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053028import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053029import org.onosproject.yangutils.utils.io.impl.TempDataStore;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053030import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053031import org.slf4j.Logger;
32
Vinod Kumar Sc4216002016-03-03 19:55:30 +053033import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_CLASS_MASK;
34import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_INTERFACE_MASK;
35import static org.onosproject.yangutils.translator.GeneratedFileType.IMPL_CLASS_MASK;
36import static org.onosproject.yangutils.translator.GeneratedFileType.INTERFACE_MASK;
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Generates java file.
41 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +053042public final class JavaFileGenerator {
43
44 private static final Logger log = getLogger(JavaFileGenerator.class);
45
46 /**
47 * Default constructor.
48 */
49 private JavaFileGenerator() {
50 }
51
52 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053053 * Returns a file object for generated file.
54 *
55 * @param fileName file name
56 * @param filePath file package path
57 * @param extension file extension
58 * @return file object
59 */
60 public static File getFileObject(String filePath, String fileName, String extension) {
61 return new File(UtilConstants.YANG_GEN_DIR + filePath + File.separator + fileName + extension);
62 }
63
64 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +053065 * Returns generated interface file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053066 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +053067 * @param file file
68 * @param className class name
69 * @param imports imports for the file
70 * @param attrList attribute info
71 * @param pkg generated file package
72 * @return interface file
Vinod Kumar Sc4216002016-03-03 19:55:30 +053073 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +053074 */
75 public static File generateInterfaceFile(File file, String className, List<String> imports,
76 List<AttributeInfo> attrList, String pkg) throws IOException {
77
Vinod Kumar Sc4216002016-03-03 19:55:30 +053078 initiateFile(file, className, INTERFACE_MASK, imports, pkg);
79
80 List<String> methods;
81 try {
82 methods = TempDataStore.getTempData(TempDataStoreType.GETTER_METHODS, className);
83 } catch (ClassNotFoundException | IOException e) {
84 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
85 throw new IOException("Fail to read data from temp file.");
86 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +053087
88 /**
89 * Add getter methods to interface file.
90 */
91 for (String method : methods) {
92 appendMethod(file, method);
93 }
94 return file;
95 }
96
97 /**
98 * Return generated builder interface file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +053099 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530100 * @param file file
101 * @param className class name
102 * @param pkg generated file package
103 * @param attrList attribute info
104 * @return builder interface file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530105 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530106 */
107 public static File generateBuilderInterfaceFile(File file, String className, String pkg,
108 List<AttributeInfo> attrList) throws IOException {
109
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530110 initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg);
111 List<String> methods;
112 try {
113 methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_INTERFACE_METHODS,
114 className + UtilConstants.BUILDER + UtilConstants.INTERFACE);
115 } catch (ClassNotFoundException | IOException e) {
116 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
117 throw new IOException("Fail to read data from temp file.");
118 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530119
120 /**
121 * Add build method to builder interface file.
122 */
123 methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className));
124
125 /**
126 * Add getters and setters in builder interface.
127 */
128 for (String method : methods) {
129 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
130 }
131
132 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
133 return file;
134 }
135
136 /**
137 * Returns generated builder class file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530138 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530139 * @param file file
140 * @param className class name
141 * @param imports imports for the file
142 * @param pkg generated file package
143 * @param attrList attribute info
144 * @return builder class file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530145 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530146 */
147 public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg,
148 List<AttributeInfo> attrList) throws IOException {
149
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530150 initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg);
151
152 /**
153 * Add attribute strings.
154 */
155 List<String> attributes;
156 try {
157 attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
158 } catch (ClassNotFoundException | IOException e) {
159 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
160 throw new IOException("Fail to read data from temp file.");
161 }
162 /**
163 * Add attributes to the file.
164 */
165 for (String attribute : attributes) {
166 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
167 }
168 insert(file, UtilConstants.NEW_LINE);
169
170 List<String> methods;
171 try {
172 methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_METHODS, className + UtilConstants.BUILDER);
173 } catch (ClassNotFoundException | IOException e) {
174 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
175 throw new IOException("Fail to read data from temp file.");
176 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530177
178 /**
179 * Add default constructor and build method impl.
180 */
181 methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530182 + MethodsGenerator.getDefaultConstructorString(BUILDER_CLASS_MASK, className));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530183 methods.add(MethodsGenerator.getBuildString(className));
184
185 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530186 * Add methods in builder class.
187 */
188 for (String method : methods) {
189 appendMethod(file, method + UtilConstants.NEW_LINE);
190 }
191 return file;
192 }
193
194 /**
195 * Returns generated impl class file for current node.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530196 *
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530197 * @param file file
198 * @param className class name
199 * @param pkg generated file package
200 * @param attrList attribute's info
201 * @return impl class file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530202 * @throws IOException when fails to write in file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530203 */
204 public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList)
205 throws IOException {
206
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530207 initiateFile(file, className, IMPL_CLASS_MASK, null, pkg);
208
209 List<String> attributes;
210 try {
211 attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
212 } catch (ClassNotFoundException | IOException e) {
213 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
214 throw new IOException("Fail to read data from temp file.");
215 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530216
217 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530218 * Add attributes to the file.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530219 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530220 for (String attribute : attributes) {
221 insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
222 }
223 insert(file, UtilConstants.NEW_LINE);
224
225 List<String> methods;
226 try {
227 methods = TempDataStore.getTempData(TempDataStoreType.IMPL_METHODS, className + UtilConstants.IMPL);
228 } catch (ClassNotFoundException | IOException e) {
229 log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
230 throw new IOException("Fail to read data from temp file.");
231 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530232
233 /**
234 * Add default constructor and constructor methods.
235 */
236 methods.add(UtilConstants.JAVA_DOC_FIRST_LINE
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530237 + MethodsGenerator.getDefaultConstructorString(IMPL_CLASS_MASK, className));
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530238 methods.add(MethodsGenerator.getConstructorString(className));
239
240 /**
241 * Add methods in impl class.
242 */
243 for (String method : methods) {
244 appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
245 }
246 insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
247
248 return file;
249 }
250
251 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530252 * Initiate generation of file based on generated file type.
253 *
254 * @param file generated file
255 * @param className generated file class name
256 * @param type generated file type
257 * @param imports imports for the file
258 * @param pkg generated file package
259 * @throws IOException when fails to generate a file
260 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530261 private static void initiateFile(File file, String className, int type, List<String> imports,
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530262 String pkg) throws IOException {
263 try {
264 file.createNewFile();
265 appendContents(file, className, type, imports, pkg);
266 } catch (IOException e) {
267 throw new IOException("Failed to create " + file.getName() + " class file.");
268 }
269 }
270
271 /**
272 * Appends the temp files to main files.
273 *
274 * @param appendFile temp file
275 * @param srcFile main file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530276 * @throws IOException when fails to append contents
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530277 */
278 public static void appendFileContents(File appendFile, File srcFile) throws IOException {
279 try {
280 FileSystemUtil.appendFileContents(appendFile, srcFile);
281 } catch (IOException e) {
282 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
283 }
284 }
285
286 /**
287 * Append methods to the generated files.
288 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530289 * @param file file in which method needs to be appended
290 * @param method method which needs to be appended
291 * @exception IOException file operation exceptions
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530292 */
293 private static void appendMethod(File file, String method) throws IOException {
294 insert(file, method);
295 }
296
297 /**
298 * Closes the current generated file.
299 *
300 * @param fileType generate file type
301 * @param yangName file name
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530302 * @return end of class definition string
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530303 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530304 public static String closeFile(int fileType, String yangName) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530305 return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
306 }
307
308 /**
309 * Parses attribute info and fetch specific data and creates serialized
310 * files of it.
311 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530312 * @param attr attribute info
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530313 * @param genFileType generated file type
314 * @param className class name
315 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530316 public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530317
318 String attrString = "";
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530319 String builderInterfaceMethodString = "";
320 String builderClassMethodString = "";
321 String implClassMethodString = "";
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530322 String getterString = "";
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530323 className = JavaIdentifierSyntax.getCaptialCase(className);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530324
325 try {
326 /*
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530327 * Get the attribute definition and save attributes to temporary
328 * file.
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530329 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530330 attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
331 attr.getImportInfo().getClassInfo(),
332 attr.getAttributeName());
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530333 TempDataStore.setTempData(attrString, TempDataStore.TempDataStoreType.ATTRIBUTE, className);
334
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530335 if ((genFileType & INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530336 getterString = MethodsGenerator.getGetterString(attr);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530337 TempDataStore.setTempData(getterString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
338 }
339
340 if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
341 builderInterfaceMethodString = MethodsGenerator.parseBuilderInterfaceMethodString(attr, className);
342 TempDataStore.setTempData(builderInterfaceMethodString,
343 TempDataStore.TempDataStoreType.BUILDER_INTERFACE_METHODS,
344 className + UtilConstants.BUILDER + UtilConstants.INTERFACE);
345 }
346
347 if ((genFileType & BUILDER_CLASS_MASK) != 0) {
348 builderClassMethodString = MethodsGenerator.parseBuilderMethodString(attr, className);
349 TempDataStore.setTempData(builderClassMethodString, TempDataStore.TempDataStoreType.BUILDER_METHODS,
350 className + UtilConstants.BUILDER);
351 }
352
353 if ((genFileType & IMPL_CLASS_MASK) != 0) {
354 implClassMethodString = MethodsGenerator.parseImplMethodString(attr);
355 TempDataStore.setTempData(implClassMethodString, TempDataStore.TempDataStoreType.IMPL_METHODS,
356 className + UtilConstants.IMPL);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530357 }
358 } catch (IOException e) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530359 log.info("Failed to set data for " + attr.getAttributeName() + " in temp data files.");
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530360 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530361
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530362 }
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
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530371 * @param importsList list of java imports
372 * @throws IOException when fails to append contents
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530373 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530374 private static void appendContents(File file, String fileName, int type, List<String> importsList,
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530375 String pkg) throws IOException {
376
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530377 if ((type & IMPL_CLASS_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530378
379 write(file, fileName, type, JavaDocType.IMPL_CLASS);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530380 } else if ((type & BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530381
382 write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
383 } else {
384
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530385 if ((type & INTERFACE_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530386 insert(file, CopyrightHeader.getCopyrightHeader());
387 insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
388 if (importsList != null) {
389 insert(file, UtilConstants.NEW_LINE);
390 for (String imports : importsList) {
391 insert(file, imports);
392 }
393 insert(file, UtilConstants.NEW_LINE);
394 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530395 write(file, fileName, type, JavaDocType.INTERFACE);
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530396 } else if ((type & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530397 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 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530406 write(file, fileName, type, JavaDocType.BUILDER_CLASS);
407 }
408 }
409 }
410
411 /**
412 * Write data to the specific generated file.
413 *
414 * @param file generated file
415 * @param fileName file name
416 * @param genType generated file type
417 * @param javaDocType java doc type
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530418 * @throws IOException when fails to write into a file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530419 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530420 private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType)
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530421 throws IOException {
422
423 insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName));
424 insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
425 }
426
427 /**
428 * Insert in the generated file.
429 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530430 * @param file file in which need to be inserted
431 * @param data data which need to be inserted
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530432 * @throws IOException when fails to insert into file
433 */
434 public static void insert(File file, String data) throws IOException {
435 try {
436 FileSystemUtil.insertStringInFile(file, data);
437 } catch (IOException e) {
438 throw new IOException("Failed to insert in " + file + "file");
439 }
440 }
441
442 /**
443 * Removes temp files.
444 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530445 * @param file file to be removed
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530446 */
447 public static void clean(File file) {
448 if (file.exists()) {
449 file.delete();
450 }
451 }
452}