[ONOS-3908] YANG container translator.
Change-Id: I4e239509df747238905ca0995f41019679093627
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangContainer.java b/src/main/java/org/onosproject/yangutils/datamodel/YangContainer.java
index c38bbbc..99baf4a 100644
--- a/src/main/java/org/onosproject/yangutils/datamodel/YangContainer.java
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangContainer.java
@@ -26,6 +26,7 @@
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
+import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/*-
* Reference RFC 6020.
@@ -472,17 +473,21 @@
@Override
public void generateJavaCodeEntry() throws IOException {
YangNode parent = getParent();
- String modPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), getName());
- setPackage(modPkg);
+ String contPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
+ setPackage(contPkg);
CachedFileHandle handle = null;
try {
- FileSystemUtil.createPackage(getPackage(), getName());
+ FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName());
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
+ handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/"));
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
+
+ addLeavesAttributes();
+ addLeafListAttributes();
addAttributeInParent();
}
@@ -498,8 +503,6 @@
@Override
public void generateJavaCodeExit() throws IOException {
- addLeavesAttributes();
- addLeafListAttributes();
getFileHandle().close();
return;
}
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/YangModule.java b/src/main/java/org/onosproject/yangutils/datamodel/YangModule.java
index 836ad77..8eda617 100644
--- a/src/main/java/org/onosproject/yangutils/datamodel/YangModule.java
+++ b/src/main/java/org/onosproject/yangutils/datamodel/YangModule.java
@@ -26,6 +26,7 @@
import org.onosproject.yangutils.translator.CodeGenerator;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
+import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/*-
@@ -558,18 +559,19 @@
CachedFileHandle handle = null;
try {
- FileSystemUtil.createPackage(getPackage(), getName());
+ FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName());
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
+ handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/"));
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
+ addLeavesAttributes();
+ addLeafListAttributes();
}
@Override
public void generateJavaCodeExit() throws IOException {
- addLeavesAttributes();
- addLeafListAttributes();
getFileHandle().close();
return;
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/CachedFileHandle.java b/src/main/java/org/onosproject/yangutils/translator/CachedFileHandle.java
index b73b821..4d7e51b 100644
--- a/src/main/java/org/onosproject/yangutils/translator/CachedFileHandle.java
+++ b/src/main/java/org/onosproject/yangutils/translator/CachedFileHandle.java
@@ -49,4 +49,11 @@
* @param pkg child's package path
*/
void setChildsPackage(String pkg);
+
+ /**
+ * Sets directory package path for code generation.
+ *
+ * @param filePath directory package path for code generation
+ */
+ void setFilePath(String filePath);
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/AttributeInfo.java b/src/main/java/org/onosproject/yangutils/translator/tojava/AttributeInfo.java
index c61c96c..f2c1562 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/AttributeInfo.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/AttributeInfo.java
@@ -19,6 +19,7 @@
import java.io.Serializable;
import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
/**
* Maintains the attribute info corresponding to class/interface generated.
@@ -71,7 +72,10 @@
* @param type the data type info of attribute.
*/
public void setAttributeType(YangType<?> type) {
- attrType = type;
+
+ if (type != null) {
+ attrType = AttributesJavaDataType.getJavaDataType(type);
+ }
}
/**
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java b/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java
index ac917aa..c6fcea4 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java
@@ -27,26 +27,18 @@
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.GeneratedFileType;
+import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
import org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen;
+import org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator;
import org.onosproject.yangutils.utils.UtilConstants;
-import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
-import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
-import org.onosproject.yangutils.utils.io.impl.SerializedDataStore;
-import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
-import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
-
-import static org.slf4j.LoggerFactory.getLogger;
-import org.slf4j.Logger;
/**
* Maintain the information about the java file to be generated.
*/
public class CachedJavaFileHandle implements CachedFileHandle {
- private static final Logger log = getLogger(CachedJavaFileHandle.class);
-
private static final int MAX_CACHABLE_ATTR = 64;
private static final String JAVA_FILE_EXTENSION = ".java";
private static final String TEMP_FILE_EXTENSION = ".tmp";
@@ -58,12 +50,6 @@
private GeneratedFileType genFileTypes;
/**
- * The type(s) of java method to be generated when the cached file handle is
- * closed.
- */
- private GeneratedMethodTypes genMethodTypes;
-
- /**
* Java package in which the class/interface needs to be generated.
*/
private String pkg;
@@ -90,9 +76,14 @@
private List<AttributeInfo> attributeList;
/**
+ * File generation directory path.
+ */
+ private String filePath;
+
+ /**
* Prevent invoking default constructor.
*/
- private CachedJavaFileHandle() {
+ public CachedJavaFileHandle() {
setCachedAttributeList(new LinkedList<AttributeInfo>());
}
@@ -106,16 +97,9 @@
* @throws IOException file IO exception.
*/
public CachedJavaFileHandle(String pcg, String yangName, GeneratedFileType types) throws IOException {
- if ((new File(pcg).exists())) {
- setGeneratedFileTypes(types);
- setPackage(pcg);
- setYangName(yangName);
- } else {
- FileSystemUtil.createPackage(pcg, yangName);
- setGeneratedFileTypes(types);
- setPackage(pcg);
- setYangName(yangName);
- }
+ setGeneratedFileTypes(types);
+ setPackage(pcg);
+ setYangName(yangName);
}
/**
@@ -185,11 +169,6 @@
return childsPkg;
}
- /**
- * Set the java package.
- *
- * @param pcg the package to set
- */
@Override
public void setChildsPackage(String pcg) {
childsPkg = pcg;
@@ -251,13 +230,27 @@
attributeList = attrList;
}
+ @Override
+ public void setFilePath(String path) {
+ filePath = path;
+ }
+
+ /**
+ * Set the cached attribute list.
+ *
+ * @param attrList attribute list.
+ */
+ private String getFilePath() {
+ return filePath;
+ }
+
/**
* Flush the cached attribute list to the serialized file.
*/
- private void flushCacheAttrToSerFile() {
+ private void flushCacheAttrToSerFile(String className) {
for (AttributeInfo attr : getCachedAttributeList()) {
- parseAttributeInfo(attr);
+ JavaFileGenerator.parseAttributeInfo(attr, getGeneratedFileTypes(), className);
}
/*
@@ -279,10 +272,6 @@
AttributeInfo newAttr = new AttributeInfo();
if (attrType != null) {
- attrType.setDataTypeName(attrType.getDataTypeName().replace("\"", ""));
- if (attrType.getDataTypeName().equals("string")) {
- attrType.setDataTypeName(JavaIdentifierSyntax.getCaptialCase(attrType.getDataTypeName()));
- }
newAttr.setAttributeType(attrType);
} else {
ImportInfo importInfo = new ImportInfo();
@@ -301,6 +290,10 @@
newAttr.setAttributeName(name);
newAttr.setListAttr(isListAttr);
+ if (newAttr.isListAttr()) {
+ newAttr.setAttributeType(AttributesJavaDataType.getListString(newAttr));
+ }
+
if (isListAttr) {
String listImport = UtilConstants.COLLECTION_IMPORTS + UtilConstants.LIST + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
@@ -317,7 +310,7 @@
if (getCachedAttributeList() != null) {
if (getCachedAttributeList().size() == MAX_CACHABLE_ATTR) {
- flushCacheAttrToSerFile();
+ flushCacheAttrToSerFile(getYangName());
}
getCachedAttributeList().add(newAttr);
} else {
@@ -361,89 +354,19 @@
String className = getYangName();
className = JavaIdentifierSyntax.getCaptialCase(className);
- String packagePath = getPackage();
- String filePath = UtilConstants.YANG_GEN_DIR + packagePath.replace(".", "/");
+ String filePath = getFilePath();
GeneratedFileType fileType = getGeneratedFileTypes();
- /**
- * Create interface file.
- */
- String interfaceFileName = className + JAVA_FILE_EXTENSION;
- File interfaceFile = new File(filePath + File.separator + interfaceFileName);
-
- /**
- * Create temp builder interface file.
- */
- String builderInterfaceFileName = interfaceFileName + TEMP_FILE_EXTENSION;
- File builderInterfaceFile = new File(filePath + File.separator + builderInterfaceFileName);
-
- /**
- * Create builder class file.
- */
- String builderFileName = className + UtilConstants.BUILDER + JAVA_FILE_EXTENSION;
- File builderFile = new File(filePath + File.separator + builderFileName);
- MethodsGenerator.setBuilderClassName(className + UtilConstants.BUILDER);
-
- /**
- * Create temp impl class file.
- */
-
- String implFileName = className + UtilConstants.IMPL + TEMP_FILE_EXTENSION;
- File implTempFile = new File(filePath + File.separator + implFileName);
-
/*
* TODO: add the file header using
* JavaCodeSnippetGen.getFileHeaderComment
*/
- List<String> attributes = new LinkedList<>();
- List<String> interfaceMethods = new LinkedList<>();
- List<String> builderInterfaceMethods = new LinkedList<>();
- List<String> builderClassMethods = new LinkedList<>();
- List<String> implClassMethods = new LinkedList<>();
List<String> imports = new LinkedList<>();
- try {
- attributes = SerializedDataStore.getSerializeData(SerializedDataStore.SerializedDataStoreType.ATTRIBUTE);
-
- interfaceMethods = SerializedDataStore
- .getSerializeData(SerializedDataStore.SerializedDataStoreType.INTERFACE_METHODS);
-
- builderInterfaceMethods = SerializedDataStore
- .getSerializeData(SerializedDataStore.SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
-
- builderClassMethods = SerializedDataStore
- .getSerializeData(SerializedDataStore.SerializedDataStoreType.BUILDER_METHODS);
-
- implClassMethods = SerializedDataStore
- .getSerializeData(SerializedDataStore.SerializedDataStoreType.IMPL_METHODS);
-
- imports = SerializedDataStore.getSerializeData(SerializedDataStore.SerializedDataStoreType.IMPORT);
- } catch (ClassNotFoundException | IOException e) {
- log.info("There is no attribute info of " + className + " YANG file in the serialized files.");
- }
-
if (getCachedAttributeList() != null) {
MethodsGenerator.setAttrInfo(getCachedAttributeList());
for (AttributeInfo attr : getCachedAttributeList()) {
- if (attr.isListAttr()) {
- String listString = JavaCodeSnippetGen.getListAttribute(attr.getAttributeType().getDataTypeName());
- @SuppressWarnings("rawtypes")
- YangType<?> type = new YangType();
- type.setDataTypeName(listString);
- attr.setAttributeType(type);
- }
-
- attributes.add(getAttributeString(attr));
-
- interfaceMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.INTERFACE));
-
- builderClassMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_CLASS));
-
- builderInterfaceMethods
- .add(MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_INTERFACE));
-
- implClassMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.IMPL));
if (getImportSet() != null) {
imports = new ArrayList<>(getImportSet());
@@ -451,288 +374,73 @@
}
}
- builderInterfaceMethods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className));
- builderClassMethods.add(UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
- + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.BUILDER_CLASS, className));
- builderClassMethods.add(MethodsGenerator.getBuildString(className));
-
- implClassMethods.add(UtilConstants.JAVA_DOC_FIRST_LINE
- + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.IMPL, className));
- implClassMethods.add(MethodsGenerator.getConstructorString(className));
-
/**
* Start generation of files.
*/
if (fileType.equals(GeneratedFileType.INTERFACE) || fileType.equals(GeneratedFileType.ALL)) {
- initiateFile(interfaceFile, className, GeneratedFileType.INTERFACE, imports);
+
+ /**
+ * Create interface file.
+ */
+ String interfaceFileName = className;
+ File interfaceFile = new File(filePath + File.separator + interfaceFileName + JAVA_FILE_EXTENSION);
+ interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports,
+ getCachedAttributeList(), getPackage());
+
+ /**
+ * Create temp builder interface file.
+ */
+ String builderInterfaceFileName = className + UtilConstants.BUILDER + UtilConstants.INTERFACE;
+ File builderInterfaceFile = new File(
+ filePath + File.separator + builderInterfaceFileName + TEMP_FILE_EXTENSION);
+ builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className,
+ getPackage(), getCachedAttributeList());
+
+ /**
+ * Append builder interface file to interface file and close it.
+ */
+ JavaFileGenerator.appendFileContents(builderInterfaceFile, interfaceFile);
+ JavaFileGenerator.insert(interfaceFile,
+ JavaFileGenerator.closeFile(GeneratedFileType.INTERFACE, interfaceFileName));
+
+ /**
+ * Remove temp files.
+ */
+ JavaFileGenerator.clean(builderInterfaceFile);
}
if (fileType.equals(GeneratedFileType.BUILDER_CLASS) || fileType.equals(GeneratedFileType.ALL)) {
- initiateFile(builderFile, className, GeneratedFileType.BUILDER_CLASS, imports);
- }
- if (fileType.equals(GeneratedFileType.IMPL) || fileType.equals(GeneratedFileType.ALL)) {
- initiateFile(implTempFile, className, GeneratedFileType.IMPL, imports);
- }
-
- if (fileType.equals(GeneratedFileType.BUILDER_INTERFACE) || fileType.equals(GeneratedFileType.ALL)) {
- initiateFile(builderInterfaceFile, className, GeneratedFileType.BUILDER_INTERFACE, imports);
- }
-
- /**
- * Add attributes to the file.
- */
- for (String attribute : attributes) {
- insert(builderFile, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
- insert(implTempFile, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
- }
-
- insert(builderFile, UtilConstants.NEW_LINE);
- insert(implTempFile, UtilConstants.NEW_LINE);
-
- /**
- * Add getter methods to interface file.
- */
- for (String method : interfaceMethods) {
- appendMethod(interfaceFile, method + UtilConstants.NEW_LINE);
- }
-
- /**
- * Add getters and setters in builder interface.
- */
- for (String method : builderInterfaceMethods) {
- appendMethod(builderInterfaceFile, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
- }
-
- insert(builderInterfaceFile, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
- /**
- * Add methods in builder class.
- */
- for (String method : builderClassMethods) {
- appendMethod(builderFile, method + UtilConstants.NEW_LINE);
- }
-
- /**
- * Add methods in impl class.
- */
- for (String method : implClassMethods) {
- appendMethod(implTempFile, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
- }
-
- insert(implTempFile, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
-
- /**
- * Append builder interface file to interface file and close it.
- */
- appendFileContents(builderInterfaceFile, interfaceFile);
- insert(interfaceFile, closeFile(GeneratedFileType.INTERFACE, interfaceFileName));
-
- /**
- * Append impl class to builder class and close it.
- */
- appendFileContents(implTempFile, builderFile);
- insert(builderFile, closeFile(GeneratedFileType.BUILDER_CLASS, builderFileName));
-
- /**
- * Remove temp files.
- */
- clean(implTempFile);
- clean(builderInterfaceFile);
- }
-
- /**
- * Initiate generation of file based on generated file type.
- *
- * @param file generated file
- * @param className generated file class name
- * @param type generated file type
- * @param imports imports for the file
- * @throws IOException when fails to generate a file
- */
- private void initiateFile(File file, String className, GeneratedFileType type, List<String> imports)
- throws IOException {
- try {
- file.createNewFile();
- appendContents(file, className, type, imports);
- } catch (IOException e) {
- throw new IOException("Failed to create " + file.getName() + " class file.");
- }
- }
-
- /**
- * Appends the temp files to main files.
- *
- * @param appendFile temp file
- * @param srcFile main file
- */
- private static void appendFileContents(File appendFile, File srcFile) throws IOException {
- try {
- FileSystemUtil.appendFileContents(appendFile, srcFile);
- } catch (IOException e) {
- throw new IOException("Failed to append " + appendFile + " in " + srcFile);
- }
- }
-
- /**
- * Append methods to the generated files.
- *
- * @param file file in which method needs to be appended.
- * @param method method which needs to be appended.
- */
- private static void appendMethod(File file, String method) throws IOException {
- insert(file, method);
- }
-
- /**
- * Closes the current generated file.
- *
- * @param fileType generate file type
- * @param yangName file name
- * @return end of class definition string.
- */
- private static String closeFile(GeneratedFileType fileType, String yangName) {
- return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
- }
-
- /**
- * Parses attribute info and fetch specific data and creates serialized
- * files of it.
- *
- * @param attr attribute info.
- */
- private void parseAttributeInfo(AttributeInfo attr) {
-
- String attrString = "";
- String methodString = "";
- String getterString = "";
-
- try {
- /*
- * Serialize attributes.
+ /**
+ * Create builder class file.
*/
- attrString = getAttributeString(attr);
- attrString = attrString.replace("\"", "");
- SerializedDataStore.setSerializeData(attrString, SerializedDataStore.SerializedDataStoreType.ATTRIBUTE);
+ String builderFileName = className + UtilConstants.BUILDER;
+ File builderFile = new File(filePath + File.separator + builderFileName + JAVA_FILE_EXTENSION);
+ MethodsGenerator.setBuilderClassName(className + UtilConstants.BUILDER);
- if (getGeneratedFileTypes().equals(GeneratedFileType.ALL)) {
+ builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports, getPackage(),
+ getCachedAttributeList());
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.INTERFACE);
- SerializedDataStore.setSerializeData(methodString,
- SerializedDataStore.SerializedDataStoreType.INTERFACE_METHODS);
+ /**
+ * Create temp impl class file.
+ */
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_CLASS);
- SerializedDataStore.setSerializeData(methodString,
- SerializedDataStore.SerializedDataStoreType.BUILDER_METHODS);
+ String implFileName = className + UtilConstants.IMPL;
+ File implTempFile = new File(filePath + File.separator + implFileName + TEMP_FILE_EXTENSION);
+ implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className, getPackage(),
+ getCachedAttributeList());
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_INTERFACE);
- SerializedDataStore.setSerializeData(methodString,
- SerializedDataStore.SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
+ /**
+ * Append impl class to builder class and close it.
+ */
+ JavaFileGenerator.appendFileContents(implTempFile, builderFile);
+ JavaFileGenerator.insert(builderFile,
+ JavaFileGenerator.closeFile(GeneratedFileType.BUILDER_CLASS, builderFileName));
- methodString = MethodsGenerator.getMethodString(attr, GeneratedFileType.IMPL);
- SerializedDataStore.setSerializeData(methodString,
- SerializedDataStore.SerializedDataStoreType.IMPL_METHODS);
-
- } else if (getGeneratedFileTypes().equals(GeneratedFileType.INTERFACE)) {
-
- getterString = MethodsGenerator.getGetterString(attr);
- SerializedDataStore.setSerializeData(methodString,
- SerializedDataStore.SerializedDataStoreType.INTERFACE_METHODS);
- }
- } catch (IOException e) {
- log.info("Failed to get data for " + attr.getAttributeName() + " from serialized files.");
- }
- }
-
- /**
- * Returns attribute string.
- *
- * @param attr attribute info
- * @return attribute string
- */
- private String getAttributeString(AttributeInfo attr) {
- return JavaCodeSnippetGen.getJavaAttributeInfo(getGeneratedFileTypes(), attr.getAttributeName(),
- attr.getAttributeType());
- }
-
- /**
- * Appends all the contents into a generated java file.
- *
- * @param file generated file
- * @param fileName generated file name
- * @param type generated file type
- */
- private void appendContents(File file, String fileName, GeneratedFileType type, List<String> importsList)
- throws IOException {
-
- if (type.equals(GeneratedFileType.IMPL)) {
-
- write(file, fileName, type, JavaDocType.IMPL_CLASS);
- } else if (type.equals(GeneratedFileType.BUILDER_INTERFACE)) {
-
- write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
- } else {
-
- // TODO: handle imports for attributes.
-
- if (type.equals(GeneratedFileType.INTERFACE)) {
- insert(file, CopyrightHeader.getCopyrightHeader());
- insert(file, "package" + UtilConstants.SPACE + getPackage() + UtilConstants.SEMI_COLAN
- + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE);
- for (String imports : importsList) {
- insert(file, imports);
- }
- insert(file, UtilConstants.NEW_LINE);
- write(file, fileName, type, JavaDocType.INTERFACE);
- } else if (type.equals(GeneratedFileType.BUILDER_CLASS)) {
- insert(file, CopyrightHeader.getCopyrightHeader());
- insert(file, "package" + UtilConstants.SPACE + getPackage() + UtilConstants.SEMI_COLAN
- + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE);
- for (String imports : importsList) {
- insert(file, imports);
- }
- insert(file, UtilConstants.NEW_LINE);
- write(file, fileName, type, JavaDocType.BUILDER_CLASS);
- }
- }
- }
-
- /**
- * Write data to the specific generated file.
- *
- * @param file generated file
- * @param fileName file name
- * @param genType generated file type
- * @param javaDocType java doc type
- */
- private static void write(File file, String fileName, GeneratedFileType genType, JavaDocGen.JavaDocType javaDocType)
- throws IOException {
-
- insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName));
- insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
- }
-
- /**
- * Insert in the generated file.
- *
- * @param file file in which need to be inserted.
- * @param data data which need to be inserted.
- */
- private static void insert(File file, String data) throws IOException {
- try {
- FileSystemUtil.insertStringInFile(file, data);
- } catch (IOException e) {
- throw new IOException("Failed to insert in " + file + "file");
- }
- }
-
- /**
- * Removes temp files.
- *
- * @param file file to be removed.
- */
- private static void clean(File file) {
- if (file.exists()) {
- file.delete();
+ /**
+ * Remove temp files.
+ */
+ JavaFileGenerator.clean(implTempFile);
}
}
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGenerator.java
index 50b0d06..0c70747 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGenerator.java
@@ -42,21 +42,22 @@
TraversalType curTraversal = TraversalType.ROOT;
while (!(curNode == null)) {
- if (curTraversal != TraversalType.PARENT || curTraversal == TraversalType.SIBILING) {
+ if (curTraversal != TraversalType.PARENT) {
curNode.generateJavaCodeEntry();
}
- if (curTraversal != TraversalType.PARENT && !(curNode.getChild() == null)) {
+ if (curTraversal != TraversalType.PARENT && (curNode.getChild() != null)) {
curTraversal = TraversalType.CHILD;
curNode = curNode.getChild();
- } else if (curTraversal == TraversalType.PARENT && !(curNode.getNextSibling() == null)) {
+ } else if ((curNode.getNextSibling() != null)) {
curNode.generateJavaCodeExit();
curTraversal = TraversalType.SIBILING;
curNode = curNode.getNextSibling();
} else {
- curNode.generateJavaCodeExit();
curTraversal = TraversalType.PARENT;
+ curNode.generateJavaCodeExit();
curNode = curNode.getParent();
}
}
+
}
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
new file mode 100644
index 0000000..e2d27ac
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.translator.tojava.AttributeInfo;
+import org.onosproject.yangutils.utils.UtilConstants;
+
+/**
+ * Provides java data types corresponding to YANG type.
+ */
+public final class AttributesJavaDataType {
+
+ /**
+ * Default constructor.
+ */
+ private AttributesJavaDataType() {
+ }
+
+ /**
+ * Returns YANG type.
+ *
+ * @param yangType YANG type
+ * @return YANG type
+ */
+ public static YangType<?> getJavaDataType(YangType<?> yangType) {
+ yangType.setDataTypeName(yangType.getDataTypeName().replace("\"", ""));
+ if (yangType.getDataType() != null) {
+ yangType.setDataTypeName(parseYangDataType(yangType.getDataType()));
+ }
+ return yangType;
+ }
+
+ /**
+ * Returns list string as attribute name when attribute is a list.
+ *
+ * @param attr attribute info.
+ * @return list attribute
+ */
+ @SuppressWarnings("rawtypes")
+ public static YangType<?> getListString(AttributeInfo attr) {
+ String listString = JavaCodeSnippetGen.getListAttribute(attr.getAttributeType().getDataTypeName());
+ YangType<?> type = new YangType();
+ type.setDataTypeName(listString);
+ attr.setAttributeType(type);
+ return type;
+ }
+
+ /**
+ * Parses YANG data type and returns corresponding java data type.
+ *
+ * @param type YANG data type
+ * @return java data type
+ */
+ private static String parseYangDataType(YangDataTypes type) {
+ if (type.equals(YangDataTypes.INT8)) {
+ return UtilConstants.BYTE;
+ } else if (type.equals(YangDataTypes.INT16)) {
+ return UtilConstants.SHORT;
+ } else if (type.equals(YangDataTypes.INT32)) {
+ return UtilConstants.INT;
+ } else if (type.equals(YangDataTypes.INT64)) {
+ return UtilConstants.LONG;
+ } else if (type.equals(YangDataTypes.UINT8)) {
+ return UtilConstants.SHORT;
+ } else if (type.equals(YangDataTypes.UINT16)) {
+ return UtilConstants.INT;
+ } else if (type.equals(YangDataTypes.UINT32)) {
+ return UtilConstants.LONG;
+ } else if (type.equals(YangDataTypes.UINT64)) {
+ //TODO: BIGINTEGER.
+ } else if (type.equals(YangDataTypes.DECIMAL64)) {
+ //TODO: DECIMAL64.
+ } else if (type.equals(YangDataTypes.STRING)) {
+ return UtilConstants.STRING;
+ } else if (type.equals(YangDataTypes.BOOLEAN)) {
+ return UtilConstants.BOOLEAN;
+ } else if (type.equals(YangDataTypes.ENUMERATION)) {
+ //TODO: ENUMERATION.
+ } else if (type.equals(YangDataTypes.BITS)) {
+ //TODO:BITS
+ } else if (type.equals(YangDataTypes.BINARY)) {
+ //TODO:BINARY
+ } else if (type.equals(YangDataTypes.LEAFREF)) {
+ //TODO:LEAFREF
+ } else if (type.equals(YangDataTypes.IDENTITYREF)) {
+ //TODO:IDENTITYREF
+ } else if (type.equals(YangDataTypes.EMPTY)) {
+ //TODO:EMPTY
+ } else if (type.equals(YangDataTypes.UNION)) {
+ //TODO:UNION
+ } else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
+ //TODO:INSTANCE_IDENTIFIER
+ } else if (type.equals(YangDataTypes.DERIVED)) {
+ //TODO:DERIVED
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
index a470aef..e8bade3 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
@@ -16,9 +16,6 @@
package org.onosproject.yangutils.translator.tojava.utils;
-import java.util.List;
-import java.util.SortedSet;
-
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
@@ -50,17 +47,6 @@
}
/**
- * reorder the import list based on the ONOS import rules.
- *
- * @param importInfo the set of classes/interfaces to be imported.
- * @return string of import info.
- */
- public List<ImportInfo> sortImportOrder(SortedSet<ImportInfo> importInfo) {
- /* TODO: reorder the import list based on the ONOS import rules. */
- return null;
- }
-
- /**
* Get the textual java code information corresponding to the import list.
*
* @param importInfo import info.
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
new file mode 100644
index 0000000..60d8c2a
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
@@ -0,0 +1,453 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+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.TempDataStoreType;
+
+import static org.slf4j.LoggerFactory.getLogger;
+import org.slf4j.Logger;
+
+public final class JavaFileGenerator {
+
+ private static final Logger log = getLogger(JavaFileGenerator.class);
+
+ /**
+ * Default constructor.
+ */
+ private JavaFileGenerator() {
+ }
+
+ /**
+ * 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.
+ */
+ 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);
+
+ /**
+ * Add getter methods to interface file.
+ */
+ for (String method : methods) {
+ appendMethod(file, method);
+ }
+ return file;
+ }
+
+ /**
+ * 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.
+ */
+ 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);
+
+ /**
+ * Add build method to builder interface file.
+ */
+ methods.add(MethodsGenerator.parseBuilderInterfaceBuildMethodString(className));
+
+ /**
+ * Add getters and setters in builder interface.
+ */
+ for (String method : methods) {
+ appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
+ }
+
+ insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
+ return file;
+ }
+
+ /**
+ * 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.
+ */
+ 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);
+
+ /**
+ * 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));
+ methods.add(MethodsGenerator.getBuildString(className));
+
+ /**
+ * Add attribute strings.
+ */
+ addAttributeSring(file, className, attrList, GeneratedFileType.BUILDER_CLASS);
+
+ /**
+ * Add methods in builder class.
+ */
+ for (String method : methods) {
+ appendMethod(file, method + UtilConstants.NEW_LINE);
+ }
+ return file;
+ }
+
+ /**
+ * 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.
+ */
+ 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);
+
+ /**
+ * Add attributes.
+ */
+ addAttributeSring(file, className, attrList, GeneratedFileType.IMPL);
+
+ /**
+ * Add default constructor and constructor methods.
+ */
+ methods.add(UtilConstants.JAVA_DOC_FIRST_LINE
+ + MethodsGenerator.getDefaultConstructorString(GeneratedFileType.IMPL, className));
+ methods.add(MethodsGenerator.getConstructorString(className));
+
+ /**
+ * Add methods in impl class.
+ */
+ for (String method : methods) {
+ appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
+ }
+ insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
+
+ return file;
+ }
+
+ /**
+ * 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
+ * @param className generated file class name
+ * @param type generated file type
+ * @param imports imports for the file
+ * @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,
+ String pkg) throws IOException {
+ try {
+ file.createNewFile();
+ appendContents(file, className, type, imports, pkg);
+ } catch (IOException e) {
+ throw new IOException("Failed to create " + file.getName() + " class file.");
+ }
+ }
+
+ /**
+ * Appends the temp files to main files.
+ *
+ * @param appendFile temp file
+ * @param srcFile main file
+ * @throws IOException when fails to append contents.
+ */
+ public static void appendFileContents(File appendFile, File srcFile) throws IOException {
+ try {
+ FileSystemUtil.appendFileContents(appendFile, srcFile);
+ } catch (IOException e) {
+ throw new IOException("Failed to append " + appendFile + " in " + srcFile);
+ }
+ }
+
+ /**
+ * Append methods to the generated files.
+ *
+ * @param file file in which method needs to be appended.
+ * @param method method which needs to be appended.
+ */
+ private static void appendMethod(File file, String method) throws IOException {
+ insert(file, method);
+ }
+
+ /**
+ * Closes the current generated file.
+ *
+ * @param fileType generate file type
+ * @param yangName file name
+ * @return end of class definition string.
+ */
+ public static String closeFile(GeneratedFileType fileType, String yangName) {
+ return JavaCodeSnippetGen.getJavaClassDefClose(fileType, yangName);
+ }
+
+ /**
+ * Parses attribute info and fetch specific data and creates serialized
+ * files of it.
+ *
+ * @param attr attribute info.
+ * @param genFileType generated file type
+ * @param className class name
+ */
+ public static void parseAttributeInfo(AttributeInfo attr, GeneratedFileType genFileType, String className) {
+
+ String attrString = "";
+ String methodString = "";
+ String getterString = "";
+
+ try {
+ /*
+ * Serialize attributes.
+ */
+ attrString = getAttributeString(attr, genFileType);
+ attrString = attrString.replace("\"", "");
+ 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)) {
+
+ getterString = MethodsGenerator.getGetterString(attr);
+ TempDataStore.setTempData(methodString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
+ }
+ } catch (IOException e) {
+ log.info("Failed to get data for " + attr.getAttributeName() + " from serialized 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());
+ }
+
+ /**
+ * Appends all the contents into a generated java file.
+ *
+ * @param file generated file
+ * @param fileName generated file name
+ * @param type generated file type
+ * @param pkg generated file package
+ * @throws IOException when fails to append contents.
+ */
+ private static void appendContents(File file, String fileName, GeneratedFileType type, List<String> importsList,
+ String pkg) throws IOException {
+
+ if (type.equals(GeneratedFileType.IMPL)) {
+
+ write(file, fileName, type, JavaDocType.IMPL_CLASS);
+ } else if (type.equals(GeneratedFileType.BUILDER_INTERFACE)) {
+
+ write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
+ } else {
+
+ if (type.equals(GeneratedFileType.INTERFACE)) {
+ insert(file, CopyrightHeader.getCopyrightHeader());
+ insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
+ if (importsList != null) {
+ insert(file, UtilConstants.NEW_LINE);
+ for (String imports : importsList) {
+ insert(file, imports);
+ }
+ insert(file, UtilConstants.NEW_LINE);
+ }
+ insert(file, UtilConstants.NEW_LINE);
+ write(file, fileName, type, JavaDocType.INTERFACE);
+ } else if (type.equals(GeneratedFileType.BUILDER_CLASS)) {
+ insert(file, CopyrightHeader.getCopyrightHeader());
+ insert(file, "package" + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE);
+ if (importsList != null) {
+ insert(file, UtilConstants.NEW_LINE);
+ for (String imports : importsList) {
+ insert(file, imports);
+ }
+ insert(file, UtilConstants.NEW_LINE);
+ }
+ insert(file, UtilConstants.NEW_LINE);
+ write(file, fileName, type, JavaDocType.BUILDER_CLASS);
+ }
+ }
+ }
+
+ /**
+ * Write data to the specific generated file.
+ *
+ * @param file generated file
+ * @param fileName file name
+ * @param genType generated file type
+ * @param javaDocType java doc type
+ * @throws IOException when fails to write into a file.
+ */
+ private static void write(File file, String fileName, GeneratedFileType genType, JavaDocGen.JavaDocType javaDocType)
+ throws IOException {
+
+ insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName));
+ insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
+ }
+
+ /**
+ * Insert in the generated file.
+ *
+ * @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 {
+ try {
+ FileSystemUtil.insertStringInFile(file, data);
+ } catch (IOException e) {
+ throw new IOException("Failed to insert in " + file + "file");
+ }
+ }
+
+ /**
+ * Removes temp files.
+ *
+ * @param file file to be removed.
+ */
+ public static void clean(File file) {
+ if (file.exists()) {
+ file.delete();
+ }
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
index cd26056..58005b1 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
@@ -50,7 +50,7 @@
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + getYangRevisionStr(revision);
- return pkg;
+ return pkg.toLowerCase();
}
/**
@@ -126,11 +126,11 @@
* Get the package from parent's package and string.
*
* @param parentPkg parent's package.
- * @param childName child's name.
+ * @param parentName parent's name.
* @return package string.
*/
- public static String getPackageFromParent(String parentPkg, String childName) {
- return parentPkg + UtilConstants.PERIOD + getSubPkgFromName(childName);
+ public static String getPackageFromParent(String parentPkg, String parentName) {
+ return (parentPkg + UtilConstants.PERIOD + getSubPkgFromName(parentName)).toLowerCase();
}
/**
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
index 23b4afb..8fdaf5f 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
@@ -109,6 +109,7 @@
* @param returnType return type of method
* @return constructed method impl
*/
+ @SuppressWarnings("rawtypes")
public static String constructMethodInfo(GeneratedFileType genFileTypes, String yangName,
GeneratedMethodTypes methodTypes, YangType<?> returnType) {
@@ -187,8 +188,7 @@
attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType());
- return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString
- + UtilConstants.NEW_LINE;
+ return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString;
}
/**
@@ -201,8 +201,7 @@
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
+ UtilConstants.NEW_LINE + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
- attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType())
- + UtilConstants.NEW_LINE;
+ attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
}
/**
@@ -400,8 +399,7 @@
}
getAttrInfo().clear();
}
- return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET
- + UtilConstants.NEW_LINE;
+ return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
index 58dfb69..8a537f5 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
@@ -54,7 +54,6 @@
return false;
}
-
/**
* Create a package structure with package info java file if not present.
*
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
index 19a130e..4be5037 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
@@ -170,8 +170,8 @@
* @return javaDocs.
*/
private static String generateForBuilderClass(String className) {
- return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC
- + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
+ return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC + className
+ + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
}
/**
@@ -181,8 +181,8 @@
* @return javaDocs.
*/
private static String generateForInterface(String interfaceName) {
- return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC
- + interfaceName + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
+ return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC + interfaceName
+ + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
}
/**
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java
deleted file mode 100644
index 9ceb50f..0000000
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStore.java
+++ /dev/null
@@ -1,210 +0,0 @@
-/*
- * Copyright 2016 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.onosproject.yangutils.utils.io.impl;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInput;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutput;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Provides storage for serialized data while traversing data model tree for code generation.
- */
-public final class SerializedDataStore {
-
- /**
- * Data Store types.
- */
- public static enum SerializedDataStoreType {
-
- /**
- * Methods.
- */
- INTERFACE_METHODS,
-
- /**
- * Methods.
- */
- BUILDER_METHODS,
-
- /**
- * Methods.
- */
- BUILDER_INTERFACE_METHODS,
-
- /**
- * Methods.
- */
- IMPL_METHODS,
-
- /**
- * Attributes.
- */
- ATTRIBUTE,
-
- /**
- * Imports.
- */
- IMPORT
- }
-
- /**
- * File name string for serialized files of methods.
- */
- private static final String INTERFACE_METHOD_FILE_NAME = "SerializedInterfaceMethodDataStore";
-
- /**
- * File name string for serialized files of methods.
- */
- private static final String BUILDER_METHOD_FILE_NAME = "SerializedBuilderMethodDataStore";
-
- /**
- * File name string for serialized files of methods.
- */
- private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "SerializedBuilderInterfaceMethodDataStore";
-
- /**
- * File name string for serialized files of methods.
- */
- private static final String IMPL_METHOD_FILE_NAME = "SerializedImplMethodDataStore";
-
- /**
- * File name string for serialized files of attributes.
- */
- private static final String ATTRIBUTE_FILE_NAME = "SerializedAttributeDataStore";
-
- /**
- * File name string for serialized files of imports.
- */
- private static final String IMPORT_FILE_NAME = "SerializedImportDataStore";
-
- /**
- * File extension of serialized files.
- */
- private static final String SERIALIZE_FILE_EXTENSION = ".ser";
-
- /**
- * Directory for generating Serialized files.
- */
- private static final String GEN_DIR = "target/";
-
- /**
- * Buffer size.
- */
- private static final int BUFFER_SIZE = 8 * 1024;
-
- /**
- * Default constructor.
- */
- private SerializedDataStore() {
- }
-
- /**
- * Writes specific info to a serialized file.
- *
- * @param data data to be stored
- * @param type type of serialized data store
- * @throws IOException when fails to create a serialized data file.
- */
- public static void setSerializeData(String data, SerializedDataStoreType type) throws IOException {
-
- String fileName = "";
- if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
- fileName = ATTRIBUTE_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
- fileName = INTERFACE_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
- fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
- fileName = BUILDER_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
- fileName = IMPL_METHOD_FILE_NAME;
- } else {
- fileName = IMPORT_FILE_NAME;
- }
-
- try {
- OutputStream file = new FileOutputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
- OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
-
- ObjectOutput output = new ObjectOutputStream(buffer);
- try {
- output.writeObject(data);
- } finally {
- output.close();
- }
- } catch (IOException ex) {
- throw new IOException("failed to serialize data");
- }
- }
-
- /**
- * Get the serialized data.
- *
- * @param type type of serialized data store
- * @return list of attribute info.
- * @throws IOException when fails to read from the file.
- * @throws ClassNotFoundException when class is missing.
- * @throws FileNotFoundException when file is missing.
- */
- public static List<String> getSerializeData(SerializedDataStoreType type)
- throws IOException, FileNotFoundException, ClassNotFoundException {
-
- String fileName = "";
- if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
- fileName = ATTRIBUTE_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
- fileName = INTERFACE_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
- fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
- fileName = BUILDER_METHOD_FILE_NAME;
- } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
- fileName = IMPL_METHOD_FILE_NAME;
- } else {
- fileName = IMPORT_FILE_NAME;
- }
- try {
- InputStream file = new FileInputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
- InputStream buffer = new BufferedInputStream(file);
- ObjectInput input = new ObjectInputStream(buffer);
- try {
- String data = (String) input.readObject();
- List<String> recoveredData = new ArrayList<>();
- recoveredData.add(data);
- return recoveredData;
- } finally {
- input.close();
- file.close();
- }
- } catch (FileNotFoundException ex) {
- throw new FileNotFoundException("No such file or directory.");
- } catch (ClassNotFoundException ex) {
- throw new ClassNotFoundException("failed to fetch the serialized data file.");
- }
- }
-}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/TempDataStore.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/TempDataStore.java
new file mode 100644
index 0000000..63cba97
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/TempDataStore.java
@@ -0,0 +1,215 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.utils.io.impl;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Provides storage for Temp data while traversing data model tree for code generation.
+ */
+public final class TempDataStore {
+
+ /**
+ * Data Store types.
+ */
+ public static enum TempDataStoreType {
+
+ /**
+ * Methods.
+ */
+ GETTER_METHODS,
+
+ /**
+ * Methods.
+ */
+ BUILDER_METHODS,
+
+ /**
+ * Methods.
+ */
+ BUILDER_INTERFACE_METHODS,
+
+ /**
+ * Methods.
+ */
+ IMPL_METHODS,
+
+ /**
+ * Attributes.
+ */
+ ATTRIBUTE,
+
+ /**
+ * Imports.
+ */
+ IMPORT
+ }
+
+ /**
+ * File name string for Temp files of methods.
+ */
+ private static final String GETTER_METHOD_FILE_NAME = "TempGetterMethodDataStore";
+
+ /**
+ * File name string for Temp files of methods.
+ */
+ private static final String BUILDER_METHOD_FILE_NAME = "TempBuilderMethodDataStore";
+
+ /**
+ * File name string for Temp files of methods.
+ */
+ private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "TempBuilderInterfaceMethodDataStore";
+
+ /**
+ * File name string for Temp files of methods.
+ */
+ private static final String IMPL_METHOD_FILE_NAME = "TempImplMethodDataStore";
+
+ /**
+ * File name string for Temp files of attributes.
+ */
+ private static final String ATTRIBUTE_FILE_NAME = "TempAttributeDataStore";
+
+ /**
+ * File name string for Temp files of imports.
+ */
+ private static final String IMPORT_FILE_NAME = "TempImportDataStore";
+
+ /**
+ * File extension of Temp files.
+ */
+ private static final String FILE_EXTENSION = ".tmp";
+
+ /**
+ * Directory for generating Temp files.
+ */
+ private static final String GEN_DIR = "target/";
+
+ /**
+ * Buffer size.
+ */
+ private static final int BUFFER_SIZE = 8 * 1024;
+
+ /**
+ * Default constructor.
+ */
+ private TempDataStore() {
+ }
+
+ /**
+ * Writes specific info to a Temp file.
+ *
+ * @param data data to be stored
+ * @param type type of Temp data store
+ * @param className class name
+ * @throws IOException when fails to create a Temp data file.
+ */
+ public static void setTempData(String data, TempDataStoreType type, String className) throws IOException {
+
+ String fileName = "";
+ if (type.equals(TempDataStoreType.ATTRIBUTE)) {
+ fileName = ATTRIBUTE_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
+ fileName = GETTER_METHOD_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
+ fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
+ fileName = BUILDER_METHOD_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
+ fileName = IMPL_METHOD_FILE_NAME;
+ } else {
+ fileName = IMPORT_FILE_NAME;
+ }
+
+ File dir = new File(GEN_DIR + className + File.separator);
+ dir.mkdirs();
+ try {
+ OutputStream file = new FileOutputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
+ OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
+
+ ObjectOutput output = new ObjectOutputStream(buffer);
+ try {
+ output.writeObject(data);
+ } finally {
+ output.close();
+ }
+ } catch (IOException ex) {
+ throw new IOException("failed to serialize data");
+ }
+ }
+
+ /**
+ * Get the Temp data.
+ *
+ * @param type type of Temp data store
+ * @param className name of the class.
+ * @return list of attribute info.
+ * @throws IOException when fails to read from the file.
+ * @throws ClassNotFoundException when class is missing.
+ * @throws FileNotFoundException when file is missing.
+ */
+ public static List<String> getTempData(TempDataStoreType type, String className)
+ throws IOException, FileNotFoundException, ClassNotFoundException {
+
+ String fileName = "";
+ if (type.equals(TempDataStoreType.ATTRIBUTE)) {
+ fileName = ATTRIBUTE_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
+ fileName = GETTER_METHOD_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
+ fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
+ fileName = BUILDER_METHOD_FILE_NAME;
+ } else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
+ fileName = IMPL_METHOD_FILE_NAME;
+ } else {
+ fileName = IMPORT_FILE_NAME;
+ }
+ try {
+ InputStream file = new FileInputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
+ InputStream buffer = new BufferedInputStream(file);
+ ObjectInput input = new ObjectInputStream(buffer);
+ try {
+ String data = (String) input.readObject();
+ List<String> recoveredData = new ArrayList<>();
+ recoveredData.add(data);
+ return recoveredData;
+ } finally {
+ input.close();
+ file.close();
+ }
+ } catch (FileNotFoundException ex) {
+ throw new FileNotFoundException("No such file or directory.");
+ } catch (ClassNotFoundException ex) {
+ throw new ClassNotFoundException("failed to fetch the Temp data file.");
+ }
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
index 1f8a159..bb94b35 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangFileScanner.java
@@ -33,7 +33,6 @@
private YangFileScanner() {
}
-
/**
* Returns the list of java files.
*
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
index 8e97a7a..3357000 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
@@ -69,10 +69,9 @@
public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
if (pack.contains(UtilConstants.YANG_GEN_DIR)) {
- String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR + UtilConstants.SLASH);
- pack = strArray[1];
- }
-
+ String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR);
+ pack = strArray[1];
+ }
try {
File packageInfo = new File(path + File.separator + "package-info.java");
diff --git a/src/test/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandleTest.java b/src/test/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandleTest.java
new file mode 100644
index 0000000..9a62458
--- /dev/null
+++ b/src/test/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandleTest.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.translator.CachedFileHandle;
+import org.onosproject.yangutils.translator.GeneratedFileType;
+import org.onosproject.yangutils.utils.UtilConstants;
+import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
+import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
+
+/**
+ * Unit test case for cached java file handle.
+ */
+public class CachedJavaFileHandleTest {
+
+ private static final String DIR_PKG = "target/unit/cachedfile/";
+ private static final String PKG = "org.onosproject.unittest";
+ private static final String CHILD_PKG = "target/unit/cachedfile/child";
+ private static final String YANG_NAME = "Test1";
+ private static final GeneratedFileType GEN_TYPE = GeneratedFileType.ALL;
+
+ /**
+ * Unit test case for add attribute info.
+ *
+ * @throws IOException when fails to add an attribute.
+ */
+ @Test
+ public void testForAddAttributeInfo() throws IOException {
+
+ AttributeInfo attr = getAttr();
+ attr.setListAttr(false);
+ getFileHandle().addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
+ }
+
+ /**
+ * Unit test case for close of cached files.
+ *
+ * @throws IOException when fails to generate files.
+ */
+ @Test
+ public void testForClose() throws IOException {
+
+ CopyrightHeader.parseCopyrightHeader();
+
+ AttributeInfo attr = getAttr();
+ attr.setListAttr(false);
+ CachedFileHandle handle = getFileHandle();
+ handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
+ handle.close();
+
+ assertThat(true, is(getStubDir().exists()));
+ assertThat(true, is(getStubPkgInfo().exists()));
+ assertThat(true, is(getStubInterfaceFile().exists()));
+ assertThat(true, is(getStubBuilderFile().exists()));
+ }
+
+ /**
+ * Unit test case for setting child's package.
+ *
+ * @throws IOException when fails to add child's package
+ */
+ @Test
+ public void testForSetChildsPackage() throws IOException {
+
+ AttributeInfo attr = getAttr();
+ attr.setListAttr(false);
+ CachedFileHandle handle = getFileHandle();
+ handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
+
+ handle.setChildsPackage(CHILD_PKG);
+ }
+
+ /**
+ * Returns attribute info.
+ *
+ * @return attribute info
+ */
+ @SuppressWarnings("rawtypes")
+ private AttributeInfo getAttr() {
+ YangType<?> type = new YangType();
+ YangDataTypes dataType = YangDataTypes.STRING;
+
+ type.setDataTypeName("string");
+ type.setDataType(dataType);
+
+ AttributeInfo attr = new AttributeInfo();
+
+ attr.setAttributeName("testAttr");
+ attr.setAttributeType(type);
+ return attr;
+ }
+
+ /**
+ * Returns cached java file handle.
+ *
+ * @return java file handle.
+ */
+ private CachedFileHandle getFileHandle() throws IOException {
+ CopyrightHeader.parseCopyrightHeader();
+ FileSystemUtil.createPackage(DIR_PKG + File.separator + PKG, YANG_NAME);
+ CachedFileHandle fileHandle = FileSystemUtil.createSourceFiles(PKG, YANG_NAME, GEN_TYPE);
+ fileHandle.setFilePath(DIR_PKG + PKG.replace(".", "/"));
+
+ return fileHandle;
+ }
+
+ /**
+ * Returns stub directory file object.
+ *
+ * @return stub directory file
+ */
+ private File getStubDir() {
+ return new File(DIR_PKG);
+ }
+
+ /**
+ * Returns stub package-info file object.
+ *
+ * @return stub package-info file
+ */
+ private File getStubPkgInfo() {
+ return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator
+ + "package-info.java");
+ }
+
+ /**
+ * Returns stub interface file object.
+ *
+ * @return stub interface file
+ */
+ private File getStubInterfaceFile() {
+ return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
+ + ".java");
+ }
+
+ /**
+ * Returns stub builder file.
+ *
+ * @return stub builder file
+ */
+ private File getStubBuilderFile() {
+ return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
+ + "Builder.java");
+ }
+
+}
diff --git a/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java b/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java
new file mode 100644
index 0000000..bf73689
--- /dev/null
+++ b/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.tojava.utils;
+
+import org.junit.Test;
+import org.onosproject.yangutils.datamodel.YangDataTypes;
+import org.onosproject.yangutils.datamodel.YangType;
+import org.onosproject.yangutils.translator.GeneratedFileType;
+import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
+import org.onosproject.yangutils.translator.tojava.ImportInfo;
+import org.onosproject.yangutils.utils.UtilConstants;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Unit test cases for java code snippet generator.
+ */
+public class JavaCodeSnippetGenTest {
+
+ private static final String PKG_INFO = "org.onosproject.unittest";
+ private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
+ private static final GeneratedFileType FILE_GEN_TYPE = GeneratedFileType.INTERFACE;
+ private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER;
+ private static final String YANG_NAME = "Test";
+ private static final String STRING = "String";
+
+ /**
+ * Unit test case for import text.
+ */
+ @Test
+ public void testForImportText() {
+ ImportInfo importInfo = new ImportInfo();
+ importInfo.setPkgInfo(PKG_INFO);
+ importInfo.setClassInfo(CLASS_INFO);
+
+ String imports = JavaCodeSnippetGen.getImportText(importInfo);
+
+ assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO
+ + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
+ }
+
+ /**
+ * Unit test case for java class definition start.
+ */
+ @Test
+ public void testForJavaClassDefStart() {
+ String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
+ assertThat(true,
+ is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE
+ + UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ + UtilConstants.NEW_LINE)));
+ }
+
+ /**
+ * Unit test case for java attribute info.
+ */
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void testForJavaAttributeInfo() {
+
+ String attributeWithType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, getType());
+ assertThat(true, is(attributeWithType.equals(UtilConstants.PRIVATE + UtilConstants.SPACE
+ + getType().getDataTypeName() + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
+
+ String attributeWithoutType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, null);
+ assertThat(true,
+ is(attributeWithoutType.equals(
+ UtilConstants.PRIVATE + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(YANG_NAME)
+ + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
+
+ }
+
+ /**
+ * Unit test case for list attribute.
+ */
+ @Test
+ public void testForListAttribute() {
+ String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING);
+ assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING
+ + UtilConstants.DIAMOND_CLOSE_BRACKET)));
+ }
+
+ /**
+ * Unit test case for java method info.
+ */
+ @Test
+ public void testForJavaMethodInfo() {
+
+ String method = JavaCodeSnippetGen.getJavaMethodInfo(FILE_GEN_TYPE, YANG_NAME, METHOD_GEN_TYPE, getType());
+ assertThat(true,
+ is(method.equals(UtilConstants.FOUR_SPACE_INDENTATION
+ + JavaIdentifierSyntax.getCaptialCase(getType().getDataTypeName()) + UtilConstants.SPACE
+ + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(YANG_NAME)
+ + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS
+ + UtilConstants.SEMI_COLAN)));
+ }
+
+ /**
+ * Unit test case for java class definition close.
+ */
+ @Test
+ public void testForJavaClassDefClose() {
+ String classDef = JavaCodeSnippetGen.getJavaClassDefClose(FILE_GEN_TYPE, YANG_NAME);
+ assertThat(true, is(classDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
+ }
+
+ /**
+ * Returns YANG type.
+ *
+ * @return type
+ */
+ @SuppressWarnings("rawtypes")
+ private YangType<?> getType() {
+ YangType<?> type = new YangType();
+ type.setDataTypeName(STRING);
+ type.setDataType(YangDataTypes.STRING);
+ return type;
+ }
+}
diff --git a/src/test/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStoreTest.java b/src/test/java/org/onosproject/yangutils/utils/io/impl/TempDataStoreTest.java
similarity index 75%
rename from src/test/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStoreTest.java
rename to src/test/java/org/onosproject/yangutils/utils/io/impl/TempDataStoreTest.java
index 154dcdd..11e31c5 100644
--- a/src/test/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStoreTest.java
+++ b/src/test/java/org/onosproject/yangutils/utils/io/impl/TempDataStoreTest.java
@@ -19,7 +19,7 @@
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
-import org.onosproject.yangutils.utils.io.impl.SerializedDataStore.SerializedDataStoreType;
+import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -36,11 +36,12 @@
import static org.junit.Assert.assertNotNull;
/**
- * Unit tests for the serialized data store for its contents.
+ * Unit tests for the Tempd data store for its contents.
*/
-public final class SerializedDataStoreTest {
+public final class TempDataStoreTest {
private final Logger log = getLogger(getClass());
+ private static final String CLASS_NAME = "YANG";
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -59,7 +60,7 @@
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
- Class<?>[] classesToConstruct = {SerializedDataStore.class };
+ Class<?>[] classesToConstruct = {TempDataStore.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
@@ -74,12 +75,12 @@
public void insertAttributeDataTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String attributeData = "attribute content lists this";
- SerializedDataStore.setSerializeData(attributeData, SerializedDataStoreType.ATTRIBUTE);
- List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.ATTRIBUTE);
+ TempDataStore.setTempData(attributeData, TempDataStoreType.ATTRIBUTE, CLASS_NAME);
+ List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(attributeData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
- SerializedDataStoreType.valueOf(SerializedDataStoreType.ATTRIBUTE.toString());
+ TempDataStoreType.valueOf(TempDataStoreType.ATTRIBUTE.toString());
}
/**
@@ -89,10 +90,8 @@
public void insertBuilderInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String builderInterfaceMethodsData = "builder interface methods content lists this";
- SerializedDataStore.setSerializeData(builderInterfaceMethodsData,
- SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
- List<String> attributeInfo = SerializedDataStore
- .getSerializeData(SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
+ TempDataStore.setTempData(builderInterfaceMethodsData, TempDataStoreType.BUILDER_INTERFACE_METHODS, CLASS_NAME);
+ List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.BUILDER_INTERFACE_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(builderInterfaceMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
@@ -105,8 +104,8 @@
public void insertBuilderMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String builderMethodsData = "builder methods content lists this";
- SerializedDataStore.setSerializeData(builderMethodsData, SerializedDataStoreType.BUILDER_METHODS);
- List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.BUILDER_METHODS);
+ TempDataStore.setTempData(builderMethodsData, TempDataStoreType.BUILDER_METHODS, CLASS_NAME);
+ List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.BUILDER_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(builderMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
@@ -119,8 +118,8 @@
public void insertImplMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String implMethodsData = "impl methods content lists this";
- SerializedDataStore.setSerializeData(implMethodsData, SerializedDataStoreType.IMPL_METHODS);
- List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPL_METHODS);
+ TempDataStore.setTempData(implMethodsData, TempDataStoreType.IMPL_METHODS, CLASS_NAME);
+ List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.IMPL_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(implMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
@@ -133,8 +132,8 @@
public void insertImportTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String importData = "interface methods content lists this";
- SerializedDataStore.setSerializeData(importData, SerializedDataStoreType.IMPORT);
- List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPORT);
+ TempDataStore.setTempData(importData, TempDataStoreType.IMPORT, CLASS_NAME);
+ List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.IMPORT, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(importData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
@@ -147,8 +146,8 @@
public void insertInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String interfaceMethodsData = "interface methods content lists this";
- SerializedDataStore.setSerializeData(interfaceMethodsData, SerializedDataStoreType.INTERFACE_METHODS);
- List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.INTERFACE_METHODS);
+ TempDataStore.setTempData(interfaceMethodsData, TempDataStoreType.GETTER_METHODS, CLASS_NAME);
+ List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.GETTER_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(interfaceMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));