removed code duplication in translator, and addressed review comments
Change-Id: I27767a81c4bf279c80d2b98192f75f8f507b4457
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
index 60d8c2a..ff275ae 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
@@ -18,22 +18,27 @@
import java.io.File;
import java.io.IOException;
-import java.util.LinkedList;
import java.util.List;
-import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.AttributeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
-import org.onosproject.yangutils.utils.io.impl.TempDataStore;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
+import org.onosproject.yangutils.utils.io.impl.TempDataStore;
import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType;
-
-import static org.slf4j.LoggerFactory.getLogger;
import org.slf4j.Logger;
+import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_CLASS_MASK;
+import static org.onosproject.yangutils.translator.GeneratedFileType.BUILDER_INTERFACE_MASK;
+import static org.onosproject.yangutils.translator.GeneratedFileType.IMPL_CLASS_MASK;
+import static org.onosproject.yangutils.translator.GeneratedFileType.INTERFACE_MASK;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Generates java file.
+ */
public final class JavaFileGenerator {
private static final Logger log = getLogger(JavaFileGenerator.class);
@@ -45,21 +50,40 @@
}
/**
+ * Returns a file object for generated file.
+ *
+ * @param fileName file name
+ * @param filePath file package path
+ * @param extension file extension
+ * @return file object
+ */
+ public static File getFileObject(String filePath, String fileName, String extension) {
+ return new File(UtilConstants.YANG_GEN_DIR + filePath + File.separator + fileName + extension);
+ }
+
+ /**
* Returns generated interface file for current node.
+ *
* @param file file
* @param className class name
* @param imports imports for the file
* @param attrList attribute info
* @param pkg generated file package
* @return interface file
- * @throws IOException when fails to write in file.
+ * @throws IOException when fails to write in file
*/
public static File generateInterfaceFile(File file, String className, List<String> imports,
List<AttributeInfo> attrList, String pkg) throws IOException {
- initiateFile(file, className, GeneratedFileType.INTERFACE, imports, pkg);
- List<String> methods = getMethodStrings(TempDataStoreType.GETTER_METHODS, GeneratedFileType.INTERFACE,
- className, file, attrList);
+ initiateFile(file, className, INTERFACE_MASK, imports, pkg);
+
+ List<String> methods;
+ try {
+ methods = TempDataStore.getTempData(TempDataStoreType.GETTER_METHODS, className);
+ } catch (ClassNotFoundException | IOException e) {
+ log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+ throw new IOException("Fail to read data from temp file.");
+ }
/**
* Add getter methods to interface file.
@@ -72,19 +96,26 @@
/**
* Return generated builder interface file for current node.
+ *
* @param file file
* @param className class name
* @param pkg generated file package
* @param attrList attribute info
* @return builder interface file
- * @throws IOException when fails to write in file.
+ * @throws IOException when fails to write in file
*/
public static File generateBuilderInterfaceFile(File file, String className, String pkg,
List<AttributeInfo> attrList) throws IOException {
- initiateFile(file, className, GeneratedFileType.BUILDER_INTERFACE, null, pkg);
- List<String> methods = getMethodStrings(TempDataStoreType.BUILDER_INTERFACE_METHODS,
- GeneratedFileType.BUILDER_INTERFACE, className, file, attrList);
+ initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg);
+ List<String> methods;
+ try {
+ methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_INTERFACE_METHODS,
+ className + UtilConstants.BUILDER + UtilConstants.INTERFACE);
+ } catch (ClassNotFoundException | IOException e) {
+ log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+ throw new IOException("Fail to read data from temp file.");
+ }
/**
* Add build method to builder interface file.
@@ -104,34 +135,54 @@
/**
* Returns generated builder class file for current node.
+ *
* @param file file
* @param className class name
* @param imports imports for the file
* @param pkg generated file package
* @param attrList attribute info
* @return builder class file
- * @throws IOException when fails to write in file.
+ * @throws IOException when fails to write in file
*/
public static File generateBuilderClassFile(File file, String className, List<String> imports, String pkg,
List<AttributeInfo> attrList) throws IOException {
- initiateFile(file, className, GeneratedFileType.BUILDER_CLASS, imports, pkg);
- List<String> methods = getMethodStrings(TempDataStoreType.BUILDER_METHODS, GeneratedFileType.BUILDER_CLASS,
- className, file, attrList);
+ initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg);
+
+ /**
+ * Add attribute strings.
+ */
+ List<String> attributes;
+ try {
+ attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
+ } catch (ClassNotFoundException | IOException e) {
+ log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+ throw new IOException("Fail to read data from temp file.");
+ }
+ /**
+ * Add attributes to the file.
+ */
+ for (String attribute : attributes) {
+ insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
+ }
+ insert(file, UtilConstants.NEW_LINE);
+
+ List<String> methods;
+ try {
+ methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_METHODS, className + UtilConstants.BUILDER);
+ } catch (ClassNotFoundException | IOException e) {
+ log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+ throw new IOException("Fail to read data from temp file.");
+ }
/**
* Add default constructor and build method impl.
*/
methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
- + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.BUILDER_CLASS, className));
+ + MethodsGenerator.getDefaultConstructorString(BUILDER_CLASS_MASK, className));
methods.add(MethodsGenerator.getBuildString(className));
/**
- * Add attribute strings.
- */
- addAttributeSring(file, className, attrList, GeneratedFileType.BUILDER_CLASS);
-
- /**
* Add methods in builder class.
*/
for (String method : methods) {
@@ -142,30 +193,48 @@
/**
* Returns generated impl class file for current node.
+ *
* @param file file
* @param className class name
* @param pkg generated file package
* @param attrList attribute's info
* @return impl class file
- * @throws IOException when fails to write in file.
+ * @throws IOException when fails to write in file
*/
public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList)
throws IOException {
- initiateFile(file, className, GeneratedFileType.IMPL, null, pkg);
- List<String> methods = getMethodStrings(TempDataStoreType.IMPL_METHODS, GeneratedFileType.IMPL, className, file,
- attrList);
+ initiateFile(file, className, IMPL_CLASS_MASK, null, pkg);
+
+ List<String> attributes;
+ try {
+ attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
+ } catch (ClassNotFoundException | IOException e) {
+ log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+ throw new IOException("Fail to read data from temp file.");
+ }
/**
- * Add attributes.
+ * Add attributes to the file.
*/
- addAttributeSring(file, className, attrList, GeneratedFileType.IMPL);
+ for (String attribute : attributes) {
+ insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
+ }
+ insert(file, UtilConstants.NEW_LINE);
+
+ List<String> methods;
+ try {
+ methods = TempDataStore.getTempData(TempDataStoreType.IMPL_METHODS, className + UtilConstants.IMPL);
+ } catch (ClassNotFoundException | IOException e) {
+ log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+ throw new IOException("Fail to read data from temp file.");
+ }
/**
* Add default constructor and constructor methods.
*/
methods.add(UtilConstants.JAVA_DOC_FIRST_LINE
- + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.IMPL, className));
+ + MethodsGenerator.getDefaultConstructorString(IMPL_CLASS_MASK, className));
methods.add(MethodsGenerator.getConstructorString(className));
/**
@@ -180,74 +249,6 @@
}
/**
- * Adds attribute string for generated files.
- *
- * @param className class name
- * @param file generated file
- * @param attrList attribute info
- * @param genFileType generated file type
- * @param IOException when fails to add attributes in files.
- */
- private static void addAttributeSring(File file, String className, List<AttributeInfo> attrList,
- GeneratedFileType genFileType) throws IOException {
- List<String> attributes = new LinkedList<>();
- try {
- attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the serialized files.");
- }
-
- if (attrList != null) {
- MethodsGenerator.setAttrInfo(attrList);
- for (AttributeInfo attr : attrList) {
- if (attr.isListAttr()) {
- attr.setAttributeType(AttributesJavaDataType.getListString(attr));
- }
- attributes.add(getAttributeString(attr, genFileType));
- }
- }
-
- /**
- * Add attributes to the file.
- */
- for (String attribute : attributes) {
- insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
- }
- insert(file, UtilConstants.NEW_LINE);
- }
-
- /**
- * Returns method strings for generated files.
- *
- * @param dataStoreType temp data store file type.
- * @param genFileType generated file type
- * @param className generated file name
- * @param attrList attribute info
- * @return method strings
- */
- private static List<String> getMethodStrings(TempDataStoreType dataStoreType, GeneratedFileType genFileType,
- String className, File file, List<AttributeInfo> attrList) {
-
- List<String> methods = new LinkedList<>();
- try {
- methods = TempDataStore.getTempData(dataStoreType, className);
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the serialized files.");
- }
-
- if (attrList != null) {
- MethodsGenerator.setAttrInfo(attrList);
- for (AttributeInfo attr : attrList) {
- if (attr.isListAttr()) {
- attr.setAttributeType(AttributesJavaDataType.getListString(attr));
- }
- methods.add(MethodsGenerator.getMethodString(attr, genFileType));
- }
- }
- return methods;
- }
-
- /**
* Initiate generation of file based on generated file type.
*
* @param file generated file
@@ -257,7 +258,7 @@
* @param pkg generated file package
* @throws IOException when fails to generate a file
*/
- private static void initiateFile(File file, String className, GeneratedFileType type, List<String> imports,
+ private static void initiateFile(File file, String className, int type, List<String> imports,
String pkg) throws IOException {
try {
file.createNewFile();
@@ -272,7 +273,7 @@
*
* @param appendFile temp file
* @param srcFile main file
- * @throws IOException when fails to append contents.
+ * @throws IOException when fails to append contents
*/
public static void appendFileContents(File appendFile, File srcFile) throws IOException {
try {
@@ -285,8 +286,9 @@
/**
* Append methods to the generated files.
*
- * @param file file in which method needs to be appended.
- * @param method method which needs to be appended.
+ * @param file file in which method needs to be appended
+ * @param method method which needs to be appended
+ * @exception IOException file operation exceptions
*/
private static void appendMethod(File file, String method) throws IOException {
insert(file, method);
@@ -297,9 +299,9 @@
*
* @param fileType generate file type
* @param yangName file name
- * @return end of class definition string.
+ * @return end of class definition string
*/
- public static String closeFile(GeneratedFileType fileType, String yangName) {
+ public static String closeFile(int fileType, String yangName) {
return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
}
@@ -307,58 +309,56 @@
* Parses attribute info and fetch specific data and creates serialized
* files of it.
*
- * @param attr attribute info.
+ * @param attr attribute info
* @param genFileType generated file type
* @param className class name
*/
- public static void parseAttributeInfo(AttributeInfo attr, GeneratedFileType genFileType, String className) {
+ public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className) {
String attrString = "";
- String methodString = "";
+ String builderInterfaceMethodString = "";
+ String builderClassMethodString = "";
+ String implClassMethodString = "";
String getterString = "";
+ className = JavaIdentifierSyntax.getCaptialCase(className);
try {
/*
- * Serialize attributes.
+ * Get the attribute definition and save attributes to temporary
+ * file.
*/
- attrString = getAttributeString(attr, genFileType);
- attrString = attrString.replace("\"", "");
+ attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
+ attr.getImportInfo().getClassInfo(),
+ attr.getAttributeName());
TempDataStore.setTempData(attrString, TempDataStore.TempDataStoreType.ATTRIBUTE, className);
- if (genFileType.equals(GeneratedFileType.ALL)) {
-
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.INTERFACE);
- TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
-
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_CLASS);
- TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.BUILDER_METHODS, className);
-
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_INTERFACE);
- TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.BUILDER_INTERFACE_METHODS,
- className);
-
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.IMPL);
- TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.IMPL_METHODS, className);
-
- } else if (genFileType.equals(GeneratedFileType.INTERFACE)) {
-
+ if ((genFileType & INTERFACE_MASK) != 0) {
getterString = MethodsGenerator.getGetterString(attr);
- TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
+ TempDataStore.setTempData(getterString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
+ }
+
+ if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
+ builderInterfaceMethodString = MethodsGenerator.parseBuilderInterfaceMethodString(attr, className);
+ TempDataStore.setTempData(builderInterfaceMethodString,
+ TempDataStore.TempDataStoreType.BUILDER_INTERFACE_METHODS,
+ className + UtilConstants.BUILDER + UtilConstants.INTERFACE);
+ }
+
+ if ((genFileType & BUILDER_CLASS_MASK) != 0) {
+ builderClassMethodString = MethodsGenerator.parseBuilderMethodString(attr, className);
+ TempDataStore.setTempData(builderClassMethodString, TempDataStore.TempDataStoreType.BUILDER_METHODS,
+ className + UtilConstants.BUILDER);
+ }
+
+ if ((genFileType & IMPL_CLASS_MASK) != 0) {
+ implClassMethodString = MethodsGenerator.parseImplMethodString(attr);
+ TempDataStore.setTempData(implClassMethodString, TempDataStore.TempDataStoreType.IMPL_METHODS,
+ className + UtilConstants.IMPL);
}
} catch (IOException e) {
- log.info("Failed to get data for " + attr.getAttributeName() + " from serialized files.");
+ log.info("Failed to set data for " + attr.getAttributeName() + " in temp data files.");
}
- }
- /**
- * Returns attribute string.
- *
- * @param attr attribute info
- * @param genFileType generated file type
- * @return attribute string
- */
- private static String getAttributeString(AttributeInfo attr, GeneratedFileType genFileType) {
- return JavaCodeSnippetGen.getJavaAttributeInfo(genFileType, attr.getAttributeName(), attr.getAttributeType());
}
/**
@@ -368,20 +368,21 @@
* @param fileName generated file name
* @param type generated file type
* @param pkg generated file package
- * @throws IOException when fails to append contents.
+ * @param importsList list of java imports
+ * @throws IOException when fails to append contents
*/
- private static void appendContents(File file, String fileName, GeneratedFileType type, List<String> importsList,
+ private static void appendContents(File file, String fileName, int type, List<String> importsList,
String pkg) throws IOException {
- if (type.equals(GeneratedFileType.IMPL)) {
+ if ((type & IMPL_CLASS_MASK) != 0) {
write(file, fileName, type, JavaDocType.IMPL_CLASS);
- } else if (type.equals(GeneratedFileType.BUILDER_INTERFACE)) {
+ } else if ((type & BUILDER_INTERFACE_MASK) != 0) {
write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
} else {
- if (type.equals(GeneratedFileType.INTERFACE)) {
+ if ((type & INTERFACE_MASK) != 0) {
insert(file, CopyrightHeader.getCopyrightHeader());
insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
if (importsList != null) {
@@ -391,9 +392,8 @@
}
insert(file, UtilConstants.NEW_LINE);
}
- insert(file, UtilConstants.NEW_LINE);
write(file, fileName, type, JavaDocType.INTERFACE);
- } else if (type.equals(GeneratedFileType.BUILDER_CLASS)) {
+ } else if ((type & BUILDER_CLASS_MASK) != 0) {
insert(file, CopyrightHeader.getCopyrightHeader());
insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
if (importsList != null) {
@@ -403,7 +403,6 @@
}
insert(file, UtilConstants.NEW_LINE);
}
- insert(file, UtilConstants.NEW_LINE);
write(file, fileName, type, JavaDocType.BUILDER_CLASS);
}
}
@@ -416,9 +415,9 @@
* @param fileName file name
* @param genType generated file type
* @param javaDocType java doc type
- * @throws IOException when fails to write into a file.
+ * @throws IOException when fails to write into a file
*/
- private static void write(File file, String fileName, GeneratedFileType genType, JavaDocGen.JavaDocType javaDocType)
+ private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType)
throws IOException {
insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName));
@@ -428,8 +427,8 @@
/**
* Insert in the generated file.
*
- * @param file file in which need to be inserted.
- * @param data data which need to be inserted.
+ * @param file file in which need to be inserted
+ * @param data data which need to be inserted
* @throws IOException when fails to insert into file
*/
public static void insert(File file, String data) throws IOException {
@@ -443,7 +442,7 @@
/**
* Removes temp files.
*
- * @param file file to be removed.
+ * @param file file to be removed
*/
public static void clean(File file) {
if (file.exists()) {